Skip to content

Instantly share code, notes, and snippets.

@arrowtype
Created July 19, 2022 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrowtype/a09d638cbe1790792384b47723b64437 to your computer and use it in GitHub Desktop.
Save arrowtype/a09d638cbe1790792384b47723b64437 to your computer and use it in GitHub Desktop.
RoboFont script to generate a simplified MetricsMachine pair list from an input font and pair list.
"""
Perhaps useful for kerning secondary sources once primary sources are kerned.
E.g. kern numerals to caps in your Regular, then run this, then kern the new list in your Bold.
Or maybe useful for a second pass at kerning, if you only want to view items which have already been kerned.
Basic approach:
- Take in UFO(s),
- Take in existing MetricsMachine kerning pair list,
- Go through kern pair list and check which pairs are kerned in the font,
- Make and save new list with only those "relavent" pairs.
Disclaimers:
- May have issues, but it works for me right now, and might be helpful for others.
- Always back up your work before running strange Python scripts!
"""
from vanilla.dialogs import getFile
from mojo.UI import OutputWindow
import os
inputFonts = getFile("Select UFO(s) to check", allowsMultipleSelection=True, fileTypes=["ufo"])
pair_list_path = getFile("Select MM pair list", allowsMultipleSelection=False, fileTypes=["txt"])[0]
OutputWindow().show()
print("checking for kerns in pair list...", end="")
new_list = []
with open(pair_list_path) as f:
lines = f.readlines()
open_fonts = [font.path for font in AllFonts()]
print()
print()
print()
def findKernValue(font, pair):
"""
I can't find a method to check for a kern pair in RoboFont, so here's an attempt at one.
font is a font object.
pair is a tuple of glyph names.
"""
side1 = font.groups.findGlyph(pair[0])
side2 = font.groups.findGlyph(pair[1])
# check if side1 in a side1 group, otherwise, just use it directly
try:
side1 = [group for group in side1 if "kern1" in group][0]
except IndexError:
side1 = pair[0]
try:
side2 = [group for group in side2 if "kern2" in group][0]
except IndexError:
side2 = pair[1]
try:
pairKernValue = (font.kerning[(side1, side2)])
return pairKernValue
except KeyError:
return None
# go through text pair list; skip lines that are commented out
for line in lines:
line = line.replace("\n","")
if "#" not in line:
pair = tuple(line.split(" "))
for fontPath in inputFonts:
font = OpenFont(fontPath, showInterface=False)
kernValue = findKernValue(font, pair)
# if pair is in kerning...
if kernValue is not None:
# if pair isn't in new list, add it
if pair not in new_list:
new_list.append(line)
print(".", end="")
# don't close already-open fonts
if font.path not in open_fonts:
font.close()
new_pair_list = "\n".join(new_list)
new_pair_list_path = os.path.split(pair_list_path)[0] + "/" + os.path.split(pair_list_path)[1].replace('.txt','-only_kerns.txt')
with open(new_pair_list_path, 'w') as f:
f.write("#KPL:P: Simplified Pair List\n")
f.write(new_pair_list)
print(f"New list saved! Find it at: \n{new_pair_list_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment