• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3dir=`mktemp -d`
4
5if which sha1sum 2>/dev/null >/dev/null; then
6	SHA1SUM=sha1sum
7elif which shasum 2>/dev/null >/dev/null; then
8	SHA1SUM='shasum -a 1'
9elif which digest 2>/dev/null >/dev/null; then
10	SHA1SUM='digest -a sha1'
11else
12	echo "'sha1sum' not found"
13	exit 2
14fi
15
16out=/dev/stdout
17if test "x$1" == 'x-o'; then
18	shift
19	out=$1
20	shift
21fi
22hb_shape=$1
23shift
24fontfile=$1
25if test "x${fontfile:0:1}" == 'x-'; then
26	echo "Specify font file before other options." >&2
27	exit 1
28fi
29shift
30if ! echo "$hb_shape" | grep -q 'hb-shape'; then
31	echo "Specify hb-shape (not hb-view, etc): got "$hb_shape"." >&2
32	exit 1
33fi
34options=
35have_text=false
36for arg in "$@"; do
37	if test "x${arg:0:1}" == 'x-'; then
38		if echo "$arg" | grep -q ' '; then
39			echo "Space in argument is not supported: '$arg'." >&2
40			exit 1
41		fi
42		options="$options${options:+ }$arg"
43		continue
44	fi
45	if $have_text; then
46		echo "Too many arguments found...  Use '=' notation for options: '$arg'" >&2
47		exit 1;
48	fi
49	text="$arg"
50	have_text=true
51done
52if ! $have_text; then
53	text=`cat`
54fi
55unicodes=`echo "$text" | ./hb-unicode-decode`
56glyphs=`echo "$text" | $hb_shape $options "$fontfile"`
57if test $? != 0; then
58	echo "hb-shape failed." >&2
59	exit 2
60fi
61glyph_ids=`echo "$text" | $hb_shape $options --no-glyph-names --no-clusters --no-positions "$fontfile" | sed 's/[][]//g; s/|/,/g'`
62
63cp "$fontfile" "$dir/font.ttf"
64echo fonttools subset \
65	--glyph-names \
66	--no-hinting \
67	--layout-features='*' \
68	--gids="$glyph_ids" \
69	--text="$text" \
70	--output-file="$dir/font.subset.ttf" \
71	"$dir/font.ttf"
72fonttools subset \
73	--glyph-names \
74	--no-hinting \
75	--layout-features='*' \
76	--gids="$glyph_ids" \
77	--text="$text" \
78	--output-file="$dir/font.subset.ttf" \
79	"$dir/font.ttf"
80if ! test -s "$dir/font.subset.ttf"; then
81	echo "Subsetter didn't produce nonempty subset font in $dir/font.subset.ttf" >&2
82	exit 2
83fi
84
85# Verify that subset font produces same glyphs!
86glyphs_subset=`echo "$text" | $hb_shape $options "$dir/font.subset.ttf"`
87
88if ! test "x$glyphs" = "x$glyphs_subset"; then
89	echo "Subset font produced different glyphs!" >&2
90	echo "Perhaps font doesn't have glyph names; checking visually..." >&2
91	hb_view=${hb_shape/shape/view}
92	echo "$text" | $hb_view $options "$dir/font.ttf" --output-format=png --output-file="$dir/orig.png"
93	echo "$text" | $hb_view $options "$dir/font.subset.ttf" --output-format=png --output-file="$dir/subset.png"
94	if ! cmp "$dir/orig.png" "$dir/subset.png"; then
95		echo "Images differ.  Please inspect $dir/*.png." >&2
96		echo "$glyphs" >> "$out"
97		echo "$glyphs_subset" >> "$out"
98		exit 2
99	fi
100	echo "Yep; all good." >&2
101	rm -f "$dir/orig.png"
102	rm -f "$dir/subset.png"
103	glyphs=$glyphs_subset
104fi
105
106sha1sum=`$SHA1SUM "$dir/font.subset.ttf" | cut -d' ' -f1`
107subset="data/in-house/fonts/$sha1sum.ttf"
108mv "$dir/font.subset.ttf" "$subset"
109
110# There ought to be an easier way to do this, but it escapes me...
111unicodes_file=`mktemp`
112glyphs_file=`mktemp`
113echo "$unicodes" > "$unicodes_file"
114echo "$glyphs" > "$glyphs_file"
115# Open the "file"s
116exec 3<"$unicodes_file"
117exec 4<"$glyphs_file"
118relative_subset="$subset"
119if test "$out" != "/dev/stdout"; then
120	relative_subset="$(/usr/bin/env python3 -c 'import os, sys; print (os.path.relpath (sys.argv[1], sys.argv[2]))' "$subset" "$(dirname "$out")")"
121fi
122while read uline <&3 && read gline <&4; do
123	echo "$relative_subset;$options;$uline;$gline" >> "$out"
124done
125
126
127rm -f "$dir/font.ttf"
128rmdir "$dir"
129