Skip to content

Commit

Permalink
Merge pull request #512 from simoncozens/fix-506
Browse files Browse the repository at this point in the history
Maintain order of features when writing
  • Loading branch information
anthrotype authored Jul 16, 2021
2 parents dcff218 + 63886ad commit 232c316
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
27 changes: 22 additions & 5 deletions Lib/ufo2ft/featureWriters/baseFeatureWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ def _insert(
"""

statements = feaFile.statements
inserted = {}

# Collect insert markers in blocks
# First handle those with a known location, i.e. insert markers
insertComments = self.context.insertComments

indices = []
for feature in features:
for ix, feature in enumerate(features):
if insertComments and feature.name in insertComments:
block, comment = insertComments[feature.name]
markerIndex = block.statements.index(comment)
Expand Down Expand Up @@ -207,8 +207,25 @@ def _insert(
f"{block.name} cannot be inserted. This is not supported."
)

else:
index = len(statements)
statements.insert(index, feature)
indices.append(index)
inserted[id(feature)] = True

# Now walk feature list backwards and insert any dependent features
for i in range(ix - 1, -1, -1):
if id(features[i]) in inserted:
break
# Insert this before the current one i.e. at same array index
statements.insert(index, features[i])
# All the indices recorded previously have now shifted up by one
indices = [index] + [j + 1 for j in indices]
inserted[id(features[i])] = True

# Finally, deal with any remaining features
for feature in features:
if id(feature) in inserted:
continue
index = len(statements)
statements.insert(index, feature)
indices.append(index)

Expand Down
30 changes: 15 additions & 15 deletions tests/featureWriters/markFeatureWriter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,21 @@ def test_defs_and_lookups_first(self, testufo):
markClass acutecomb <anchor 100 200> @MC_top;
markClass tildecomb <anchor 100 200> @MC_top;
feature mark {
lookup mark2base {
pos base a
<anchor 100 200> mark @MC_top;
} mark2base;
lookup mark2liga {
pos ligature f_i
<anchor 100 500> mark @MC_top
ligComponent
<anchor 600 500> mark @MC_top;
} mark2liga;
} mark;
feature mkmk {
lookup mark2mark_top {
@MFS_mark2mark_top = [acutecomb tildecomb];
Expand All @@ -591,21 +606,6 @@ def test_defs_and_lookups_first(self, testufo):
} move_acutecomb;
} mkmk;
feature mark {
lookup mark2base {
pos base a
<anchor 100 200> mark @MC_top;
} mark2base;
lookup mark2liga {
pos ligature f_i
<anchor 100 500> mark @MC_top
ligComponent
<anchor 600 500> mark @MC_top;
} mark2liga;
} mark;
"""
)

Expand Down

0 comments on commit 232c316

Please sign in to comment.