php - read out every char of (icon-) font

532 views Asked by At

I have a (icon-)font and want to read with php out all possible icons (chars) which are available in the font to list them. Is there any php function or libary that makes that possible? It doesnt matter which filetype the font is because most of them are available: svg, eot, ttf, woff.

1

There are 1 answers

0
blacksunshineCoding On BEST ANSWER

Edit: now on github: https://github.com/blacksunshineCoding/svgFontReader

Now i have found a solution and built a class for it:

class svgFontReader {

    function listGlyphs($svgFile) {
        $allGlyphs = $this->getGlyphs($svgFile);
        return implode(',', $allGlyphs);
    }

    function getGlyphs($svgFile) {
        $svgCopy = 'font.svg';

        if (file_exists($svgCopy)) {
            unlink($svgCopy);
        }
        copy($svgFile, $svgCopy);

        $svgContent = file_get_contents($svgCopy);

        $xmlInit = simplexml_load_string($svgContent);
        $svgJson = json_encode($xmlInit);
        $svgArray = json_decode($svgJson, true);

        $svgGlyphs = $svgArray['defs']['font']['glyph'];

        if (count($svgGlyphs) > 0) {

            foreach ($svgGlyphs as $glyphId => $glyph) {
                $svgGlyphs[$glyphId] = $glyph['@attributes']['unicode'];
            }

        }

        return $svgGlyphs;
    }

}

Use as followed:

$svg = dirname(__FILE__) . '/font-icons.svg'; // define font-file
$svgFontReader = new svgFontReader; // initiate class
$glyphsAll = $svgFontReader->getGlyphs($svg); // get glyphs as array
$glyphsList = $svgFontReader->listGlyphs($svg); //get glyphs as comma separate

some rules:

  • charset must be unicode/utf8
  • you have to include the font in css
  • for the relevant area you have to define the font-family