record-test.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/bash
  2. dir=`mktemp -d`
  3. if which sha1sum 2>/dev/null >/dev/null; then
  4. SHA1SUM=sha1sum
  5. elif which shasum 2>/dev/null >/dev/null; then
  6. SHA1SUM='shasum -a 1'
  7. elif which digest 2>/dev/null >/dev/null; then
  8. SHA1SUM='digest -a sha1'
  9. else
  10. echo "'sha1sum' not found"
  11. exit 2
  12. fi
  13. out=/dev/stdout
  14. if test "x$1" == 'x-o'; then
  15. shift
  16. out=$1
  17. shift
  18. fi
  19. hb_subset=$1
  20. shift
  21. hb_shape=$1
  22. shift
  23. fontfile=$1
  24. if test "x${fontfile:0:1}" == 'x-'; then
  25. echo "Specify font file before other options." >&2
  26. exit 1
  27. fi
  28. shift
  29. if ! echo "$hb_subset" | grep -q 'subset'; then
  30. echo "Specify hb-subset (or \"fonttools subset\"): got '$hb_subset'." >&2
  31. exit 1
  32. fi
  33. if ! echo "$hb_shape" | grep -q 'hb-shape'; then
  34. echo "Specify hb-shape (not hb-view, etc): got '$hb_shape'." >&2
  35. exit 1
  36. fi
  37. options=
  38. have_text=false
  39. for arg in "$@"; do
  40. if test "x${arg:0:1}" == 'x-'; then
  41. if echo "$arg" | grep -q ' '; then
  42. echo "Space in argument is not supported: '$arg'." >&2
  43. exit 1
  44. fi
  45. options="$options${options:+ }$arg"
  46. continue
  47. fi
  48. if $have_text; then
  49. echo "Too many arguments found... Use '=' notation for options: '$arg'" >&2
  50. exit 1;
  51. fi
  52. text="$arg"
  53. have_text=true
  54. done
  55. if ! $have_text; then
  56. text=`cat`
  57. fi
  58. unicodes=`echo "$text" | ./hb-unicode-decode`
  59. glyphs=`echo "$text" | $hb_shape $options "$fontfile"`
  60. if test $? != 0; then
  61. echo "hb-shape failed." >&2
  62. exit 2
  63. fi
  64. glyph_ids=`echo "$text" | $hb_shape $options --no-glyph-names --no-clusters --no-positions "$fontfile" | sed 's/[][]//g; s/|/,/g'`
  65. cp "$fontfile" "$dir/font.ttf"
  66. echo $hb_subset \
  67. --glyph-names \
  68. --no-hinting \
  69. --layout-features='*' \
  70. --gids="$glyph_ids" \
  71. --text="$text" \
  72. --output-file="$dir/font.subset.ttf" \
  73. "$dir/font.ttf"
  74. $hb_subset \
  75. --glyph-names \
  76. --no-hinting \
  77. --layout-features='*' \
  78. --gids="$glyph_ids" \
  79. --text="$text" \
  80. --output-file="$dir/font.subset.ttf" \
  81. "$dir/font.ttf"
  82. if ! test -s "$dir/font.subset.ttf"; then
  83. echo "Subsetter didn't produce nonempty subset font in $dir/font.subset.ttf" >&2
  84. exit 2
  85. fi
  86. # Verify that subset font produces same glyphs!
  87. glyphs_subset=`echo "$text" | $hb_shape $options "$dir/font.subset.ttf"`
  88. if ! test "x$glyphs" = "x$glyphs_subset"; then
  89. echo "Subset font produced different glyphs!" >&2
  90. echo "Perhaps font doesn't have glyph names; checking visually..." >&2
  91. hb_view=${hb_shape/shape/view}
  92. echo "$text" | $hb_view $options "$dir/font.ttf" --output-format=png --output-file="$dir/orig.png"
  93. echo "$text" | $hb_view $options "$dir/font.subset.ttf" --output-format=png --output-file="$dir/subset.png"
  94. if ! cmp "$dir/orig.png" "$dir/subset.png"; then
  95. echo "Images differ. Please inspect $dir/*.png." >&2
  96. echo "$glyphs" >> "$out"
  97. echo "$glyphs_subset" >> "$out"
  98. exit 2
  99. fi
  100. echo "Yep; all good." >&2
  101. rm -f "$dir/orig.png"
  102. rm -f "$dir/subset.png"
  103. glyphs=$glyphs_subset
  104. fi
  105. sha1sum=`$SHA1SUM "$dir/font.subset.ttf" | cut -d' ' -f1`
  106. subset="data/in-house/fonts/$sha1sum.ttf"
  107. mv "$dir/font.subset.ttf" "$subset"
  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. rm -f "$dir/font.ttf"
  124. rmdir "$dir"