Last active
November 26, 2022 20:54
-
-
Save arrowtype/04a77f85eee6cf85f72267ee18d21428 to your computer and use it in GitHub Desktop.
Remove instances with a non-default opsz location from a variable font, as a workaround to a current Safari bug (https://bugs.webkit.org/show_bug.cgi?id=247987)
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
""" | |
Remove instances with a non-default opsz location from a variable font, | |
in order to avoid Safari v16 bug (https://bugs.webkit.org/show_bug.cgi?id=247987) | |
NOTE: seems to trigger another Safari issue for wght + opsz fonts, so I’m currently only | |
using this on an opsz-only font. Do your own testing! | |
Assumes: | |
- exactly one font path is being fed in | |
- it’s a variable font | |
- the font includes an opsz axis | |
- you wish to save the changes directly in place | |
Tested on AT Kyrios (a font with only an opsz axis) and AT Name Sans (with wght plus opsz axes). | |
Requirements: | |
- fonttools | |
- brotli (if you want to feed in a woff2) | |
Usage: | |
python3 limit-fvar-to-default-opsz-instances.py YOUR_FONT_PATH_HERE.woff2 | |
Helpful reference code found in the FontTools Instancer, written by Cosimo Lupo, found at: | |
https://github.com/fonttools/fonttools/blob/ded595f03c9718082921c57cb8b4ff161df48a4f/Lib/fontTools/varLib/instancer/__init__.py#L963-L972 | |
""" | |
import sys | |
from fontTools.ttLib import TTFont | |
# pass in font path from command line | |
filepath = sys.argv[1] | |
# open font in memory with TTFont | |
font = TTFont(filepath) | |
# determine default opsz value | |
for axis in font["fvar"].axes: | |
if axis.axisTag == "opsz": | |
defaultOpsz = axis.defaultValue | |
# make blank list for adding instances we want to keep | |
newInstances = [] | |
# find instances with default opsz, add them to the list | |
for instance in font["fvar"].instances: | |
if instance.coordinates["opsz"] == defaultOpsz: | |
newInstances.append(instance) | |
# set the font’s fvar instances to our new, simplified list | |
font["fvar"].instances = newInstances | |
# save font to the same path as before | |
font.save(filepath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment