From 4f6d9d1523f238ff9e93503a7e1617fe941610de Mon Sep 17 00:00:00 2001 From: NightFurySL2001 <33471049+NightFurySL2001@users.noreply.github.com> Date: Thu, 8 Jun 2023 15:43:55 +0800 Subject: [PATCH] Use fontFeatures to extract OpenType features --- Lib/extractor/__init__.py | 2 ++ Lib/extractor/formats/opentype.py | 16 ++++++++++++++++ Lib/extractor/formats/ttx.py | 5 +++++ Lib/extractor/formats/type1.py | 4 ++++ Lib/extractor/formats/woff.py | 5 +++++ requirements.txt | 1 + 6 files changed, 33 insertions(+) diff --git a/Lib/extractor/__init__.py b/Lib/extractor/__init__.py index f78d8a6..397558c 100644 --- a/Lib/extractor/__init__.py +++ b/Lib/extractor/__init__.py @@ -43,6 +43,7 @@ def extractUFO( doGlyphs=True, doInfo=True, doKerning=True, + doFeatures=True, format=None, customFunctions={}, ): @@ -63,6 +64,7 @@ def extractUFO( doGlyphs=doGlyphs, doInfo=doInfo, doKerning=doKerning, + doFeatures=doFeatures, customFunctions=customFunctions.get(format, []), ) except: diff --git a/Lib/extractor/formats/opentype.py b/Lib/extractor/formats/opentype.py index 8cac61c..46dd501 100644 --- a/Lib/extractor/formats/opentype.py +++ b/Lib/extractor/formats/opentype.py @@ -11,6 +11,7 @@ from extractor.exceptions import ExtractorError from extractor.stream import InstructionStream from extractor.tools import RelaxedInfo, copyAttr +from fontFeatures.ttLib import unparse TRUETYPE_INSTRUCTIONS_KEY = "public.truetype.instructions" @@ -40,6 +41,7 @@ def extractFontFromOpenType( doGlyphs=True, doInfo=True, doKerning=True, + doFeatures=True, customFunctions=[], doInstructions=True, ): @@ -56,6 +58,9 @@ def extractFontFromOpenType( destination.groups.update(groups) destination.kerning.clear() destination.kerning.update(kerning) + if doFeatures: + features = extractOpenTypeFeatures(source) + destination.features.text = features for function in customFunctions: function(source, destination) if doInstructions: @@ -1054,3 +1059,14 @@ def _extractOpenTypeKerningFromKern(source): # there are no minimum values. kerning.update(subtable.kernTable) return kerning + + +# ------- +# Features +# ------- + + +def extractOpenTypeFeatures(source): + return unparse(source).asFea() + + diff --git a/Lib/extractor/formats/ttx.py b/Lib/extractor/formats/ttx.py index 6f52a29..9bbd1fc 100644 --- a/Lib/extractor/formats/ttx.py +++ b/Lib/extractor/formats/ttx.py @@ -2,6 +2,7 @@ extractOpenTypeInfo, extractOpenTypeGlyphs, extractOpenTypeKerning, + extractOpenTypeFeatures, ) @@ -23,6 +24,7 @@ def extractFontFromTTX( doGlyphs=True, doInfo=True, doKerning=True, + doFeatures=True, customFunctions=[], ): from fontTools.ttLib import TTFont, TTLibError @@ -38,6 +40,9 @@ def extractFontFromTTX( destination.groups.update(groups) destination.kerning.clear() destination.kerning.update(kerning) + if doFeatures: + features = extractOpenTypeFeatures(source) + destination.features.text = features for function in customFunctions: function(source, destination) source.close() diff --git a/Lib/extractor/formats/type1.py b/Lib/extractor/formats/type1.py index f4cfb4a..3aee394 100644 --- a/Lib/extractor/formats/type1.py +++ b/Lib/extractor/formats/type1.py @@ -26,6 +26,7 @@ def extractFontFromType1( doGlyphs=True, doInfo=True, doKerning=True, + doFeatures=False, customFunctions=[], ): source = T1Font(pathOrFile) @@ -39,6 +40,9 @@ def extractFontFromType1( # in theory, it could be retried from an AFM. # we need to find the AFM naming rules so that we can sniff for the file. pass + if doFeatures: + # Type1 does not have OpenType features + pass for function in customFunctions: function(source, destination) diff --git a/Lib/extractor/formats/woff.py b/Lib/extractor/formats/woff.py index fabe6a3..2a367d5 100644 --- a/Lib/extractor/formats/woff.py +++ b/Lib/extractor/formats/woff.py @@ -5,6 +5,7 @@ extractOpenTypeInfo, extractOpenTypeGlyphs, extractOpenTypeKerning, + extractOpenTypeFeatures, ) try: @@ -34,6 +35,7 @@ def extractFontFromWOFF( doGlyphs=True, doInfo=True, doKerning=True, + doFeatures=True, customFunctions=[], ): source = TTFont(pathOrFile) @@ -46,6 +48,9 @@ def extractFontFromWOFF( destination.groups.update(groups) destination.kerning.clear() destination.kerning.update(kerning) + if doFeatures: + features = extractOpenTypeFeatures(source) + destination.features.text = features for function in customFunctions: function(source, destination) source.close() diff --git a/requirements.txt b/requirements.txt index f004420..305e0b3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ fonttools defcon ufoLib2 +fontFeatures