forked from huertatipografica/HTLetterspacer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHT_LetterSpacer_UI.py
executable file
·232 lines (191 loc) · 7.85 KB
/
HT_LetterSpacer_UI.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#MenuTitle: HT LetterSpacer UI
#
# Letterspacer, an auto-spacing tool
# Copyright (C) 2009 - 2016, The Letterspacer Project Authors
#
# Version 1.0.0
# Basic config
# if window = True, scripts run with a UI
window = True
createProofGlyph = False
# program
# Dependencies
import GlyphsApp
import vanilla
from vanilla import dialogs
import HT_LetterSpacer_lib
reload(HT_LetterSpacer_lib)
from defaultConfigFile import *
def readConfig():
directory, glyphsfile = os.path.split(Glyphs.font.filepath)
conffile = glyphsfile.split('.')[0] + "_autospace.py"
confpath = os.path.join(directory, conffile)
array = []
if os.path.isfile(confpath) == True:
print 'Config file exists'
else :
createFilePrompt = dialogs.askYesNo(\
messageText='\nMissing config file for this font.',\
informativeText='want to create one?')
if createFilePrompt == 1:
newFile = open(confpath,'w')
newFile.write(defaultConfigFile)
newFile.close()
elif createFilePrompt == 0 or createFilePrompt == -1:
Glyphs.clearLog()
Glyphs.showMacroWindow()
print "HT Letterspacer can't work without a config file"
return None
with open(confpath) as f:
for line in f:
if line[0] != '#' and len(line) > 5:
newline = line.split(",")
del newline[-1]
newline[3] = float(newline[3])
array.append(newline)
return array
class HTLetterspacerScript(object):
def __init__(self):
self.engine = HT_LetterSpacer_lib.HTLetterpacerLib()
self.font = Glyphs.font
selectedLayers = Glyphs.font.selectedLayers
if selectedLayers is None:
print("Nothing selected\n")
return
self.mySelection = list(set(selectedLayers))
self.output = ''
self.layerID = self.mySelection[0].associatedMasterId
self.master = self.font.masters[self.layerID]
self.config = readConfig()
self.engine.upm = self.font.upm
self.engine.angle = self.master.italicAngle
self.engine.xHeight = self.master.xHeight
if self.config :
self.getParams()
self.engine.tabVersion = False
self.engine.LSB = True
self.engine.RSB = True
if window:
self.window()
else:
self.spaceMain()
def getParams(self):
for param in ["paramArea", "paramDepth", "paramOver"]:
customParam = self.master.customParameters[param]
if customParam:
setattr(self.engine, param, float(customParam))
self.output += 'Using master custom parameter, %s: %s\n' % (param, float(customParam))
# print float(customParam)
else:
self.output += 'Using default parameter %s: %i\n' % (param, getattr(self.engine, param))
def window(self):
self.w = vanilla.FloatingWindow((250, 200), "HT Letterspacer", minSize=(225, 200), maxSize=(225, 200), autosaveName="com.ht.spacer")
self.w.text_3 = vanilla.TextBox((210, 25, -170, 14), "%", sizeStyle='small')
self.w.text_4 = vanilla.TextBox((15, 50, 100, 14), "Area", sizeStyle='small')
self.w.text_4b = vanilla.TextBox((140, 50, 50, 14), self.engine.paramArea, sizeStyle='small')
self.w.text_5 = vanilla.TextBox((15, 75, 100, 14), "Depth", sizeStyle='small')
self.w.text_5b = vanilla.TextBox((140, 75, 50, 14), self.engine.paramDepth, sizeStyle='small')
self.w.text_6 = vanilla.TextBox((15, 100, 100, 14), "Overshoot", sizeStyle='small')
self.w.text_6b = vanilla.TextBox((140, 100, 50, 14), self.engine.paramOver, sizeStyle='small')
self.w.LSB = vanilla.CheckBox((15, 25, 40, 18), "LSB", value=True, sizeStyle='small', callback=self.SavePreferences)
self.w.RSB = vanilla.CheckBox((15 + 45, 25, 40, 18), "RSB", value=True, sizeStyle='small', callback=self.SavePreferences)
self.w.tab = vanilla.CheckBox((15 + 45 + 45, 25, 60, 18), "Tabular", value=False, sizeStyle='small', callback=self.SavePreferences)
self.w.width = vanilla.EditText((170, 25, 40, 18), self.mySelection[0].width, sizeStyle='small')
self.w.area = vanilla.EditText((170, 50 - 3, 40, 18), "430", sizeStyle='small')
self.w.prof = vanilla.EditText((170, 75 - 3, 40, 18), "20", sizeStyle='small')
self.w.ex = vanilla.EditText((170, 100 - 3, 40, 18), "0", sizeStyle='small')
self.w.runButton = vanilla.Button((15, 125, 90, 25), "Apply", sizeStyle='small', callback=self.dialogCallback)
self.w.setDefaultButton(self.w.runButton)
if not self.LoadPreferences():
print "Error: Could not load preferences. Will resort to defaults."
self.w.open()
def dialogCallback(self, sender):
self.output = ""
self.engine.paramArea = int(self.w.area.get())
self.engine.paramDepth = float(self.w.prof.get())
self.engine.paramOver = int(self.w.ex.get())
self.engine.tabVersion = self.w.tab.get()
self.engine.LSB = self.w.LSB.get()
self.engine.RSB = self.w.RSB.get()
self.engine.width = float(self.w.width.get())
self.mySelection = list(set(Glyphs.font.selectedLayers))
self.spaceMain()
if not self.SavePreferences(self):
print "Note: Couldn't save preferences."
def SavePreferences(self, sender):
try:
Glyphs.defaults["com.ht.spacer.LSB"] = self.w.LSB.get()
Glyphs.defaults["com.ht.spacer.RSB"] = self.w.RSB.get()
Glyphs.defaults["com.ht.spacer.tab"] = self.w.tab.get()
Glyphs.defaults["com.ht.spacer.area"] = self.w.area.get()
Glyphs.defaults["com.ht.spacer.depth"] = self.w.prof.get()
Glyphs.defaults["com.ht.spacer.over"] = self.w.ex.get()
except:
return False
return True
def LoadPreferences(self):
try:
self.w.LSB.set(Glyphs.defaults["com.ht.spacer.LSB"])
self.w.RSB.set(Glyphs.defaults["com.ht.spacer.RSB"])
self.w.tab.set(Glyphs.defaults["com.ht.spacer.tab"])
self.w.area.set(Glyphs.defaults["com.ht.spacer.area"])
self.w.prof.set(Glyphs.defaults["com.ht.spacer.depth"])
self.w.ex.set(Glyphs.defaults["com.ht.spacer.over"])
except:
return False
return True
# progress bar
def progressBar(self):
self.p = vanilla.Window((300, 40))
self.p.bar = vanilla.ProgressBar((10, 10, -10, 16))
self.p.open()
self.p.bar.set(0)
self.wunit = 100.000 / len(self.mySelection)
def findException(self):
exception = False
items = []
for item in self.config:
if self.script == item[0] or item[0] == '*':
if self.category == item[1] or item[1] == '*':
if self.subCategory == item[2] or item[2] == '*':
if not exception or item[5] in self.glyph.name:
exception = item
return exception
def setG(self, layer):
if layer.isKindOfClass_(objc.lookUpClass("GSControlLayer")):
return
self.output = '\\' + layer.parent.name + '\\\n' + self.output
self.layerID = layer.associatedMasterId
self.master = self.font.masters[self.layerID]
self.glyph = layer.parent
self.layer = layer
self.category = layer.parent.category
self.subCategory = layer.parent.subCategory
self.script = layer.parent.script
self.engine.reference = self.glyph.name
exception = self.findException()
if (exception):
self.engine.factor = exception[3]
item = exception[4]
if item != '*':
self.engine.reference = item
self.engine.newWidth = False
# check reference layer existance and contours
if self.font.glyphs[self.engine.reference]:
self.referenceLayer = self.font.glyphs[self.engine.reference].layers[self.layerID]
if len(self.referenceLayer.paths) < 1:
self.output += "WARNING: The reference glyph declared (" + self.engine.reference + ") doesn't have contours. Glyph " + self.layer.parent.name + " was spaced uses its own vertical range.\n"
self.referenceLayer = self.layer
else:
self.referenceLayer = self.layer
self.output += "WARNING: The reference glyph declared (" + self.engine.reference + ") doesn't exist. Glyph " + self.layer.parent.name + " was spaced uses its own vertical range.\n"
def spaceMain(self):
for layer in self.mySelection:
self.setG(layer)
lpolygon, rpolygon = self.engine.spaceMain(layer, self.referenceLayer)
print(self.output)
if len(self.mySelection) < 2 and createProofGlyph and lpolygon is not None:
self.engine.createAreasGlyph(self.font, self.mySelection[0],self.layerID, [lpolygon, rpolygon])
if self.font.currentTab:
self.font.currentTab.forceRedraw()
HTLetterspacerScript()