forked from adobe-type-tools/python-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernExport.py
226 lines (171 loc) · 6.86 KB
/
kernExport.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
import os
from defcon import Font as defconFont
__license__ = """
Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
__copyright__ = __license__
class ClassKerningToUFO(object):
'''
Module to write FontLab class-kerning to UFO.
Needs to be called with a FL font object (fl.font), and a prefixOption.
The prefixOption is used for renaming kerning classes to various UFO-styles.
If the prefixOption is None, class names will be prefixed with to @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 prefixOptions are:
'MM': convert class names to MetricsMachine-readable group names
'UFO3': convert to UFO3-style class names
usage (one of the three):
kernExport.ClassKerningToUFO(fl.font)
kernExport.ClassKerningToUFO(fl.font, prefixOption = 'MM')
kernExport.ClassKerningToUFO(fl.font, prefixOption = 'UFO3')
'''
def __init__(self, font, prefixOption = None):
self.f = font
self.destFont = self.getUFO()
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 goodbye(self):
print 'Unhappy End.'
def getUFO(self):
UFOfound = False
assumedUFO = self.f.file_name.replace('.vfb', '.ufo')
if os.path.exists(assumedUFO):
print 'UFO found at %s.' % assumedUFO
foundFont = defconFont(assumedUFO)
UFOfound = True
else:
try:
from robofab.interface.all.dialogs import GetFile
except ImportError:
print 'No Robofab installed, this script ends here; also the World!!!'
self.goodbye()
userPick = GetFile('Select corresponding UFO file:')
if not userPick:
print 'No UFO picked.'
self.goodbye()
else:
if os.path.splitext(userPick)[1].lower() == '.ufo':
foundFont = defconFont(userPick)
UFOfound = True
if UFOfound:
return foundFont
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):
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.value)
pair = self.getClass(gName,'left'), self.getClass(gNameRightglyph,'right')
self.kerning[pair] = kernValue
def analyzeKernClasses(self):
print 'analyzing classes ...'
classes = {}
for ci, className in enumerate(self.f.classes):
if className[0] == '_': # it is a kerning class
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 = ":"
className = c.split(sep)[0] # FL class name, e.g. _L_LC_LEFT
# OTgroupName = '@%s' % className[1:] # OT group name, e.g. @L_LC_LEFT
leftName = '%s%s' % (self.leftPrefix, className[1:])
rightName = '%s%s' % (self.rightPrefix, className[1:])
glyphList = c.split(sep)[1].split()
cleanGlyphList = [i.strip("'") for i in glyphList] # strips out the keyglyph marker
if '_EXC_' in className:
# Exception classes: (complicated invention sometimes used when generating
# kern features from FL, messes up MetricsMachine, therefore I include them as ref groups only.)
self.groups[className] = cleanGlyphList
print "%s is an exception class. Adding to UFO as reference group." % (className)
else:
for g in glyphList:
if g[-1] == "'": # finds keyglyph
rep = g.strip("'")
repFound = True
break
else:
rep = glyphList[0]
if repFound == False:
print "\tWARNING: Kerning class %s has no explicit key glyph.\n\tAssuming it is the first glyph found (%s).\n" % (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.\n" % className
def run(self):
glyphset = [g.name for g in self.f.glyphs]
if not set(glyphset) <= set(self.destFont.keys()):
# test if glyphs in font are a subset of the UFO; in case a wrong UFO is picked.
print 'Glyphs in VFB and UFO do not match.'
self.goodbye()
else:
self.analyzeKernClasses()
self.readFontKerning()
self.destFont.groups.clear()
self.destFont.kerning.clear()
self.destFont.groups.update(self.groups)
self.destFont.kerning.update(self.kerning)
self.destFont.save()
print 'done'
__doc__ = ClassKerningToUFO.__doc__