Custom Tools

You can add custom tools to RoboFont, by subclassing the BaseEventTool class or an existing core tool. RoboFont core tools are BezierDrawingTool, EditingTool, MeasurementTool and SliceTool. They are available through the mojo.events module.

from mojo.events import BaseEventTool 
## or subclassing a core tool
from mojo.events import BezierDrawingTool, EditingTool, MeasurementTool, SliceTool

This is an easy example, with most event callback. You need the installTool to tell RoboFont you want to install in a new tool.
Custom tools can also be activated at start up through the preferences.

example:

from mojo.events import BaseEventTool, installTool
from AppKit import *

class MyEvent(BaseEventTool):

    def becomeActive(self):
        print "active"

    def becomeInactive(self):
        print "inactive"

    def mouseDown(self, point, clickCount):
        # getGLyph returns the current glyph as robofab object
        print "mouseDown", self.getGlyph(), clickCount

    def rightMouseDown(self, point, event):
        print "rightMouseDown"

    def mouseDragged(self, point, delta):
        print "mousedragged"

    def rightMouseDragged(self, point, delta):
        print "rightMouseDragged"

    def mouseUp(self, point):
        print "mouseup"

    def keyDown(self, event):
        # a dict of all modifiers, shift, command, alt, option
        print "keyDown", self.getModifiers() 

    def keyUp(self, event):
        print "keyUp"

    def modifiersChanged(self):
        print "modifiersChanged"

    def draw(self, scale):
        print "draw", self.isDragging()
        if self.isDragging():
            ## draw a red dot when dragging
            r = 50
            NSColor.redColor().set()
            x, y = self.currentPoint
            NSBezierPath.bezierPathWithOvalInRect_(((x-r, y-r), (r*2, r*2))).fill()

    def drawBackground(self, scale):
        print "drawBackground here"

    #def getDefaultCursor(self):
    #   this will be the cursor default is an arrow
    #   return aNSCursor
    #def getToolbarIcon(self):
    #   this is setting the icon in the toolbar default is an arrow
    #   return aNSImage

    def getToolbarTip(self):
        return "My Event Tool Bar Tip"

    #notifications

    def viewDidChangeGlyph(self):
        print "view changed glyph"

    def preferencesChanged(self):
        print "prefs changed"

installTool(MyEvent())

Tools Methods

  • getGlyph()
    Returns the current glyph used by the tool 
  • getNSView()
    Returns the current glyph view 
  • getCurrentEvent()
    Returns the current NSEvent.
  • refreshView()
    updates the current glyph view
  • setCursor(cursor=None)
    Sets the cursor as current cursor (needs a NSCursor object), if None is given it will be return to default cursor of the tool
  • isDraggin()
    Returns a bool if the tool is draggin
  • zoomRect(offset=None)
    zoom to a given rect
  • toggleTransformMode()
    Toggle the transform
  • getDefaultCursor()
    returns the default cursor for the tool, this must be overwritten
  • getToolbarIcon()
    returns the toolbar icon for the tool, this must be overwritten
  • mouseDown(point, clickCount)
    send a mouse down notification to the tool, with the mouse point in the and the clickCount
  • rightMouseDown(point, clickCount)
    send a right mouse down notification to the tool, with the mouse point in the and the clickCount
  • modifyPoint(point)
    modify the point coordinates
    In the embedded tools this is used to modify the point when fe. on shiftDown
  • mouseMoved(point)
    send a mouse moved notification to the tool, be careful and optimize the code cause the mouse moves all the time
  • mouseDragged(point, delta)
    send a mouse dragged notification to the tool, the delta is the difference between the previous drag point
  • rightMouseDragged(point, delta)
    send a right mouse dragged notification to the tool, the delta is the difference between the previous drag point 
  • mouseUp(point)
    send a mouse up notification to the tool
  • isDragging()
    returns a bools if the tool is in a dragging event
  • keyDown(event)
    send a key down notification to the tool, possible to get the pressed characters by event.characters()
  • keyUp(event)
    send a key up notification to the tool 
  • modifiersChanged()
    send a modifiers changed notification to the tool, happens when shift, command, control, alt is changed
  • getModifiers()
    returns a dictionary with all the modifiers keys as bools. True when pressed down, keys are: shiftDowncommandDownoptionDowncontrolDown
  • getArrowsKeys()
    returns a dictionary with all the arrows keys as bools. True when pressed down, keys are: up, down, left, right

