| 1 |
import io |
| 2 |
import re |
| 3 |
import fontFeatures |
| 4 |
from fontTools.feaLib.parser import Parser |
| 5 |
import fontTools.feaLib.ast as ast |
| 6 |
from warnings import warn |
| 7 |
|
| 8 |
|
| 9 |
def bad_statement_to_comment(s): |
| 10 |
# We could check through the attributes but that's hard... |
| 11 |
fea = s.asFea() |
| 12 |
if re.search(r"\[\s*\]", fea): # Empty classes |
| 13 |
warn("Empty class found in: '" + fea + "'") |
| 14 |
return ast.Comment("# " + fea) |
| 15 |
return s |
| 16 |
|
| 17 |
|
| 18 |
class FeaParser: |
| 19 |
"""Turns a AFDKO feature file or string into a FontFeatures object. |
| 20 |
|
| 21 |
Args: |
| 22 |
featurefile: File object or string. |
| 23 |
font: Optionally, a TTFont object. |
| 24 |
glyphNames: Optionally, a list of glyph names in the font |
| 25 |
""" |
| 26 |
|
| 27 |
def __init__(self, featurefile, font=None, glyphNames=None, includeDir=None): |
| 28 |
self.ff = fontFeatures.FontFeatures() |
| 29 |
self.markclasses = {} |
| 30 |
self.currentFeature = None |
| 31 |
self.currentRoutine = None |
| 32 |
self.gensym = 1 |
| 33 |
self.glyphmap = () |
| 34 |
self.currentLanguage = None |
| 35 |
if font and not glyphNames: |
| 36 |
glyphNames = font.getGlyphOrder() |
| 37 |
if isinstance(featurefile, str): |
| 38 |
featurefile = io.StringIO(featurefile) |
| 39 |
self.featurefile = featurefile |
| 40 |
if glyphNames: |
| 41 |
self.parser = Parser( |
| 42 |
self.featurefile, glyphNames=glyphNames, includeDir=includeDir |
| 43 |
) |
| 44 |
else: |
| 45 |
self.parser = Parser(self.featurefile, includeDir=includeDir) |
| 46 |
self.parser.ast.ValueRecord = fontFeatures.ValueRecord |
| 47 |
|
| 48 |
def parse(self): |
| 49 |
"""Parse the feature code. |
| 50 |
|
| 51 |
Returns: |
| 52 |
A ``FontFeatures`` object containing the rules of this file. |
| 53 |
""" |
| 54 |
|
| 55 |
# Borrow glyph classes |
| 56 |
for name, members in self.ff.namedClasses.items(): |
| 57 |
glyphclass = ast.GlyphClassDefinition( |
| 58 |
name, ast.GlyphClass([m for m in members]) |
| 59 |
) |
| 60 |
self.parser.glyphclasses_.define(name, glyphclass) |
| 61 |
|
| 62 |
parsetree = self.parser.parse() |
| 63 |
|
| 64 |
# Return glyph classes |
| 65 |
if len(self.parser.glyphclasses_.scopes_): |
| 66 |
for name, definition in self.parser.glyphclasses_.scopes_[-1].items(): |
| 67 |
if isinstance(definition, ast.MarkClass): |
| 68 |
pass |
| 69 |
# self.ff.namedClasses[name] = list(definition.glyphs.keys()) |
| 70 |
else: |
| 71 |
self.ff.namedClasses[name] = definition.glyphs.glyphs |
| 72 |
|
| 73 |
self.features_ = {} |
| 74 |
parsetree.build(self) |
| 75 |
return self.ff |
| 76 |
|
| 77 |
def _start_routine_if_necessary(self, location): |
| 78 |
if not self.currentRoutine: |
| 79 |
self._start_routine(location, "") |
| 80 |
|
| 81 |
def _start_routine(self, location, name): |
| 82 |
location = "%s:%i:%i" % (location) |
| 83 |
self._discard_empty_routine() |
| 84 |
self.currentRoutine = fontFeatures.Routine(name=name, address=location) |
| 85 |
if not name: |
| 86 |
self.currentRoutine.name = "unnamed_routine_%i" % self.gensym |
| 87 |
self.gensym = self.gensym + 1 |
| 88 |
self.currentRoutineFlag = 0 |
| 89 |
if self.currentFeature: |
| 90 |
reference = self.ff.referenceRoutine(self.currentRoutine) |
| 91 |
self.ff.addFeature(self.currentFeature, [reference]) |
| 92 |
else: |
| 93 |
self.ff.routines.append(self.currentRoutine) |
| 94 |
|
| 95 |
def start_lookup_block(self, location, name, use_extension=False): |
| 96 |
self._start_routine(location, name) |
| 97 |
|
| 98 |
def start_feature(self, location, name, use_extension=False): |
| 99 |
self.currentFeature = name |
| 100 |
|
| 101 |
def set_font_revision(self, location, revision): |
| 102 |
pass |
| 103 |
|
| 104 |
def add_name_record(self, *args): |
| 105 |
pass |
| 106 |
|
| 107 |
def add_featureName(self, tag): |
| 108 |
# XXX support this |
| 109 |
pass |
| 110 |
|
| 111 |
def set_script(self, location, script): |
| 112 |
self.currentLanguage = [(script, "*")] |
| 113 |
|
| 114 |
def set_language(self, location, language, include_default, required): |
| 115 |
self.currentLanguage = [(self.currentLanguage[0][0], language)] |
| 116 |
|
| 117 |
def add_single_subst(self, location, prefix, suffix, mapping, forceChain): |
| 118 |
self._start_routine_if_necessary(location) |
| 119 |
location = "%s:%i:%i" % (location) |
| 120 |
s = fontFeatures.Substitution( |
| 121 |
input_=[list(mapping.keys())], |
| 122 |
replacement=[list(mapping.values())], |
| 123 |
precontext=[[str(g) for g in group] for group in prefix], |
| 124 |
postcontext=[[str(g) for g in group] for group in suffix], |
| 125 |
address=location, |
| 126 |
languages=self.currentLanguage, |
| 127 |
) |
| 128 |
self.currentRoutine.addRule(s) |
| 129 |
|
| 130 |
def add_reverse_chain_single_subst(self, location, prefix, suffix, mapping): |
| 131 |
self._start_routine_if_necessary(location) |
| 132 |
location = "%s:%i:%i" % (location) |
| 133 |
s = fontFeatures.Substitution( |
| 134 |
input_=[list(mapping.keys())], |
| 135 |
replacement=[list(mapping.values())], |
| 136 |
precontext=[[str(g) for g in group] for group in prefix], |
| 137 |
postcontext=[[str(g) for g in group] for group in suffix], |
| 138 |
address=location, |
| 139 |
languages=self.currentLanguage, |
| 140 |
reverse=True, |
| 141 |
) |
| 142 |
self.currentRoutine.addRule(s) |
| 143 |
|
| 144 |
def add_multiple_subst( |
| 145 |
self, location, prefix, glyph, suffix, replacements, forceChain |
| 146 |
): |
| 147 |
self._start_routine_if_necessary(location) |
| 148 |
location = "%s:%i:%i" % (location) |
| 149 |
s = fontFeatures.Substitution( |
| 150 |
input_=[[glyph]], |
| 151 |
replacement=[[g] for g in replacements], |
| 152 |
precontext=[[str(g) for g in group] for group in prefix], |
| 153 |
postcontext=[[str(g) for g in group] for group in suffix], |
| 154 |
address=location, |
| 155 |
languages=self.currentLanguage, |
| 156 |
) |
| 157 |
self.currentRoutine.addRule(s) |
| 158 |
|
| 159 |
def add_alternate_subst(self, location, prefix, glyph, suffix, replacement): |
| 160 |
self._start_routine_if_necessary(location) |
| 161 |
location = "%s:%i:%i" % (location) |
| 162 |
s = fontFeatures.Substitution( |
| 163 |
input_=[[glyph]], |
| 164 |
replacement=[replacement], |
| 165 |
precontext=[[str(g) for g in group] for group in prefix], |
| 166 |
postcontext=[[str(g) for g in group] for group in suffix], |
| 167 |
address=location, |
| 168 |
languages=self.currentLanguage, |
| 169 |
) |
| 170 |
self.currentRoutine.addRule(s) |
| 171 |
|
| 172 |
def add_ligature_subst( |
| 173 |
self, location, prefix, glyphs, suffix, replacement, forceChain |
| 174 |
): |
| 175 |
self._start_routine_if_necessary(location) |
| 176 |
location = "%s:%i:%i" % (location) |
| 177 |
s = fontFeatures.Substitution( |
| 178 |
input_=[list(x) for x in glyphs], |
| 179 |
replacement=[[replacement]], |
| 180 |
precontext=[[str(g) for g in group] for group in prefix], |
| 181 |
postcontext=[[str(g) for g in group] for group in suffix], |
| 182 |
address=location, |
| 183 |
languages=self.currentLanguage, |
| 184 |
) |
| 185 |
self.currentRoutine.addRule(s) |
| 186 |
|
| 187 |
def add_chain_context_subst(self, location, prefix, glyphs, suffix, lookups): |
| 188 |
self._start_routine_if_necessary(location) |
| 189 |
location = "%s:%i:%i" % (location) |
| 190 |
# Find named feature |
| 191 |
mylookups = [] |
| 192 |
for x in lookups: |
| 193 |
if x: |
| 194 |
mylookups.append([self.ff.routineNamed(y.name) for y in x]) |
| 195 |
else: |
| 196 |
mylookups.append(None) |
| 197 |
s = fontFeatures.Chaining( |
| 198 |
input_=[list(x) for x in glyphs], |
| 199 |
precontext=[[str(g) for g in group] for group in prefix], |
| 200 |
postcontext=[[str(g) for g in group] for group in suffix], |
| 201 |
lookups=mylookups, |
| 202 |
address=location, |
| 203 |
languages=self.currentLanguage, |
| 204 |
) |
| 205 |
self.currentRoutine.addRule(s) |
| 206 |
|
| 207 |
add_chain_context_pos = add_chain_context_subst |
| 208 |
|
| 209 |
def add_single_pos(self, location, prefix, suffix, pos, forceChain): |
| 210 |
self._start_routine_if_necessary(location) |
| 211 |
location = "%s:%i:%i" % (location) |
| 212 |
s = fontFeatures.Positioning( |
| 213 |
glyphs=[p[0] for p in pos], |
| 214 |
valuerecords=[p[1] for p in pos], |
| 215 |
precontext=[[str(g) for g in group] for group in prefix], |
| 216 |
postcontext=[[str(g) for g in group] for group in suffix], |
| 217 |
address=location, |
| 218 |
languages=self.currentLanguage, |
| 219 |
) |
| 220 |
self.currentRoutine.addRule(s) |
| 221 |
|
| 222 |
def add_specific_pair_pos(self, location, glyph1, value1, glyph2, value2): |
| 223 |
self._start_routine_if_necessary(location) |
| 224 |
location = "%s:%i:%i" % (location) |
| 225 |
s = fontFeatures.Positioning( |
| 226 |
glyphs=[[glyph1], [glyph2]], |
| 227 |
valuerecords=[value1, value2], |
| 228 |
address=location, |
| 229 |
languages=self.currentLanguage, |
| 230 |
) |
| 231 |
self.currentRoutine.addRule(s) |
| 232 |
|
| 233 |
def add_class_pair_pos(self, location, glyphclass1, value1, glyphclass2, value2): |
| 234 |
self._start_routine_if_necessary(location) |
| 235 |
location = "%s:%i:%i" % (location) |
| 236 |
s = fontFeatures.Positioning( |
| 237 |
glyphs=[glyphclass1, glyphclass2], |
| 238 |
valuerecords=[value1, value2], |
| 239 |
address=location, |
| 240 |
languages=self.currentLanguage, |
| 241 |
) |
| 242 |
self.currentRoutine.addRule(s) |
| 243 |
|
| 244 |
def add_cursive_pos(self, location, glyphclass, entryAnchor, exitAnchor): |
| 245 |
self._start_routine_if_necessary(location) |
| 246 |
location = "%s:%i:%i" % (location) |
| 247 |
basedict, markdict = {}, {} |
| 248 |
if entryAnchor: |
| 249 |
basedict = {g: (entryAnchor.x, entryAnchor.y) for g in glyphclass} |
| 250 |
if exitAnchor: |
| 251 |
markdict = {g: (exitAnchor.x, exitAnchor.y) for g in glyphclass} |
| 252 |
s = fontFeatures.Attachment( |
| 253 |
base_name="cursive_entry", |
| 254 |
mark_name="cursive_exit", |
| 255 |
bases=basedict, |
| 256 |
marks=markdict, |
| 257 |
address=location, |
| 258 |
languages=self.currentLanguage, |
| 259 |
) |
| 260 |
self.currentRoutine.addRule(s) |
| 261 |
|
| 262 |
def add_mark_base_pos(self, location, bases, marks): |
| 263 |
self._start_routine_if_necessary(location) |
| 264 |
location = "%s:%i:%i" % (location) |
| 265 |
for baseanchor, markclass in marks: |
| 266 |
for definition in markclass.definitions: |
| 267 |
markanchor = definition.anchor |
| 268 |
s = fontFeatures.Attachment( |
| 269 |
base_name=markclass.name, |
| 270 |
mark_name=markclass.name, |
| 271 |
bases={g: (baseanchor.x, baseanchor.y) for g in bases}, |
| 272 |
marks={ |
| 273 |
g: (markanchor.x, markanchor.y) for g in markclass.glyphs.keys() |
| 274 |
}, |
| 275 |
address=location, |
| 276 |
languages=self.currentLanguage, |
| 277 |
) |
| 278 |
s.fontfeatures = self.ff |
| 279 |
self.currentRoutine.addRule(s) |
| 280 |
|
| 281 |
add_mark_mark_pos = add_mark_base_pos |
| 282 |
|
| 283 |
def add_mark_lig_pos(self, location, bases, marks): |
| 284 |
self._start_routine_if_necessary(location) |
| 285 |
location = "%s:%i:%i" % (location) |
| 286 |
# xxx pass |
| 287 |
|
| 288 |
def set_lookup_flag(self, location, value, markAttach, markFilter): |
| 289 |
if self.currentRoutine and value == self.currentRoutineFlag: |
| 290 |
return |
| 291 |
# If we're mid-feature, start a new routine here |
| 292 |
if self.currentFeature: |
| 293 |
self.end_lookup_block() |
| 294 |
self._discard_empty_routine() |
| 295 |
self._start_routine(location, None) |
| 296 |
self.currentRoutineFlag = value |
| 297 |
|
| 298 |
def add_language_system(self, location, script, language): |
| 299 |
pass |
| 300 |
|
| 301 |
def setElidedFallbackName(self, location, *args): |
| 302 |
pass |
| 303 |
|
| 304 |
def addDesignAxis(self, location, *args): |
| 305 |
pass |
| 306 |
|
| 307 |
def addAxisValueRecord(self, location, *args): |
| 308 |
pass |
| 309 |
|
| 310 |
def add_ligatureCaretByPos_(self, location, *args): # Urgh |
| 311 |
pass |
| 312 |
|
| 313 |
def add_lookup_call(self, lookup_name): |
| 314 |
routine = self.ff.routineNamed(lookup_name) |
| 315 |
rr = fontFeatures.RoutineReference(name=lookup_name, routine=routine) |
| 316 |
rr.languages = self.currentLanguage |
| 317 |
if self.currentFeature: |
| 318 |
self._discard_empty_routine() |
| 319 |
self.ff.addFeature(self.currentFeature, [rr]) |
| 320 |
else: |
| 321 |
raise ValueError("Huh?") |
| 322 |
|
| 323 |
def end_lookup_block(self): |
| 324 |
if self.currentRoutine: |
| 325 |
for rule in self.currentRoutine.rules: |
| 326 |
rule.flags = self.currentRoutineFlag |
| 327 |
|
| 328 |
def end_feature(self): |
| 329 |
self._discard_empty_routine() |
| 330 |
self.currentFeature = None |
| 331 |
self.currentLanguage = None |
| 332 |
if self.currentRoutine: |
| 333 |
for rule in self.currentRoutine.rules: |
| 334 |
rule.flags = self.currentRoutineFlag |
| 335 |
self.currentRoutine = None |
| 336 |
|
| 337 |
def _discard_empty_routine(self): |
| 338 |
if not self.currentFeature: |
| 339 |
return |
| 340 |
|
| 341 |
# If there genuinely is an empty routine, we should keep it, no? |
| 342 |
# The only problem is that for some reason, we're generating empty |
| 343 |
# routines when they should have rules in them, and this is causing |
| 344 |
# some feature files to fail to parse. :-/ |
| 345 |
|
| 346 |
# if self.currentRoutine and not self.currentRoutine.rules: |
| 347 |
# del(self.ff.routines[self.ff.routines.index(self.currentRoutine)]) |
| 348 |
# if self.currentFeature in self.ff.features: |
| 349 |
# del(self.ff.features[self.currentFeature][-1]) |
| 350 |
pass |
| 351 |
|
| 352 |
def add_feature_reference(self, location, featurename): |
| 353 |
# XXX |
| 354 |
pass |
| 355 |
|
| 356 |
def add_glyphClassDef(self, location, base, ligature, mark, component): |
| 357 |
for g in base: |
| 358 |
self.ff.glyphclasses[g] = "base" |
| 359 |
for g in ligature: |
| 360 |
self.ff.glyphclasses[g] = "ligature" |
| 361 |
for g in mark: |
| 362 |
self.ff.glyphclasses[g] = "mark" |
| 363 |
for g in component: |
| 364 |
self.ff.glyphclasses[g] = "component" |