-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
Changes from 3 commits
b6c2fcb
ee5eddf
2d20de5
fa4d221
80a5330
f1782b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": { | ||
|
@@ -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": { | ||
|
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; | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) :)