Skip to content

Instantly share code, notes, and snippets.

@weiweihuanghuang
Created February 10, 2022 06:06
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 weiweihuanghuang/eed292a60662f51c52c95cf9a054cad1 to your computer and use it in GitHub Desktop.
Save weiweihuanghuang/eed292a60662f51c52c95cf9a054cad1 to your computer and use it in GitHub Desktop.
Show Kerning Pairs.py
#MenuTitle: Show Kerning Pairs
# -*- coding: utf-8 -*-
__doc__="""
Show Kerning Pairs for this glyph in a new tab.
"""
import GlyphsApp
import traceback
thisFont = Glyphs.font
Doc = Glyphs.currentDocument
selectedLayers = thisFont.selectedLayers
selectedMaster = thisFont.selectedFontMaster
masterID = selectedMaster.id
leftGroups = {}
rightGroups = {}
for g in thisFont.glyphs:
if g.rightKerningGroup:
group_name = g.rightKerningGroupId()
try:
leftGroups[group_name].append(g.name)
except:
leftGroups[group_name] = [g.name]
if g.leftKerningGroup:
group_name = g.leftKerningGroupId()
try:
rightGroups[group_name].append(g.name)
except:
rightGroups[group_name] = [g.name]
def nameMaker(kernGlyphOrGroup, side):
# if this is a kerning group
if kernGlyphOrGroup[0] == "@":
# right glyph, left kerning group
if side == "right":
try:
# return rightGroups[kernGlyphOrGroup][0]
return sorted(rightGroups[kernGlyphOrGroup], key=len)[0]
except:
pass
elif side == "left":
# left glyph, right kerning group
try:
# return leftGroups[kernGlyphOrGroup][0]
return sorted(leftGroups[kernGlyphOrGroup], key=len)[0]
except:
pass
else:
return thisFont.glyphForId_(kernGlyphOrGroup).name
editString = u""""""
editStringL = u""""""
editStringR = u""""""
for thisLayer in selectedLayers:
thisGlyph = thisLayer.parent
thisGlyphName = thisGlyph.name
rGroupName = str(thisGlyph.rightKerningGroup)
lGroupName = str(thisGlyph.leftKerningGroup)
# print "\t", rGroupName, lGroupName
kernPairListL = []
kernPairListSortedL = []
kernPairListR = []
kernPairListSortedR = []
for L in thisFont.kerning[ masterID ].keys():
try:
# if the this kerning-pair's left glyph matches rGroupName (right side kerning group of thisGlyph)
if rGroupName == L[7:] or rGroupName == thisFont.glyphForId_(L).name or thisFont.glyphForId_(L).name == thisGlyph.name:
# for every R counterpart to L in the kerning pairs of rGroupName
for R in thisFont.kerning[masterID][L].keys():
if thisFont.kerning[masterID][L][R] != 0:
kernPairListL += [nameMaker(R, "right")]
except:
# print traceback.format_exc()
pass
for R in thisFont.kerning[masterID][L].keys():
try:
# if the R counterpart (class glyph) of L glyph is the selectedGlyph
if lGroupName == R[7:] or lGroupName == thisFont.glyphForId_(R).name or thisFont.glyphForId_(R).name == thisGlyph.name:
if thisFont.kerning[masterID][L][R] != 0:
kernPairListR += [nameMaker(L, "left")]
except:
pass
kernPairListSortedL = [g.name for g in Font.glyphs if g.name in kernPairListL]
for everyGlyph in kernPairListSortedL:
editStringL += "/%s/%s " % (thisGlyphName, everyGlyph)
kernPairListSortedR = [g.name for g in Font.glyphs if g.name in kernPairListR]
for everyGlyph in kernPairListSortedR:
editStringR += "/%s/%s " % (everyGlyph, thisGlyphName)
editString = editStringL + "\n\n" + editStringR
thisFont.newTab(editString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment