1#!/bin/bash
2
3dir=`mktemp --directory`
4
5hb_shape=$1
6shift
7fontfile=$1
8shift
9hb_shape="$hb_shape $@"
10unicodes=`./hb-unicode-decode`
11text=`./hb-unicode-encode "$unicodes"`
12glyphs=`echo "$text" | $hb_shape "$fontfile"`
13
14cp "$fontfile" "$dir/font.ttf"
15pyftsubset \
16	--glyph-names \
17	"$dir/font.ttf" \
18	--text="$text"
19if ! test -s "$dir/font.ttf.subset"; then
20	echo "Subsetter didn't produce nonempty subset font in $dir/font.ttf.subset" >&2
21	exit 2
22fi
23
24# Verify that subset font produces same glyphs!
25glyphs_subset=`echo "$text" | $hb_shape "$dir/font.ttf.subset"`
26
27if ! test "x$glyphs" = "x$glyphs_subset"; then
28	echo "Subset font produced different glyphs!" >&2
29	echo "Perhaps font doesn't have glyph names; checking visually..." >&2
30	hb_view=${hb_shape//shape/view}
31	echo "$text" | $hb_view "$dir/font.ttf" --output-format=png --output-file="$dir/orig.png"
32	echo "$text" | $hb_view "$dir/font.ttf.subset" --output-format=png --output-file="$dir/subset.png"
33	if ! cmp "$dir/orig.png" "$dir/subset.png"; then
34		echo "Images differ.  Please inspect $dir/*.png." >&2
35		echo "$glyphs"
36		echo "$glyphs_subset"
37		exit 2
38	fi
39	echo "Yep; all good." >&2
40	rm -f "$dir/orig.png"
41	rm -f "$dir/subset.png"
42	glyphs=$glyphs_subset
43fi
44
45sha1sum=`sha1sum "$dir/font.ttf.subset" | cut -d' ' -f1`
46subset="fonts/sha1sum/$sha1sum.ttf"
47mv "$dir/font.ttf.subset" "$subset"
48
49echo "$subset:$unicodes:$glyphs"
50
51rm -f "$dir/font.ttf"
52rmdir "$dir"
53