Last active
December 21, 2015 23:18
-
-
Save okay-type/6381098 to your computer and use it in GitHub Desktop.
Metrics Adjuster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# jackson@okaytype.com | |
### requires an icon file toolGoMetrics.pdf in the same folder | |
# download it from www.theharrietseries.com/crypt/robofont/toolGoMetrics.pdf | |
### updated 2013/03/10 — linked to didfontopen observers, just set as a startup script | |
### updated 2013/07/16 — fixed lame selection traceback and broken spacecenter adjustments | |
# to do: adjust by percent, uniform adjust, remember last setting | |
import os | |
from vanilla import * | |
from mojo.UI import CurrentFontWindow, CurrentSpaceCenter | |
from mojo.events import addObserver | |
from AppKit import NSImage, NSImageNameAdvanced | |
from mojo.extensions import getExtensionDefault, setExtensionDefault | |
from lib.UI.integerEditText import NumberEditText | |
### short term memory | |
# myToolIdentifier = "com.okaytype.gometrics.settings" | |
# print getExtensionDefault(myToolIdentifier, fallback=None) | |
# setExtensionDefault(myToolIdentifier, "myDefaultValue") | |
### link to font opening | |
class AddToolbarItemObserver(object): | |
base_path = os.path.dirname(__file__) | |
def __init__(self): | |
addObserver(self, "didOpen", "fontDidOpen") | |
addObserver(self, "didOpen", "newFontDidOpen") | |
### add menu stuff | |
def didOpen(self, notification): | |
window = CurrentFontWindow() | |
if window is None: | |
return | |
toolbarItems = window.getToolbarItems() | |
toolbarIcon = NSImage.alloc().initByReferencingFile_(os.path.join(self.base_path, "toolGoMetrics.pdf")) | |
newItem = dict( | |
itemIdentifier='goMetrics', | |
label='Go Metrics', | |
imageObject=toolbarIcon, | |
view=toolbarIcon, | |
callback=self.openSheet | |
) | |
index=-2 | |
toolbarItems.insert(index, newItem) | |
vanillaWindow = window.window() | |
vanillaWindow.addToolbar( | |
toolbarIdentifier='goMetrics', | |
toolbarItems=toolbarItems, | |
addStandardItems=False | |
) | |
def openSheet(self, sender): | |
window = CurrentFontWindow().window() | |
if window is None: | |
return | |
SheetMetrics(window) | |
### build the slide out menu | |
class SheetMetrics(object): | |
def __init__(self, parentWindow): | |
self.w = Sheet((250, 185), parentWindow) | |
self.w.myTextBoxLeft = TextBox((65, 25, 0, 17), "Left Side Bearing") # X, Y, Width, Height | |
self.w.myTextValLeft = EditText((20, 25, 40, 19), sizeStyle="small") | |
self.w.myTextValLeft.set(0) | |
self.w.myTextBoxRight = TextBox((65, 55, 0, 17), "Right Side Bearing") | |
self.w.myTextValRight = EditText((20, 55, 40, 19), sizeStyle="small") | |
self.w.myTextValRight.set(0) | |
self.w.goSelected = Button((20, 95, 210, 20), "Selected", self.changeMetricsSel) | |
self.w.goSpaceCtr = Button((20, 120, 210, 20), "In Space Center", self.changeMetricsSC) | |
self.w.closeButtn = Button((20, 145, 210, 20), "Close", self.closeCallback) | |
self.w.open() | |
def closeCallback(self, sender): | |
self.w.close() | |
### change selected metrics | |
def changeMetricsSel(self, sender): | |
right=self.w.myTextValRight.get() | |
left=self.w.myTextValLeft.get() | |
f = CurrentFont() | |
for g in f.selection: | |
f[g].prepareUndo('Metrics Adjustment') | |
if len(f[g].components) == 0: | |
f[g].rightMargin += int(right) | |
f[g].leftMargin += int(left) | |
elif len(f[g].components) != 0: | |
for p in f[g].components: | |
if p.baseGlyph not in f.selection: | |
p.move((int(left), 0)) | |
f[g].rightMargin += int(right) | |
f[g].rightMargin += int(left) | |
else: | |
return | |
f[g].update | |
f[g].performUndo() | |
### change spacecenter metrics | |
def changeMetricsSC(self, sender): | |
right=self.w.myTextValRight.get() | |
left=self.w.myTextValLeft.get() | |
f = CurrentFont() | |
sc = CurrentSpaceCenter().get() | |
for g in sc: | |
if g in f: | |
f[g].prepareUndo('Metrics Adjustment') | |
if len(f[g].components) == 0: | |
f[g].rightMargin += int(right) | |
f[g].leftMargin += int(left) | |
elif len(f[g].components) != 0: | |
for p in f[g].components: | |
if p.baseGlyph not in f.selection: | |
p.move((int(left), 0)) | |
f[g].rightMargin += int(right) | |
f[g].rightMargin += int(left) | |
else: | |
return | |
f[g].update | |
f[g].performUndo() | |
AddToolbarItemObserver() |
I'm still figuring out vanilla's addToolbar(). Everything I've tried beyond this script gets buggy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this one as well... I have always been a little confused on how to get things into the Toolbar. This helps a lot. Thanks!