Skip to content

Instantly share code, notes, and snippets.

@arrowtype
Last active May 29, 2023 21:55
Show Gist options
  • Save arrowtype/9fefe9633cae500bbaf0000230f6a3ed to your computer and use it in GitHub Desktop.
Save arrowtype/9fefe9633cae500bbaf0000230f6a3ed to your computer and use it in GitHub Desktop.
A Python script to set the default instance of a variable font’s Weight axis to 400 (Regular).
"""
A script to set the default instance of a variable font’s wght axis to 400 (Regular).
From https://gist.github.com/arrowtype/9fefe9633cae500bbaf0000230f6a3ed
This can perhaps be more intuitive to designers who expect "Regular" to be the default weight of a variable font,
or for web developers who don’t set a weight range in their @font-face webfont setup.
This could be easily adapted to set defaults along other axes. It simply uses the FontTools Instancer module.
Warning: this slightly increases the filesize on a wght-only variable font,
and can really increase file size on a multi-axes variable font.
Given a Latin glyph set of around 800 glyphs, quick tests suggest a file size increases of about:
- 10–20% for a 2-source variable font (e.g. wght axis only)
- 25% for a 6-source variable font (e.g. wght and opsz axes)
- 50% for a 24-source variable font (i.e. Arrow Type Recursive – but the difference is just 22% if both before/after fonts are woff2 compressed)
USAGE:
- Use pip to install FontTools (https://github.com/fonttools/fonttools)
- Run this script from the command line: python3 set-default-wght-to-400.py "PATH_TO_VARIABLE_FONT"
LICENSE:
Copyright 2022 Arrow Type / Stephen Nixon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
from fontTools.ttLib import TTFont
from fontTools.varLib import instancer
# get fontpath passed in
fontpath = sys.argv[-1]
# open the font in memory
varfont = TTFont(fontpath)
# determine the min and max wght axis values
for axis in varfont["fvar"].axes:
if axis.axisTag == "wght":
minWght = axis.minValue
maxWght = axis.maxValue
oldDflt = axis.defaultValue
# create a new font with a default of 400 for the wght axis
newFont = instancer.instantiateVariableFont(varfont, {"wght": (minWght, 400, maxWght)})
# --------------------------------------------------------------------------
# STUFF TO UPDATE THE FONT NAMING FOR THE NEW DEFAULT STYLE
def getFontNameID(font, ID, platformID=3, platEncID=1):
name = str(font['name'].getName(ID, platformID, platEncID))
return name
def setFontNameID(font, ID, newName, macOnly=False):
print(f"\n\t• name {ID}:")
macIDs = {"platformID": 3, "platEncID": 1, "langID": 0x409}
winIDs = {"platformID": 1, "platEncID": 0, "langID": 0x0}
oldMacName = font['name'].getName(ID, *macIDs.values())
oldWinName = font['name'].getName(ID, *winIDs.values())
if oldMacName != newName:
print(f"\n\t\t Mac name was '{oldMacName}'")
font['name'].setName(newName, ID, *macIDs.values())
print(f"\n\t\t Mac name now '{newName}'")
# if name is macOnly, end the update before assigning Windows naming
if macOnly:
return
if oldWinName != newName:
print(f"\n\t\t Win name was '{oldWinName}'")
font['name'].setName(newName, ID, *winIDs.values())
print(f"\n\t\t Win name now '{newName}'")
def updateNaming(font):
# TODO? this could be a lot smarter, e.g. going through font["fvar"].instances to find
# the default style, and the associated name, and use that to replace name strings.
# But... it should work for some fonts, where Name ID 17 is just a single word, e.g. "Bold" but not "Condensed Bold"
# update name 17
oldPreferredStyleName = getFontNameID(font, 17)
# decide new preferred style name
if "Italic" in getFontNameID(font, 2):
# newPreferredStyleName = "Regular Italic" # specific to any font that uses "Regular Italic" as a style name
newPreferredStyleName = "Italic" # specific to any font that uses "Italic" as a style name
else:
newPreferredStyleName = "Regular"
setFontNameID(font, 17, newPreferredStyleName)
# update name 6 - postscript
oldPostscriptName = getFontNameID(font, 6)
newPostscriptName = oldPostscriptName.replace(oldPreferredStyleName.replace(" ",""), newPreferredStyleName.replace(" ",""))
setFontNameID(font, 6, newPostscriptName)
# update name 4
oldFullName = getFontNameID(font, 4)
newFullName = oldFullName.replace(oldPreferredStyleName, newPreferredStyleName)
setFontNameID(font, 4, newFullName)
# update name 3 - unique name
oldUniqueName = getFontNameID(font, 3)
newUniqueName = oldUniqueName.replace(oldPostscriptName, newPostscriptName)
setFontNameID(font, 3, newUniqueName)
# update name 2
oldBasicStyleName = getFontNameID(font, 2)
newBasicStyleName = oldBasicStyleName.replace(oldPreferredStyleName, newPreferredStyleName)
setFontNameID(font, 2, newBasicStyleName)
# update name 1
oldUniqueName = getFontNameID(font, 1)
newUniqueName = oldUniqueName.replace(oldPostscriptName, newPostscriptName)
setFontNameID(font, 1, newUniqueName)
updateNaming(newFont)
# save the new font, with same path as input font
newFont.save(fontpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment