xref: /aosp_15_r20/external/fonttools/Tests/otlLib/mock_builder_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.otlLib.builder import (
2*e1fe3e4aSElliott Hughes    AlternateSubstBuilder,
3*e1fe3e4aSElliott Hughes    ChainContextPosBuilder,
4*e1fe3e4aSElliott Hughes    ChainContextSubstBuilder,
5*e1fe3e4aSElliott Hughes    LigatureSubstBuilder,
6*e1fe3e4aSElliott Hughes    MultipleSubstBuilder,
7*e1fe3e4aSElliott Hughes    CursivePosBuilder,
8*e1fe3e4aSElliott Hughes    MarkBasePosBuilder,
9*e1fe3e4aSElliott Hughes    MarkLigPosBuilder,
10*e1fe3e4aSElliott Hughes    MarkMarkPosBuilder,
11*e1fe3e4aSElliott Hughes    ReverseChainSingleSubstBuilder,
12*e1fe3e4aSElliott Hughes    SingleSubstBuilder,
13*e1fe3e4aSElliott Hughes    ClassPairPosSubtableBuilder,
14*e1fe3e4aSElliott Hughes    PairPosBuilder,
15*e1fe3e4aSElliott Hughes    SinglePosBuilder,
16*e1fe3e4aSElliott Hughes    ChainContextualRule,
17*e1fe3e4aSElliott Hughes)
18*e1fe3e4aSElliott Hughesfrom fontTools.otlLib.error import OpenTypeLibError
19*e1fe3e4aSElliott Hughesfrom fontTools.ttLib import TTFont
20*e1fe3e4aSElliott Hughesfrom fontTools.misc.loggingTools import CapturingLogHandler
21*e1fe3e4aSElliott Hughesimport logging
22*e1fe3e4aSElliott Hughesimport pytest
23*e1fe3e4aSElliott Hughes
24*e1fe3e4aSElliott Hughes
25*e1fe3e4aSElliott Hughes@pytest.fixture
26*e1fe3e4aSElliott Hughesdef ttfont():
27*e1fe3e4aSElliott Hughes    glyphs = """
28*e1fe3e4aSElliott Hughes        .notdef space slash fraction semicolon period comma ampersand
29*e1fe3e4aSElliott Hughes        quotedblleft quotedblright quoteleft quoteright
30*e1fe3e4aSElliott Hughes        zero one two three four five six seven eight nine
31*e1fe3e4aSElliott Hughes        zero.oldstyle one.oldstyle two.oldstyle three.oldstyle
32*e1fe3e4aSElliott Hughes        four.oldstyle five.oldstyle six.oldstyle seven.oldstyle
33*e1fe3e4aSElliott Hughes        eight.oldstyle nine.oldstyle onequarter onehalf threequarters
34*e1fe3e4aSElliott Hughes        onesuperior twosuperior threesuperior ordfeminine ordmasculine
35*e1fe3e4aSElliott Hughes        A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
36*e1fe3e4aSElliott Hughes        a b c d e f g h i j k l m n o p q r s t u v w x y z
37*e1fe3e4aSElliott Hughes        A.sc B.sc C.sc D.sc E.sc F.sc G.sc H.sc I.sc J.sc K.sc L.sc M.sc
38*e1fe3e4aSElliott Hughes        N.sc O.sc P.sc Q.sc R.sc S.sc T.sc U.sc V.sc W.sc X.sc Y.sc Z.sc
39*e1fe3e4aSElliott Hughes        A.alt1 A.alt2 A.alt3 B.alt1 B.alt2 B.alt3 C.alt1 C.alt2 C.alt3
40*e1fe3e4aSElliott Hughes        a.alt1 a.alt2 a.alt3 a.end b.alt c.mid d.alt d.mid
41*e1fe3e4aSElliott Hughes        e.begin e.mid e.end m.begin n.end s.end z.end
42*e1fe3e4aSElliott Hughes        Eng Eng.alt1 Eng.alt2 Eng.alt3
43*e1fe3e4aSElliott Hughes        A.swash B.swash C.swash D.swash E.swash F.swash G.swash H.swash
44*e1fe3e4aSElliott Hughes        I.swash J.swash K.swash L.swash M.swash N.swash O.swash P.swash
45*e1fe3e4aSElliott Hughes        Q.swash R.swash S.swash T.swash U.swash V.swash W.swash X.swash
46*e1fe3e4aSElliott Hughes        Y.swash Z.swash
47*e1fe3e4aSElliott Hughes        f_l c_h c_k c_s c_t f_f f_f_i f_f_l f_i o_f_f_i s_t f_i.begin
48*e1fe3e4aSElliott Hughes        a_n_d T_h T_h.swash germandbls ydieresis yacute breve
49*e1fe3e4aSElliott Hughes        grave acute dieresis macron circumflex cedilla umlaut ogonek caron
50*e1fe3e4aSElliott Hughes        damma hamza sukun kasratan lam_meem_jeem noon.final noon.initial
51*e1fe3e4aSElliott Hughes        by feature lookup sub table uni0327 uni0328 e.fina
52*e1fe3e4aSElliott Hughes    """.split()
53*e1fe3e4aSElliott Hughes    glyphs.extend("cid{:05d}".format(cid) for cid in range(800, 1001 + 1))
54*e1fe3e4aSElliott Hughes    font = TTFont()
55*e1fe3e4aSElliott Hughes    font.setGlyphOrder(glyphs)
56*e1fe3e4aSElliott Hughes    return font
57*e1fe3e4aSElliott Hughes
58*e1fe3e4aSElliott Hughes
59*e1fe3e4aSElliott Hughesclass MockBuilderLocation(object):
60*e1fe3e4aSElliott Hughes    def __init__(self, location):
61*e1fe3e4aSElliott Hughes        self.location = location
62*e1fe3e4aSElliott Hughes
63*e1fe3e4aSElliott Hughes    def __str__(self):
64*e1fe3e4aSElliott Hughes        return "%s:%s" % self.location
65*e1fe3e4aSElliott Hughes
66*e1fe3e4aSElliott Hughes
67*e1fe3e4aSElliott Hughesdef test_unsupported_subtable_break_1(ttfont):
68*e1fe3e4aSElliott Hughes    location = MockBuilderLocation((0, "alpha"))
69*e1fe3e4aSElliott Hughes
70*e1fe3e4aSElliott Hughes    logger = logging.getLogger("fontTools.otlLib.builder")
71*e1fe3e4aSElliott Hughes
72*e1fe3e4aSElliott Hughes    with CapturingLogHandler(logger, "INFO") as captor:
73*e1fe3e4aSElliott Hughes        builder = SinglePosBuilder(ttfont, location)
74*e1fe3e4aSElliott Hughes        builder.add_subtable_break(MockBuilderLocation((5, "beta")))
75*e1fe3e4aSElliott Hughes        builder.build()
76*e1fe3e4aSElliott Hughes
77*e1fe3e4aSElliott Hughes    captor.assertRegex('5:beta: unsupported "subtable" statement for lookup type')
78*e1fe3e4aSElliott Hughes
79*e1fe3e4aSElliott Hughes
80*e1fe3e4aSElliott Hughesdef test_chain_pos_references_GSUB_lookup(ttfont):
81*e1fe3e4aSElliott Hughes    location = MockBuilderLocation((0, "alpha"))
82*e1fe3e4aSElliott Hughes    builder = ChainContextPosBuilder(ttfont, location)
83*e1fe3e4aSElliott Hughes    builder2 = SingleSubstBuilder(ttfont, location)
84*e1fe3e4aSElliott Hughes    builder.rules.append(ChainContextualRule([], [], [], [[builder2]]))
85*e1fe3e4aSElliott Hughes
86*e1fe3e4aSElliott Hughes    with pytest.raises(
87*e1fe3e4aSElliott Hughes        OpenTypeLibError,
88*e1fe3e4aSElliott Hughes        match="0:alpha: Missing index of the specified lookup, might be a substitution lookup",
89*e1fe3e4aSElliott Hughes    ):
90*e1fe3e4aSElliott Hughes        builder.build()
91