import ezui from mojo.subscriber import Subscriber, registerRoboFontSubscriber from mojo.roboFont import CurrentGlyph class GlyphVisualizer(Subscriber, ezui.WindowController): debug = True def build(self): self.glyph = CurrentGlyph() content = """ * VerticalStack @stack > * MerzView @merzView > (🌛) @button """ descriptionData = dict( merzView=dict( backgroundColor=(1, 1, 1, 1), delegate=self ), stack=dict( alignment="center" ), ) self.w = ezui.EZWindow( title="Glyph Visualizer", content=content, size=(self.glyph.width, self.glyph.font.info.unitsPerEm), descriptionData=descriptionData, controller=self ) merzView = self.w.getItem("merzView") container = merzView.getMerzContainer() # a layer for the glyph and the baseline self.backgroundLayer = container.appendBaseSublayer( size=(self.glyph.width, self.glyph.font.info.unitsPerEm), backgroundColor=(1, 1, 1, 1) ) self.glyphLayer = self.backgroundLayer.appendPathSublayer( position=(0, -self.glyph.font.info.descender) ) glyphPath = self.glyph.getRepresentation("merz.CGPath") self.glyphLayer.setPath(glyphPath) self.lineLayer = self.backgroundLayer.appendLineSublayer( startPoint=(0, -self.glyph.font.info.descender), endPoint=(self.glyph.width, -self.glyph.font.info.descender), strokeWidth=1, strokeColor=(1, 0, 0, 1) ) def started(self): self.w.open() def glyphEditorDidSetGlyph(self, info): self.glyph = info['glyph'] glyphPath = self.glyph.getRepresentation("merz.CGPath") self.glyphLayer.setPath(glyphPath) self.backgroundLayer.setSize((self.glyph.width, self.glyph.font.info.unitsPerEm)) self.lineLayer.setEndPoint((self.glyph.width, -self.glyph.font.info.descender)) def _switchToDarkMode(self): self.glyphLayer.setFillColor((1, 1, 1, 1)) self.backgroundLayer.setBackgroundColor((0, 0, 0, 1)) def _switchToLightMode(self): self.glyphLayer.setFillColor((0, 0, 0, 1)) self.backgroundLayer.setBackgroundColor((1, 1, 1, 1)) def buttonCallback(self, sender): if sender.getTitle() == "🌛": self._switchToDarkMode() sender.setTitle('🌞') else: self._switchToLightMode() sender.setTitle('🌛') if __name__ == '__main__': registerRoboFontSubscriber(GlyphVisualizer)