2014-07-16 19:32:51 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-10-25 20:31:22 +02:00
|
|
|
dir=`mktemp -d`
|
2014-07-16 19:32:51 +02:00
|
|
|
|
2018-01-18 22:34:13 +01:00
|
|
|
out=/dev/stdout
|
|
|
|
if test "x${1:0:3}" == 'x-o='; then
|
|
|
|
out=${1:3}
|
|
|
|
shift
|
|
|
|
fi
|
2014-07-16 19:32:51 +02:00
|
|
|
hb_shape=$1
|
2014-07-17 21:27:46 +02:00
|
|
|
shift
|
|
|
|
fontfile=$1
|
2015-07-23 13:14:03 +02:00
|
|
|
if test "x${fontfile:0:1}" == 'x-'; then
|
|
|
|
echo "Specify font file before other options." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-07-17 21:27:46 +02:00
|
|
|
shift
|
2015-07-23 13:14:03 +02:00
|
|
|
if ! echo "$hb_shape" | grep -q 'hb-shape'; then
|
|
|
|
echo "Specify hb-shape (not hb-view, etc)." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
options=
|
|
|
|
have_text=false
|
|
|
|
for arg in "$@"; do
|
|
|
|
if test "x${arg:0:1}" == 'x-'; then
|
|
|
|
if echo "$arg" | grep -q ' '; then
|
|
|
|
echo "Space in argument is not supported: '$arg'." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
options="$options${options:+ }$arg"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if $have_text; then
|
|
|
|
echo "Too many arguments found... Use '=' notation for options: '$arg'" >&2
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
text="$arg"
|
|
|
|
have_text=true
|
|
|
|
done
|
|
|
|
if ! $have_text; then
|
|
|
|
text=`cat`
|
|
|
|
fi
|
2015-11-05 00:30:48 +01:00
|
|
|
unicodes=`echo "$text" | ./hb-unicode-decode`
|
2015-07-23 13:14:03 +02:00
|
|
|
glyphs=`echo "$text" | $hb_shape $options "$fontfile"`
|
|
|
|
if test $? != 0; then
|
|
|
|
echo "hb-shape failed." >&2
|
|
|
|
exit 2
|
|
|
|
fi
|
2017-10-26 16:29:28 +02:00
|
|
|
glyph_names=`echo "$text" | $hb_shape $options --no-clusters --no-positions "$fontfile" | sed 's/[][]//g; s/|/,/g'`
|
2014-07-16 19:32:51 +02:00
|
|
|
|
|
|
|
cp "$fontfile" "$dir/font.ttf"
|
2017-10-04 14:47:10 +02:00
|
|
|
fonttools subset \
|
2014-07-16 19:32:51 +02:00
|
|
|
--glyph-names \
|
2015-11-05 02:37:30 +01:00
|
|
|
--no-hinting \
|
2017-08-09 03:37:03 +02:00
|
|
|
--layout-features='*' \
|
2014-07-16 19:32:51 +02:00
|
|
|
"$dir/font.ttf" \
|
2017-10-26 16:29:28 +02:00
|
|
|
--glyphs="$glyph_names" \
|
2014-07-16 19:32:51 +02:00
|
|
|
--text="$text"
|
2017-03-26 09:22:34 +02:00
|
|
|
if ! test -s "$dir/font.subset.ttf"; then
|
|
|
|
echo "Subsetter didn't produce nonempty subset font in $dir/font.subset.ttf" >&2
|
2014-07-16 19:32:51 +02:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Verify that subset font produces same glyphs!
|
2017-03-26 09:22:34 +02:00
|
|
|
glyphs_subset=`echo "$text" | $hb_shape $options "$dir/font.subset.ttf"`
|
2014-07-16 19:32:51 +02:00
|
|
|
|
|
|
|
if ! test "x$glyphs" = "x$glyphs_subset"; then
|
|
|
|
echo "Subset font produced different glyphs!" >&2
|
|
|
|
echo "Perhaps font doesn't have glyph names; checking visually..." >&2
|
2014-08-01 00:33:11 +02:00
|
|
|
hb_view=${hb_shape/shape/view}
|
2015-07-23 13:14:03 +02:00
|
|
|
echo "$text" | $hb_view $options "$dir/font.ttf" --output-format=png --output-file="$dir/orig.png"
|
2017-03-26 09:22:34 +02:00
|
|
|
echo "$text" | $hb_view $options "$dir/font.subset.ttf" --output-format=png --output-file="$dir/subset.png"
|
2014-07-16 19:32:51 +02:00
|
|
|
if ! cmp "$dir/orig.png" "$dir/subset.png"; then
|
|
|
|
echo "Images differ. Please inspect $dir/*.png." >&2
|
2018-01-18 22:34:13 +01:00
|
|
|
echo "$glyphs" >> "$out"
|
|
|
|
echo "$glyphs_subset" >> "$out"
|
2014-07-16 19:32:51 +02:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
echo "Yep; all good." >&2
|
|
|
|
rm -f "$dir/orig.png"
|
|
|
|
rm -f "$dir/subset.png"
|
|
|
|
glyphs=$glyphs_subset
|
|
|
|
fi
|
|
|
|
|
2017-03-26 09:22:34 +02:00
|
|
|
sha1sum=`sha1sum "$dir/font.subset.ttf" | cut -d' ' -f1`
|
2018-01-18 22:34:13 +01:00
|
|
|
subset="data/in-house/fonts/$sha1sum.ttf"
|
2017-03-26 09:22:34 +02:00
|
|
|
mv "$dir/font.subset.ttf" "$subset"
|
2014-07-16 19:32:51 +02:00
|
|
|
|
2015-11-05 00:30:48 +01:00
|
|
|
# There ought to be an easier way to do this, but it escapes me...
|
|
|
|
unicodes_file=`mktemp`
|
|
|
|
glyphs_file=`mktemp`
|
|
|
|
echo "$unicodes" > "$unicodes_file"
|
|
|
|
echo "$glyphs" > "$glyphs_file"
|
|
|
|
# Open the "file"s
|
|
|
|
exec 3<"$unicodes_file"
|
|
|
|
exec 4<"$glyphs_file"
|
2018-01-18 22:34:13 +01:00
|
|
|
relative_subset="$subset"
|
|
|
|
if test "$out" != "/dev/stdout"; then
|
|
|
|
relative_subset="$(/usr/bin/python -c 'import os, sys; print (os.path.relpath (sys.argv[1], sys.argv[2]))' "$subset" "$(dirname "$out")")"
|
|
|
|
fi
|
2015-11-05 00:30:48 +01:00
|
|
|
while read uline <&3 && read gline <&4; do
|
2018-01-18 22:34:13 +01:00
|
|
|
echo "$relative_subset:$options:$uline:$gline" >> "$out"
|
2015-11-05 00:30:48 +01:00
|
|
|
done
|
|
|
|
|
2014-07-16 19:32:51 +02:00
|
|
|
|
|
|
|
rm -f "$dir/font.ttf"
|
|
|
|
rmdir "$dir"
|