Blame test/shaping/record-test.sh

Packit 874993
#!/bin/bash
Packit 874993
Packit 874993
dir=`mktemp --directory`
Packit 874993
Packit 874993
hb_shape=$1
Packit 874993
shift
Packit 874993
fontfile=$1
Packit 874993
if test "x${fontfile:0:1}" == 'x-'; then
Packit 874993
	echo "Specify font file before other options." >&2
Packit 874993
	exit 1
Packit 874993
fi
Packit 874993
shift
Packit 874993
if ! echo "$hb_shape" | grep -q 'hb-shape'; then
Packit 874993
	echo "Specify hb-shape (not hb-view, etc)." >&2
Packit 874993
	exit 1
Packit 874993
fi
Packit 874993
options=
Packit 874993
have_text=false
Packit 874993
for arg in "$@"; do
Packit 874993
	if test "x${arg:0:1}" == 'x-'; then
Packit 874993
		if echo "$arg" | grep -q ' '; then
Packit 874993
			echo "Space in argument is not supported: '$arg'." >&2
Packit 874993
			exit 1
Packit 874993
		fi
Packit 874993
		options="$options${options:+ }$arg"
Packit 874993
		continue
Packit 874993
	fi
Packit 874993
	if $have_text; then
Packit 874993
		echo "Too many arguments found...  Use '=' notation for options: '$arg'" >&2
Packit 874993
		exit 1;
Packit 874993
	fi
Packit 874993
	text="$arg"
Packit 874993
	have_text=true
Packit 874993
done
Packit 874993
if ! $have_text; then
Packit 874993
	text=`cat`
Packit 874993
fi
Packit 874993
unicodes=`echo "$text" | ./hb-unicode-decode`
Packit 874993
glyphs=`echo "$text" | $hb_shape $options "$fontfile"`
Packit 874993
if test $? != 0; then
Packit 874993
	echo "hb-shape failed." >&2
Packit 874993
	exit 2
Packit 874993
fi
Packit 874993
Packit 874993
cp "$fontfile" "$dir/font.ttf"
Packit 874993
pyftsubset \
Packit 874993
	--glyph-names \
Packit 874993
	--no-hinting \
Packit 874993
	"$dir/font.ttf" \
Packit 874993
	--text="$text"
Packit 874993
if ! test -s "$dir/font.subset.ttf"; then
Packit 874993
	echo "Subsetter didn't produce nonempty subset font in $dir/font.subset.ttf" >&2
Packit 874993
	exit 2
Packit 874993
fi
Packit 874993
Packit 874993
# Verify that subset font produces same glyphs!
Packit 874993
glyphs_subset=`echo "$text" | $hb_shape $options "$dir/font.subset.ttf"`
Packit 874993
Packit 874993
if ! test "x$glyphs" = "x$glyphs_subset"; then
Packit 874993
	echo "Subset font produced different glyphs!" >&2
Packit 874993
	echo "Perhaps font doesn't have glyph names; checking visually..." >&2
Packit 874993
	hb_view=${hb_shape/shape/view}
Packit 874993
	echo "$text" | $hb_view $options "$dir/font.ttf" --output-format=png --output-file="$dir/orig.png"
Packit 874993
	echo "$text" | $hb_view $options "$dir/font.subset.ttf" --output-format=png --output-file="$dir/subset.png"
Packit 874993
	if ! cmp "$dir/orig.png" "$dir/subset.png"; then
Packit 874993
		echo "Images differ.  Please inspect $dir/*.png." >&2
Packit 874993
		echo "$glyphs"
Packit 874993
		echo "$glyphs_subset"
Packit 874993
		exit 2
Packit 874993
	fi
Packit 874993
	echo "Yep; all good." >&2
Packit 874993
	rm -f "$dir/orig.png"
Packit 874993
	rm -f "$dir/subset.png"
Packit 874993
	glyphs=$glyphs_subset
Packit 874993
fi
Packit 874993
Packit 874993
sha1sum=`sha1sum "$dir/font.subset.ttf" | cut -d' ' -f1`
Packit 874993
subset="fonts/sha1sum/$sha1sum.ttf"
Packit 874993
mv "$dir/font.subset.ttf" "$subset"
Packit 874993
Packit 874993
# There ought to be an easier way to do this, but it escapes me...
Packit 874993
unicodes_file=`mktemp`
Packit 874993
glyphs_file=`mktemp`
Packit 874993
echo "$unicodes" > "$unicodes_file"
Packit 874993
echo "$glyphs" > "$glyphs_file"
Packit 874993
# Open the "file"s
Packit 874993
exec 3<"$unicodes_file"
Packit 874993
exec 4<"$glyphs_file"
Packit 874993
while read uline <&3 && read gline <&4; do
Packit 874993
	echo "$subset:$options:$uline:$gline"
Packit 874993
done
Packit 874993
Packit 874993
Packit 874993
rm -f "$dir/font.ttf"
Packit 874993
rmdir "$dir"