forked from typedev/MMK_FL_kerning_exchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KernExchangeFLMM.py
439 lines (350 loc) · 13.4 KB
/
KernExchangeFLMM.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# FLM: KERN Exchange FL/MM
"""
Exchange classes and kerning between Fontlab and MetricsMachine
typedev
Alexander Lubovenko
www.github.com/typedev
"""
import os
from time import asctime
from robofab.world import CurrentFont
from robofab.ufoLib import UFOWriter, UFOReader
# from robofab.interface.all.dialogs import ProgressBar
from dialogKit import *
VERSION = '0.2.2'
WARNING_TEXT = """The .vfb and UFO must be in the same folder, and the names match fontname.vfb=fontname.ufo
WARNING! I strongly recommended to backup your .vfb and .ufo files before any operation with Import and Export kerning.
"""
HELP_TEXT = """Export to UFO: FontLab will replace all groups, kerning and font.lib from VFB file during export to UFO.
Import from UFO: Only groups and kerning will be replaced during import from UFO.
"""
# IMPORT section =========================================
def get_L_R_ident (kern_L_R_table, n_group):
l = ''
r = ''
if kern_L_R_table.has_key(n_group):
L, R = kern_L_R_table[n_group]
if L: l = '*LEFT'
if R: r = '*RIGHT'
return l + r
def compareContent (g1, g2):
a = ''.join(g1)
b = ''.join(g2)
if a != b:
return False
else:
return True
def diffGroups (oldTable, newTable, kern_L_R_table):
newGroups = {}
delGroups = {}
chgGroups = {}
# progress = ProgressBar('Making report: Classes', ticks = len(newTable.items()) + len(oldTable.items()))
report = '\n\n* * * * * Groups report:'
for n_group, content in newTable.items():
# progress.tick()
if not oldTable.has_key(n_group):
newGroups[n_group] = content
report = report + '\n\nNew Group Added: %s%s [%s]' % (
n_group, get_L_R_ident(kern_L_R_table, n_group), ' '.join(content))
else:
if not compareContent(oldTable[n_group], newTable[n_group]):
chgGroups[n_group] = [oldTable[n_group], newTable[n_group]]
report = report + '\n\nChanged Group: %s%s' % (n_group, get_L_R_ident(kern_L_R_table, n_group) )
report = report + '\n\told: [%s]' % (' '.join(oldTable[n_group]))
report = report + '\n\tnew: [%s]' % (' '.join(newTable[n_group]))
for o_group, content in oldTable.items():
# progress.tick()
if not newTable.has_key(o_group):
delGroups[o_group] = content
report = report + '\n\nGroup Deleted: %s [%s]' % (o_group, ' '.join(content))
report = report + '\n\nGroups TOTAL: Added=%i Deleted=%i Changed=%i' % (
len(newGroups), len(delGroups), len(chgGroups))
# progress.close()
return report#, newGroups, delGroups, chgGroups
def diffKerning (oldTable, newTable):
newPairs = {}
delPairs = {}
chgPairs = {}
nulPairs = {}
report = '\n\n* * * * * Kerning report:\n'
# progress = ProgressBar('Making report: Kerning', ticks = len(newTable.items()) + len(oldTable.items()))
for (l, r), v in newTable.items():
# # progress.tick()
if v == 0:
nulPairs[(l, r)] = v
else:
if not oldTable.has_key((l, r)):
newPairs[(l, r)] = v
report = report + '\nNew Pair: %s %s %i' % (l, r, v)
else:
if oldTable[(l, r)] != newTable[(l, r)]:
chgPairs[(l, r)] = [oldTable[(l, r)], newTable[(l, r)]]
report = report + '\nChanged Pair: %s %s' % (l, r)
report = report + '\n\told: %i\tnew: %i' % (oldTable[(l, r)], newTable[(l, r)])
for (l, r), v in oldTable.items():
# progress.tick()
if not newTable.has_key((l, r)):
delPairs[(l, r)] = v
report = report + '\nPair Deleted: %s %s %i' % (l, r, v)
report = report + '\n\nPairs TOTAL: Added=%i Deleted=%i Changed=%i Null pairs (ignored)=%i' % (
len(newPairs), len(delPairs), len(chgPairs), len(nulPairs))
# progress.close()
return report#,newPairs, delPairs, chgPairs, nulPairs
def generateClassName (kernClasses, classname):
for i in range(1, 1000, 1):
if not kernClasses.has_key(classname + str(i)):
return classname + str(i)
break
def importKerningMMK (font, UFOfilepath):
report = 'Import Kerning Report ' + asctime() + '\nUFO file: ' + UFOfilepath
kernClasses = {}
kern_L_R_table = {}
kernTable = {}
feaClasses = {}
dicGroups = {}
UFO = UFOReader(UFOfilepath)
kernGroups = UFO.readGroups()
kernTable = UFO.readKerning()
# progress = ProgressBar('Converting group names', ticks = len(kernGroups.items()))
for groupname, content in kernGroups.items():
# progress.tick()
if len(content) != 0:
if content[0] == '':
content.pop(0)
if groupname.startswith('@'): # @MMK_ = MM kern class
classname = '_' + groupname[7:]
baseGlyph = content[0]
content[0] = baseGlyph + '\''
classcontent = content
dicGroups[groupname] = baseGlyph
if kernClasses.has_key(classname):
if kernClasses[classname] == classcontent:
kern_L_R_table[classname] = (True, True)
else:
classname = generateClassName(kernClasses, classname)
if groupname[5] == 'L': # @MMK_L_namegroup
kern_L_R_table[classname] = (True, False)
elif groupname[5] == 'R': # @MMK_R_namegroup
kern_L_R_table[classname] = (False, True)
else:
print "WARNING! Something wrong whith LEFT/RIGHT identification...", groupname
else:
if groupname[5] == 'L': # @MMK_L_namegroup
kern_L_R_table[classname] = (True, False)
elif groupname[5] == 'R': # @MMK_R_namegroup
kern_L_R_table[classname] = (False, True)
else:
print "WARNING! Something wrong whith LEFT/RIGHT identification...", groupname
kernClasses[classname] = classcontent
else: # fea class
feaClasses[groupname] = content#' '.join(content)
else:
print 'WARNING! Group with NULL content: %s Ignored.' % groupname
report = report + '\n\nGroup with NULL content: %s Ignored.' % groupname
# progress.close()
# progress = ProgressBar('Merging kerning and fea-classes',ticks = len(kernClasses.items()) + len(feaClasses.items()))
classes = {}
for classname, content in kernClasses.items():
# progress.tick()
classes[classname] = content#.split(' ')
for classname, content in feaClasses.items():
# progress.tick()
classes[classname] = content#.split(' ')
# progress.close()
report = report + diffGroups(font.groups, classes, kern_L_R_table)
font.groups.clear()
font.groups = classes
font.update()
# progress = ProgressBar('Left/Right identification', ticks = len(font.naked().classes))
for index, kernClass in enumerate(font.naked().classes):
# progress.tick()
if kernClass.startswith('_'):
nameClass = kernClass.split(':')[0]
leftBool, rightBool = kern_L_R_table[nameClass]
font.naked().SetClassFlags(index, leftBool, rightBool)
font.update()
print '\nConverting Groups from MetricsMachine to Fontlab Classes: DONE\n'
# progress.close()
# progress = ProgressBar('Left pairs converting', ticks = len(kernTable.items()))
new_kern1 = {}
for (left, right), value in kernTable.items():
# progress.tick()
if dicGroups.has_key(left):
baseGlyph = dicGroups[left]
new_kern1[(baseGlyph, right)] = value
else:
if left.startswith('@'):
print "WARNING! Something wrong with pair:", left, right, value, 'Ignored.'
else:
new_kern1[(left, right)] = value
# progress.close()
# progress = ProgressBar('Right pairs converting', ticks = len(new_kern1.items()))
new_kern2 = {}
for (left, right), value in new_kern1.items():
# progress.tick()
if dicGroups.has_key(right):
baseGlyph = dicGroups[right]
new_kern2[(left, baseGlyph)] = value
else:
if right.startswith('@'):
print "WARNING! Something wrong with pair:", left, right, value, 'Ignored.'
else:
new_kern2[(left, right)] = value
# progress.close()
report = report + diffKerning(font.kerning, new_kern2)
font.kerning.clear()
font.kerning.update(new_kern2)
font.update()
reportfile = open(UFOfilepath.replace('.ufo', '.log'), 'w')
reportfile.write(report)
reportfile.close()
print '\nConverting Kerning from MetricsMachine to Fontlab: DONE\n'
# END IMPORT section =====================================
# EXPORT section =========================================
KEY_MMK_Colors = 'com.typesupply.MetricsMachine4.groupColors'
GROUP_COLORS = [(1.0, 0.0, 0.0, 0.25),
(1.0, 0.5, 0.0, 0.25),
(1.0, 1.0, 0.0, 0.25),
(0.0, 1.0, 0.0, 0.25),
(0.0, 1.0, 1.0, 0.25),
(0.0, 0.5, 1.0, 0.25),
(0.0, 0.0, 1.0, 0.25),
(0.5, 0.0, 1.0, 0.25),
(1.0, 0.0, 1.0, 0.25),
(1.0, 0.0, 0.5, 0.25)]
def fixGlyphOrder (font, groupname, baseglyph):
newcontent = []
newcontent.append(baseglyph)
for glyphname in font.groups[groupname]:
if glyphname != baseglyph:
newcontent.append(glyphname)
font.groups[groupname] = newcontent
def checkGlyphOrder (font):
for group in font.groups.items():
content = []
groupname, content = group
for idx, glyphname in enumerate(content):
if ('\'' in glyphname) and (idx != 0):
print 'Group %s has wrong order... fixed.' % groupname
fixGlyphOrder(font, groupname, glyphname)
break
def checkContent (content):
result = []
for name in content:
if name != '':
result.append(name)
return result
def getKernStrukt (font):
feaClasses = []
kernClasses = []
kern_L_R_table = {}
for index, glyphClass in enumerate(font.naked().classes):
if glyphClass.startswith("_"):
kernClasses.append(glyphClass)
kern_L_R_table[glyphClass] = (font.naked().GetClassLeft(index), font.naked().GetClassRight(index))
else:
feaClasses.append(glyphClass)
return [kernClasses, kern_L_R_table, feaClasses]
def exportKerningFL (font, UFOfilepath):
MMK_kernClasses = []
MMK_baseGlyph_Left = {}
MMK_baseGlyph_Right = {}
UFO = UFOWriter(UFOfilepath)
kernClasses, kern_L_R_table, feaClasses = getKernStrukt(font)
for glyphClass in kernClasses:
MMK_glyphClass = glyphClass.replace('\'', '')
MMK_Name_glyphClass, MMK_Content_glyphClass = MMK_glyphClass.split(':')[0], MMK_glyphClass.split(':')[1]
MMK_Base_Glyph = MMK_Content_glyphClass.split(' ')[1]
leftClass, rightClass = kern_L_R_table[glyphClass]
if (leftClass == 1) and (rightClass == 1):
MMK_Name_glyphClass_L = '@MMK_L' + MMK_Name_glyphClass
MMK_kernClasses.append([MMK_Name_glyphClass_L, MMK_Content_glyphClass])
MMK_baseGlyph_Left[MMK_Base_Glyph] = MMK_Name_glyphClass_L
MMK_Name_glyphClass_R = '@MMK_R' + MMK_Name_glyphClass
MMK_kernClasses.append([MMK_Name_glyphClass_R, MMK_Content_glyphClass])
MMK_baseGlyph_Right[MMK_Base_Glyph] = MMK_Name_glyphClass_R
else:
if leftClass == 1:
MMK_Name_glyphClass = '@MMK_L' + MMK_Name_glyphClass
MMK_baseGlyph_Left[MMK_Base_Glyph] = MMK_Name_glyphClass
MMK_kernClasses.append([MMK_Name_glyphClass, MMK_Content_glyphClass])
if rightClass == 1:
MMK_Name_glyphClass = '@MMK_R' + MMK_Name_glyphClass
MMK_baseGlyph_Right[MMK_Base_Glyph] = MMK_Name_glyphClass
MMK_kernClasses.append([MMK_Name_glyphClass, MMK_Content_glyphClass])
if (leftClass == 0) and (rightClass == 0):
print 'WARNING! Wrong Kern group:', MMK_Name_glyphClass, '* NOT Left and NOT Right *. Please fix it.'
for glyphClass in feaClasses:
FEA_Name_glyphClass, FEA_Content_glyphClass = glyphClass.split(':')[0], glyphClass.split(':')[1]
MMK_kernClasses.append([FEA_Name_glyphClass, FEA_Content_glyphClass])
cycleCountColors = len(GROUP_COLORS)
dicColors = {}
groups = {}
for index, gl in enumerate(MMK_kernClasses):
content = gl[1].split(' ')
nameClass = gl[0]
content = checkContent(content)
groups[nameClass] = content
dicColors[nameClass] = GROUP_COLORS[index % cycleCountColors]
font.lib[KEY_MMK_Colors] = dicColors
font.update()
UFO.writeGroups(groups)
# TODO maybe need to compare UFO and VFB before overwriting font.LIB , and merge LIB if they are not equal
UFO.writeLib(font.lib)
kerntabl = font.kerning
new_kern1 = {}
new_kern2 = {}
for (left, right), value in kerntabl.items(): #font.kerning.items():
if MMK_baseGlyph_Left.has_key(left):
new_L = MMK_baseGlyph_Left[left]
new_kern1[(new_L, right)] = value
else:
new_kern1[(left, right)] = value
for (left, right), value in new_kern1.items(): #font.kerning.items():
if MMK_baseGlyph_Right.has_key(right):
new_R = MMK_baseGlyph_Right[right]
new_kern2[(left, new_R)] = value
else:
new_kern2[(left, right)] = value
UFO.writeKerning(new_kern2)
print 'Converting Classes and Kerning from Fontlab to MetricsMachine: DONE'
# END EXPORT section =====================================
# UI section =============================================
def getUFOpath (font):
ufoPath = font.path.replace(".vfb", ".ufo")
if os.path.exists(ufoPath):
print 'UFO file: ', ufoPath
return ufoPath
else:
print 'UFO file not found...'
return None
class KERNExchanger(object):
def __init__ (self):
self.w = ModalDialog((400, 320), 'Kerning Exchange FontLab<>MetricsMachine. v' + VERSION)
self.w.btnExport = Button((10, 25, 180, 20), 'Export Kerning to UFO', callback = self.run_exportFLK)
self.w.btnImport = Button((210, 25, 180, 20), 'Import Kerning from UFO', callback = self.run_importMMK)
self.w.warninglabel = TextBox((10, 70, -10, 100), WARNING_TEXT)
self.w.helplabel = TextBox((10, 180, -10, 80), HELP_TEXT)
self.w.open()
def run_importMMK (self, sender):
font = CurrentFont()
# print 'Import Kerning from:' + font.info.name
if font != None:
ufopath = getUFOpath(font)
if ufopath != None:
importKerningMMK(font, ufopath)
else:
print 'Please, open any .vfb with kerning :)'
self.w.close()
def run_exportFLK (self, sender):
font = CurrentFont()
if font != None:
checkGlyphOrder(font)
ufopath = getUFOpath(font)
if ufopath != None:
exportKerningFL(font, ufopath)
else:
print 'Please, open any .vfb with kerning :)'
self.w.close()
if __name__ == "__main__":
KERNExchanger()