Skip to content

Commit

Permalink
Added border radius to SVG (#95)
Browse files Browse the repository at this point in the history
* Added border radius to SVG

Co-authored-by: Bayu Hendra Winata <uyab.exe@gmail.com>
  • Loading branch information
chris-mackie and uyab committed Feb 29, 2020
1 parent bec4686 commit 0e24cda
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
// 'background' (same as background color)
// or any valid hex ('#aabbcc')
'color' => 'background',

// border radius, currently only work for SVG
'radius' => 0,
],

// List of theme name to be used when rendering avatar
Expand Down
32 changes: 20 additions & 12 deletions src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Avatar

protected $borderColor;

protected $borderRadius = 0;

protected $ascii = false;

protected $uppercase = false;
Expand Down Expand Up @@ -73,8 +75,8 @@ class Avatar
/**
* Avatar constructor.
*
* @param array $config
* @param Repository $cache
* @param array $config
* @param Repository $cache
*/
public function __construct(array $config = [], Repository $cache = null)
{
Expand Down Expand Up @@ -131,6 +133,7 @@ public function applyTheme(array $config)
$this->uppercase = $config['uppercase'];
$this->borderSize = $config['border']['size'];
$this->borderColor = $config['border']['color'];
$this->borderRadius = $config['border']['radius'];
}

public function addTheme(string $name, array $config)
Expand Down Expand Up @@ -209,6 +212,7 @@ public function toSvg()
.'" width="'.$width.'" height="'.$height
.'" stroke="'.$this->borderColor
.'" stroke-width="'.$this->borderSize
.'" rx="'.$this->borderRadius
.'" fill="'.$this->background.'" />';
} elseif ($this->shape == 'circle') {
$svg .= '<circle cx="'.$center
Expand Down Expand Up @@ -436,22 +440,26 @@ protected function buildInitial()
protected function validateConfig($config)
{
$fallback = [
'shape' => 'circle',
'chars' => 2,
'shape' => 'circle',
'chars' => 2,
'backgrounds' => [$this->background],
'foregrounds' => [$this->foreground],
'fonts' => [$this->defaultFont],
'fontSize' => 48,
'width' => 100,
'height' => 100,
'ascii' => false,
'uppercase' => false,
'border' => [
'size' => 1,
'fonts' => [$this->defaultFont],
'fontSize' => 48,
'width' => 100,
'height' => 100,
'ascii' => false,
'uppercase' => false,
'border' => [
'size' => 1,
'color' => 'foreground',
'radius' => 0,
],
];

// Handle nested config
$config['border'] = ($config['border'] ?? []) + ($this->defaultTheme['border'] ?? []) + $fallback['border'];

return $config + $this->defaultTheme + $fallback;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Concerns/AttributeSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public function setBorder($size, $color)
return $this;
}

public function setBorderRadius($radius)
{
$this->borderRadius = $radius;

return $this;
}

public function setShape($shape)
{
$this->shape = $shape;
Expand Down
38 changes: 30 additions & 8 deletions tests/AvatarLaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ class AvatarLaravelTest extends \PHPUnit\Framework\TestCase
public function it_can_override_attributes_when_instantiated()
{
$config = [
'ascii' => false,
'shape' => 'circle',
'width' => 100,
'height' => 100,
'chars' => 2,
'fontSize' => 48,
'fonts' => ['arial.ttf'],
'ascii' => false,
'shape' => 'circle',
'width' => 100,
'height' => 100,
'chars' => 2,
'fontSize' => 48,
'fonts' => ['arial.ttf'],
'foregrounds' => ['#FFFFFF'],
'backgrounds' => ['#000000'],
'border' => ['size' => 1, 'color' => '#999999'],
'border' => ['size' => 1, 'color' => '#999999', 'radius' => 15],
];

$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');
Expand All @@ -39,9 +39,31 @@ public function it_can_override_attributes_when_instantiated()
$this->assertAttributeEquals(48, 'fontSize', $avatar);
$this->assertAttributeEquals(1, 'borderSize', $avatar);
$this->assertAttributeEquals('#999999', 'borderColor', $avatar);
$this->assertAttributeEquals(15, 'borderRadius', $avatar);
$this->assertAttributeEquals(false, 'ascii', $avatar);
}

/**
* @test
*/
public function it_have_no_border_radius_as_default()
{
$config = [
'border' => ['size' => 1, 'color' => '#999999'],
];

$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('make')->andReturn('AB');
$generator->shouldReceive('setUppercase');
$generator->shouldReceive('setAscii');

$avatar = new \Laravolt\Avatar\Avatar($config, $cache, $generator);

$this->assertAttributeEquals(0, 'borderRadius', $avatar);
}

/**
* @test
*/
Expand Down
6 changes: 4 additions & 2 deletions tests/AvatarPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function it_can_override_attributes_when_instantiated()
'fonts' => ['arial.ttf'],
'foregrounds' => ['#FFFFFF'],
'backgrounds' => ['#000000'],
'border' => ['size' => 1, 'color' => '#999999'],
'border' => ['size' => 1, 'color' => '#999999', 'radius' => 15],
];

$avatar = new \Laravolt\Avatar\Avatar($config);
Expand All @@ -32,6 +32,7 @@ public function it_can_override_attributes_when_instantiated()
$this->assertAttributeEquals(48, 'fontSize', $avatar);
$this->assertAttributeEquals(1, 'borderSize', $avatar);
$this->assertAttributeEquals('#999999', 'borderColor', $avatar);
$this->assertAttributeEquals(15, 'borderRadius', $avatar);
$this->assertAttributeEquals(false, 'ascii', $avatar);
}

Expand Down Expand Up @@ -198,7 +199,7 @@ public function it_can_generate_circle_svg()
public function it_can_generate_rectangle_svg()
{
$expected = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">';
$expected .= '<rect x="5" y="5" width="90" height="90" stroke="yellow" stroke-width="10" fill="red" />';
$expected .= '<rect x="5" y="5" width="90" height="90" stroke="yellow" stroke-width="10" rx="15" fill="red" />';
$expected .= '<text x="50" y="50" font-size="24" fill="white" alignment-baseline="middle" text-anchor="middle" dominant-baseline="central">AB</text>';
$expected .= '</svg>';

Expand All @@ -209,6 +210,7 @@ public function it_can_generate_rectangle_svg()
->setDimension(100, 100)
->setForeground('white')
->setBorder(10, 'yellow')
->setBorderRadius(15)
->setBackground('red')
->toSvg();

Expand Down

0 comments on commit 0e24cda

Please sign in to comment.