diff --git a/composer.json b/composer.json index 58a5fe4..e45cc2c 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "php": ">=7.4", "ext-gd": "*", "ext-simplexml": "*", + "cybercog/php-svg-font": "*", "kartsims/easysvg": "^2.4", "symfony/console": "^4.0|^5.0|^6.0" }, @@ -69,5 +70,7 @@ "branch-alias": { "dev-master": "2.x-dev" } - } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/src/Calculator/Font/DejaVuSans.svg b/src/Calculator/Font/DejaVuSans.svg new file mode 100644 index 0000000..984b0ac --- /dev/null +++ b/src/Calculator/Font/DejaVuSans.svg @@ -0,0 +1,251 @@ + + + + +This is a custom SVG webfont generated by Font Squirrel. +Copyright : Copyright c 2003 by Bitstream Inc All Rights ReservedCopyright c 2006 by Tavmjong Bah All Rights ReservedDejaVu changes are in public domain +Foundry : DejaVu fonts team +Foundry URL : httpdejavusourceforgenet + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Calculator/SvgTextSizeCalculator.php b/src/Calculator/SvgTextSizeCalculator.php new file mode 100644 index 0000000..7414084 --- /dev/null +++ b/src/Calculator/SvgTextSizeCalculator.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PUGX\Poser\Calculator; + +use Cog\SvgFont\Parser\SimpleXmlSvgFontFileParser; +use Cog\Unicode\UnicodeString; + +/** + * @author Anton Komarev + */ +class SvgTextSizeCalculator implements TextSizeCalculatorInterface +{ + private const SHIELD_PADDING = 12; + + /** + * Calculate the width of the text box. + */ + public function calculateWidth(string $text, int $size = self::TEXT_SIZE): float + { + $font = (new SimpleXmlSvgFontFileParser()) + ->parseFile(__DIR__ . '/DejaVuSans.svg') + ->getById('DejaVuSansBook'); + + $letterSpacing = 0.0; + + $width = $font->computeStringWidth( + UnicodeString::of($text), + $size, + $letterSpacing, + ); + + return \round($width + self::SHIELD_PADDING, 1); + } +}