/** * save the current font as single svg files in a directory (counterpart of generateFromDir) * * @param string $dir directory path * @return static this */ public function saveGlyphsToDir($dir) { $fontOptions = $this->font->getOptions(); $svgTemplate = '<?xml version="1.0" encoding="utf-8"?>' . "\n" . '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n" . '<svg' . "\n" . ' version="1.1"' . "\n" . ' id="Layer_1"' . "\n" . ' xmlns="http://www.w3.org/2000/svg"' . "\n" . ' xmlns:xlink="http://www.w3.org/1999/xlink"' . "\n" . ' x="0px"' . "\n" . ' y="0px"' . "\n" . ' width="%%%WIDTH%%%px"' . "\n" . ' height="512px"' . "\n" . ' viewBox="0 0 %%%WIDTH%%% 512"' . "\n" . ' enable-background="new 0 0 512 512"' . "\n" . ' xml:space="preserve"' . "\n" . '>' . "\n" . ' <g id="Grid">' . "\n" . ' <rect x="0" fill="none" stroke="#A9CCDB" stroke-miterlimit="10" width="512" height="512"/>' . "\n"; for ($i = 32; $i < 512; $i += 32) { $color = 'A9CCDB'; if ($i === 448) { $color = 'FF0000'; } $svgTemplate .= ' <line fill="none" stroke="#' . $color . '" stroke-miterlimit="10" x1="0" y1="' . $i . '" x2="512" y2="' . $i . '"/>' . "\n"; } for ($i = 32; $i < 512; $i += 32) { $svgTemplate .= ' <line fill="none" stroke="#A9CCDB" stroke-miterlimit="10" x1="' . $i . '" y1="0" x2="' . $i . '" y2="512"/>' . "\n"; } $svgTemplate .= ' </g>' . "\n" . ' <path d="%%%PATH%%%"/>' . "\n" . '</svg>' . "\n"; if (!is_dir($dir)) { throw new \InvalidArgumentException('$dir must be a writable directory'); } foreach ($this->font->getGlyphs() as $glyph) { $targetPath = $dir . DIRECTORY_SEPARATOR . (empty($glyph['name']) ? 'icon' : preg_replace('([^a-z0-9]+)i', '-', $glyph['name'])) . '-x' . static::unicodeToHex($glyph['char']) . '.svg'; if (isset($this->mapping[$glyph['char']])) { if (!copy($this->mapping[$glyph['char']]['path'], $targetPath)) { throw new \Exception('unable to copy "' . $this->mapping[$glyph['char']]['path'] . '" to "' . $targetPath . '"'); } } else { $glyphDocument = Document::createFromPath($glyph['path'], $fontOptions['horiz-adv-x'], $fontOptions['units-per-em']); if (file_put_contents($targetPath, str_replace(array('%%%PATH%%%', '%%%WIDTH%%%'), array($glyphDocument->getPath(512 / $fontOptions['units-per-em'], null, 'vertical', true, 0, -64), empty($glyph['width']) ? 512 : $glyph['width'] * 512 / $fontOptions['units-per-em']), $svgTemplate)) === false) { throw new \Exception('unable to write to "' . $targetPath . '"'); } } } return $this; }