googlefonts / glyphsLib

A bridge from Glyphs source files (.glyphs) to UFOs
Apache License 2.0
178 stars 51 forks source link

glyphsLib failing to add Italic Angle if metric isn’t already in a master ("ValueError: 'italic angle' is not in list") #998

Open arrowtype opened 2 months ago

arrowtype commented 2 months ago

I’m attempting to use glyphsLib to set the Italic Angle in masters of a Glyphs source. It seems to work if that metric is already in the masters, but it fails to add it if it doesn’t yet exist.

My code is basically this:

from glyphsLib import GSFont

path = "path/to/glyphs/font.glyphs"

font = GSFont(path)

for master in font.masters:
    master.italicAngle = angle

font.save(path)

However, when I run it, I get the following traceback:

Traceback (most recent call last):
  File "/Users/stephennixon/type-repos/VintageVoyageDesign/../font-tools/set-glyphs-italic-angle.py", line 37, in <module>
    main()
  File "/Users/stephennixon/type-repos/VintageVoyageDesign/../font-tools/set-glyphs-italic-angle.py", line 29, in main
    master.italicAngle = angle
    ^^^^^^^^^^^^^^^^^^
  File "/Users/stephennixon/.pyenv/versions/3.11.3/lib/python3.11/site-packages/glyphsLib/classes.py", line 1907, in italicAngle
    self._set_metric("italic angle", value)
  File "/Users/stephennixon/.pyenv/versions/3.11.3/lib/python3.11/site-packages/glyphsLib/classes.py", line 1782, in _set_metric
    metricIndex = metricLabels.index(metricname)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: 'italic angle' is not in list

This is surprising, as it looks like glyphsLib does try to add a metric if it doesn’t yet exist:

https://github.com/googlefonts/glyphsLib/blob/e2ebf5b517d59bec0c9437da3a748c58f2999911/Lib/glyphsLib/classes.py#L1780-L1782

If I manually add the "Italic Angle" metric using the GlyphsApp UI, the script then does work, but this then defeats the purpose of the script.

Am I doing something wrong? E.g. is it intended that my glyphsLib script should be adding that metric if it doesn’t yet exist? Or, am I interpreting it correctly that glyphsLib is trying but failing to add it, for some reason?

Thanks so much for taking a look and sharing any insights here!

anthrotype commented 2 months ago

looks like a bug:

https://github.com/googlefonts/glyphsLib/blob/e2ebf5b517d59bec0c9437da3a748c58f2999911/Lib/glyphsLib/classes.py#L1779-L1782

 metricLabels = [x.type for x in metrics] 
 if metricname not in metricLabels: 
     self.font.metrics.append(GSMetric(type=metricname)) 
+    metricLabels.append(metricname)
  metricIndex = metricLabels.index(metricname) 
schriftgestalt commented 2 months ago

This is working in the Glyphs3 branch.