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

Fix AttributeError when parsing optional glyph_data parameter #1020

Merged
merged 2 commits into from
Aug 15, 2024
Merged
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
12 changes: 11 additions & 1 deletion Lib/glyphsLib/builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import copy
import logging

from glyphsLib import classes
from glyphsLib import classes, glyphdata

from .builders import UFOBuilder, GlyphsBuilder
from .transformations import TRANSFORMATIONS, TRANSFORMATION_CUSTOM_PARAMS
Expand Down Expand Up @@ -61,9 +61,14 @@ def to_ufos(

If preserve_original is True, this works on a copy of the font object
to avoid modifying the original object.

The optional glyph_data parameter takes a list of GlyphData.xml paths or
a pre-parsed GlyphData object that overrides the default one.
"""
if preserve_original:
font = copy.deepcopy(font)
if glyph_data is not None and not isinstance(glyph_data, glyphdata.GlyphData):
glyph_data = glyphdata.GlyphData.from_files(*glyph_data)
font = preflight_glyphs(
font, glyph_data=glyph_data, do_propagate_all_anchors=propagate_anchors
)
Expand Down Expand Up @@ -128,9 +133,14 @@ def to_designspace(

If preserve_original is True, this works on a copy of the font object
to avoid modifying the original object.

The optional glyph_data parameter takes a list of GlyphData.xml paths or
a pre-parsed GlyphData object that overrides the default one.
"""
if preserve_original:
font = copy.deepcopy(font)
if glyph_data is not None and not isinstance(glyph_data, glyphdata.GlyphData):
glyph_data = glyphdata.GlyphData.from_files(*glyph_data)
font = preflight_glyphs(
font, glyph_data=glyph_data, do_propagate_all_anchors=propagate_anchors
)
Expand Down
13 changes: 3 additions & 10 deletions Lib/glyphsLib/builder/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,9 @@ def __init__(
# instances with matching 'familyName' custom parameter
self._do_filter_instances_by_family = True

if glyph_data:
from io import BytesIO

glyphdata_files = []
for path in glyph_data:
with open(path, "rb") as fp:
glyphdata_files.append(BytesIO(fp.read()))
self.glyphdata = glyphdata.GlyphData.from_files(*glyphdata_files)
else:
self.glyphdata = None
if glyph_data is not None and not isinstance(glyph_data, glyphdata.GlyphData):
glyph_data = glyphdata.GlyphData.from_files(*glyph_data)
self.glyphdata = glyph_data

def _is_vertical(self):
master_ids = {m.id for m in self.font.masters}
Expand Down
2 changes: 2 additions & 0 deletions tests/builder/builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2298,6 +2298,8 @@ def test_custom_glyph_data(ufo_module):
font = generate_minimal_font()
for glyph_name in ("A", "Aitalic-math", "Aitalic-math.ssty1", "foo", "bar", "baz"):
add_glyph(font, glyph_name)
# add a composite glyph to trigger propagate_anchors
add_component(font, "bar", "baz", (1, 0, 0, 1, 0, 0))
font.glyphs["baz"].production = "bazglyph"
font.glyphs["baz"].category = "Number"
font.glyphs["baz"].subCategory = "Decimal Digit"
Expand Down
Loading