Skip to content

Commit 54a0dec

Browse files
committed
add scripting support
1 parent fb4c303 commit 54a0dec

File tree

6 files changed

+148
-17
lines changed

6 files changed

+148
-17
lines changed

source/lib/batch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ def settingsCallback(self, sender):
230230

231231
def generateCallback(self, sender):
232232
generateOptions = self.w.getItemValues()
233-
generateOptions["sourceUFOPaths"], designspaceDocuments = self.getAllUFOPaths()
234-
generateOptions["sourceDesignspacePaths"] = self.getAllDesignspacePaths()
233+
generateOptions["sourceUFOs"], designspaceDocuments = self.getAllUFOPaths()
234+
generateOptions["sourceDesignspaces"] = self.getAllDesignspacePaths()
235235

236-
if not generateOptions["sourceUFOPaths"] and not generateOptions["sourceDesignspacePaths"]:
236+
if not generateOptions["sourceUFOs"] and not generateOptions["sourceDesignspaces"]:
237237
# no fonts found in the source table
238238
return
239239

source/lib/batchCompileTools.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from batchGenerators.batchTools import Report, DummyProgress
2+
import batchGenerators
3+
import batchSettings
4+
5+
6+
def generateDesktopFonts(ufoPathsOrObjects, destinationRoot, format="ttf", decompose=True, removeOverlap=True, autohint=False, releaseMode=True, suffix=""):
7+
if not isinstance(ufoPathsOrObjects, list):
8+
ufoPathsOrObjects = [ufoPathsOrObjects]
9+
report = Report()
10+
generateOptions = dict(
11+
sourceUFOs=ufoPathsOrObjects,
12+
)
13+
settings = dict(batchSettings.defaultSettings)
14+
settings.update(
15+
dict(
16+
batchSettingExportInSubFolders=False,
17+
desktopFontsAutohint=autohint,
18+
desktopFontsDecompose=decompose,
19+
desktopFontsReleaseMode=releaseMode,
20+
desktopFontsRemoveOverlap=removeOverlap,
21+
desktopFontsSuffix=suffix
22+
)
23+
)
24+
if format == "ttf":
25+
generateOptions["desktopFontGenerate_TTF"] = True
26+
if format == "otf":
27+
generateOptions["desktopFontGenerate_OTF"] = True
28+
29+
batchGenerators.desktopFontsGenerator.build(
30+
root=destinationRoot,
31+
generateOptions=generateOptions,
32+
settings=settings,
33+
progress=DummyProgress(),
34+
report=report
35+
)
36+
return report.get()
37+
38+
39+
def generateWebFonts(ufoPathsOrObjects, destinationRoot, format="ttf", woff=False, decompose=True, removeOverlap=True, autohint=False, releaseMode=True, suffix="", html=False, htmlPreview=None):
40+
if not isinstance(ufoPathsOrObjects, list):
41+
ufoPathsOrObjects = [ufoPathsOrObjects]
42+
report = Report()
43+
generateOptions = dict(
44+
sourceUFOs=ufoPathsOrObjects,
45+
)
46+
settings = dict(batchSettings.defaultSettings)
47+
settings.update(
48+
dict(
49+
batchSettingExportInSubFolders=False,
50+
webFontsAutohint=autohint,
51+
webFontsDecompose=decompose,
52+
webFontsGenerateHTML=html,
53+
webFontsReleaseMode=releaseMode,
54+
webFontsRemoveOverlap=removeOverlap,
55+
webFontsSuffix=suffix
56+
)
57+
)
58+
if htmlPreview:
59+
settings["webFontsHtmlPreview"] = htmlPreview
60+
if format == "ttf" and woff:
61+
generateOptions["webFontGenerate_TTFWOFF2"] = True
62+
if format == "otf" and woff:
63+
generateOptions["webFontGenerate_OTFWOFF2"] = True
64+
if format == "ttf" and not woff:
65+
generateOptions["webFontGenerate_TTF"] = True
66+
if format == "otf" and not woff:
67+
generateOptions["webFontGenerate_OTF"] = True
68+
69+
batchGenerators.webFontsGenerator.build(
70+
root=destinationRoot,
71+
generateOptions=generateOptions,
72+
settings=settings,
73+
progress=DummyProgress(),
74+
report=report
75+
)
76+
return report.get()
77+
78+
79+
def generateVariableFonts(designspacePathsOrObjects, destinationRoot, format="ttf", woff=False, autohint=False, fitToExtremes=False, suffix=""):
80+
if not isinstance(designspacePathsOrObjects, list):
81+
designspacePathsOrObjects = [designspacePathsOrObjects]
82+
report = Report()
83+
generateOptions = dict(
84+
sourceDesignspaces=designspacePathsOrObjects,
85+
)
86+
settings = dict(batchSettings.defaultSettings)
87+
settings.update(
88+
dict(
89+
batchSettingExportInSubFolders=False,
90+
variableFontsAutohint=autohint,
91+
variableFontsInterpolateToFitAxesExtremes=fitToExtremes,
92+
variableFontsSuffix=suffix
93+
)
94+
)
95+
if format == "ttf" and woff:
96+
generateOptions["variableFontGenerate_TTFWOFF2"] = True
97+
if format == "otf" and woff:
98+
generateOptions["variableFontGenerate_OTFWOFF2"] = True
99+
if format == "ttf" and not woff:
100+
generateOptions["variableFontGenerate_TTF"] = True
101+
if format == "otf" and not woff:
102+
generateOptions["variableFontGenerate_OTF"] = True
103+
104+
batchGenerators.variableFontsGenerator.build(
105+
root=destinationRoot,
106+
generateOptions=generateOptions,
107+
settings=settings,
108+
progress=DummyProgress(),
109+
report=report
110+
)
111+
return report.get()

