-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCreateDropShadow.py
128 lines (98 loc) · 4.02 KB
/
CreateDropShadow.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#MenuTitle: Create Drop Shadow
#Created by Kyle Wayne Benson
# -*- coding: utf-8 -*-
__doc__="""
Replace each selected glyphs with a drop shadow
"""
import vanilla
from vanilla import *
import GlyphsApp
class DropShadow( object ):
def __init__( self ):
# Window 'self.w':
windowWidth = 250
windowHeight = 180
windowWidthResize = 300 # user can resize width by this value
windowHeightResize = 0 # user can resize height by this value
self.w = vanilla.Window(
( windowWidth, windowHeight ), # default window size
"Create Drop Shadow", # window title
minSize = ( windowWidth, windowHeight ), # minimum size (for resizing)
maxSize = ( windowWidth + windowWidthResize, windowHeight + windowHeightResize ), # maximum size (for resizing)
autosaveName = "com.kylewaynebenson.DropShadow.mainwindow" # stores last window position and size
)
# UI elements:
YOffset = 10
self.w.text_1 = vanilla.TextBox( (15, YOffset, -15, 30), "Create drop shadow that is this many units removed from original drawing:", sizeStyle='small' )
LineHeight = 40
YOffset += LineHeight
self.w.text_2 = vanilla.TextBox( ( 15, YOffset, 20, 20), "X:", sizeStyle='regular' )
self.w.xAxis = vanilla.EditText( ( 40, YOffset, 50, 21), "-20", sizeStyle='regular' )
self.w.text_3 = vanilla.TextBox( (-95, YOffset, 20, 20), "Y:", sizeStyle='regular' )
self.w.yAxis = vanilla.EditText( (-20-50, YOffset, -20, 21), "-20", sizeStyle='regular' )
LineHeight = 30
YOffset += LineHeight
self.w.text_4 = vanilla.TextBox( (15, YOffset, 42, 20), "Offset:", sizeStyle='regular' )
self.w.offset = vanilla.EditText( (62, YOffset, 50, 21), "0", sizeStyle='regular' )
self.w.checkBox = CheckBox((-110, YOffset, 0, 20), "Keep letters", value=False)
YOffset += LineHeight
# Run Button:
self.w.runButton = vanilla.Button((-130, -20-15, -15, -15), "Create Shadows", sizeStyle='regular', callback=self.DropShadowMain )
self.w.setDefaultButton( self.w.runButton )
# Open window and focus on it:
self.w.open()
self.w.makeKey()
def DropShadowMain( self, sender ):
self.offsetCurveFilter = NSClassFromString("GlyphsFilterOffsetCurve")
pathOp = GSPathOperator.alloc().init()
Font = Glyphs.font
selectedLayers = Font.selectedLayers
xAxis = float(self.w.xAxis.get())
yAxis = float(self.w.yAxis.get())
offsetX = float(self.w.offset.get())
offsetY = float(self.w.offset.get())
glyphsChanged = []
try:
for thisLayer in selectedLayers:
glyphsChanged.append( thisLayer.parent.name )
thisLayer.parent.beginUndo() # wrapper for undo function
thisLayer.correctPathDirection() #
thisLayer.correctPathDirection() #
thisLayer.correctPathDirection() #
shadowPathList = NSMutableArray.array()
prePathList = NSMutableArray.array()
#save original outline
for thisPath in thisLayer.paths:
newPath = GSPath()
for n in thisPath.nodes:
newNode = GSNode()
setX = n.x + xAxis
setY = n.y + yAxis
newNode.type = n.type
newNode.setPosition_((setX, setY))
newPath.addNode_( newNode )
newPath.closed = thisPath.closed
shadowPathList.append( newPath )
prePathList.append( thisPath )
self.offsetCurveFilter.offsetLayer_offsetX_offsetY_makeStroke_position_error_shadow_( thisLayer, offsetX, offsetY, False, 0.5, None, None )
for thisPath in thisLayer.paths:
shadowPathList.append( thisPath )
pathOp.removeOverlapPaths_error_( shadowPathList, None)
#reverse shadow
for thisPath in thisLayer.paths:
thisPath.reverse()
#punch out the old drawing
for shadowPath in shadowPathList:
thisLayer.addPath_( shadowPath )
thisLayer.removeOverlap()
if (self.w.checkBox.get() == True):
for prePath in prePathList:
thisLayer.addPath_( prePath )
thisLayer.correctPathDirection()
thisLayer.parent.endUndo() # wrapper for undo function
print "Created drop shadow for these glyphs:", glyphsChanged
except Exception, e:
# print error
Glyphs.showMacroWindow()
print "Create Drop Shadow Error: %s" % e
DropShadow()