From 84f66a2bec6131ec168e78deb3ea5cdfe988ed19 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Tue, 20 Dec 2022 12:32:54 +0100 Subject: [PATCH 1/3] Improve reading T1Font by testing on encoding Fontographer had macroman as default. + simplify glyphOrder extractor: python 3 dict keys are ordered --- Lib/extractor/formats/type1.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/Lib/extractor/formats/type1.py b/Lib/extractor/formats/type1.py index f4cfb4a..fe9c6c0 100644 --- a/Lib/extractor/formats/type1.py +++ b/Lib/extractor/formats/type1.py @@ -28,7 +28,14 @@ def extractFontFromType1( doKerning=True, customFunctions=[], ): - source = T1Font(pathOrFile) + try: + encoding = "ascii" + source = T1Font(pathOrFile, encoding=encoding) + source["FontInfo"] + except UnicodeDecodeError: + encoding = "macroman" + source = T1Font(pathOrFile, encoding=encoding) + destination.lib["public.glyphOrder"] = _extractType1GlyphOrder(source) if doInfo: extractType1Info(source, destination) @@ -159,24 +166,6 @@ def extractType1Glyphs(source, destination): # ----------- -class GlyphOrderPSInterpreter(PSInterpreter): - def __init__(self): - PSInterpreter.__init__(self) - self.glyphOrder = [] - self.collectTokenForGlyphOrder = False - - def do_literal(self, token): - result = PSInterpreter.do_literal(self, token) - if token == "/FontName": - self.collectTokenForGlyphOrder = False - if self.collectTokenForGlyphOrder: - self.glyphOrder.append(result.value) - if token == "/CharStrings": - self.collectTokenForGlyphOrder = True - return result - - def _extractType1GlyphOrder(t1Font): - interpreter = GlyphOrderPSInterpreter() - interpreter.interpret(t1Font.data) - return interpreter.glyphOrder + glyphSet = t1Font.getGlyphSet() + return list(glyphSet) From 96955ea15a9ecfe49de35cb10cbaa6a979acfa50 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Tue, 20 Dec 2022 12:33:49 +0100 Subject: [PATCH 2/3] remove unused variable --- Lib/extractor/formats/type1.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/extractor/formats/type1.py b/Lib/extractor/formats/type1.py index fe9c6c0..0f23767 100644 --- a/Lib/extractor/formats/type1.py +++ b/Lib/extractor/formats/type1.py @@ -29,12 +29,10 @@ def extractFontFromType1( customFunctions=[], ): try: - encoding = "ascii" - source = T1Font(pathOrFile, encoding=encoding) + source = T1Font(pathOrFile, encoding="ascii") source["FontInfo"] except UnicodeDecodeError: - encoding = "macroman" - source = T1Font(pathOrFile, encoding=encoding) + source = T1Font(pathOrFile, encoding="macroman") destination.lib["public.glyphOrder"] = _extractType1GlyphOrder(source) if doInfo: From 0e070a204086f4964cf5d31afa32059c8baa6804 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Tue, 20 Dec 2022 14:39:55 +0100 Subject: [PATCH 3/3] use macroman directly --- Lib/extractor/formats/type1.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Lib/extractor/formats/type1.py b/Lib/extractor/formats/type1.py index 0f23767..0fbb9d8 100644 --- a/Lib/extractor/formats/type1.py +++ b/Lib/extractor/formats/type1.py @@ -28,12 +28,7 @@ def extractFontFromType1( doKerning=True, customFunctions=[], ): - try: - source = T1Font(pathOrFile, encoding="ascii") - source["FontInfo"] - except UnicodeDecodeError: - source = T1Font(pathOrFile, encoding="macroman") - + source = T1Font(pathOrFile, encoding="macroman") destination.lib["public.glyphOrder"] = _extractType1GlyphOrder(source) if doInfo: extractType1Info(source, destination)