source/lib/batchGenerators/batchTools.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ def _instantiateFont(self, path):
1717
return internalFontClasses.createFontObject(path)
1818

1919

20-
def loadFonts(ufoPaths):
20+
def loadFonts(sourceUFOs):
2121
fonts = []
22-
for path in ufoPaths:
23-
font = RFont(path, document=False, showInterface=False)
22+
for sourceUFO in sourceUFOs:
23+
if isinstance(sourceUFO, str):
24+
font = RFont(sourceUFO, document=False, showInterface=False)
25+
else:
26+
font = sourceUFO
2427
# check font info
2528
requiredFontInfo = dict(descender=-250, xHeight=500, ascender=750, capHeight=750, unitsPerEm=1000)
2629
for attr, value in requiredFontInfo.items():
@@ -104,6 +107,19 @@ def get(self):
104107
return "\n".join(self._data)
105108

106109

110+
class DummyProgress:
111+
112+
def __dummy(self, *args, **kwargs):
113+
pass
114+
115+
setText = __dummy
116+
update = __dummy
117+
increment = __dummy
118+
setMaxValue = __dummy
119+
setTickCount = __dummy
120+
close = __dummy
121+
122+
107123
blankFontCss = """<style>
108124
@font-face {
109125
font-family: AdobeBlank;
@@ -166,7 +182,7 @@ def __bool__(self):
166182

167183

168184
def generatePaths(
169-
ufoPaths,
185+
sourceUFOs,
170186
binaryFormats,
171187
decompose,
172188
removeOverlap,
@@ -179,7 +195,7 @@ def generatePaths(
179195
report,
180196
progress
181197
):
182-
fonts = loadFonts(ufoPaths)
198+
fonts = loadFonts(sourceUFOs)
183199

184200
if decompose:
185201
report.writeTitle("Decompose:")
@@ -215,11 +231,12 @@ def generatePaths(
215231
report.indent()
216232

217233
for index, font in enumerate(fonts):
234+
fontPath = font.path
218235
progress.increment()
219-
report.writeTitle((os.path.basename(ufoPaths[index])))
236+
report.writeTitle((os.path.basename(fontPath)))
220237
report.indent()
221238
report.newLine()
222-
report.write(f"source: {ufoPaths[index]}")
239+
report.write(f"source: {fontPath}")
223240
report.newLine()
224241
for binaryFormat, postProcessCallback in binaryFormats:
225242
binaryExtention = binaryFormat.split("-")[0]
@@ -229,8 +246,8 @@ def generatePaths(
229246
familyName = familyName.replace(" ", "")
230247
styleName = font.info.styleName or f"styleName-{index}"
231248
styleName = styleName.replace(" ", "")
232-
if keepFileNames:
233-
fileName = os.path.basename(ufoPaths[index])
249+
if keepFileNames and fontPath is not None:
250+
fileName = os.path.basename(fontPath)
234251
fileName, _ = os.path.splitext(fileName)
235252
fileName = f"{fileName}{suffix}.{binaryExtention}"
236253
else:

source/lib/batchGenerators/desktopFontsGenerator/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def build(root, generateOptions, settings, progress, report):
2525
removeTree(desktopFontsRoot)
2626

2727
generatePaths(
28-
ufoPaths=generateOptions["sourceUFOPaths"],
28+
sourceUFOs=generateOptions["sourceUFOs"],
2929
binaryFormats=binaryFormats,
3030
decompose=settings["desktopFontsDecompose"],
3131
removeOverlap=settings["desktopFontsRemoveOverlap"],

source/lib/batchGenerators/variableFontsGenerator/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,11 @@ def build(root, generateOptions, settings, progress, report):
477477
variableFontsRoot = os.path.join(root, "Variable")
478478
removeTree(variableFontsRoot)
479479

480-
for sourceDesignspacePath in generateOptions["sourceDesignspacePaths"]:
481-
operator = BatchEditorOperator(sourceDesignspacePath)
480+
for sourceDesignspace in generateOptions["sourceDesignspaces"]:
481+
if isinstance(sourceDesignspace, str):
482+
operator = BatchEditorOperator(sourceDesignspace)
483+
else:
484+
operator = sourceDesignspace
482485
# loop over all interpolable operators based on the given variable fonts
483486
for name, interpolableOperator in operator.getInterpolableUFOOperators(useVariableFonts=True):
484487
for binaryFormat, postProcessCallback in binaryFormats:

source/lib/batchGenerators/webFontsGenerator/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ def build(root, generateOptions, settings, progress, report):
107107
removeTree(webFontsRoot)
108108

109109
report.writeTitle("Batch Generated Web Fonts:")
110-
progress.update("Collecting Data...")
110+
progress.setText("Collecting Data...")
111111

112112
generatePaths(
113-
ufoPaths=generateOptions["sourceUFOPaths"],
113+
sourceUFOs=generateOptions["sourceUFOs"],
114114
binaryFormats=binaryFormats,
115115
decompose=settings["webFontsDecompose"],
116116
removeOverlap=settings["webFontsRemoveOverlap"],

0 commit comments

Comments
 (0)