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