Debian Codegraph @

Lib/fontMath/test/test_mathTransform.py

raw symbols SCIP
1 import unittest
2 import math
3 from random import random
4 from fontMath.mathFunctions import _roundNumber
5 from fontMath.mathTransform import (
6 Transform, FontMathWarning, matrixToMathTransform, mathTransformToMatrix,
7 _polarDecomposeInterpolationTransformation,
8 _mathPolarDecomposeInterpolationTransformation,
9 _linearInterpolationTransformMatrix
10 )
11
12
13 class MathTransformToFunctionsTest(unittest.TestCase):
14 def __init__(self, methodName):
15 unittest.TestCase.__init__(self, methodName)
16
17 def test_matrixToTransform(self):
18 pass
19
20 def test_TransformToMatrix(self):
21 pass
22
23
24 class ShallowTransformTest(unittest.TestCase):
25 def __init__(self, methodName):
26 unittest.TestCase.__init__(self, methodName)
27
28
29 _testData = [
30 (
31 Transform().rotate(math.radians(0)),
32 Transform().rotate(math.radians(90))
33 ),
34
35 (
36 Transform().skew(math.radians(60), math.radians(10)),
37 Transform().rotate(math.radians(90))
38 ),
39
40 (
41 Transform().scale(.3, 1.3),
42 Transform().rotate(math.radians(90))
43 ),
44
45 (
46 Transform().scale(.3, 1.3).rotate(math.radians(-15)),
47 Transform().rotate(math.radians(90)).scale(.7, .3)
48 ),
49
50 (
51 Transform().translate(250, 250).rotate(math.radians(-15))
52 .translate(-250, -250),
53 Transform().translate(0, 400).rotate(math.radians(80))
54 .translate(-100, 0).rotate(math.radians(80)),
55 ),
56
57 (
58 Transform().skew(math.radians(50)).scale(1.5).rotate(math.radians(60)),
59 Transform().rotate(math.radians(90))
60 ),
61 ]
62
63
64 class MathTransformTest(unittest.TestCase):
65 def __init__(self, methodName):
66 unittest.TestCase.__init__(self, methodName)
67 # Python 3 renamed assertRaisesRegexp to assertRaisesRegex,
68 # and fires deprecation warnings if a program uses the old name.
69 if not hasattr(self, "assertRaisesRegex"):
70 self.assertRaisesRegex = self.assertRaisesRegexp
71
72 # Disabling this test as it fails intermittently and it's not clear what it
73 # does (cf. https://github.com/typesupply/fontMath/issues/35)
74 #
75 # def test_functions(self):
76 # """
77 # In this test various complex transformations are interpolated using
78 # 3 different methods:
79 # - straight linear interpolation, the way glyphMath does it now.
80 # - using the MathTransform interpolation method.
81 # - using the ShallowTransform with an initial decompose and final
82 # compose.
83 # """
84 # value = random()
85 # testFunctions = [
86 # _polarDecomposeInterpolationTransformation,
87 # _mathPolarDecomposeInterpolationTransformation,
88 # _linearInterpolationTransformMatrix,
89 # ]
90 # with self.assertRaisesRegex(
91 # FontMathWarning,
92 # "Minor differences occured when "
93 # "comparing the interpolation functions."):
94 # for i, m in enumerate(_testData):
95 # m1, m2 = m
96 # results = []
97 # for func in testFunctions:
98 # r = func(m1, m2, value)
99 # results.append(r)
100 # if not results[0] == results[1]:
101 # raise FontMathWarning(
102 # "Minor differences occured when "
103 # "comparing the interpolation functions.")
104
105 def _wrapUnWrap(self, precision=12):
106 """
107 Wrap and unwrap a matrix with random values to establish rounding error
108 """
109 t1 = []
110 for i in range(6):
111 t1.append(random())
112 m = matrixToMathTransform(t1)
113 t2 = mathTransformToMatrix(m)
114
115 if not sum([_roundNumber(t1[i] - t2[i], precision)
116 for i in range(len(t1))]) == 0:
117 raise FontMathWarning(
118 "Matrix round-tripping failed for precision value %s."
119 % (precision))
120
121 def test_wrapUnWrap(self):
122 self._wrapUnWrap()
123
124 def test_wrapUnWrapPrecision(self):
125 """
126 Wrap and unwrap should have no rounding errors at least up to
127 a precision value of 12.
128 Rounding errors seem to start occuring at a precision value of 14.
129 """
130 for p in range(5, 13):
131 for i in range(1000):
132 self._wrapUnWrap(p)
133 with self.assertRaisesRegex(
134 FontMathWarning,
135 "Matrix round-tripping failed for precision value"):
136 for p in range(14, 16):
137 for i in range(1000):
138 self._wrapUnWrap(p)