build-subsets.sh 11 KB

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