From 4d117ca299c6162fad15dde6c8079bc3f527ef28 Mon Sep 17 00:00:00 2001
From: Sowren Sen <80112748+nemoitis@users.noreply.github.com>
Date: Mon, 1 May 2023 16:07:26 +0600
Subject: [PATCH 1/8] wip
---
src/SvgAvatarGenerator.php | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/SvgAvatarGenerator.php b/src/SvgAvatarGenerator.php
index 16c6c91..9a2fcd3 100755
--- a/src/SvgAvatarGenerator.php
+++ b/src/SvgAvatarGenerator.php
@@ -33,6 +33,9 @@ class SvgAvatarGenerator
*/
protected Shape $shape;
+ /**
+ * Corner radius of Rectangle shape.
+ */
protected int $cornerRadius;
/**
From 0a13f2627ae9b26986be5241dcf126206b627df5 Mon Sep 17 00:00:00 2001
From: Sowren Sen <80112748+nemoitis@users.noreply.github.com>
Date: Tue, 2 May 2023 10:19:14 +0600
Subject: [PATCH 2/8] new keys for font families
---
config/svg-avatar.php | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/config/svg-avatar.php b/config/svg-avatar.php
index 9472a3c..dddb6c7 100644
--- a/config/svg-avatar.php
+++ b/config/svg-avatar.php
@@ -49,6 +49,38 @@
*/
'corner_radius' => 0,
+ /*
+ |--------------------------------------------------------------------------
+ | Custom Font URL
+ |--------------------------------------------------------------------------
+ |
+ | Specify the URL of the font family you want to use. You can use
+ | Google Fonts, Fontshare or other services.
+ |
+ | e.g. https://api.fontshare.com/v2/css?f[]=kola@400&display=swap
+ |
+ | Type: string
+ | Default: null
+ |
+ */
+ 'custom_font_url' => null,
+
+ /*
+ |--------------------------------------------------------------------------
+ | Font Family
+ |--------------------------------------------------------------------------
+ |
+ | If you are using a custom font, specify the name of the family here,
+ | otherwise it won't work. For example, for the URL in the previous
+ | `custom_font_url` section's example, you should define 'Kola'
+ | as the family name in this key.
+ |
+ | Type: string
+ | Default: null
+ |
+ */
+ 'font_family' => null,
+
/*
|--------------------------------------------------------------------------
| Font Size
From 5d873f8c44061cdcb3c92946ccb93b559e4f3ccd Mon Sep 17 00:00:00 2001
From: Sowren Sen <80112748+nemoitis@users.noreply.github.com>
Date: Tue, 2 May 2023 10:22:21 +0600
Subject: [PATCH 3/8] support for custom font family
---
src/Exceptions/InvalidUrlException.php | 11 +++++
src/Http/Controllers/SvgController.php | 2 +
src/SvgAvatarGenerator.php | 60 ++++++++++++++++++++++++++
3 files changed, 73 insertions(+)
create mode 100644 src/Exceptions/InvalidUrlException.php
diff --git a/src/Exceptions/InvalidUrlException.php b/src/Exceptions/InvalidUrlException.php
new file mode 100644
index 0000000..da4c310
--- /dev/null
+++ b/src/Exceptions/InvalidUrlException.php
@@ -0,0 +1,11 @@
+customFontUrl;
+ }
+
+ /**
+ * Set the custom font url.
+ *
+ * @throws InvalidUrlException
+ */
+ public function setCustomFontUrl(?string $url = null): static
+ {
+ if (!empty($url) && ! URL::isValidUrl($url)) {
+ throw InvalidUrlException::create($url);
+ }
+
+ $this->customFontUrl = $url;
+
+ return $this;
+ }
+
+ /**
+ * Get the SVG font family.
+ */
+ public function getFontFamily(): ?string
+ {
+ return $this->fontFamily;
+ }
+
+ /**
+ * Set the SVG font family.
+ */
+ public function setFontFamily(?string $name = null): static
+ {
+ $this->fontFamily = $name;
+
+ return $this;
+ }
+
/**
* Get the font size.
*/
@@ -386,6 +443,7 @@ protected function setGradientSet(): static
* @throws InvalidGradientRotationException
* @throws InvalidSvgSizeException
* @throws InvalidGradientStopException
+ * @throws InvalidUrlException
*/
protected function build(): void
{
@@ -393,6 +451,8 @@ protected function build(): void
->setSize($this->config['size'])
->setShape($this->config['shape'])
->setCornerRadius($this->config['corner_radius'])
+ ->setCustomFontUrl($this->config['custom_font_url'] ?? null)
+ ->setFontFamily($this->config['font_family'] ?? null)
->setFontSize($this->config['font_size'])
->setFontWeight($this->config['font_weight'])
->setForeground($this->config['foreground'])
From c828898aa9f02163b6c5b4dab7cf05c19dc60298 Mon Sep 17 00:00:00 2001
From: Sowren Sen <80112748+nemoitis@users.noreply.github.com>
Date: Tue, 2 May 2023 10:22:48 +0600
Subject: [PATCH 4/8] move font family to its own blade file
---
.../views/elements/font-family.blade.php | 24 +++++++++++++++++++
resources/views/elements/svg.blade.php | 6 ++++-
tests/CustomFontTest.php | 16 +++++++++++++
tests/ExceptionTest.php | 10 ++++++--
4 files changed, 53 insertions(+), 3 deletions(-)
create mode 100644 resources/views/elements/font-family.blade.php
create mode 100644 tests/CustomFontTest.php
diff --git a/resources/views/elements/font-family.blade.php b/resources/views/elements/font-family.blade.php
new file mode 100644
index 0000000..7e252a0
--- /dev/null
+++ b/resources/views/elements/font-family.blade.php
@@ -0,0 +1,24 @@
+@php
+ /**
+ * @var Sowren\SvgAvatarGenerator\SvgAvatarGenerator $generator
+ */
+@endphp
+
+@if($url = $generator->getCustomFontUrl())
+
+
+@else
+
+@endif
diff --git a/resources/views/elements/svg.blade.php b/resources/views/elements/svg.blade.php
index e612893..de9cd64 100644
--- a/resources/views/elements/svg.blade.php
+++ b/resources/views/elements/svg.blade.php
@@ -11,13 +11,17 @@
viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
>
+
+ @include('svg::elements.font-family')
+
@include('svg::elements.linear-gradient')
+
@include($generator->getShape()->render())
+
setCustomFontUrl('https://api.fontshare.com/v2/css?f[]=kola@400&display=swap')
+ ->setFontFamily('Kola')
+ ->render();
+
+ expect($svg)
+ ->toContain('Kola')
+ ->toContain("@import url(https://api.fontshare.com");
+});
diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php
index 2627053..b113424 100644
--- a/tests/ExceptionTest.php
+++ b/tests/ExceptionTest.php
@@ -1,5 +1,6 @@
getInitials();
})->throws(MissingTextException::class);
-it('it will throw exception if gradient stop less than minimum is provided', function () {
+it('will throw exception if gradient stop less than minimum is provided', function () {
config(['svg-avatar.gradient_stops' => [-1, 1]]);
new SvgAvatarGenerator('Lyanna Stark');
})->throws(InvalidGradientStopException::class);
-it('it will throw exception if gradient stop greater than maximum is provided', function () {
+it('will throw exception if gradient stop greater than maximum is provided', function () {
config(['svg-avatar.gradient_stops' => [0, 2]]);
new SvgAvatarGenerator('Benjen Stark');
})->throws(InvalidGradientStopException::class);
+
+it('will throw exception if invalid font url is provided', function () {
+ config(['svg-avatar.custom_font_url' => 'invalid_url']);
+ new SvgAvatarGenerator('Rickard Stark');
+})->throws(InvalidUrlException::class);
From d075d6879becf00fae661ee9e30dbbd684375413 Mon Sep 17 00:00:00 2001
From: sowrensen
Date: Tue, 2 May 2023 04:23:21 +0000
Subject: [PATCH 5/8] Fix styling
---
src/Http/Controllers/SvgController.php | 2 +-
src/SvgAvatarGenerator.php | 16 +++++++---------
tests/CustomFontTest.php | 4 ++--
tests/ExceptionTest.php | 2 +-
4 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/src/Http/Controllers/SvgController.php b/src/Http/Controllers/SvgController.php
index a0a13a7..257a5c1 100644
--- a/src/Http/Controllers/SvgController.php
+++ b/src/Http/Controllers/SvgController.php
@@ -6,11 +6,11 @@
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidCornerRadius;
-use Sowren\SvgAvatarGenerator\Exceptions\InvalidUrlException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidFontSizeException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientRotationException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientStopException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidSvgSizeException;
+use Sowren\SvgAvatarGenerator\Exceptions\InvalidUrlException;
use Sowren\SvgAvatarGenerator\Exceptions\MissingTextException;
use Sowren\SvgAvatarGenerator\SvgAvatarGenerator;
diff --git a/src/SvgAvatarGenerator.php b/src/SvgAvatarGenerator.php
index 3239a84..4ebaa5e 100755
--- a/src/SvgAvatarGenerator.php
+++ b/src/SvgAvatarGenerator.php
@@ -3,20 +3,18 @@
namespace Sowren\SvgAvatarGenerator;
use Arr;
-use URL;
-use Validator;
-use Exception;
use Sowren\SvgAvatarGenerator\Concerns\Tool;
use Sowren\SvgAvatarGenerator\Enums\FontWeight;
use Sowren\SvgAvatarGenerator\Enums\Shape;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidCornerRadius;
-use Sowren\SvgAvatarGenerator\Exceptions\InvalidUrlException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidFontSizeException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientRotationException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientStopException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidSvgSizeException;
+use Sowren\SvgAvatarGenerator\Exceptions\InvalidUrlException;
use Sowren\SvgAvatarGenerator\Exceptions\MissingTextException;
use Sowren\SvgAvatarGenerator\Extractors\Extractor;
+use URL;
class SvgAvatarGenerator
{
@@ -247,13 +245,13 @@ public function getCustomFontUrl(): ?string
*/
public function setCustomFontUrl(?string $url = null): static
{
- if (!empty($url) && ! URL::isValidUrl($url)) {
- throw InvalidUrlException::create($url);
- }
+ if (! empty($url) && ! URL::isValidUrl($url)) {
+ throw InvalidUrlException::create($url);
+ }
- $this->customFontUrl = $url;
+ $this->customFontUrl = $url;
- return $this;
+ return $this;
}
/**
diff --git a/tests/CustomFontTest.php b/tests/CustomFontTest.php
index 80593fa..741b0c6 100644
--- a/tests/CustomFontTest.php
+++ b/tests/CustomFontTest.php
@@ -2,7 +2,7 @@
use Sowren\SvgAvatarGenerator\SvgAvatarGenerator;
-it('will have a custom font family defined', function() {
+it('will have a custom font family defined', function () {
$generator = new SvgAvatarGenerator('Saul Goodman');
$svg = (string) $generator
@@ -12,5 +12,5 @@
expect($svg)
->toContain('Kola')
- ->toContain("@import url(https://api.fontshare.com");
+ ->toContain('@import url(https://api.fontshare.com');
});
diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php
index b113424..a492e27 100644
--- a/tests/ExceptionTest.php
+++ b/tests/ExceptionTest.php
@@ -1,10 +1,10 @@
Date: Tue, 2 May 2023 11:00:27 +0600
Subject: [PATCH 6/8] if either of url or family is missing load default style
---
resources/views/elements/font-family.blade.php | 4 ++--
src/Exceptions/InvalidUrlException.php | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/resources/views/elements/font-family.blade.php b/resources/views/elements/font-family.blade.php
index 7e252a0..008d870 100644
--- a/resources/views/elements/font-family.blade.php
+++ b/resources/views/elements/font-family.blade.php
@@ -4,7 +4,7 @@
*/
@endphp
-@if($url = $generator->getCustomFontUrl())
+@if(($url = $generator->getCustomFontUrl()) && ($family = $generator->getFontFamily()))
@else
diff --git a/src/Exceptions/InvalidUrlException.php b/src/Exceptions/InvalidUrlException.php
index da4c310..102211e 100644
--- a/src/Exceptions/InvalidUrlException.php
+++ b/src/Exceptions/InvalidUrlException.php
@@ -6,6 +6,6 @@ class InvalidUrlException extends \Exception
{
public static function create(string $url): InvalidUrlException
{
- return new self("Invalid URL {$url} is provided.");
+ return new self("`{$url}` is not a valid URL.");
}
}
From 79820c7ec0adf1f980f97d594d5b0600bc801887 Mon Sep 17 00:00:00 2001
From: Sowren Sen <80112748+nemoitis@users.noreply.github.com>
Date: Tue, 2 May 2023 11:00:42 +0600
Subject: [PATCH 7/8] updated readme and changelog
---
CHANGELOG.md | 6 ++++++
README.md | 11 +++++++----
2 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1332587..a44a81c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,12 @@
All notable changes to `svg-avatar-generator` will be documented in this file.
+## 1.4.0
+
+- Support for custom fonts.
+- New keys `custom_font_url` and `font_family` in config.
+- Moved font family config to `font-family.blade.php` file.
+
## 1.3.1
- Revert `Shape::CIRCLE` back as default shape which was changed to `Shape::RECTANGLE` by mistake.
diff --git a/README.md b/README.md
index ea94126..f6b9dc9 100644
--- a/README.md
+++ b/README.md
@@ -11,14 +11,15 @@ another package for same task?
Well, this one has some subtle but nifty advantages over available packages, here's a few of them:
-- [x] No external api call is required, it's totally offline. ๐ฐ๏ธ
-- [x] Unlike some other available options, doesn't require heavy-weight image processing libraries like **Intervention**.
- ๐งบ
-- [x] Doesn't have any binary dependency, so nothing needs to be installed on server. ๐๏ธ
+- [x] Supports custom font. ๐งฃ
- [x] Supports gradient background. ๐ฆ
- [x] Supports random gradients based on defined presets in config. ๐ฆ
- [x] Multiple shapes: rectangular, rounded-rectangular, or circular. ๐
- [x] Ability to customize initials and extractor. โ๐ผ
+- [x] No external api call is required, it's totally offline. ๐ฐ๏ธ
+- [x] Unlike some other available options, doesn't require heavy-weight image processing libraries like **Intervention**.
+ ๐งบ
+- [x] Doesn't have any binary dependency, so nothing needs to be installed on server. ๐๏ธ
## Requirements
@@ -85,6 +86,8 @@ use Sowren\SvgAvatarGenerator\Enums\FontWeight;
Svg::for('John Doe')
->asCircle() // or, asRectangle() along with optional setCornerRadius($radius) method
->setSize(64)
+ ->setCustomFontUrl('https://api.fontshare.com/v2/css?f[]=kola@400&display=swap')
+ ->setFontFamily('Kola')
->setFontSize(40)
->setFontWeight(FontWeight::SEMIBOLD)
->setForeground('#FFFFFF')
From 69567c8ae2aab2aa0574ed3f8eb0bf857cd49137 Mon Sep 17 00:00:00 2001
From: Sowren Sen <13097375+sowrensen@users.noreply.github.com>
Date: Tue, 2 May 2023 12:43:32 +0600
Subject: [PATCH 8/8] changed sample output
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index f6b9dc9..bcd9ec1 100644
--- a/README.md
+++ b/README.md
@@ -141,7 +141,7 @@ After doing that, set the class as default extractor in config.
## Sample Output
-
+
## Testing