-
Notifications
You must be signed in to change notification settings - Fork 0
/
glyphs-to-svg.py
28 lines (21 loc) · 897 Bytes
/
glyphs-to-svg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
from blackrenderer.font import BlackRendererFont
from blackrenderer.backends import getSurfaceClass
from pathlib import Path
glyph_dir = "glyphs" # the directory containing all glyph svgs
font = BlackRendererFont("font.ttf") # the font file
font.setLocation({"EDPT": 200}) # if the font is variable, set the axis values
palette = font.getPalette(1) # set the palette
def replace_svg(path):
glyphname = path.stem
bounding_box = font.getGlyphBounds(glyphname)
# alternative backend classes to svg are: skia, cairo, coregraphics
surfaceClass = getSurfaceClass("svg")
surface = surfaceClass()
with surface.canvas(bounding_box) as canvas:
font.drawGlyph(glyphname, canvas, palette=palette)
surface.saveImage(path)
for f in Path(glyph_dir).glob('**/*'):
if not f.is_file() and f.suffix != "svg":
continue
replace_svg(f)