Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to parse some TTF files #688

Open
Dvd848 opened this issue Jan 17, 2024 · 0 comments
Open

Failure to parse some TTF files #688

Dvd848 opened this issue Jan 17, 2024 · 0 comments

Comments

@Dvd848
Copy link

Dvd848 commented Jan 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant