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