Last active
June 17, 2023 16:07
-
-
Save typoman/d1d8e7c328690c0fda088e63af82127d to your computer and use it in GitHub Desktop.
A RoboFont script to expand kerning groups from base glyph to their composites.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from simpleKerning import * | |
""" | |
A RoboFont script to expand kerning groups from base glyph to their composites. | |
1. Select the composites which are not grouped | |
2. Run the script | |
3. The groups will be expanded to the composites, if: | |
- Their width is same as the base glyph | |
- They're not already grouped | |
""" | |
def getComps(glyphName, compMap): | |
""" | |
Get all the related components **recursively**. | |
""" | |
result = set() | |
inputGlyphs = set([glyphName]) | |
while inputGlyphs: | |
glyphName = inputGlyphs.pop() | |
comps = compMap.get(glyphName, set()) - result - inputGlyphs | |
if comps: | |
result.update(comps) | |
inputGlyphs.update(comps) | |
return result | |
def expandKerningGroupsFromBaseGlyphs(f, selection): | |
""" | |
Selection should be the composites. | |
""" | |
groups = f.groups | |
glyphToGroupMap = getGlyphToGroupDictionary(groups) | |
newGroups = dict(groups) | |
for compositeGlyph in selection: | |
compositeGroups = glyphToGroupMap.get(compositeGlyph.name, []) | |
if compositeGroups: | |
continue | |
for c in compositeGlyph.components: | |
if c.baseGlyph in glyphToGroupMap: | |
baseGlyph = f[c.baseGlyph] | |
baseGlyphGroups = glyphToGroupMap.get(baseGlyph.name, []) | |
baseGlyphWidth = baseGlyph.width | |
if compositeGlyph.width == baseGlyphWidth: | |
# success | |
for gr in baseGlyphGroups: | |
members = list(newGroups[gr]) | |
members.append(compositeGlyph.name) | |
newGroups[gr] = members | |
compositeGlyph.markColor = (0.3364, 0.9951, 0.5913, 1) | |
baseGlyph.markColor = (0.3364, 0.9951, 0.5913, 1) | |
f.groups.clear() | |
f.groups.update(newGroups) | |
if __name__ == '__main__': | |
f = CurrentFont() | |
selection = f.selectedGlyphs | |
expandKerningGroupsFromBaseGlyphs(f, selection) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment