Skip to content

Instantly share code, notes, and snippets.

@frankrolf
Last active January 17, 2023 13:09
Show Gist options
  • Save frankrolf/8a4ee825f22db3fa7be68e1585b9b5d6 to your computer and use it in GitHub Desktop.
Save frankrolf/8a4ee825f22db3fa7be68e1585b9b5d6 to your computer and use it in GitHub Desktop.
Toggle Metrics Machine pair list
'''
Toggle the pair list of an open MetricsMachine (extension) window
from all kerning pairs (sorted by glyph order) to exceptions and back.
'''
import metricsMachine
import mm4
f = CurrentFont()
def get_exception_pairs(f, kerning_pairs):
'''
Find kerning exceptions.
'''
exceptions = []
groups_l = [gr for gr in f.groups.keys() if gr.startswith('public.kern1')]
groups_r = [gr for gr in f.groups.keys() if gr.startswith('public.kern2')]
grouped_l = sum([list(f.groups.get(gr)) for gr in groups_l], [])
grouped_r = sum([list(f.groups.get(gr)) for gr in groups_r], [])
for pair in kerning_pairs:
left, right = pair
if any([left in grouped_l, right in grouped_r]):
exceptions.append(pair)
return exceptions
def get_repr_glyph(item_name, glyph_order):
'''
Get a representative glyph name for a given group.
The level of importance is deduced by the glyph’s position in the
glyph order: lower = better
In addition to the above, symmetrical glyphs like O, H will be preferred
over group members like D, C, B, E, etc.
'''
symmetrical = 'HOno'
glyph_list = f.groups.get(item_name, [item_name])
sorted_glyph_list = sorted(glyph_list, key=lambda g: glyph_order.index(g))
for gname in sorted_glyph_list:
if gname.split('.')[0] in symmetrical:
return gname
return sorted_glyph_list[0]
glyph_order = f.glyphOrder
mm_all_pairs = []
mm_all_pairs_flipped = []
mm_exc_pairs = []
sorted_kerning = sorted(
f.kerning.keys(), key=lambda p:
(
glyph_order.index(get_repr_glyph(p[0], glyph_order)),
glyph_order.index(get_repr_glyph(p[1], glyph_order))))
sorted_kerning_right = sorted(
f.kerning.keys(), key=lambda p:
(
glyph_order.index(get_repr_glyph(p[1], glyph_order)),
glyph_order.index(get_repr_glyph(p[0], glyph_order))))
for pair in get_exception_pairs(f, sorted_kerning):
left, right = pair
mm_exc_pairs.append((
get_repr_glyph(left, glyph_order),
get_repr_glyph(right, glyph_order)))
for pair in sorted_kerning:
left, right = pair
mm_all_pairs.append((
get_repr_glyph(left, glyph_order),
get_repr_glyph(right, glyph_order)))
for pair in sorted_kerning_right:
left, right = pair
mm_all_pairs_flipped.append((
get_repr_glyph(left, glyph_order),
get_repr_glyph(right, glyph_order)))
if mm_exc_pairs:
pair_lists = [mm_all_pairs, mm_exc_pairs, mm_all_pairs_flipped]
else:
pair_lists = [mm_all_pairs_flipped, mm_all_pairs]
try:
current_pairlist = metricsMachine.GetPairList()
if current_pairlist in pair_lists:
cp_index = pair_lists.index(current_pairlist)
else:
cp_index = 0
next_pairlist = pair_lists[(cp_index + 1) % len(pair_lists)]
# if metricsMachine.GetPairList() == mm_all_pairs:
# metricsMachine.SetPairList(mm_exc_pairs, f)
# else:
metricsMachine.SetPairList(next_pairlist, f)
except mm4.mmScripting.MetricsMachineScriptingError:
print(
'Please open Metrics Machine! '
'(I don’t know how to do that in a script)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment