Debian Codegraph @

sources/build_font

raw symbols SCIP
1 #!/bin/bash
2
3 Help()
4 {
5 echo ""
6 echo "Usage: build_font [-t | -o] [-i] [-a] [-w] [-c] [-f FAMILYNAME] [-s NUM] [-p NUM] file.glyphs"
7 echo ""
8 echo "A script for building variable and static versions of the Elstob font."
9 echo "It does several things that are specific to Elstob and probably unsuitable"
10 echo "for other fonts. Either run this script in the sources directory or copy"
11 echo "all files from the sources directory to a build directory and run it there."
12 echo ""
13 echo " -t Builds static TrueType fonts instead of a variable font"
14 echo " -o Builds static CFF fonts instead of a variable font"
15 echo " -a Autohint the variable font with ttfautohint-vf"
16 echo " -n Do not hint TrueType fonts"
17 echo " -i Generate a shorter collection of instances"
18 echo " -w Create a woff2 version of the variable font"
19 echo " -c Do not clean up temporary files and directories (for debugging)"
20 echo " -f FAMILYNAME Supply a new family name"
21 echo " -s NUM (0-15) Set italic slant for static fonts (15 = more upright)"
22 echo " -p NUM (0-1) Set width of space for static fonts (1 = wider)"
23 echo " -d PATH Directory containing source files (default is ./)"
24 echo " -z PATH Directory in which to write new fonts (default is ./dist/)"
25 echo " -b PATH Directory for temporary files (default is ./build/)"
26 echo ""
27 }
28
29 Usage()
30 {
31 echo
32 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 echo " Type \"build_font -h\" for a longer help message."
34 echo
35 }
36
37 delete_tmp () {
38 if [ ! -z "$tmp_d" ]
39 then
40 echo "Deleting temporary file ${tmp_d}."
41 rm -fr $tmp_d
42 fi
43 }
44
45 if [ $# -lt 1 ]
46 then
47 Usage
48 exit 1
49 fi
50
51 HOMEDIR=`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 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 [ $(( $# - $OPTIND )) -ne 0 ]
128 then
129 echo "Need name of Glyphs file"
130 exit 1
131 fi
132 glyphsfile=${@:$OPTIND:1}
133 if [ ${glyphsfile##*.} != "glyphs" ]
134 then
135 echo "${glyphsfile} appears not to be a Glyphs file"
136 exit 1
137 fi
138
139 ff=$(basename -- $glyphsfile)
140 fontname=${ff%.*}
141
142 # If the arguments somehow haven't worked
143
144 if [ $glyphsfile == "nofile" ] || [ $fontname == "nofont" ]
145 then
146 echo "Could not derive the filename or fontname."
147 exit 1
148 fi
149
150 rm -rf $BUILDDIR
151 mkdir -p $BUILDDIR
152 [ ! -d $DESTDIR ] && 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=$(mktemp -d)
166 tmp_f=$tmp_d/$ff
167 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 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 [ $? -ne 0 ]
188 then
189 echo "Failed to generate UFO and designspace"
190 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/null
199 then
200 sedcmd="gsed"
201 fi
202
203 familyname=`$sedcmd -n '1,/<source.*familyname/s/.*familyname="\([^"]*\)"[^\n]*/\1/p' $des`
204 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 echo "Removing extraneous anchors"
215
216 for u in `$sedcmd -n '/<source.*filename/s/.*filename="\([^"]*\)"[^\n]*/\1/p' $des`
217 do
218 for fff in `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 [ $? -ne 0 ]
224 then
225 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 [ $fonttype == "italic" ]
237 then
238 sn="Grade Italic"
239 else
240 sn="Grade"
241 fi
242 grep -q "stylename=\"${sn}\"" $des
243 if [ $? -eq 0 ]
244 then
245 echo "Removing unnneeded instances"
246 xsltproc -o $des $instanceScript $des
247 fi
248
249 if [ $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 [ $? -ne 0 ]
257 then
258 echo "fix-spacing script failed"
259 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 [ $fonttype == "italic" ]
268 then
269 xsltproc --param sl $italslant -o $des $SOURCEDIR/fix-italic-slant.xsl $des
270 if [ $? -ne 0 ]
271 then
272 echo "fix-italic-slant script failed"
273 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 [ $outputType = "s" ];
282 then
283
284 echo "Running fontmake to build static TrueType instances"
285
286 fontmake -o ttf $hintoption -i -m $des
287 if [ $? -ne 0 ]
288 then
289 echo "fontmake failed"
290 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 cp $BUILDDIR/autohinted/instance_ttf/Elstob*.ttf $DESTDIR
306
307 # Next CFF OpenType fonts, largely duplicating the moves for static TrueType.
308
309 elif [ $outputType == "o" ]
310 then
311
312 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 [ $? -ne 0 ]
317 then
318 echo "fontmake failed"
319 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 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 echo "Running fontmake to build variable font"
338
339 fontmake -o variable -m $des
340 if [ $? -ne 0 ]
341 then
342 echo "fontmake failed"
343 delete_tmp
344 exit 1
345 fi
346
347 cp "variable_ttf/${fontname}-VF.ttf" .
348
349 echo "Adding dsig table"
350
351 python $SOURCEDIR/add_dsig.py "${fontname}-VF.ttf"
352
353 echo "Building STAT table and fixing name table"
354
355 python $SOURCEDIR/mkstat.py $fonttype
356 if [ $? -ne 0 ]
357 then
358 echo "Failed to build STAT table"
359 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 [ $autohint == 1 ]
370 then
371 if command -v ttfautohint-vf &> /dev/null
372 then
373 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 cp tmp-font.ttf.fix "${fontname}.ttf"
378 else
379 echo "ttfautohint-vf (the variable-font-enabled version of ttfautohint)"
380 echo "is required to auto-hint a variable font."
381 fi
382 elif [ $autohint -ne -1 ]
383 then
384 echo "Running Xgridfit"
385 xgridfit -q -i "${fontname}-VF-withSTAT.ttf" -o "${fontname}.ttf" $SOURCEDIR/xgf/$fontname.yaml
386 if [ $? -ne 0 ]
387 then
388 echo "Failed to hint with Xgridfit."
389 cp "${fontname}-VF-withSTAT.ttf" "${fontname}.ttf"
390 fi
391 else
392 echo "Skipping hinting"
393 cp "${fontname}-VF-withSTAT.ttf" "${fontname}.ttf"
394 fi
395 if [ $createwoff == 1 ]
396 then
397 echo "Creating woff2 version"
398 woff2_compress "${fontname}.ttf"
399 fi
400 cp $fontname.ttf $DESTDIR/
401
402 fi
403
404 delete_tmp
405
406 cd $HOMEDIR