Skip to content

Commit

Permalink
Merge pull request #1020 from googlefonts/fix-glyph-data-param
Browse files Browse the repository at this point in the history
Fix AttributeError when parsing optional glyph_data parameter
  • Loading branch information
anthrotype authored Aug 15, 2024
2 parents 8698f2c + 47dc620 commit 9afd0d8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
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

0 comments on commit 9afd0d8

Please sign in to comment.