1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
"""
GTableUnparser: Base class for reading binary GSUB/GPOS tables
==============================================================
"""
from collections import OrderedDict
from fontTools.misc.xmlWriter import XMLWriter
import fontFeatures
from io import BytesIO
import warnings
def glyph(x):
"""Helper routine to document that a glyph name goes in a slot."""
assert isinstance(x, str)
return [x]
class GTableUnparser:
"""Base class for reading binary GSUB/GPOS tables."""
def __init__(self, table, ff, languageSystems, font=None, config={}):
"""Create a new unparser
Args:
table: An object returned by ``font["GSUB"]`` or ``font["GPOS"]``.
ff: A fontFeatures object in which to return the output.
font: A ``TTFont`` object (optional).
config: A dictionary mapping generated names to friendly names.
The config dictionary is used to make the output more human readable.
For example, after dumping a binary TTF and reading the output, you might
discover that what we are calling ``SingleSubstitution1`` would be better
named ``fiLigature``. Passing ``{"SingleSubstitution1": "fiLigature"}``
to the call will cause the routine to be renamed appropriately."""
self.table = table.table
self.font = font
self.fontFeatures = ff
self.config = config
self.index = 0
self.lookups = [] # We keep a separate list because this is per-table
self.sharedClasses = {}
self.languageSystems = languageSystems
self.sharedLookups = OrderedDict()
def _unparse_lookups(self, slr, inputs, in_lookups=None):
lookups = []
note = None
if in_lookups:
lookups = in_lookups
indices = [sl.SequenceIndex for sl in slr]
if indices != list(sorted(indices)):
# https://github.com/adobe-type-tools/afdko/issues/1167
note = (
f"Out-of-order lookup application in lookup {self.currentLookup}\n"
f" A chaining rule calls lookups in the order {indices}\n"
" This cannot be directly expressed in Adobe FEA syntax;\n"
" asFea() output will be incorrect"
)
warnings.warn(note)
for sl in slr:
if len(lookups) <= sl.SequenceIndex:
lookups.extend([None] * (1 + sl.SequenceIndex - len(lookups)))
if not lookups[sl.SequenceIndex]:
lookups[sl.SequenceIndex] = []
if sl.LookupListIndex >= len(self.lookups):
raise ValueError(
"Lookups unparsed out of order: unparse %i first!"
% sl.LookupListIndex
)
rr = fontFeatures.RoutineReference(routine=self.lookups[sl.LookupListIndex])
lookups[sl.SequenceIndex].append(rr)
if len(lookups) < len(inputs):
lookups.extend([None] * (len(inputs) - len(lookups)))
assert len(lookups) == len(inputs)
return lookups, note
def _invertClassDef(self, a, font):
classes = {}
for glyph, klass in a.items():
if klass not in classes:
classes[klass] = []
classes[klass].append(glyph)
glyphset = set(font.getGlyphOrder())
classes[0] = glyphset - set(a.keys())
return classes
def getname(self, n):
"""Renames its input using the configuration dictionary"""
return self.config.get(n, n)
def gensym(self):
"""Generates a new unique name"""
self.index = self.index + 1
return str(self.index)
def unparse(self, doLookups=True):
"""Unparse the table to the fontFeatures object."""
if not self.table.ScriptList:
return
self.unparseLookups()
self.collectFeatures()
self.fontFeatures.resolveAllRoutines()
def _prepareFeatureLangSys(self, langTag, langSys, table, features, scriptTag):
# This is a part of prepareFeatures
for featureIdx in langSys.FeatureIndex:
featureRecord = self.table.FeatureList.FeatureRecord[featureIdx]
featureTag = featureRecord.FeatureTag
scripts = features.get(featureTag, None)
if scripts is None:
scripts = OrderedDict()
features[featureTag] = scripts
languages = scripts.get(scriptTag, None)
if languages is None:
languages = OrderedDict()
scripts[scriptTag] = languages
lookups = languages.get(langTag, None)
if lookups is None:
lookups = []
languages[langTag] = lookups
for lookupIdx in featureRecord.Feature.LookupListIndex:
if (scriptTag, langTag) not in self.lookups[lookupIdx].languages:
self.lookups[lookupIdx].languages.append((scriptTag, langTag))
# Add reference if there isn't one
if featureTag not in self.fontFeatures.features:
self.fontFeatures.features[featureTag] = []
if not any(
r.routine == self.lookups[lookupIdx]
for r in self.fontFeatures.features[featureTag]
):
self.fontFeatures.addFeature(
featureTag,
[self.lookups[lookupIdx]],
)
lookups.append(lookupIdx)
def collectFeatures(self):
"""Traverse the script/language table looking for features."""
features = OrderedDict()
for scriptRecord in self.table.ScriptList.ScriptRecord:
scriptTag = scriptRecord.ScriptTag
if scriptRecord.Script.DefaultLangSys is not None:
self._prepareFeatureLangSys(
"dflt",
scriptRecord.Script.DefaultLangSys,
self.table,
features,
scriptTag,
)
for langSysRecord in scriptRecord.Script.LangSysRecord:
self._prepareFeatureLangSys(
langSysRecord.LangSysTag,
langSysRecord.LangSys,
self.table,
features,
scriptTag,
)
self.features = features
def unparseLookups(self):
"""Unparses the lookups to fontFeatures routines."""
if not self.table.LookupList:
return
# Create a dummy list first, to allow resolving chained lookups
for _ in self.table.LookupList.Lookup:
r = fontFeatures.Routine()
self.lookups.append(r)
self.fontFeatures.routines.append(r)
for lookupIdx, lookup in enumerate(self.table.LookupList.Lookup):
routine, deps = self.unparseLookup(lookup, lookupIdx)
debug = self.getDebugInfo(self._table, lookupIdx)
if debug:
routine.address = (self._table, lookupIdx, *debug)
if debug[1]:
routine.name = debug[1]
self._copyRoutineToRoutine(routine, self.lookups[lookupIdx])
def _copyRoutineToRoutine(self, src, dst):
dst.name = src.name
dst.rules = src.rules
dst.flags = src.flags
dst.address = src.address
dst.comments = src.comments
dst.inlined = src.inlined
dst.languages = src.languages
dst.parent = src.parent
dst.flags = src.flags
dst.markFilteringSet = src.markFilteringSet
dst.markAttachmentSet = src.markAttachmentSet
def unparseLookup(self, lookup, lookupIdx):
"""Dispatches to the appropriate lookup unparser."""
self.currentLookup = lookupIdx
unparser = getattr(self, "unparse" + self.lookupTypes[lookup.LookupType])
return unparser(lookup)
def unparseExtension(self, lookup):
"""Handles extension lookups by recursing into them."""
routines = []
dependencies = []
for xt in lookup.SubTable:
xt.SubTable = [xt.ExtSubTable]
xt.LookupType = xt.ExtSubTable.LookupType
xt.LookupFlag = lookup.LookupFlag
if hasattr(lookup, "MarkFilteringSet"):
xt.MarkFilteringSet = lookup.MarkFilteringSet
routine, deps = self.unparseLookup(xt, self.currentLookup)
routines.append(routine)
dependencies.extend(deps)
extension = fontFeatures.ExtensionRoutine(
routines=routines,
name=self.getname("Extension" + self.gensym()),
)
self._fix_flags(extension, lookup)
return extension, dependencies
def getDebugInfo(self, table, ix):
"""If information is present in the font's Debg table, use that to
retrieve the original address and routine names."""
if not self.font or "Debg" not in self.font:
return None
debug_data = self.font["Debg"].data
if "com.github.fonttools.feaLib" not in debug_data:
return None
debug_data = debug_data["com.github.fonttools.feaLib"][table][str(ix)]
return debug_data[0], debug_data[1].replace("-", "_")
def _asXML(self, sub):
writer = XMLWriter(BytesIO())
sub.toXML(writer, self.font)
out = writer.file.getvalue().decode("utf-8")
return out
def unparsable(self, b, e, sub):
"""Warn about an unparsable lookup."""
import warnings
warnings.warn(
"# XXX Unparsable rule: " + str(e) + " in " + str(self.currentLookup)
)
b.addComment("# ----")
out = self._asXML(sub).splitlines()
for ln in out:
b.addComment("# " + ln)
b.addComment("# ----\n")
def _fix_flags(self, routine, lookup):
routine.flags = lookup.LookupFlag
mat = lookup.LookupFlag & 0xFF00
if mat:
mat = mat >> 8
classDefs = self.font["GDEF"].table.MarkAttachClassDef.classDefs
glyphs = [g for g in classDefs.keys() if classDefs[g] == mat]
routine.markAttachmentSet = glyphs
if lookup.LookupFlag & 0x10 and hasattr(lookup, "MarkFilteringSet"):
routine.markFilteringSet = (
self.font["GDEF"]
.table.MarkGlyphSetsDef.Coverage[lookup.MarkFilteringSet]
.glyphs
)
if routine.flags & 0x10:
assert routine.markFilteringSet is not None
def unparseContextual(self, lookup):
"""Handles a generic contextual lookup, in various formats."""
b = fontFeatures.Routine(
name=self.getname("Contextual" + self._table + self.gensym())
)
self._fix_flags(b, lookup)
for sub in lookup.SubTable:
if sub.Format == 1:
self._unparse_contextual_format1(sub, b, lookup)
elif sub.Format == 2:
self._unparse_contextual_format2(sub, b, lookup)
elif sub.Format == 3:
self._unparse_contextual_format3(sub, b, lookup)
else:
raise ValueError
return b, []
def _unparse_contextual_format1(self, sub, b, lookup):
lookups = []
rulesetattr, ruleattr, lookupattr = [
self._attrs[x] for x in ["format1_ruleset", "format1_rule", "lookup"]
]
for subrulesets, input_ in zip(getattr(sub, rulesetattr), sub.Coverage.glyphs):
for subrule in getattr(subrulesets, ruleattr):
lookups = []
allinput = [glyph(x) for x in ([input_] + subrule.Input)]
lookups, note = self._unparse_lookups(
getattr(subrule, lookupattr), allinput
)
rule = fontFeatures.Chaining(
allinput,
lookups=lookups,
address=self.currentLookup,
flags=lookup.LookupFlag,
)
if note:
rule.note = note
b.addRule(rule)
return
def _unparse_contextual_format2(self, sub, b, lookup):
return self._unparse_contextual_chain_format2(sub, b, lookup, chain=False)
def _unparse_contextual_format3(self, sub, b, lookup):
return self._unparse_contextual_chain_format3(sub, b, lookup, chain=False)
def unparseChainedContextual(self, lookup):
"""Handles a generic chained contextual lookup, in various formats."""
b = fontFeatures.Routine(
name=self.getname("ChainedContextual" + self._table + self.gensym())
)
self._fix_flags(b, lookup)
for sub in lookup.SubTable:
if sub.Format == 1:
self._unparse_contextual_chain_format1(sub, b, lookup)
elif sub.Format == 2:
self._unparse_contextual_chain_format2(sub, b, lookup)
elif sub.Format == 3:
self._unparse_contextual_chain_format3(sub, b, lookup)
else:
raise ValueError
return b, []
def _unparse_contextual_chain_format1(self, sub, b, lookup):
rulesetattr, ruleattr, lookupattr = [
self._attrs[x]
for x in ["chain_format1_ruleset", "chain_format1_rule", "lookup"]
]
for subrulesets, input_ in zip(getattr(sub, rulesetattr), sub.Coverage.glyphs):
for subrule in getattr(subrulesets, ruleattr):
lookups = []
inputs = [glyph(x) for x in ([input_] + subrule.Input)]
prefix = [glyph(x) for x in reversed(subrule.Backtrack)]
suffix = [glyph(x) for x in subrule.LookAhead]
lookups, note = self._unparse_lookups(
getattr(subrule, lookupattr), inputs
)
rule = fontFeatures.Chaining(
inputs,
prefix,
suffix,
lookups=lookups,
address=self.currentLookup,
flags=lookup.LookupFlag,
)
if note:
rule.note = note
b.addRule(rule)
def _unparse_contextual_chain_format2(self, sub, b, lookup, chain=True):
if chain:
rulesetattr, ruleattr, lookupattr = [
self._attrs[x]
for x in ["chain_format2_classset", "chain_format2_rule", "lookup"]
]
else:
rulesetattr, ruleattr, lookupattr = [
self._attrs[x] for x in ["format2_classset", "format2_rule", "lookup"]
]
if chain:
backtrack = {}
if sub.BacktrackClassDef:
backtrack = self._invertClassDef(
sub.BacktrackClassDef.classDefs, self.font
)
lookahead = {}
if sub.LookAheadClassDef:
lookahead = self._invertClassDef(
sub.LookAheadClassDef.classDefs, self.font
)
inputs = {}
inputs = self._invertClassDef(sub.InputClassDef.classDefs, self.font)
else:
inputs = self._invertClassDef(sub.ClassDef.classDefs, self.font)
rulesets = getattr(sub, rulesetattr)
for classId, ruleset in enumerate(rulesets):
if not ruleset:
continue
rules = getattr(ruleset, ruleattr)
inputclass = inputs.get(classId, [])
# The coverage filters the input class...
inputclass = [g for g in inputclass if g in sub.Coverage.glyphs]
for r in rules:
if chain:
prefix = list(reversed([backtrack[x] for x in r.Backtrack]))
suffix = [lookahead[x] for x in r.LookAhead]
input_ = [inputclass] + [inputs[x] for x in r.Input]
else:
prefix, suffix = [], []
input_ = [inputclass] + [inputs[x] for x in r.Class]
lookups, note = self._unparse_lookups(getattr(r, lookupattr), input_)
rule = fontFeatures.Chaining(
input_,
prefix,
suffix,
lookups=lookups,
address=self.currentLookup,
flags=lookup.LookupFlag,
)
if note:
rule.note = note
b.addRule(rule)
def _unparse_contextual_chain_format3(self, sub, b, lookup, chain=True):
lookupattr = self._attrs["lookup"]
prefix, suffix, inputs = [], [], []
if chain:
for coverage in reversed(sub.BacktrackCoverage):
prefix.append(coverage.glyphs)
for coverage in sub.LookAheadCoverage:
suffix.append(coverage.glyphs)
for coverage in sub.InputCoverage:
inputs.append(coverage.glyphs)
else:
for coverage in sub.Coverage:
inputs.append(coverage.glyphs)
lookups, note = self._unparse_lookups(getattr(sub, lookupattr), inputs)
rule = fontFeatures.Chaining(
inputs,
prefix,
suffix,
lookups=lookups,
address=self.currentLookup,
flags=lookup.LookupFlag,
)
if note:
rule.note = note
b.addRule(rule)
|