Skip to content

Instantly share code, notes, and snippets.

@frankrolf
Created May 4, 2018 22:44
Show Gist options
  • Save frankrolf/ab04adf8dea79142e22208b39fc34042 to your computer and use it in GitHub Desktop.
Save frankrolf/ab04adf8dea79142e22208b39fc34042 to your computer and use it in GitHub Desktop.
Save UFO2 files from Robofont 3
import os
from defcon import Font
def remap_item_name(name):
'''
Remaps a single item name from public.kern style to @MMK style
'''
if 'public.kern1.' in name:
return name.replace('public.kern1.', '@MMK_L_')
elif 'public.kern2.' in name:
return name.replace('public.kern2.', '@MMK_R_')
else:
return name
def remap_groups(groups):
'''
Remaps groups dictionary to not contain
public.kern prefixes.
'''
remapped_groups = {}
for group_name, glyph_list in groups.items():
remapped_group_name = remap_item_name(group_name)
remapped_groups[remapped_group_name] = glyph_list
return remapped_groups
def remap_kerning(kerning):
'''
Remaps kerning dictionary to not contain
public.kern prefixes.
'''
remapped_kerning = {}
for (left, right), value in kerning.items():
remapped_pair = (
remap_item_name(left),
remap_item_name(right))
remapped_kerning[remapped_pair] = value
return remapped_kerning
f = CurrentFont()
ufo_folder, ufo_name = os.path.split(os.path.abspath(f.path))
ufo_basename, ufo_suffix = os.path.splitext(ufo_name)
ufo2_name = '{}_UFO2{}'.format(ufo_basename, ufo_suffix)
ufo2_path = os.path.join(ufo_folder, ufo2_name)
df = Font(f.path)
df.save(ufo2_path, formatVersion=2)
df2 = Font(ufo2_path)
# revert groups and kerning to UFO2 style
ufo_2_groups = remap_groups(df2.groups)
ufo_2_kerning = remap_kerning(df2.kerning)
df2.groups.clear()
df2.kerning.clear()
df2.groups.update(ufo_2_groups)
df2.kerning.update(ufo_2_kerning)
# revert mark color to Robofont UFO2 style
marked_glyphs = [g for g in df2 if 'public.markColor' in g.lib.keys()]
for g in marked_glyphs:
mark_str_list = g.lib['public.markColor'].split(',')
mark_val_list = [eval(v) for v in mark_str_list]
g.lib['com.typemytype.robofont.mark'] = mark_val_list
del g.lib['public.markColor']
df2.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment