Skip to content

Instantly share code, notes, and snippets.

@ryanbugden
Created April 12, 2024 18:14
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/161803b226ebb1e7c2c93378aef3c620 to your computer and use it in GitHub Desktop.
Save ryanbugden/161803b226ebb1e7c2c93378aef3c620 to your computer and use it in GitHub Desktop.
Make new glyphs (ligatures?) with given input glyphs.
# menuTitle: Glyph Recipes
recipes = {
# Desired name, and recipe of what glyphs go in there
"fi": ('f', 'i'),
"f_l": ('f', 'l')
}
f = CurrentFont()
# A function the returns a kerning value, given two glyph names as input
def get_kerning(left, right):
if f.groups.findGlyph(left):
for group in f.groups.findGlyph(left):
if group in f.groups.side1KerningGroups:
left = group
break
if f.groups.findGlyph(right):
for group in f.groups.findGlyph(right):
if group in f.groups.side2KerningGroups:
right = group
break
if (left, right) in f.kerning.keys():
return f.kerning[(left, right)]
else:
return 0
# Loop through each recipe you put together at the top of the script.
for recipe_name, recipe in recipes.items():
# Start a new empty glyph object, and start the cursor metaphor
new_g = RGlyph()
cursor = 0
# Build the glyph with the names in the recipe, looping through each glyph name
for i, g_name in enumerate(recipe):
new_g.appendComponent(g_name, offset=(cursor, 0))
cursor += f[g_name].width
# If there's another glyph coming up, account for kerning
if len(recipe) > i + 1:
next_g_name = recipe[i + 1]
cursor += get_kerning(g_name, next_g_name)
# Insert the glyph object with the name you decided in the dictionary above
f.insertGlyph(new_g, name=recipe_name)
new_g = f[recipe_name]
# Automatically set the width
new_g.width = cursor
# Automatically set the unicode
new_g.autoUnicodes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment