diff --git a/composer.json b/composer.json
index 58a5fe4..48060bb 100644
--- a/composer.json
+++ b/composer.json
@@ -29,12 +29,17 @@
{
"type": "vcs",
"url": "https://github.com/JellyBellyDev/phpspec-data-provider-extension"
+ },
+ {
+ "type": "vcs",
+ "url": "https://github.com/cybercog/php-svg-font"
}
],
"require": {
"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 +74,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 @@
+
+
+
diff --git a/src/Calculator/SvgTextSizeCalculator.php b/src/Calculator/SvgTextSizeCalculator.php
new file mode 100644
index 0000000..579af6f
--- /dev/null
+++ b/src/Calculator/SvgTextSizeCalculator.php
@@ -0,0 +1,66 @@
+
+ *
+ * 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\Font;
+use Cog\SvgFont\Parser\SimpleXmlSvgFontFileParser;
+use Cog\Unicode\UnicodeString;
+
+/**
+ * @author Anton Komarev
+ */
+class SvgTextSizeCalculator implements TextSizeCalculatorInterface
+{
+ private const SHIELD_PADDING = 12;
+
+ private const UNICODE_CODE_POINT_LINE_FEED = 10;
+
+ /**
+ * 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');
+
+ $width = $this->computeTextWidth(
+ $font,
+ $text,
+ $size,
+ );
+
+ return \round($width + self::SHIELD_PADDING, 1);
+ }
+
+ private function computeTextWidth(
+ Font $font,
+ string $text,
+ int $fontSize
+ ): float {
+ $characterList = UnicodeString::of($text)->characterList();
+
+ $maxLineWidth = $lineWidth = 0;
+
+ foreach ($characterList as $character) {
+ if (self::UNICODE_CODE_POINT_LINE_FEED === $character->toDecimal()) {
+ $maxLineWidth = \max($maxLineWidth, $lineWidth);
+ $lineWidth = 0;
+ continue;
+ }
+
+ $lineWidth += $font->computeWidth($character, $fontSize);
+ }
+
+ return \max($maxLineWidth, $lineWidth);
+ }
+}