Skip to content

Commit

Permalink
Add script to create color fonts
Browse files Browse the repository at this point in the history
Will eventually handle the proposal in
#3
  • Loading branch information
georgd committed Apr 10, 2023
1 parent e221096 commit 92b7815
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tools/mkcolorfont.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from fontTools.ttLib import TTFont
from fontTools.colorLib import builder as colr_builder
import argparse

argparser = argparse.ArgumentParser(description=
"Generate a color font from a given font where the color layer glyphs have a suffix .bg (background layer) and .fg (foreground layer).")
argparser.add_argument(
'input',
help='Input font source, e.g. "/some/path/Bla.sfdir"')
argparser.add_argument(
'output',
help='Output font file, e.g. "/some/path/Bla.otf"')
args = argparser.parse_args()

# Open the font file
font = TTFont(args.input)

# create two color palettes
palette1 = [(0,0,0,1), (1,0,0,1)]
palette2 = [(0.7,0.5,0.2,1), (0,0,1,1)]
cpal=colr_builder.buildCPAL([palette1, palette2])


font['CPAL'] = cpal

# create the COLR table
# Define the list of suffixes for the layer glyphs

# Create a list of glyph names and corresponding layer glyph names
glyphs = {}
for glyph_name in font.getGlyphOrder():
if glyph_name.endswith(".bg"):
base_glyph_name, layer_suffix = glyph_name.rsplit(".", 1)
glyphs[base_glyph_name] = [(base_glyph_name + "." + "bg", 0),(base_glyph_name + "." + "fg", 1)]
colr_table = colr_builder.buildCOLR(glyphs)

# add the COLR table to the font
font['COLR'] = colr_table
# Save the modified font file
font.save(args.output)

0 comments on commit 92b7815

Please sign in to comment.