Skip to content

Failure to parse some TTF files #688

@Dvd848

Description

@Dvd848

The Python TTF parser fails to parse some TTF files (e.g. Roboto).

Steps to reproduce:

(ttf_venv) user@user-VirtualBox:~/temp$ python3 -m pip install kaitaistruct
Collecting kaitaistruct
  Downloading kaitaistruct-0.10-py2.py3-none-any.whl (7.0 kB)
Installing collected packages: kaitaistruct
Successfully installed kaitaistruct-0.10
(ttf_venv) user@user-VirtualBox:~/temp$ wget https://github.com/google/fonts/raw/d27d4149a40a9db16637f493f039255f5cb52d1f/ofl/roboto/Roboto%5Bwdth,wght%5D.ttf -q -O roboto.ttf
(ttf_venv) user@user-VirtualBox:~/temp$ wget http://formats.kaitai.io/ttf/src/python/ttf.py -q
(ttf_venv) user@user-VirtualBox:~/temp$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ttf import Ttf
>>> data = Ttf.from_file("roboto.ttf")
>>> data._read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/temp/ttf.py", line 28, in _read
    self.directory_table.append(Ttf.DirTableEntry(self._io, self, self._root))
  File "/home/user/temp/ttf.py", line 347, in __init__
    self._read()
  File "/home/user/temp/ttf.py", line 350, in _read
    self.tag = (self._io.read_bytes(4)).decode(u"ascii")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf5 in position 3: ordinal not in range(128)

For what it's worth, the problem can be worked around by modifying the decode()s to ignore errors:

(ttf_venv) user@user-VirtualBox:~/temp$ sed -i -e 's/decode(u"\([^"]*\)")/decode(u"\1", errors="ignore")/g' ttf.py
(ttf_venv) user@user-VirtualBox:~/temp$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ttf import Ttf
>>> data = Ttf.from_file("roboto.ttf")
>>> data._read()
>>> data
<ttf.Ttf object at 0x7f68577cc460>

I'm not sure if this can be considered a real fix though.

Originally found as part of a pytai issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions