Debian Codegraph @

Lib/fontFeatures/ttLib/GPOSUnparser.py

raw
1 """GPOSUnparser: Convert binary GPOS lookups to fontFeatures objects."""
2 from .GTableUnparser import GTableUnparser
3 import fontFeatures
4
5
6 class GPOSUnparser(GTableUnparser):
7 """Unparse a GPOS table into a fontFeatures object. See :py:class:`fontFeatures.ttLib.GTableUnparser`."""
8
9 _table = "GPOS"
10 lookupTypes = {
11 1: "SinglePositioning",
12 2: "PairPositioning",
13 3: "CursiveAttachment",
14 4: "MarkToBase",
15 5: "MarkToLigature",
16 6: "MarkToMark",
17 7: "Contextual",
18 8: "ChainedContextual",
19 9: "Extension",
20 }
21
22 _attrs = {
23 "lookup": "PosLookupRecord",
24 "format1_ruleset": "PosRuleSet",
25 "format1_rule": "PosRule",
26 "format2_classset": "PosClassSet",
27 "format2_rule": "PosClassRule",
28 "chain_format1_ruleset": "ChainPosRuleSet",
29 "chain_format1_rule": "ChainPosRule",
30 "chain_format2_classset": "ChainPosClassSet",
31 "chain_format2_rule": "ChainPosClassRule",
32 }
33
34 def makeValueRecord(self, valueRecord):
35 """Helper routine to create ValueRecord instances.
36
37 Args:
38 valueRecord: An ``otTables.ValueRecord`` object.
39
40 Returns a ``fontFeatures.ValueRecord`` object."""
41 valueFormatFlags = (
42 ("XPlacement", 0x0001), # Includes horizontal adjustment for placement
43 ("YPlacement", 0x0002), # Includes vertical adjustment for placement
44 ("XAdvance", 0x0004), # Includes horizontal adjustment for advance
45 ("YAdvance", 0x0008), # Includes vertical adjustment for advance
46 # Currently we don't have a way to express or represent value records
47 # with Device tables, whether old-style (ppem adjustment) or new-style
48 # (VariationIndex). Even if we could represent them, then what? See
49 # issue #31.
50 # ("XPlaDevice", 0x0010), # Includes horizontal Device table for placement
51 # ("YPlaDevice", 0x0020), # Includes vertical Device table for placement
52 # ("XAdvDevice", 0x0040), # Includes horizontal Device table for advance
53 # ("YAdvDevice", 0x0080) # Includes vertical Device table for advance
54 # , 'Reserved': 0xF000 For future use (set to zero)
55 )
56
57 # defaults to 0
58 values = [getattr(valueRecord, name, 0) or None for name, _ in valueFormatFlags]
59 return fontFeatures.ValueRecord(*values)
60
61 def isChaining(self, lookupType):
62 """Returns true if the given lookup type is a chaining lookup."""
63 return lookupType >= 7
64
65 def unparseSinglePositioning(self, lookup):
66 """Turn a GPOS1 (single positioning) subtable into a fontFeatures Routine."""
67 b = fontFeatures.Routine(name=self.getname("SinglePositioning" + self.gensym()))
68 self._fix_flags(b, lookup)
69
70 for subtable in lookup.SubTable:
71 if subtable.Format == 1:
72 spos = fontFeatures.Positioning(
73 [subtable.Coverage.glyphs],
74 [self.makeValueRecord(subtable.Value)],
75 address=self.currentLookup,
76 flags=lookup.LookupFlag,
77 )
78 b.addRule(spos)
79 else:
80 # Optimize it later
81 for g, v in zip(subtable.Coverage.glyphs, subtable.Value):
82 spos = fontFeatures.Positioning(
83 [[g]],
84 [self.makeValueRecord(v)],
85 address=self.currentLookup,
86 flags=lookup.LookupFlag,
87 )
88 b.addRule(spos)
89 return b, []
90
91 def unparsePairPositioning(self, lookup):
92 """Turn a GPOS2 (pair adjustment) subtable into a fontFeatures Routine."""
93 b = fontFeatures.Routine(name=self.getname("PairPositioning" + self.gensym()))
94 self._fix_flags(b, lookup)
95 for subtable in lookup.SubTable:
96 if subtable.Format == 1:
97 for g, pair in zip(subtable.Coverage.glyphs, subtable.PairSet):
98 for vr in pair.PairValueRecord:
99 spos = fontFeatures.Positioning(
100 [[g], [vr.SecondGlyph]],
101 [
102 self.makeValueRecord(vr.Value1),
103 self.makeValueRecord(vr.Value2),
104 ],
105 address=self.currentLookup,
106 flags=lookup.LookupFlag,
107 )
108 b.addRule(spos)
109 else:
110 class1 = self._invertClassDef(subtable.ClassDef1.classDefs, self.font)
111 class2 = self._invertClassDef(subtable.ClassDef2.classDefs, self.font)
112 for ix1, c1 in enumerate(subtable.Class1Record):
113 if ix1 not in class1:
114 continue # XXX
115 for ix2, c2 in enumerate(c1.Class2Record):
116 if ix2 not in class2:
117 continue # XXX
118 vr1 = self.makeValueRecord(c2.Value1)
119 vr2 = self.makeValueRecord(c2.Value2)
120 if not vr1 and not vr2:
121 continue
122 firstClass = list(
123 set(class1[ix1]) & set(subtable.Coverage.glyphs)
124 )
125 spos = fontFeatures.Positioning(
126 [firstClass, class2[ix2]],
127 [vr1, vr2],
128 address=self.currentLookup,
129 flags=lookup.LookupFlag,
130 )
131 b.addRule(spos)
132 return b, []
133
134 def unparseCursiveAttachment(self, lookup):
135 """Turn a GPOS3 (cursive attachment) subtable into a fontFeatures Routine."""
136 b = fontFeatures.Routine(name=self.getname("CursiveAttachment" + self.gensym()))
137 self._fix_flags(b, lookup)
138 entries = {}
139 exits = {}
140 for s in lookup.SubTable:
141 assert s.Format == 1
142 for glyph, record in zip(s.Coverage.glyphs, s.EntryExitRecord):
143 if record.EntryAnchor:
144 entries[glyph] = (
145 record.EntryAnchor.XCoordinate,
146 record.EntryAnchor.YCoordinate,
147 )
148 if record.ExitAnchor:
149 exits[glyph] = (
150 record.ExitAnchor.XCoordinate,
151 record.ExitAnchor.YCoordinate,
152 )
153 b.addRule(
154 fontFeatures.Attachment(
155 "cursive_entry",
156 "cursive_exit",
157 entries,
158 exits,
159 flags=lookup.LookupFlag,
160 address=self.currentLookup,
161 )
162 )
163 return b, []
164
165 def unparseMarkToBase(self, lookup):
166 """Turn a GPOS4 (mark to base) subtable into a fontFeatures Routine."""
167 b = fontFeatures.Routine(name=self.getname("MarkToBase" + self.gensym()))
168 self._fix_flags(b, lookup)
169 for subtable in lookup.SubTable: # fontTools.ttLib.tables.otTables.MarkBasePos
170 assert subtable.Format == 1
171 for classId in range(0, subtable.ClassCount):
172 anchorClassPrefix = "Anchor" + self.gensym()
173 marks = self._formatMarkArray(
174 subtable.MarkArray, subtable.MarkCoverage, classId
175 )
176 bases = self._formatBaseArray(
177 subtable.BaseArray, subtable.BaseCoverage, classId
178 )
179 b.addRule(
180 fontFeatures.Attachment(
181 anchorClassPrefix,
182 "_" + anchorClassPrefix,
183 bases,
184 marks,
185 font=self.font,
186 address=self.currentLookup,
187 flags=lookup.LookupFlag,
188 )
189 )
190 return b, []
191
192 def _formatMarkArray(self, markArray, markCoverage, classId):
193 id2Name = markCoverage.glyphs
194 marks = {}
195 for i, markRecord in enumerate(markArray.MarkRecord):
196 if markRecord.Class == classId:
197 marks[id2Name[i]] = (
198 markRecord.MarkAnchor.XCoordinate,
199 markRecord.MarkAnchor.YCoordinate,
200 # markRecord.Class
201 )
202 return marks
203
204 def _formatMark2Array(self, markArray, markCoverage, anchorClassPrefix):
205 id2Name = markCoverage.glyphs
206 marks = {}
207 for i, markRecord in enumerate(markArray.Mark2Record):
208 anchor = markRecord.Mark2Anchor[anchorClassPrefix]
209 if not anchor:
210 continue
211 marks[id2Name[i]] = (
212 anchor.XCoordinate,
213 anchor.YCoordinate,
214 )
215 return marks
216
217 def _formatBaseArray(self, baseArray, baseCoverage, wantedClassId):
218 id2Name = baseCoverage.glyphs
219 bases = {}
220 for i, baseRecord in enumerate(baseArray.BaseRecord):
221 for classId, anchor in enumerate(baseRecord.BaseAnchor):
222 if classId != wantedClassId:
223 continue
224 if not anchor:
225 continue
226 bases[id2Name[i]] = (anchor.XCoordinate, anchor.YCoordinate) # ClassId?
227 return bases
228
229 def unparseMarkToLigature(self, lookup):
230 """Turn a GPOS5 (mark to ligature) subtable into a fontFeatures Routine."""
231 b = fontFeatures.Routine(name=self.getname("MarkToLigature" + self.gensym()))
232 self._fix_flags(b, lookup)
233 self.unparsable(b, "Mark to lig pos", lookup)
234 return b, []
235
236 def unparseMarkToMark(self, lookup):
237 """Turn a GPOS6 (mark to mark) subtable into a fontFeatures Routine."""
238 b = fontFeatures.Routine(name=self.getname("MarkToMark" + self.gensym()))
239 self._fix_flags(b, lookup)
240 for subtable in lookup.SubTable: # fontTools.ttLib.tables.otTables.MarkBasePos
241 assert subtable.Format == 1
242 for classId in range(0, subtable.ClassCount):
243 anchorClassPrefix = "Anchor" + self.gensym()
244 marks = self._formatMarkArray(
245 subtable.Mark1Array, subtable.Mark1Coverage, classId
246 )
247 bases = self._formatMark2Array(
248 subtable.Mark2Array, subtable.Mark2Coverage, classId
249 )
250 b.addRule(
251 fontFeatures.Attachment(
252 anchorClassPrefix,
253 anchorClassPrefix + "_",
254 bases,
255 marks,
256 font=self.font,
257 address=self.currentLookup,
258 flags=lookup.LookupFlag,
259 force_markmark=True,
260 )
261 )
262 return b, []