-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathflKernExport.py
More file actions
244 lines (198 loc) · 8.72 KB
/
flKernExport.py
File metadata and controls
244 lines (198 loc) · 8.72 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
'''
This FLS5 module can be used to export FontLab class-kerning to adjacent UFO
files. It works for both single- and Multiple Master VFBs.
The module needs to be called with a `fl.font` object, and a `prefixOption`.
The `prefixOption` is used for renaming kerning classes to work in various
UFO-related scenarios.
If the prefixOption is `None`, class names will be prefixed with
`@L_ and `@R_`, to keep track of their side (in case they need to be
converted the opposite way and re-imported to FL).
The prefix options are:
* `MM`: convert class names to MetricsMachine-readable group names
* `UFO3`: convert to UFO3-style class names
usage (one of the three):
flKernExport.ClassKerningToUFO(fl.font)
flKernExport.ClassKerningToUFO(fl.font, prefixOption='MM')
flKernExport.ClassKerningToUFO(fl.font, prefixOption='UFO3')
'''
import os
from defcon import Font as defconFont
class ClassKerningToUFO(object):
def __init__(self, font, prefixOption=None):
self.f = font
self.leftKeyGlyphs = {}
self.rightKeyGlyphs = {}
self.groups = {}
self.kerning = {}
self.leftPrefix = '@L_'
self.rightPrefix = '@R_'
self.MMleftPrefix = '@MMK_L_'
self.MMrightPrefix = '@MMK_R_'
self.UFO3leftPrefix = 'public.kern1.'
self.UFO3rightPrefix = 'public.kern2.'
if prefixOption == 'MM':
self.leftPrefix = self.MMleftPrefix
self.rightPrefix = self.MMrightPrefix
if prefixOption == 'UFO3':
self.leftPrefix = self.UFO3leftPrefix
self.rightPrefix = self.UFO3rightPrefix
self.run()
def getClass(self, glyphName, side):
''''
Replaces a glyph name by its class name,
in case it is a key glyph for that side.
'''
if side == 'left':
if glyphName in self.leftKeyGlyphs:
return self.leftKeyGlyphs[glyphName]
else:
return glyphName
if side == 'right':
if glyphName in self.rightKeyGlyphs:
return self.rightKeyGlyphs[glyphName]
else:
return glyphName
def readFontKerning(self, master_idx=0):
print('analyzing kerning ...')
glyphs = self.f.glyphs
for gIdx in range(len(glyphs)):
gName = str(glyphs[gIdx].name)
gKerning = glyphs[gIdx].kerning
for gKern in gKerning:
gNameRightglyph = str(glyphs[gKern.key].name)
kernValue = int(gKern.values[master_idx])
pair = (self.getClass(gName, 'left'),
self.getClass(gNameRightglyph, 'right'))
self.kerning[pair] = kernValue
def analyzeKernClasses(self):
if self.classes_already_analyzed:
return
print('analyzing classes ...')
classes = {}
for ci, className in enumerate(self.f.classes):
# it is a kerning class
if className[0] == '_':
if ((self.f.GetClassLeft(ci),
self.f.GetClassRight(ci)) == (1, 0)):
classes[className] = "LEFT"
elif ((self.f.GetClassLeft(ci),
self.f.GetClassRight(ci)) == (0, 1)):
classes[className] = "RIGHT"
elif ((self.f.GetClassLeft(ci),
self.f.GetClassRight(ci)) == (1, 1)):
classes[className] = "BOTH"
else:
classes[className] = "NONE"
for c in classes:
repFound = False
sep = ":"
# FL class name, e.g. _L_LC_LEFT
className = c.split(sep)[0]
leftName = '%s%s' % (self.leftPrefix, className[1:])
rightName = '%s%s' % (self.rightPrefix, className[1:])
glyphList = c.split(sep)[1].split()
# strips out the keyglyph marker
cleanGlyphList = [i.strip("'") for i in glyphList]
if '_EXC_' in className:
# Exception classes: (complicated invention sometimes used when
# generating kern features from FL, messes up the handling in
# MetricsMachine, therefore included as reference groups only.)
self.groups[className] = cleanGlyphList
print("\tWARNING: %s is an exception class. Adding to UFO as "
"reference group." % (className))
elif not glyphList:
print("\tWARNING: Kerning class %s is empty. "
"Skipping." % className)
else:
for g in glyphList:
if g[-1] == "'": # finds keyglyph
rep = g.strip("'")
repFound = True
break
else:
rep = glyphList[0]
if not repFound:
print("\tWARNING: Kerning class %s has no explicit key "
"glyph. Assuming it is the first glyph found: "
"%s" % (className, glyphList[0]))
if classes[c] == 'LEFT':
self.leftKeyGlyphs[rep] = leftName
self.groups[leftName] = cleanGlyphList
elif classes[c] == 'RIGHT':
self.rightKeyGlyphs[rep] = rightName
self.groups[rightName] = cleanGlyphList
elif classes[c] == 'BOTH':
self.leftKeyGlyphs[rep] = leftName
self.groups[leftName] = cleanGlyphList
self.rightKeyGlyphs[rep] = rightName
self.groups[rightName] = cleanGlyphList
else:
print("\tWARNING: Kerning class %s is not assigned to any "
"side (No checkbox active). Skipping." % className)
def getUFOs(self):
# number of masters
masters_count = self.f[0].layers_number
# font is single master
# allow picking the matching UFO, if none is found
if masters_count == 1:
foundFont = []
assumedUFO = self.f.file_name.replace('.vfb', '.ufo')
if os.path.exists(assumedUFO):
print('UFO found at %s' % assumedUFO)
foundFont.append(defconFont(assumedUFO))
else:
try:
from robofab.interface.all.dialogs import GetFile
userPick = GetFile('Select corresponding UFO file:')
except ImportError as err:
print('%s was found.' % err)
print('Get it at '
'https://github.com/robofab-developers/robofab')
print('')
userPick = None
if userPick:
if os.path.splitext(userPick)[1].lower() == '.ufo':
print('UFO selected at %s' % userPick)
foundFont.append(defconFont(userPick))
else:
print('\tERROR: The file selected was not a UFO.')
return foundFont
# font is multiple masters
# all matching UFOs must exist
else:
assumedUFOs = []
for i in range(masters_count):
assumedUFOs.append(self.f.file_name.replace('.vfb',
'_%s.ufo' % i))
# validate the paths
if not all([os.path.exists(ufo_path) for ufo_path in assumedUFOs]):
for ufo_path in assumedUFOs:
if not os.path.exists(ufo_path):
print('\tERROR: %s not found.' % ufo_path)
return []
else:
print('UFOs found at')
print('\t%s' % '\n\t'.join(assumedUFOs))
return [defconFont(ufo_path) for ufo_path in assumedUFOs]
def run(self):
glyphset = [g.name for g in self.f.glyphs]
ufos_list = self.getUFOs()
self.classes_already_analyzed = False
for master_idx, ufo in enumerate(ufos_list):
if not set(glyphset) <= set(ufo.keys()):
# test if glyphs in font are a subset of the UFO;
# in case a wrong UFO got picked.
print('Glyphs in VFB and UFO do not match.')
print('Skipped %s' % ufo.path)
else:
self.analyzeKernClasses()
self.classes_already_analyzed = True
self.readFontKerning(master_idx)
ufo.groups.clear()
ufo.kerning.clear()
ufo.groups.update(self.groups)
ufo.kerning.update(self.kerning)
ufo.save()
print('done\n')
if __name__ == '__main__':
print(__doc__)