Skip to content

Commit

Permalink
Merge pull request #86 from ricardoboss/master
Browse files Browse the repository at this point in the history
Added SvgForTheBadgeRenderer
  • Loading branch information
JellyBellyDev authored Feb 18, 2022
2 parents c3502f0 + f1782b3 commit d517d0c
Show file tree
Hide file tree
Showing 8 changed files with 24,816 additions and 15 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ Flush an image
poser license MIT blue
```

Choose a different style
```bash
poser license MIT blue -s "for-the-badge"
```

## Usage as library

#### 1. Add to composer dependencies
Expand Down Expand Up @@ -65,7 +70,7 @@ $image = $poser->generate('license', 'MIT', '428F7E', 'plastic');
echo $image->getStyle();
```

The allowed styles are: `plastic`, `flat` and `flat-square`.
The allowed styles are: `plastic`, `flat`, `flat-square`, and `for-the-badge`.


## Encoding
Expand Down
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@
{
"type": "vcs",
"url": "https://github.com/JellyBellyDev/phpspec-data-provider-extension"
},
{
"type": "vcs",
"url": "https://github.com/ricardoboss/easysvg"
}
],
"require": {
"php": ">=7.4",
"ext-gd": "*",
"ext-simplexml": "*",
"kartsims/easysvg": "^2.0",
"symfony/console": "^4.0|^5.0|^6.0"
},
"require-dev": {
Expand All @@ -47,7 +52,10 @@
},
"config": {
"bin-dir": "bin",
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"bin": ["bin/poser"],
"scripts": {
Expand Down
2,209 changes: 2,209 additions & 0 deletions src/Calculator/Font/Verdana-Bold.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22,495 changes: 22,495 additions & 0 deletions src/Calculator/Font/Verdana.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 5 additions & 13 deletions src/Render/LocalSvgRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,13 @@ abstract class LocalSvgRenderer implements RenderInterface
{
public const VENDOR_COLOR = '#555';

private ?TextSizeCalculatorInterface $textSizeCalculator = null;

private ?string $templatesDirectory = null;
private TextSizeCalculatorInterface $textSizeCalculator;
private string $templatesDirectory;

public function __construct(?TextSizeCalculatorInterface $textSizeCalculator = null, ?string $templatesDirectory = null)
{
$this->textSizeCalculator = $textSizeCalculator;
if (null === $this->textSizeCalculator) {
$this->textSizeCalculator = new GDTextSizeCalculator();
}

$this->templatesDirectory = $templatesDirectory;
if (null === $this->templatesDirectory) {
$this->templatesDirectory = __DIR__ . '/../Resources/templates';
}
$this->textSizeCalculator = $textSizeCalculator ?? new GDTextSizeCalculator();
$this->templatesDirectory = $templatesDirectory ?? (__DIR__ . '/../Resources/templates');
}

public function render(Badge $badge): Image
Expand Down Expand Up @@ -99,7 +91,7 @@ private function renderSvg(string $render, array $parameters, string $style): Im
return Image::createFromString($render, $style);
}

private function buildParameters(Badge $badge): array
protected function buildParameters(Badge $badge): array
{
$parameters = [];

Expand Down
78 changes: 78 additions & 0 deletions src/Render/SvgForTheBadgeRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the badge-poser package.
*
* (c) PUGX <http://pugx.github.io/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\Poser\Render;

use EasySVG;
use PUGX\Poser\Badge;
use PUGX\Poser\Calculator\TextSizeCalculatorInterface;

class SvgForTheBadgeRenderer extends LocalSvgRenderer
{
public const VENDOR_TEXT_FONT = __DIR__ . '/../Calculator/Font/Verdana.svg';
public const VALUE_TEXT_FONT = __DIR__ . '/../Calculator/Font/Verdana-Bold.svg';
public const TEXT_FONT_SIZE = 10;
public const TEXT_FONT_COLOR = '#FFFFFF';
public const TEXT_LETTER_SPACING = 0.1;
public const PADDING_X = 10;

private EasySVG $easy;

public function __construct(
?EasySVG $easySVG = null,
?TextSizeCalculatorInterface $textSizeCalculator = null,
?string $templatesDirectory = null
) {
parent::__construct($textSizeCalculator, $templatesDirectory);

if (null === $easySVG) {
$easySVG = new EasySVG();
}

$this->easy = $easySVG;
}

public function getBadgeStyle(): string
{
return 'for-the-badge';
}

protected function getTemplateName(): string
{
return $this->getBadgeStyle();
}

protected function buildParameters(Badge $badge): array
{
$parameters = parent::buildParameters($badge);

$parameters['vendor'] = \mb_strtoupper($parameters['vendor']);
$parameters['value'] = \mb_strtoupper($parameters['value']);

$this->easy->clearSVG();
$this->easy->setLetterSpacing(self::TEXT_LETTER_SPACING);
$this->easy->setFont(self::VENDOR_TEXT_FONT, self::TEXT_FONT_SIZE, self::TEXT_FONT_COLOR);
$vendorDimensions = $this->easy->textDimensions($parameters['vendor']);
$parameters['vendorWidth'] = $vendorDimensions[0] + 2 * self::PADDING_X;
$parameters['vendorStartPosition'] = \round($parameters['vendorWidth'] / 2, 1) + 1;

$this->easy->clearSVG();
$this->easy->setLetterSpacing(self::TEXT_LETTER_SPACING);
$this->easy->setFont(self::VALUE_TEXT_FONT, self::TEXT_FONT_SIZE, self::TEXT_FONT_COLOR);
$valueDimensions = $this->easy->textDimensions($parameters['value']);
$parameters['valueWidth'] = $valueDimensions[0] + 2 * self::PADDING_X;
$parameters['valueStartPosition'] = $parameters['vendorWidth'] + \round($parameters['valueWidth'] / 2, 1) - 1;

$parameters['totalWidth'] = $parameters['valueWidth'] + $parameters['vendorWidth'];

return $parameters;
}
}
11 changes: 11 additions & 0 deletions src/Resources/templates/for-the-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/UI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PUGX\Poser\Poser;
use PUGX\Poser\Render\SvgFlatRender;
use PUGX\Poser\Render\SvgFlatSquareRender;
use PUGX\Poser\Render\SvgForTheBadgeRenderer;
use PUGX\Poser\Render\SvgPlasticRender;
use PUGX\Poser\ValueObject\InputRequest;
use Symfony\Component\Console\Command\Command as BaseCommand;
Expand Down Expand Up @@ -36,6 +37,7 @@ public function __construct(string $name = null)
new SvgPlasticRender(),
new SvgFlatRender(),
new SvgFlatSquareRender(),
new SvgForTheBadgeRenderer(),
]);
$this->header = self::HEADER;
}
Expand All @@ -46,6 +48,7 @@ private function init(): void
new SvgPlasticRender(),
new SvgFlatRender(),
new SvgFlatSquareRender(),
new SvgForTheBadgeRenderer(),
]);
$this->header = self::HEADER;
}
Expand Down

0 comments on commit d517d0c

Please sign in to comment.