2
0

build-subsets.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. # ttfautohint installed
  83. # - tests for install to local path from ttfautohint-build.sh script
  84. # - 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
  85. if ! [ -f "$TTFAH" ]
  86. then
  87. if ! which ttfautohint
  88. then
  89. echo "ttfautohint was not found. Please install all build dependencies with 'make build-with-dependencies', then attempt your build again." 1>&2
  90. INSTALLFLAG=1
  91. else
  92. TTFAH="ttfautohint" # revise TTFAH variable to ttfautohint installed on the user's PATH for excecution of hints below
  93. echo "ttfautohint executable identified"
  94. fi
  95. else
  96. echo "ttfautohint executable identified"
  97. fi
  98. # sfntwoff-zopfli installed
  99. if ! [ -f "$SFNTWOFF_BIN" ]
  100. then
  101. 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
  102. INSTALLFLAG=1
  103. else
  104. echo "sfnt2woff-zopfli executable identified"
  105. fi
  106. # woff2 installed
  107. if ! [ -f "$WOFF2_BIN" ]
  108. then
  109. 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
  110. INSTALLFLAG=1
  111. else
  112. echo "woff2_compress executable identified"
  113. fi
  114. # if any of the dependency installs failed, exit and do not attempt build, notify user
  115. if [ $INSTALLFLAG -eq 1 ]
  116. then
  117. echo "Build canceled." 1>&2
  118. exit 1
  119. fi
  120. # ////////////////////////////////////////////////
  121. #
  122. #
  123. # Create temporary source files with lib.plist
  124. # replacements that include subset definitions
  125. #
  126. #
  127. # ////////////////////////////////////////////////
  128. # cleanup any previously created temp directory that was not removed
  129. if ! [ -d "$TEMP_SOURCE" ]; then
  130. rm -rf $TEMP_SOURCE
  131. fi
  132. # create temp directory for subset source files
  133. mkdir $TEMP_SOURCE
  134. # copy source to temporary directory
  135. cp -r source/Hack-Regular.ufo $TEMP_SOURCE/Hack-Regular.ufo
  136. cp -r source/Hack-Italic.ufo $TEMP_SOURCE/Hack-Italic.ufo
  137. cp -r source/Hack-Bold.ufo $TEMP_SOURCE/Hack-Bold.ufo
  138. cp -r source/Hack-BoldItalic.ufo $TEMP_SOURCE/Hack-BoldItalic.ufo
  139. # copy lib.plist files with subset definitions to temporary source directories
  140. cp source/subset-lib/lib-regular.plist $TEMP_SOURCE/Hack-Regular.ufo/lib.plist
  141. cp source/subset-lib/lib-italic.plist $TEMP_SOURCE/Hack-Italic.ufo/lib.plist
  142. cp source/subset-lib/lib-bold.plist $TEMP_SOURCE/Hack-Bold.ufo/lib.plist
  143. cp source/subset-lib/lib-bolditalic.plist $TEMP_SOURCE/Hack-BoldItalic.ufo/lib.plist
  144. # /////////////////////////////////////////////
  145. #
  146. #
  147. # Begin subset ttf font build from UFO source
  148. #
  149. #
  150. # /////////////////////////////////////////////
  151. echo "Starting web font subset build..."
  152. echo " "
  153. # remove master_ttf directory if a previous build failed + exited early and it was not cleaned up
  154. if [ -d "master_ttf" ]; then
  155. rm -rf master_ttf
  156. fi
  157. # build regular subset
  158. if ! pipenv run fontmake --subset -u "$TEMP_SOURCE/Hack-Regular.ufo" -o ttf
  159. then
  160. echo "Unable to build the Hack-Regular variant subset. Build canceled." 1>&2
  161. exit 1
  162. fi
  163. # build bold subset
  164. if ! pipenv run fontmake --subset -u "$TEMP_SOURCE/Hack-Bold.ufo" -o ttf
  165. then
  166. echo "Unable to build the Hack-Bold variant subset. Build canceled." 1>&2
  167. exit 1
  168. fi
  169. # build italic subset
  170. if ! pipenv run fontmake --subset -u "$TEMP_SOURCE/Hack-Italic.ufo" -o ttf
  171. then
  172. echo "Unable to build the Hack-Italic variant subset. Build canceled." 1>&2
  173. exit 1
  174. fi
  175. # build bold italic subset
  176. if ! pipenv run fontmake --subset -u "$TEMP_SOURCE/Hack-BoldItalic.ufo" -o ttf
  177. then
  178. echo "Unable to build the Hack-BoldItalic variant subset. Build canceled." 1>&2
  179. exit 1
  180. fi
  181. # /////////////////////////////////////////////
  182. #
  183. #
  184. # Post build fixes
  185. #
  186. #
  187. # /////////////////////////////////////////////
  188. # DSIG table fix with adapted fontbakery Python script
  189. echo " "
  190. echo "Attempting DSIG table fixes with fontbakery..."
  191. echo " "
  192. if ! pipenv run python postbuild_processing/fixes/fix-dsig.py master_ttf/*.ttf
  193. then
  194. echo "Unable to complete DSIG table fixes on the release files"
  195. exit 1
  196. fi
  197. # fstype value fix with adapted fontbakery Python script
  198. echo " "
  199. echo "Attempting fstype fixes with fontbakery..."
  200. echo " "
  201. if ! pipenv run python postbuild_processing/fixes/fix-fstype.py master_ttf/*.ttf
  202. then
  203. echo "Unable to complete fstype fixes on the release files"
  204. exit 1
  205. fi
  206. # /////////////////////////////////////////////
  207. #
  208. #
  209. # Hinting of ttf subsets
  210. #
  211. #
  212. # /////////////////////////////////////////////
  213. echo " "
  214. echo "Attempting ttfautohint hinting..."
  215. echo " "
  216. # make a temporary directory for the hinted files
  217. mkdir master_ttf/hinted
  218. # Hack-Regular.ttf
  219. 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"
  220. then
  221. echo "Unable to execute ttfautohint on the Hack-Regular variant subset. Build canceled." 1>&2
  222. exit 1
  223. fi
  224. echo "master_ttf/Hack-Regular.ttf subset - successful hinting with ttfautohint"
  225. # Hack-Bold.ttf
  226. 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"
  227. then
  228. echo "Unable to execute ttfautohint on the Hack-Bold variant subset. Build canceled." 1>&2
  229. exit 1
  230. fi
  231. echo "master_ttf/Hack-Bold.ttf subset - successful hinting with ttfautohint"
  232. # Hack-Italic.ttf
  233. 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"
  234. then
  235. echo "Unable to execute ttfautohint on the Hack-Italic variant subset. Build canceled." 1>&2
  236. exit 1
  237. fi
  238. echo "master_ttf/Hack-Italic.ttf subset - successful hinting with ttfautohint"
  239. # Hack-BoldItalic.ttf
  240. 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"
  241. then
  242. echo "Unable to execute ttfautohint on the Hack-BoldItalic variant subset. Build canceled." 1>&2
  243. exit 1
  244. fi
  245. echo "master_ttf/Hack-BoldItalic.ttf subset - successful hinting with ttfautohint"
  246. echo " "
  247. # /////////////////////////////////////////////
  248. #
  249. #
  250. # Build woff subsets
  251. #
  252. #
  253. # /////////////////////////////////////////////
  254. # regular set
  255. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$REGULAR_TTF"; then
  256. echo "Failed to build $REGULAR_WOFF from $REGULAR_TTF." 1>&2
  257. exit 1
  258. else
  259. echo "Regular woff subset successfully built from $REGULAR_TTF"
  260. fi
  261. # bold set
  262. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$BOLD_TTF"; then
  263. echo "Failed to build $BOLD_WOFF from $BOLD_TTF" 1>&2
  264. exit 1
  265. else
  266. echo "Bold woff subset successfully built from $BOLD_TTF"
  267. fi
  268. # italic set
  269. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$ITALIC_TTF"; then
  270. echo "Failed to build $BOLD_WOFF from $ITALIC_TTF" 1>&2
  271. exit 1
  272. else
  273. echo "Italic woff subset successfully built from $ITALIC_TTF"
  274. fi
  275. # bold italic set
  276. if ! "$SFNTWOFF_BIN" -n $ZOPFLI_ITERATIONS "$TTF_BUILD/$BOLDITALIC_TTF"; then
  277. echo "Failed to build $BOLDITALIC_WOFF from $BOLDITALIC_TTF" 1>&2
  278. exit 1
  279. else
  280. echo "Bold Italic woff subset successfully built from $BOLDITALIC_TTF"
  281. fi
  282. # /////////////////////////////////////////////
  283. #
  284. #
  285. # Build woff2 subsets
  286. #
  287. #
  288. # /////////////////////////////////////////////
  289. echo " "
  290. # regular set
  291. if ! "$WOFF2_BIN" "$TTF_BUILD/$REGULAR_TTF"; then
  292. echo "Failed to build woff2 subset from $REGULAR_TTF." 1>&2
  293. exit 1
  294. else
  295. echo "Regular woff2 font subset successfully built from $REGULAR_TTF"
  296. fi
  297. # bold set
  298. if ! "$WOFF2_BIN" "$TTF_BUILD/$BOLD_TTF"; then
  299. echo "Failed to build woff2 subset from $BOLD_TTF" 1>&2
  300. exit 1
  301. else
  302. echo "Bold woff2 subset successfully built from $BOLD_TTF"
  303. fi
  304. # italic set
  305. if ! "$WOFF2_BIN" "$TTF_BUILD/$ITALIC_TTF"; then
  306. echo "Failed to build woff2 subset from $ITALIC_TTF" 1>&2
  307. exit 1
  308. else
  309. echo "Italic woff2 subset successfully built from $ITALIC_TTF"
  310. fi
  311. # bold italic set
  312. if ! "$WOFF2_BIN" "$TTF_BUILD/$BOLDITALIC_TTF"; then
  313. echo "Failed to build woff2 subset from $BOLDITALIC_TTF" 1>&2
  314. exit 1
  315. else
  316. echo "Bold Italic woff2 subset successfully built from $BOLDITALIC_TTF"
  317. fi
  318. # //////////////////////////////////////////////
  319. #
  320. #
  321. # Move web font subset files to build directory
  322. #
  323. #
  324. # //////////////////////////////////////////////
  325. # create the build directory if it does not exist
  326. if ! [ -d "$WEB_BUILD" ]; then
  327. mkdir $WEB_BUILD
  328. fi
  329. echo " "
  330. echo "Moving woff files to build directory..."
  331. # move woff files to appropriate build directory
  332. mv "$TTF_BUILD/$REGULAR_WOFF_PRE" "$WEB_BUILD/$REGULAR_WOFF"
  333. mv "$TTF_BUILD/$BOLD_WOFF_PRE" "$WEB_BUILD/$BOLD_WOFF"
  334. mv "$TTF_BUILD/$ITALIC_WOFF_PRE" "$WEB_BUILD/$ITALIC_WOFF"
  335. mv "$TTF_BUILD/$BOLDITALIC_WOFF_PRE" "$WEB_BUILD/$BOLDITALIC_WOFF"
  336. if [ -f "$WEB_BUILD/$REGULAR_WOFF" ]; then
  337. echo "Regular woff build path: $WEB_BUILD/$REGULAR_WOFF"
  338. fi
  339. if [ -f "$WEB_BUILD/$BOLD_WOFF" ]; then
  340. echo "Bold woff build path: $WEB_BUILD/$BOLD_WOFF"
  341. fi
  342. if [ -f "$WEB_BUILD/$ITALIC_WOFF" ]; then
  343. echo "Italic woff build path: $WEB_BUILD/$ITALIC_WOFF"
  344. fi
  345. if [ -f "$WEB_BUILD/$BOLDITALIC_WOFF" ]; then
  346. echo "Bold Italic woff build path: $WEB_BUILD/$BOLDITALIC_WOFF"
  347. fi
  348. echo "Moving woff2 files to build directory..."
  349. # move woff files to appropriate build directory
  350. mv "$TTF_BUILD/$REGULAR_WOFF2_PRE" "$WEB_BUILD/$REGULAR_WOFF2"
  351. mv "$TTF_BUILD/$BOLD_WOFF2_PRE" "$WEB_BUILD/$BOLD_WOFF2"
  352. mv "$TTF_BUILD/$ITALIC_WOFF2_PRE" "$WEB_BUILD/$ITALIC_WOFF2"
  353. mv "$TTF_BUILD/$BOLDITALIC_WOFF2_PRE" "$WEB_BUILD/$BOLDITALIC_WOFF2"
  354. if [ -f "$WEB_BUILD/$REGULAR_WOFF2" ]; then
  355. echo "Regular woff2 subset build path: $WEB_BUILD/$REGULAR_WOFF2"
  356. fi
  357. if [ -f "$WEB_BUILD/$BOLD_WOFF2" ]; then
  358. echo "Bold woff2 subset build path: $WEB_BUILD/$BOLD_WOFF2"
  359. fi
  360. if [ -f "$WEB_BUILD/$ITALIC_WOFF2" ]; then
  361. echo "Italic woff2 subset build path: $WEB_BUILD/$ITALIC_WOFF2"
  362. fi
  363. if [ -f "$WEB_BUILD/$BOLDITALIC_WOFF2" ]; then
  364. echo "Bold Italic woff2 subset build path: $WEB_BUILD/$BOLDITALIC_WOFF2"
  365. fi
  366. # //////////////////////////////////////////////
  367. #
  368. #
  369. # Cleanup temp directory
  370. #
  371. #
  372. # //////////////////////////////////////////////
  373. rm -rf master_ttf
  374. rm -rf "$TEMP_SOURCE"