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