• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3dir=`mktemp -d`
4
5hb_shape=$1
6shift
7fontfile=$1
8if test "x${fontfile:0:1}" == 'x-'; then
9	echo "Specify font file before other options." >&2
10	exit 1
11fi
12shift
13if ! echo "$hb_shape" | grep -q 'hb-shape'; then
14	echo "Specify hb-shape (not hb-view, etc)." >&2
15	exit 1
16fi
17options=
18have_text=false
19for 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
34done
35if ! $have_text; then
36	text=`cat`
37fi
38unicodes=`echo "$text" | ./hb-unicode-decode`
39glyphs=`echo "$text" | $hb_shape $options "$fontfile"`
40if test $? != 0; then
41	echo "hb-shape failed." >&2
42	exit 2
43fi
44glyph_names=`echo "$text" | $hb_shape $options --no-clusters --no-positions "$fontfile" | sed 's/[][]//g; s/|/,/g'`
45
46cp "$fontfile" "$dir/font.ttf"
47fonttools subset \
48	--glyph-names \
49	--no-hinting \
50	--layout-features='*' \
51	"$dir/font.ttf" \
52	--glyphs="$glyph_names" \
53	--text="$text"
54if ! test -s "$dir/font.subset.ttf"; then
55	echo "Subsetter didn't produce nonempty subset font in $dir/font.subset.ttf" >&2
56	exit 2
57fi
58
59# Verify that subset font produces same glyphs!
60glyphs_subset=`echo "$text" | $hb_shape $options "$dir/font.subset.ttf"`
61
62if ! test "x$glyphs" = "x$glyphs_subset"; then
63	echo "Subset font produced different glyphs!" >&2
64	echo "Perhaps font doesn't have glyph names; checking visually..." >&2
65	hb_view=${hb_shape/shape/view}
66	echo "$text" | $hb_view $options "$dir/font.ttf" --output-format=png --output-file="$dir/orig.png"
67	echo "$text" | $hb_view $options "$dir/font.subset.ttf" --output-format=png --output-file="$dir/subset.png"
68	if ! cmp "$dir/orig.png" "$dir/subset.png"; then
69		echo "Images differ.  Please inspect $dir/*.png." >&2
70		echo "$glyphs"
71		echo "$glyphs_subset"
72		exit 2
73	fi
74	echo "Yep; all good." >&2
75	rm -f "$dir/orig.png"
76	rm -f "$dir/subset.png"
77	glyphs=$glyphs_subset
78fi
79
80sha1sum=`sha1sum "$dir/font.subset.ttf" | cut -d' ' -f1`
81subset="fonts/sha1sum/$sha1sum.ttf"
82mv "$dir/font.subset.ttf" "$subset"
83
84# There ought to be an easier way to do this, but it escapes me...
85unicodes_file=`mktemp`
86glyphs_file=`mktemp`
87echo "$unicodes" > "$unicodes_file"
88echo "$glyphs" > "$glyphs_file"
89# Open the "file"s
90exec 3<"$unicodes_file"
91exec 4<"$glyphs_file"
92while read uline <&3 && read gline <&4; do
93	echo "$subset:$options:$uline:$gline"
94done
95
96
97rm -f "$dir/font.ttf"
98rmdir "$dir"
99