Reputation: 475
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.
Upvotes: 1
Views: 5908
Reputation: 1204
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)
Upvotes: 2
Reputation: 61
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.
Upvotes: 4
Reputation: 5690
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.
Upvotes: 9