xref: /aosp_15_r20/external/fonttools/Tests/otlLib/maxContextCalc_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1import os
2import pytest
3from fontTools.ttLib import TTFont
4from fontTools.otlLib.maxContextCalc import maxCtxFont
5from fontTools.feaLib.builder import addOpenTypeFeaturesFromString
6
7
8def test_max_ctx_calc_no_features():
9    font = TTFont()
10    assert maxCtxFont(font) == 0
11    font.setGlyphOrder([".notdef"])
12    addOpenTypeFeaturesFromString(font, "")
13    assert maxCtxFont(font) == 0
14
15
16def test_max_ctx_calc_features():
17    glyphs = ".notdef space A B C a b c".split()
18    features = """
19    lookup GSUB_EXT useExtension {
20        sub a by b;
21    } GSUB_EXT;
22
23    lookup GPOS_EXT useExtension {
24        pos a b -10;
25    } GPOS_EXT;
26
27    feature sub1 {
28        sub A by a;
29        sub A B by b;
30        sub A B C by c;
31        sub [A B] C by c;
32        sub [A B] C [A B] by c;
33        sub A by A B;
34        sub A' C by A B;
35        sub a' by b;
36        sub a' b by c;
37        sub a from [A B C];
38        rsub a by b;
39        rsub a' by b;
40        rsub a b' by c;
41        rsub a b' c by A;
42        rsub [a b] c' by A;
43        rsub [a b] c' [a b] by B;
44        lookup GSUB_EXT;
45    } sub1;
46
47    feature pos1 {
48        pos A 20;
49        pos A B -50;
50        pos A B' 10 C;
51        lookup GPOS_EXT;
52    } pos1;
53    """
54    font = TTFont()
55    font.setGlyphOrder(glyphs)
56    addOpenTypeFeaturesFromString(font, features)
57
58    assert maxCtxFont(font) == 3
59
60
61@pytest.mark.parametrize(
62    "file_name, max_context",
63    [
64        ("gsub_51", 2),
65        ("gsub_52", 2),
66        ("gsub_71", 1),
67        ("gpos_91", 1),
68    ],
69)
70def test_max_ctx_calc_features_ttx(file_name, max_context):
71    ttx_path = os.path.join(
72        os.path.dirname(__file__), "data", "{}.ttx".format(file_name)
73    )
74    font = TTFont()
75    font.importXML(ttx_path)
76
77    assert maxCtxFont(font) == max_context
78