From 8a87c9f4d10562e6e88dd64164333c7a068e60bf Mon Sep 17 00:00:00 2001 From: moi15moi Date: Fri, 4 Aug 2023 08:40:18 -0400 Subject: [PATCH] [font_parser] Use the same method as GDI for get_decoded_name --- font_collector/font_parser.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/font_collector/font_parser.py b/font_collector/font_parser.py index 21d4e34..0b2412b 100644 --- a/font_collector/font_parser.py +++ b/font_collector/font_parser.py @@ -421,12 +421,13 @@ def get_decoded_name(name: NameRecord) -> str: encoding = FontParser.get_name_encoding(name) - try: - return name.string.decode(encoding) - except UnicodeDecodeError: - utf_16_decoded = name.string.decode("utf_16_be") - to_decode = bytes(utf_16_decoded, encoding="raw_unicode_escape") - return to_decode.decode(encoding) + if name.platformID == 3 and encoding != "utf_16_be": + # I spoke with a Microsoft employee and he told me that GDI performed this processing: + name_to_decode = name.string.replace(b"\x00", b"") + else: + name_to_decode = name.string + + return name_to_decode.decode(encoding) @staticmethod def get_font_postscript_property(font_path: str, font_index: int) -> Optional[str]: