generate-expected-outputs.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. # Pre-generates the expected output subset files (via fonttools) for
  3. # specified subset test suite(s).
  4. import os
  5. import sys
  6. import shutil
  7. import io
  8. import re
  9. import tempfile
  10. from difflib import unified_diff
  11. from fontTools.ttLib import TTFont
  12. from subprocess import check_call
  13. from subset_test_suite import SubsetTestSuite
  14. def usage():
  15. print("Usage: generate-expected-outputs.py hb-subset <test suite file> ...")
  16. def strip_check_sum (ttx_string):
  17. return re.sub ('checkSumAdjustment value=["]0x([0-9a-fA-F])+["]',
  18. 'checkSumAdjustment value="0x00000000"',
  19. ttx_string, count=1)
  20. def generate_expected_output(input_file, unicodes, profile_flags, instance_flags, output_directory, font_name):
  21. input_path = input_file
  22. if instance_flags:
  23. instance_path = os.path.join(tempfile.mkdtemp (), font_name)
  24. args = ["fonttools", "varLib.instancer",
  25. "--no-overlap-flag",
  26. "--no-recalc-bounds",
  27. "--no-recalc-timestamp",
  28. "--output=%s" % instance_path,
  29. input_file]
  30. args.extend(instance_flags)
  31. check_call(args)
  32. input_path = instance_path
  33. fonttools_path = os.path.join(tempfile.mkdtemp (), font_name)
  34. args = ["fonttools", "subset", input_path]
  35. args.extend(["--drop-tables+=DSIG",
  36. "--drop-tables-=sbix",
  37. "--no-harfbuzz-repacker", # disable harfbuzz repacker so we aren't comparing to ourself.
  38. "--unicodes=%s" % unicodes,
  39. "--output-file=%s" % fonttools_path])
  40. args.extend(profile_flags)
  41. check_call(args)
  42. with io.StringIO () as fp:
  43. with TTFont (fonttools_path) as font:
  44. font.saveXML (fp)
  45. fonttools_ttx = strip_check_sum (fp.getvalue ())
  46. harfbuzz_path = os.path.join(tempfile.mkdtemp (), font_name)
  47. args = [
  48. hb_subset,
  49. "--font-file=" + input_file,
  50. "--output-file=" + harfbuzz_path,
  51. "--unicodes=%s" % unicodes,
  52. "--drop-tables+=DSIG",
  53. "--drop-tables-=sbix"]
  54. args.extend(profile_flags)
  55. if instance_flags:
  56. args.extend(["--instance=%s" % ','.join(instance_flags)])
  57. check_call(args)
  58. with io.StringIO () as fp:
  59. with TTFont (harfbuzz_path) as font:
  60. font.saveXML (fp)
  61. harfbuzz_ttx = strip_check_sum (fp.getvalue ())
  62. if harfbuzz_ttx != fonttools_ttx:
  63. for line in unified_diff (fonttools_ttx.splitlines (1), harfbuzz_ttx.splitlines (1), fonttools_path, harfbuzz_path):
  64. sys.stdout.write (line)
  65. sys.stdout.flush ()
  66. raise Exception ('ttx for fonttools and harfbuzz does not match.')
  67. output_path = os.path.join(output_directory, font_name)
  68. shutil.copy(harfbuzz_path, output_path)
  69. args = sys.argv[1:]
  70. if not args:
  71. usage()
  72. hb_subset, args = args[0], args[1:]
  73. if not args:
  74. usage()
  75. for path in args:
  76. with open(path, mode="r", encoding="utf-8") as f:
  77. test_suite = SubsetTestSuite(path, f.read())
  78. output_directory = test_suite.get_output_directory()
  79. print("Generating output files for %s" % output_directory)
  80. for test in test_suite.tests():
  81. unicodes = test.unicodes()
  82. font_name = test.get_font_name()
  83. print("Creating subset %s/%s" % (output_directory, font_name))
  84. generate_expected_output(test.font_path, unicodes, test.get_profile_flags(),
  85. test.get_instance_flags(), output_directory, font_name)