aboutsummaryrefslogtreecommitdiff
path: root/Tests/varLib/models_test.py
blob: ae67e0e932926ba571044c9528ca06dd382f865c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
from fontTools.varLib.models import (
    normalizeLocation, supportScalar, VariationModel)
import pytest


def test_normalizeLocation():
    axes = {"wght": (100, 400, 900)}
    assert normalizeLocation({"wght": 400}, axes) == {'wght': 0.0}
    assert normalizeLocation({"wght": 100}, axes) == {'wght': -1.0}
    assert normalizeLocation({"wght": 900}, axes) == {'wght': 1.0}
    assert normalizeLocation({"wght": 650}, axes) == {'wght': 0.5}
    assert normalizeLocation({"wght": 1000}, axes) == {'wght': 1.0}
    assert normalizeLocation({"wght": 0}, axes) == {'wght': -1.0}

    axes = {"wght": (0, 0, 1000)}
    assert normalizeLocation({"wght": 0}, axes) == {'wght': 0.0}
    assert normalizeLocation({"wght": -1}, axes) == {'wght': 0.0}
    assert normalizeLocation({"wght": 1000}, axes) == {'wght': 1.0}
    assert normalizeLocation({"wght": 500}, axes) == {'wght': 0.5}
    assert normalizeLocation({"wght": 1001}, axes) == {'wght': 1.0}

    axes = {"wght": (0, 1000, 1000)}
    assert normalizeLocation({"wght": 0}, axes) == {'wght': -1.0}
    assert normalizeLocation({"wght": -1}, axes) == {'wght': -1.0}
    assert normalizeLocation({"wght": 500}, axes) == {'wght': -0.5}
    assert normalizeLocation({"wght": 1000}, axes) == {'wght': 0.0}
    assert normalizeLocation({"wght": 1001}, axes) == {'wght': 0.0}


def test_supportScalar():
    assert supportScalar({}, {}) == 1.0
    assert supportScalar({'wght':.2}, {}) == 1.0
    assert supportScalar({'wght':.2}, {'wght':(0,2,3)}) == 0.1
    assert supportScalar({'wght':2.5}, {'wght':(0,2,4)}) == 0.75


class VariationModelTest(object):

    @pytest.mark.parametrize(
        "locations, axisOrder, sortedLocs, supports, deltaWeights",
        [
            (
                [
                    {'wght': 0.55, 'wdth': 0.0},
                    {'wght': -0.55, 'wdth': 0.0},
                    {'wght': -1.0, 'wdth': 0.0},
                    {'wght': 0.0, 'wdth': 1.0},
                    {'wght': 0.66, 'wdth': 1.0},
                    {'wght': 0.66, 'wdth': 0.66},
                    {'wght': 0.0, 'wdth': 0.0},
                    {'wght': 1.0, 'wdth': 1.0},
                    {'wght': 1.0, 'wdth': 0.0},
                ],
                ["wght"],
                [
                    {},
                    {'wght': -0.55},
                    {'wght': -1.0},
                    {'wght': 0.55},
                    {'wght': 1.0},
                    {'wdth': 1.0},
                    {'wdth': 1.0, 'wght': 1.0},
                    {'wdth': 1.0, 'wght': 0.66},
                    {'wdth': 0.66, 'wght': 0.66}
                ],
                [
                    {},
                    {'wght': (-1.0, -0.55, 0)},
                    {'wght': (-1.0, -1.0, -0.55)},
                    {'wght': (0, 0.55, 1.0)},
                    {'wght': (0.55, 1.0, 1.0)},
                    {'wdth': (0, 1.0, 1.0)},
                    {'wdth': (0, 1.0, 1.0), 'wght': (0, 1.0, 1.0)},
                    {'wdth': (0, 1.0, 1.0), 'wght': (0, 0.66, 1.0)},
                    {'wdth': (0, 0.66, 1.0), 'wght': (0, 0.66, 1.0)}
                ],
                [
                    {},
                    {0: 1.0},
                    {0: 1.0},
                    {0: 1.0},
                    {0: 1.0},
                    {0: 1.0},
                    {0: 1.0,
                     4: 1.0,
                     5: 1.0},
                    {0: 1.0,
                     3: 0.7555555555555555,
                     4: 0.24444444444444444,
                     5: 1.0,
                     6: 0.66},
                    {0: 1.0,
                     3: 0.7555555555555555,
                     4: 0.24444444444444444,
                     5: 0.66,
                     6: 0.43560000000000006,
                     7: 0.66}
                ]
            ),
            (
                [
                    {},
                    {'bar': 0.5},
                    {'bar': 1.0},
                    {'foo': 1.0},
                    {'bar': 0.5, 'foo': 1.0},
                    {'bar': 1.0, 'foo': 1.0},
                ],
                None,
                [
                    {},
                    {'bar': 0.5},
                    {'bar': 1.0},
                    {'foo': 1.0},
                    {'bar': 0.5, 'foo': 1.0},
                    {'bar': 1.0, 'foo': 1.0},
                ],
                [
                    {},
                    {'bar': (0, 0.5, 1.0)},
                    {'bar': (0.5, 1.0, 1.0)},
                    {'foo': (0, 1.0, 1.0)},
                    {'bar': (0, 0.5, 1.0), 'foo': (0, 1.0, 1.0)},
                    {'bar': (0.5, 1.0, 1.0), 'foo': (0, 1.0, 1.0)},
                ],
                [
                    {},
                    {0: 1.0},
                    {0: 1.0},
                    {0: 1.0},
                    {0: 1.0, 1: 1.0, 3: 1.0},
                    {0: 1.0, 2: 1.0, 3: 1.0},
                ],
            )
        ]
    )
    def test_init(
        self, locations, axisOrder, sortedLocs, supports, deltaWeights
    ):
        model = VariationModel(locations, axisOrder=axisOrder)

        assert model.locations == sortedLocs
        assert model.supports == supports
        assert model.deltaWeights == deltaWeights

    def test_init_duplicate_locations(self):
        with pytest.raises(ValueError, match="locations must be unique"):
            VariationModel(
                [
                    {"foo": 0.0, "bar": 0.0},
                    {"foo": 1.0, "bar": 1.0},
                    {"bar": 1.0, "foo": 1.0},
                ]
            )