Created
December 9, 2020 14:23
-
-
Save jansindl3r/0670ad93a1106885dba8e5988b936765 to your computer and use it in GitHub Desktop.
subset
This file contains 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 sys | |
import argparse | |
from fontTools.subset import main as subsetter | |
from fontTools.ttLib import TTFont | |
from pathlib import Path | |
parser = argparse.ArgumentParser() | |
parser.add_argument('font_in', type=Path) | |
parser.add_argument('font_out', type=Path) | |
args = parser.parse_args() | |
font = TTFont(str(args.font_in)) | |
cmap = font.getBestCmap() | |
cmap_reversed = {v:k for k,v in cmap.items()} | |
keep = list('BCDFGHIJKLMNOPRSTUVWXY .bcdghijklmnopqrstuvwxy') | |
keep = list(map(ord, keep)) | |
# keep_extend = ['dotlessi', 'dotlessj', 'dotaccent'] | |
keep += [567, 305, 729] | |
base = Path(__file__).parent | |
replacer = cmap[46] # period | |
if font.get('CFF '): | |
cff = font['CFF '] | |
cff.desubroutinize() | |
content = cff.cff[cff.cff.keys()[0]] | |
for key in content.CharStrings.keys(): | |
if cmap_reversed.get(key) in keep: | |
continue | |
content.CharStrings[key] = content.CharStrings[replacer] | |
else: | |
keep = list(map(lambda x:cmap.get(x), keep)) | |
glyf = font['glyf'] | |
for glyphName in glyf.glyphOrder: | |
if glyphName in keep or replacer in glyphName: | |
continue | |
glyf[glyphName] = glyf[replacer] | |
font.save(args.font_out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment