-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path01_export_fontlab_lato.vfpy
More file actions
executable file
·226 lines (197 loc) · 7.76 KB
/
Copy path01_export_fontlab_lato.vfpy
File metadata and controls
executable file
·226 lines (197 loc) · 7.76 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
import os
import os.path
import glob
from collections import OrderedDict
from fontlab import *
from PythonQt import QtCore, QtGui, Qt
ws = flWorkspace.instance()
main = ws.mainWindow
item = flItems
qapp = QtGui.QApplication.instance()
pref = flPreferences()
settings = QtCore.QSettings('Fontlab', 'fontlab7')
cwd = os.path.dirname(__file__)
def log(s):
main.doOutput(s + u'\n', True)
class ExportProfiles(object):
def __init__(self):
self.profiles = OrderedDict()
self._tempProfileNames = set([])
self._readAll()
def _uniqName(self, profileName):
if profileName in self.profiles:
return self._uniqName(profileName + u'1')
else:
return profileName
def _readAll(self):
settingsProfiles = settings.value(u'profiles')
for profileText in settingsProfiles:
profileName, profileContent = self._parse(profileText)
self.profiles[self._uniqName(profileName)] = profileContent
def _parse(self, profileText):
profileLines = profileText.splitlines()
profileName = profileLines[0].split(u' = ')[1].strip(u'"')
profileContent = OrderedDict()
for profileLine in profileLines[1:]:
profileKey, profileVal = profileLine.split(u' = ')
profileContent[profileKey] = profileVal
return profileName, profileContent
def _repr(self, profileName):
profileContent = self.profiles.get(profileName, None)
strProfile = u''
if profileContent:
strProfile += u'ProfileName = "%s"\n' % (profileName)
for profileKey, profileVal in profileContent.items():
strProfile += u'%s = %s\n' % (profileKey, profileVal)
return strProfile
def loadFile(self, profilePath):
with open(profilePath, 'r') as profileFile:
profileText = profileFile.read()
profileName, profileContent = self._parse(profileText)
profileName += u'D' # TODO
if not profileName in self.profiles.keys():
self._tempProfileNames.add(profileName)
self.profiles[profileName] = profileContent
self.update()
return profileName
def saveFile(self, profileName, profilePath):
profileText = self._repr(profileName)
if profileText:
with open(profilePath, 'w') as profileFile:
profileFile.write(profileText)
else:
log('Profile %s not found' % (profileName))
def remove(self, profileName):
if profileName in self.profiles.keys():
del self.profiles[profileName]
def cleanUp(self):
for profileName in self._tempProfileNames:
self.remove(profileName)
self._tempProfileNames = set([])
self.update()
def update(self):
listProfiles = []
for profileName in self.profiles.keys():
listProfiles.append(self._repr(profileName))
settings.setValue(u'profiles', tuple(listProfiles))
ec = ExportControl()
ec.load()
ec.store()
profiles = ExportProfiles()
class ExportFont(object):
def __init__(self, outBaseFolder=None):
if outBaseFolder:
self.outBaseFolder = outBaseFolder
else:
self.outBaseFolder = None
self.fontPath = None
self.fp = None
def openFont(self, fontPath):
main.loadFont(fontPath)
self.fp = ws.currentPackage
self.fg = CurrentFont()
self.fontPath = self.fp.path
self.subFolder = os.path.splitext(os.path.basename(self.fontPath))[0]
#log(self.fp, self.fg)
def exportFontDSUFO(self, outFolder=None):
if not outFolder:
outFolder = os.path.join(
self.outBaseFolder, self.subFolder, 'ds-ufo')
if not os.path.isdir(outFolder):
os.makedirs(outFolder)
ec = ExportControl()
ec.profileName = u'DesignSpace + UFO'
ec.destinationMode = pref.DestinationFolder
ec.conflictMode = pref.ConflictOverwrite
ec.contentMode = pref.ContentInstances
ec.destinationFolder = outFolder
ec.groupProfiles = False
ec.groupFamily = False
#log('EXPORT %s / %s' % (ec.profileName, type(ec.profileName)))
ws.exportFont(self.fp, ec)
def exportFontInstUFO(self, fontProfile=None, outFolder=None):
if not outFolder:
outFolder = os.path.join(
self.outBaseFolder, self.subFolder, 'ds-ufo', 'masters')
if not os.path.isdir(outFolder):
os.makedirs(outFolder)
ec = ExportControl()
ec.init()
ec.load()
ec.store()
if not fontProfile:
fontProfile = u'UFO Package'
ec.profileName = fontProfile
ec.destinationMode = pref.DestinationFolder
ec.conflictMode = pref.ConflictOverwrite
ec.contentMode = pref.ContentInstances
ec.destinationFolder = outFolder
ec.groupProfiles = False
ec.groupFamily = False
#log('EXPORT %s / %s' % (ec.profileName, type(ec.profileName)))
ws.exportFont(self.fp, ec)
class LatoFontLabExport(object):
def __init__(self, toolFolder=None):
if toolFolder:
self.toolFolder = toolFolder
else:
self.toolFolder = cwd
self.fontFolder = os.path.join(self.toolFolder, '..', 'sources')
self.outBaseFolder = os.path.join(self.toolFolder, '..', 'build')
self.fontSpecs = {}
self.baseFonts = [
'Lato3Upr',
'Lato3Ita'
]
self.main()
def getFontSpecs(self, fontFolder=None, baseFonts=None):
if fontFolder:
self.fontFolder = fontFolder
if baseFonts:
self.baseFonts = baseFonts
for baseFont in self.baseFonts:
fontPaths = tuple(glob.glob(
os.path.join(self.fontFolder, '%s*.vfj' % baseFont)
))
fontProfiles = tuple(glob.glob(
os.path.join(self.fontFolder, '%s*.fl_profile' % baseFont)
))
self.fontSpecs[fontPaths] = fontProfiles
if not len(self.fontSpecs.keys()):
self.fontFolder = QtGui.QFileDialog.getExistingDirectory(
None, 'Choose folder with VFJ files', self.fontFolder, QFileDialog.ShowDirsOnly)
if self.fontFolder:
fontPaths = tuple(glob.glob(
os.path.join(self.fontFolder, '*.vfj')))
fontProfiles = None
self.fontSpecs[fontPaths] = fontProfiles
if not len(self.fontSpecs.keys()):
log("No VFJ files found in folder!")
def main(self):
self.getFontSpecs()
for fontPaths, fontProfiles in self.fontSpecs.items():
for fontPath in fontPaths:
log("Exporting %s..." % (fontPath))
export = ExportFont(outBaseFolder=self.outBaseFolder)
export.openFont(fontPath)
#log("...as DesignSpace + UFO...")
# export.exportFontDSUFO()
if fontProfiles:
for fontProfilePath in fontProfiles:
fontProfile = profiles.loadFile(
fontProfilePath)
log("...as %s..." % fontProfile)
export.exportFontInstUFO(fontProfile=fontProfile)
# TODO profiles.cleanUp()
else:
exp.exportFontInstUFO()
if __name__ == "__main__":
ep = ExportProfiles()
profile = ep.loadFile(os.path.join(
cwd, '..', 'sources', 'Lato3Ita.fl_profile'))
# ep.saveFile(profile, os.path.join(
# cwd, '..', 'sources', 'Lato3Ita-OUT.fl_profile'))
ex = LatoFontLabExport()
# qapp.quit()
main.findChildren(QtGui.QAction, QtCore.QRegExp(
'Exit', Qt.Qt.CaseInsensitive))[0].trigger()