93ebcfc99ca2d3541e5a7b7e0e827993114314c2
[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_ids=`echo "$text" | $hb_shape $options --no-glyph-names --no-clusters --no-positions "$fontfile" | sed 's/[][]//g; s/|/,/g'`
50
51 cp "$fontfile" "$dir/font.ttf"
52 echo fonttools subset \
53         --glyph-names \
54         --no-hinting \
55         --layout-features='*' \
56         "$dir/font.ttf" \
57         --gids="$glyph_ids" \
58         --text="$text"
59 fonttools subset \
60         --glyph-names \
61         --no-hinting \
62         --layout-features='*' \
63         "$dir/font.ttf" \
64         --gids="$glyph_ids" \
65         --text="$text"
66 if ! test -s "$dir/font.subset.ttf"; then
67         echo "Subsetter didn't produce nonempty subset font in $dir/font.subset.ttf" >&2
68         exit 2
69 fi
70
71 # Verify that subset font produces same glyphs!
72 glyphs_subset=`echo "$text" | $hb_shape $options "$dir/font.subset.ttf"`
73
74 if ! test "x$glyphs" = "x$glyphs_subset"; then
75         echo "Subset font produced different glyphs!" >&2
76         echo "Perhaps font doesn't have glyph names; checking visually..." >&2
77         hb_view=${hb_shape/shape/view}
78         echo "$text" | $hb_view $options "$dir/font.ttf" --output-format=png --output-file="$dir/orig.png"
79         echo "$text" | $hb_view $options "$dir/font.subset.ttf" --output-format=png --output-file="$dir/subset.png"
80         if ! cmp "$dir/orig.png" "$dir/subset.png"; then
81                 echo "Images differ.  Please inspect $dir/*.png." >&2
82                 echo "$glyphs" >> "$out"
83                 echo "$glyphs_subset" >> "$out"
84                 exit 2
85         fi
86         echo "Yep; all good." >&2
87         rm -f "$dir/orig.png"
88         rm -f "$dir/subset.png"
89         glyphs=$glyphs_subset
90 fi
91
92 sha1sum=`sha1sum "$dir/font.subset.ttf" | cut -d' ' -f1`
93 subset="data/in-house/fonts/$sha1sum.ttf"
94 mv "$dir/font.subset.ttf" "$subset"
95
96 # There ought to be an easier way to do this, but it escapes me...
97 unicodes_file=`mktemp`
98 glyphs_file=`mktemp`
99 echo "$unicodes" > "$unicodes_file"
100 echo "$glyphs" > "$glyphs_file"
101 # Open the "file"s
102 exec 3<"$unicodes_file"
103 exec 4<"$glyphs_file"
104 relative_subset="$subset"
105 if test "$out" != "/dev/stdout"; then
106         relative_subset="$(/usr/bin/python -c 'import os, sys; print (os.path.relpath (sys.argv[1], sys.argv[2]))' "$subset" "$(dirname "$out")")"
107 fi
108 while read uline <&3 && read gline <&4; do
109         echo "$relative_subset:$options:$uline:$gline" >> "$out"
110 done
111
112
113 rm -f "$dir/font.ttf"
114 rmdir "$dir"