| 1 |
# Code for converting a Routine object into feaLib statements |
| 2 |
import fontTools.feaLib.ast as feaast |
| 3 |
from fontFeatures.ttLib.Substitution import lookup_type as sub_lookup_type |
| 4 |
from fontFeatures.ttLib.Positioning import lookup_type as pos_lookup_type |
| 5 |
from fontFeatures.feaLib import bad_statement_to_comment |
| 6 |
|
| 7 |
|
| 8 |
def lookup_type(rule): |
| 9 |
from fontFeatures import Substitution, Positioning, Attachment, Chaining |
| 10 |
|
| 11 |
if isinstance(rule, Substitution): |
| 12 |
return sub_lookup_type(rule) |
| 13 |
if isinstance(rule, Positioning): |
| 14 |
return pos_lookup_type(rule) |
| 15 |
if isinstance(rule, Attachment): |
| 16 |
return rule.is_cursive |
| 17 |
if isinstance(rule, Chaining): |
| 18 |
return 1 |
| 19 |
raise ValueError |
| 20 |
|
| 21 |
|
| 22 |
counter = 0 |
| 23 |
|
| 24 |
|
| 25 |
def gensym(prefix): |
| 26 |
global counter |
| 27 |
counter = counter + 1 |
| 28 |
return prefix + str(counter) |
| 29 |
|
| 30 |
|
| 31 |
def feaPreamble(self, ff): |
| 32 |
preamble = [] |
| 33 |
for r in self.rules: |
| 34 |
preamble.extend(r.feaPreamble(ff)) |
| 35 |
if self.flags & 0xFF00: |
| 36 |
assert self.markAttachmentSet is not None |
| 37 |
self.markAttachmentSetAsClass = ff.getNamedClassFor( |
| 38 |
self.markAttachmentSet, gensym("markAttachmentSet") |
| 39 |
) |
| 40 |
if self.flags & 0x10: |
| 41 |
assert self.markFilteringSet is not None |
| 42 |
self.markFilteringSetAsClass = ff.getNamedClassFor( |
| 43 |
self.markFilteringSet, gensym("markFilteringSet") |
| 44 |
) |
| 45 |
return preamble |
| 46 |
|
| 47 |
|
| 48 |
def asFeaAST(self): |
| 49 |
if self.name: |
| 50 |
f = feaast.LookupBlock(name=self.name) |
| 51 |
else: |
| 52 |
f = feaast.Block() |
| 53 |
|
| 54 |
if hasattr(self, "flags"): |
| 55 |
flags = feaast.LookupFlagStatement(self.flags) |
| 56 |
if self.flags & 0x10 and hasattr(self, "markFilteringSetAsClass"): # XXX |
| 57 |
# We only need the name, not the contents |
| 58 |
mfs = feaast.GlyphClassDefinition( |
| 59 |
self.markFilteringSetAsClass, feaast.GlyphClass([]) |
| 60 |
) |
| 61 |
flags.markFilteringSet = feaast.GlyphClassName(mfs) |
| 62 |
if self.flags & 0xFF00 and hasattr(self, "markAttachmentSetAsClass"): # XXX |
| 63 |
mfs = feaast.GlyphClassDefinition( |
| 64 |
self.markAttachmentSetAsClass, feaast.GlyphClass([]) |
| 65 |
) |
| 66 |
flags.markAttachment = feaast.GlyphClassName(mfs) |
| 67 |
|
| 68 |
f.statements.append(flags) |
| 69 |
|
| 70 |
for x in self.comments: |
| 71 |
f.statements.append(feaast.Comment(x)) |
| 72 |
|
| 73 |
f.statements.append(feaast.Comment(";")) |
| 74 |
lastaddress = self.address |
| 75 |
if lastaddress: |
| 76 |
f.statements.append( |
| 77 |
feaast.Comment( |
| 78 |
"# Original source: %s " % (" ".join([str(x) for x in lastaddress])) |
| 79 |
) |
| 80 |
) |
| 81 |
for x in self.rules: |
| 82 |
if x.address and x.address != lastaddress: |
| 83 |
f.statements.append(feaast.Comment("# Original source: %s " % x.address)) |
| 84 |
lastaddress = x.address |
| 85 |
if hasattr(x, "note"): |
| 86 |
f.statements.append( |
| 87 |
feaast.Comment("\n".join([f"# {n}" for n in x.note.split("\n")])) |
| 88 |
) |
| 89 |
f.statements.append(bad_statement_to_comment(x.asFeaAST())) |
| 90 |
return f |
| 91 |
|
| 92 |
|
| 93 |
def asFea(self): |
| 94 |
return self.asFeaAST().asFea() |