update.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python3
  2. import sys, os, subprocess, shutil, glob
  3. import xml.etree.ElementTree as ET
  4. # Can we extract this from HTML element itself? I couldn't.
  5. namespaces = {
  6. 'ft': 'https://github.com/OpenType/fonttest',
  7. 'xlink': 'http://www.w3.org/1999/xlink',
  8. }
  9. def ns (s):
  10. ns,s = s.split(':')
  11. return '{%s}%s' % (namespaces[ns], s)
  12. def unistr (s):
  13. return ','.join('U+%04X' % ord(c) for c in s)
  14. def glyphstr (glyphs):
  15. out = []
  16. for glyphname, x, y in glyphs:
  17. if x or y:
  18. out.append ('%s@%d,%d' % (glyphname, x, y))
  19. else:
  20. out.append (glyphname)
  21. return '[' + '|'.join (out) + ']'
  22. def extract_tests (input):
  23. html = ET.fromstring (input)
  24. found = False
  25. result = []
  26. for elt in html.findall (".//*[@class='expected'][@ft:id]", namespaces):
  27. found = True
  28. name = elt.get (ns ('ft:id'))
  29. text = elt.get (ns ('ft:render'))
  30. font = elt.get (ns ('ft:font'))
  31. variations = elt.get (ns ('ft:var'), '').replace (':', '=').replace (';', ',')
  32. glyphs = []
  33. for use in elt.findall (".//use"):
  34. x = int (use.get ('x'))
  35. y = int (use.get ('y'))
  36. href = use.get (ns ('xlink:href'))
  37. assert href[0] == '#'
  38. glyphname = '.'.join (href[1:].split ('/')[1].split ('.')[1:])
  39. glyphs.append ((glyphname, x, y))
  40. opts = '--font-size=1000 --ned --remove-default-ignorables --font-funcs=ft'
  41. if variations:
  42. opts = opts + ' --variations=%s' % variations
  43. result.append ("../fonts/%s:%s:%s:%s" % (font, opts, unistr(text), glyphstr(glyphs)))
  44. for elt in html.findall (".//*[@class='expected-no-crash'][@ft:id]", namespaces):
  45. found = True
  46. name = elt.get (ns ('ft:id'))
  47. text = elt.get (ns ('ft:render'))
  48. font = elt.get (ns ('ft:font'))
  49. variations = elt.get (ns ('ft:var'), '').replace (':', '=').replace (';', ',')
  50. opts = ''
  51. if variations:
  52. opts = '--variations=%s' % variations
  53. result.append ("../fonts/%s:%s:%s:*" % (font, opts, unistr (text)))
  54. assert found
  55. return '\n'.join (result) + '\n'
  56. os.chdir (os.getenv ('srcdir', os.path.dirname (__file__)))
  57. git = shutil.which ('git')
  58. assert git
  59. if os.path.isdir ('./text-rendering-tests'):
  60. subprocess.run ([git, 'pull'], cwd='text-rendering-tests', check=True)
  61. else:
  62. subprocess.run ([git, 'clone', 'https://github.com/unicode-org/text-rendering-tests'], check=True)
  63. shutil.rmtree ('fonts', ignore_errors=True)
  64. assert not os.path.exists ('fonts')
  65. shutil.copytree ('text-rendering-tests/fonts', 'fonts')
  66. subprocess.run([git, 'add', 'fonts'], check=True)
  67. shutil.rmtree ('tests', ignore_errors=True)
  68. assert not os.path.isdir('tests')
  69. os.mkdir ('tests')
  70. with open ('DISABLED', 'r') as f: disabled = f.read ()
  71. tests = []
  72. disabled_tests = []
  73. for x in sorted (os.listdir ('text-rendering-tests/testcases')):
  74. if not x.endswith ('.html') or x == 'index.html': continue
  75. out = 'tests/%s.tests' % x.split('.html')[0]
  76. with open ('text-rendering-tests/testcases/' + x, 'r') as f: content = f.read ()
  77. with open (out, 'w') as f: f.write (extract_tests (content))
  78. if out in disabled:
  79. disabled_tests.append (out)
  80. else:
  81. tests.append (out)
  82. subprocess.run([git, 'add', 'tests'], check=True)
  83. with open ('meson.build', 'w') as f: f.write ('\n'.join (
  84. ['text_rendering_tests = ['] +
  85. [' \'%s\',' % x.split('tests/')[1] for x in tests] +
  86. [']', '', 'disabled_text_rendering_tests = ['] +
  87. [' \'%s\',' % x.split('tests/')[1] for x in disabled_tests] +
  88. [']', '']
  89. ))
  90. subprocess.run([git, 'add', 'meson.build'], check=True)
  91. print ('Updated the testsuit, now run `git commit -e -m "[test/text-rendering-tests] Update from upstream"`')