Tools Menu methods

  • acceptMenuEditCallbacks()
    Overwrite this if you want your tool to accept menu callbacks.
  • selectAll()
    perform a select all in your tool, don't overwrite it if you don't want to do anything with it
  • selecteAllAlternate()
    perform a select all in your tool with alt key down, don't overwrite it if you don't want to do anything with it
  • deselectAll()
    perform a deselect all in your tool, don't overwrite it if you don't want to do anything with it
  • copy()
  • copyAsComponent()
  • cut()
  • delete()
  • paste()
  • additionContectualMenuItems()
    called when a contextual menu is created, it is required to return a list of tuples with a title string and a callback function
    example [("additional stuff", myObject.myAdditionMenuCallback), ]

Tools Draw Methods

  • draw(scale)
    draw in the glyph
  • drawBackground(scale)
    draw in the background of the glyph view
  • drawPreview(scale)
    additional drawing in the glyph view while holding the glyph view preview key
  • drawInactive(scale, glyph, view)
    additional drawing in the glyph view when the glyph view is not active, the glyph will not be the current glyph, same for the view
  • drawBackgroundSelection(scale)
    draw in the background, should be related to a selection
  • drawSelection(scale, glyph, view)
    draw the selection for a glyph in a view

Space Center

  • spaceCenterDraw(glyph, scale, selected, spaceCenter)
    draw in the space center for given glyph
  • spaceCenterKeyDown(glyph, event, spaceCenter)
    send a key down notification to the tool, possible to get the pressed characters by event.characters()
  • spaceCenterKeyUp(glyph, event, spaceCenter)
    send a key up notification to the tool 

Tools Notifications Methods

  • viewWillChangeGlyph()
    the glyph in the current view will change
  • viewDidChangGlyph()
    the glyph in the current view did change
  • currentGlyphChanged()
    the current glyph in the app changed
  • preferencesChanged()
    the user defaults changed
  • becomeInactive()
    the tool becomes inactive
  • becomeActive()
    the tool becomes active
  • didUndo()
    a undo notification when the undo is already performed
  • glyphWindowWillOpen()
    a new glyph window will open
  • glyphWindowDidOpen()
    a new glyph window did open
  • glyphWindowWillClose()
    a glyph window will close
  • spaceCenterWillOpen()
    a new Space Center will open
  • spaceCenterDidOpen()
    a new Space Center did open
  • spaceCenterWillClose()
    a Space Center will close
  • fontWillOpen(font)
    a UFO will been opened
  • fontDidOpen(font)
    a UFO did open 
  • newFontWillOpen(font)
    a new UFO will been created
  • newFontDidOpen(font)
    a new UFO is been created 
  • binaryFontWillOpen(font, source, format)
    a binary font will open
    the source is the fontTools font object 
  • fontWillClose(font)
    a UFO is been closed
  • applicationDidFinishLauching() 
    the app is ready launching
  • applicationDidBecomeActive()
    the app becomes active
  • applicationWillResignActive()
    the app will resign being active
  • fontBecameCurrent(font)
    a font becomes active
  • fontResignCurrent(font) 
    a font resigns being active 
  • fontWillAutoSave(font)
    font will auto save
  • fontDidAutoSave(font)
    font did auto save
  • fontWillSave(font, path)
    font will save
  • fontDidSave(font, path)
    font did save
  • fontWillGenerate(font, format, path)
    font will generate to a binary format
  • fontDidGenerate(font, format, path) 
    font did generate to a binary format
  • fontDidChangeExternally(font)
    An external change to the font has happend 

Tools Attributes

  • allPointList
    A list of clicked and dragged points used by the tool
  • mouseDownPoints
    A list of clicked points used by the tool
  • shiftDown
    Bool if shift is down 
  • commandDown
    Bool if command is down 
  • optionDown
    Bool if option is down 
  • controlDown
    Bool if control is down 
  • spaceBarDown
    Bool if space is down 
  • arrowKeysDown
    a dictionary of bools with keys: up, down, left, right
  • currentPoint
    returns the last points from the allPointsList
  • selection
    returns the current glyph selection object