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