-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvarfont-prep.py
451 lines (303 loc) · 12.5 KB
/
varfont-prep.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# varfont prep
from vanilla.dialogs import *
import os
import shutil
import datetime
from fontTools.designspaceLib import BaseDocReader, DesignSpaceDocument
from mojo.UI import OutputWindow
import helpers
from helpers.removeGlyphs import *
debug = True
if debug == True:
import importlib
importlib.reload(helpers.removeGlyphs)
from helpers.removeGlyphs import *
OutputWindow().show()
OutputWindow().clear()
report = """
Var Prep Report
********************************************
"""
now = datetime.datetime.now()
def getSourcePathsFromDesignspace():
designspacePath = getFile("select designspace for variable font",
allowsMultipleSelection=False, fileTypes=["designspace"])[0]
designspace = DesignSpaceDocument.fromfile(designspacePath)
inputFontPaths = []
for source in designspace.sources:
inputFontPaths.append(source.path)
return designspacePath, inputFontPaths
def openFontsInList(fontPaths):
fontsList = []
for path in fontPaths:
f = OpenFont(path, showInterface=False)
fontsList.append(f)
return fontsList
def checkIfSameFamilyName(fontsList):
fontFamilyNames = []
global report
for f in fontsList:
familyName = f.info.familyName
fontFamilyNames.append(familyName)
sameName = all(x == fontFamilyNames[0] for x in fontFamilyNames)
if sameName == True:
return sameName, fontFamilyNames[0]
else:
errorMsg = "The input UFOs have different font family names: " + \
str(set(fontFamilyNames))
# generateReport(inputFontPaths, errorMsg)
report += errorMsg + "\n"
return False, fontFamilyNames[0]
# return errorMsg
def makeVarFontPrepFolder(fontsList, designspacePath):
sameFamily, familyName = checkIfSameFamilyName(fontsList)
# # check that selected fonts have same family name
if sameFamily == True:
# get family name
familyName = familyName
else:
familyName = "ERROR: families have different names"
# make new folder name with font family name and "varfontprep" label
newFolderName = familyName.replace(" ", "_").lower() + "-varfontprep"
# get head of font path
head, tail = os.path.split(designspacePath)
# make new folder path
newFolderPath = head + "/" + newFolderName
now = datetime.datetime.now()
newFolderPath += "-" + now.strftime("%Y_%m_%d-%H_%M_%S")
os.mkdir(newFolderPath)
print(newFolderPath)
return str(newFolderPath)
def copyFonts(inputFontPaths, newFolderPath):
for fontPath in inputFontPaths:
head, tail = os.path.split(fontPath)
newPath = newFolderPath + "/" + tail
if os.path.exists(newPath) == False:
# copy UFO into newFolderPath
# "+ /varprep- +" was formerly added to path, before tail
shutil.copytree(fontPath, newPath)
designspacePath, inputFontPaths = getSourcePathsFromDesignspace()
inputFontsList = openFontsInList(inputFontPaths)
newFolderPath = makeVarFontPrepFolder(inputFontsList, designspacePath)
copyFonts(inputFontPaths, newFolderPath)
print(inputFontsList)
for f in inputFontsList:
f.close()
# open copied fonts in new dictionary
copiedFontPaths = []
for fontPath in inputFontPaths:
head, tail = os.path.split(fontPath)
newPath = newFolderPath + "/" + tail
copiedFontPaths.append(newPath)
print(newPath)
fontsList = openFontsInList(copiedFontPaths)
print(fontsList)
###############################################
######### copy and update designspace #########
def copyDesignSpace(designspacePath, newFolderPath):
# duplicate designspace into new folder
inputDStail = os.path.split(designspacePath)[1]
outputDSpath = newFolderPath + "/" + inputDStail
shutil.copyfile(designspacePath, outputDSpath)
# update source & instance paths in designspace as needed
outputDS = DesignSpaceDocument.fromfile(outputDSpath)
# updates path if sources were originally in a different directory than designspace file
for source in outputDS.sources:
newFontPath = newFolderPath + '/' + os.path.split(source.path)[1]
source.path = newFontPath
outputDS.write(outputDSpath)
copyDesignSpace(designspacePath, newFolderPath)
#########################################
######### make fonts compatible #########
#########################################
copiedFonts = []
# get paths
for file in os.listdir(newFolderPath):
if os.path.splitext(file)[1] == ".ufo":
copiedFonts.append(file)
listOfGlyphsLists = []
def addGlyphListToGlyphLists(f):
fontName = f.info.familyName + " " + f.info.styleName
# print(fontName)
glyphs = []
for g in f:
glyphs.append(g.name)
listOfGlyphsLists.append(glyphs)
# should you only remove glyphs ONCE? make list of compatible AND similar glyphs?
def constrainCharSetToSimilarGlyphs(f, commonGlyphs):
global report
report += "Unique glyphs removed from " + \
f.info.familyName + " " + f.info.styleName + ":\n"
# print(f.info.familyName + " " + f.info.styleName)
# print(f.keys())
diff = set(f.keys()) - set(commonGlyphs)
print('diff of font keys vs commonGlyphs')
# print(list(diff).sort())
print(diff)
uncommonGlyphs = []
for g in f:
if g.name not in commonGlyphs:
uncommonGlyphs.append(g.name)
# print(g.name + " removed from " + f.info.styleName)
report += g.name + ", "
print("removing uncommon glyphs from ", f.info.styleName)
# print(list(uncommonGlyphs).sort())
print(uncommonGlyphs)
diffDiffs = set(diff) - set(uncommonGlyphs)
print("\n--------------------------")
print('diff of diffs?')
print(diffDiffs)
removeGlyphs(f, uncommonGlyphs)
report += "\n \n"
# --------------------------------------------------------------------------------------------------------
# decompose non-exporting glyphs (glyphs with a underscore leading in their name, such as "_arrowhead")
def nonExporting(glyphName):
if glyphName[0] == "_":
return True
def findAndDecomposeComponents(font, componentNames):
for g in font:
if len(g.components) > 0:
for component in g.components:
if component.baseGlyph in componentNames:
component.decompose()
def decomposeNonExportingComponents(f):
global report
nonExportingGlyphs = []
for g in f:
print("\t",g.name)
if nonExporting(g.name) == True:
# add to list
nonExportingGlyphs.append(g.name)
findAndDecomposeComponents(f, nonExportingGlyphs)
removeGlyphs(f, nonExportingGlyphs)
report += f"Decomposed and removed non-exporting glyphs: {nonExportingGlyphs}"
# --------------------------------------------------------------------------------------------------------
# remove guides
def removeGuides(f):
global report
report += "******************* \n"
report += "Guides removed from " + \
f.info.familyName + " " + f.info.styleName + ":\n"
for g in f:
if g.guidelines != ():
g.clearGuidelines()
report += g.name + "; "
report += "\n \n"
# --------------------------------------------------------------------------------------------------------
# compatiblity checks
def findCompatibleGlyphs(fontsList):
global report
nonCompatibleGlyphs = []
nonCompatibleGlyphsReport = ""
for g in fontsList[0]:
# print(g.name)
# f in all fonts
for checkingFont in fontsList:
print(checkingFont.path)
# test glyphCompatibility
if g.name in checkingFont.keys():
glyphCompatibility = g.isCompatible(checkingFont[g.name])
if glyphCompatibility[0] != True:
print(glyphCompatibility[0])
nonCompatibleGlyphs.append(g.name)
nonCompatibleGlyphsReport += g.name + \
"\n" + str(glyphCompatibility) + "\n"
else :
nonCompatibleGlyphs.append(g.name)
nonCompatibleGlyphsReport += g.name + \
"\n" + str(glyphCompatibility) + "\n"
report += "\n ******************* \n"
report += "non-compatible glyphs removed: \n"
report += nonCompatibleGlyphsReport
return nonCompatibleGlyphs
# remove glyphs that aren't compatible in every font
def removeNonCompatibleGlyphs(f, nonCompatibleGlyphs):
removeGlyphs(f, nonCompatibleGlyphs)
for g in f.templateKeys():
f.removeGlyph(g)
if 'space' not in f.keys():
f.newGlyph('space')
f['space'].unicode = '0020'
f['space'].width = 600
if f['space'].width == 0:
f['space'].width = 600
# --------------------------------------------------------------------------------------------------------
# FIX FILES
# in first pass, decompose nonExporting glyphs, remove guides, and find common glyphs
print("\n---------------------------------------------------\n")
print("first pass: decompose nonExporting glyphs, remove guides, and find common glyphs")
for f in fontsList:
print(f.info.styleName)
# decompose non-exporting glyphs
decomposeNonExportingComponents(f)
# remove guides
removeGuides(f)
# TODO (Urgent) – fix function to find uncommon glyphs. Currently seems to be failing
# in second pass, remove glyphs that aren't present in every font
print("\n---------------------------------------------------\n")
print("second pass: remove glyphs that aren't present in every font")
for f in fontsList:
# create lists of glyphs in each font
addGlyphListToGlyphLists(f)
# print("\n---------------------------------------------------\n")
# print("listOfGlyphsLists")
# print(listOfGlyphsLists)
# create one list of glyphs present in every font
commonGlyphs = set(listOfGlyphsLists[0]).intersection(*listOfGlyphsLists[1:])
# commonGlyphs = set(fontsList[0].keys()).intersection(*fontsList[1:].keys())
print("commonGlyphs")
print(commonGlyphs)
for f in fontsList:
constrainCharSetToSimilarGlyphs(f, commonGlyphs)
# find compatible glyphs
nonCompatibleGlyphs = findCompatibleGlyphs(fontsList)
# remove nonCompatibleGlyphs from each font
for f in fontsList:
removeNonCompatibleGlyphs(f, nonCompatibleGlyphs)
# --------------------------------------------------------------------------------------------------------
# kerning compatibility
# check if there is kerning in any of the UFOs
kerningInFonts = False
for f in fontsList:
if len(f.kerning) > 0:
kerningInFonts = True
# if there is kerning in some UFOs, check others and add blank kerning if needed
if kerningInFonts:
report += "\n ******************* \n"
for f in fontsList:
if len(f.kerning) == 0:
f.kerning[("A", "A")] = 0
print("adding blank kerning to ", f.info.styleName)
report += f"Adding blank kerning to {f.info.styleName}\n"
report += "\n ******************* \n"
# --------------------------------------------------------------------------------------------------------
# sort fonts
print("\nsorting fonts\n")
def sortFont(font):
# the new default is at the end, so this will re-apply a "smart sort" to the font
newGlyphOrder = font.naked().unicodeData.sortGlyphNames(font.glyphOrder, sortDescriptors=[dict(type="cannedDesign", ascending=True, allowPseudoUnicode=True)])
font.glyphOrder = newGlyphOrder
for f in fontsList:
sortFont(f)
# --------------------------------------------------------------------------------------------------------
# close fonts
for f in fontsList:
f.save()
f.close()
#########################################################################
############# TO DO: present vanilla.ProgressBar ########################
#########################################################################
#########################################################################
############# TO DO: sort fonts in the same way #########################
#########################################################################
#########################################################################
############# TO DO: check anchor compatibility ? #######################
#########################################################################
#########################################
############# write report ##############
#########################################
reportOutput = open(newFolderPath + "/" + 'varfontprep-report.txt', 'w')
reportOutput.write(now.strftime("%H:%M:%S; %d %B, %Y\n\n"))
reportOutput.write("*******************\n")
reportOutput.write(report)
reportOutput.close()