| 1 | import os |
| 2 | import re |
| 3 | import tempfile |
| 4 | |
| 5 | import fontforge |
| 6 | |
| 7 | |
| 8 | def make_glyph(font, name, codepoint): |
| 9 | glyph = font.createChar(codepoint, name) |
| 10 | pen = glyph.glyphPen() |
| 11 | pen.moveTo((0, 0)) |
| 12 | pen.lineTo((500, 0)) |
| 13 | pen.lineTo((500, 500)) |
| 14 | pen.lineTo((0, 500)) |
| 15 | pen.closePath() |
| 16 | glyph.width = 600 |
| 17 | |
| 18 | |
| 19 | font = fontforge.font() |
| 20 | font.encoding = "UnicodeFull" |
| 21 | font.fontname = "ContextClassMultiRule" |
| 22 | font.familyname = "Context Class MultiRule" |
| 23 | font.fullname = "Context Class MultiRule" |
| 24 | |
| 25 | make_glyph(font, "baseA", 0xE000) |
| 26 | make_glyph(font, "baseB", 0xE001) |
| 27 | make_glyph(font, "markA", 0xE002) |
| 28 | make_glyph(font, "markB", 0xE003) |
| 29 | make_glyph(font, "target", 0xE004) |
| 30 | |
| 31 | features = (("mark", (("DFLT", ("dflt",)),)),) |
| 32 | |
| 33 | font.addLookup("raise_020", "gpos_single", (), ()) |
| 34 | font.addLookupSubtable("raise_020", "raise_020-1") |
| 35 | font["target"].addPosSub("raise_020-1", 0, 20, 0, 0) |
| 36 | |
| 37 | font.addLookup("raise_040", "gpos_single", (), (), "raise_020") |
| 38 | font.addLookupSubtable("raise_040", "raise_040-1") |
| 39 | font["baseA"].addPosSub("raise_040-1", 0, 40, 0, 0) |
| 40 | font["target"].addPosSub("raise_040-1", 0, 40, 0, 0) |
| 41 | |
| 42 | font.addLookup("ctx", "gpos_contextchain", (), features, "raise_040") |
| 43 | font.addContextualSubtable( |
| 44 | "ctx", |
| 45 | "ctx-1", |
| 46 | "class", |
| 47 | ( |
| 48 | "1 2 | 1 @<raise_020> |", |
| 49 | "1 3 | 1 @<raise_040> |", |
| 50 | ), |
| 51 | bclasses=(None, ("baseA", "baseB"), ("markA",), ("markB",)), |
| 52 | mclasses=(None, ("target",)), |
| 53 | ) |
| 54 | |
| 55 | assert font.getLookupSubtables("ctx") == ("ctx-1",) |
| 56 | |
| 57 | for subtable, rule, error_type, error_message in [ |
| 58 | ( |
| 59 | "ctx-non-string", |
| 60 | ("1 2 | 1 @<raise_020> |", object()), |
| 61 | TypeError, |
| 62 | "rule 1 must be a string", |
| 63 | ), |
| 64 | ( |
| 65 | "ctx-bytes", |
| 66 | b"1 2 | 1 @<raise_020> |", |
| 67 | TypeError, |
| 68 | "rule 0 must be a string", |
| 69 | ), |
| 70 | ( |
| 71 | "ctx-item-bytes", |
| 72 | ("1 2 | 1 @<raise_020> |", b"1 3 | 1 @<raise_040> |"), |
| 73 | TypeError, |
| 74 | "rule 1 must be a string", |
| 75 | ), |
| 76 | ( |
| 77 | "ctx-bad-parse", |
| 78 | ("1 2 | 1 @<raise_020> |", "99 3 | 1 @<raise_040> |"), |
| 79 | TypeError, |
| 80 | "Error in rule 1:", |
| 81 | ), |
| 82 | ]: |
| 83 | try: |
| 84 | font.addContextualSubtable( |
| 85 | "ctx", |
| 86 | subtable, |
| 87 | "class", |
| 88 | rule, |
| 89 | bclasses=(None, ("baseA", "baseB"), ("markA",), ("markB",)), |
| 90 | mclasses=(None, ("target",)), |
| 91 | ) |
| 92 | raise AssertionError("no error raised, but expected") |
| 93 | except error_type as error: |
| 94 | assert error_message in str(error), "wrong error message for correct error type" |
| 95 | except Exception: |
| 96 | raise AssertionError("wrong error type") |
| 97 | |
| 98 | assert font.getLookupSubtables("ctx") == ("ctx-1",) |
| 99 | |
| 100 | font.addLookup("glyph_ctx", "gpos_context", (), features, "ctx") |
| 101 | font.addContextualSubtable( |
| 102 | "glyph_ctx", |
| 103 | "glyph_ctx-1", |
| 104 | "glyph", |
| 105 | ( |
| 106 | "target @<raise_020>", |
| 107 | "baseA @<raise_040>", |
| 108 | ), |
| 109 | ) |
| 110 | |
| 111 | assert font.getLookupSubtables("glyph_ctx") == ("glyph_ctx-1",) |
| 112 | |
| 113 | with tempfile.TemporaryDirectory() as tmpdirname: |
| 114 | sfd_path = os.path.join(tmpdirname, "ContextClassMultiRule.sfd") |
| 115 | fea_path = os.path.join(tmpdirname, "ContextClassMultiRule.fea") |
| 116 | font.save(sfd_path) |
| 117 | font.close() |
| 118 | |
| 119 | reopened = fontforge.open(sfd_path) |
| 120 | reopened.generateFeatureFile(fea_path) |
| 121 | reopened.close() |
| 122 | |
| 123 | with open(fea_path) as fea_file: |
| 124 | fea = fea_file.read() |
| 125 | |
| 126 | assert re.search(r"@cc2_match_1 = \[\\target \];", fea) |
| 127 | assert re.search(r"@cc2_back_1 = \[\\baseA \\baseB \];", fea) |
| 128 | assert re.search(r"@cc2_back_2 = \[\\markA \];", fea) |
| 129 | assert re.search(r"@cc2_back_3 = \[\\markB \];", fea) |
| 130 | assert re.search(r"pos @cc2_back_2 @cc2_back_1 @cc2_match_1'lookup raise_020", fea) |
| 131 | assert re.search(r"pos @cc2_back_3 @cc2_back_1 @cc2_match_1'lookup raise_040", fea) |
| 132 | assert re.search(r"pos\s+\\target'lookup raise_020", fea) |
| 133 | assert re.search(r"pos\s+\\baseA'lookup raise_040", fea) |