From c91f95acb412b20638ed79cd1e8a104f48d91105 Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Mon, 16 Oct 2023 22:28:54 +0300 Subject: [PATCH] Remove redundant classes --- src/Calculator/SvgFont.php | 121 ------------------------ src/Calculator/TextUnicodeConverter.php | 56 ----------- 2 files changed, 177 deletions(-) delete mode 100644 src/Calculator/SvgFont.php delete mode 100644 src/Calculator/TextUnicodeConverter.php diff --git a/src/Calculator/SvgFont.php b/src/Calculator/SvgFont.php deleted file mode 100644 index 8e5044c..0000000 --- a/src/Calculator/SvgFont.php +++ /dev/null @@ -1,121 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace PUGX\Poser\Calculator; - -/** - * @author Anton Komarev - */ -class SvgFont -{ - private const DEFAULT_UNITS_PER_EM = 1000; - - private array $glyphs; - - private int $unitsPerEm; - - private int $missingGlyphAdvX; - - public function __construct( - array $glyphs = [], - int $unitsPerEm = self::DEFAULT_UNITS_PER_EM, - int $missingGlyphAdvX = 0 - ) { - $this->glyphs = $glyphs; - $this->unitsPerEm = $unitsPerEm; - $this->missingGlyphAdvX = $missingGlyphAdvX; - } - - /** - * Takes path to SVG font (local path) and processes its XML - * to get path representation of every character and additional - * font parameters. - */ - public static function fromFile( - string $filePath - ): self { - $xml = new \XMLReader(); - $xml->open($filePath); - - $glyphs = []; - - while ($xml->read()) { - if (\XMLReader::ELEMENT !== $xml->nodeType) { - continue; - } - - if ('font' === $xml->name) { - $defaultHorizAdvX = (int) $xml->getAttribute('horiz-adv-x'); - } - - if ('font-face' === $xml->name) { - $unitsPerEm = (int) $xml->getAttribute('units-per-em'); - } - - if ('missing-glyph' === $xml->name) { - $missingGlyphHorizAdvX = (int) $xml->getAttribute('horiz-adv-x'); - } - - if ('glyph' === $xml->name) { - $unicode = $xml->getAttribute('unicode'); - - if (isset($unicode)) { - $codePoints = TextUnicodeConverter::convertTextToCodePoints($unicode); - - if (isset($codePoints[0])) { - $codePoint = $codePoints[0]; - - $glyphs[$codePoint] = new \stdClass(); - - $glyphHorizAdvX = $xml->getAttribute('horiz-adv-x'); - - if (empty($glyphHorizAdvX)) { - $glyphs[$codePoint]->horizAdvX = $defaultHorizAdvX ?? 0; - } else { - $glyphs[$codePoint]->horizAdvX = (int) $glyphHorizAdvX; - } - - $glyphs[$codePoint]->d = $xml->getAttribute('d'); - } - } - } - } - - return new self( - $glyphs, - $unitsPerEm ?? self::DEFAULT_UNITS_PER_EM, - $missingGlyphHorizAdvX ?? 0, - ); - } - - public function computeWidth( - int $codePoint, - int $size, - float $glyphSpacing = 0.0 - ): float { - $size /= $this->unitsPerEm; - - $glyphAdvX = $this->getGlyphAdvX($codePoint); - - $glyphWidth = $glyphAdvX * $size; - $glyphSpacingWidth = $this->unitsPerEm * $glyphSpacing * $size; - - return $glyphWidth + $glyphSpacingWidth; - } - - private function getGlyphAdvX( - int $codePoint - ): int { - return isset($this->glyphs[$codePoint]) - ? $this->glyphs[$codePoint]->horizAdvX - : $this->missingGlyphAdvX; - } -} diff --git a/src/Calculator/TextUnicodeConverter.php b/src/Calculator/TextUnicodeConverter.php deleted file mode 100644 index 360c407..0000000 --- a/src/Calculator/TextUnicodeConverter.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace PUGX\Poser\Calculator; - -/** - * @author Anton Komarev - */ -class TextUnicodeConverter -{ - /** - * Converts UTF-8 encoded string and returns unicode code points for every character. - * - * @return list - */ - public static function convertTextToCodePoints(string $string): array - { - $codePoints = []; - $values = []; - $lookingFor = 1; - - for ($i = 0; $i < \strlen($string); ++$i) { - $thisValue = \ord($string[$i]); - - if ($thisValue < 128) { - $codePoints[] = $thisValue; - } else { - if (0 === \count($values)) { - $lookingFor = ($thisValue < 224) ? 2 : 3; - } - - $values[] = $thisValue; - - if (\count($values) === $lookingFor) { - $number = (3 === $lookingFor) - ? (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64) - : (($values[0] % 32) * 64) + ($values[1] % 64); - - $codePoints[] = $number; - $values = []; - $lookingFor = 1; - } - } - } - - return $codePoints; - } -}