xref: /aosp_15_r20/external/fonttools/Tests/mtiLib/mti_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.misc.xmlWriter import XMLWriter
2*e1fe3e4aSElliott Hughesfrom fontTools.ttLib import TTFont
3*e1fe3e4aSElliott Hughesfrom fontTools.feaLib.lookupDebugInfo import LOOKUP_DEBUG_ENV_VAR
4*e1fe3e4aSElliott Hughesfrom fontTools import mtiLib
5*e1fe3e4aSElliott Hughesimport difflib
6*e1fe3e4aSElliott Hughesfrom io import StringIO
7*e1fe3e4aSElliott Hughesimport os
8*e1fe3e4aSElliott Hughesimport sys
9*e1fe3e4aSElliott Hughesimport pytest
10*e1fe3e4aSElliott Hughes
11*e1fe3e4aSElliott Hughes
12*e1fe3e4aSElliott Hughes@pytest.fixture(autouse=True)
13*e1fe3e4aSElliott Hughesdef set_lookup_debug_env_var(monkeypatch):
14*e1fe3e4aSElliott Hughes    monkeypatch.setenv(LOOKUP_DEBUG_ENV_VAR, "1")
15*e1fe3e4aSElliott Hughes
16*e1fe3e4aSElliott Hughes
17*e1fe3e4aSElliott Hughesclass MtiTest:
18*e1fe3e4aSElliott Hughes    GLYPH_ORDER = [
19*e1fe3e4aSElliott Hughes        ".notdef",
20*e1fe3e4aSElliott Hughes        "a",
21*e1fe3e4aSElliott Hughes        "b",
22*e1fe3e4aSElliott Hughes        "pakannada",
23*e1fe3e4aSElliott Hughes        "phakannada",
24*e1fe3e4aSElliott Hughes        "vakannada",
25*e1fe3e4aSElliott Hughes        "pevowelkannada",
26*e1fe3e4aSElliott Hughes        "phevowelkannada",
27*e1fe3e4aSElliott Hughes        "vevowelkannada",
28*e1fe3e4aSElliott Hughes        "uvowelsignkannada",
29*e1fe3e4aSElliott Hughes        "uuvowelsignkannada",
30*e1fe3e4aSElliott Hughes        "uvowelsignaltkannada",
31*e1fe3e4aSElliott Hughes        "uuvowelsignaltkannada",
32*e1fe3e4aSElliott Hughes        "uuvowelsignsinh",
33*e1fe3e4aSElliott Hughes        "uvowelsignsinh",
34*e1fe3e4aSElliott Hughes        "rakarsinh",
35*e1fe3e4aSElliott Hughes        "zero",
36*e1fe3e4aSElliott Hughes        "one",
37*e1fe3e4aSElliott Hughes        "two",
38*e1fe3e4aSElliott Hughes        "three",
39*e1fe3e4aSElliott Hughes        "four",
40*e1fe3e4aSElliott Hughes        "five",
41*e1fe3e4aSElliott Hughes        "six",
42*e1fe3e4aSElliott Hughes        "seven",
43*e1fe3e4aSElliott Hughes        "eight",
44*e1fe3e4aSElliott Hughes        "nine",
45*e1fe3e4aSElliott Hughes        "slash",
46*e1fe3e4aSElliott Hughes        "fraction",
47*e1fe3e4aSElliott Hughes        "A",
48*e1fe3e4aSElliott Hughes        "B",
49*e1fe3e4aSElliott Hughes        "C",
50*e1fe3e4aSElliott Hughes        "fi",
51*e1fe3e4aSElliott Hughes        "fl",
52*e1fe3e4aSElliott Hughes        "breve",
53*e1fe3e4aSElliott Hughes        "acute",
54*e1fe3e4aSElliott Hughes        "uniFB01",
55*e1fe3e4aSElliott Hughes        "ffi",
56*e1fe3e4aSElliott Hughes        "grave",
57*e1fe3e4aSElliott Hughes        "commaacent",
58*e1fe3e4aSElliott Hughes        "dotbelow",
59*e1fe3e4aSElliott Hughes        "dotabove",
60*e1fe3e4aSElliott Hughes        "cedilla",
61*e1fe3e4aSElliott Hughes        "commaaccent",
62*e1fe3e4aSElliott Hughes        "Acircumflex",
63*e1fe3e4aSElliott Hughes        "V",
64*e1fe3e4aSElliott Hughes        "T",
65*e1fe3e4aSElliott Hughes        "acircumflex",
66*e1fe3e4aSElliott Hughes        "Aacute",
67*e1fe3e4aSElliott Hughes        "Agrave",
68*e1fe3e4aSElliott Hughes        "O",
69*e1fe3e4aSElliott Hughes        "Oacute",
70*e1fe3e4aSElliott Hughes        "Ograve",
71*e1fe3e4aSElliott Hughes        "Ocircumflex",
72*e1fe3e4aSElliott Hughes        "aacute",
73*e1fe3e4aSElliott Hughes        "agrave",
74*e1fe3e4aSElliott Hughes        "aimatrabindigurmukhi",
75*e1fe3e4aSElliott Hughes        "aimatragurmukhi",
76*e1fe3e4aSElliott Hughes        "aimatratippigurmukhi",
77*e1fe3e4aSElliott Hughes        "aumatrabindigurmukhi",
78*e1fe3e4aSElliott Hughes        "aumatragurmukhi",
79*e1fe3e4aSElliott Hughes        "bindigurmukhi",
80*e1fe3e4aSElliott Hughes        "eematrabindigurmukhi",
81*e1fe3e4aSElliott Hughes        "eematragurmukhi",
82*e1fe3e4aSElliott Hughes        "eematratippigurmukhi",
83*e1fe3e4aSElliott Hughes        "oomatrabindigurmukhi",
84*e1fe3e4aSElliott Hughes        "oomatragurmukhi",
85*e1fe3e4aSElliott Hughes        "oomatratippigurmukhi",
86*e1fe3e4aSElliott Hughes        "lagurmukhi",
87*e1fe3e4aSElliott Hughes        "lanuktagurmukhi",
88*e1fe3e4aSElliott Hughes        "nagurmukhi",
89*e1fe3e4aSElliott Hughes        "nanuktagurmukhi",
90*e1fe3e4aSElliott Hughes        "ngagurmukhi",
91*e1fe3e4aSElliott Hughes        "nganuktagurmukhi",
92*e1fe3e4aSElliott Hughes        "nnagurmukhi",
93*e1fe3e4aSElliott Hughes        "nnanuktagurmukhi",
94*e1fe3e4aSElliott Hughes        "tthagurmukhi",
95*e1fe3e4aSElliott Hughes        "tthanuktagurmukhi",
96*e1fe3e4aSElliott Hughes        "bsuperior",
97*e1fe3e4aSElliott Hughes        "isuperior",
98*e1fe3e4aSElliott Hughes        "vsuperior",
99*e1fe3e4aSElliott Hughes        "wsuperior",
100*e1fe3e4aSElliott Hughes        "periodsuperior",
101*e1fe3e4aSElliott Hughes        "osuperior",
102*e1fe3e4aSElliott Hughes        "tsuperior",
103*e1fe3e4aSElliott Hughes        "dollarsuperior",
104*e1fe3e4aSElliott Hughes        "fsuperior",
105*e1fe3e4aSElliott Hughes        "gsuperior",
106*e1fe3e4aSElliott Hughes        "zsuperior",
107*e1fe3e4aSElliott Hughes        "dsuperior",
108*e1fe3e4aSElliott Hughes        "psuperior",
109*e1fe3e4aSElliott Hughes        "hsuperior",
110*e1fe3e4aSElliott Hughes        "oesuperior",
111*e1fe3e4aSElliott Hughes        "aesuperior",
112*e1fe3e4aSElliott Hughes        "centsuperior",
113*e1fe3e4aSElliott Hughes        "esuperior",
114*e1fe3e4aSElliott Hughes        "lsuperior",
115*e1fe3e4aSElliott Hughes        "qsuperior",
116*e1fe3e4aSElliott Hughes        "csuperior",
117*e1fe3e4aSElliott Hughes        "asuperior",
118*e1fe3e4aSElliott Hughes        "commasuperior",
119*e1fe3e4aSElliott Hughes        "xsuperior",
120*e1fe3e4aSElliott Hughes        "egravesuperior",
121*e1fe3e4aSElliott Hughes        "usuperior",
122*e1fe3e4aSElliott Hughes        "rsuperior",
123*e1fe3e4aSElliott Hughes        "nsuperior",
124*e1fe3e4aSElliott Hughes        "ssuperior",
125*e1fe3e4aSElliott Hughes        "msuperior",
126*e1fe3e4aSElliott Hughes        "jsuperior",
127*e1fe3e4aSElliott Hughes        "ysuperior",
128*e1fe3e4aSElliott Hughes        "ksuperior",
129*e1fe3e4aSElliott Hughes        "guilsinglright",
130*e1fe3e4aSElliott Hughes        "guilsinglleft",
131*e1fe3e4aSElliott Hughes        "uniF737",
132*e1fe3e4aSElliott Hughes        "uniE11C",
133*e1fe3e4aSElliott Hughes        "uniE11D",
134*e1fe3e4aSElliott Hughes        "uniE11A",
135*e1fe3e4aSElliott Hughes        "uni2077",
136*e1fe3e4aSElliott Hughes        "uni2087",
137*e1fe3e4aSElliott Hughes        "uniE11B",
138*e1fe3e4aSElliott Hughes        "uniE119",
139*e1fe3e4aSElliott Hughes        "uniE0DD",
140*e1fe3e4aSElliott Hughes        "uniE0DE",
141*e1fe3e4aSElliott Hughes        "uniF736",
142*e1fe3e4aSElliott Hughes        "uniE121",
143*e1fe3e4aSElliott Hughes        "uniE122",
144*e1fe3e4aSElliott Hughes        "uniE11F",
145*e1fe3e4aSElliott Hughes        "uni2076",
146*e1fe3e4aSElliott Hughes        "uni2086",
147*e1fe3e4aSElliott Hughes        "uniE120",
148*e1fe3e4aSElliott Hughes        "uniE11E",
149*e1fe3e4aSElliott Hughes        "uniE0DB",
150*e1fe3e4aSElliott Hughes        "uniE0DC",
151*e1fe3e4aSElliott Hughes        "uniF733",
152*e1fe3e4aSElliott Hughes        "uniE12B",
153*e1fe3e4aSElliott Hughes        "uniE12C",
154*e1fe3e4aSElliott Hughes        "uniE129",
155*e1fe3e4aSElliott Hughes        "uni00B3",
156*e1fe3e4aSElliott Hughes        "uni2083",
157*e1fe3e4aSElliott Hughes        "uniE12A",
158*e1fe3e4aSElliott Hughes        "uniE128",
159*e1fe3e4aSElliott Hughes        "uniF732",
160*e1fe3e4aSElliott Hughes        "uniE133",
161*e1fe3e4aSElliott Hughes        "uniE134",
162*e1fe3e4aSElliott Hughes        "uniE131",
163*e1fe3e4aSElliott Hughes        "uni00B2",
164*e1fe3e4aSElliott Hughes        "uni2082",
165*e1fe3e4aSElliott Hughes        "uniE132",
166*e1fe3e4aSElliott Hughes        "uniE130",
167*e1fe3e4aSElliott Hughes        "uniE0F9",
168*e1fe3e4aSElliott Hughes        "uniF734",
169*e1fe3e4aSElliott Hughes        "uniE0D4",
170*e1fe3e4aSElliott Hughes        "uniE0D5",
171*e1fe3e4aSElliott Hughes        "uniE0D2",
172*e1fe3e4aSElliott Hughes        "uni2074",
173*e1fe3e4aSElliott Hughes        "uni2084",
174*e1fe3e4aSElliott Hughes        "uniE0D3",
175*e1fe3e4aSElliott Hughes        "uniE0D1",
176*e1fe3e4aSElliott Hughes        "uniF730",
177*e1fe3e4aSElliott Hughes        "uniE13D",
178*e1fe3e4aSElliott Hughes        "uniE13E",
179*e1fe3e4aSElliott Hughes        "uniE13A",
180*e1fe3e4aSElliott Hughes        "uni2070",
181*e1fe3e4aSElliott Hughes        "uni2080",
182*e1fe3e4aSElliott Hughes        "uniE13B",
183*e1fe3e4aSElliott Hughes        "uniE139",
184*e1fe3e4aSElliott Hughes        "uniE13C",
185*e1fe3e4aSElliott Hughes        "uniF739",
186*e1fe3e4aSElliott Hughes        "uniE0EC",
187*e1fe3e4aSElliott Hughes        "uniE0ED",
188*e1fe3e4aSElliott Hughes        "uniE0EA",
189*e1fe3e4aSElliott Hughes        "uni2079",
190*e1fe3e4aSElliott Hughes        "uni2089",
191*e1fe3e4aSElliott Hughes        "uniE0EB",
192*e1fe3e4aSElliott Hughes        "uniE0E9",
193*e1fe3e4aSElliott Hughes        "uniF735",
194*e1fe3e4aSElliott Hughes        "uniE0CD",
195*e1fe3e4aSElliott Hughes        "uniE0CE",
196*e1fe3e4aSElliott Hughes        "uniE0CB",
197*e1fe3e4aSElliott Hughes        "uni2075",
198*e1fe3e4aSElliott Hughes        "uni2085",
199*e1fe3e4aSElliott Hughes        "uniE0CC",
200*e1fe3e4aSElliott Hughes        "uniE0CA",
201*e1fe3e4aSElliott Hughes        "uniF731",
202*e1fe3e4aSElliott Hughes        "uniE0F3",
203*e1fe3e4aSElliott Hughes        "uniE0F4",
204*e1fe3e4aSElliott Hughes        "uniE0F1",
205*e1fe3e4aSElliott Hughes        "uni00B9",
206*e1fe3e4aSElliott Hughes        "uni2081",
207*e1fe3e4aSElliott Hughes        "uniE0F2",
208*e1fe3e4aSElliott Hughes        "uniE0F0",
209*e1fe3e4aSElliott Hughes        "uniE0F8",
210*e1fe3e4aSElliott Hughes        "uniF738",
211*e1fe3e4aSElliott Hughes        "uniE0C0",
212*e1fe3e4aSElliott Hughes        "uniE0C1",
213*e1fe3e4aSElliott Hughes        "uniE0BE",
214*e1fe3e4aSElliott Hughes        "uni2078",
215*e1fe3e4aSElliott Hughes        "uni2088",
216*e1fe3e4aSElliott Hughes        "uniE0BF",
217*e1fe3e4aSElliott Hughes        "uniE0BD",
218*e1fe3e4aSElliott Hughes        "I",
219*e1fe3e4aSElliott Hughes        "Ismall",
220*e1fe3e4aSElliott Hughes        "t",
221*e1fe3e4aSElliott Hughes        "i",
222*e1fe3e4aSElliott Hughes        "f",
223*e1fe3e4aSElliott Hughes        "IJ",
224*e1fe3e4aSElliott Hughes        "J",
225*e1fe3e4aSElliott Hughes        "IJsmall",
226*e1fe3e4aSElliott Hughes        "Jsmall",
227*e1fe3e4aSElliott Hughes        "tt",
228*e1fe3e4aSElliott Hughes        "ij",
229*e1fe3e4aSElliott Hughes        "j",
230*e1fe3e4aSElliott Hughes        "ffb",
231*e1fe3e4aSElliott Hughes        "ffh",
232*e1fe3e4aSElliott Hughes        "h",
233*e1fe3e4aSElliott Hughes        "ffk",
234*e1fe3e4aSElliott Hughes        "k",
235*e1fe3e4aSElliott Hughes        "ffl",
236*e1fe3e4aSElliott Hughes        "l",
237*e1fe3e4aSElliott Hughes        "fft",
238*e1fe3e4aSElliott Hughes        "fb",
239*e1fe3e4aSElliott Hughes        "ff",
240*e1fe3e4aSElliott Hughes        "fh",
241*e1fe3e4aSElliott Hughes        "fj",
242*e1fe3e4aSElliott Hughes        "fk",
243*e1fe3e4aSElliott Hughes        "ft",
244*e1fe3e4aSElliott Hughes        "janyevoweltelugu",
245*e1fe3e4aSElliott Hughes        "kassevoweltelugu",
246*e1fe3e4aSElliott Hughes        "jaivoweltelugu",
247*e1fe3e4aSElliott Hughes        "nyasubscripttelugu",
248*e1fe3e4aSElliott Hughes        "kaivoweltelugu",
249*e1fe3e4aSElliott Hughes        "ssasubscripttelugu",
250*e1fe3e4aSElliott Hughes        "bayi1",
251*e1fe3e4aSElliott Hughes        "jeemi1",
252*e1fe3e4aSElliott Hughes        "kafi1",
253*e1fe3e4aSElliott Hughes        "ghafi1",
254*e1fe3e4aSElliott Hughes        "laami1",
255*e1fe3e4aSElliott Hughes        "kafm1",
256*e1fe3e4aSElliott Hughes        "ghafm1",
257*e1fe3e4aSElliott Hughes        "laamm1",
258*e1fe3e4aSElliott Hughes        "rayf2",
259*e1fe3e4aSElliott Hughes        "reyf2",
260*e1fe3e4aSElliott Hughes        "yayf2",
261*e1fe3e4aSElliott Hughes        "zayf2",
262*e1fe3e4aSElliott Hughes        "fayi1",
263*e1fe3e4aSElliott Hughes        "ayehf2",
264*e1fe3e4aSElliott Hughes        "hamzayeharabf2",
265*e1fe3e4aSElliott Hughes        "hamzayehf2",
266*e1fe3e4aSElliott Hughes        "yehf2",
267*e1fe3e4aSElliott Hughes        "ray",
268*e1fe3e4aSElliott Hughes        "rey",
269*e1fe3e4aSElliott Hughes        "zay",
270*e1fe3e4aSElliott Hughes        "yay",
271*e1fe3e4aSElliott Hughes        "dal",
272*e1fe3e4aSElliott Hughes        "del",
273*e1fe3e4aSElliott Hughes        "zal",
274*e1fe3e4aSElliott Hughes        "rayf1",
275*e1fe3e4aSElliott Hughes        "reyf1",
276*e1fe3e4aSElliott Hughes        "yayf1",
277*e1fe3e4aSElliott Hughes        "zayf1",
278*e1fe3e4aSElliott Hughes        "ayehf1",
279*e1fe3e4aSElliott Hughes        "hamzayeharabf1",
280*e1fe3e4aSElliott Hughes        "hamzayehf1",
281*e1fe3e4aSElliott Hughes        "yehf1",
282*e1fe3e4aSElliott Hughes        "dal1",
283*e1fe3e4aSElliott Hughes        "del1",
284*e1fe3e4aSElliott Hughes        "zal1",
285*e1fe3e4aSElliott Hughes        "onehalf",
286*e1fe3e4aSElliott Hughes        "onehalf.alt",
287*e1fe3e4aSElliott Hughes        "onequarter",
288*e1fe3e4aSElliott Hughes        "onequarter.alt",
289*e1fe3e4aSElliott Hughes        "threequarters",
290*e1fe3e4aSElliott Hughes        "threequarters.alt",
291*e1fe3e4aSElliott Hughes        "AlefSuperiorNS",
292*e1fe3e4aSElliott Hughes        "DammaNS",
293*e1fe3e4aSElliott Hughes        "DammaRflxNS",
294*e1fe3e4aSElliott Hughes        "DammatanNS",
295*e1fe3e4aSElliott Hughes        "Fatha2dotsNS",
296*e1fe3e4aSElliott Hughes        "FathaNS",
297*e1fe3e4aSElliott Hughes        "FathatanNS",
298*e1fe3e4aSElliott Hughes        "FourDotsAboveNS",
299*e1fe3e4aSElliott Hughes        "HamzaAboveNS",
300*e1fe3e4aSElliott Hughes        "MaddaNS",
301*e1fe3e4aSElliott Hughes        "OneDotAbove2NS",
302*e1fe3e4aSElliott Hughes        "OneDotAboveNS",
303*e1fe3e4aSElliott Hughes        "ShaddaAlefNS",
304*e1fe3e4aSElliott Hughes        "ShaddaDammaNS",
305*e1fe3e4aSElliott Hughes        "ShaddaDammatanNS",
306*e1fe3e4aSElliott Hughes        "ShaddaFathatanNS",
307*e1fe3e4aSElliott Hughes        "ShaddaKasraNS",
308*e1fe3e4aSElliott Hughes        "ShaddaKasratanNS",
309*e1fe3e4aSElliott Hughes        "ShaddaNS",
310*e1fe3e4aSElliott Hughes        "SharetKafNS",
311*e1fe3e4aSElliott Hughes        "SukunNS",
312*e1fe3e4aSElliott Hughes        "ThreeDotsDownAboveNS",
313*e1fe3e4aSElliott Hughes        "ThreeDotsUpAboveNS",
314*e1fe3e4aSElliott Hughes        "TwoDotsAboveNS",
315*e1fe3e4aSElliott Hughes        "TwoDotsVerticalAboveNS",
316*e1fe3e4aSElliott Hughes        "UltapeshNS",
317*e1fe3e4aSElliott Hughes        "WaslaNS",
318*e1fe3e4aSElliott Hughes        "AinIni.12m_MeemFin.02",
319*e1fe3e4aSElliott Hughes        "AinIni_YehBarreeFin",
320*e1fe3e4aSElliott Hughes        "AinMed_YehBarreeFin",
321*e1fe3e4aSElliott Hughes        "BehxIni_MeemFin",
322*e1fe3e4aSElliott Hughes        "BehxIni_NoonGhunnaFin",
323*e1fe3e4aSElliott Hughes        "BehxIni_RehFin",
324*e1fe3e4aSElliott Hughes        "BehxIni_RehFin.b",
325*e1fe3e4aSElliott Hughes        "BehxMed_MeemFin.py",
326*e1fe3e4aSElliott Hughes        "BehxMed_NoonGhunnaFin",
327*e1fe3e4aSElliott Hughes        "BehxMed_NoonGhunnaFin.cup",
328*e1fe3e4aSElliott Hughes        "BehxMed_RehFin",
329*e1fe3e4aSElliott Hughes        "BehxMed_RehFin.cup",
330*e1fe3e4aSElliott Hughes        "BehxMed_YehxFin",
331*e1fe3e4aSElliott Hughes        "FehxMed_YehBarreeFin",
332*e1fe3e4aSElliott Hughes        "HahIni_YehBarreeFin",
333*e1fe3e4aSElliott Hughes        "KafIni_YehBarreeFin",
334*e1fe3e4aSElliott Hughes        "KafMed.12_YehxFin.01",
335*e1fe3e4aSElliott Hughes        "KafMed_MeemFin",
336*e1fe3e4aSElliott Hughes        "KafMed_YehBarreeFin",
337*e1fe3e4aSElliott Hughes        "LamAlefFin",
338*e1fe3e4aSElliott Hughes        "LamAlefFin.cup",
339*e1fe3e4aSElliott Hughes        "LamAlefFin.cut",
340*e1fe3e4aSElliott Hughes        "LamAlefFin.short",
341*e1fe3e4aSElliott Hughes        "LamAlefSep",
342*e1fe3e4aSElliott Hughes        "LamIni_MeemFin",
343*e1fe3e4aSElliott Hughes        "LamIni_YehBarreeFin",
344*e1fe3e4aSElliott Hughes        "LamMed_MeemFin",
345*e1fe3e4aSElliott Hughes        "LamMed_MeemFin.b",
346*e1fe3e4aSElliott Hughes        "LamMed_YehxFin",
347*e1fe3e4aSElliott Hughes        "LamMed_YehxFin.cup",
348*e1fe3e4aSElliott Hughes        "TahIni_YehBarreeFin",
349*e1fe3e4aSElliott Hughes        "null",
350*e1fe3e4aSElliott Hughes        "CR",
351*e1fe3e4aSElliott Hughes        "space",
352*e1fe3e4aSElliott Hughes        "exclam",
353*e1fe3e4aSElliott Hughes        "quotedbl",
354*e1fe3e4aSElliott Hughes        "numbersign",
355*e1fe3e4aSElliott Hughes    ]
356*e1fe3e4aSElliott Hughes
357*e1fe3e4aSElliott Hughes    # Feature files in data/*.txt; output gets compared to data/*.ttx.
358*e1fe3e4aSElliott Hughes    TESTS = {
359*e1fe3e4aSElliott Hughes        None: ("mti/cmap",),
360*e1fe3e4aSElliott Hughes        "cmap": ("mti/cmap",),
361*e1fe3e4aSElliott Hughes        "GSUB": (
362*e1fe3e4aSElliott Hughes            "featurename-backward",
363*e1fe3e4aSElliott Hughes            "featurename-forward",
364*e1fe3e4aSElliott Hughes            "lookupnames-backward",
365*e1fe3e4aSElliott Hughes            "lookupnames-forward",
366*e1fe3e4aSElliott Hughes            "mixed-toplevels",
367*e1fe3e4aSElliott Hughes            "mti/scripttable",
368*e1fe3e4aSElliott Hughes            "mti/chainedclass",
369*e1fe3e4aSElliott Hughes            "mti/chainedcoverage",
370*e1fe3e4aSElliott Hughes            "mti/chained-glyph",
371*e1fe3e4aSElliott Hughes            "mti/gsubalternate",
372*e1fe3e4aSElliott Hughes            "mti/gsubligature",
373*e1fe3e4aSElliott Hughes            "mti/gsubmultiple",
374*e1fe3e4aSElliott Hughes            "mti/gsubreversechanined",
375*e1fe3e4aSElliott Hughes            "mti/gsubsingle",
376*e1fe3e4aSElliott Hughes        ),
377*e1fe3e4aSElliott Hughes        "GPOS": (
378*e1fe3e4aSElliott Hughes            "mti/scripttable",
379*e1fe3e4aSElliott Hughes            "mti/chained-glyph",
380*e1fe3e4aSElliott Hughes            "mti/gposcursive",
381*e1fe3e4aSElliott Hughes            "mti/gposkernset",
382*e1fe3e4aSElliott Hughes            "mti/gposmarktobase",
383*e1fe3e4aSElliott Hughes            "mti/gpospairclass",
384*e1fe3e4aSElliott Hughes            "mti/gpospairglyph",
385*e1fe3e4aSElliott Hughes            "mti/gpossingle",
386*e1fe3e4aSElliott Hughes            "mti/mark-to-ligature",
387*e1fe3e4aSElliott Hughes        ),
388*e1fe3e4aSElliott Hughes        "GDEF": (
389*e1fe3e4aSElliott Hughes            "mti/gdefattach",
390*e1fe3e4aSElliott Hughes            "mti/gdefclasses",
391*e1fe3e4aSElliott Hughes            "mti/gdefligcaret",
392*e1fe3e4aSElliott Hughes            "mti/gdefmarkattach",
393*e1fe3e4aSElliott Hughes            "mti/gdefmarkfilter",
394*e1fe3e4aSElliott Hughes        ),
395*e1fe3e4aSElliott Hughes    }
396*e1fe3e4aSElliott Hughes    # TODO:
397*e1fe3e4aSElliott Hughes    # https://github.com/Monotype/OpenType_Table_Source/issues/12
398*e1fe3e4aSElliott Hughes    #
399*e1fe3e4aSElliott Hughes    #        'mti/featuretable'
400*e1fe3e4aSElliott Hughes    #        'mti/contextclass'
401*e1fe3e4aSElliott Hughes    #        'mti/contextcoverage'
402*e1fe3e4aSElliott Hughes    #        'mti/context-glyph'
403*e1fe3e4aSElliott Hughes
404*e1fe3e4aSElliott Hughes    @staticmethod
405*e1fe3e4aSElliott Hughes    def getpath(testfile):
406*e1fe3e4aSElliott Hughes        path, _ = os.path.split(__file__)
407*e1fe3e4aSElliott Hughes        return os.path.join(path, "data", testfile)
408*e1fe3e4aSElliott Hughes
409*e1fe3e4aSElliott Hughes    def expect_ttx(self, expected_ttx, actual_ttx, fromfile=None, tofile=None):
410*e1fe3e4aSElliott Hughes        expected = [l + "\n" for l in expected_ttx.split("\n")]
411*e1fe3e4aSElliott Hughes        actual = [l + "\n" for l in actual_ttx.split("\n")]
412*e1fe3e4aSElliott Hughes        if actual != expected:
413*e1fe3e4aSElliott Hughes            sys.stderr.write("\n")
414*e1fe3e4aSElliott Hughes            for line in difflib.unified_diff(
415*e1fe3e4aSElliott Hughes                expected, actual, fromfile=fromfile, tofile=tofile
416*e1fe3e4aSElliott Hughes            ):
417*e1fe3e4aSElliott Hughes                sys.stderr.write(line)
418*e1fe3e4aSElliott Hughes            pytest.fail("TTX output is different from expected")
419*e1fe3e4aSElliott Hughes
420*e1fe3e4aSElliott Hughes    @classmethod
421*e1fe3e4aSElliott Hughes    def create_font(celf):
422*e1fe3e4aSElliott Hughes        font = TTFont()
423*e1fe3e4aSElliott Hughes        font.setGlyphOrder(celf.GLYPH_ORDER)
424*e1fe3e4aSElliott Hughes        return font
425*e1fe3e4aSElliott Hughes
426*e1fe3e4aSElliott Hughes    def check_mti_file(self, name, tableTag=None):
427*e1fe3e4aSElliott Hughes        xml_expected_path = self.getpath(
428*e1fe3e4aSElliott Hughes            "%s.ttx" % name + ("." + tableTag if tableTag is not None else "")
429*e1fe3e4aSElliott Hughes        )
430*e1fe3e4aSElliott Hughes        with open(xml_expected_path, "rt", encoding="utf-8") as xml_expected_file:
431*e1fe3e4aSElliott Hughes            xml_expected = xml_expected_file.read()
432*e1fe3e4aSElliott Hughes
433*e1fe3e4aSElliott Hughes        font = self.create_font()
434*e1fe3e4aSElliott Hughes
435*e1fe3e4aSElliott Hughes        with open(self.getpath("%s.txt" % name), "rt", encoding="utf-8") as f:
436*e1fe3e4aSElliott Hughes            table = mtiLib.build(f, font, tableTag=tableTag)
437*e1fe3e4aSElliott Hughes
438*e1fe3e4aSElliott Hughes        if tableTag is not None:
439*e1fe3e4aSElliott Hughes            assert tableTag == table.tableTag
440*e1fe3e4aSElliott Hughes        tableTag = table.tableTag
441*e1fe3e4aSElliott Hughes
442*e1fe3e4aSElliott Hughes        # Make sure it compiles.
443*e1fe3e4aSElliott Hughes        blob = table.compile(font)
444*e1fe3e4aSElliott Hughes
445*e1fe3e4aSElliott Hughes        # Make sure it decompiles.
446*e1fe3e4aSElliott Hughes        decompiled = table.__class__()
447*e1fe3e4aSElliott Hughes        decompiled.decompile(blob, font)
448*e1fe3e4aSElliott Hughes
449*e1fe3e4aSElliott Hughes        # XML from built object.
450*e1fe3e4aSElliott Hughes        writer = XMLWriter(StringIO())
451*e1fe3e4aSElliott Hughes        writer.begintag(tableTag)
452*e1fe3e4aSElliott Hughes        writer.newline()
453*e1fe3e4aSElliott Hughes        table.toXML(writer, font)
454*e1fe3e4aSElliott Hughes        writer.endtag(tableTag)
455*e1fe3e4aSElliott Hughes        writer.newline()
456*e1fe3e4aSElliott Hughes        xml_built = writer.file.getvalue()
457*e1fe3e4aSElliott Hughes
458*e1fe3e4aSElliott Hughes        # XML from decompiled object.
459*e1fe3e4aSElliott Hughes        writer = XMLWriter(StringIO())
460*e1fe3e4aSElliott Hughes        writer.begintag(tableTag)
461*e1fe3e4aSElliott Hughes        writer.newline()
462*e1fe3e4aSElliott Hughes        decompiled.toXML(writer, font)
463*e1fe3e4aSElliott Hughes        writer.endtag(tableTag)
464*e1fe3e4aSElliott Hughes        writer.newline()
465*e1fe3e4aSElliott Hughes        xml_binary = writer.file.getvalue()
466*e1fe3e4aSElliott Hughes
467*e1fe3e4aSElliott Hughes        self.expect_ttx(xml_binary, xml_built, fromfile="decompiled", tofile="built")
468*e1fe3e4aSElliott Hughes        self.expect_ttx(
469*e1fe3e4aSElliott Hughes            xml_expected, xml_built, fromfile=xml_expected_path, tofile="built"
470*e1fe3e4aSElliott Hughes        )
471*e1fe3e4aSElliott Hughes
472*e1fe3e4aSElliott Hughes        from fontTools.misc import xmlReader
473*e1fe3e4aSElliott Hughes
474*e1fe3e4aSElliott Hughes        f = StringIO()
475*e1fe3e4aSElliott Hughes        f.write(xml_expected)
476*e1fe3e4aSElliott Hughes        f.seek(0)
477*e1fe3e4aSElliott Hughes        font2 = TTFont()
478*e1fe3e4aSElliott Hughes        font2.setGlyphOrder(font.getGlyphOrder())
479*e1fe3e4aSElliott Hughes        reader = xmlReader.XMLReader(f, font2)
480*e1fe3e4aSElliott Hughes        reader.read(rootless=True)
481*e1fe3e4aSElliott Hughes
482*e1fe3e4aSElliott Hughes        # XML from object read from XML.
483*e1fe3e4aSElliott Hughes        writer = XMLWriter(StringIO())
484*e1fe3e4aSElliott Hughes        writer.begintag(tableTag)
485*e1fe3e4aSElliott Hughes        writer.newline()
486*e1fe3e4aSElliott Hughes        font2[tableTag].toXML(writer, font)
487*e1fe3e4aSElliott Hughes        writer.endtag(tableTag)
488*e1fe3e4aSElliott Hughes        writer.newline()
489*e1fe3e4aSElliott Hughes        xml_fromxml = writer.file.getvalue()
490*e1fe3e4aSElliott Hughes
491*e1fe3e4aSElliott Hughes        self.expect_ttx(
492*e1fe3e4aSElliott Hughes            xml_expected, xml_fromxml, fromfile=xml_expected_path, tofile="fromxml"
493*e1fe3e4aSElliott Hughes        )
494*e1fe3e4aSElliott Hughes
495*e1fe3e4aSElliott Hughes
496*e1fe3e4aSElliott Hughesdef generate_mti_file_test(name, tableTag=None):
497*e1fe3e4aSElliott Hughes    return lambda self: self.check_mti_file(
498*e1fe3e4aSElliott Hughes        os.path.join(*name.split("/")), tableTag=tableTag
499*e1fe3e4aSElliott Hughes    )
500*e1fe3e4aSElliott Hughes
501*e1fe3e4aSElliott Hughes
502*e1fe3e4aSElliott Hughesfor tableTag, tests in MtiTest.TESTS.items():
503*e1fe3e4aSElliott Hughes    for name in tests:
504*e1fe3e4aSElliott Hughes        setattr(
505*e1fe3e4aSElliott Hughes            MtiTest,
506*e1fe3e4aSElliott Hughes            "test_MtiFile_%s%s" % (name, "_" + tableTag if tableTag else ""),
507*e1fe3e4aSElliott Hughes            generate_mti_file_test(name, tableTag=tableTag),
508*e1fe3e4aSElliott Hughes        )
509*e1fe3e4aSElliott Hughes
510*e1fe3e4aSElliott Hughes
511*e1fe3e4aSElliott Hughesif __name__ == "__main__":
512*e1fe3e4aSElliott Hughes    if len(sys.argv) > 1:
513*e1fe3e4aSElliott Hughes        from fontTools.mtiLib import main
514*e1fe3e4aSElliott Hughes
515*e1fe3e4aSElliott Hughes        font = MtiTest.create_font()
516*e1fe3e4aSElliott Hughes        sys.exit(main(sys.argv[1:], font))
517*e1fe3e4aSElliott Hughes    sys.exit(pytest.main(sys.argv))
518