Skip to content

Instantly share code, notes, and snippets.

@typoman
Last active November 6, 2023 19:29
Show Gist options
  • Save typoman/88c6a8b31cf2177f97f40adb29e699ad to your computer and use it in GitHub Desktop.
Save typoman/88c6a8b31cf2177f97f40adb29e699ad to your computer and use it in GitHub Desktop.
from mojo.UI import *
"""
RoboFont Script
- Type: Composites
- Purpose: To create RoboFont glyph construction syntax out of the exisiting
composites inside a font using the anchor names.
- Specifications:
- Determines the base letter and marks according to anchor names.
- Reconstruct the glyphConstruction syntax from the anchor names.
- Publish Date: 7 Dec 2020
- Author: Bahman Eslami
- License: MIT
"""
resultGlyphContstruction = ""
f = CurrentFont()
OutputWindow().clear()
def getMarkAnchor(g):
result = []
for a in g.anchors:
if a.name[0] == "_":
result.append(a)
if len(result) == 1:
return result[0]
# print(f"Can't find main anchor in glyph {g.name}")
def getMarkAndBaseGlyph(g):
baseGlyphs = []
marks = {} # {a.name: [g1, g2]}
for c in g.components:
baseGlyph = f[c.baseGlyph]
markAnchor = getMarkAnchor(baseGlyph)
if markAnchor is not None:
marks.setdefault(markAnchor.name[1:], []).append(baseGlyph.name)
else:
baseGlyphs.append(baseGlyph)
if marks != {} and len(baseGlyphs) == 1:
return marks, baseGlyphs[0].name
for g in f.selectedGlyphs:
if g.contours:
continue
if g.components:
line = f"{g.name} = "
markAndBaseGlyphs = getMarkAndBaseGlyph(g)
if markAndBaseGlyphs is not None:
marks, baseGlyph = markAndBaseGlyphs
marksSyntax = f"{baseGlyph} + "
for i, (anchorName, markGlyphs) in enumerate(marks.items()):
if i == 0:
for markG in markGlyphs:
marksSyntax += f"{markG}@{anchorName}"
else:
for markG in markGlyphs:
marksSyntax += f" + {markG}@{baseGlyph}:{anchorName}"
line += marksSyntax
else:
cList = []
for c in g.components:
cList.append(c.baseGlyph)
line += " + ".join(cList)
if g.unicodes:
uni = "{:04X}".format(g.unicodes[0])
line += f" |{uni}"
line += "\n"
resultGlyphContstruction += line
print(resultGlyphContstruction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment