build-subsets.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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
  10. #
  11. # NOTE: must install build dependencies in:
  12. # 1) build-ttf.sh
  13. # 2) build-woff.sh
  14. # 3) build-woff2.sh
  15. # before you execute this script. You can do this with:
  16. #
  17. #
  18. # $ ./build-ttf.sh --install-dependencies
  19. # $ ./build-woff.sh --install-dependencies
  20. # $ ./build-woff2.sh --install-dependencies
  21. #
  22. # //////////////////////////////////////////////////////////////////////
  23. # ttfautohint local install path from Werner Lemberg's ttfautohint-build.sh install script
  24. # - This is revised to ttfautohint on the user's PATH if this local install is not identified
  25. # then the identified ttfautohint is used to execute hinting. Versions of ttfautohint < 1.6 exit with status
  26. # code 1 due to use of -R option
  27. # - The intent is to support use of ttfautohint installed on a user's PATH (e.g. they've previously installed it)
  28. TTFAH="$HOME/ttfautohint-build/local/bin/ttfautohint"
  29. # The sfnt2woff-zopfli build directory.
  30. SFNTWOFF_BUILD="$HOME/sfnt2woff-zopfli-build"
  31. # sfnt2woff-zopfli version
  32. SFNTWOFF_VERSION="1.1.0"
  33. SFNTWOFF="sfnt2woff-zopfli-$SFNTWOFF_VERSION"
  34. # Path to sfnt2woff-zopfli executable
  35. SFNTWOFF_BIN="$SFNTWOFF_BUILD/$SFNTWOFF/sfnt2woff-zopfli"
  36. ZOPFLI_ITERATIONS="3"
  37. # The woff2 git clone directory.
  38. WOFF2_BUILD="$HOME"
  39. # woff2 executable path
  40. WOFF2_BIN="$WOFF2_BUILD/woff2/woff2_compress"
  41. # temporary source directory for subset source files
  42. TEMP_SOURCE="source/temp"
  43. # The font build directory paths and file paths for the woff builds
  44. TTF_BUILD="master_ttf"
  45. REGULAR_TTF="Hack-Regular.ttf"
  46. REGULAR_WOFF_PRE="Hack-Regular.woff"
  47. REGULAR_WOFF="hack-regular-subset.woff"
  48. REGULAR_WOFF2_PRE="Hack-Regular.woff2"
  49. REGULAR_WOFF2="hack-regular-subset.woff2"
  50. BOLD_TTF="Hack-Bold.ttf"
  51. BOLD_WOFF_PRE="Hack-Bold.woff"
  52. BOLD_WOFF="hack-bold-subset.woff"
  53. BOLD_WOFF2_PRE="Hack-Bold.woff2"
  54. BOLD_WOFF2="hack-bold-subset.woff2"
  55. ITALIC_TTF="Hack-Italic.ttf"
  56. ITALIC_WOFF_PRE="Hack-Italic.woff"
  57. ITALIC_WOFF="hack-italic-subset.woff"
  58. ITALIC_WOFF2_PRE="Hack-Italic.woff2"
  59. ITALIC_WOFF2="hack-italic-subset.woff2"
  60. BOLDITALIC_TTF="Hack-BoldItalic.ttf"
  61. BOLDITALIC_WOFF_PRE="Hack-BoldItalic.woff"
  62. BOLDITALIC_WOFF="hack-bolditalic-subset.woff"
  63. BOLDITALIC_WOFF2_PRE="Hack-BoldItalic.woff2"
  64. BOLDITALIC_WOFF2="hack-bolditalic-subset.woff2"
  65. # release directory path for web fonts
  66. WEB_BUILD="build/web/fonts"
  67. # test for number of arguments
  68. if [ $# -gt 0 ]
  69. then
  70. echo "Inappropriate arguments included in your command." 1>&2
  71. echo "Usage: ./build-subsets.sh" 1>&2
  72. exit 1
  73. fi
  74. # ////////////////////////////////////////
  75. #
  76. #
  77. # Confirm that dependencies are installed
  78. #
  79. #
  80. # ////////////////////////////////////////
  81. INSTALLFLAG=0
  82. echo "Confirming that build dependencies are installed..."
  83. echo " "
  84. # fontmake installed
  85. if ! which fontmake
  86. then
  87. echo "fontmake was not found. Please install all build dependencies with 'make build-with-dependencies', then attempt your build again." 1>&2
  88. INSTALLFLAG=1
  89. fi
  90. # fontTools python library can be imported
  91. if ! python -c "import fontTools"
  92. then
  93. echo "The fontTools library was not found. Please install all build dependencies with 'make build-with-dependencies', then attempt your build again." 1>&2
  94. INSTALLFLAG=1
  95. else
  96. echo "fontTools Python library identified"
  97. fi
  98. # ttfautohint installed
  99. # - tests for install to local path from ttfautohint-build.sh script
  100. # - if not found on this path, tests for install on system PATH - if found, revises TTFAH to the string "ttfautohint" for execution of instruction sets
  101. if ! [ -f "$TTFAH" ]
  102. then
  103. if ! which ttfautohint
  104. then
  105. echo "ttfautohint was not found. Please install all build dependencies with 'make build-with-dependencies', then attempt your build again." 1>&2
  106. INSTALLFLAG=1
  107. else
  108. TTFAH="ttfautohint" # revise TTFAH variable to ttfautohint installed on the user's PATH for excecution of hints below
  109. echo "ttfautohint executable identified"
  110. fi
  111. else
  112. echo "ttfautohint executable identified"
  113. fi
  114. # sfntwoff-zopfli installed
  115. if ! [ -f "$SFNTWOFF_BIN" ]
  116. then
  117. echo "sfnt2woff-zopfli was not found on the path $SFNTWOFF_BIN. Please install all build dependencies with 'make build-with-dependencies', then attempt your build again." 1>&2
  118. INSTALLFLAG=1
  119. else
  120. echo "sfnt2woff-zopfli executable identified"
  121. fi
  122. # woff2 installed
  123. if ! [ -f "$WOFF2_BIN" ]
  124. then
  125. echo "woff2_compress was not found on the path $WOFF2_BIN. Please install all build dependencies with 'make build-with-dependencies', then attempt your build again." 1>&2
  126. INSTALLFLAG=1
  127. else
  128. echo "woff2_compress executable identified"
  129. fi
  130. # if any of the dependency installs failed, exit and do not attempt build, notify user
  131. if [ $INSTALLFLAG -eq 1 ]
  132. then
  133. echo "Build canceled." 1>&2
  134. exit 1
  135. fi
  136. # ////////////////////////////////////////////////
  137. #
  138. #
  139. # Create temporary source files with lib.plist
  140. # replacements that include subset definitions
  141. #
  142. #
  143. # ////////////////////////////////////////////////
  144. # cleanup any previously created temp directory that was not removed
  145. if ! [ -d "$TEMP_SOURCE" ]; then
  146. rm -rf $TEMP_SOURCE
  147. fi
  148. # create temp directory for subset source files
  149. mkdir $TEMP_SOURCE
  150. # copy source to temporary directory
  151. cp -r source/Hack-Regular.ufo $TEMP_SOURCE/Hack-Regular.ufo
  152. cp -r source/Hack-Italic.ufo $TEMP_SOURCE/Hack-Italic.ufo
  153. cp -r source/Hack-Bold.ufo $TEMP_SOURCE/Hack-Bold.ufo
  154. cp -r source/Hack-BoldItalic.ufo $TEMP_SOURCE/Hack-BoldItalic.ufo
  155. # copy lib.plist files with subset definitions to temporary source directories
  156. cp source/subset-lib/lib-regular.plist $TEMP_SOURCE/Hack-Regular.ufo/lib.plist
  157. cp source/subset-lib/lib-italic.plist $TEMP_SOURCE/Hack-Italic.ufo/lib.plist
  158. cp source/subset-lib/lib-bold.plist $TEMP_SOURCE/Hack-Bold.ufo/lib.plist
  159. cp source/subset-lib/lib-bolditalic.plist $TEMP_SOURCE/Hack-BoldItalic.ufo/lib.plist
  160. # /////////////////////////////////////////////
  161. #
  162. #
  163. # Begin subset ttf font build from UFO source
  164. #
  165. #
  166. # /////////////////////////////////////////////
  167. echo "Starting web font subset build..."
  168. echo " "
  169. # remove master_ttf directory if a previous build failed + exited early and it was not cleaned up
  170. if [ -d "master_ttf" ]; then
  171. rm -rf master_ttf
  172. fi
  173. # build regular subset
  174. if ! fontmake --subset -u "$TEMP_SOURCE/Hack-Regular.ufo" -o ttf
  175. then
  176. echo "Unable to build the Hack-Regular variant subset. Build canceled." 1>&2
  177. exit 1
  178. fi
  179. # build bold subset
  180. if ! fontmake --subset -u "$TEMP_SOURCE/Hack-Bold.ufo" -o ttf
  181. then
  182. echo "Unable to build the Hack-Bold variant subset. Build canceled." 1>&2
  183. exit 1
  184. fi
  185. # build italic subset
  186. if ! fontmake --subset -u "$TEMP_SOURCE/Hack-Italic.ufo" -o ttf
  187. then
  188. echo "Unable to build the Hack-Italic variant subset. Build canceled." 1>&2
  189. exit 1
  190. fi
  191. # build bold italic subset
  192. if ! fontmake --subset -u "$TEMP_SOURCE/Hack-BoldItalic.ufo" -o ttf
  193. then
  194. echo "Unable to build the Hack-BoldItalic variant subset. Build canceled." 1>&2
  195. exit 1
  196. fi
  197. # /////////////////////////////////////////////
  198. #
  199. #
  200. # Post build fixes
  201. #
  202. #
  203. # /////////////////////////////////////////////
  204. # DSIG table fix with adapted fontbakery Python script
  205. echo " "
  206. echo "Attempting DSIG table fixes with fontbakery..."
  207. echo " "
  208. if ! python postbuild_processing/fixes/fix-dsig.py master_ttf/*.ttf
  209. then
  210. echo "Unable to complete DSIG table fixes on the release files"
  211. exit 1
  212. fi
  213. # fstype value fix with adapted fontbakery Python script
  214. echo " "
  215. echo "Attempting fstype fixes with fontbakery..."
  216. echo " "
  217. if ! python postbuild_processing/fixes/fix-fstype.py master_ttf/*.ttf
  218. then
  219. echo "Unable to complete fstype fixes on the release files"
  220. exit 1
  221. fi
  222. # /////////////////////////////////////////////
  223. #
  224. #
  225. # Hinting of ttf subsets
  226. #
  227. #
  228. # /////////////////////////////////////////////
  229. echo " "
  230. echo "Attempting ttfautohint hinting..."
  231. echo " "
  232. # make a temporary directory for the hinted files
  233. mkdir master_ttf/hinted
  234. # Hack-Regular.ttf
  235. 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"
  236. then
  237. echo "Unable to execute ttfautohint on the Hack-Regular variant subset. Build canceled." 1>&2
  238. exit 1
  239. fi
  240. echo "master_ttf/Hack-Regular.ttf subset - successful hinting with ttfautohint"
  241. # Hack-Bold.ttf
  242. 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"
  243. then
  244. echo "Unable to execute ttfautohint on the Hack-Bold variant subset. Build canceled." 1>&2
  245. exit 1
  246. fi
  247. echo "master_ttf/Hack-Bold.ttf subset - successful hinting with ttfautohint"
  248. # Hack-Italic.ttf
  249. 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"
  250. then
  251. echo "Unable to execute ttfautohint on the Hack-Italic variant subset. Build canceled." 1>&2
  252. exit 1
  253. fi
  254. echo "master_ttf/Hack-Italic.ttf subset - successful hinting with ttfautohint"
  255. # Hack-BoldItalic.ttf
  256. 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"
  257. then
  258. echo "Unable to execute ttfautohint on the Hack-BoldItalic variant subset. Build canceled." 1>&2
  259. exit 1
  260. fi
  261. echo "master_ttf/Hack-BoldItalic.ttf subset - successful hinting with ttfautohint"
  262. echo " "
  263. # /////////////////////////////////////////////
  264. #
  265. #
  266. # Build woff subsets
  267. #
  268. #
  269. # /////////////////////////////////////////////
  270. # regular set
  271. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$REGULAR_TTF"; then
  272. echo "Failed to build $REGULAR_WOFF from $REGULAR_TTF." 1>&2
  273. exit 1
  274. else
  275. echo "Regular woff subset successfully built from $REGULAR_TTF"
  276. fi
  277. # bold set
  278. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$BOLD_TTF"; then
  279. echo "Failed to build $BOLD_WOFF from $BOLD_TTF" 1>&2
  280. exit 1
  281. else
  282. echo "Bold woff subset successfully built from $BOLD_TTF"
  283. fi
  284. # italic set
  285. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$ITALIC_TTF"; then
  286. echo "Failed to build $BOLD_WOFF from $ITALIC_TTF" 1>&2
  287. exit 1
  288. else
  289. echo "Italic woff subset successfully built from $ITALIC_TTF"
  290. fi
  291. # bold italic set
  292. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$BOLDITALIC_TTF"; then
  293. echo "Failed to build $BOLDITALIC_WOFF from $BOLDITALIC_TTF" 1>&2
  294. exit 1
  295. else
  296. echo "Bold Italic woff subset successfully built from $BOLDITALIC_TTF"
  297. fi
  298. # /////////////////////////////////////////////
  299. #
  300. #
  301. # Build woff2 subsets
  302. #
  303. #
  304. # /////////////////////////////////////////////
  305. echo " "
  306. # regular set
  307. if ! "$WOFF2_BIN" "$TTF_BUILD/$REGULAR_TTF"; then
  308. echo "Failed to build woff2 subset from $REGULAR_TTF." 1>&2
  309. exit 1
  310. else
  311. echo "Regular woff2 font subset successfully built from $REGULAR_TTF"
  312. fi
  313. # bold set
  314. if ! "$WOFF2_BIN" "$TTF_BUILD/$BOLD_TTF"; then
  315. echo "Failed to build woff2 subset from $BOLD_TTF" 1>&2
  316. exit 1
  317. else
  318. echo "Bold woff2 subset successfully built from $BOLD_TTF"
  319. fi
  320. # italic set
  321. if ! "$WOFF2_BIN" "$TTF_BUILD/$ITALIC_TTF"; then
  322. echo "Failed to build woff2 subset from $ITALIC_TTF" 1>&2
  323. exit 1
  324. else
  325. echo "Italic woff2 subset successfully built from $ITALIC_TTF"
  326. fi
  327. # bold italic set
  328. if ! "$WOFF2_BIN" "$TTF_BUILD/$BOLDITALIC_TTF"; then
  329. echo "Failed to build woff2 subset from $BOLDITALIC_TTF" 1>&2
  330. exit 1
  331. else
  332. echo "Bold Italic woff2 subset successfully built from $BOLDITALIC_TTF"
  333. fi
  334. # //////////////////////////////////////////////
  335. #
  336. #
  337. # Move web font subset files to build directory
  338. #
  339. #
  340. # //////////////////////////////////////////////
  341. # create the build directory if it does not exist
  342. if ! [ -d "$WEB_BUILD" ]; then
  343. mkdir $WEB_BUILD
  344. fi
  345. echo " "
  346. echo "Moving woff files to build directory..."
  347. # move woff files to appropriate build directory
  348. mv "$TTF_BUILD/$REGULAR_WOFF_PRE" "$WEB_BUILD/$REGULAR_WOFF"
  349. mv "$TTF_BUILD/$BOLD_WOFF_PRE" "$WEB_BUILD/$BOLD_WOFF"
  350. mv "$TTF_BUILD/$ITALIC_WOFF_PRE" "$WEB_BUILD/$ITALIC_WOFF"
  351. mv "$TTF_BUILD/$BOLDITALIC_WOFF_PRE" "$WEB_BUILD/$BOLDITALIC_WOFF"
  352. if [ -f "$WEB_BUILD/$REGULAR_WOFF" ]; then
  353. echo "Regular woff build path: $WEB_BUILD/$REGULAR_WOFF"
  354. fi
  355. if [ -f "$WEB_BUILD/$BOLD_WOFF" ]; then
  356. echo "Bold woff build path: $WEB_BUILD/$BOLD_WOFF"
  357. fi
  358. if [ -f "$WEB_BUILD/$ITALIC_WOFF" ]; then
  359. echo "Italic woff build path: $WEB_BUILD/$ITALIC_WOFF"
  360. fi
  361. if [ -f "$WEB_BUILD/$BOLDITALIC_WOFF" ]; then
  362. echo "Bold Italic woff build path: $WEB_BUILD/$BOLDITALIC_WOFF"
  363. fi
  364. echo "Moving woff2 files to build directory..."
  365. # move woff files to appropriate build directory
  366. mv "$TTF_BUILD/$REGULAR_WOFF2_PRE" "$WEB_BUILD/$REGULAR_WOFF2"
  367. mv "$TTF_BUILD/$BOLD_WOFF2_PRE" "$WEB_BUILD/$BOLD_WOFF2"
  368. mv "$TTF_BUILD/$ITALIC_WOFF2_PRE" "$WEB_BUILD/$ITALIC_WOFF2"
  369. mv "$TTF_BUILD/$BOLDITALIC_WOFF2_PRE" "$WEB_BUILD/$BOLDITALIC_WOFF2"
  370. if [ -f "$WEB_BUILD/$REGULAR_WOFF2" ]; then
  371. echo "Regular woff2 subset build path: $WEB_BUILD/$REGULAR_WOFF2"
  372. fi
  373. if [ -f "$WEB_BUILD/$BOLD_WOFF2" ]; then
  374. echo "Bold woff2 subset build path: $WEB_BUILD/$BOLD_WOFF2"
  375. fi
  376. if [ -f "$WEB_BUILD/$ITALIC_WOFF2" ]; then
  377. echo "Italic woff2 subset build path: $WEB_BUILD/$ITALIC_WOFF2"
  378. fi
  379. if [ -f "$WEB_BUILD/$BOLDITALIC_WOFF2" ]; then
  380. echo "Bold Italic woff2 subset build path: $WEB_BUILD/$BOLDITALIC_WOFF2"
  381. fi
  382. # //////////////////////////////////////////////
  383. #
  384. #
  385. # Cleanup temp directory
  386. #
  387. #
  388. # //////////////////////////////////////////////
  389. rm -rf master_ttf
  390. rm -rf "$TEMP_SOURCE"