From 4c77c81bff4367253139e086db93ef24da99cd8f Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Mon, 16 Oct 2023 23:48:56 +0300 Subject: [PATCH] Add tests --- .../Poser/Calculator/GDTextSizeCalculatorSpec.php | 15 +++++++++++++++ .../Calculator/SvgTextSizeCalculatorSpec.php | 15 +++++++++++++++ src/Calculator/SvgTextSizeCalculator.php | 10 ++++------ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php create mode 100644 spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php diff --git a/spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php b/spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php new file mode 100644 index 0000000..0864008 --- /dev/null +++ b/spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php @@ -0,0 +1,15 @@ +calculateWidth('MIT', 8)->shouldBeLike(24); + $this->calculateWidth('MIT', 10)->shouldBeLike(29); + $this->calculateWidth('MIT', 14)->shouldBeLike(34); + } +} diff --git a/spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php b/spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php new file mode 100644 index 0000000..56120e0 --- /dev/null +++ b/spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php @@ -0,0 +1,15 @@ +calculateWidth('MIT', 8)->shouldBeLike(25); + $this->calculateWidth('MIT', 10)->shouldBeLike(29); + $this->calculateWidth('MIT', 14)->shouldBeLike(36); + } +} diff --git a/src/Calculator/SvgTextSizeCalculator.php b/src/Calculator/SvgTextSizeCalculator.php index 7414084..b740ba9 100644 --- a/src/Calculator/SvgTextSizeCalculator.php +++ b/src/Calculator/SvgTextSizeCalculator.php @@ -11,7 +11,7 @@ namespace PUGX\Poser\Calculator; -use Cog\SvgFont\Parser\SimpleXmlSvgFontFileParser; +use Cog\SvgFont\FontList; use Cog\Unicode\UnicodeString; /** @@ -19,16 +19,14 @@ */ class SvgTextSizeCalculator implements TextSizeCalculatorInterface { - private const SHIELD_PADDING = 12; + private const SHIELD_PADDING_X = 11; /** * 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'); + $font = FontList::ofFile(__DIR__ . '/Font/DejaVuSans.svg')->getById('DejaVuSansBook'); $letterSpacing = 0.0; @@ -38,6 +36,6 @@ public function calculateWidth(string $text, int $size = self::TEXT_SIZE): float $letterSpacing, ); - return \round($width + self::SHIELD_PADDING, 1); + return \round($width + self::SHIELD_PADDING_X, 0); } }