Skip to content

Commit

Permalink
added a script to convert COLRv1 to SVGs
Browse files Browse the repository at this point in the history
Useful for debugging and testing.
  • Loading branch information
anthrotype committed Jul 10, 2020
1 parent ab5c180 commit f6ea6e1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tools/colr2svg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
"""Convert COLRv1 font to a set of SVG files, one per base color glyph."""
import os
import sys

# extend PYTHONPATH to include ../tests dir where colr_to_svg module is located
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), "tests"))

from colr_to_svg import colr_to_svg
from fontTools.ttLib import TTFont
from picosvg.geometric_types import Rect
from lxml import etree


VIEW_BOX_SIZE = 128

try:
fontfile, destdir = sys.argv[1:]
except ValueError:
sys.exit("usage: ./colr2svg.py FONTFILE SVG_OUTPUT_DIR")

font = TTFont(fontfile)
viewbox = Rect(0, 0, VIEW_BOX_SIZE, VIEW_BOX_SIZE)

os.makedirs(destdir, exist_ok=True)

for glyph_name, svg in colr_to_svg(viewbox, font).items():
output_file = os.path.join(destdir, f"{glyph_name}.svg")
with open(output_file, "wb") as f:
f.write(etree.tostring(svg.svg_root, pretty_print=True))
print(f"{output_file}")

0 comments on commit f6ea6e1

Please sign in to comment.