1#!/bin/bash 2 3dir=`mktemp -d` 4 5out=/dev/stdout 6if test "x$1" == 'x-o'; then 7 shift 8 out=$1 9 shift 10fi 11hb_shape=$1 12shift 13fontfile=$1 14if test "x${fontfile:0:1}" == 'x-'; then 15 echo "Specify font file before other options." >&2 16 exit 1 17fi 18shift 19if ! echo "$hb_shape" | grep -q 'hb-shape'; then 20 echo "Specify hb-shape (not hb-view, etc): got "$hb_shape"." >&2 21 exit 1 22fi 23options= 24have_text=false 25for arg in "$@"; do 26 if test "x${arg:0:1}" == 'x-'; then 27 if echo "$arg" | grep -q ' '; then 28 echo "Space in argument is not supported: '$arg'." >&2 29 exit 1 30 fi 31 options="$options${options:+ }$arg" 32 continue 33 fi 34 if $have_text; then 35 echo "Too many arguments found... Use '=' notation for options: '$arg'" >&2 36 exit 1; 37 fi 38 text="$arg" 39 have_text=true 40done 41if ! $have_text; then 42 text=`cat` 43fi 44unicodes=`echo "$text" | ./hb-unicode-decode` 45glyphs=`echo "$text" | $hb_shape $options "$fontfile"` 46if test $? != 0; then 47 echo "hb-shape failed." >&2 48 exit 2 49fi 50glyph_ids=`echo "$text" | $hb_shape $options --no-glyph-names --no-clusters --no-positions "$fontfile" | sed 's/[][]//g; s/|/,/g'` 51 52cp "$fontfile" "$dir/font.ttf" 53echo fonttools subset \ 54 --glyph-names \ 55 --no-hinting \ 56 --layout-features='*' \ 57 "$dir/font.ttf" \ 58 --gids="$glyph_ids" \ 59 --text="$text" 60fonttools subset \ 61 --glyph-names \ 62 --no-hinting \ 63 --layout-features='*' \ 64 "$dir/font.ttf" \ 65 --gids="$glyph_ids" \ 66 --text="$text" 67if ! test -s "$dir/font.subset.ttf"; then 68 echo "Subsetter didn't produce nonempty subset font in $dir/font.subset.ttf" >&2 69 exit 2 70fi 71 72# Verify that subset font produces same glyphs! 73glyphs_subset=`echo "$text" | $hb_shape $options "$dir/font.subset.ttf"` 74 75if ! test "x$glyphs" = "x$glyphs_subset"; then 76 echo "Subset font produced different glyphs!" >&2 77 echo "Perhaps font doesn't have glyph names; checking visually..." >&2 78 hb_view=${hb_shape/shape/view} 79 echo "$text" | $hb_view $options "$dir/font.ttf" --output-format=png --output-file="$dir/orig.png" 80 echo "$text" | $hb_view $options "$dir/font.subset.ttf" --output-format=png --output-file="$dir/subset.png" 81 if ! cmp "$dir/orig.png" "$dir/subset.png"; then 82 echo "Images differ. Please inspect $dir/*.png." >&2 83 echo "$glyphs" >> "$out" 84 echo "$glyphs_subset" >> "$out" 85 exit 2 86 fi 87 echo "Yep; all good." >&2 88 rm -f "$dir/orig.png" 89 rm -f "$dir/subset.png" 90 glyphs=$glyphs_subset 91fi 92 93sha1sum=`sha1sum "$dir/font.subset.ttf" | cut -d' ' -f1` 94subset="data/in-house/fonts/$sha1sum.ttf" 95mv "$dir/font.subset.ttf" "$subset" 96 97# There ought to be an easier way to do this, but it escapes me... 98unicodes_file=`mktemp` 99glyphs_file=`mktemp` 100echo "$unicodes" > "$unicodes_file" 101echo "$glyphs" > "$glyphs_file" 102# Open the "file"s 103exec 3<"$unicodes_file" 104exec 4<"$glyphs_file" 105relative_subset="$subset" 106if test "$out" != "/dev/stdout"; then 107 relative_subset="$(/usr/bin/python -c 'import os, sys; print (os.path.relpath (sys.argv[1], sys.argv[2]))' "$subset" "$(dirname "$out")")" 108fi 109while read uline <&3 && read gline <&4; do 110 echo "$relative_subset:$options:$uline:$gline" >> "$out" 111done 112 113 114rm -f "$dir/font.ttf" 115rmdir "$dir" 116