-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
773afba
commit 8af6d8b
Showing
4 changed files
with
167 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cog\SvgFont\Parser; | ||
|
||
use Cog\SvgFont\Font; | ||
use Cog\SvgFont\FontFace; | ||
use Cog\SvgFont\Glyph; | ||
use Cog\SvgFont\MissingGlyph; | ||
use Cog\SvgFont\SvgFontFileCollection; | ||
use Cog\Unicode\CodePoint; | ||
|
||
final class SimpleXmlSvgFontFileParser implements | ||
SvgFontFileParser | ||
{ | ||
private const ATTRIBUTE_ID = 'id'; | ||
private const ATTRIBUTE_HORIZ_ADV_X = 'horiz-adv-x'; | ||
private const ATTRIBUTE_UNITS_PER_EM = 'units-per-em'; | ||
private const ATTRIBUTE_UNICODE = 'unicode'; | ||
private const ATTRIBUTE_GLYPH_NAME = 'glyph-name'; | ||
|
||
private const ELEMENT_NAME_DEFS = 'defs'; | ||
private const ELEMENT_NAME_FONT = 'font'; | ||
private const ELEMENT_NAME_FONT_FACE = 'font-face'; | ||
private const ELEMENT_NAME_MISSING_GLYPH = 'missing-glyph'; | ||
private const ELEMENT_NAME_GLYPH = 'glyph'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function parseFile(string $filePath): SvgFontFileCollection | ||
{ | ||
$xml = simplexml_load_file($filePath); | ||
|
||
$fonts = []; | ||
|
||
foreach ($xml as $xmlRootElement) { | ||
if ($xmlRootElement->getName() === self::ELEMENT_NAME_DEFS) { | ||
foreach ($xmlRootElement as $def) { | ||
if ($def->getName() === self::ELEMENT_NAME_FONT) { | ||
$fonts[] = $this->initFont($def); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return new SvgFontFileCollection($fonts); | ||
} | ||
|
||
private function initFont( | ||
\SimpleXMLElement $fontElement | ||
): Font { | ||
$fontId = strval($fontElement[self::ATTRIBUTE_ID]); | ||
$defaultHorizontalAdvance = intval($fontElement[self::ATTRIBUTE_HORIZ_ADV_X]); | ||
$glyphs = []; | ||
|
||
foreach ($fontElement as $fontChildElement) { | ||
switch ($fontChildElement->getName()) { | ||
case self::ELEMENT_NAME_FONT_FACE: | ||
$fontFace = $this->initFontFace($fontChildElement); | ||
break; | ||
case self::ELEMENT_NAME_MISSING_GLYPH: | ||
$missingGlyph = $this->initMissingGlyph($fontChildElement); | ||
break; | ||
case self::ELEMENT_NAME_GLYPH: | ||
$character = strval($fontChildElement[self::ATTRIBUTE_UNICODE]); | ||
|
||
if ($character !== '') { | ||
$codePointDecimal = CodePoint::ofCharacter($character)->toDecimal(); | ||
$glyphs[$codePointDecimal] = $this->initGlyph($fontChildElement); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return new Font( | ||
$fontId, | ||
$defaultHorizontalAdvance, | ||
$fontFace, | ||
$missingGlyph, | ||
$glyphs | ||
); | ||
} | ||
|
||
private function initFontFace( | ||
\SimpleXMLElement $fontFaceElement | ||
): FontFace { | ||
$unitsPerEm = intval($fontFaceElement[self::ATTRIBUTE_UNITS_PER_EM]); | ||
|
||
if ($unitsPerEm === 0) { | ||
$unitsPerEm = 1000; | ||
} | ||
|
||
return new FontFace( | ||
$unitsPerEm, | ||
); | ||
} | ||
|
||
private function initGlyph( | ||
\SimpleXMLElement $glyphElement | ||
): Glyph { | ||
$name = strval($glyphElement[self::ATTRIBUTE_GLYPH_NAME]); | ||
|
||
if ($name === '') { | ||
$name = null; | ||
} | ||
|
||
$glyphHorizontalAdvance = intval($glyphElement[self::ATTRIBUTE_HORIZ_ADV_X]); | ||
|
||
if ($glyphHorizontalAdvance === 0) { | ||
$glyphHorizontalAdvance = null; | ||
} | ||
|
||
//$glyph->pathData = $xml->getAttribute('d'); | ||
|
||
return new Glyph( | ||
$name, | ||
$glyphHorizontalAdvance, | ||
); | ||
} | ||
|
||
private function initMissingGlyph( | ||
\SimpleXMLElement $missingGlyphElement | ||
): MissingGlyph { | ||
$horizontalAdvance = intval($missingGlyphElement[self::ATTRIBUTE_HORIZ_ADV_X]); | ||
|
||
if ($horizontalAdvance === 0) { | ||
$horizontalAdvance = null; | ||
} | ||
|
||
return new MissingGlyph( | ||
$horizontalAdvance, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters