From 6bc1998ad98e10ad7d16f2b0784a286c105d7991 Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Sat, 6 Jan 2024 21:14:09 +0300 Subject: [PATCH] Implement SvgTextSizeCalculator (#97) Co-authored-by: Anton Komarev --- composer.json | 1 + .../Calculator/GDTextSizeCalculatorSpec.php | 15 ++ .../Calculator/SvgTextSizeCalculatorSpec.php | 15 ++ src/Calculator/Font/DejaVuSans.svg | 251 ++++++++++++++++++ src/Calculator/SvgTextSizeCalculator.php | 41 +++ 5 files changed, 323 insertions(+) create mode 100644 spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php create mode 100644 spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php create mode 100644 src/Calculator/Font/DejaVuSans.svg create mode 100644 src/Calculator/SvgTextSizeCalculator.php diff --git a/composer.json b/composer.json index 97000e6..b371ef0 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "php": "^8.1", "ext-gd": "*", "ext-simplexml": "*", + "cybercog/php-svg-font": "^1.0", "kartsims/easysvg": "^2.5", "symfony/console": "^5.0 || ^6.0 || ^7.0" }, diff --git a/spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php b/spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php new file mode 100644 index 0000000..64e1127 --- /dev/null +++ b/spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php @@ -0,0 +1,15 @@ +calculateWidth('MIT', 8)->shouldBeLike(24.0); + $this->calculateWidth('MIT', 10)->shouldBeLike(29.0); + $this->calculateWidth('MIT', 14)->shouldBeLike(34.0); + } +} diff --git a/spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php b/spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php new file mode 100644 index 0000000..7930d3b --- /dev/null +++ b/spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php @@ -0,0 +1,15 @@ +calculateWidth('MIT', 8)->shouldBeLike(24.1); + $this->calculateWidth('MIT', 10)->shouldBeLike(27.7); + $this->calculateWidth('MIT', 14)->shouldBeLike(34.8); + } +} 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..887fa3c --- /dev/null +++ b/src/Calculator/SvgTextSizeCalculator.php @@ -0,0 +1,41 @@ + + * + * 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\FontList; +use Cog\Unicode\UnicodeString; + +/** + * @author Anton Komarev + */ +class SvgTextSizeCalculator implements TextSizeCalculatorInterface +{ + /** + * Calculate the width of the text box. + */ + public function calculateWidth(string $text, int $size = self::TEXT_SIZE): float + { + $font = FontList::ofFile(__DIR__ . '/Font/DejaVuSans.svg')->getById('DejaVuSansBook'); + + $letterSpacing = 0.0; + + $width = $font->computeStringWidth( + UnicodeString::of($text), + $size, + $letterSpacing, + ); + + $shieldPaddingX = self::SHIELD_PADDING_EXTERNAL + self::SHIELD_PADDING_INTERNAL; + + return \round($width + $shieldPaddingX, 1); + } +}