Skip to content

Commit 1f31c1b

Browse files
committed
Workaround for issue #8: TTF Parsing
1 parent c77a605 commit 1f31c1b

File tree

6 files changed

+3565
-6
lines changed

6 files changed

+3565
-6
lines changed

pytai/kaitai/formats/ttf.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from kaitaistruct import KaitaiStruct, KaitaiStream, BytesIO
2929
import collections
3030
from enum import Enum
31+
import string
3132

3233

3334
if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 9):
@@ -276,7 +277,11 @@ def ascii_value(self):
276277
_pos = io.pos()
277278
io.seek((self._parent.ofs_strings + self.ofs_str))
278279
self._debug['_m_ascii_value']['start'] = io.pos()
279-
self._m_ascii_value = (io.read_bytes(self.len_str)).decode(u"ascii")
280+
try:
281+
self._m_ascii_value = (io.read_bytes(self.len_str)).decode(u"ascii")
282+
self._m_ascii_value = ''.join(filter(lambda x:x in string.printable, self._m_ascii_value))
283+
except UnicodeDecodeError:
284+
self._m_ascii_value = ''
280285
self._debug['_m_ascii_value']['end'] = io.pos()
281286
io.seek(_pos)
282287
return getattr(self, '_m_ascii_value', None)
@@ -290,7 +295,10 @@ def unicode_value(self):
290295
_pos = io.pos()
291296
io.seek((self._parent.ofs_strings + self.ofs_str))
292297
self._debug['_m_unicode_value']['start'] = io.pos()
293-
self._m_unicode_value = (io.read_bytes(self.len_str)).decode(u"utf-16be")
298+
try:
299+
self._m_unicode_value = (io.read_bytes(self.len_str)).decode(u"utf-16be")
300+
except UnicodeDecodeError:
301+
self._m_unicode_value = ''
294302
self._debug['_m_unicode_value']['end'] = io.pos()
295303
io.seek(_pos)
296304
return getattr(self, '_m_unicode_value', None)
@@ -1714,7 +1722,7 @@ def _read(self):
17141722
self.value = Ttf.Cmap.Subtable.TrimmedTableMapping(_io__raw_value, self, self._root)
17151723
self.value._read()
17161724
else:
1717-
self.value = self._io.read_bytes((self.length - 6))
1725+
self.value = self._io.read_bytes(max(self.length - 6, 0))
17181726
self._debug['value']['end'] = self._io.pos()
17191727

17201728
class ByteEncodingTable(KaitaiStruct):

pytai/tests/xml_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ def xml_to_str(root: ET.ElementTree) -> str:
106106

107107
def xml_from_file(path: str) -> ET.ElementTree:
108108
"""Return XML ElementTree from given file path"""
109-
with open(path) as f:
110-
return ET.fromstring(f.read())
109+
with open(path, encoding="utf-8") as f:
110+
return ET.fromstring(f.read())

tests/resources/ttf.ttf

458 KB
Binary file not shown.

0 commit comments

Comments
 (0)