-
Notifications
You must be signed in to change notification settings - Fork 8
/
kernInfoWindow.py
216 lines (161 loc) · 6.13 KB
/
kernInfoWindow.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
# coding: utf-8
from vanilla import Window, RadioGroup, TextBox, Slider, Button
import getKerningPairsFromUFO
import os
class KernInfoWindow(object):
def __init__(self):
self.f = CurrentFont()
self.u = getKerningPairsFromUFO.UFOkernReader(self.f)
self.absKerning = int(self.u.absoluteKerning)
self.amountOfPairs = len(self.u.allKerningPairs)
self.textString = (
'The font has %s flat kerning pairs.\n'
'Set at %s points, the absolute amount\n'
'of kerning would cover the distance of\n%s.')
wWidth = 300
wHeight = 250
if self.amountOfPairs:
message = u'CONGRATULATIONS! \U0001F600'
else:
message = u'Bummer. \U0001F622'
self.w = Window((wWidth, wHeight), message)
self.w.measurementSystem = RadioGroup(
(20, 15, -10, 20),
["Metric", "Imperial"],
callback=self.parametersChanged,
isVertical=False)
self.w._pointSize = TextBox(
(20, 145, -30, 17),
'Point size:')
self.w.pointSize = Slider(
(100, 145, -30, 17),
minValue=0,
maxValue=1000,
callback=self.parametersChanged,
value=12)
pointSize = int(self.w.pointSize.get())
absKerning = int(
self.absKerning * (pointSize / self.f.info.unitsPerEm))
self.w.text = TextBox(
(20, 45, -20, 85),
self.textString % (
self.amountOfPairs,
int(self.w.pointSize.get()),
self.convertToMetric(absKerning)))
self.w.button = Button(
(20, -40, -30, 20),
"Copy kerning pairs to clipboard",
callback=self.button)
self.w.open()
def convertToImperial(self, number):
remainderList = []
if number >= 72:
# points to inches:
number, remainder = divmod(number, 72)
remainderList.insert(0, remainder)
if number == 0:
remainderList.insert(0, number)
if number >= 12:
# inches to feet:
number, remainder = divmod(number, 12)
remainderList.insert(0, remainder)
if number == 0:
remainderList.insert(0, number)
# if number >= 3:
# # feet to yards:
# number, remainder = divmod(number, 3)
# remainderList.insert(0, remainder)
# if number == 0:
# remainderList.insert(0, number)
if number >= 5280:
# feet to miles:
number, remainder = divmod(number, 5280)
remainderList.insert(0, remainder)
if number == 0:
remainderList.insert(0, number)
remainderList.insert(0, number)
while len(remainderList) < 4:
remainderList.insert(0, 0)
remainderList = [int(round(number)) for number in remainderList]
unitsList = ['miles', 'feet', 'inches', 'points']
singularUnits = {
'miles': 'mile',
'feet': 'foot',
'inches': 'inch',
'points': 'point'}
combinedList = zip(remainderList, unitsList)
sentenceList = []
for (number, unit) in combinedList:
if int(number) != 0:
if int(number) == 1:
sentenceList.append(
u'%s\u00A0%s' % (number, singularUnits[unit]))
else:
sentenceList.append(
u'%s\u00A0%s' % (number, unit))
if len(sentenceList) == 0:
sentenceList = ['absolutely zero']
if len(sentenceList) > 1:
sentenceList.insert(-1, 'and')
sentence = ', '.join(sentenceList)
sentence = sentence.replace(', and,', ' and')
return sentence
def convertToMetric(self, number):
# ps point assumed
number = number * 0.352777778
# ps point to millimeters
remainderList = []
if number >= 10:
# mm to cm
number, remainder = divmod(number, 10)
remainderList.insert(0, remainder)
if number == 0:
remainderList.insert(0, number)
if number >= 100:
# cm to m
number, remainder = divmod(number, 100)
remainderList.insert(0, remainder)
if number == 0:
remainderList.insert(0, number)
if number >= 1000:
# m to km
number, remainder = divmod(number, 1000)
remainderList.insert(0, remainder)
if number == 0:
remainderList.insert(0, number)
remainderList.insert(0, number)
remainderList = [int(round(number)) for number in remainderList]
while len(remainderList) < 4:
remainderList.insert(0, 0)
unitsList = ['km', 'm', 'cm', 'mm']
combinedList = zip(remainderList, unitsList)
sentenceList = []
for (number, unit) in combinedList:
if number != 0:
sentenceList.append(u'%s\u00A0%s' % (number, unit))
if len(sentenceList) == 0:
sentenceList = ['absolutely zero']
if len(sentenceList) > 1:
sentenceList.insert(-1, 'and')
sentence = ', '.join(sentenceList)
sentence = sentence.replace(', and,', ' and')
return sentence
def parametersChanged(self, sender=None):
measurementSystem = self.w.measurementSystem.get()
pointSize = int(self.w.pointSize.get() / 4.0) * 4
absKerning = int(self.absKerning * pointSize / self.f.info.unitsPerEm)
if measurementSystem == 0:
absKerning = self.convertToMetric(absKerning)
else:
absKerning = self.convertToImperial(absKerning)
self.w.text.set(
self.textString % (self.amountOfPairs, pointSize, absKerning))
def button(self, sender=None):
output = '\n'.join(self.u.output)
scrap = os.popen('pbcopy', 'w')
scrap.write(output)
scrap.close()
if CurrentFont():
KernInfoWindow()
else:
print('No font is open.')