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..bcd9ec1 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')
@@ -138,7 +141,7 @@ After doing that, set the class as default extractor in config.
## Sample Output
-
+
## Testing
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
diff --git a/resources/views/elements/font-family.blade.php b/resources/views/elements/font-family.blade.php
new file mode 100644
index 0000000..008d870
--- /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()) && ($family = $generator->getFontFamily()))
+
+
+@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())
+
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.
*/
@@ -383,6 +441,7 @@ protected function setGradientSet(): static
* @throws InvalidGradientRotationException
* @throws InvalidSvgSizeException
* @throws InvalidGradientStopException
+ * @throws InvalidUrlException
*/
protected function build(): void
{
@@ -390,6 +449,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'])
diff --git a/tests/CustomFontTest.php b/tests/CustomFontTest.php
new file mode 100644
index 0000000..741b0c6
--- /dev/null
+++ b/tests/CustomFontTest.php
@@ -0,0 +1,16 @@
+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..a492e27 100644
--- a/tests/ExceptionTest.php
+++ b/tests/ExceptionTest.php
@@ -4,6 +4,7 @@
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;
@@ -42,12 +43,17 @@
$generator->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);