aboutsummaryrefslogtreecommitdiffstats
path: root/equalize_sidebearings.py
blob: 7f7f5ccf23c7a4f558d26c768651d5ecc5627c53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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):
        addObserver(self, 'center', 'spaceCenterKeyUp')

    def center(self, info):
        if info['event'].characters() == 'a':
            # g = info['glyph']
            self.equalize_sidebearings(info['glyph'])
            # SpaceCenterMenuForGlyph(info['event'], self, g)
            #
            # g.prepareUndo('Equalize Sidebearings')
            #
            # g.center()
            #
            # 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()