Debian Codegraph @

tools/mkcolorfont.py

raw symbols SCIP
1 from fontTools.ttLib import TTFont
2 from fontTools.colorLib import builder as colr_builder
3 import argparse
4
5 argparser = argparse.ArgumentParser(description=
6 "Generate a color font from a given font where the color layer glyphs have a suffix .bg (background layer) and .fg (foreground layer).")
7 argparser.add_argument(
8 'input',
9 help='Input font source, e.g. "/some/path/Bla.sfdir"')
10 argparser.add_argument(
11 'output',
12 help='Output font file, e.g. "/some/path/Bla.otf"')
13 args = argparser.parse_args()
14
15 # Open the font file
16 font = TTFont(args.input)
17
18 # create two color palettes
19 palette1 = [(0,0,0,1), (1,0,0,1)]
20 palette2 = [(0.7,0.5,0.2,1), (0,0,1,1)]
21 cpal=colr_builder.buildCPAL([palette1, palette2])
22
23
24 font['CPAL'] = cpal
25
26 # create the COLR table
27 # Define the list of suffixes for the layer glyphs
28
29 # Create a list of glyph names and corresponding layer glyph names
30 glyphs = {}
31 for glyph_name in font.getGlyphOrder():
32 if glyph_name.endswith(".bg"):
33 base_glyph_name, layer_suffix = glyph_name.rsplit(".", 1)
34 glyphs[base_glyph_name] = [(base_glyph_name + "." + "bg", 0),(base_glyph_name + "." + "fg", 1)]
35 colr_table = colr_builder.buildCOLR(glyphs)
36
37 # add the COLR table to the font
38 font['COLR'] = colr_table
39 # Save the modified font file
40 font.save(args.output)
41