Created
April 12, 2022 14:23
-
-
Save connordavenport/2c91fbca9de75465868647c2a062524a to your computer and use it in GitHub Desktop.
Swap onum and lnum features in SFNT file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from fontTools.ttLib import TTFont | |
''' | |
list of SFNTs | |
''' | |
fontList = [] | |
for path in fontList: | |
ext = os.path.splitext(path)[1] | |
savePath = path.replace(ext,f"-swapped{ext}") | |
f = TTFont(path) | |
GSUB = f["GSUB"] | |
lnum = [] | |
onum = [] | |
for r in GSUB.table.FeatureList.FeatureRecord: | |
if r.FeatureTag == "lnum": | |
lnum.append(r.Feature.LookupListIndex[0]) | |
for r in GSUB.table.FeatureList.FeatureRecord: | |
if r.FeatureTag == "onum": | |
onum.append(r.Feature.LookupListIndex[0]) | |
lnum = sorted(list(set(lnum))) | |
onum = sorted(list(set(onum))) | |
lnumMapping = None | |
onumMapping = None | |
lnumSubtable = None | |
onumSubtable = None | |
if lnum and onum: | |
for LookupID in lnum: | |
lnumLookup = GSUB.table.LookupList.Lookup[LookupID] | |
for Subtable in lnumLookup.SubTable: | |
if Subtable.LookupType == 1: | |
lnumSubtable = Subtable | |
lnumMapping = Subtable.mapping | |
else: | |
print("lnum lookup isn't type1") | |
for LookupID in onum: | |
onumLookup = GSUB.table.LookupList.Lookup[LookupID] | |
for Subtable in onumLookup.SubTable: | |
if Subtable.LookupType == 1: | |
onumSubtable = Subtable | |
onumMapping = Subtable.mapping | |
else: | |
print("onum lookup isn't type1") | |
if onumMapping and lnumMapping: | |
onumSubtable.mapping = lnumMapping | |
lnumSubtable.mapping = onumMapping | |
f.save(savePath) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment