I need to subset very many font files and I need to do that from within the python environment. Yet, Fonttools is very poorly documented and I cannot find a module and the proper function syntax to perform subsetting based on unicode from within python, not as a command line tool (pyftsubset
). Some of my files contain various errors when read by the Fonttools and I cannot catch exceptions using !command
inside jupyter.
3 Answers
pyftsubset
is itself just a Python script, which calls fontTools.subset.main
, which in turn parses sys.argv
(command-line args) to perform subsetting. You can do the same thing pretty easily in your own script, for example:
import sys
from fontTools.subset import main as ss
sys.argv = [None, '/path/to/font/file.ttf', '--unicodes=U+0020-002F']
ss() # this is what actually does the subsetting and writes the output file
Obviously you'll want to use your own values for --unicodes
plus the numerous other pyftsubset
options, but in general this scheme should work. Possible caveat is if you have other parts of your program that use/rely on sys.argv
; if that's the case you might want to capture the initial values in another variable before modifying sys.argv
and calling the subsetter, then re-set it to the initial values after.
-
Thank you for your solution. Is it actually possible to run it more elegantly, i.e. access the function that actually does the subsetting? I checked the source code and the subset module's
__main__.py
references itself somehow. Here,from __future__ import print_function, division, absolute_import from fontTools.misc.py23 import * import sys from fontTools.subset import main if __name__ == '__main__': sys.exit(main())
Mar 7, 2019 at 11:03 -
1The way to do that would be to create your own
fontTools.subset.Subset
object, and populate it with the values you want, then call that object'ssubset()
method on afontTools.ttLib.TTFont
object that you create...all of which, to me, anyway, seems less elegant than simply settingsys.argv
with well-documented command-line options, then callingfontTools.subset.main()
with a font path. Mar 7, 2019 at 14:48
I think that should be a pythonic way to do it properly:
from fontTools import subset
subsetter = subset.Subsetter()
subsetter.populate(unicodes=["U+0020", "U+0021"])
subsetter.subset(font)
While font
is your TTFont and you might need to check the documentation for how to exactly pass in the the list of unicodes. I didn’t test this exact code, but I tested it with subsetter.populate(glyphs=["a", "b"])
which does a similar job, but with glyphNames instead. The populate
method can take these arguments as documented: populate(self, glyphs=[], gids=[], unicodes=[], text='')
I found a clue to that in this discussion.
-
1Note that the way to instantiate
font
isfont = subset.load_font("path/to/font", options)
whereoptions
is asubset.Options
; defaults can be had withsubset.load_font("path/to/font", subset.Options())
May 3, 2022 at 4:08
In 2023 the proper way how to call fonttools subset from python is to use standard interface, similar to command line:
from fontTools import subset
args = [
"font.woff2",
"--unicodes=5f-7a,30-39,e8a6,e1b1,e5cf,e15b,e5c4,e8fd",
"--no-layout-closure",
"--output-file=./out.woff2",
"--flavor=woff2",
]
subset.main(args)