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

Add insert feature marker when Glyphs' Automatic Code Start/End is present #655

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/glyphsLib/builder/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

ANONYMOUS_FEATURE_PREFIX_NAME = "<anonymous>"
ORIGINAL_FEATURE_CODE_KEY = GLYPHLIB_PREFIX + "originalFeatureCode"
GLYPHSAPP_INSERT_CODE = ("# Automatic Code Start", "# Automatic Code End")


def autostr(automatic):
Expand Down Expand Up @@ -95,6 +96,11 @@ def _to_ufo_features(font, ufo=None, generate_GDEF=False, skip_export_glyphs=Non
lines.append("# disabled")
lines.extend("#" + line for line in code.splitlines())
else:
insert_code = GLYPHSAPP_INSERT_CODE
for ic in insert_code:
if ic in code.split("\n"):
lines = ["### INSERT %s" % feature.name, ""] + lines
break
lines.append(code)
lines.append("} %s;" % feature.name)
feature_defs.append("\n".join(lines))
Expand Down
37 changes: 37 additions & 0 deletions tests/builder/features_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,43 @@ def test_roundtrip_feature_prefix_with_only_a_comment(ufo_module):
assert prefix_r.code == "#include(../family.fea)"


def test_roundtrip_automatic_code_insert_feature(ufo_module):
font = to_glyphs([ufo_module.Font()])
feature = classes.GSFeature(name="kern")
feature.code = dedent(
"""
# Automatic Code Start
pos L apostrophe' -20 a;
"""
)
font.features.append(feature)

(ufo,) = to_ufos(font, ufo_module=ufo_module)
assert ufo.features.text == dedent(
"""\
### INSERT kern

feature kern {

# Automatic Code Start
pos L apostrophe' -20 a;

} kern;
"""
)

font_r = to_glyphs([ufo])
assert len(font_r.features) == 1
feature_r = font_r.features[0]
assert feature_r.name == "kern"
assert feature.code == dedent(
"""
# Automatic Code Start
pos L apostrophe' -20 a;
"""
)


@pytest.fixture
def ufo_with_GDEF(ufo_module):
ufo = ufo_module.Font()
Expand Down