googlefonts/glyphsLib

glyph_data has contradictory types

simoncozens opened this issue · 4 comments

glyphsLib.builder.to_ufos takes a glyph_data parameter which is (a) passed to preflight_glyphs, and (b) passed to UFOBuilder.

In preflight_glyphs, glyph_data is interpreted as:

glyph_data: an optional GlyphData object associating various properties to
glyph names (e.g. category) that overrides the default one

So it's a GlyphData object. However, in UFOBuilder, it is described as

glyph_data -- A list of GlyphData.

What does that mean?

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)

Oh, it's a list of paths.

It's historically been a list of paths, but now if you pass a list of paths, preflight_glyphs crashes because it's expecting a GlyphData object:

  File "/Users/simon/work/noto/math/venv/lib/python3.12/site-packages/glyphsLib/builder/transformations/propagate_anchors.py", line 67, in propagate_all_anchors_impl
    anchors = anchors_traversing_components(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/simon/work/noto/math/venv/lib/python3.12/site-packages/glyphsLib/builder/transformations/propagate_anchors.py", line 157, in anchors_traversing_components
    if layer.anchors and _get_category(glyph, glyph_data) == "Mark":
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/simon/work/noto/math/venv/lib/python3.12/site-packages/glyphsLib/builder/transformations/propagate_anchors.py", line 113, in _get_category
    or glyphdata.get_glyph(
       ^^^^^^^^^^^^^^^^^^^^
  File "/Users/simon/work/noto/math/venv/lib/python3.12/site-packages/glyphsLib/glyphdata.py", line 121, in get_glyph
    attributes = _lookup_attributes(glyph_name, data)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/simon/work/noto/math/venv/lib/python3.12/site-packages/glyphsLib/glyphdata.py", line 164, in _lookup_attributes
    data.names.get(glyph_name)
    ^^^^^^^^^^
AttributeError: 'list' object has no attribute 'names'

ouch, my bad.
I think we should avoid re-parsing it twice; if it's a list of str, interpret as paths as before, if it's already a GlyphData object, use as is

@simoncozens let me know if you are working on a fix already, so we don't duplicate work

Not planning to work on in for the forseeable.

ok i'll fix that thanks