Debian Codegraph @

sources/build_font

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 Helpscip-shell shell fonts-elstob 3.003+ds-1 Help().()
4 {
5 echoscip-shell system . . echo. ""
6 echoscip-shell system . . echo. "Usage: build_font [-t | -o] [-i] [-a] [-w] [-c] [-f FAMILYNAME] [-s NUM] [-p NUM] file.glyphs"
7 echoscip-shell system . . echo. ""
8 echoscip-shell system . . echo. "A script for building variable and static versions of the Elstob font."
9 echoscip-shell system . . echo. "It does several things that are specific to Elstob and probably unsuitable"
10 echoscip-shell system . . echo. "for other fonts. Either run this script in the sources directory or copy"
11 echoscip-shell system . . echo. "all files from the sources directory to a build directory and run it there."
12 echoscip-shell system . . echo. ""
13 echoscip-shell system . . echo. " -t Builds static TrueType fonts instead of a variable font"
14 echoscip-shell system . . echo. " -o Builds static CFF fonts instead of a variable font"
15 echoscip-shell system . . echo. " -a Autohint the variable font with ttfautohint-vf"
16 echoscip-shell system . . echo. " -n Do not hint TrueType fonts"
17 echoscip-shell system . . echo. " -i Generate a shorter collection of instances"
18 echoscip-shell system . . echo. " -w Create a woff2 version of the variable font"
19 echoscip-shell system . . echo. " -c Do not clean up temporary files and directories (for debugging)"
20 echoscip-shell system . . echo. " -f FAMILYNAME Supply a new family name"
21 echoscip-shell system . . echo. " -s NUM (0-15) Set italic slant for static fonts (15 = more upright)"
22 echoscip-shell system . . echo. " -p NUM (0-1) Set width of space for static fonts (1 = wider)"
23 echoscip-shell system . . echo. " -d PATH Directory containing source files (default is ./)"
24 echoscip-shell system . . echo. " -z PATH Directory in which to write new fonts (default is ./dist/)"
25 echoscip-shell system . . echo. " -b PATH Directory for temporary files (default is ./build/)"
26 echoscip-shell system . . echo. ""
27 }
28
29 Usagescip-shell shell fonts-elstob 3.003+ds-1 Usage().()
30 {
31 echoscip-shell system . . echo.
32 echoscip-shell system . . echo. "Usage: build_font [-t | -o] [-i] [-a] [-w] [-c] [-f FAMILYNAME] [-s NUM] [-p NUM] [-d PATH] [-z PATH] [-b PATH] file.glyphs"
33 echoscip-shell system . . echo. " Type \"build_font -h\" for a longer help message."
34 echoscip-shell system . . echo.
35 }
36
37 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp(). () {
38 if [scip-shell system . . `[`. ! -z "$tmp_d" ]
39 then
40 echoscip-shell system . . echo. "Deleting temporary file ${tmp_d}."
41 rmscip-shell system . . rm. -fr $tmp_d
42 fi
43 }
44
45 if [scip-shell system . . `[`. $# -lt 1 ]
46 then
47 Usagescip-shell shell fonts-elstob 3.003+ds-1 Usage().
48 exit 1
49 fi
50
51 HOMEDIR=`pwdscip-shell system . . pwd.`
52
53 SOURCEDIR=$HOMEDIR
54
55 DESTDIR=$HOMEDIR/dist
56
57 BUILDDIR=$HOMEDIR/build
58
59 outputType="v"
60
61 glyphsfile="nofile"
62
63 fontname="nofont"
64
65 familyName="Elstob"
66
67 instanceScript="del_instances_vf.xsl"
68
69 hintoption="-a"
70
71 autohint=0
72
73 createwoff=0
74
75 italslant=6
76
77 spacing=0
78
79 while getopts "htoanwcif:s:p:d:z:b:" options; do
80 case "${options}" in
81 h)
82 Helpscip-shell shell fonts-elstob 3.003+ds-1 Help().
83 exit 0
84 ;;
85 t)
86 outputType="s"
87 instanceScript="del_instances.xsl"
88 ;;
89 o)
90 outputType="o"
91 instanceScript="del_instances.xsl"
92 ;;
93 a)
94 autohint=1
95 ;;
96 n)
97 autohint=-1
98 hintoption="-A"
99 ;;
100 w)
101 createwoff=1
102 ;;
103 i)
104 instanceScript="del_instances_shorter.xsl"
105 ;;
106 f)
107 familyName=${OPTARG}
108 ;;
109 s)
110 italslant=${OPTARG}
111 ;;
112 p)
113 spacing=${OPTARG}
114 ;;
115 d)
116 SOURCEDIR=${OPTARG}
117 ;;
118 z)
119 DESTDIR=${OPTARG}
120 ;;
121 b)
122 BUILDDIR=${OPTARG}
123 ;;
124 esac
125 done
126
127 if [scip-shell system . . `[`. $(( $# - $OPTIND )) -ne 0 ]
128 then
129 echoscip-shell system . . echo. "Need name of Glyphs file"
130 exit 1
131 fi
132 glyphsfile=${@:$OPTIND:1}
133 if [scip-shell system . . `[`. ${glyphsfile##*.} != "glyphs" ]
134 then
135 echoscip-shell system . . echo. "${glyphsfile} appears not to be a Glyphs file"
136 exit 1
137 fi
138
139 ff=$(basenamescip-shell system . . basename. -- $glyphsfile)
140 fontname=${ff%.*}
141
142 # If the arguments somehow haven't worked
143
144 if [scip-shell system . . `[`. $glyphsfile == "nofile" ] || [scip-shell system . . `[`. $fontname == "nofont" ]
145 then
146 echoscip-shell system . . echo. "Could not derive the filename or fontname."
147 exit 1
148 fi
149
150 rmscip-shell system . . rm. -rf $BUILDDIR
151 mkdirscip-shell system . . mkdir. -p $BUILDDIR
152 [scip-shell system . . `[`. ! -d $DESTDIR ] && mkdirscip-shell system . . mkdir. $DESTDIR
153 cd $BUILDDIR
154
155 glyphsfile=$SOURCEDIR/$glyphsfile
156 instanceScript=$SOURCEDIR/$instanceScript
157
158 # If a new family name is specified, make a copy of the Glyphs file (with the
159 # new familyName) in a temp directory and use that copy to build the font.
160 # Remove the temp directory at the end of the program.
161
162 tmp_d=""
163 if [[ $familyName != "Elstob" ]]
164 then
165 tmp_d=$(mktempscip-shell system . . mktemp. -d)
166 tmp_f=$tmp_d/$ff
167 sedscip-shell system . . sed. -e "s/familyName = Elstob;/familyName = ${familyName};/" $glyphsfile > $tmp_f
168 glyphsfile=$tmp_f
169 fi
170
171 # Determine whether this is the italic or the roman font
172
173 if [[ $fontname == *"Italic"* ]]
174 then
175 fonttype="italic"
176 else
177 fonttype="roman"
178 fi
179
180 # Build the designspace filename
181
182 des=$BUILDDIR/master_ufo/$fontname.designspace
183
184 echoscip-shell system . . echo. "Generating master UFOs and designspace files"
185
186 glyphs2ufo --propagate-anchors --generate-GDEF --no-store-editor-state -m master_ufo -d $des $glyphsfile
187 if [scip-shell system . . `[`. $? -ne 0 ]
188 then
189 echoscip-shell system . . echo. "Failed to generate UFO and designspace"
190 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
191 exit 1
192 fi
193
194 # Need Gnu sed. The default (BSD) sed on the Mac won't cut it. There use
195 # Homebrew to get gsed.
196
197 sedcmd="sed"
198 if command -v gsed &> /dev/nullscip-shell filesystem . . `/dev/null`.
199 then
200 sedcmd="gsed"
201 fi
202
203 familyname=`$sedcmd -n '1,/<source.*familyname/s/.*familyname="\([^"]*\)"[^\n]*/\1/p' $des`
204 echoscip-shell system . . echo. "Building font(s) with family name \"${familyname}\""
205
206 # PERFORM EDITS ON DESIGNSPACE AND UFO FILES.
207
208 # Every glyph with a "us" anchor also needs a "_us" anchor or fontmake (glyphsLib,
209 # I think) will mess up the anchors for that glyph. But there is no requirement
210 # that anchors must be paired this way, and it's easy to imagine the extraneous
211 # anchors causing problems (though I don't know that it has). As it is easy to
212 # strip the extraneous anchors out of the UFO, it's best that we do so.
213
214 echoscip-shell system . . echo. "Removing extraneous anchors"
215
216 for u in `$sedcmd -n '/<source.*filename/s/.*filename="\([^"]*\)"[^\n]*/\1/pscip-shell filesystem . . `/<source.*filename/s/.*filename="\([^"]*\)"[^\n]*/\1/p`.' $des`
217 do
218 for fff in `grepscip-shell system . . grep. -l "anchor.*\"_us\"" master_ufo/$u/glyphs/*.glif`
219 do
220 if [[ ! $fff == *"usabovecomb.glif"* ]]
221 then
222 xsltproc -o $fff $SOURCEDIR/del_usanchor.xsl $fff
223 if [scip-shell system . . `[`. $? -ne 0 ]
224 then
225 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
226 exit 1
227 fi
228 fi
229 done
230 done
231
232 # The Glyphs file has more instances than needed--some only there to hold down
233 # the corners of the design space. There are standard collections of instances
234 # for the variable font, the desktop set (ElstobD), and the web static set.
235
236 if [scip-shell system . . `[`. $fonttype == "italic" ]
237 then
238 sn="Grade Italic"
239 else
240 sn="Grade"
241 fi
242 grepscip-shell system . . grep. -q "stylename=\"${sn}\"" $des
243 if [scip-shell system . . `[`. $? -eq 0 ]
244 then
245 echoscip-shell system . . echo. "Removing unnneeded instances"
246 xsltproc -o $des $instanceScript $des
247 fi
248
249 if [scip-shell system . . `[`. $outputType != "v" ];
250 then
251 # fix-spacing.xsl sets the default spacing via the Spacing axis. Change
252 # the number "sp" (use anything between 0 and 1) to change the spacing.
253 # Now use -p option for this (static fonts only).
254
255 xsltproc --param sp $spacing -o $des $SOURCEDIR/fix-spacing.xsl $des
256 if [scip-shell system . . `[`. $? -ne 0 ]
257 then
258 echoscip-shell system . . echo. "fix-spacing script failed"
259 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
260 exit 1
261 fi
262
263 # Set the slant of the static Italic fonts. Change the number "sl"
264 # (using anything between 0 and 15) to change the slant.
265 # Now use -s option for this (static fonts only).
266
267 if [scip-shell system . . `[`. $fonttype == "italic" ]
268 then
269 xsltproc --param sl $italslant -o $des $SOURCEDIR/fix-italic-slant.xsl $des
270 if [scip-shell system . . `[`. $? -ne 0 ]
271 then
272 echoscip-shell system . . echo. "fix-italic-slant script failed"
273 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
274 exit 1
275 fi
276 fi
277 fi
278
279 # RUN FONTMAKE TO BUILD THE FONTS (s = static TrueType; o = static CFF; v = variable TrueType).
280
281 if [scip-shell system . . `[`. $outputType = "s" ];
282 then
283
284 echoscip-shell system . . echo. "Running fontmake to build static TrueType instances"
285
286 fontmake -o ttf $hintoption -i -m $des
287 if [scip-shell system . . `[`. $? -ne 0 ]
288 then
289 echoscip-shell system . . echo. "fontmake failed"
290 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
291 exit 1
292 fi
293
294 # Add a dummny dsig table in the fonts we've just built.
295
296 #cd instance_ttf
297 #if [ $fonttype == "italic" ]
298 #then
299 # echo "Adding a DSIG table to the italic font"
300 # find . -maxdepth 1 -name "${familyname}*Italic.ttf" -exec python $SOURCEDIR/add_dsig.py {} \;
301 #else
302 # echo "Adding a DSIG table to the roman font"
303 # find . -maxdepth 1 -name "${familyname}*.otf" -not -name "*Italic.ttf" -exec python $SOURCEDIR/add_dsig.py {} \;
304 #fi
305 cpscip-shell system . . cp. $BUILDDIR/autohinted/instance_ttf/Elstob*.ttf $DESTDIR
306
307 # Next CFF OpenType fonts, largely duplicating the moves for static TrueType.
308
309 elif [scip-shell system . . `[`. $outputType == "o" ]
310 then
311
312 echoscip-shell system . . echo. "Running fontmake to build static CFF instances"
313
314 xsltproc -o $des $SOURCEDIR/fix-spacing.xsl $des
315 fontmake -o otf -i -m $des
316 if [scip-shell system . . `[`. $? -ne 0 ]
317 then
318 echoscip-shell system . . echo. "fontmake failed"
319 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
320 exit 1
321 fi
322 #cd instance_otf
323 #if [ $fonttype == "italic" ]
324 #then
325 # echo "Adding DSIG table and hinting the italic font"
326 # find . -maxdepth 1 -name "${familyname}*Italic.otf" -exec python $SOURCEDIR/add_dsig.py {} \; -exec psautohint {} \;
327 #else
328 # echo "Adding DSIG table and hinting the roman font"
329 # find . -maxdepth 1 -name "${familyname}*.otf" -not -name "*Italic.otf" -exec python $SOURCEDIR/add_dsig.py {} \; -exec psautohint {} \;
330 #fi
331 cpscip-shell system . . cp. $BUILDDIR/instance_otf/Elstob*.otf $DESTDIR
332 # Finally a variable font. We only build the TrueType flavor, since that seems
333 # to work everywhere.
334
335 else
336
337 echoscip-shell system . . echo. "Running fontmake to build variable font"
338
339 fontmake -o variable -m $des
340 if [scip-shell system . . `[`. $? -ne 0 ]
341 then
342 echoscip-shell system . . echo. "fontmake failed"
343 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
344 exit 1
345 fi
346
347 cpscip-shell system . . cp. "variable_ttf/${fontname}-VF.ttf" .
348
349 echoscip-shell system . . echo. "Adding dsig table"
350
351 python $SOURCEDIR/add_dsig.py "${fontname}-VF.ttf"
352
353 echoscip-shell system . . echo. "Building STAT table and fixing name table"
354
355 python $SOURCEDIR/mkstat.py $fonttype
356 if [scip-shell system . . `[`. $? -ne 0 ]
357 then
358 echoscip-shell system . . echo. "Failed to build STAT table"
359 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
360 exit 1
361 fi
362
363 # Hinting: if autohinting requested, check for existence of ttfautohint-vf and
364 # run it if available.
365 # If autohinting not requested, run Xgridfit.
366 # If hinting suppressed or either of these methods fails, user will be left
367 # with unhinted font.
368
369 if [scip-shell system . . `[`. $autohint == 1 ]
370 then
371 if command -v ttfautohint-vf &> /dev/nullscip-shell filesystem . . `/dev/null`.
372 then
373 echoscip-shell system . . echo. "Running ttfautohint-vf"
374 ttfautohint-vf "${fontname}-VF-withSTAT.ttf" tmp-font.ttf
375 # Fix a flag in head table, as Google Fonts prescribes; then clean up.
376 gftools-fix-hinting.py tmp-font.ttf
377 cpscip-shell system . . cp. tmp-font.ttf.fix "${fontname}.ttf"
378 else
379 echoscip-shell system . . echo. "ttfautohint-vf (the variable-font-enabled version of ttfautohint)"
380 echoscip-shell system . . echo. "is required to auto-hint a variable font."
381 fi
382 elif [scip-shell system . . `[`. $autohint -ne -1 ]
383 then
384 echoscip-shell system . . echo. "Running Xgridfit"
385 xgridfit -q -i "${fontname}-VF-withSTAT.ttf" -o "${fontname}.ttf" $SOURCEDIR/xgf/$fontname.yaml
386 if [scip-shell system . . `[`. $? -ne 0 ]
387 then
388 echoscip-shell system . . echo. "Failed to hint with Xgridfit."
389 cpscip-shell system . . cp. "${fontname}-VF-withSTAT.ttf" "${fontname}.ttf"
390 fi
391 else
392 echoscip-shell system . . echo. "Skipping hinting"
393 cpscip-shell system . . cp. "${fontname}-VF-withSTAT.ttf" "${fontname}.ttf"
394 fi
395 if [scip-shell system . . `[`. $createwoff == 1 ]
396 then
397 echoscip-shell system . . echo. "Creating woff2 version"
398 woff2_compress "${fontname}.ttf"
399 fi
400 cpscip-shell system . . cp. $fontname.ttf $DESTDIR/
401
402 fi
403
404 delete_tmpscip-shell shell fonts-elstob 3.003+ds-1 delete_tmp().
405
406 cd $HOMEDIR