1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.misc.transform import Transform, Identity, Offset, Scale
4import math
5import pytest
6
7
8class TransformTest(object):
9
10    def test_examples(self):
11        t = Transform()
12        assert repr(t) == "<Transform [1 0 0 1 0 0]>"
13        assert t.scale(2) == Transform(2, 0, 0, 2, 0, 0)
14        assert t.scale(2.5, 5.5) == Transform(2.5, 0, 0, 5.5, 0, 0)
15        assert t.scale(2, 3).transformPoint((100, 100)) == (200, 300)
16
17    def test__init__(self):
18        assert Transform(12) == Transform(12, 0, 0, 1, 0, 0)
19        assert Transform(dx=12) == Transform(1, 0, 0, 1, 12, 0)
20        assert Transform(yx=12) == Transform(1, 0, 12, 1, 0, 0)
21
22    def test_transformPoints(self):
23        t = Transform(2, 0, 0, 3, 0, 0)
24        assert t.transformPoints(
25            [(0, 0), (0, 100), (100, 100), (100, 0)]
26        ) == [(0, 0), (0, 300), (200, 300), (200, 0)]
27
28    def test_translate(self):
29        t = Transform()
30        assert t.translate(20, 30) == Transform(1, 0, 0, 1, 20, 30)
31
32    def test_scale(self):
33        t = Transform()
34        assert t.scale(5) == Transform(5, 0, 0, 5, 0, 0)
35        assert t.scale(5, 6) == Transform(5, 0, 0, 6, 0, 0)
36
37    def test_rotate(self):
38        t = Transform()
39        assert t.rotate(math.pi / 2) == Transform(0, 1, -1, 0, 0, 0)
40        t = Transform()
41        assert t.rotate(-math.pi / 2) == Transform(0, -1, 1, 0, 0, 0)
42        t = Transform()
43        assert tuple(t.rotate(math.radians(30))) == pytest.approx(
44            tuple(Transform(0.866025, 0.5, -0.5, 0.866025, 0, 0)))
45
46    def test_skew(self):
47        t = Transform().skew(math.pi / 4)
48        assert tuple(t) == pytest.approx(tuple(Transform(1, 0, 1, 1, 0, 0)))
49
50    def test_transform(self):
51        t = Transform(2, 0, 0, 3, 1, 6)
52        assert t.transform((4, 3, 2, 1, 5, 6)) == Transform(8, 9, 4, 3, 11, 24)
53
54    def test_reverseTransform(self):
55        t = Transform(2, 0, 0, 3, 1, 6)
56        reverse_t = t.reverseTransform((4, 3, 2, 1, 5, 6))
57        assert reverse_t == Transform(8, 6, 6, 3, 21, 15)
58        t = Transform(4, 3, 2, 1, 5, 6)
59        reverse_t = t.transform((2, 0, 0, 3, 1, 6))
60        assert reverse_t == Transform(8, 6, 6, 3, 21, 15)
61
62    def test_inverse(self):
63        t = Transform().translate(2, 3).scale(4, 5)
64        assert t.transformPoint((10, 20)) == (42, 103)
65        it = t.inverse()
66        assert it.transformPoint((42, 103)) == (10.0, 20.0)
67        assert Transform().inverse() == Transform()
68
69    def test_toPS(self):
70        t = Transform().scale(2, 3).translate(4, 5)
71        assert t.toPS() == '[2 0 0 3 8 15]'
72
73    def test__ne__(self):
74        assert Transform() != Transform(2, 0, 0, 2, 0, 0)
75
76    def test__hash__(self):
77        t = Transform(12, 0, 0, 13, 0, 0)
78        d = {t: None}
79        assert t in d.keys()
80
81    def test__bool__(self):
82        assert not bool(Transform())
83        assert Transform(2, 0, 0, 2, 0, 0)
84        assert Transform(1, 0, 0, 1, 1, 0)
85
86    def test__repr__(self):
87        assert repr(Transform(1, 2, 3, 4, 5, 6)) == '<Transform [1 2 3 4 5 6]>'
88
89    def test_Identity(self):
90        assert isinstance(Identity, Transform)
91        assert Identity == Transform(1, 0, 0, 1, 0, 0)
92
93    def test_Offset(self):
94        assert Offset() == Transform(1, 0, 0, 1, 0, 0)
95        assert Offset(1) == Transform(1, 0, 0, 1, 1, 0)
96        assert Offset(1, 2) == Transform(1, 0, 0, 1, 1, 2)
97
98    def test_Scale(self):
99        assert Scale(1) == Transform(1, 0, 0, 1, 0, 0)
100        assert Scale(2) == Transform(2, 0, 0, 2, 0, 0)
101        assert Scale(1, 2) == Transform(1, 0, 0, 2, 0, 0)
102