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