Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Oct 16, 2023
1 parent 8d3e9d2 commit b4eae24
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Cog\SvgFont;

use Cog\Unicode\CompositeCharacter;
use Cog\Unicode\Character;

final class Font
{
Expand Down Expand Up @@ -48,13 +48,13 @@ public function id(): string
}

public function computeWidth(
CompositeCharacter $compositeCharacter,
Character $character,
int $size,
float $letterSpacing = 0.0
): float {
$size = $size / $this->fontFace->unitsPerEm();

$glyphHorizontalAdvance = $this->getGlyphHorizontalAdvance($compositeCharacter);
$glyphHorizontalAdvance = $this->getGlyphHorizontalAdvance($character);

$glyphWidth = $glyphHorizontalAdvance * $size;
$letterSpacingWidth = $this->fontFace->unitsPerEm() * $letterSpacing * $size;
Expand All @@ -63,14 +63,14 @@ public function computeWidth(
}

private function getGlyphHorizontalAdvance(
CompositeCharacter $compositeCharacter
Character $character
): int {
if (!isset($this->glyphs[$compositeCharacter->toChars()])) {
if (!isset($this->glyphs[strval($character)])) {
return $this->missingGlyph->horizontalAdvance()
?? $this->horizontalAdvance;
}

return $this->glyphs[$compositeCharacter->toChars()]->horizontalAdvance()
return $this->glyphs[strval($character)]->horizontalAdvance()
?? $this->horizontalAdvance;
}
}
14 changes: 7 additions & 7 deletions src/Glyph.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace Cog\SvgFont;

use Cog\Unicode\CompositeCharacter;
use Cog\Unicode\Character;

final class Glyph
{
private CompositeCharacter $compositeCharacter;
private Character $character;

private ?string $name;

private ?int $horizontalAdvance;

public function __construct(
CompositeCharacter $compositeCharacter,
Character $character,
?string $name = null,
?int $horizontalAdvance = null
) {
Expand All @@ -24,15 +24,15 @@ public function __construct(

if ($horizontalAdvance !== null && $horizontalAdvance < 0) {
throw new \InvalidArgumentException(
"Glyph with unicode `{$compositeCharacter->toChars()}` has negative horizontal advance",
"Glyph with unicode `$character` has negative horizontal advance",
);
}
$this->compositeCharacter = $compositeCharacter;
$this->character = $character;
}

public function compositeCharacter(): CompositeCharacter
public function character(): Character
{
return $this->compositeCharacter;
return $this->character;
}

public function name(): ?string
Expand Down
10 changes: 5 additions & 5 deletions src/Parser/SimpleXmlSvgFontFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cog\SvgFont\Glyph;
use Cog\SvgFont\MissingGlyph;
use Cog\SvgFont\FontList;
use Cog\Unicode\CompositeCharacter;
use Cog\Unicode\Character;

final class SimpleXmlSvgFontFileParser implements
SvgFontFileParser
Expand Down Expand Up @@ -71,8 +71,8 @@ private function initFont(
$unicode = strval($fontChildElement[self::ATTRIBUTE_UNICODE]);

if ($unicode !== '') {
$compositeCharacter = CompositeCharacter::ofChars($unicode);
$glyphs[$unicode] = $this->initGlyph($fontChildElement, $compositeCharacter);
$character = Character::of($unicode);
$glyphs[$unicode] = $this->initGlyph($fontChildElement, $character);
}
break;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ private function initFontFace(

private function initGlyph(
\SimpleXMLElement $glyphElement,
CompositeCharacter $compositeCharacter
Character $character
): Glyph {
$name = strval($glyphElement[self::ATTRIBUTE_GLYPH_NAME]);

Expand All @@ -130,7 +130,7 @@ private function initGlyph(
}

return new Glyph(
$compositeCharacter,
$character,
$name,
$glyphHorizontalAdvance,
);
Expand Down
10 changes: 5 additions & 5 deletions src/Parser/XmlReaderSvgFontFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cog\SvgFont\Glyph;
use Cog\SvgFont\MissingGlyph;
use Cog\SvgFont\FontList;
use Cog\Unicode\CompositeCharacter;
use Cog\Unicode\Character;

final class XmlReaderSvgFontFileParser implements
SvgFontFileParser
Expand Down Expand Up @@ -85,8 +85,8 @@ public function parseFile(
$unicode = strval($xml->getAttribute(self::ATTRIBUTE_UNICODE));

if ($unicode !== '') {
$compositeCharacter = CompositeCharacter::ofChars($unicode);
$glyphs[$unicode] = $this->initGlyph($xml, $compositeCharacter);
$character = Character::of($unicode);
$glyphs[$unicode] = $this->initGlyph($xml, $character);
}
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ private function initFontFace(

private function initGlyph(
\XMLReader $glyphElement,
CompositeCharacter $compositeCharacter
Character $character
): Glyph {
$name = $glyphElement->getAttribute(self::ATTRIBUTE_GLYPH_NAME);

Expand All @@ -127,7 +127,7 @@ private function initGlyph(
//$glyph->pathData = $xml->getAttribute('d');

return new Glyph(
$compositeCharacter,
$character,
$name,
$glyphHorizontalAdvance,
);
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/SvgFontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Cog\SvgFont\Font;
use Cog\SvgFont\Parser\SimpleXmlSvgFontFileParser;
use Cog\Unicode\CompositeCharacter;
use Cog\Unicode\Character;
use PHPUnit\Framework\TestCase;

final class SvgFontTest extends TestCase
Expand All @@ -23,7 +23,7 @@ public function testItCanComputeTextWidth(
$this->assertSame(
$expectedWidth,
$font->computeWidth(
CompositeCharacter::ofChars($characters),
Character::of($characters),
$fontSize,
$letterSpacing,
),
Expand Down

0 comments on commit b4eae24

Please sign in to comment.