Skip to content

Instantly share code, notes, and snippets.

@frankrolf
Created August 12, 2020 09:30
Show Gist options
  • Save frankrolf/e417af1406704e61a11c4238e578ca97 to your computer and use it in GitHub Desktop.
Save frankrolf/e417af1406704e61a11c4238e578ca97 to your computer and use it in GitHub Desktop.
Robofont: startup script to find out which kerning group the selected glyph belongs to
'''
Robofont startup script to find out which kerning group
the selected glyph belongs to.
'''
import AppKit
from mojo.UI import CurrentFontWindow
from mojo.events import addObserver
from mojo.roboFont import CurrentFont
class KernGroupContextualMenu(object):
'''
This follows
https://robofont.com/documentation/building-tools/toolspace/observers/custom-font-overview-contextual-menu/
'''
def __init__(self):
addObserver(
self,
"fontOverviewAdditionContextualMenuItems",
"fontOverviewAdditionContextualMenuItems"
)
def fontOverviewAdditionContextualMenuItems(self, notification):
font = CurrentFont()
if font.selectedGlyphNames:
myMenuItems = [
("L kerning group",
self.add_attribute(self.kerning_glyph_group, font, 'R')),
("R kerning group",
self.add_attribute(self.kerning_glyph_group, font, 'L')),
]
notification["additionContextualMenuItems"].extend(myMenuItems)
def add_attribute(self, func, font, attribute):
'''
Allows adding a font and an attribute to a callback,
such as the side of the kerning group. This avoids writing a function
for each side.
'''
def wrapper(sender):
func(sender, font, attribute)
return wrapper
def make_query(self, glyph_list):
'''
Filter the font overview, just like when searching it
'''
query_text = 'Name in {"%s"}' % '", "'.join(glyph_list)
query = AppKit.NSPredicate.predicateWithFormat_(query_text)
CurrentFontWindow().getGlyphCollection().setQuery(query)
def kerning_glyph_group(self, sender, font, side):
'''
make the selection, heavily based on groups.findGlyph
'''
gname = font.selectedGlyphNames[0]
if len(font.selectedGlyphNames) > 1:
print('using first selected glyph:', gname)
containing_groups = font.groups.findGlyph(gname)
if containing_groups:
if side == 'L':
kern_group = next((
gr_name for gr_name in containing_groups if
gr_name.startswith('public.kern1.')), None)
if kern_group:
self.make_query(font.groups.get(kern_group))
else:
self.make_query([gname])
elif side == 'R':
kern_group = next((
gr_name for gr_name in containing_groups if
gr_name.startswith('public.kern2.')), None)
if kern_group:
self.make_query(font.groups.get(kern_group))
else:
self.make_query([gname])
KernGroupContextualMenu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment