Custom Observers

An observer has to be subscribed to an specific event. When the event happens the observer will get a notification.

from mojo.events import addObserver

class myEventObserver:
    def __init__(self):
        addObserver(self, "myObserver", "mouseDown")

    def myObserver(self, info):
        print info #do something...

myEventObserver()

Real live example drawing a reference to the current glyph in the Glyph View using the unicode value of the glyph:

"""
An example script of adding an observers and do *something*

It draws a simple unicode reference of an existing installed font.
"""

from mojo.events import addObserver
from mojo.drawingTools import *

from lib.tools.misc import unicodeToChar


class DrawReferenceGlyph(object):

    def __init__(self):
        addObserver(self, "drawReferenceGlyph", "draw")

    def drawReferenceGlyph(self, info):
        glyph = info["glyph"]
        scaleValue = info["scale"]
        r = 0
        g = 0
        b = 0
        a = .5

        if glyph is not None and glyph.unicode is not None:
            t = unicodeToChar(glyph.unicode)
            save()
            translate(glyph.width + 10, 10)
            scale(scaleValue)
            font("Georgia", 20)
            stroke(None)
            fill(r, g, b, a)
            text(t, (0, 0))
            restore()


DrawReferenceGlyph()

More observers examples.

Observers

  • addObserver(observer, method, event)
    Adds an observer for an event to the observer method.
  • removeObserver(observer, event)
    Removes the observer for an event.

Events

Any observer can subscriber to these events. The callback receives a dictionary object containing additional objects related to the send event.

All notification dictionaries contains the following keys:

glyph the current/edited/active glyph
tool the current tool
view the current glyph view

  • applicationDidFinishLaunching
    Send when RoboFont did finish launching.
  • applicationDidBecomeActive
    Send when RoboFont become the active application.
  • applicationWillResignActive
    Send when RoboFont resigns being the active application.
  • binaryFontWillOpen
    Send when a binary font will open.
    font  the font object
    source the fontTools source object
    format the format of the source font
  • fontBecameCurrent
    font  the font object
  • fontResignCurrent
    font  the font object
  • fontWillSave
    Send when a font will save.
    font the font object
    path the path where the font will be saved
  • fontDidSave
    Send when a font is done saving.
    font the font object
    path the path where the font did save
  • fontWillClose
    Send when a font will close.
    font the font object
  • newFontWillOpen
    Send when a new font will open.
    font the font object
  • newFontDidOpen
    Send when a new font did open.
    font the font object
  • fontWillOpen
    Send when a font will open.
    font the font object
  • fontDidOpen
    Send when a font did open.
    font the font object
  • fontWillAutoSave
    Send when a font will auto save.
    font the font object
    path the path where the font did autosave
  • fontDidAutoSave
    Send when a font did auto save.
    font the font object
    path the path where the font did autosave
  • fontDidChangeExternally
    Send when a font did change externally, outside RoboFont.
    font the font object
  • fontWillGenerate
    Send when a font will generate.
    font the font object
    format the format of the generated font
    path the path where the binary font will save
  • fontDidGenerate
    Send when a font did generate.
    font the font object
    format the format of the generated font
    path the path where the binary font will save
  • currentGlyphChanged
    Send when the current glyph changed, this can be either in the glyph view or in the font overview.
  • viewWillChangeGlyph
    Send when the glyph view will switch to an other glyph.
  • viewDidChangeGlyph
    Send when the glyph view did switch to an other glyph.
  • glyphWindowWillOpen
    Send when a glyph window will open.
    window the glyph window that will open
  • glyphWindowDidOpen
    Send when a glyph window did open.
    window the glyph window that did open
  • glyphWindowWillClose
    Send when a glyph window will close.
    window the glyph window that will close
  • spaceCenterWillOpen
    Send when a space center will open.
    window the space center window that will open
  • spaceCenterDidOpen
    Send when a space center did open.
    window the space center window that did open
  • spaceCenterWillClose
    Send when a space center will close.
    window the space center window that will close
  • transformChanged
    Send when a transformation is applied to a glyph.
    scale
    translate
    rotate
    skew
    repeatMatrix
  • extensionDidGenerate
    Send when an extension did generate.
    path the path to the extension
  • spaceCenterDraw
    Send when a space center draws a glyph, this can happen a lot so be care full.
    scale the drawing scale
    spaceCenter the space center
    selected a bool if the glyph is selected in the space center
  • spaceCenterKeyDown
    Send on a key down in a space center.
    event the NSEvent object
    spaceCenter the space center
  • spaceCenterKeyUp
    Send on a key up in space center.
    event the NSEvent object
    spaceCenter the space center
  • drawPreview
    Send when the glyph view draws a preview.
    scale
  • drawBackground
    Send when the glyph view draws the background, before the actual glyph data.
    scale
  • draw
    Send when the glyph view draw the glyph data.
    scale
  • drawInactive
    Send when the glyph view draw when the glyph window is not the active one.
    scale
  • mouseDown
    Send on mouse down in the glyph view.
    point the point coordinates of the mouse in the glyph coordinate system
    clickCount the click count
    offset offset of the zero zero point in the glyph view
    event the NSEvent object
  • rightMouseDown
    Send on right mouse down in the glyph view.
    point the point coordinates of the mouse in the glyph coordinate system
    event the NSEvent object
  • mouseDragged
    Send on a mouse drag in the glyph view.
    point the point coordinates of the mouse in the glyph coordinate system
    offset offset of the zero zero point in the glyph view
    delta delta of the drag form the first click
    event the NSEvent object
  • rightMouseDragged
    Send on a right mouse drag in the glyph view.
    point the point coordinates of the mouse in the glyph coordinate system
    offset offset of the zero zero point in the glyph view
    event the NSEvent object
  • mouseUp
    Send on a mouse up in the glyph view.
    point the point coordinates of the mouse in the glyph coordinate system
    offset offset of the zero zero point in the glyph view
    event the NSEvent object
  • keyDown
    Send on a key down in the glyph view.
    event the NSEvent object
  • keyUp
    Send on a key up in the glyph view.
    event the NSEvent object
  • modifiersChanged
    Send when the modifier changed in the glyph view (command, alt, control and shift keys)
    event the NSEvent object
  • toggleTransformMode
    Send when the glyph view toggles from or out the transform mode.
  • acceptMenuEditCallbacks
    Send when a contextual menu is been build.
    item a menu item
  • selectAll
    Send on select all in the glyph view.
  • deselectAll
    Send on deselect all in the glyph view.
  • selectAllAlternate
    Send on select all alternate in the glyph view.
  • copy
    Send on copy in the glyph view.
  • copyAsComponent
    Send on copy as component in the glyph view.
  • cut
    Send on cut in the glyph view.
  • delete
    Send on delete in the glyph view.
  • paste
    Send on paste in the glyph view.