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