[test] Add script for extracting tests from text-rendering-tests
authorBehdad Esfahbod <behdad@behdad.org>
Wed, 10 Jan 2018 00:54:12 +0000 (01:54 +0100)
committerBehdad Esfahbod <behdad@behdad.org>
Wed, 10 Jan 2018 01:50:49 +0000 (02:50 +0100)
Work in progress...

https://github.com/unicode-org/text-rendering-tests

test/shaping/data/text-rendering-tests/extract-tests.py [new file with mode: 0755]

diff --git a/test/shaping/data/text-rendering-tests/extract-tests.py b/test/shaping/data/text-rendering-tests/extract-tests.py
new file mode 100755 (executable)
index 0000000..cb003db
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+import sys
+import xml.etree.ElementTree as ET
+
+# Can we extract this from HTML element itself? I couldn't.
+namespaces = {
+       'ft': 'https://github.com/OpenType/fonttest',
+       'xlink': 'http://www.w3.org/1999/xlink',
+}
+def ns(s):
+       ns,s = s.split(':')
+       return '{%s}%s' % (namespaces[ns], s)
+
+def unistr(s):
+       return ','.join('U+%04X' % ord(c) for c in s)
+
+def glyphstr(glyphs):
+       out = []
+       for glyphname,x,y in glyphs:
+               if x or y:
+                       out.append('%s@%d,%d' % (glyphname, x, y))
+               else:
+                       out.append(glyphname)
+       return '['+'|'.join(out)+']'
+
+html = ET.fromstring(sys.stdin.read())
+found = False
+for elt in html.findall(".//*[@class='expected'][@ft:id]", namespaces):
+       found = True
+       name = elt.get(ns('ft:id'))
+       text = elt.get(ns('ft:render'))
+       font = elt.get(ns('ft:font'))
+       glyphs = []
+       for use in elt.findall(".//use"):
+               x = int(use.get('x'))
+               y = int(use.get('y'))
+               href = use.get(ns('xlink:href'))
+               assert href[0] == '#'
+               glyphname = '.'.join(href[1:].split('/')[1].split('.')[1:])
+               glyphs.append((glyphname, x, y))
+       print("../fonts/%s:--font-size=1000 --no-clusters --no-advances:%s:%s" % (font, unistr(text), glyphstr(glyphs)))
+
+sys.exit(0 if found else 1)