Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SvgForTheBadgeRenderer #86

Merged
merged 6 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
JellyBellyDev marked this conversation as resolved.
Show resolved Hide resolved


## 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"
Copy link
Collaborator

@antonkomarev antonkomarev Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need to depend on the unpublished fork of the third party lib?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by unpublished?

I specifically explained why this fork has a release: https://github.com/ricardoboss/easysvg/releases/tag/2.0

We need this fork, since the original library has a lot of deprecation warnings (and even fatal errors) because it supported very old PHP versions. In my fork, I fixed them and opened a PR. I am in talks with the creator of the library to merge my PR and then we can drop this extra repository.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I pinged kartsims/easysvg#32 (comment) :)

}
],
"require": {
"php": ">=7.4",
"ext-gd": "*",
"ext-simplexml": "*",
"kartsims/easysvg": "^2.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need extra dependency? How svg rendering is implemented in other badge styles without it?

Copy link
Collaborator Author

@ricardoboss ricardoboss Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In https://github.com/ekfuhrmann/badge-generator it is implemented using opentype.js. We need some way to render a font in SVG, since not everyone has the required fonts installed on their systems. If you know another way, let me know!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem for me to new deps but i prefer to use the standard font as talk by @antonkomarev here: #86 (comment)

"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
20,673 changes: 20,673 additions & 0 deletions src/Calculator/Font/MontserratExtraBold.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
626 changes: 626 additions & 0 deletions src/Calculator/Font/RobotoMedium.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
81 changes: 81 additions & 0 deletions src/Render/SvgForTheBadgeRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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/RobotoMedium.svg';
public const VALUE_TEXT_FONT = __DIR__ . '/../Calculator/Font/MontserratExtraBold.svg';
public const TEXT_FONT_SIZE = 11;
public const TEXT_FONT_COLOR = '#FFFFFF';
public const TEXT_LETTER_SPACING = 0.1;
public const PADDING_X = 12;
public const Y_OFFSET_ROBOTO = 2;

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['vendorTextStartPosition'] = self::PADDING_X;
$parameters['vendorTextPath'] = $this->easy->addText($parameters['vendor'], $parameters['vendorTextStartPosition'], self::TEXT_FONT_SIZE / 2 + self::Y_OFFSET_ROBOTO)->asXML();

$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['valueTextStartPosition'] = $parameters['vendorWidth'] + self::PADDING_X;
$parameters['valueTextPath'] = $this->easy->addText($parameters['value'], $parameters['valueTextStartPosition'], self::TEXT_FONT_SIZE / 2)->asXML();

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

return $parameters;
}
}
6 changes: 6 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