Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Created January 15, 2024 20:40
Show Gist options
  • Save connordavenport/efc9a333dbccdcd1e4e8924f99e1ec8b to your computer and use it in GitHub Desktop.
Save connordavenport/efc9a333dbccdcd1e4e8924f99e1ec8b to your computer and use it in GitHub Desktop.
a script to set MetricsMachine pair lists with high gamut ranges. functions taken from Frank's Kern-a-lytics
import metricsMachine as mm
# --------------------------------
# functions taken from Kern-a-lytics
# --------------------------------
import itertools
import collections
def high_gamut_dict(cmb_kerning, approx_amount=100):
'''
Pairs with the highest kerning gamut
'''
output = collections.OrderedDict({})
gamut_dict = {}
for pair, values in cmb_kerning.items():
gamut = _gamut(values)
gamut_dict.setdefault(gamut, []).append(pair)
max_gamut_list = sorted(gamut_dict.keys(), reverse=True)
pair_count = 0
for gamut_value in max_gamut_list:
pairs = gamut_dict.get(gamut_value)
if pair_count < approx_amount:
for pair in pairs:
output[pair] = cmb_kerning.get(pair)
pair_count += len(pairs)
return output
def _gamut(value_list):
'''
Returns the maximum value distance in a list of values
'''
difference = 0
n_list = [i for i in value_list if i]
if any(n_list):
difference = (max(n_list) - min(n_list))
return difference
def _sort_kern_dict(input_dict):
'''
Returns sorted kerning as an OrderedDict
'''
sorted_dict = collections.OrderedDict(
sorted(input_dict.items(), key=lambda t: t[0]))
return sorted_dict
def get_combined_kern_dict(fonts):
'''
Returns a sorted, combined kerning dictionary for a
number of fonts. If a specific pair is not kerned,
kerning value is None.
'''
combined_pairs = set(itertools.chain.from_iterable(
font.kerning.keys() for font in fonts))
c_kerning = {}
for font in fonts:
for pair in combined_pairs:
value = font.kerning.find(pair, None)
c_kerning.setdefault(pair, []).append(value)
# make the dict an ordered dict, so we do not have
# to sort it downstream
return _sort_kern_dict(c_kerning)
# --------------------------------
# --------------------------------
combined = (get_combined_kern_dict(AllFonts()))
high_gamut_dict = high_gamut_dict(combined)
ps = []
for pair in high_gamut_dict.keys():
l,r = pair
if "public" in l:
l = sorted(CurrentFont().groups.get(l))[0]
if "public" in r:
r = sorted(CurrentFont().groups.get(r))[0]
ps.append((l,r))
mm.SetPairList(ps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment