Skip to content

Instantly share code, notes, and snippets.

@ryanbugden
Last active January 9, 2024 02:05
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 ryanbugden/02a2485c16ce519ee9b7b7379d1d1e6c to your computer and use it in GitHub Desktop.
Save ryanbugden/02a2485c16ce519ee9b7b7379d1d1e6c to your computer and use it in GitHub Desktop.
Save the kern value from the last pair you were on, for quick application to your current pair (with the Page-Down key). Install "store..." as a start-up script, and "set...py" as a normal script in the script folder.
# menuTitle: Set the Latest Kern Value
# shortCut: pagedown
from mojo.UI import getDefault
import metricsMachine as mm
KERN_VALUE_LIB_KEY = 'com.ryanbugden.storeLatestKernValue.kernValue'
def get_kern_group(f, glyph, side):
if side == 'left':
groups = f.groups.side1KerningGroups
if side == 'right':
groups = f.groups.side2KerningGroups
for key, value in groups.items():
if glyph in value:
glyph = key
break
return glyph
def convert_to_group_pair(f, pair):
l = get_kern_group(f, pair[0], 'left')
r = get_kern_group(f, pair[1], 'right')
return (l, r)
f = CurrentFont()
cp = mm.GetCurrentPair()
group_pair = convert_to_group_pair(f, cp)
try:
value = f.lib[KERN_VALUE_LIB_KEY]
f.kerning.update({group_pair: int(value)})
except:
self.f.lib[KERN_VALUE_LIB_KEY] = 0
# if group_pair in f.kerning.keys():
# f.kerning[group_pair] = int(value)
# else:
# f.kerning.update({group_pair: int(value)})
# menuTitle: (Start-up) Store Latest Kern Value
from mojo.UI import getDefault, setDefault, preferencesChanged
from mojo.events import addObserver
'''
Ryan Bugden
23.03.23
'''
KERN_VALUE_LIB_KEY = 'com.ryanbugden.storeLatestKernValue.kernValue'
def get_kern_group(f, glyph, side):
if side == 'left':
groups = f.groups.side1KerningGroups
if side == 'right':
groups = f.groups.side2KerningGroups
for key, value in groups.items():
if glyph in value:
glyph = key
break
return glyph
def convert_to_group_pair(f, pair):
l = get_kern_group(f, pair[0], 'left')
r = get_kern_group(f, pair[1], 'right')
return (l, r)
def get_kern_value(f, pair):
kern_value = 0
if pair in f.kerning.keys():
if f.kerning[pair] != None:
kern_value = f.kerning[pair]
return int(kern_value)
debug = False
class storeLastKernValue:
def __init__(self):
self.prev_kern = ((), 0)
addObserver(self, "pairChanged", "MetricsMachine.currentPairChanged")
def pairChanged(self, sender):
try:
from mm4.mmScripting import MetricsMachineScriptingError
import metricsMachine as mm
self.f = CurrentFont()
cp = mm.GetCurrentPair()
group_pair = convert_to_group_pair(self.f, cp)
# if the pair changed, store the previous pair value, and set it as the new previous
if group_pair != self.prev_kern[0]:
if debug == True:
print("Previous kern pair and value:", self.prev_kern)
# register the previous kern value
self.kern_value = get_kern_value(self.f, self.prev_kern[0])
self.f.lib[KERN_VALUE_LIB_KEY] = self.kern_value
# if the current pair isn't none, set it to the previous now
value = get_kern_value(self.f, group_pair)
self.prev_kern = (group_pair, value)
except (MetricsMachineScriptingError, ModuleNotFoundError) as se:
return
if debug == True:
print(f"Registered {KERN_VALUE_LIB_KEY} default right now:", self.f.lib[KERN_VALUE_LIB_KEY])
storeLastKernValue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment