Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Last active November 17, 2023 18:06
Show Gist options
  • Save connordavenport/cfd2f8aa284dd427e38b0c3e89d30393 to your computer and use it in GitHub Desktop.
Save connordavenport/cfd2f8aa284dd427e38b0c3e89d30393 to your computer and use it in GitHub Desktop.
a contextual menu for the font overview that adds an alternate glyph next to the default glyph
from mojo.events import addObserver
import ezui
from mojo.subscriber import Subscriber, registerFontOverviewSubscriber
from mojo.UI import CurrentFontWindow
class alternatesController(ezui.WindowController):
def build(self, parent):
content = """
Note:
[[_ _]] @textEditor
"""
self.parent = parent
self.font = CurrentFont()
self.copy = ".alt"
self.referenceLayer = "AltReference"
self.selection = self.font.selectedGlyphs
descriptionData = dict(
textEditor=dict(
value=self.copy,
),
)
self.w = ezui.EZPopover(
size=(200, 150),
content=content,
descriptionData=descriptionData,
parent=parent,
behavior="transient",
controller=self
)
self.w.bind("will close", self.windowWillClose)
def getWindowCenterPos(self):
x,y,w,h = self.parent.getPosSize()
xc = (w/2) + x
yc = (h/2) + y
return (xc,yc)
def openWindow(self):
x,y = self.getWindowCenterPos()
self.w.open(location=(x, y, 1, 1))
def textEditorCallback(self,sender):
self.copy = sender.get()
def windowWillClose(self,sender):
suffix = self.copy
if not suffix.startswith("."):
suffix = f".{suffix}"
if self.selection:
for glyph in self.selection:
altName = glyph.name + suffix
if altName in self.font.keys():
pass
self.font.newGlyph(altName)
altGlyph = self.font[altName]
altGlyph.clear()
altGlyph.appendGlyph(glyph)
altGlyph.width = glyph.width
altGlyph.unicode = None
# copy to refernce layer
if self.referenceLayer not in self.font.layerOrder: self.font.newLayer(self.referenceLayer)
altGlyph.copyToLayer(self.referenceLayer, clear=True)
mask = altGlyph.getLayer(self.referenceLayer)
mask.width = altGlyph.width
# move alternate next to default
order = list(self.font.templateGlyphOrder)
place = (order.index(glyph.name)) + 1
order.insert(place, order.pop(-1))
self.font.templateGlyphOrder = order
class makeAlternatesMenu(Subscriber):
def build(self):
pass
def fontOverviewWantsContextualMenuItems(self, info):
myMenuItems = [
("Make alternate(s)...", self.makeAlternates),
]
info["itemDescriptions"].extend(myMenuItems)
def makeAlternates(self,sender):
w = CurrentFontWindow()
self.altController = alternatesController(w.window())
self.altController.openWindow()
if __name__ == "__main__":
registerFontOverviewSubscriber(makeAlternatesMenu)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment