Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder class definitions in feature writers #725

Merged
merged 5 commits into from Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 10 additions & 12 deletions Lib/ufo2ft/featureWriters/baseFeatureWriter.py
Expand Up @@ -234,23 +234,21 @@ def _insert(
statements.insert(index, feature)
indices.append(index)

# Write classDefs, anchorsDefs, markClassDefs, lookups at earliest
# opportunity.
others = []
minindex = min(indices)
if lookups:
feaFile.statements = statements = (
statements[:minindex] + lookups + statements[minindex:]
)

# Write classDefs, anchorsDefs, markClassDefs, lookups at
# the very top of the feature file.
others = []
for defs in [classDefs, anchorDefs, markClassDefs]:
if defs:
others.extend(defs)
others.append(ast.Comment(""))
# Insert lookups
if lookups:
if minindex > 0 and not others:
others.append(ast.Comment(""))
others.extend(lookups)
if others:
feaFile.statements = statements = (
statements[:minindex] + others + statements[minindex:]
)

feaFile.statements = statements = others + statements

@staticmethod
def collectInsertMarkers(feaFile, insertFeatureMarker, featureTags):
Expand Down
8 changes: 6 additions & 2 deletions tests/featureWriters/__init__.py
Expand Up @@ -13,8 +13,12 @@ def writeFeatures(cls, ufo, **kwargs):
"""
writer = cls.FeatureWriter(**kwargs)
feaFile = parseLayoutFeatures(ufo)
n = len(feaFile.statements)
old_statements = [st.asFea() for st in feaFile.statements]

if writer.write(ufo, feaFile):
new = ast.FeatureFile()
new.statements = feaFile.statements[n:]

for statement in feaFile.statements:
if statement.asFea() not in old_statements:
new.statements.append(statement)
return new
24 changes: 11 additions & 13 deletions tests/featureWriters/kernFeatureWriter2_test.py
Expand Up @@ -147,7 +147,7 @@ def test_mark_to_base_kern(self, FontClass):
# default is ignoreMarks=True
feaFile = self.writeFeatures(font)
assert str(feaFile) == dedent(
"""
"""\
lookup kern_ltr {
lookupflag IgnoreMarks;
pos B C -30;
Expand All @@ -166,7 +166,7 @@ def test_mark_to_base_kern(self, FontClass):

feaFile = self.writeFeatures(font, ignoreMarks=False)
assert str(feaFile) == dedent(
"""
"""\
lookup kern_ltr {
pos A acutecomb -55;
pos B C -30;
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_mark_to_base_only(self, FontClass):
# default is ignoreMarks=True
feaFile = self.writeFeatures(font)
assert str(feaFile) == dedent(
"""
"""\
lookup kern_ltr_marks {
pos A acutecomb -55;
} kern_ltr_marks;
Expand Down Expand Up @@ -236,7 +236,6 @@ def test_mode(self, FontClass):

expected = existing + dedent(
"""

lookup kern_ltr {
lookupflag IgnoreMarks;
pos seven six 25;
Expand Down Expand Up @@ -301,7 +300,7 @@ def test_insert_comment_before(self, FontClass):
# test append mode ignores insert marker
generated = self.writeFeatures(ufo, mode="append")
assert str(generated) == dedent(
"""
"""\
lookup kern_ltr {
lookupflag IgnoreMarks;
pos seven six 25;
Expand Down Expand Up @@ -384,7 +383,6 @@ def test_insert_comment_after(self, FontClass):
#
} kern;


lookup kern_ltr {
lookupflag IgnoreMarks;
pos seven six 25;
Expand All @@ -401,7 +399,7 @@ def test_insert_comment_after(self, FontClass):
# test append mode ignores insert marker
generated = self.writeFeatures(ufo, mode="append")
assert str(generated) == dedent(
"""
"""\
lookup kern_ltr {
lookupflag IgnoreMarks;
pos seven six 25;
Expand Down Expand Up @@ -444,7 +442,7 @@ def test_insert_comment_middle(self, FontClass):
# test append mode ignores insert marker
generated = self.writeFeatures(ufo, mode="append")
assert str(generated) == dedent(
"""
"""\
lookup kern_ltr {
lookupflag IgnoreMarks;
pos seven six 25;
Expand Down Expand Up @@ -476,7 +474,7 @@ def test_arabic_numerals(self, FontClass):
generated = self.writeFeatures(ufo)

assert str(generated) == dedent(
"""
"""\
lookup kern_rtl {
lookupflag IgnoreMarks;
pos four-ar seven-ar -30;
Expand Down Expand Up @@ -906,7 +904,7 @@ def test_kern_LTR_and_RTL_one_uses_DFLT(self, FontClass):
generated = self.writeFeatures(ufo)

assert str(generated) == dedent(
"""
"""\
lookup kern_ltr {
lookupflag IgnoreMarks;
pos A V -40;
Expand All @@ -933,7 +931,7 @@ def test_kern_LTR_and_RTL_one_uses_DFLT(self, FontClass):
generated = self.writeFeatures(ufo)

assert str(generated) == dedent(
"""
"""\
lookup kern_ltr {
lookupflag IgnoreMarks;
pos A V -40;
Expand Down Expand Up @@ -1025,7 +1023,7 @@ def test_dist_RTL(self, FontClass):
generated = self.writeFeatures(ufo)

assert str(generated) == dedent(
"""
"""\
lookup kern_rtl {
lookupflag IgnoreMarks;
pos u10A1E u10A06 <117 0 117 0>;
Expand Down Expand Up @@ -1153,7 +1151,7 @@ def test_kern_RTL_and_DFLT_numbers(self, FontClass):
generated = self.writeFeatures(ufo)

assert str(generated) == dedent(
"""
"""\
lookup kern_dflt {
lookupflag IgnoreMarks;
pos seven four -25;
Expand Down
24 changes: 11 additions & 13 deletions tests/featureWriters/kernFeatureWriter_test.py
Expand Up @@ -155,7 +155,7 @@ def test_mark_to_base_kern(self, FontClass):
# default is ignoreMarks=True
feaFile = self.writeFeatures(font)
assert str(feaFile) == dedent(
"""
"""\
lookup kern_Latn {
lookupflag IgnoreMarks;
pos B C -30;
Expand All @@ -176,7 +176,7 @@ def test_mark_to_base_kern(self, FontClass):

feaFile = self.writeFeatures(font, ignoreMarks=False)
assert str(feaFile) == dedent(
"""
"""\
lookup kern_Latn {
pos A acutecomb -55;
pos B C -30;
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_mark_to_base_only(self, FontClass):
# default is ignoreMarks=True
feaFile = self.writeFeatures(font)
assert str(feaFile) == dedent(
"""
"""\
lookup kern_Default_marks {
pos A acutecomb -55;
} kern_Default_marks;
Expand Down Expand Up @@ -250,7 +250,6 @@ def test_mode(self, FontClass):

expected = existing + dedent(
"""

lookup kern_Default {
lookupflag IgnoreMarks;
pos seven six 25;
Expand Down Expand Up @@ -319,7 +318,7 @@ def test_insert_comment_before(self, FontClass):
# test append mode ignores insert marker
generated = self.writeFeatures(ufo, mode="append")
assert str(generated) == dedent(
"""
"""\
lookup kern_Default {
lookupflag IgnoreMarks;
pos seven six 25;
Expand Down Expand Up @@ -406,7 +405,6 @@ def test_insert_comment_after(self, FontClass):
#
} kern;


lookup kern_Default {
lookupflag IgnoreMarks;
pos seven six 25;
Expand All @@ -425,7 +423,7 @@ def test_insert_comment_after(self, FontClass):
# test append mode ignores insert marker
generated = self.writeFeatures(ufo, mode="append")
assert str(generated) == dedent(
"""
"""\
lookup kern_Default {
lookupflag IgnoreMarks;
pos seven six 25;
Expand Down Expand Up @@ -470,7 +468,7 @@ def test_insert_comment_middle(self, FontClass):
# test append mode ignores insert marker
generated = self.writeFeatures(ufo, mode="append")
assert str(generated) == dedent(
"""
"""\
lookup kern_Default {
lookupflag IgnoreMarks;
pos seven six 25;
Expand Down Expand Up @@ -547,7 +545,7 @@ def test_arabic_numerals(self, FontClass):
generated = self.writeFeatures(ufo)

assert dedent(str(generated)) == dedent(
"""
"""\
lookup kern_Arab {
lookupflag IgnoreMarks;
pos four-ar seven-ar -30;
Expand All @@ -574,7 +572,7 @@ def test_arabic_numerals(self, FontClass):
generated = self.writeFeatures(ufo)

assert dedent(str(generated)) == dedent(
"""
"""\
lookup kern_Thaa {
lookupflag IgnoreMarks;
pos four-ar seven-ar -30;
Expand Down Expand Up @@ -1091,7 +1089,7 @@ def test_dist_RTL(self, FontClass):
generated = self.writeFeatures(ufo)

assert str(generated) == dedent(
"""
"""\
lookup kern_Khar {
lookupflag IgnoreMarks;
pos u10A1E u10A06 <117 0 117 0>;
Expand Down Expand Up @@ -1211,7 +1209,7 @@ def test_kern_RTL_and_DFLT_numbers(self, FontClass):
generated = self.writeFeatures(ufo)

assert dedent(str(generated)) == dedent(
"""
"""\
lookup kern_Hebr {
lookupflag IgnoreMarks;
pos yod-hb bet-hb <-100 0 -100 0>;
Expand Down Expand Up @@ -2115,7 +2113,7 @@ def test_dflt_language(FontClass):
newFeatures = KernFeatureWriterTest.writeFeatures(ufo)

assert dedent(str(newFeatures)) == dedent(
"""
"""\
lookup kern_Latn {
lookupflag IgnoreMarks;
pos a a 1;
Expand Down
6 changes: 3 additions & 3 deletions tests/featureWriters/markFeatureWriter_test.py
Expand Up @@ -282,9 +282,9 @@ def test_insert_comment_before(self, testufo):

assert str(feaFile) == dedent(
"""\
markClass acutecomb <anchor 100 200> @MC_top;
markClass tildecomb <anchor 100 200> @MC_top;

markClass acutecomb <anchor 100 200> @MC_top;
feature mark {
lookup mark2base {
pos base a
Expand Down Expand Up @@ -377,6 +377,8 @@ def test_insert_comment_after(self, testufo):

assert str(feaFile) == dedent(
"""\
markClass tildecomb <anchor 100 200> @MC_top;

markClass acutecomb <anchor 100 200> @MC_top;
feature mark {
lookup mark1 {
Expand All @@ -388,8 +390,6 @@ def test_insert_comment_after(self, testufo):
#
} mark;

markClass tildecomb <anchor 100 200> @MC_top;

feature mark {
lookup mark2base {
pos base a
Expand Down