Skip to content

Instantly share code, notes, and snippets.

@arrowtype
Last active February 28, 2022 23:05
Show Gist options
  • Save arrowtype/561a3696a4ff319d1f6f9756c0a4a410 to your computer and use it in GitHub Desktop.
Save arrowtype/561a3696a4ff319d1f6f9756c0a4a410 to your computer and use it in GitHub Desktop.
A script to make all glyphs in a TTF blank, while leaving metrics, features, etc in place.
"""
A script to make all glyphs in a TTF blank, while leaving metrics, features, etc in place.
"""
from fontTools import ttLib
from fontTools.ttLib.tables._g_l_y_f import Glyph
from argparse import ArgumentParser
def eraseGlyphs(fontPath, inplace=False):
# open font to edit as TTFont object
font = ttLib.TTFont(fontPath)
for glyphName in font.getGlyphNames():
# check if the glyph is composite, and set it to an empty glyph object if not
# credit to @_justinpenner and @jenskutilek for pointing this out
if not font['glyf'][glyphName].isComposite():
font['glyf'][glyphName] = Glyph()
## Alternatively, you could make a blank shape in a pen, then set the glyph to that blank path.
## It’s not as good a solution as above, but I’m leaving it here for potential future reference. :D
# from fontTools.pens.ttGlyphPen import TTGlyphPen # move this to the top, obviously
# pen = TTGlyphPen(None) # make a new pen
# pen.lineTo((0,0)) # set up a single point
# pen.closePath() # close path
# font['glyf'][glyphName] = pen.glyph() # set glyph to the shape
# if the font is variable, delete the gvar table because it’s no longer needed, and deleting it will reduce font data by a lot
if 'gvar' in font:
del font['gvar']
# save font
if inplace:
font.save(fontPath)
print("\nGlyphs are now blank.\n")
else:
newPath = fontPath.replace('.ttf','.blank.ttf')
font.save(newPath)
print("Saved font with blank glyphs at ", newPath)
def main():
description = "Make all glyphs in a TTF blank, while leaving metrics, features, etc in place."
parser = ArgumentParser(description=description)
parser.add_argument('font', nargs=1)
parser.add_argument('--inplace', action='store_true')
args = parser.parse_args()
eraseGlyphs(args.font[0], args.inplace)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment