From ba3371e28272cfad70aaa66a79fcc3ec83eb65cd Mon Sep 17 00:00:00 2001 From: Louis Simard Date: Mon, 21 Feb 2011 20:51:35 -0500 Subject: [PATCH] scour.py: Modify optimizeTransform to remove superfluous []s and ()s. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit testscour.py: Add unit tests for Johan Sundström's fix for bug 722544, "SVG transform matrix() arg order is a1 b1 a2 b2 a3 b3, not a1 a2 a3 b1 b2 b3". unittests/: Edit the unit tests' support files not to have a second line with the correct transformation. This is customarily in testscour.py. --- scour.py | 26 +++--- testscour.py | 92 ++++++++++++++++++- unittests/transform-matrix-is-identity.svg | 1 - unittests/transform-matrix-is-rotate-135.svg | 1 - unittests/transform-matrix-is-rotate-45.svg | 1 - unittests/transform-matrix-is-rotate-90.svg | 1 - .../transform-matrix-is-rotate-neg-135.svg | 1 - .../transform-matrix-is-rotate-neg-45.svg | 1 - .../transform-matrix-is-rotate-neg-90.svg | 1 - unittests/transform-matrix-is-scale-2-3.svg | 1 - unittests/transform-matrix-is-scale-neg-1.svg | 1 - unittests/transform-matrix-is-translate.svg | 1 - unittests/transform-rotate-fold-3args.svg | 1 - unittests/transform-skewX-is-identity.svg | 1 - unittests/transform-skewY-is-identity.svg | 1 - 15 files changed, 102 insertions(+), 29 deletions(-) diff --git a/scour.py b/scour.py index ecbc912..ca862a0 100755 --- a/scour.py +++ b/scour.py @@ -2315,7 +2315,7 @@ def optimizeTransform(transform): # | b d f | translating them | B1 B2 B3 | # |_ 0 0 1 _| to more readable |_ 0 0 1 _| if len(transform) == 1 and transform[0][0] == 'matrix': - matrix = [A1, B1, A2, B2, A3, B3] = transform[0][1] + matrix = A1, B1, A2, B2, A3, B3 = transform[0][1] # |¯ 1 0 0 ¯| # | 0 1 0 | Identity matrix (no transformation) # |_ 0 0 1 _| @@ -2342,7 +2342,7 @@ def optimizeTransform(transform): # FIXME: the "epsilon" term here should really be some function # of the precision of the (sin|cos)_A terms, not 1e-15: and abs((B1 ** 2) + (A1 ** 2) - 1) < Decimal("1e-15")): - [sin_A, cos_A] = [B1, A1] + sin_A, cos_A = B1, A1 # while asin(A) and acos(A) both only have an 180° range # the sign of sin(A) and cos(A) varies across quadrants, # letting us hone in on the angle the matrix represents: @@ -2358,24 +2358,22 @@ def optimizeTransform(transform): A = 180 - A transform[0] = ('rotate', [A]) - # Simplify transformations where numbers are optional. - for [type, args] in transform: + # Simplify transformations where numbers are optional. + for type, args in transform: if type == 'translate': # Only the X coordinate is required for translations. # If the Y coordinate is unspecified, it's 0. - if (len(args) == 2 and args[1] == 0): + if len(args) == 2 and args[1] == 0: del args[1] elif type == 'rotate': # Only the angle is required for rotations. # If the coordinates are unspecified, it's the origin (0, 0). - if (len(args) == 3 and - args[1] == 0 and - args[2] == 0): + if len(args) == 3 and args[1] == args[2] == 0: del args[1:] elif type == 'scale': # Only the X scaling factor is required. # If the Y factor is unspecified, it's the same as X. - if (len(args) == 2 and args[0] == args[1]): + if len(args) == 2 and args[0] == args[1]: del args[1] # Attempt to coalesce runs of the same transformation. @@ -2396,8 +2394,8 @@ def optimizeTransform(transform): # would be safe to multiply together, too. i = 1 while i < len(transform): - [currType, currArgs] = transform[i] - [prevType, prevArgs] = transform[i - 1] + currType, currArgs = transform[i] + prevType, prevArgs = transform[i - 1] if currType == prevType == 'translate': prevArgs[0] += currArgs[0] # x # for y, only add if the second translation has an explicit y @@ -2435,9 +2433,9 @@ def optimizeTransform(transform): del transform[i] elif ((currType == 'skewX' or currType == 'skewY') and len(currArgs) == 1 and currArgs[0] == 0): - # Identity skew! - i -= 1 - del transform[i] + # Identity skew! + i -= 1 + del transform[i] else: i += 1 diff --git a/testscour.py b/testscour.py index e29aa44..3838347 100755 --- a/testscour.py +++ b/testscour.py @@ -1217,12 +1217,100 @@ class RemoveDefsWithWhitespace(unittest.TestCase): self.assertEquals(doc.getElementsByTagName('defs').length, 0, 'Kept defs, although it contains only whitespace or is ') +class TransformIdentityMatrix(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-identity.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), '', + 'Transform containing identity matrix not removed') + +class TransformRotate135(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-rotate-135.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'rotate(135)', + 'Rotation matrix not converted to rotate(135)') + +class TransformRotate45(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-rotate-45.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'rotate(45)', + 'Rotation matrix not converted to rotate(45)') + +class TransformRotate90(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-rotate-90.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'rotate(90)', + 'Rotation matrix not converted to rotate(90)') + +class TransformRotateCCW135(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-rotate-neg-135.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'rotate(-135)', + 'Counter-clockwise rotation matrix not converted to rotate(-135)') + +class TransformRotateCCW45(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-rotate-neg-45.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'rotate(-45)', + 'Counter-clockwise rotation matrix not converted to rotate(-45)') + +class TransformRotateCCW90(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-rotate-neg-90.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'rotate(-90)', + 'Counter-clockwise rotation matrix not converted to rotate(-90)') + +class TransformScale2by3(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-scale-2-3.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'scale(2 3)', + 'Scaling matrix not converted to scale(2 3)') + +class TransformScaleMinus1(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-scale-neg-1.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'scale(-1)', + 'Scaling matrix not converted to scale(-1)') + +class TransformTranslate(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-matrix-is-translate.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'translate(2 3)', + 'Translation matrix not converted to translate(2 3)') + +class TransformRotation3Args(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-rotate-fold-3args.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), 'rotate(90)', + 'Optional zeroes in rotate(angle 0 0) not removed') + +class TransformIdentityRotation(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-rotate-is-identity.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), '', + 'Transform containing identity rotation not removed') + +class TransformIdentitySkewX(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-skewX-is-identity.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), '', + 'Transform containing identity X-axis skew not removed') + +class TransformIdentitySkewY(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-skewY-is-identity.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), '', + 'Transform containing identity Y-axis skew not removed') + +class TransformIdentityTranslate(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/transform-translate-is-identity.svg') + self.assertEquals(doc.getElementsByTagName('line')[0].getAttribute('transform'), '', + 'Transform containing identity translation not removed') + # TODO: write tests for --enable-viewboxing # TODO; write a test for embedding rasters # TODO: write a test for --disable-embed-rasters # TODO: write tests for --keep-editor-data -# TODO: write tests for scouring transformations -# progress: the unittests/transform-* files list expected before/after behaviour if __name__ == '__main__': testcss = __import__('testcss') diff --git a/unittests/transform-matrix-is-identity.svg b/unittests/transform-matrix-is-identity.svg index c49f211..9764b28 100644 --- a/unittests/transform-matrix-is-identity.svg +++ b/unittests/transform-matrix-is-identity.svg @@ -1,4 +1,3 @@ - diff --git a/unittests/transform-matrix-is-rotate-135.svg b/unittests/transform-matrix-is-rotate-135.svg index e17dec5..a0583bc 100644 --- a/unittests/transform-matrix-is-rotate-135.svg +++ b/unittests/transform-matrix-is-rotate-135.svg @@ -1,5 +1,4 @@ - diff --git a/unittests/transform-matrix-is-rotate-45.svg b/unittests/transform-matrix-is-rotate-45.svg index b9361fe..1749d98 100644 --- a/unittests/transform-matrix-is-rotate-45.svg +++ b/unittests/transform-matrix-is-rotate-45.svg @@ -1,5 +1,4 @@ - diff --git a/unittests/transform-matrix-is-rotate-90.svg b/unittests/transform-matrix-is-rotate-90.svg index b46e7e0..269d526 100644 --- a/unittests/transform-matrix-is-rotate-90.svg +++ b/unittests/transform-matrix-is-rotate-90.svg @@ -1,5 +1,4 @@ - diff --git a/unittests/transform-matrix-is-rotate-neg-135.svg b/unittests/transform-matrix-is-rotate-neg-135.svg index 40672c4..1aa21ef 100644 --- a/unittests/transform-matrix-is-rotate-neg-135.svg +++ b/unittests/transform-matrix-is-rotate-neg-135.svg @@ -1,5 +1,4 @@ - diff --git a/unittests/transform-matrix-is-rotate-neg-45.svg b/unittests/transform-matrix-is-rotate-neg-45.svg index 4676969..37b46e8 100644 --- a/unittests/transform-matrix-is-rotate-neg-45.svg +++ b/unittests/transform-matrix-is-rotate-neg-45.svg @@ -1,5 +1,4 @@ - diff --git a/unittests/transform-matrix-is-rotate-neg-90.svg b/unittests/transform-matrix-is-rotate-neg-90.svg index dc48ffd..8fbbd4f 100644 --- a/unittests/transform-matrix-is-rotate-neg-90.svg +++ b/unittests/transform-matrix-is-rotate-neg-90.svg @@ -1,5 +1,4 @@ - diff --git a/unittests/transform-matrix-is-scale-2-3.svg b/unittests/transform-matrix-is-scale-2-3.svg index e09f490..7a04ce5 100644 --- a/unittests/transform-matrix-is-scale-2-3.svg +++ b/unittests/transform-matrix-is-scale-2-3.svg @@ -1,4 +1,3 @@ - diff --git a/unittests/transform-matrix-is-scale-neg-1.svg b/unittests/transform-matrix-is-scale-neg-1.svg index ec55b04..d402058 100644 --- a/unittests/transform-matrix-is-scale-neg-1.svg +++ b/unittests/transform-matrix-is-scale-neg-1.svg @@ -1,5 +1,4 @@ - diff --git a/unittests/transform-matrix-is-translate.svg b/unittests/transform-matrix-is-translate.svg index 6076ecf..0dfcd9d 100644 --- a/unittests/transform-matrix-is-translate.svg +++ b/unittests/transform-matrix-is-translate.svg @@ -1,4 +1,3 @@ - diff --git a/unittests/transform-rotate-fold-3args.svg b/unittests/transform-rotate-fold-3args.svg index 8786e1c..0139610 100644 --- a/unittests/transform-rotate-fold-3args.svg +++ b/unittests/transform-rotate-fold-3args.svg @@ -1,6 +1,5 @@ - diff --git a/unittests/transform-skewX-is-identity.svg b/unittests/transform-skewX-is-identity.svg index eadeb6f..b038c6e 100644 --- a/unittests/transform-skewX-is-identity.svg +++ b/unittests/transform-skewX-is-identity.svg @@ -1,5 +1,4 @@ - diff --git a/unittests/transform-skewY-is-identity.svg b/unittests/transform-skewY-is-identity.svg index 68a4fb2..27da015 100644 --- a/unittests/transform-skewY-is-identity.svg +++ b/unittests/transform-skewY-is-identity.svg @@ -1,5 +1,4 @@ -