-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
155 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,97 @@ | ||
import unittest | ||
|
||
from svg import path | ||
from svg.transform import make_matrix | ||
from svg import path, transform | ||
from math import pi, tan | ||
|
||
|
||
class TransformTests(unittest.TestCase): | ||
def test_svg_path_transform(self): | ||
def _confirm_skews(self, original): | ||
for alpha in range(2, 5): | ||
angle = (alpha * 15 * 2 * pi) / 360 | ||
skewx = transform.skewx_matrix(angle) | ||
xformed_x = original.transform(skewx) | ||
skewy = transform.skewy_matrix(angle) | ||
xformed_y = original.transform(skewy) | ||
|
||
for x in range(1, 101): | ||
px = xformed_x.point(x * 0.01) | ||
py = xformed_y.point(x * 0.01) | ||
o = original.point(x * 0.01) | ||
|
||
assert abs(px.real - (tan(angle) * o.imag + o.real)) < 0.00001 | ||
assert abs(px.imag - o.imag) < 0.00001 | ||
|
||
assert abs(py.real - o.real) < 0.00001 | ||
assert abs(py.imag - (tan(angle) * o.real + o.imag)) < 0.00001 | ||
|
||
def test_confirm_line_skews(self): | ||
line = path.Line(0, 100 + 100j) | ||
self._confirm_skews(line) | ||
|
||
def test_confirm_quad_skews(self): | ||
quad = path.QuadraticBezier(0, 100j, 100 + 100j) | ||
self._confirm_skews(quad) | ||
|
||
def test_confirm_cube_skews(self): | ||
cube = path.CubicBezier(0, +100j, 100, 100 + 100j) | ||
self._confirm_skews(cube) | ||
|
||
def test_confirm_arc_skews(self): | ||
arc1 = path.Arc(-100, 100 + 50j, 0, 0, 1, 100) | ||
self._confirm_skews(arc1) | ||
|
||
arc2 = path.Arc(100, 100 + 50j, 0, 0, 1, -100) | ||
self._confirm_skews(arc2) | ||
|
||
def notest_svg_path_transform(self): | ||
line = path.Line(0, 100 + 100j) | ||
xline = line.transform(make_matrix(sx=0.1, sy=0.2)) | ||
assert xline == path.Line(0, 10 + 20j) | ||
linex = line.transform(transform.make_matrix(sx=0.1, sy=0.2)) | ||
assert linex == path.Line(0, 10 + 20j) | ||
|
||
d = path.parser.parse_path("M 750,100 L 250,900 L 1250,900 z") | ||
# Makes it 10% as big in x and 20% as big in y | ||
td = d.transform(make_matrix(sx=0.1, sy=0.2)) | ||
td = d.transform(transform.make_matrix(sx=0.1, sy=0.2)) | ||
assert td.d() == "M 75,20 L 25,180 L 125,180 z" | ||
|
||
d = path.parser.parse_path( | ||
"M 10, 30 A 20, 20 0, 0, 1 50, 30 A 20,20 0, 0, 1 90, 30 Q 90, 60 50, 90 Q 10, 60 10, 30 z" | ||
) | ||
# Makes it 10% as big in x and 20% as big in y | ||
m = ( | ||
transform.rotate_matrix((-10 * 2 * pi) / 360, 50, 100) | ||
@ transform.translate_matrix(-36, 45.5) | ||
@ transform.skewx_matrix((40 * 2 * pi) / 360) | ||
@ transform.scale_matrix(1, 0.5) | ||
) | ||
td = d.transform(m) | ||
# assert ( | ||
# td.d() | ||
# == "M 149.32,82.6809 A 20,20 0 0,1 115.757,104.442 A 20,20 0 0,1 82.1939,126.203 " | ||
# "Q 88.0949,104.5 127.559,61.0359 Q 155.221,60.978 149.32,82.6809 z" | ||
# ) | ||
|
||
# import turtle | ||
# t = turtle.Turtle() | ||
# t.penup() | ||
# arc = path.parser.parse_path( | ||
# "M 90, 30 Q 90, 60 50, 90" | ||
# ) | ||
|
||
# #arc = d | ||
# for m in (#transform.rotate_matrix((-10*2*pi)/360), | ||
# #transform.translate_matrix(-36, 45.5), | ||
# transform.skewx_matrix((40*2*pi)/360), | ||
# transform.scale_matrix(1, 0.5)): | ||
|
||
# p = arc.point(0) | ||
# t.goto(p.real - 1, -p.imag + 1) | ||
# t.dot(3, 'black') | ||
# t.pendown() | ||
# for x in range(1, 101): | ||
# p = arc.point(x * 0.01) | ||
# t.goto(p.real - 1, -p.imag + 1) | ||
# t.penup() | ||
# t.dot(3, 'black') | ||
# arc = arc.transform(m) | ||
|
||
# import pdb;pdb.set_trace() |