Skip to content

Commit

Permalink
Workaround for issue #8: TTF Parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dvd848 committed Jan 23, 2024
1 parent c77a605 commit 1f31c1b
Show file tree
Hide file tree
Showing 6 changed files with 3,565 additions and 6 deletions.
14 changes: 11 additions & 3 deletions pytai/kaitai/formats/ttf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from kaitaistruct import KaitaiStruct, KaitaiStream, BytesIO
import collections
from enum import Enum
import string


if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 9):
Expand Down Expand Up @@ -276,7 +277,11 @@ def ascii_value(self):
_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")
try:
self._m_ascii_value = (io.read_bytes(self.len_str)).decode(u"ascii")
self._m_ascii_value = ''.join(filter(lambda x:x in string.printable, self._m_ascii_value))
except UnicodeDecodeError:
self._m_ascii_value = ''
self._debug['_m_ascii_value']['end'] = io.pos()
io.seek(_pos)
return getattr(self, '_m_ascii_value', None)
Expand All @@ -290,7 +295,10 @@ def unicode_value(self):
_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")
try:
self._m_unicode_value = (io.read_bytes(self.len_str)).decode(u"utf-16be")
except UnicodeDecodeError:
self._m_unicode_value = ''
self._debug['_m_unicode_value']['end'] = io.pos()
io.seek(_pos)
return getattr(self, '_m_unicode_value', None)
Expand Down Expand Up @@ -1714,7 +1722,7 @@ def _read(self):
self.value = Ttf.Cmap.Subtable.TrimmedTableMapping(_io__raw_value, self, self._root)
self.value._read()
else:
self.value = self._io.read_bytes((self.length - 6))
self.value = self._io.read_bytes(max(self.length - 6, 0))
self._debug['value']['end'] = self._io.pos()

class ByteEncodingTable(KaitaiStruct):
Expand Down
4 changes: 2 additions & 2 deletions pytai/tests/xml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ def xml_to_str(root: ET.ElementTree) -> str:

def xml_from_file(path: str) -> ET.ElementTree:
"""Return XML ElementTree from given file path"""
with open(path) as f:
return ET.fromstring(f.read())
with open(path, encoding="utf-8") as f:
return ET.fromstring(f.read())
Binary file added tests/resources/ttf.ttf
Binary file not shown.
Loading

0 comments on commit 1f31c1b

Please sign in to comment.