1from fontTools.varLib.featureVars import ( 2 overlayFeatureVariations) 3 4 5def test_linear(n = 10): 6 conds = [] 7 for i in range(n): 8 end = i / n 9 start = end - 1. 10 region = [{'X': (start, end)}] 11 subst = {'g%.2g'%start: 'g%.2g'%end} 12 conds.append((region, subst)) 13 overlaps = overlayFeatureVariations(conds) 14 assert len(overlaps) == 2 * n - 1, overlaps 15 return conds, overlaps 16 17def test_quadratic(n = 10): 18 conds = [] 19 for i in range(1, n + 1): 20 region = [{'X': (0, i / n), 21 'Y': (0, (n + 1 - i) / n)}] 22 subst = {str(i): str(n + 1 - i)} 23 conds.append((region, subst)) 24 overlaps = overlayFeatureVariations(conds) 25 assert len(overlaps) == n * (n + 1) // 2, overlaps 26 return conds, overlaps 27 28def _merge_substitutions(substitutions): 29 merged = {} 30 for subst in substitutions: 31 merged.update(subst) 32 return merged 33 34def _match_condition(location, overlaps): 35 for box, substitutions in overlaps: 36 for tag, coord in location.items(): 37 start, end = box[tag] 38 if start <= coord <= end: 39 return _merge_substitutions(substitutions) 40 return {} # no match 41 42def test_overlaps_1(): 43 # https://github.com/fonttools/fonttools/issues/1400 44 conds = [ 45 ([{'abcd': (4, 9)}], {0: 0}), 46 ([{'abcd': (5, 10)}], {1: 1}), 47 ([{'abcd': (0, 8)}], {2: 2}), 48 ([{'abcd': (3, 7)}], {3: 3}), 49 ] 50 overlaps = overlayFeatureVariations(conds) 51 subst = _match_condition({'abcd': 0}, overlaps) 52 assert subst == {2: 2} 53 subst = _match_condition({'abcd': 1}, overlaps) 54 assert subst == {2: 2} 55 subst = _match_condition({'abcd': 3}, overlaps) 56 assert subst == {2: 2, 3: 3} 57 subst = _match_condition({'abcd': 4}, overlaps) 58 assert subst == {0: 0, 2: 2, 3: 3} 59 subst = _match_condition({'abcd': 5}, overlaps) 60 assert subst == {0: 0, 1: 1, 2: 2, 3: 3} 61 subst = _match_condition({'abcd': 7}, overlaps) 62 assert subst == {0: 0, 1: 1, 2: 2, 3: 3} 63 subst = _match_condition({'abcd': 8}, overlaps) 64 assert subst == {0: 0, 1: 1, 2: 2} 65 subst = _match_condition({'abcd': 9}, overlaps) 66 assert subst == {0: 0, 1: 1} 67 subst = _match_condition({'abcd': 10}, overlaps) 68 assert subst == {1: 1} 69 70def test_overlaps_2(): 71 # https://github.com/fonttools/fonttools/issues/1400 72 conds = [ 73 ([{'abcd': (1, 9)}], {0: 0}), 74 ([{'abcd': (8, 10)}], {1: 1}), 75 ([{'abcd': (3, 4)}], {2: 2}), 76 ([{'abcd': (1, 10)}], {3: 3}), 77 ] 78 overlaps = overlayFeatureVariations(conds) 79 subst = _match_condition({'abcd': 0}, overlaps) 80 assert subst == {} 81 subst = _match_condition({'abcd': 1}, overlaps) 82 assert subst == {0: 0, 3: 3} 83 subst = _match_condition({'abcd': 2}, overlaps) 84 assert subst == {0: 0, 3: 3} 85 subst = _match_condition({'abcd': 3}, overlaps) 86 assert subst == {0: 0, 2: 2, 3: 3} 87 subst = _match_condition({'abcd': 5}, overlaps) 88 assert subst == {0: 0, 3: 3} 89 subst = _match_condition({'abcd': 10}, overlaps) 90 assert subst == {1: 1, 3: 3} 91 92 93def run(test, n, quiet): 94 95 print() 96 print("%s:" % test.__name__) 97 input, output = test(n) 98 if quiet: 99 print(len(output)) 100 else: 101 print() 102 print("Input:") 103 pprint(input) 104 print() 105 print("Output:") 106 pprint(output) 107 print() 108 109if __name__ == "__main__": 110 import sys 111 from pprint import pprint 112 quiet = False 113 n = 3 114 if len(sys.argv) > 1 and sys.argv[1] == '-q': 115 quiet = True 116 del sys.argv[1] 117 if len(sys.argv) > 1: 118 n = int(sys.argv[1]) 119 120 run(test_linear, n=n, quiet=quiet) 121 run(test_quadratic, n=n, quiet=quiet) 122