googlefonts / glyphsLib

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

Adding dictionary to font.userdata produce broken file #843

Closed HugoJourdan closed 1 year ago

HugoJourdan commented 1 year ago

I tried to use font.userdata to add a dictionary but it does not work properly and creates a broken file.

from glyphsLib import GSFont

glyphFilePath = "XXX.glyphs"
font = GSFont(glyphFilePath)
font.userData['com.glyphLib-TEST'] = {"KEY1":{0:["A","B","C"], 1:["A","B","C"]}, "KEY2":{0:["A","B","C"], 1:["A","B","C"]}}
font.save(glyphFilePath)

Capture d’écran 2023-01-03 à 12 10 08

openstep_plist.parser.ParseError: Expected terminating '}' for dictionary at line 4766

schriftgestalt commented 1 year ago

You should see a big backtrace when saving the file. The user data writer doesn't like numbers as keys. so this should work:

from glyphsLib import GSFont

glyphFilePath = "XXX.glyphs"
font = GSFont(glyphFilePath)
font.userData['com.glyphLib-TEST'] = {"KEY1":{"0":["A","B","C"], "1":["A","B","C"]}, "KEY2":{"0":["A","B","C"], "1":["A","B","C"]}}

font.save(glyphFilePath)
schriftgestalt commented 1 year ago

I get this when saving:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    font.save(glyphFilePath)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/classes.py", line 4340, in save
    w.write(self)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 52, in write
    self.writeDict(rootObject)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 58, in writeDict
    dictValue._serialize_to_plist(self)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/classes.py", line 4232, in _serialize_to_plist
    writer.writeObjectKeyValue(self, "userData", "if_true")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 120, in writeObjectKeyValue
    self.writeValue(value, key)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 142, in writeValue
    self.writeUserData(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 100, in writeUserData
    self.writeValue(value, key)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 146, in writeValue
    self.writeDict(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 76, in writeDict
    self.writeKeyValue(key, value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 106, in writeKeyValue
    self.writeValue(value, key)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 146, in writeValue
    self.writeDict(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 76, in writeDict
    self.writeKeyValue(key, value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 105, in writeKeyValue
    self.writeKey(key)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 165, in writeKey
    key = self.escape_string(key, None)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 169, in escape_string
    if _needs_quotes(string):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/glyphsLib/writer.py", line 342, in _needs_quotes
    if len(string) == 0:
TypeError: object of type 'int' has no len()
HugoJourdan commented 1 year ago

Great ! Thanks Georg