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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
import unittest
from svgelements import *
class TestPathMatrix(unittest.TestCase):
"""Tests the functionality of the Matrix element."""
def test_rotate_css_angles(self):
matrix = Matrix("rotate(90, 100,100)")
path = Path("M0,0Z")
path *= matrix
self.assertEqual("M 200,0 Z", path.d())
matrix = Matrix("rotate(90deg, 100,100)")
path = Path("M0,0Z")
path *= matrix
self.assertEqual("M 200,0 Z", path.d())
matrix = Matrix("rotate(0.25turn, 100,100)")
path = Path("M0,0Z")
path *= matrix
self.assertEqual("M 200,0 Z", path.d())
matrix = Matrix("rotate(100grad, 100,100)")
path = Path("M0,0Z")
path *= matrix
self.assertEqual("M 200,0 Z", path.d())
matrix = Matrix("rotate(1.5707963267948966rad, 100,100)")
path = Path("M0,0Z")
path *= matrix
self.assertEqual("M 200,0 Z", path.d())
def test_matrix_multiplication(self):
self.assertEqual(Matrix("scale(0.2) translate(-5,-5)"), Matrix("translate(-5,-5)") * Matrix("scale(0.2)"))
self.assertEqual(Matrix("translate(-5,-5) scale(0.2)"), Matrix("scale(0.2)") * Matrix("translate(-5,-5)"))
def test_rotate_css_distance(self):
matrix = Matrix("rotate(90deg,100cm,100cm)")
matrix.render(ppi=DEFAULT_PPI)
path = Path("M0,0z")
path *= matrix
d = Length("1cm").value(ppi=DEFAULT_PPI)
p2 = Path("M 200,0 Z") * Matrix("scale(%f)" % d)
p2.values[SVG_ATTR_VECTOR_EFFECT] = SVG_VALUE_NON_SCALING_STROKE
self.assertEqual(p2, path)
def test_skew_single_value(self):
m0 = Matrix("skew(15deg,0deg)")
m1 = Matrix("skewX(15deg)")
self.assertEqual(m0, m1)
m0 = Matrix("skew(0deg,15deg)")
m1 = Matrix("skewY(15deg)")
self.assertEqual(m0, m1)
def test_scale_single_value(self):
m0 = Matrix("scale(2,1)")
m1 = Matrix("scaleX(2)")
self.assertEqual(m0, m1)
m0 = Matrix("scale(1,2)")
m1 = Matrix("scaleY(2)")
self.assertEqual(m0, m1)
def test_translate_single_value(self):
m0 = Matrix("translate(500cm,0)")
m1 = Matrix("translateX(500cm)")
self.assertEqual(m0, m1)
m0 = Matrix("translate(0,500cm)")
m1 = Matrix("translateY(500cm)")
self.assertEqual(m0, m1)
m0 = Matrix("translate(500cm)")
m1 = Matrix("translateX(500cm)")
self.assertEqual(m0, m1)
def test_translate_css_value(self):
m0 = Matrix("translate(50mm,5cm)")
m1 = Matrix("translate(5cm,5cm)")
self.assertEqual(m0, m1)
def test_rotate_css_value(self):
m0 = Matrix("rotate(90deg, 50cm,50cm)", ppi=DEFAULT_PPI)
m1 = Matrix("rotate(0.25turn, 500mm,500mm)", ppi=DEFAULT_PPI)
self.assertEqual(m0, m1)
def test_transform_translate(self):
matrix = Matrix("translate(5,4)")
path = Path()
path.move((0, 0), (0, 100), (100, 100), 100 + 0j, "z").closed()
path *= matrix
self.assertEqual("M 5,4 L 5,104 L 105,104 L 105,4 L 5,4 Z", path.d())
def test_transform_scale(self):
matrix = Matrix("scale(2)")
path = Path()
path.move((0, 0), (0, 100), (100, 100), 100 + 0j, 'z').closed()
path *= matrix
self.assertEqual("M 0,0 L 0,200 L 200,200 L 200,0 L 0,0 Z", path.d())
def test_transform_rotate(self):
matrix = Matrix("rotate(360)")
path = Path()
path.move((0, 0), (0, 100), (100, 100), 100 + 0j, "z")
path *= matrix
self.assertAlmostEqual(path[0][1].x, 0)
self.assertAlmostEqual(path[0][1].y, 0)
self.assertAlmostEqual(path[1][1].x, 0)
self.assertAlmostEqual(path[1][1].y, 100)
self.assertAlmostEqual(path[2][1].x, 100)
self.assertAlmostEqual(path[2][1].y, 100)
self.assertAlmostEqual(path[3][1].x, 100)
self.assertAlmostEqual(path[3][1].y, 0)
self.assertAlmostEqual(path[4][1].x, 0)
self.assertAlmostEqual(path[4][1].y, 0)
def test_transform_value(self):
matrix = Matrix("rotate(360,50,50)")
path = Path()
path.move((0, 0), (0, 100), (100, 100), 100 + 0j, "z")
path *= matrix
self.assertAlmostEqual(path[0][1].x, 0)
self.assertAlmostEqual(path[0][1].y, 0)
self.assertAlmostEqual(path[1][1].x, 0)
self.assertAlmostEqual(path[1][1].y, 100)
self.assertAlmostEqual(path[2][1].x, 100)
self.assertAlmostEqual(path[2][1].y, 100)
self.assertAlmostEqual(path[3][1].x, 100)
self.assertAlmostEqual(path[3][1].y, 0)
self.assertAlmostEqual(path[4][1].x, 0)
self.assertAlmostEqual(path[4][1].y, 0)
def test_transform_skewx(self):
matrix = Matrix("skewX(10,50,50)")
path = Path()
path.move((0, 0), (0, 100), (100, 100), 100 + 0j, "z").closed()
path *= matrix
self.assertEqual(
"M -8.81634903542,0 L 8.81634903542,100 L 108.816349035,100 L 91.1836509646,0 L -8.81634903542,0 Z",
path.d())
def test_transform_skewy(self):
matrix = Matrix("skewY(10, 50,50)")
path = Path()
path.move((0, 0), (0, 100), (100, 100), 100 + 0j, "z").closed()
path *= matrix
self.assertEqual(
"M 0,-8.81634903542 L 0,91.1836509646 L 100,108.816349035 L 100,8.81634903542 L 0,-8.81634903542 Z",
path.d())
def test_matrix_repr_rotate(self):
"""
[a c e]
[b d f]
"""
self.assertEqual(Matrix(0, 1, -1, 0, 0, 0), Matrix.rotate(radians(90)))
def test_matrix_repr_scale(self):
"""
[a c e]
[b d f]
"""
self.assertEqual(Matrix(2, 0, 0, 2, 0, 0), Matrix.scale(2))
def test_matrix_repr_hflip(self):
"""
[a c e]
[b d f]
"""
self.assertEqual(Matrix(-1, 0, 0, 1, 0, 0), Matrix.scale(-1, 1))
def test_matrix_repr_vflip(self):
"""
[a c e]
[b d f]
"""
self.assertEqual(Matrix(1, 0, 0, -1, 0, 0), Matrix.scale(1, -1))
def test_matrix_repr_post_cat(self):
"""
[a c e]
[b d f]
"""
m = Matrix.scale(2)
m.post_cat(Matrix.translate(-20, -20))
self.assertEqual(Matrix(2, 0, 0, 2, -20, -20), m)
def test_matrix_repr_pre_cat(self):
"""
[a c e]
[b d f]
"""
m = Matrix.translate(-20, -20)
m.pre_cat(Matrix.scale(2))
self.assertEqual(Matrix(2, 0, 0, 2, -20, -20), m)
def test_matrix_point_rotated_by_matrix(self):
matrix = Matrix()
matrix.post_rotate(radians(90), 100, 100)
p = matrix.point_in_matrix_space((50, 50))
self.assertAlmostEqual(p[0], 150)
self.assertAlmostEqual(p[1], 50)
def test_matrix_point_scaled_in_space(self):
matrix = Matrix()
matrix.post_scale(2, 2, 50, 50)
p = matrix.point_in_matrix_space((50, 50))
self.assertAlmostEqual(p[0], 50)
self.assertAlmostEqual(p[1], 50)
p = matrix.point_in_matrix_space((25, 25))
self.assertAlmostEqual(p[0], 0)
self.assertAlmostEqual(p[1], 0)
matrix.post_rotate(radians(45), 50, 50)
p = matrix.point_in_matrix_space((25, 25))
self.assertAlmostEqual(p[0], 50)
matrix = Matrix()
matrix.post_scale(0.5, 0.5)
p = matrix.point_in_matrix_space((100, 100))
self.assertAlmostEqual(p[0], 50)
self.assertAlmostEqual(p[1], 50)
matrix = Matrix()
matrix.post_scale(2, 2, 100, 100)
p = matrix.point_in_matrix_space((50, 50))
self.assertAlmostEqual(p[0], 0)
self.assertAlmostEqual(p[1], 0)
def test_matrix_cat_identity(self):
identity = Matrix()
from random import random
for i in range(50):
q = Matrix(random(), random(), random(), random(), random(), random())
p = copy(q)
q.post_cat(identity)
self.assertEqual(q, p)
def test_matrix_pre_and_post_1(self):
from random import random
for i in range(50):
tx = random() * 1000 - 500
ty = random() * 1000 - 500
rx = random() * 2
ry = random() * 2
a = random() * tau
q = Matrix()
q.post_translate(tx, ty)
p = Matrix()
p.pre_translate(tx, ty)
self.assertEqual(p, q)
q = Matrix()
q.post_scale(rx, ry, tx, ty)
p = Matrix()
p.pre_scale(rx, ry, tx, ty)
self.assertEqual(p, q)
q = Matrix()
q.post_rotate(a, tx, ty)
p = Matrix()
p.pre_rotate(a, tx, ty)
self.assertEqual(p, q)
q = Matrix()
q.post_skew_x(a, tx, ty)
p = Matrix()
p.pre_skew_x(a, tx, ty)
self.assertEqual(p, q)
q = Matrix()
q.post_skew_y(a, tx, ty)
p = Matrix()
p.pre_skew_y(a, tx, ty)
self.assertEqual(p, q)
def test_matrix_eval_repr(self):
self.assertTrue(Matrix("rotate(20)") == eval(repr(Matrix("rotate(20)"))))
self.assertFalse(Matrix("rotate(20)") != eval(repr(Matrix("rotate(20)"))))
def test_matrix_reverse_scale(self):
m1 = Matrix("scale(2)")
m1.inverse()
m2 = Matrix("scale(0.5)")
self.assertEqual(m1, m2)
m1.inverse()
self.assertEqual(m1, "scale(2)")
def test_matrix_reverse_translate(self):
m1 = Matrix("translate(20,20)")
m1.inverse()
self.assertEqual(m1, Matrix("translate(-20,-20)"))
def test_matrix_reverse_rotate(self):
m1 = Matrix("rotate(30)")
m1.inverse()
self.assertEqual(m1, Matrix("rotate(-30)"))
def test_matrix_reverse_skew(self):
m1 = Matrix("skewX(1)")
m1.inverse()
self.assertEqual(m1, Matrix("skewX(-1)"))
m1 = Matrix("skewY(1)")
m1.inverse()
self.assertEqual(m1, Matrix("skewY(-1)"))
def test_matrix_reverse_scale_translate(self):
m1 = Matrix("scale(2) translate(40,40)")
m1.inverse()
self.assertEqual(m1, Matrix("translate(-40,-40) scale(0.5)"))
|