Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / test / shaping / data / text-rendering-tests / extract-tests.py
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4 import sys
5 import xml.etree.ElementTree as ET
6
7 # Can we extract this from HTML element itself? I couldn't.
8 namespaces = {
9         'ft': 'https://github.com/OpenType/fonttest',
10         'xlink': 'http://www.w3.org/1999/xlink',
11 }
12 def ns(s):
13         ns,s = s.split(':')
14         return '{%s}%s' % (namespaces[ns], s)
15
16 def unistr(s):
17         return ','.join('U+%04X' % ord(c) for c in s)
18
19 def glyphstr(glyphs):
20         out = []
21         for glyphname,x,y in glyphs:
22                 if x or y:
23                         out.append('%s@%d,%d' % (glyphname, x, y))
24                 else:
25                         out.append(glyphname)
26         return '['+'|'.join(out)+']'
27
28 html = ET.fromstring(sys.stdin.read())
29 found = False
30 for elt in html.findall(".//*[@class='expected'][@ft:id]", namespaces):
31         found = True
32         name = elt.get(ns('ft:id'))
33         text = elt.get(ns('ft:render'))
34         font = elt.get(ns('ft:font'))
35         vars = elt.get(ns('ft:var'), '').replace(':', '=').replace(';', ',')
36         glyphs = []
37         for use in elt.findall(".//use"):
38                 x = int(use.get('x'))
39                 y = int(use.get('y'))
40                 href = use.get(ns('xlink:href'))
41                 assert href[0] == '#'
42                 glyphname = '.'.join(href[1:].split('/')[1].split('.')[1:])
43                 glyphs.append((glyphname, x, y))
44         opts = '--font-size=1000 --ned --remove-default-ignorables --font-funcs=ft'
45         if vars:
46                 opts = opts + ' --variations=%s' % vars
47         print ("../fonts/%s:%s:%s:%s" % (font, opts, unistr(text), glyphstr(glyphs)))
48
49 sys.exit(0 if found else 1)