Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Last active December 7, 2023 20:27
Show Gist options
  • Save connordavenport/4b93dcc0fdcd10e28a176125d3316b41 to your computer and use it in GitHub Desktop.
Save connordavenport/4b93dcc0fdcd10e28a176125d3316b41 to your computer and use it in GitHub Desktop.
a simply RF popover to jump between fonts
import ezui
from merz.tools.drawingTools import NSImageDrawingTools
from mojo.UI import AllFontWindows, CurrentFontWindow, AllWindows, CurrentGlyphWindow
class frontAndCenter(ezui.WindowController):
def build(self, parent, viewType):
self.buttonMap = {}
self.parent = parent
self.font = None
self.viewType = viewType
self.fontWindows = [(w.document.font,w) for w in AllFontWindows()]
content = "* VerticalStack\n"
descriptionData = {}
for i,f in enumerate(AllFonts(sortOptions="magic")):
if i % 4 == 0: content += "* HorizontalStack\n"
content += f"> ({{}}) @image{i}\n"
descriptionData[f"image{i}"] = {"image":self._glyphToToolbarImage(f,"a"), "callback":self.buttonCallback, "identifier":f.path}
self.w = ezui.EZPopover(
size=("auto", "auto"),
content=content,
descriptionData=descriptionData,
parent=parent,
behavior="semitransient",
controller=self
)
for i,f in enumerate(AllFonts(sortOptions="magic")):
item = self.w.getItem(f.path)
self.buttonMap[item._button] = f
def started(self):
if self.viewType == "fontWindow":
left, top, width, height = self.parent.getPosSize()
self.w.open(location=(int(width/2),int(height/2)+top, 1, 1))
else:
x,y = self.parent._getMousePosition()
self.w.open(location=(x, y, 1, 1))
def buttonCallback(self,sender):
self.font = self.buttonMap.get(sender)
self.w.close()
self.bringWindowToFront()
def bringWindowToFront(self):
for font, win in self.fontWindows:
f = RFont(font)
if f == self.font:
win.w.getNSWindow().makeKeyAndOrderFront_(None)
if viewType == "glyphWindow":
window = CurrentGlyphWindow()
window.setGlyph(self.font[window.getGlyph().name])
def _glyphToToolbarImage(self,font,glyphName):
glyph = font[glyphName]
w = font.info.unitsPerEm
sf = 1000/w
bot = NSImageDrawingTools((w, font.info.xHeight * 1.1), scale=.045 * sf)
bot.translate((w - glyph.width)/2,abs(glyph.bounds[1]))
bot.fill(0, 0, 0, 1)
pen = bot.BezierPath()
glyph.draw(pen)
bot.drawPath(pen)
image = bot.getImage()
image.setTemplate_(True)
return image
if AllWindows()[0] == CurrentFontWindow():
w = CurrentFontWindow().window()
viewType = "fontWindow"
elif AllWindows()[0] == CurrentGlyphWindow():
w = CurrentGlyphWindow().getGlyphView()
viewType = "glyphWindow"
frontAndCenter(w,viewType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment