Debian Codegraph @

Lib/fontFeatures/ttLib/GSUBUnparser.py

raw
1 """GSUBUnparser: Convert binary GSUB lookups to fontFeatures objects."""
2 from .GTableUnparser import GTableUnparser
3 import fontFeatures
4
5
6 # These are silly little functions which help to document the intent
7 def glyph(x):
8 """Wraps a glyph name in an array, to document the fact that it occupies a slot."""
9 assert isinstance(x, str)
10 return [x]
11
12
13 def singleglyph(x):
14 """Wraps a glyph name in two arrays, to document the fact that it is the only occupant of a slot."""
15 return [glyph(x)]
16
17
18 class GSUBUnparser(GTableUnparser):
19 """Unparse a GSUB table into a fontFeatures object. See :py:class:`fontFeatures.ttLib.GTableUnparser`."""
20
21 _table = "GSUB"
22 lookupTypes = {
23 1: "SingleSubstitution",
24 2: "MultipleSubstitution",
25 3: "AlternateSubstitution",
26 4: "LigatureSubstitution",
27 5: "Contextual",
28 6: "ChainedContextual",
29 7: "Extension",
30 8: "ReverseContextualSubstitution",
31 }
32
33 _attrs = {
34 "lookup": "SubstLookupRecord",
35 "format1_ruleset": "SubRuleSet",
36 "format1_rule": "SubRule",
37 "format2_classset": "SubClassSet",
38 "format2_rule": "SubClassRule",
39 "chain_format1_ruleset": "ChainSubRuleSet",
40 "chain_format1_rule": "ChainSubRule",
41 "chain_format2_classset": "ChainSubClassSet",
42 "chain_format2_rule": "ChainSubClassRule",
43 }
44
45 def isChaining(self, lookupType):
46 """Returns true if the given lookup type is a chaining lookup."""
47 return lookupType >= 5
48
49 def unparseReverseContextualSubstitution(self, lookup):
50 """Turn a GPOS8 (reverse contextual substitution) subtable into a fontFeatures Routine."""
51 b = fontFeatures.Routine(
52 name=self.getname("ReverseContextualSubstitution" + self.gensym())
53 )
54 self._fix_flags(b, lookup)
55 for sub in lookup.SubTable:
56 prefix = []
57 outputs = []
58 suffix = []
59 if hasattr(sub, "BacktrackCoverage"):
60 for coverage in reversed(sub.BacktrackCoverage):
61 prefix.append(coverage.glyphs)
62 if hasattr(sub, "LookAheadCoverage"):
63 for i, coverage in enumerate(sub.LookAheadCoverage):
64 suffix.append(coverage.glyphs)
65 outputs = [sub.Substitute]
66 inputs = [sub.Coverage.glyphs]
67 b.addRule(
68 fontFeatures.Substitution(
69 inputs,
70 outputs,
71 prefix,
72 suffix,
73 flags=lookup.LookupFlag,
74 reverse=True,
75 )
76 )
77 return b, []
78
79 def unparseLigatureSubstitution(self, lookup):
80 """Turn a GPOS4 (ligature substitution) subtable into a fontFeatures Routine."""
81 b = fontFeatures.Routine(
82 name=self.getname("LigatureSubstitution" + self.gensym())
83 )
84 self._fix_flags(b, lookup)
85 for sub in lookup.SubTable:
86 for first, ligatures in sub.ligatures.items():
87 for lig in ligatures:
88 substarray = [glyph(first)]
89 for x in lig.Component:
90 substarray.append(glyph(x))
91 b.addRule(
92 fontFeatures.Substitution(
93 substarray,
94 singleglyph(lig.LigGlyph),
95 address=self.currentLookup,
96 flags=lookup.LookupFlag,
97 )
98 )
99 return b, []
100
101 def unparseMultipleSubstitution(self, lookup):
102 """Turn a GPOS2 (multiple substitution) subtable into a fontFeatures Routine."""
103 b = fontFeatures.Routine(
104 name=self.getname("MultipleSubstitution" + self.gensym())
105 )
106 self._fix_flags(b, lookup)
107
108 for sub in lookup.SubTable:
109 for in_glyph, out_glyphs in sub.mapping.items():
110 b.addRule(
111 fontFeatures.Substitution(
112 singleglyph(in_glyph),
113 [glyph(x) for x in out_glyphs],
114 address=self.currentLookup,
115 flags=lookup.LookupFlag,
116 )
117 )
118 return b, []
119
120 def unparseAlternateSubstitution(self, lookup):
121 """Turn a GPOS3 (alternate substitution) subtable into a fontFeatures Routine."""
122 b = fontFeatures.Routine(
123 name=self.getname("AlternateSubstitution" + self.gensym())
124 )
125 self._fix_flags(b, lookup)
126 for sub in lookup.SubTable:
127 for in_glyph, out_glyphs in sub.alternates.items():
128 b.addRule(
129 fontFeatures.Substitution(
130 singleglyph(in_glyph),
131 [out_glyphs],
132 address=self.currentLookup,
133 flags=lookup.LookupFlag,
134 force_alt=True,
135 )
136 )
137 return b, []
138
139 def unparseSingleSubstitution(self, lookup):
140 """Turn a GPOS1 (single substitution) subtable into a fontFeatures Routine."""
141 b = fontFeatures.Routine(
142 name=self.getname("SingleSubstitution" + self.gensym())
143 )
144 self._fix_flags(b, lookup)
145 for sub in lookup.SubTable:
146 for k, v in sub.mapping.items():
147 b.addRule(
148 fontFeatures.Substitution(
149 [[k]],
150 [[v]],
151 address=self.currentLookup,
152 flags=lookup.LookupFlag,
153 )
154 )
155 return b, []