From 385ba95ce64661a773af2a7f28191a768149c572 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 20 Nov 2015 05:32:52 -0500 Subject: equalize_sidebearings.py: Copy RoboFont implementation Copy `equalize_sidebearings` method and `getDefault` function from RoboFont after uncompiling the `lib/UI/spaceCenter/*.pyc`, `lib/doodleMenus.pyc`, and `lib/tools/defaults.pyc` files. This gives us the exact same functionality that happens when choosing "Equalize sidebearings" from the contextual menu in the Space Center. Including the code directly instead of referencing it because the `SpaceCenterMenuForGlyph` class opens a contextual menu on init, which we really don't want, and the `equalizeSideBearings_` method that it defined is an instance method, so I would need an instance of the class but don't want to trigger the contextual menu. So I figured I'd just copy the code directly to speed things up. Unfortunately undo functionality doesn't work with the method I copied, and I can't seem to figure out how to get it sorted out. --- equalize_sidebearings.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'equalize_sidebearings.py') diff --git a/equalize_sidebearings.py b/equalize_sidebearings.py index 92f518b..7f7f5cc 100644 --- a/equalize_sidebearings.py +++ b/equalize_sidebearings.py @@ -1,4 +1,15 @@ +from AppKit import NSUserDefaults + from mojo.events import addObserver +# from lib.doodleMenus import SpaceCenterMenuForGlyph +# from tools.defaults import getDefault + +def getDefault(key, defaultValue = None, defaultClass = None): + defaultsFromFile = NSUserDefaults.standardUserDefaults() + value = defaultsFromFile.get(key, defaultValue) + if defaultClass is not None: + return defaultClass(value) + return value class EqualizeSidebearings(object): def __init__(self): @@ -6,12 +17,30 @@ class EqualizeSidebearings(object): def center(self, info): if info['event'].characters() == 'a': - g = info['glyph'] - - g.prepareUndo('Equalize Sidebearings') - - g.center() + # g = info['glyph'] + self.equalize_sidebearings(info['glyph']) + # SpaceCenterMenuForGlyph(info['event'], self, g) + # + # g.prepareUndo('Equalize Sidebearings') + # + # g.center() + # + # g.performUndo() - g.performUndo() + def equalize_sidebearings(self, glyph): + useItalicAngleForDisplay = getDefault('glyphViewShouldUseItalicAngleForDisplay') + leftMarginAttribute = 'leftMargin' + rightMarginAttribute = 'rightMargin' + if useItalicAngleForDisplay: + leftMarginAttribute = 'angledLeftMargin' + rightMarginAttribute = 'angledRightMargin' + left = getattr(glyph, leftMarginAttribute) + right = getattr(glyph, rightMarginAttribute) + margin = int(round((left + right) / 2.0)) + glyph.prepareUndo('Equalize sidebearings') + setattr(glyph, leftMarginAttribute, margin) + setattr(glyph, rightMarginAttribute, margin) + glyph.selection.resetSelectionPath() + glyph.performUndo() EqualizeSidebearings() \ No newline at end of file -- cgit v1.2.3