/sendSelectedContoursToGlyph.py Secret
Created
February 2, 2021 21:28
Select some contours, and run this script to send those contours to a different glyph of your choosing.
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
# menuTitle : Send Selection to Glyph | |
from vanilla import EditText, Window, Button | |
from mojo.UI import AskString, StatusInteractivePopUpWindow, CurrentGlyphWindow | |
from defconAppKit.windows.baseWindow import BaseWindowController | |
from AppKit import NSColor | |
f = CurrentFont() | |
g = CurrentGlyph() | |
def getGlyphEditorRectAndScreen(editorWindow): | |
screen = editorWindow.w.getNSWindow().screen() | |
nsWindow = editorWindow.w.getNSWindow() | |
scrollView = editorWindow.getGlyphView().enclosingScrollView() | |
rectInWindowCoords = scrollView.convertRect_toView_(scrollView.frame(), None) | |
rectInScreenCoords = nsWindow.convertRectToScreen_(rectInWindowCoords) | |
(screenX, screenY), (screenWidth, screenHeight) = screen.frame() | |
(x, y), (w, h) = rectInScreenCoords | |
y = -(y + h) | |
return (x, y, w, h), screen | |
class GlyphSender(BaseWindowController): | |
def __init__(self): | |
margin = 8 | |
text_height = 22 | |
text_width = 110 | |
gutter = margin - 3 | |
button_width = text_width | |
button_height = text_height | |
window_height = margin*2 + text_height + gutter + button_height + 2 | |
window_width = margin*2 + text_width | |
editorWindow = CurrentGlyphWindow() | |
(editorX, editorY, editorW, editorH), screen = getGlyphEditorRectAndScreen(editorWindow) | |
x = editorX + ((editorW - window_width) / 2) | |
y = editorY + ((editorH - window_height) / 2) | |
self.w = StatusInteractivePopUpWindow((window_width, window_height)) | |
self.w.center() | |
self.chosen_glyph = str(g.name) | |
self.w.glyph_input = EditText( | |
(margin, margin, text_width, text_height), | |
callback=self.editTextCallback, | |
text=self.chosen_glyph | |
) | |
g_i_NS = self.w.glyph_input.getNSTextField() | |
g_i_NS.setBackgroundColor_(NSColor.colorWithCalibratedRed_green_blue_alpha_(0.9, 0.9, 0.9, 1)) | |
g_i_NS.setAlignment_(2) | |
g_i_NS.setFocusRingType_(1) | |
g_i_NS.becomeFirstResponder() | |
self.w.throw_button = Button( | |
(margin, margin + text_height + gutter, button_width, button_height), | |
"Send", | |
callback=self.throwButtonCallback) | |
self.w.setDefaultButton(self.w.throw_button) | |
self.w.close_button = Button((window_width + 20, window_height + 20, 1, 1), "", callback=self.closeCallback) | |
self.w.close_button.bind(chr(27), []) | |
self.w.open() | |
def editTextCallback(self, sender): | |
self.chosen_glyph = str(sender.get()) | |
self.w.throw_button.setTitle("Send to %s" % self.chosen_glyph) | |
def throwButtonCallback(self, sender): | |
if g is not None: | |
try: | |
with g.undo('Send Selection to Glyph (%s)' % g.name): | |
for c in g.selectedContours: | |
copy = c.copy() | |
if not self.chosen_glyph in f.keys(): | |
f.newGlyph(self.chosen_glyph) | |
f[self.chosen_glyph].appendContour(copy) | |
g.removeContour(c) | |
g.changed() | |
g.selectedContours = () | |
g.changed() | |
self.w.close() | |
except ValueError: | |
g.selectedContours = () | |
g.changed() | |
self.w.close() | |
else: | |
g.selectedContours = () | |
g.changed() | |
self.w.close() | |
def closeCallback(self, sender): | |
self.w.close() | |
GlyphSender() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment