Skip to content

Instantly share code, notes, and snippets.

@anthrotype
Last active March 17, 2020 19:07
Show Gist options
  • Select an option

  • Save anthrotype/fcd62b9f29cf24fb54b8eaebf1e125e0 to your computer and use it in GitHub Desktop.

Select an option

Save anthrotype/fcd62b9f29cf24fb54b8eaebf1e125e0 to your computer and use it in GitHub Desktop.
import sys
from ufo2ft.constants import *
from ufoLib2 import Font
UPEM = 1000
WIDTH = UPEM // 2
def _layer(font, name):
try:
return font.layers[name]
except KeyError:
return font.newLayer(name)
def _draw_ellipse(pen):
pen.moveTo((260, 82))
pen.curveTo((342, 82), (408, 189), (408, 321))
pen.curveTo((408, 452), (342, 560), (260, 560))
pen.curveTo((178, 560), (111, 452), (111, 321))
pen.curveTo((111, 189), (178, 82), (260, 82))
pen.closePath()
def _draw_rectangle(pen):
pen.moveTo((195, 230))
pen.lineTo((485, 230))
pen.lineTo((485, 626))
pen.lineTo((195, 626))
pen.closePath()
f = Font()
f.info.familyName = "ColorTest"
f.info.unitsPerEm = UPEM
f.glyphOrder = ["space", "a", "b", "c"]
space = f.newGlyph("space")
space.unicode = 0x0020
space.width = WIDTH
for name in f.glyphOrder[1:]:
g = f.newGlyph(name)
g.width = WIDTH
g.unicode = ord(name)
z0 = _layer(f, "z_0")
g_z0 = z0.newGlyph(name)
g_z0.width = WIDTH
_draw_ellipse(g_z0.getPen())
z1 = _layer(f, "z_1")
g_z1 = z1.newGlyph(name)
g_z1.width = WIDTH
_draw_rectangle(g_z1.getPen())
a = f["a"]
a.lib[COLOR_LAYER_MAPPING_KEY] = [
("z_0", 0),
("z_1", {"format": 1, "paletteIndex": 1, "transparency": 0.6}),
]
b = f["b"]
b.lib[COLOR_LAYER_MAPPING_KEY] = [
(
"z_0",
{
"format": 2,
"colorLine": {"stops": [(0.0, 0), (1.0, 1)], "extend": "repeat"},
"p0": (111, 82),
"p1": (408, 560),
"p2": (300, 500),
},
),
(
"z_1",
{
"format": 2,
"colorLine": {
"stops": [
{"offset": 0.0, "paletteIndex": 1},
{"offset": 0.5, "paletteIndex": 0, "transparency": 0.3},
{"offset": 1.0, "paletteIndex": 1},
]
},
"p0": (195, 230),
"p1": (458, 626),
},
),
]
c = f["c"]
c.lib[COLOR_LAYER_MAPPING_KEY] = [
(
"z_0",
{
"format": 3,
"colorLine": {"stops": [(0.0, 1), (1.0, 0)]},
"c0": (250, 350),
"r0": 200,
"c1": (250, 250),
"r1": 0,
},
),
(
"z_1",
{
"format": 3,
"colorLine": {"stops": [(0.4, 1), (0.8, 0)], "extend": "reflect"},
"c0": (300, 400),
"r0": 100,
"c1": (458, 626),
"r1": 10,
},
),
]
f.lib[COLOR_PALETTES_KEY] = [[(1, 0.3, 0.1, 1), (0, 0.4, 0.8, 1)]]
try:
output_ufo_path = sys.argv[1]
except IndexError:
sys.exit("error: must provide the UFO output path")
f.save(output_ufo_path, overwrite=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment