2.1 --- a/libxml2dom/svg.py Fri Aug 29 00:50:23 2008 +0200
2.2 +++ b/libxml2dom/svg.py Sat Aug 30 00:51:04 2008 +0200
2.3 @@ -261,6 +261,9 @@
2.4 def __init__(self, a=0, b=0, c=0, d=0, e=0, f=0):
2.5 self.matrix = a, b, c, d, e, f
2.6
2.7 + def __repr__(self):
2.8 + return "SVGMatrix(%f, %f, %f, %f, %f, %f)" % self.matrix
2.9 +
2.10 def __eq__(self, other):
2.11 return self.matrix == other.matrix
2.12
2.13 @@ -399,11 +402,11 @@
2.14 def mMultiply(self, secondMatrix):
2.15
2.16 """
2.17 - Multiply this matrix with 'secondMatrix' and update its contents to the
2.18 - result of the multiplication operation defined as follows:
2.19 + Post-multiply this matrix with 'secondMatrix' and update its contents to
2.20 + the result of the multiplication operation defined as follows:
2.21
2.22 - [ a c e ] [ A C E ]
2.23 - [ b d f ] [ B D F ]
2.24 + [ A C E ] [ a c e ]
2.25 + [ B D F ] [ b d f ]
2.26 [ 0 0 1 ] [ 0 0 1 ]
2.27
2.28 Return this object as a result.
2.29 @@ -411,7 +414,7 @@
2.30
2.31 a, b, c, d, e, f = self.matrix
2.32 A, B, C, D, E, F = secondMatrix.matrix
2.33 - self.matrix = a*A + c*B, b*A + d*B, a*C + c*D, b*C + d*D, a*E + c*F + e, b*E + d*F + f
2.34 + self.matrix = A*a + C*b, B*a + D*b, A*c + C*d, B*c + D*d, A*e + C*f + E, B*e + D*f + F
2.35 return self
2.36
2.37 def inverse(self):
2.38 @@ -590,6 +593,9 @@
2.39 self.x = x
2.40 self.y = y
2.41
2.42 + def __repr__(self):
2.43 + return "SVGPoint(%f, %f)" % (self.x, self.y)
2.44 +
2.45 class SVGRect:
2.46
2.47 "A rectangle."
2.48 @@ -632,13 +638,22 @@
2.49 except (IndexError, ValueError):
2.50 raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR)
2.51
2.52 + def __repr__(self):
2.53 + return "SVGRect(%f, %f, %f, %f)" % (self.x, self.y, self.width, self.height)
2.54 +
2.55 class SVGRGBColor:
2.56
2.57 - "A colour."
2.58 + """
2.59 + A colour.
2.60 + See: http://www.w3.org/TR/SVGMobile12/painting.html#colorSyntax
2.61 + """
2.62
2.63 def __init__(self, red, green, blue):
2.64 self.red, self.green, self.blue = red, green, blue
2.65
2.66 + def __repr__(self):
2.67 + return "SVGRGBColor(%f, %f, %f)" % (self.red, self.green, self.blue)
2.68 +
2.69 class TraitAccess:
2.70
2.71 """
2.72 @@ -663,11 +678,15 @@
2.73 rect.toNode(self, name)
2.74
2.75 def getMatrixTrait(self, name):
2.76 + if name != "transform":
2.77 + raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR)
2.78 matrix = SVGMatrix()
2.79 matrix.fromNode(self, name)
2.80 return matrix
2.81
2.82 def setMatrixTrait(self, name, matrix):
2.83 + if name != "transform":
2.84 + raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR)
2.85 matrix.toNode(self, name)
2.86
2.87 # Node classes.
2.88 @@ -773,6 +792,9 @@
2.89 self.rotate = 0
2.90 self.translate = SVGPoint(0, 0)
2.91
2.92 + # NOTE: The scale, rotate and translate properties are not persistent, and
2.93 + # NOTE: are specific to individual objects.
2.94 +
2.95 def _currentScale(self):
2.96 return self.scale
2.97
2.98 @@ -782,8 +804,35 @@
2.99 def _currentTranslate(self):
2.100 return self.translate
2.101
2.102 + def _setCurrentScale(self, scale):
2.103 + if scale == 0:
2.104 + raise xml.dom.DOMException(xml.dom.INVALID_ACCESS_ERR)
2.105 + self.scale = scale
2.106 +
2.107 + def _setCurrentRotate(self, rotate):
2.108 + self.rotate = rotate
2.109 +
2.110 def _viewport(self):
2.111 - return self.getRectTrait("viewBox")
2.112 + if self.hasAttribute("viewBox"):
2.113 + return self.getRectTrait("viewBox")
2.114 + else:
2.115 + return SVGRect(0, 0, self._convertMeasurement(self.getAttribute("width")),
2.116 + self._convertMeasurement(self.getAttribute("height")))
2.117 +
2.118 + # Utility methods.
2.119 +
2.120 + units = ["in", "cm", "mm", "pt", "pc", "px", "%"]
2.121 +
2.122 + def _convertMeasurement(self, value):
2.123 + value = value.strip()
2.124 + for unit in self.units:
2.125 + if value.endswith(unit):
2.126 + # NOTE: No conversion yet!
2.127 + return float(value[:-len(unit)].strip())
2.128 +
2.129 + raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR)
2.130 +
2.131 + # Standard methods.
2.132
2.133 def getCurrentTime(self):
2.134 return self.document_time
2.135 @@ -812,8 +861,8 @@
2.136 def getCurrentFocusedObject(self):
2.137 raise NotImplementedError, "getCurrentFocusedObject"
2.138
2.139 - currentScale = property(_currentScale)
2.140 - currentRotate = property(_currentRotate)
2.141 + currentScale = property(_currentScale, _setCurrentScale)
2.142 + currentRotate = property(_currentRotate, _setCurrentRotate)
2.143 currentTranslate = property(_currentTranslate)
2.144 viewport = property(_viewport)
2.145
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/tests/test_svg_matrix_rotation.py Sat Aug 30 00:51:04 2008 +0200
7.3 @@ -0,0 +1,19 @@
7.4 +#!/usr/bin/env python
7.5 +
7.6 +import libxml2dom.svg
7.7 +
7.8 +# NOTE: Need to incorporate unit conversion.
7.9 +
7.10 +xscale, yscale = 3.75, 3.75
7.11 +
7.12 +d = libxml2dom.svg.parse("tests/test_svg.xml")
7.13 +svg = d.documentElement
7.14 +path = svg.xpath(".//svg:path")[0]
7.15 +m = svg.createSVGMatrixComponents(1, 0, 0, 1, 0, 0)
7.16 +m.mTranslate(-20 * xscale, -30 * yscale)
7.17 +m.mRotate(60)
7.18 +m.mTranslate(20 * xscale, 30 * yscale)
7.19 +path.setMatrixTrait("transform", m)
7.20 +d.toFile(open("tmp_test.svg", "wb"))
7.21 +
7.22 +# vim: tabstop=4 expandtab shiftwidth=4