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