-
-
Save notasausage/bfd6877ad1d804dffd1a2c81fddbfaff to your computer and use it in GitHub Desktop.
Convert SVG font to individual SVG files
This file contains hidden or 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
| # http://helpfulsheep.com/2015-03-25-converting-svg-fonts-to-svg/ | |
| import sys, re | |
| if len(sys.argv) < 2: | |
| print ('Usage: python {} webfont-file.svg').format(sys.argv[0]) | |
| sys.exit() | |
| with open(sys.argv[1], 'r') as r: | |
| font = r.read() | |
| # Glyph attributes (including the path's d attribute) may span several lines. | |
| # Match through the end of each element rather than extracting only its first | |
| # line. SVG fonts normally use self-closing glyph elements, but handle an | |
| # explicit closing tag as well. | |
| glyphs = re.findall(r'<glyph\b.*?(?:/>|</glyph\s*>)', font, re.DOTALL) | |
| # for every glyph element in the file | |
| for i in range(0, len(glyphs)): | |
| filename = re.search(r'glyph-name="([^"]+)"', glyphs[i]) | |
| filename = filename.group(1) if filename else str(i + 1).rjust(3, '0') | |
| with open(filename + ".svg", 'w') as w: | |
| w.write('<svg width="2000" height="2000" viewBox="0 0 2000 2000" xmlns="http://www.w3.org/2000/svg">\n') | |
| # replace 'glyph' with 'path' and flip vertically | |
| path = re.sub(r'<glyph\b', '<path transform="scale(1, -1) translate(0, -2000)"', glyphs[i], count=1) | |
| path = re.sub(r'</glyph\s*>', '</path>', path) | |
| w.write(path + '\n') | |
| w.write('</svg>') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed a few issues including a missing closing
pathtag.