Debian Codegraph @

tests/test_shaping_harfbuzz.py

raw
1 from fontFeatures import FontFeatures
2 from fontFeatures.shaperLib.Buffer import Buffer
3 from fontFeatures.shaperLib.Shaper import Shaper
4 from fontFeatures.ttLib import unparse
5 from fontTools.ttLib import TTFont
6 from babelfont import load
7 import pytest
8 import glob
9 import os
10 import re
11 import logging
12
13 if "CI" in os.environ:
14 pytest.skip("Not running shaping tests on CI", allow_module_level=True)
15
16 script_map = {"Qaag": "Myanmar_Zawgyi"}
17
18 # logging.getLogger("fontFeatures.shaperLib").setLevel(logging.DEBUG)
19
20
21 def tounicode(s):
22 out = ""
23 for part in s.split(","):
24 if part.startswith("U+"):
25 part = part[2:]
26 out = out + chr(int(part, 16))
27 return out
28
29
30 tests = []
31 for testfile in glob.glob("tests/harfbuzz/*/tests/*.tests"):
32 filetests = open(testfile, "r").readlines()
33 for ix, t in enumerate(filetests):
34 t = t.rstrip()
35 if not t:
36 continue
37 if t[0] == "#":
38 continue
39 # t = t[1:]
40 testname = testfile[:-6] + "_" + str(ix) # ".tests"
41 font, hb_args, buf, expectation = t.split(":")
42 font = os.path.join(os.path.dirname(testfile), font)
43 if not os.path.exists(font):
44 continue
45 tests.append(pytest.param(font, hb_args, buf, expectation, id=testname))
46
47
48 @pytest.mark.parametrize("fontname,hb_args,input_string,expectation", tests)
49 def test_shaping(request, fontname, hb_args, input_string, expectation):
50 font = TTFont(fontname)
51 ff = unparse(font)
52 bbf = load(fontname)
53 if not bbf:
54 return pytest.skip("Font too busted to use")
55 buf = Buffer(bbf, unicodes=tounicode(input_string))
56 shaper = Shaper(ff, bbf)
57 feature_string = ""
58 if "[" in hb_args:
59 return pytest.skip("Partial application not supported yet")
60
61 if "variations" in hb_args:
62 return pytest.skip("Not planning to support variations")
63 if "ttb" in hb_args:
64 return pytest.skip("Not planning to support top-to-bottom")
65
66 if "rand" in request.node.name:
67 return pytest.skip("Not planning to support rand feature")
68 if "aat" in request.node.name:
69 return pytest.skip("Not planning to support Apple Advanced Typography")
70 if "kern-format2" in request.node.name:
71 return pytest.skip("Not planning to support kern table")
72 if "fraction" in request.node.name:
73 return pytest.skip("Not planning to support automatic fractions")
74
75 m = re.search(r'--features="([^"]+)"', hb_args)
76 if m:
77 feature_string = m[1]
78
79 if "--script" in hb_args:
80 m = re.search(r"--script=(\w+)", hb_args)
81 buf.script = script_map[m[1]]
82
83 shaper.execute(buf, features=feature_string)
84 serialize_options = {}
85 if "--no-glyph-names" in hb_args:
86 serialize_options["names"] = False
87 if "--ned" in hb_args:
88 serialize_options["ned"] = True
89 if "--no-position" in hb_args:
90 serialize_options["position"] = False
91 if "post" not in font or font["post"].formatType != 2.0:
92 serialize_options["names"] = False
93 expectation = re.sub("gid", "", expectation)
94 serialized = buf.serialize(**serialize_options)
95
96 # Finesse cluster information
97 serialized = re.sub(r"=\d+", "", serialized)
98 expectation = re.sub(r"=\d+", "", expectation)
99 assert "[" + serialized + "]" == expectation