Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Created April 24, 2025 16:27
Show Gist options
  • Save connordavenport/15bc89c603268e3cf50364171c71abda to your computer and use it in GitHub Desktop.
Save connordavenport/15bc89c603268e3cf50364171c71abda to your computer and use it in GitHub Desktop.
swap out individual alternates in space center
from mojo.subscriber import Subscriber, registerRoboFontSubscriber
from mojo.UI import GlyphRecord
class space_center_alts_manager(Subscriber):
def started(self):
self.glyph = None
self.alternates = []
self.space_center = None
def spaceCenterDidChangeSelection(self, info):
glyph = info["lowLevelEvents"][0]["glyph"]
space = info["lowLevelEvents"][0]["spaceCenter"]
if glyph:
self.glyph = glyph
self.compile_alternates()
if space:
self.space_center = space
def compile_alternates(self):
if self.glyph:
f = self.glyph.font
alts = [g.name for g in f if "." in g.name and not g.name.startswith(".")]
aa = {}
for g in f:
_a = aa.get(g.name, set())
if "." in g.name and not g.name.startswith("."):
ref = g.name.split(".")[0]
oo = [gs.name for gs in f if ref in gs.name.split(".") and gs.name != ref and gs.name != g.name]
_a.update(oo)
if ref in f.keys():
_a.add(ref)
aa[g.name] = list(_a)
else:
for an in alts:
s = an.split(".")
if s[0] == g.name:
_a.add(an)
aa[g.name] = list(_a)
self.alternates = aa
def spaceCenterWantsContextualMenuItems(self, info):
info["itemDescriptions"].extend(
[
("Replace with...",
[(name, self.swap_glyph_callback) for name in self.alternates.get(self.glyph.name, [])]
),
]
)
def swap_glyph_callback(self, sender):
glyph_name = sender.title()
if self.space_center:
r = GlyphRecord(self.glyph.font[glyph_name].naked())
rr = self.space_center.glyphRecords
cs = self.space_center.glyphLineView.getSelectedGlyphRecord()
ii = rr.index(cs)
rr[ii] = r
self.space_center.updateInputFields(
sender=None,
changedItem=cs,
newGlyph=self.glyph.font[glyph_name].asDefcon()
)
new_text = self.space_center._textFromGlyphRecords(rr)
self.space_center.setRaw(new_text)
if __name__ == '__main__':
registerRoboFontSubscriber(space_center_alts_manager)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment