Last active
June 26, 2020 13:29
-
-
Save arrowtype/306107a1a87890b2736ffe150177d8a4 to your computer and use it in GitHub Desktop.
A script to run on UFO files from a command line. Goes through a pair list and copy kerns from one UFO to others, if those kern pairs are in the pair list.
This file contains hidden or 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. Goes through a pair list and | |
copy kerns from one UFO to others, if those kern pairs are in the pair list. | |
What happens: | |
- copy groups from ufoToCopyFrom to each other UFOs | |
- for each pair in the pair list, copy kern from ufoToCopyFrom to each other UFOs | |
--- | |
USAGE: | |
python "DIR/PATH/copy-kerning-in-pair_list-without-metrics_machine.py" "DIR/PATH/pair_list.txt" "ufo/to/copy/from" "dir/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: | |
- Your kerning groups include exact glyph names from your font, | |
e.g. "public.kern1.quotesingle", NOT "public.kern1.quotes_basic". | |
- You wish to exactly copy groups.plist from the UFO path to all | |
other UFOs in the same directory (to ensure similar groups). | |
- You wish to exactly copy kerning data from the pair list from | |
the UFO path to all UFOs in a specified directory. | |
- You are working with Git, so you can spot-check changes before | |
truly saving them. | |
------------------------------------------------------------------------ | |
MIT License. | |
Copyright 2020 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") | |
except IndexError: | |
print("You must include a kerning pair .txt file") | |
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") | |
except IndexError: | |
print("The second arg must be the path of UFO to copy groups & kerning from") | |
try: | |
if sys.argv[3]: | |
print("Copying from UFO to UFOs in another Directory") | |
dirToCopyTo = sys.argv[3] | |
ufosToCopyTo = next(os.walk(dirToCopyTo))[1] | |
head, tail = dirToCopyTo, os.path.split(ufoToCopyFrom)[1] | |
except IndexError: | |
print("Copying from UFO to UFOs in the same Directory") | |
head, tail = os.path.split(ufoToCopyFrom) | |
ufosToCopyTo = next(os.walk(head))[1] | |
# ---------------------------------------------------------------- | |
# COPY GROUPS | |
groupsPath = f"{ufoToCopyFrom}/groups.plist" | |
for ufo in sorted(ufosToCopyTo): | |
ufoPath = f"{head}/{ufo}" | |
if ufoPath != ufoToCopyFrom and ".ufo" in ufoPath and "sparse" not in ufoPath: | |
print(ufoPath) | |
groupsDest = f"{ufoPath}/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: | |
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) | |
for ufo in sorted(ufosToCopyTo): | |
ufoPath = f"{head}/{ufo}" | |
if ufoPath != ufoToCopyFrom and ".ufo" in ufoPath and "sparse" not in ufoPath: | |
print("copying kerning pairs to ", ufoPath) | |
fontToCopyTo = OpenFont(ufoPath, 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(ufoPath, 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