This example shows a very simple tool which displays the current glyph in a system font next to the glyph shape in the Glyph Editor using Subscriber.

This is how the code works:

  1. the tool subclasses Subscriber

  2. the tool overrides three Subscriber methods:
    1. with build() accesses the glyphEditor object and its merz container
    2. with started() creates a text line sublayer into the merz container
    3. with destroy() cleans the sublayers from the container once unregistered
  3. the tools uses the Subscriber glyphEditorDidSetGlyph callback to set the text attribute of the text line sublayer

  4. note that the tool is initiated using a registerGlyphEditorSubscriber, so the tool will catch only events related to the glyph editor in use
from mojo.subscriber import Subscriber, registerGlyphEditorSubscriber


class DrawReferenceGlyph(Subscriber):

    debug = True

    def build(self):
        glyphEditor = self.getGlyphEditor()
        self.container = glyphEditor.extensionContainer(
            identifier="com.roboFont.DrawReferenceGlyph.foreground",
            location="foreground",
            clear=True)

    def started(self):
        self.textLineLayer = self.container.appendTextLineSublayer(
            pointSize=100,
            font='Georgia',
            text="",
            fillColor=(0, 0, 0, 0.4),
            horizontalAlignment="left"
        )

    def destroy(self):
        self.container.clearSublayers()

    def glyphEditorDidSetGlyph(self, info):
        glyph = info["glyph"]
        if glyph is None:
            self.textLineLayer.setText("")
            return

        txt = ""
        if glyph.unicode is not None:
            # get character for glyph
            txt = chr(glyph.unicode)
        self.textLineLayer.setPosition((glyph.width, 0))
        self.textLineLayer.setText(txt)

    def glyphEditorGlyphDidChangeMetrics(self, info):
        glyph = info["glyph"]
        if glyph is None:
            self.textLineLayer.setText("")
            return
        self.textLineLayer.setPosition((glyph.width, 0))


if __name__ == '__main__':
    registerGlyphEditorSubscriber(DrawReferenceGlyph)

Last edited on 01/09/2021