Skip to content

Instantly share code, notes, and snippets.

@aryelgois
Created May 23, 2018 19:42
Show Gist options
  • Save aryelgois/9119efdcfb9754dc37f26399838bba5c to your computer and use it in GitHub Desktop.
Save aryelgois/9119efdcfb9754dc37f26399838bba5c to your computer and use it in GitHub Desktop.
Automated process to extract SVG Icomoon icons
#!/usr/bin/env php
<?php
/**
* Automated process to extract SVG Icomoon icons
*
* Usage: php icomoon-extract.php path/to/icomoon.svg [path/to/destiny/]
*
* It takes each non-empty <glyph> and outputs into a svg file with one <path>.
* It also adds a matrix to resize to 48x48 px, some rectangles to help
* positioning, and includes some guides and grid.
*
* @link https://icomoon.io/app
*
* @author Aryel Mota Góis
* @license MIT
*/
if (count($argv) <= 1) {
echo "Usage: $argv[0] path/to/icomoon.svg [path/to/destiny/]\n";
exit(1);
}
$file = file_get_contents($argv[1]);
$dest = rtrim(trim($argv[2] ?? ''), '/');
$dest = ($dest == '' ? '.' : $dest);
if (!file_exists($dest)) {
mkdir($dest, 0777, true);
} elseif (!is_dir($dest)) {
echo "E: Destiny is not a directory\n";
exit(1);
}
$header = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="48" height="48" version="1.1">
<sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="8" inkscape:cx="24" inkscape:cy="24" inkscape:document-units="px" showgrid="true" units="px" showguides="true" inkscape:guide-bbox="true" inkscape:lockguides="true">
<sodipodi:guide inkscape:color="rgb(255,0,0)" orientation="1,0" position="4,0" />
<sodipodi:guide inkscape:color="rgb(255,0,0)" orientation="0,1" position="0,4" />
<sodipodi:guide inkscape:color="rgb(0,255,0)" orientation="1,0" position="8,0" />
<sodipodi:guide inkscape:color="rgb(0,255,0)" orientation="0,1" position="0,8" />
<sodipodi:guide inkscape:color="rgb(0,0,255)" orientation="1,0" position="24,0" />
<sodipodi:guide inkscape:color="rgb(0,0,255)" orientation="0,1" position="0,24" />
<sodipodi:guide inkscape:color="rgb(0,255,0)" orientation="1,0" position="40,0" />
<sodipodi:guide inkscape:color="rgb(0,255,0)" orientation="0,1" position="0,40" />
<sodipodi:guide inkscape:color="rgb(255,0,0)" orientation="0,1" position="0,44" />
<sodipodi:guide inkscape:color="rgb(255,0,0)" orientation="1,0" position="44,0" />
<inkscape:grid type="xygrid" spacingx="2" spacingy="2" empspacing="2" />
</sodipodi:namedview>
<rect x="-24" y="-24" width="96" height="96" style="opacity:0.1" />
<rect x="0" y="0" width="48" height="48" style="opacity:0.1" />
XML;
$footer = '</svg>';
if (!preg_match_all('~<glyph.*name="([^"]+)".*(d="[^"]+").*/>~', $file, $matches, PREG_SET_ORDER)) {
echo "E: Could not find any <glyph>!\n";
exit(1);
}
foreach ($matches as $match) {
file_put_contents(
"$dest/$match[1].svg",
"$header\n <path transform=\"matrix(0.046875 0 0 -0.046875 0 44)\" $match[2] />\n$footer"
);
}
@aryelgois
Copy link
Author

does this script still work? 😅

the last argument is optional and defaults to the current working directory, so it should not be necessary to specify in the docker run line.. @MaxAntony did it break without it?

when specified a path that does not exist, the script creates a new directory. I think it should not have permission 0777 (default for mkdir()).. it could be 0775, or 0755. Maybe check the current umask?

this script is fairly trivial, and could be ported to a language that is more likely to be available without docker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment