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

Fails to parse TTF file -> "Error while parsing file: requested 1 bytes, but only 0 bytes available" #8

Closed
N00BIER opened this issue Jan 16, 2024 · 6 comments
Assignees

Comments

@N00BIER
Copy link

N00BIER commented Jan 16, 2024

No description provided.

@Dvd848
Copy link
Owner

Dvd848 commented Jan 16, 2024

Thanks for reporting this!

I tried a few ttf files but couldn't reproduce the exact error.
I was able to reproduce two other errors instead:

  • Roboto ->"'utf-16-be' codec can't decode byte 0x68 in position 4: truncated data"
  • Courier New -> "'ascii' codec can't decode byte 0xa9 in position 1: ordinal not in range(128)"

From a quick look, both of these errors seem related to the way the core Kaitai library itself handles the files. I'll double check this again later this week. For what it's worth, I was able to bypass the errors by applying the following patch:

diff --git a/pytai/kaitai/formats/ttf.py b/pytai/kaitai/formats/ttf.py
index dad7930..9b97063 100644
--- a/pytai/kaitai/formats/ttf.py
+++ b/pytai/kaitai/formats/ttf.py
@@ -276,7 +276,7 @@ class Ttf(KaitaiStruct):
                 _pos = io.pos()
                 io.seek((self._parent.ofs_strings + self.ofs_str))
                 self._debug['_m_ascii_value']['start'] = io.pos()
-                self._m_ascii_value = (io.read_bytes(self.len_str)).decode(u"ascii")
+                self._m_ascii_value = (io.read_bytes(self.len_str)).decode(u"ascii", errors="ignore")
                 self._debug['_m_ascii_value']['end'] = io.pos()
                 io.seek(_pos)
                 return getattr(self, '_m_ascii_value', None)
@@ -290,7 +290,7 @@ class Ttf(KaitaiStruct):
                 _pos = io.pos()
                 io.seek((self._parent.ofs_strings + self.ofs_str))
                 self._debug['_m_unicode_value']['start'] = io.pos()
-                self._m_unicode_value = (io.read_bytes(self.len_str)).decode(u"utf-16be")
+                self._m_unicode_value = (io.read_bytes(self.len_str)).decode(u"utf-16be", errors="ignore")
                 self._debug['_m_unicode_value']['end'] = io.pos()
                 io.seek(_pos)
                 return getattr(self, '_m_unicode_value', None)
  1. Does the workaround above work for your file as well?
  2. If not, would it be possible for you to attach the problematic ttf file?

Thanks!

@N00BIER
Copy link
Author

N00BIER commented Jan 17, 2024

Thanks for a quick turnaround!

  • Roboto ->"'utf-16-be' codec can't decode byte 0x68 in position 4: truncated data"
  • Courier New -> "'ascii' codec can't decode byte 0xa9 in position 1: ordinal not in range(128)"

I've had those too.

Have you pushed the update to the python repo, so I could upgrade it with pip --upgrade?

@Dvd848
Copy link
Owner

Dvd848 commented Jan 17, 2024

Ideally I'd like to make sure that we fix all known TTF parsing issues before releasing a new official release.
Would you be able to test the fix on your font by running the following (or alternatively attach it here)?

git clone https://github.com/Dvd848/pytai.git pytai_test && cd pytai_test/
sed -i -e 's/decode(u"\([^"]*\)")/decode(u"\1", errors="ignore")/g' pytai/kaitai/formats/ttf.py
python3 __main__.py /path/to/your/font.ttf -kf ttf

Be sure to replace /path/to/your/font.ttf with the correct path.
Thanks!

@N00BIER
Copy link
Author

N00BIER commented Jan 23, 2024

Ideally I'd like to make sure that we fix all known TTF parsing issues before releasing a new official release. Would you be able to test the fix on your font by running the following (or alternatively attach it here)?

git clone https://github.com/Dvd848/pytai.git pytai_test && cd pytai_test/
sed -i -e 's/decode(u"\([^"]*\)")/decode(u"\1", errors="ignore")/g' pytai/kaitai/formats/ttf.py
python3 __main__.py /path/to/your/font.ttf -kf ttf

Be sure to replace /path/to/your/font.ttf with the correct path. Thanks!

Now, seems to be working fine. Thanks a lot for your effort!

Dvd848 added a commit that referenced this issue Jan 23, 2024
@Dvd848
Copy link
Owner

Dvd848 commented Jan 23, 2024

Thanks for your feedback!
After diving a bit more into the issue, I chose to apply a different workaround.
I've already submitted the workaround to the repository, and it can be tested with:

git clone https://github.com/Dvd848/pytai.git pytai_test && cd pytai_test/
python3 __main__.py /path/to/your/font.ttf -kf ttf

I'll hopefully be able to release a new official version to PyPI some time later this week.
If you want, feel free to check your font with the latest code and let me know if you encounter any issues with the revised workaround.

@Dvd848 Dvd848 self-assigned this Jan 23, 2024
@Dvd848
Copy link
Owner

Dvd848 commented Jan 26, 2024

Released v0.2.8 with the workaround.

@Dvd848 Dvd848 closed this as completed Jan 26, 2024
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

2 participants