simoncozens / babelfont

Interrogate and manipulate UFO, TTF and OTF fonts with a common interface
Apache License 2.0
53 stars 9 forks source link

Error in loading older TTFs #33

Closed yanone closed 1 year ago

yanone commented 1 year ago

I'm not preparing a PR here because creating a branch of my babelfont fork includes my previous unmerged PR, and I'm not so good with Github to be able to easily separate that.

Several dozen Google Fonts have this issue (example: ofl/glegoo/Glegoo-Bold.ttf):

In [2]: font = babelfont.load("ofl/abhayalibre/AbhayaLibre-Bold.ttf")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 1
----> 1 font = babelfont.load("ofl/abhayalibre/AbhayaLibre-Bold.ttf")

File ~/.pyenv/versions/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/babelfont/__init__.py:17, in load(filename)
     16 def load(filename):
---> 17     return Convert(filename).load()

File ~/.pyenv/versions/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/babelfont/convertors/__init__.py:70, in Convert.load(self, **kwargs)
     68 for c in self.convertors:
     69     if c.can_load(self, **kwargs):
---> 70         return c.load(self, **kwargs)
     71 raise NotImplementedError

File ~/.pyenv/versions/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/babelfont/convertors/__init__.py:26, in BaseConvertor.load(cls, convertor)
     24 self.filename = convertor.filename
     25 self.scratch = convertor.scratch
---> 26 return self._load()

File ~/.pyenv/versions/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/babelfont/convertors/truetype.py:41, in TrueType._load(self)
     39 self._load_fvar()
     40 self._load_head()
---> 41 self._load_masters()
     42 self._load_names()
     43 self._load_glyphs()

File ~/.pyenv/versions/3.11.1/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/babelfont/convertors/truetype.py:78, in TrueType._load_masters(self)
     75 m = Master(location={},name="Default", id=str(uuid.uuid1()) )
     76 # Metrics
     77 m.metrics = {
---> 78     "xHeight": self.tt["OS/2"].sxHeight if getattr(self.tt["OS/2"], "sxHeight") else 0,
     79     "capHeight": self.tt["OS/2"].sCapHeight if getattr(self.tt["OS/2"], "sCapHeight") else 0,
     80     "ascender": self.tt["hhea"].ascender,
     81     "descender": self.tt["hhea"].descender
     82 }
     83 m.font = self.font
     84 self.font.masters = [m]

AttributeError: 'table_O_S_2f_2' object has no attribute 'sxHeight'

Checking for the attribute helps:

        m.metrics = {
            "xHeight": self.tt["OS/2"].sxHeight if hasattr(self.tt["OS/2"], "sxHeight") else None,
            "capHeight": self.tt["OS/2"].sCapHeight if hasattr(self.tt["OS/2"], "sCapHeight") else None,
            "ascender": self.tt["hhea"].ascender,
            "descender": self.tt["hhea"].descender
        }