#!/bin/bash Help() { echo "" echo "Usage: build_font [-t | -o] [-i] [-a] [-w] [-c] [-f FAMILYNAME] [-s NUM] [-p NUM] file.glyphs" echo "" echo "A script for building variable and static versions of the Elstob font." echo "It does several things that are specific to Elstob and probably unsuitable" echo "for other fonts. Either run this script in the sources directory or copy" echo "all files from the sources directory to a build directory and run it there." echo "" echo " -t Builds static TrueType fonts instead of a variable font" echo " -o Builds static CFF fonts instead of a variable font" echo " -a Autohint the variable font with ttfautohint-vf" echo " -n Do not hint TrueType fonts" echo " -i Generate a shorter collection of instances" echo " -w Create a woff2 version of the variable font" echo " -c Do not clean up temporary files and directories (for debugging)" echo " -f FAMILYNAME Supply a new family name" echo " -s NUM (0-15) Set italic slant for static fonts (15 = more upright)" echo " -p NUM (0-1) Set width of space for static fonts (1 = wider)" echo " -d PATH Directory containing source files (default is ./)" echo " -z PATH Directory in which to write new fonts (default is ./dist/)" echo " -b PATH Directory for temporary files (default is ./build/)" echo "" } Usage() { echo echo "Usage: build_font [-t | -o] [-i] [-a] [-w] [-c] [-f FAMILYNAME] [-s NUM] [-p NUM] [-d PATH] [-z PATH] [-b PATH] file.glyphs" echo " Type \"build_font -h\" for a longer help message." echo } delete_tmp () { if [ ! -z "$tmp_d" ] then echo "Deleting temporary file ${tmp_d}." rm -fr $tmp_d fi } if [ $# -lt 1 ] then Usage exit 1 fi HOMEDIR=`pwd` SOURCEDIR=$HOMEDIR DESTDIR=$HOMEDIR/dist BUILDDIR=$HOMEDIR/build outputType="v" glyphsfile="nofile" fontname="nofont" familyName="Elstob" instanceScript="del_instances_vf.xsl" hintoption="-a" autohint=0 createwoff=0 italslant=6 spacing=0 while getopts "htoanwcif:s:p:d:z:b:" options; do case "${options}" in h) Help exit 0 ;; t) outputType="s" instanceScript="del_instances.xsl" ;; o) outputType="o" instanceScript="del_instances.xsl" ;; a) autohint=1 ;; n) autohint=-1 hintoption="-A" ;; w) createwoff=1 ;; i) instanceScript="del_instances_shorter.xsl" ;; f) familyName=${OPTARG} ;; s) italslant=${OPTARG} ;; p) spacing=${OPTARG} ;; d) SOURCEDIR=${OPTARG} ;; z) DESTDIR=${OPTARG} ;; b) BUILDDIR=${OPTARG} ;; esac done if [ $(( $# - $OPTIND )) -ne 0 ] then echo "Need name of Glyphs file" exit 1 fi glyphsfile=${@:$OPTIND:1} if [ ${glyphsfile##*.} != "glyphs" ] then echo "${glyphsfile} appears not to be a Glyphs file" exit 1 fi ff=$(basename -- $glyphsfile) fontname=${ff%.*} # If the arguments somehow haven't worked if [ $glyphsfile == "nofile" ] || [ $fontname == "nofont" ] then echo "Could not derive the filename or fontname." exit 1 fi rm -rf $BUILDDIR mkdir -p $BUILDDIR [ ! -d $DESTDIR ] && mkdir $DESTDIR cd $BUILDDIR glyphsfile=$SOURCEDIR/$glyphsfile instanceScript=$SOURCEDIR/$instanceScript # If a new family name is specified, make a copy of the Glyphs file (with the # new familyName) in a temp directory and use that copy to build the font. # Remove the temp directory at the end of the program. tmp_d="" if [[ $familyName != "Elstob" ]] then tmp_d=$(mktemp -d) tmp_f=$tmp_d/$ff sed -e "s/familyName = Elstob;/familyName = ${familyName};/" $glyphsfile > $tmp_f glyphsfile=$tmp_f fi # Determine whether this is the italic or the roman font if [[ $fontname == *"Italic"* ]] then fonttype="italic" else fonttype="roman" fi # Build the designspace filename des=$BUILDDIR/master_ufo/$fontname.designspace echo "Generating master UFOs and designspace files" glyphs2ufo --propagate-anchors --generate-GDEF --no-store-editor-state -m master_ufo -d $des $glyphsfile if [ $? -ne 0 ] then echo "Failed to generate UFO and designspace" delete_tmp exit 1 fi # Need Gnu sed. The default (BSD) sed on the Mac won't cut it. There use # Homebrew to get gsed. sedcmd="sed" if command -v gsed &> /dev/null then sedcmd="gsed" fi familyname=`$sedcmd -n '1,/ /dev/null then echo "Running ttfautohint-vf" ttfautohint-vf "${fontname}-VF-withSTAT.ttf" tmp-font.ttf # Fix a flag in head table, as Google Fonts prescribes; then clean up. gftools-fix-hinting.py tmp-font.ttf cp tmp-font.ttf.fix "${fontname}.ttf" else echo "ttfautohint-vf (the variable-font-enabled version of ttfautohint)" echo "is required to auto-hint a variable font." fi elif [ $autohint -ne -1 ] then echo "Running Xgridfit" xgridfit -q -i "${fontname}-VF-withSTAT.ttf" -o "${fontname}.ttf" $SOURCEDIR/xgf/$fontname.yaml if [ $? -ne 0 ] then echo "Failed to hint with Xgridfit." cp "${fontname}-VF-withSTAT.ttf" "${fontname}.ttf" fi else echo "Skipping hinting" cp "${fontname}-VF-withSTAT.ttf" "${fontname}.ttf" fi if [ $createwoff == 1 ] then echo "Creating woff2 version" woff2_compress "${fontname}.ttf" fi cp $fontname.ttf $DESTDIR/ fi delete_tmp cd $HOMEDIR