Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Created February 14, 2024 16:08
Show Gist options
  • Save connordavenport/cc3d6dbd9af9151e8c841b91da061097 to your computer and use it in GitHub Desktop.
Save connordavenport/cc3d6dbd9af9151e8c841b91da061097 to your computer and use it in GitHub Desktop.
An RKerning add on to allow for setting kerning pairs without having to know the group names
def smartSet(self, pair, value, exceptionType=None):
'''
pair must be a tuple, its contents can be a glyphName or a group's name
value must be an integer, why would you even kern on fractions....
exceptionType is the level of searching the function will do
None : use the top level group or glyph names, no exceptions
g2G : glyph to Group exception
g2g : glyph to glyph exception
G2g : Group to glyph exception
'''
font = self.font
if not isinstance(value,int):
raise TypeError("value must be an integer")
if not isinstance(pair,tuple):
raise TypeError("pair must be a tuple")
l,r = pair
if not isinstance(l,str) and isinstance(r,str):
raise TypeError("items in pair must be a string")
if not exceptionType:
exceptionType = "find"
else:
if exceptionType in ["g2G", "g2g", "G2g"]:
pass
else:
raise TypeError("exception type unknown")
if "public.kern1" in l:
if l not in font.groups.keys():
raise TypeError("left group not in font")
leftGroup = l
else:
if l not in font.keys():
raise TypeError("left glyph not in font")
leftGlyph = l
leftGroup = l
for gs in font.groups.findGlyph(l):
if "public.kern1" in gs:
leftGroup = gs
if "public.kern2" in r:
if r not in font.groups.keys():
raise TypeError("right group not in font")
rightGroup = r
else:
if r not in font.keys():
raise TypeError("right glyph not in font")
rightGlyph = r
rightGroup = r
for gs in font.groups.findGlyph(r):
if "public.kern2" in gs:
rightGroup = gs
if exceptionType == "find":
kp = (leftGroup, rightGroup)
elif exceptionType == "g2G":
kp = (leftGlyph, rightGroup)
elif exceptionType == "g2g":
kp = (leftGlyph, rightGlyph)
elif exceptionType == "G2g":
kp = (leftGroup, rightGlyph)
else:
raise TypeError("exception type unknown")
font.kerning[kp] = value
# ------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------
from mojo.roboFont import RKerning
RKerning.smartSet = smartSet
# ------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment