• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3dir=`mktemp --directory`
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
44
45cp "$fontfile" "$dir/font.ttf"
46pyftsubset \
47	--glyph-names \
48	--no-hinting \
49	"$dir/font.ttf" \
50	--text="$text"
51if ! 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
54fi
55
56# Verify that subset font produces same glyphs!
57glyphs_subset=`echo "$text" | $hb_shape $options "$dir/font.ttf.subset"`
58
59if ! 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
75fi
76
77sha1sum=`sha1sum "$dir/font.ttf.subset" | cut -d' ' -f1`
78subset="fonts/sha1sum/$sha1sum.ttf"
79mv "$dir/font.ttf.subset" "$subset"
80
81# There ought to be an easier way to do this, but it escapes me...
82unicodes_file=`mktemp`
83glyphs_file=`mktemp`
84echo "$unicodes" > "$unicodes_file"
85echo "$glyphs" > "$glyphs_file"
86# Open the "file"s
87exec 3<"$unicodes_file"
88exec 4<"$glyphs_file"
89while read uline <&3 && read gline <&4; do
90	echo "$subset:$options:$uline:$gline"
91done
92
93
94rm -f "$dir/font.ttf"
95rmdir "$dir"
96