Debian Codegraph @

Lib/notobuilder/builduivf.py

raw symbols SCIP
1 import argparse
2 from dataclasses import replace
3 import sys
4 import glyphsLib
5 from fontTools.ttLib import TTFont
6 from fontTools.pens.transformPen import TransformPen
7 from fontTools.pens.ttGlyphPen import TTGlyphPen
8
9 def transform(ttfont, x, y):
10 glyphset = ttfont.getGlyphSet()
11
12 for glyphname in glyphset.keys():
13 glyph = glyphset[glyphname]
14 if glyph._getGlyphAndOffset()[0].numberOfContours < 1:
15 continue
16 pen = TTGlyphPen(glyphset)
17 transformpen = TransformPen(pen, (1, 0, 0, 1, x, y))
18 glyph.draw(transformpen)
19 transformed = pen.glyph()
20 ttfont['glyf'].glyphs[glyphname] = transformed
21
22 def reencode(ttfont, glyph, cp):
23 for subtable in ttfont["cmap"].tables:
24 subtable.cmap[cp] = glyph
25
26 def grovel_substitutions(font, lookup, glyphmap):
27 if lookup.LookupType == 7:
28 raise NotImplementedError
29 gmap = lambda g: glyphmap.get(g,g)
30 go = font.getGlyphOrder()
31
32 def do_coverage(c):
33 c.glyphs = list(sorted([gmap(g) for g in c.glyphs], key=lambda g:go.index(g)))
34 return c
35
36 for st in lookup.SubTable:
37 if lookup.LookupType == 1:
38 newmap = {}
39 for inglyph, outglyph in st.mapping.items():
40 newmap[gmap(inglyph)] = gmap(outglyph)
41 st.mapping = newmap
42 elif lookup.LookupType == 2:
43 newmap = {}
44 for inglyph, outglyphs in st.mapping.items():
45 newmap[gmap(inglyph)] = [gmap(g) for g in outglyphs]
46 st.mapping = newmap
47 elif lookup.LookupType == 4:
48 newligatures = {}
49 for outglyph, inglyphs in st.ligatures.items():
50 for ig in inglyphs:
51 ig.LigGlyph = gmap(ig.LigGlyph)
52 ig.Component = [gmap(c) for c in ig.Component]
53 newligatures[gmap(outglyph)] = inglyphs
54 st.ligatures = newligatures
55 elif lookup.LookupType == 5:
56 if st.Format == 1:
57 do_coverage(st.Coverage)
58 for srs in st.SubRuleSet:
59 for subrule in srs.SubRule:
60 subrule.Input = [gmap(c) for c in subrule.Input]
61 elif st.Format == 2:
62 do_coverage(st.Coverage)
63 st.ClassDef.classDefs = {gmap(k):v for k,v in st.ClassDef.classDefs.items()}
64 else:
65 st.Coverage = [do_coverage(c) for c in st.Coverage]
66 elif lookup.LookupType == 6:
67 if st.Format == 1:
68 do_coverage(st.Coverage)
69 for srs in st.ChainSubRuleSet:
70 for subrule in srs.ChainSubRule:
71 subrule.Backtrack = [gmap(c) for c in subrule.Backtrack]
72 subrule.Input = [gmap(c) for c in subrule.Input]
73 subrule.LookAhead = [gmap(c) for c in subrule.LookAhead]
74 elif st.Format == 2:
75 do_coverage(st.Coverage)
76 st.BacktrackClassDef.classDefs = {gmap(k):v for k,v in st.BacktrackClassDef.classDefs.items()}
77 st.InputClassDef.classDefs = {gmap(k):v for k,v in st.InputClassDef.classDefs.items()}
78 st.LookAheadClassDef.classDefs = {gmap(k):v for k,v in st.LookAheadClassDef.classDefs.items()}
79 elif st.Format == 3:
80 st.BacktrackCoverage = [ do_coverage(c) for c in st.BacktrackCoverage]
81 st.InputCoverage = [ do_coverage(c) for c in st.InputCoverage]
82 st.LookAheadCoverage = [ do_coverage(c) for c in st.LookAheadCoverage]
83
84 if __name__ == "__main__":
85 # Parse command line arguments
86 parser = argparse.ArgumentParser(description='Generate a UI font from existing binary and Glyphs file')
87 parser.add_argument('--output', '-o', help='Output font file')
88 parser.add_argument('binary', help='Binary font file')
89 parser.add_argument('glyphs', help='Glyphs file')
90 args = parser.parse_args()
91
92 if not args.output:
93 if "-VF.ttf" not in args.binary:
94 print("Error: Output file not specified")
95 sys.exit(1)
96 args.output = args.binary.replace('-VF.ttf', 'UI-VF.ttf')
97
98 gsfont = glyphsLib.GSFont(args.glyphs)
99 ttfont = TTFont(args.binary)
100
101 # Find UI instances
102 cp = None
103 for instance in gsfont.instances:
104 if "UI" in instance.familyName:
105 cp = instance.customParameters
106 break
107
108 if cp is None:
109 print("No UI instance found")
110 sys.exit(1)
111
112 cp = { p.name: p.value for p in cp }
113 print("Creating UI font with custom parameters %s" % cp)
114
115 if "typoAscender" in cp:
116 ttfont['OS/2'].sTypoAscender = int(cp["typoAscender"])
117 if "typoDescender" in cp:
118 ttfont['OS/2'].sTypoDescender = int(cp["typoDescender"])
119 if "typoLineGap" in cp:
120 ttfont['OS/2'].sTypoLineGap = int(cp["typoLineGap"])
121 if "winAscent" in cp:
122 ttfont['OS/2'].usWinAscent = int(cp["winAscent"])
123 if "winDescent" in cp:
124 ttfont['OS/2'].usWinDescent = int(cp["winDescent"])
125 if "hheaAscender" in cp:
126 ttfont['hhea'].ascent = int(cp["hheaAscender"])
127 if "hheaDescender" in cp:
128 ttfont['hhea'].descent = int(cp["hheaDescender"])
129 if "hheaLineGap" in cp:
130 ttfont['hhea'].lineGap = int(cp["hheaLineGap"])
131 if "xHeight" in cp:
132 ttfont['OS/2'].sxHeight = int(cp["xHeight"])
133
134 if "Filter" in cp:
135 assert cp["Filter"].startswith("Transformations;")
136 filter_args = cp["Filter"].split(";")
137 filter_args = filter_args[1:-1]
138 filter_args = { arg.split(":")[0]: arg.split(":")[1] for arg in filter_args }
139 if "ScaleX" in filter_args or "ScaleY" in filter_args:
140 print("ScaleX and ScaleY are not supported")
141 sys.exit(1)
142 offset_x = int(filter_args.get("OffsetX", "0"))
143 offset_y = int(filter_args.get("OffsetY", "0"))
144 if offset_x or offset_y:
145 print("Transforming glyphs by %d, %d" % (offset_x, offset_y))
146 transform(ttfont, offset_x, offset_y)
147
148 if "Reencode Glyphs" in cp:
149 for parm in cp["Reencode Glyphs"]:
150 glyph, codepoint = parm.split("=")
151 reencode(ttfont, glyph, int(codepoint, 16))
152
153 # Mash the GSUB table
154 uis = [ x for x in ttfont.getGlyphOrder() if "UI" in x and x.replace("UI", "") in ttfont.getGlyphOrder() ]
155 glyphmap = { g.replace("UI", ""): g for g in uis}
156 for lookup in ttfont["GSUB"].table.LookupList.Lookup:
157 grovel_substitutions(ttfont, lookup, glyphmap)
158
159 # Mash the name table
160 name = ttfont["name"]
161 name.setName(name.getName(1,3,1).toUnicode()+" UI", 1, 3, 1, 0x409)
162 name.setName(name.getName(3,3,1).toUnicode().replace("-","UI-"), 3, 3, 1, 0x409)
163 name.setName(name.getName(4,3,1).toUnicode().replace(" Regular","UI Regular"), 4, 3, 1, 0x409)
164 name.setName(name.getName(6,3,1).toUnicode().replace("-Regular","UI-Regular"), 6, 3, 1, 0x409)
165
166
167 ttfont.save(args.output)