build-subsets.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #!/bin/sh
  2. # //////////////////////////////////////////////////////////////////////
  3. #
  4. # build-subsets.sh
  5. # A shell script that builds the Hack web font subsets from UFO source
  6. # Copyright 2018 Christopher Simpkins
  7. # MIT License
  8. #
  9. # Usage: ./build-subsets.sh (--system)
  10. # Arguments:
  11. # --system (optional) - build with system installed versions
  12. # of build dependencies
  13. #
  14. # //////////////////////////////////////////////////////////////////////
  15. # set SOURCE_DATE_EPOCH to git commit date/time for reproducible builds
  16. export SOURCE_DATE_EPOCH=$(git show -s --format=%ct HEAD)
  17. # default build tooling definitions
  18. TTFAH="$HOME/ttfautohint-build/local/bin/ttfautohint"
  19. FONTMAKE="pipenv run fontmake"
  20. PYTHON="pipenv run python"
  21. # The sfnt2woff-zopfli build directory.
  22. SFNTWOFF_BUILD="$HOME/sfnt2woff-zopfli-build"
  23. # sfnt2woff-zopfli version
  24. SFNTWOFF_VERSION="1.1.0"
  25. SFNTWOFF="sfnt2woff-zopfli-$SFNTWOFF_VERSION"
  26. # Path to sfnt2woff-zopfli executable
  27. SFNTWOFF_BIN="$SFNTWOFF_BUILD/$SFNTWOFF/sfnt2woff-zopfli"
  28. ZOPFLI_ITERATIONS="3"
  29. # The woff2 git clone directory.
  30. WOFF2_BUILD="$HOME"
  31. # woff2 executable path
  32. WOFF2_BIN="$WOFF2_BUILD/woff2/woff2_compress"
  33. # temporary source directory for subset source files
  34. TEMP_SOURCE="source/temp"
  35. # The font build directory paths and file paths for the woff builds
  36. TTF_BUILD="master_ttf"
  37. REGULAR_TTF="Hack-Regular.ttf"
  38. REGULAR_WOFF_PRE="Hack-Regular.woff"
  39. REGULAR_WOFF="hack-regular-subset.woff"
  40. REGULAR_WOFF2_PRE="Hack-Regular.woff2"
  41. REGULAR_WOFF2="hack-regular-subset.woff2"
  42. BOLD_TTF="Hack-Bold.ttf"
  43. BOLD_WOFF_PRE="Hack-Bold.woff"
  44. BOLD_WOFF="hack-bold-subset.woff"
  45. BOLD_WOFF2_PRE="Hack-Bold.woff2"
  46. BOLD_WOFF2="hack-bold-subset.woff2"
  47. ITALIC_TTF="Hack-Italic.ttf"
  48. ITALIC_WOFF_PRE="Hack-Italic.woff"
  49. ITALIC_WOFF="hack-italic-subset.woff"
  50. ITALIC_WOFF2_PRE="Hack-Italic.woff2"
  51. ITALIC_WOFF2="hack-italic-subset.woff2"
  52. BOLDITALIC_TTF="Hack-BoldItalic.ttf"
  53. BOLDITALIC_WOFF_PRE="Hack-BoldItalic.woff"
  54. BOLDITALIC_WOFF="hack-bolditalic-subset.woff"
  55. BOLDITALIC_WOFF2_PRE="Hack-BoldItalic.woff2"
  56. BOLDITALIC_WOFF2="hack-bolditalic-subset.woff2"
  57. # release directory path for web fonts
  58. WEB_BUILD="build/web/fonts"
  59. # test for number of arguments
  60. if [ $# -gt 1 ]
  61. then
  62. echo "Inappropriate arguments included in your command." 1>&2
  63. echo "Usage: ./build-subsets.sh (--system)" 1>&2
  64. exit 1
  65. fi
  66. # //////////////////////////////////////////////
  67. #
  68. #
  69. # Re-define build dependencies to PATH
  70. # installed versions if --system flag is used
  71. #
  72. #
  73. # //////////////////////////////////////////////
  74. if [ "$1" = "--system" ]; then
  75. TTFAH="ttfautohint"
  76. FONTMAKE="fontmake"
  77. PYTHON="python3"
  78. SFNTWOFF_BIN="sfnt2woff-zopfli"
  79. WOFF2_BIN="woff2_compress"
  80. fi
  81. # ////////////////////////////////////////////////
  82. #
  83. #
  84. # Create temporary source files with lib.plist
  85. # replacements that include subset definitions
  86. #
  87. #
  88. # ////////////////////////////////////////////////
  89. # cleanup any previously created temp directory that was not removed
  90. if [ -d "$TEMP_SOURCE" ]; then
  91. rm -rf $TEMP_SOURCE
  92. fi
  93. # create temp directory for subset source files
  94. mkdir $TEMP_SOURCE
  95. # copy source to temporary directory
  96. cp -r source/Hack-Regular.ufo $TEMP_SOURCE/Hack-Regular.ufo
  97. cp -r source/Hack-Italic.ufo $TEMP_SOURCE/Hack-Italic.ufo
  98. cp -r source/Hack-Bold.ufo $TEMP_SOURCE/Hack-Bold.ufo
  99. cp -r source/Hack-BoldItalic.ufo $TEMP_SOURCE/Hack-BoldItalic.ufo
  100. # copy lib.plist files with subset definitions to temporary source directories
  101. cp source/subset-lib/lib-regular.plist $TEMP_SOURCE/Hack-Regular.ufo/lib.plist
  102. cp source/subset-lib/lib-italic.plist $TEMP_SOURCE/Hack-Italic.ufo/lib.plist
  103. cp source/subset-lib/lib-bold.plist $TEMP_SOURCE/Hack-Bold.ufo/lib.plist
  104. cp source/subset-lib/lib-bolditalic.plist $TEMP_SOURCE/Hack-BoldItalic.ufo/lib.plist
  105. # /////////////////////////////////////////////
  106. #
  107. #
  108. # Begin subset ttf font build from UFO source
  109. #
  110. #
  111. # /////////////////////////////////////////////
  112. echo "Starting web font subset build..."
  113. echo " "
  114. # remove master_ttf directory if a previous build failed + exited early and it was not cleaned up
  115. if [ -d "master_ttf" ]; then
  116. rm -rf master_ttf
  117. fi
  118. # build regular subset
  119. if ! $FONTMAKE --subset -u "$TEMP_SOURCE/Hack-Regular.ufo" -o ttf
  120. then
  121. echo "Unable to build the Hack-Regular variant subset. Build canceled." 1>&2
  122. exit 1
  123. fi
  124. # build bold subset
  125. if ! $FONTMAKE --subset -u "$TEMP_SOURCE/Hack-Bold.ufo" -o ttf
  126. then
  127. echo "Unable to build the Hack-Bold variant subset. Build canceled." 1>&2
  128. exit 1
  129. fi
  130. # build italic subset
  131. if ! $FONTMAKE --subset -u "$TEMP_SOURCE/Hack-Italic.ufo" -o ttf
  132. then
  133. echo "Unable to build the Hack-Italic variant subset. Build canceled." 1>&2
  134. exit 1
  135. fi
  136. # build bold italic subset
  137. if ! $FONTMAKE --subset -u "$TEMP_SOURCE/Hack-BoldItalic.ufo" -o ttf
  138. then
  139. echo "Unable to build the Hack-BoldItalic variant subset. Build canceled." 1>&2
  140. exit 1
  141. fi
  142. # /////////////////////////////////////////////
  143. #
  144. #
  145. # Post build fixes
  146. #
  147. #
  148. # /////////////////////////////////////////////
  149. # DSIG table fix with adapted fontbakery Python script
  150. echo " "
  151. echo "Attempting DSIG table fixes with fontbakery..."
  152. echo " "
  153. if ! $PYTHON postbuild_processing/fixes/fix-dsig.py master_ttf/*.ttf
  154. then
  155. echo "Unable to complete DSIG table fixes on the release files"
  156. exit 1
  157. fi
  158. # fstype value fix with adapted fontbakery Python script
  159. echo " "
  160. echo "Attempting fstype fixes with fontbakery..."
  161. echo " "
  162. if ! $PYTHON postbuild_processing/fixes/fix-fstype.py master_ttf/*.ttf
  163. then
  164. echo "Unable to complete fstype fixes on the release files"
  165. exit 1
  166. fi
  167. # /////////////////////////////////////////////
  168. #
  169. #
  170. # Hinting of ttf subsets
  171. #
  172. #
  173. # /////////////////////////////////////////////
  174. echo " "
  175. echo "Attempting ttfautohint hinting..."
  176. echo " "
  177. # make a temporary directory for the hinted files
  178. mkdir master_ttf/hinted
  179. # Hack-Regular.ttf
  180. if ! "$TTFAH" -l 6 -r 50 -x 10 -H 181 -D latn -f latn -w G -W -t -X "" -I -R "master_ttf/Hack-Regular.ttf" -m "postbuild_processing/tt-hinting/Hack-Regular-TA.txt" "master_ttf/Hack-Regular.ttf" "master_ttf/hinted/Hack-Regular.ttf"
  181. then
  182. echo "Unable to execute ttfautohint on the Hack-Regular variant subset. Build canceled." 1>&2
  183. exit 1
  184. fi
  185. echo "master_ttf/Hack-Regular.ttf subset - successful hinting with ttfautohint"
  186. # Hack-Bold.ttf
  187. if ! "$TTFAH" -l 6 -r 50 -x 10 -H 260 -D latn -f latn -w G -W -t -X "" -I -R "master_ttf/Hack-Regular.ttf" -m "postbuild_processing/tt-hinting/Hack-Bold-TA.txt" "master_ttf/Hack-Bold.ttf" "master_ttf/hinted/Hack-Bold.ttf"
  188. then
  189. echo "Unable to execute ttfautohint on the Hack-Bold variant subset. Build canceled." 1>&2
  190. exit 1
  191. fi
  192. echo "master_ttf/Hack-Bold.ttf subset - successful hinting with ttfautohint"
  193. # Hack-Italic.ttf
  194. if ! "$TTFAH" -l 6 -r 50 -x 10 -H 145 -D latn -f latn -w G -W -t -X "" -I -R "master_ttf/Hack-Regular.ttf" -m "postbuild_processing/tt-hinting/Hack-Italic-TA.txt" "master_ttf/Hack-Italic.ttf" "master_ttf/hinted/Hack-Italic.ttf"
  195. then
  196. echo "Unable to execute ttfautohint on the Hack-Italic variant subset. Build canceled." 1>&2
  197. exit 1
  198. fi
  199. echo "master_ttf/Hack-Italic.ttf subset - successful hinting with ttfautohint"
  200. # Hack-BoldItalic.ttf
  201. if ! "$TTFAH" -l 6 -r 50 -x 10 -H 265 -D latn -f latn -w G -W -t -X "" -I -R "master_ttf/Hack-Regular.ttf" -m "postbuild_processing/tt-hinting/Hack-BoldItalic-TA.txt" "master_ttf/Hack-BoldItalic.ttf" "master_ttf/hinted/Hack-BoldItalic.ttf"
  202. then
  203. echo "Unable to execute ttfautohint on the Hack-BoldItalic variant subset. Build canceled." 1>&2
  204. exit 1
  205. fi
  206. echo "master_ttf/Hack-BoldItalic.ttf subset - successful hinting with ttfautohint"
  207. echo " "
  208. # /////////////////////////////////////////////
  209. #
  210. #
  211. # Build woff subsets
  212. #
  213. #
  214. # /////////////////////////////////////////////
  215. # regular set
  216. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$REGULAR_TTF"; then
  217. echo "Failed to build $REGULAR_WOFF from $REGULAR_TTF." 1>&2
  218. exit 1
  219. else
  220. echo "Regular woff subset successfully built from $REGULAR_TTF"
  221. fi
  222. # bold set
  223. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$BOLD_TTF"; then
  224. echo "Failed to build $BOLD_WOFF from $BOLD_TTF" 1>&2
  225. exit 1
  226. else
  227. echo "Bold woff subset successfully built from $BOLD_TTF"
  228. fi
  229. # italic set
  230. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$ITALIC_TTF"; then
  231. echo "Failed to build $BOLD_WOFF from $ITALIC_TTF" 1>&2
  232. exit 1
  233. else
  234. echo "Italic woff subset successfully built from $ITALIC_TTF"
  235. fi
  236. # bold italic set
  237. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$BOLDITALIC_TTF"; then
  238. echo "Failed to build $BOLDITALIC_WOFF from $BOLDITALIC_TTF" 1>&2
  239. exit 1
  240. else
  241. echo "Bold Italic woff subset successfully built from $BOLDITALIC_TTF"
  242. fi
  243. # /////////////////////////////////////////////
  244. #
  245. #
  246. # Build woff2 subsets
  247. #
  248. #
  249. # /////////////////////////////////////////////
  250. echo " "
  251. # regular set
  252. if ! "$WOFF2_BIN" "$TTF_BUILD/$REGULAR_TTF"; then
  253. echo "Failed to build woff2 subset from $REGULAR_TTF." 1>&2
  254. exit 1
  255. else
  256. echo "Regular woff2 font subset successfully built from $REGULAR_TTF"
  257. fi
  258. # bold set
  259. if ! "$WOFF2_BIN" "$TTF_BUILD/$BOLD_TTF"; then
  260. echo "Failed to build woff2 subset from $BOLD_TTF" 1>&2
  261. exit 1
  262. else
  263. echo "Bold woff2 subset successfully built from $BOLD_TTF"
  264. fi
  265. # italic set
  266. if ! "$WOFF2_BIN" "$TTF_BUILD/$ITALIC_TTF"; then
  267. echo "Failed to build woff2 subset from $ITALIC_TTF" 1>&2
  268. exit 1
  269. else
  270. echo "Italic woff2 subset successfully built from $ITALIC_TTF"
  271. fi
  272. # bold italic set
  273. if ! "$WOFF2_BIN" "$TTF_BUILD/$BOLDITALIC_TTF"; then
  274. echo "Failed to build woff2 subset from $BOLDITALIC_TTF" 1>&2
  275. exit 1
  276. else
  277. echo "Bold Italic woff2 subset successfully built from $BOLDITALIC_TTF"
  278. fi
  279. # //////////////////////////////////////////////
  280. #
  281. #
  282. # Move web font subset files to build directory
  283. #
  284. #
  285. # //////////////////////////////////////////////
  286. # create the build directory if it does not exist
  287. if ! [ -d "$WEB_BUILD" ]; then
  288. mkdir $WEB_BUILD
  289. fi
  290. echo " "
  291. echo "Moving woff files to build directory..."
  292. # move woff files to appropriate build directory
  293. mv "$TTF_BUILD/$REGULAR_WOFF_PRE" "$WEB_BUILD/$REGULAR_WOFF"
  294. mv "$TTF_BUILD/$BOLD_WOFF_PRE" "$WEB_BUILD/$BOLD_WOFF"
  295. mv "$TTF_BUILD/$ITALIC_WOFF_PRE" "$WEB_BUILD/$ITALIC_WOFF"
  296. mv "$TTF_BUILD/$BOLDITALIC_WOFF_PRE" "$WEB_BUILD/$BOLDITALIC_WOFF"
  297. if [ -f "$WEB_BUILD/$REGULAR_WOFF" ]; then
  298. echo "Regular woff build path: $WEB_BUILD/$REGULAR_WOFF"
  299. fi
  300. if [ -f "$WEB_BUILD/$BOLD_WOFF" ]; then
  301. echo "Bold woff build path: $WEB_BUILD/$BOLD_WOFF"
  302. fi
  303. if [ -f "$WEB_BUILD/$ITALIC_WOFF" ]; then
  304. echo "Italic woff build path: $WEB_BUILD/$ITALIC_WOFF"
  305. fi
  306. if [ -f "$WEB_BUILD/$BOLDITALIC_WOFF" ]; then
  307. echo "Bold Italic woff build path: $WEB_BUILD/$BOLDITALIC_WOFF"
  308. fi
  309. echo "Moving woff2 files to build directory..."
  310. # move woff files to appropriate build directory
  311. mv "$TTF_BUILD/$REGULAR_WOFF2_PRE" "$WEB_BUILD/$REGULAR_WOFF2"
  312. mv "$TTF_BUILD/$BOLD_WOFF2_PRE" "$WEB_BUILD/$BOLD_WOFF2"
  313. mv "$TTF_BUILD/$ITALIC_WOFF2_PRE" "$WEB_BUILD/$ITALIC_WOFF2"
  314. mv "$TTF_BUILD/$BOLDITALIC_WOFF2_PRE" "$WEB_BUILD/$BOLDITALIC_WOFF2"
  315. if [ -f "$WEB_BUILD/$REGULAR_WOFF2" ]; then
  316. echo "Regular woff2 subset build path: $WEB_BUILD/$REGULAR_WOFF2"
  317. fi
  318. if [ -f "$WEB_BUILD/$BOLD_WOFF2" ]; then
  319. echo "Bold woff2 subset build path: $WEB_BUILD/$BOLD_WOFF2"
  320. fi
  321. if [ -f "$WEB_BUILD/$ITALIC_WOFF2" ]; then
  322. echo "Italic woff2 subset build path: $WEB_BUILD/$ITALIC_WOFF2"
  323. fi
  324. if [ -f "$WEB_BUILD/$BOLDITALIC_WOFF2" ]; then
  325. echo "Bold Italic woff2 subset build path: $WEB_BUILD/$BOLDITALIC_WOFF2"
  326. fi
  327. # //////////////////////////////////////////////
  328. #
  329. #
  330. # Cleanup temp directory
  331. #
  332. #
  333. # //////////////////////////////////////////////
  334. rm -rf master_ttf
  335. rm -rf "$TEMP_SOURCE"