Branch
Hash :
97103688
Author :
Date :
2025-07-11T00:30:48
[perf/hb-draw-compare] Default font-funcs2 to fontations Yay!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
#!/bin/bash
trap "exit" INT
dirname=`dirname "$0"`
# Parse options
output_dir=
font_file=
face_index=0
hb_view=hb-view
hb_info=hb-info
hb_svg_compare=$dirname/hb-svg-compare
tolerance=0
font_size=upem
features=
variations=
face_loader=ot
shaper=ot
font_funcs1=ot
font_funcs2=fontations
clear1=false
clear2=false
unicodes=
glyphs=
quiet=false
help=false
while test $# -gt 0; do
case "$1" in
-o|--output-dir)
shift
output_dir=$1
shift
;;
--font-file)
shift
font_file=$1
shift
;;
--face-index)
shift
face_index=$1
shift
;;
--hb-view)
shift
hb_view=$1
shift
;;
--hb-info)
shift
hb_info=$1
shift
;;
--hb-svg-compare)
shift
hb_svg_compare=$1
shift
;;
--tolerance)
shift
tolerance=$1
shift
;;
--font-size)
shift
font_size=$1
shift
;;
--features)
shift
features=$1
shift
;;
--variations)
shift
variations=$1
shift
;;
--unicodes)
shift
unicodes=$1
shift
;;
--face-loader)
shift
face_loader=$1
shift
;;
--shaper)
shift
shaper=$1
shift
;;
--font-funcs1)
shift
font_funcs1=$1
shift
;;
--font-funcs2)
shift
font_funcs2=$1
shift
;;
--clear1)
shift
clear1=true
;;
--clear2)
shift
clear2=true
;;
--quiet)
quiet=true
shift
;;
--help)
help=true
shift
;;
*)
if test "x$font_file" == x; then
font_file=$1
shift
else
glyphs="$glyphs $1"
shift
fi
;;
esac
done
if $help; then
cmd=`basename "$0"`
echo "Usage: $cmd [OPTIONS] FONTFILE [GLYPH...]"
echo "Render a font with two font backends and compare the results."
echo
echo "Options:"
echo " -o, --output-dir DIR: Output in DIR"
echo " --font-file FONTFILE: Font file to render"
echo " --hb-view HB_VIEW: Path to hb-view; default $hb_view"
echo " --hb-info HB_INFO: Path to hb-info; default $hb_info"
echo " --hb-svg-compare HB_SVG_COMPARE: Path to hb-svg-compare; default $hb_svg_compare"
echo " --tolerance TOLERANCE: Tolerance for SVG comparison; default $tolerance"
echo " --font-size SIZE: Font size; default $font_size"
echo " --features FEATURES: Font features; default none"
echo " --variations VARIATIONS: Font variations; default none"
echo " --face-loader: Face loader; default $face_loader"
echo " --shaper: Shaper; default $shaper"
echo " --font-funcs0 font_funcs: First font-funcs; default $font_funcs1"
echo " --font-funcs1 font_funcs: Second font-funcs; default $font_funcs2"
echo " --clear1: Clear first font backend output if exists"
echo " --clear2: Clear second font backend output if exists"
echo " --unicodes CODES: Unicodes to render"
echo " --quiet: Quiet mode"
echo " --help: Print help"
exit 0
fi
if test "x$font_file" == x; then
echo "No font file specified." >&2
exit 2
fi
if ! which "$hb_view" 2>/dev/null >/dev/null; then
echo "'$hb_view' not found" >&2
exit 2
fi
if ! which "$hb_info" 2>/dev/null >/dev/null; then
echo "'$hb_info' not found" >&2
exit 2
fi
if ! test -f "$font_file"; then
echo "Font file '$font_file' not found" >&2
exit 2
fi
# Sanity check Unicode values
if test "x$unicodes" != x; then
echo "$unicodes" |
tr ' ' '\n' |
grep -v '^U[+][0-9a-fA-F]\+$' &&
{
echo "Invalid Unicode values" >&2
exit 2
}
fi
$quiet || echo "Comparing '$font_file' with '$font_funcs1' and '$font_funcs2' font backends. " >&2
if test "x$output_dir" == x; then
output_dir=`mktemp -d`
echo "Output in '$output_dir'" >&2
fi
mkdir -p "$output_dir" || exit 1
# Populate glyphs file
glyphs_file="$output_dir/glyphs"
> "$glyphs_file"
echo "$unicodes" |
sed 's/^U+//' |
grep . |
while read unicode; do
echo "uni$unicode"
done >> "$glyphs_file"
echo "$glyphs" |
tr ' ' '\n' |
grep . >> "$glyphs_file"
if test "x$unicodes" == x -a "x$glyphs" == x; then
$quiet || echo "No unicodes or glyphs specified. Comparing all glyphs in the font." >&2
num_glyphs=`$hb_info --quiet --show-glyph-count "$font_file"`
$quiet || echo "Font has $num_glyphs glyphs." >&2
seq 0 $((num_glyphs - 1)) |
sed 's/^/gid/' >> "$glyphs_file"
fi
num_glyphs=`cat "$glyphs_file" | wc -l`
flavor=
if test "$features" != ""; then
flavor="$flavor.features=$features"
fi
if test "$variations" != ""; then
flavor="$flavor.variations=$variations"
fi
# Render with both font backends
if $clear1; then
funcs_prefix="$output_dir/$font_funcs1$flavor"
$quiet || echo "Clearing '$funcs_prefix'... " >&2
rm -rf "$funcs_prefix"
fi
if $clear2; then
funcs_prefix="$output_dir/$font_funcs2$flavor"
$quiet || echo "Clearing '$funcs_prefix'... " >&2
rm -rf "$funcs_prefix"
fi
for font_funcs in "$font_funcs1" "$font_funcs2"; do
test "x$font_funcs" == x && continue
$quiet || echo "Rendering with font backend '$font_funcs'..." >&2
funcs_prefix="$output_dir/$font_funcs$flavor"
mkdir -p "$funcs_prefix"
count=0
cat "$glyphs_file" |
while read glyph; do
dir="$funcs_prefix"
svg="$dir/$glyph.svg"
if test -f "$svg"; then
continue
fi
count=$((count + 1))
if test $((count % 100)) == 0; then
$quiet || echo -n . >&2
fi
echo \
--font-file="$font_file" \
--face-index=$face_index \
--glyphs \
--face-loader=$face_loader \
--shaper=$this_shaper \
--font-funcs=$font_funcs \
--features="$features" \
--variations="$variations" \
--output-format=svg \
--font-size=$font_size \
--output-file="$svg" \
"$glyph"
done |
sed 's/ /;/g' |
"$hb_view" --batch >/dev/null
$quiet || echo "" >&2
done
test "x$font_funcs1" == x || test "x$font_funcs2" == x && exit 0
diff="$output_dir/diff$flavor"
rm -f "$diff"
> "$diff"
test "x$flavor" == x || ln -f -s "diff$flavor" "$output_dir/diff"
$quiet || echo "Comparing SVGs into '$diff'..." >&2
funcs1_prefix="$output_dir/$font_funcs1$flavor"
funcs2_prefix="$output_dir/$font_funcs2$flavor"
count=0
cat "$glyphs_file" |
while read glyph; do
count=$((count + 1))
if test $((count % 100)) == 0; then
$quiet || echo -n . >&2
fi
svg1="$funcs1_prefix/$glyph.svg"
svg2="$funcs2_prefix/$glyph.svg"
if ! test -f "$svg1" || ! test -f "$svg2"; then
echo -e "\n$glyph not rendered." >&2
exit 1
fi
echo -e "$svg1\t$svg2"
done |
"$hb_svg_compare" "$tolerance" |
while read line; do
$quiet || echo -e "\n$line"
echo "$line" >> "$diff"
done
# Sort diff by error
sort -n -o "$diff.tmp" "$diff" && mv "$diff.tmp" "$diff" || exit 1
# Count number of differences
num_diffs=`cat "$diff" | wc -l`
$quiet || echo -e "\nFound $num_diffs differences in $num_glyphs glyphs." >&2