• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.varLib.models import (
4    normalizeLocation, supportScalar, VariationModel)
5import pytest
6
7
8def test_normalizeLocation():
9    axes = {"wght": (100, 400, 900)}
10    assert normalizeLocation({"wght": 400}, axes) == {'wght': 0.0}
11    assert normalizeLocation({"wght": 100}, axes) == {'wght': -1.0}
12    assert normalizeLocation({"wght": 900}, axes) == {'wght': 1.0}
13    assert normalizeLocation({"wght": 650}, axes) == {'wght': 0.5}
14    assert normalizeLocation({"wght": 1000}, axes) == {'wght': 1.0}
15    assert normalizeLocation({"wght": 0}, axes) == {'wght': -1.0}
16
17    axes = {"wght": (0, 0, 1000)}
18    assert normalizeLocation({"wght": 0}, axes) == {'wght': 0.0}
19    assert normalizeLocation({"wght": -1}, axes) == {'wght': 0.0}
20    assert normalizeLocation({"wght": 1000}, axes) == {'wght': 1.0}
21    assert normalizeLocation({"wght": 500}, axes) == {'wght': 0.5}
22    assert normalizeLocation({"wght": 1001}, axes) == {'wght': 1.0}
23
24    axes = {"wght": (0, 1000, 1000)}
25    assert normalizeLocation({"wght": 0}, axes) == {'wght': -1.0}
26    assert normalizeLocation({"wght": -1}, axes) == {'wght': -1.0}
27    assert normalizeLocation({"wght": 500}, axes) == {'wght': -0.5}
28    assert normalizeLocation({"wght": 1000}, axes) == {'wght': 0.0}
29    assert normalizeLocation({"wght": 1001}, axes) == {'wght': 0.0}
30
31
32def test_supportScalar():
33    assert supportScalar({}, {}) == 1.0
34    assert supportScalar({'wght':.2}, {}) == 1.0
35    assert supportScalar({'wght':.2}, {'wght':(0,2,3)}) == 0.1
36    assert supportScalar({'wght':2.5}, {'wght':(0,2,4)}) == 0.75
37
38
39class VariationModelTest(object):
40
41    @pytest.mark.parametrize(
42        "locations, axisOrder, sortedLocs, supports, deltaWeights",
43        [
44            (
45                [
46                    {'wght': 0.55, 'wdth': 0.0},
47                    {'wght': -0.55, 'wdth': 0.0},
48                    {'wght': -1.0, 'wdth': 0.0},
49                    {'wght': 0.0, 'wdth': 1.0},
50                    {'wght': 0.66, 'wdth': 1.0},
51                    {'wght': 0.66, 'wdth': 0.66},
52                    {'wght': 0.0, 'wdth': 0.0},
53                    {'wght': 1.0, 'wdth': 1.0},
54                    {'wght': 1.0, 'wdth': 0.0},
55                ],
56                ["wght"],
57                [
58                    {},
59                    {'wght': -0.55},
60                    {'wght': -1.0},
61                    {'wght': 0.55},
62                    {'wght': 1.0},
63                    {'wdth': 1.0},
64                    {'wdth': 1.0, 'wght': 1.0},
65                    {'wdth': 1.0, 'wght': 0.66},
66                    {'wdth': 0.66, 'wght': 0.66}
67                ],
68                [
69                    {},
70                    {'wght': (-1.0, -0.55, 0)},
71                    {'wght': (-1.0, -1.0, -0.55)},
72                    {'wght': (0, 0.55, 1.0)},
73                    {'wght': (0.55, 1.0, 1.0)},
74                    {'wdth': (0, 1.0, 1.0)},
75                    {'wdth': (0, 1.0, 1.0), 'wght': (0, 1.0, 1.0)},
76                    {'wdth': (0, 1.0, 1.0), 'wght': (0, 0.66, 1.0)},
77                    {'wdth': (0, 0.66, 1.0), 'wght': (0, 0.66, 1.0)}
78                ],
79                [
80                    {},
81                    {0: 1.0},
82                    {0: 1.0},
83                    {0: 1.0},
84                    {0: 1.0},
85                    {0: 1.0},
86                    {0: 1.0,
87                     4: 1.0,
88                     5: 1.0},
89                    {0: 1.0,
90                     3: 0.7555555555555555,
91                     4: 0.24444444444444444,
92                     5: 1.0,
93                     6: 0.66},
94                    {0: 1.0,
95                     3: 0.7555555555555555,
96                     4: 0.24444444444444444,
97                     5: 0.66,
98                     6: 0.43560000000000006,
99                     7: 0.66}
100                ]
101            ),
102            (
103                [
104                    {},
105                    {'bar': 0.5},
106                    {'bar': 1.0},
107                    {'foo': 1.0},
108                    {'bar': 0.5, 'foo': 1.0},
109                    {'bar': 1.0, 'foo': 1.0},
110                ],
111                None,
112                [
113                    {},
114                    {'bar': 0.5},
115                    {'bar': 1.0},
116                    {'foo': 1.0},
117                    {'bar': 0.5, 'foo': 1.0},
118                    {'bar': 1.0, 'foo': 1.0},
119                ],
120                [
121                    {},
122                    {'bar': (0, 0.5, 1.0)},
123                    {'bar': (0.5, 1.0, 1.0)},
124                    {'foo': (0, 1.0, 1.0)},
125                    {'bar': (0, 0.5, 1.0), 'foo': (0, 1.0, 1.0)},
126                    {'bar': (0.5, 1.0, 1.0), 'foo': (0, 1.0, 1.0)},
127                ],
128                [
129                    {},
130                    {0: 1.0},
131                    {0: 1.0},
132                    {0: 1.0},
133                    {0: 1.0, 1: 1.0, 3: 1.0},
134                    {0: 1.0, 2: 1.0, 3: 1.0},
135                ],
136            )
137        ]
138    )
139    def test_init(
140        self, locations, axisOrder, sortedLocs, supports, deltaWeights
141    ):
142        model = VariationModel(locations, axisOrder=axisOrder)
143
144        assert model.locations == sortedLocs
145        assert model.supports == supports
146        assert model.deltaWeights == deltaWeights
147
148    def test_init_duplicate_locations(self):
149        with pytest.raises(ValueError, match="locations must be unique"):
150            VariationModel(
151                [
152                    {"foo": 0.0, "bar": 0.0},
153                    {"foo": 1.0, "bar": 1.0},
154                    {"bar": 1.0, "foo": 1.0},
155                ]
156            )
157