Created
April 18, 2022 03:00
-
-
Save arrowtype/6e9331195a125ef8729d3b5063505cc8 to your computer and use it in GitHub Desktop.
A script to quickly copy specific kerns between fonts. A bit kludgy... be sure to use Git to know what’s happening to your fonts!
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
""" | |
A script to run on UFO files from a command line. Go through a pair list and | |
copy kerns from one UFO to one other UFO, if those kern pairs are in the pair list. | |
What happens: | |
- copy groups from ufoToCopyFrom to ufoToCopyTo | |
- for each pair in the pair list, copy kern from ufoToCopyFrom to ufoToCopyTo | |
Usage: | |
python "DIR/PATH/copy-kerning-in-pair_list-without-metrics_machine.py" "DIR/PATH/pair_list.txt" "ufo/to/copy/from" "ufo/to/copy/to" | |
Required argument: Pair list, as exported from Metrics Machine, e.g. | |
``` | |
#KPL:P: UC and Punc | |
H hyphen | |
hyphen H | |
H parenleft | |
# etc | |
``` | |
Required argument: File path of UFO to copy groups from | |
Optional argument: Dir path of UFOs to copy to (if not set, this assumes | |
you wish to copy to all UFOs in the same dir as the first one given) | |
Assumptions: | |
- You have a single group of UFOs | |
- You wish to exactly copy groups.plist from the UFO path to the other | |
- You wish to exactly copy kerning data from the pair list from | |
the first UFO to the other | |
- You are working with Git, so you can verify changes before | |
truly saving them | |
------------------------------------------------------------------------ | |
MIT License. | |
Copyright 2022 Arrow Type LLC / 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 | |
import os | |
import shutil | |
from fontParts.world import OpenFont, RFont | |
from ufonormalizer import normalizeUFO | |
# Deal with arguments | |
try: | |
if sys.argv[1].endswith(".txt"): | |
kerningPairFile = sys.argv[1] | |
else: | |
print("⚠️ You must include a kerning pair .txt file") | |
exit() | |
except IndexError: | |
print("⚠️ You must include a kerning pair .txt file") | |
exit() | |
try: | |
if sys.argv[2].endswith(".ufo"): | |
ufoToCopyFrom = sys.argv[2] | |
else: | |
print("⚠️ The second arg must be the path of UFO to copy groups & kerning from") | |
exit() | |
except IndexError: | |
print("⚠️ The second arg must be the path of UFO to copy groups & kerning from") | |
exit() | |
try: | |
if sys.argv[3].endswith(".ufo"): | |
ufoToCopyTo = sys.argv[3] | |
else: | |
print("⚠️ The third arg must be the path of UFO to copy groups & kerning into") | |
exit() | |
except IndexError: | |
print("⚠️ The third arg must be the path of UFO to copy groups & kerning into") | |
exit() | |
# ---------------------------------------------------------------- | |
# COPY GROUPS | |
if ufoToCopyTo != ufoToCopyFrom and ".ufo" in ufoToCopyTo and "sparse" not in ufoToCopyTo: | |
print(f"Copying groups to {ufoToCopyFrom}") | |
groupsPath = f"{ufoToCopyFrom}/groups.plist" | |
groupsDest = f"{ufoToCopyTo}/groups.plist" | |
shutil.copyfile(groupsPath, groupsDest) | |
# ---------------------------------------------------------------- | |
# COPY KERNS | |
# READ PAIR LIST | |
pairList = [] | |
pairsGrouped = {} | |
with open(kerningPairFile) as f: | |
data = f.read() | |
for line in data.split("\n"): | |
if line[0] != "#": | |
# maybe make line into tuple? | |
pairList.append(line.split(" ")) | |
# HELPERS | |
def getKernValue(font, pair): | |
"Try possible combinations to find the value of a given kerning pair, if it exists" | |
try: | |
font.kerning[(pair[0], pair[1])] | |
return font.kerning[(pair[0], pair[1])] | |
except KeyError: | |
pass | |
try: | |
return font.kerning[(f"public.kern1.{pair[0]}", pair[1])] | |
except KeyError: | |
pass | |
try: | |
return font.kerning[(pair[0], f"public.kern2.{pair[1]}")] | |
except KeyError: | |
pass | |
try: | |
return font.kerning[(f"public.kern1.{pair[0]}", f"public.kern2.{pair[1]}")] | |
except: | |
pass | |
return None | |
def setKernValue(font, pair, value): | |
# find if left/right of pair in groups | |
leftGroup = "" | |
rightGroup = "" | |
for group in font.groups.keys(): | |
if "kern1" in group and pair[0] in font.groups[group]: | |
leftGroup = group | |
if "kern2" in group and pair[1] in font.groups[group]: | |
rightGroup = group | |
# set kerning by group if you should | |
if leftGroup and rightGroup: | |
font.kerning[(leftGroup, rightGroup)] = value | |
elif leftGroup: | |
font.kerning[(leftGroup, pair[1])] = value | |
elif rightGroup: | |
font.kerning[(pair[0], rightGroup)] = value | |
# otherwise, just set lone pair | |
else: | |
font.kerning[(pair[0], pair[1])] = value | |
fontToCopyFrom = OpenFont(ufoToCopyFrom, showInterface=False) | |
if ufoToCopyTo != ufoToCopyFrom and ".ufo" in ufoToCopyTo and "sparse" not in ufoToCopyTo: | |
print("Copying kerning pairs to ", ufoToCopyTo) | |
fontToCopyTo = OpenFont(ufoToCopyTo, showInterface=False) | |
for pair in pairList: | |
valueToCopy = getKernValue(fontToCopyFrom, pair) | |
if valueToCopy: | |
setKernValue(fontToCopyTo, pair, valueToCopy) | |
fontToCopyTo.save() | |
# makes saved UFO not have changes outside of kern updates | |
normalizeUFO(ufoToCopyTo, writeModTimes=False) | |
fontToCopyTo.close() | |
print("All Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment