paulb@218 | 1 | #!/usr/bin/env python |
paulb@218 | 2 | |
paulb@218 | 3 | """ |
paulb@256 | 4 | SVG-specific document support. |
paulb@256 | 5 | See: http://www.w3.org/TR/SVGMobile12/python-binding.html |
paulb@256 | 6 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html |
paulb@218 | 7 | |
paulb@218 | 8 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@218 | 9 | |
paulb@218 | 10 | This library is free software; you can redistribute it and/or |
paulb@218 | 11 | modify it under the terms of the GNU Lesser General Public |
paulb@218 | 12 | License as published by the Free Software Foundation; either |
paulb@218 | 13 | version 2.1 of the License, or (at your option) any later version. |
paulb@218 | 14 | |
paulb@218 | 15 | This library is distributed in the hope that it will be useful, |
paulb@218 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@218 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@218 | 18 | Lesser General Public License for more details. |
paulb@218 | 19 | |
paulb@218 | 20 | You should have received a copy of the GNU Lesser General Public |
paulb@218 | 21 | License along with this library; if not, write to the Free Software |
paulb@218 | 22 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@218 | 23 | """ |
paulb@218 | 24 | |
paulb@218 | 25 | import libxml2dom |
paulb@256 | 26 | from libxml2dom.events import * |
paulb@218 | 27 | from libxml2dom.macrolib import * |
paulb@258 | 28 | from libxml2dom.macrolib import \ |
paulb@258 | 29 | createDocument as Node_createDocument |
paulb@256 | 30 | import xml.dom |
paulb@256 | 31 | import math |
paulb@258 | 32 | import re |
paulb@218 | 33 | |
paulb@235 | 34 | SVG_NAMESPACE = "http://www.w3.org/2000/svg" |
paulb@235 | 35 | |
paulb@256 | 36 | class _Exception(Exception): |
paulb@256 | 37 | |
paulb@256 | 38 | "A generic SVG exception." |
paulb@256 | 39 | |
paulb@256 | 40 | def __init__(self, code): |
paulb@256 | 41 | Exception.__init__(self, code) |
paulb@256 | 42 | self.code = code |
paulb@256 | 43 | |
paulb@256 | 44 | class SVGException(_Exception): |
paulb@256 | 45 | |
paulb@256 | 46 | "An SVG exception." |
paulb@256 | 47 | |
paulb@256 | 48 | SVG_WRONG_TYPE_ERR = 0 |
paulb@256 | 49 | SVG_INVALID_VALUE_ERR = 1 |
paulb@256 | 50 | SVG_MATRIX_NOT_INVERTABLE = 2 |
paulb@256 | 51 | |
paulb@256 | 52 | class GlobalException(_Exception): |
paulb@256 | 53 | |
paulb@256 | 54 | "A global exception." |
paulb@256 | 55 | |
paulb@256 | 56 | NOT_CONNECTED_ERR = 1 |
paulb@256 | 57 | ENCODING_ERR = 2 |
paulb@256 | 58 | DENIED_ERR = 3 |
paulb@256 | 59 | UNKNOWN_ERR = 4 |
paulb@256 | 60 | |
paulb@218 | 61 | class SVGImplementation(libxml2dom.Implementation): |
paulb@218 | 62 | |
paulb@218 | 63 | "Contains an SVG-specific implementation." |
paulb@218 | 64 | |
paulb@235 | 65 | # Wrapping of documents. |
paulb@235 | 66 | |
paulb@235 | 67 | def adoptDocument(self, node): |
paulb@235 | 68 | return SVGDocument(node, self) |
paulb@235 | 69 | |
paulb@235 | 70 | # Factory functions. |
paulb@235 | 71 | |
paulb@218 | 72 | def get_node(self, _node, context_node): |
paulb@218 | 73 | if Node_nodeType(_node) == context_node.ELEMENT_NODE and \ |
paulb@235 | 74 | Node_namespaceURI(_node) == SVG_NAMESPACE: |
paulb@218 | 75 | |
paulb@235 | 76 | if Node_localName(_node) == "svg": |
paulb@235 | 77 | return SVGSVGElement(_node, self, context_node.ownerDocument) |
paulb@235 | 78 | else: |
paulb@235 | 79 | return SVGElement(_node, self, context_node.ownerDocument) |
paulb@218 | 80 | else: |
paulb@218 | 81 | return libxml2dom.Implementation.get_node(self, _node, context_node) |
paulb@218 | 82 | |
paulb@258 | 83 | # Convenience functions. |
paulb@258 | 84 | |
paulb@258 | 85 | def createSVGDocument(self): |
paulb@258 | 86 | |
paulb@258 | 87 | "Create a new SVG document." |
paulb@258 | 88 | |
paulb@258 | 89 | return SVGDocument(Node_createDocument(SVG_NAMESPACE, "svg", None), self) |
paulb@258 | 90 | |
paulb@256 | 91 | # Interfaces and helper classes. |
paulb@256 | 92 | |
paulb@256 | 93 | class AsyncStatusCallback: |
paulb@256 | 94 | |
paulb@256 | 95 | "An asynchronous callback interface." |
paulb@256 | 96 | |
paulb@256 | 97 | def operationComplete(self, status): |
paulb@256 | 98 | pass |
paulb@256 | 99 | |
paulb@256 | 100 | class AsyncURLStatus: |
paulb@256 | 101 | |
paulb@256 | 102 | "The status of a URL retrieval operation." |
paulb@256 | 103 | |
paulb@256 | 104 | def __init__(self, success, contentType, content): |
paulb@256 | 105 | self.success, self.contentType, self.content = success, contentType, content |
paulb@256 | 106 | |
paulb@258 | 107 | class ElementTraversal: |
paulb@258 | 108 | |
paulb@258 | 109 | "An interface for element traversal." |
paulb@258 | 110 | |
paulb@258 | 111 | def _firstElementChild(self): |
paulb@258 | 112 | l = self.xpath("*") |
paulb@258 | 113 | if l: |
paulb@258 | 114 | return l[0] |
paulb@258 | 115 | else: |
paulb@258 | 116 | return None |
paulb@258 | 117 | |
paulb@258 | 118 | def _lastElementChild(self): |
paulb@258 | 119 | l = self.xpath("*") |
paulb@258 | 120 | if l: |
paulb@258 | 121 | return l[-1] |
paulb@258 | 122 | else: |
paulb@258 | 123 | return None |
paulb@258 | 124 | |
paulb@258 | 125 | def _nextElementSibling(self): |
paulb@258 | 126 | l = self.xpath("following-sibling::*") |
paulb@258 | 127 | if l: |
paulb@258 | 128 | return l[0] |
paulb@258 | 129 | else: |
paulb@258 | 130 | return None |
paulb@258 | 131 | |
paulb@258 | 132 | def _previousElementSibling(self): |
paulb@258 | 133 | l = self.xpath("preceding-sibling::*") |
paulb@258 | 134 | if l: |
paulb@258 | 135 | return l[0] |
paulb@258 | 136 | else: |
paulb@258 | 137 | return None |
paulb@258 | 138 | |
paulb@258 | 139 | firstElementChild = property(_firstElementChild) |
paulb@258 | 140 | lastElementChild = property(_lastElementChild) |
paulb@258 | 141 | nextElementSibling = property(_nextElementSibling) |
paulb@258 | 142 | previousElementSibling = property(_previousElementSibling) |
paulb@258 | 143 | |
paulb@256 | 144 | class SVGGlobal: # Global, EventListenerInitializer2 |
paulb@256 | 145 | |
paulb@256 | 146 | "An SVG global." |
paulb@256 | 147 | |
paulb@256 | 148 | def __init__(self, document): # parent |
paulb@256 | 149 | |
paulb@256 | 150 | "Initialise the global with the given 'document'." |
paulb@256 | 151 | |
paulb@256 | 152 | self.document = document |
paulb@256 | 153 | |
paulb@256 | 154 | def createConnection(self): |
paulb@256 | 155 | raise NotImplementedError, "createConnection" |
paulb@256 | 156 | |
paulb@256 | 157 | def createTimer(self, initialInterval, repeatInterval): |
paulb@256 | 158 | raise NotImplementedError, "createTimer" |
paulb@256 | 159 | |
paulb@256 | 160 | def gotoLocation(self, newIRI): |
paulb@256 | 161 | raise NotImplementedError, "gotoLocation" |
paulb@256 | 162 | |
paulb@256 | 163 | def binaryToString(self, octets, encoding): |
paulb@256 | 164 | raise NotImplementedError, "binaryToString" |
paulb@256 | 165 | |
paulb@256 | 166 | def stringToBinary(self, data, encoding): |
paulb@256 | 167 | raise NotImplementedError, "stringToBinary" |
paulb@256 | 168 | |
paulb@256 | 169 | def getURL(self, iri, callback): |
paulb@256 | 170 | |
paulb@256 | 171 | # NOTE: Not asynchronous. |
paulb@256 | 172 | # NOTE: The urlopen function may not support IRIs. |
paulb@256 | 173 | # No exceptions are supposed to be raised, which is a bit nasty. |
paulb@256 | 174 | |
paulb@256 | 175 | f = urllib.urlopen(iri) |
paulb@256 | 176 | try: |
paulb@256 | 177 | try: |
paulb@256 | 178 | content = f.read() |
paulb@256 | 179 | contentType = f.headers["Content-Type"] |
paulb@256 | 180 | callback.operationComplete(AsyncURLStatus(1, contentType, content)) |
paulb@256 | 181 | except: |
paulb@256 | 182 | callback.operationComplete(AsyncURLStatus(0, None, None)) |
paulb@256 | 183 | finally: |
paulb@256 | 184 | f.close() |
paulb@256 | 185 | |
paulb@256 | 186 | def postURL(self, iri, data, callback, type, encoding): |
paulb@256 | 187 | raise NotImplementedError, "postURL" |
paulb@256 | 188 | |
paulb@256 | 189 | def parseXML(self, data, contextDoc): |
paulb@256 | 190 | doc = parseString(data) |
paulb@256 | 191 | return contextDoc.importNode(doc.documentElement, 1) |
paulb@256 | 192 | |
paulb@256 | 193 | class SVGLocatable: |
paulb@256 | 194 | |
paulb@256 | 195 | "A locatable interface." |
paulb@256 | 196 | |
paulb@256 | 197 | pass |
paulb@256 | 198 | |
paulb@256 | 199 | class SVGMatrix: |
paulb@256 | 200 | |
paulb@256 | 201 | """ |
paulb@256 | 202 | A matrix. |
paulb@256 | 203 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__SVGMatrix |
paulb@256 | 204 | """ |
paulb@256 | 205 | |
paulb@258 | 206 | translate_regexp = re.compile("translate\((.*)\)$") |
paulb@258 | 207 | scale_regexp = re.compile("scale\((.*)\)$") |
paulb@258 | 208 | rotate_regexp = re.compile("rotate\((.*)\)$") |
paulb@258 | 209 | skewX_regexp = re.compile("skewX\((.*)\)$") |
paulb@258 | 210 | skewY_regexp = re.compile("skewY\((.*)\)$") |
paulb@258 | 211 | matrix_regexp = re.compile("matrix\((.*)\)$") |
paulb@258 | 212 | |
paulb@258 | 213 | def __init__(self, a=0, b=0, c=0, d=0, e=0, f=0): |
paulb@256 | 214 | self.matrix = a, b, c, d, e, f |
paulb@256 | 215 | |
paulb@258 | 216 | def __eq__(self, other): |
paulb@258 | 217 | return self.matrix == other.matrix |
paulb@258 | 218 | |
paulb@258 | 219 | def __ne__(self, other): |
paulb@258 | 220 | return not (self == other) |
paulb@258 | 221 | |
paulb@258 | 222 | def _get_params(self, param_string): |
paulb@258 | 223 | return map(float, map(lambda s: s.strip(), param_string.split(","))) |
paulb@258 | 224 | |
paulb@258 | 225 | def fromNode(self, node, name): |
paulb@258 | 226 | |
paulb@258 | 227 | """ |
paulb@258 | 228 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 229 | 'name'. |
paulb@258 | 230 | """ |
paulb@258 | 231 | |
paulb@258 | 232 | value = node.getAttribute(name) |
paulb@258 | 233 | if value is None: |
paulb@258 | 234 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@258 | 235 | |
paulb@258 | 236 | value = value.strip() |
paulb@258 | 237 | |
paulb@258 | 238 | # Translation. |
paulb@258 | 239 | |
paulb@258 | 240 | m = self.translate_regexp.match(value) |
paulb@258 | 241 | if m: |
paulb@258 | 242 | a, b, c, d = 1, 0, 0, 1 |
paulb@258 | 243 | e, f = self._get_params(m.group(1)) |
paulb@258 | 244 | self.matrix = a, b, c, d, e, f |
paulb@258 | 245 | return |
paulb@258 | 246 | |
paulb@258 | 247 | # Scaling. |
paulb@258 | 248 | |
paulb@258 | 249 | m = self.scale_regexp.match(value) |
paulb@258 | 250 | if m: |
paulb@258 | 251 | b, c, e, f = 0, 0, 0, 0 |
paulb@258 | 252 | a, d = self._get_params(m.group(1)) |
paulb@258 | 253 | self.matrix = a, b, c, d, e, f |
paulb@258 | 254 | return |
paulb@258 | 255 | |
paulb@258 | 256 | # Rotation. |
paulb@258 | 257 | |
paulb@258 | 258 | m = self.rotate_regexp.match(value) |
paulb@258 | 259 | if m: |
paulb@258 | 260 | e, f = 0, 0 |
paulb@258 | 261 | angle = float(m.group(1).strip()) |
paulb@258 | 262 | a = d = math.cos(math.radians(angle)) |
paulb@258 | 263 | b = math.sin(math.radians(angle)) |
paulb@258 | 264 | c = -b |
paulb@258 | 265 | self.matrix = a, b, c, d, e, f |
paulb@258 | 266 | return |
paulb@258 | 267 | |
paulb@258 | 268 | # Skew. |
paulb@258 | 269 | |
paulb@258 | 270 | m = self.skewX_regexp.match(value) |
paulb@258 | 271 | if m: |
paulb@258 | 272 | a, b, d, e, f = 1, 0, 1, 0, 0 |
paulb@258 | 273 | angle = float(m.group(1).strip()) |
paulb@258 | 274 | c = math.tan(math.radians(angle)) |
paulb@258 | 275 | self.matrix = a, b, c, d, e, f |
paulb@258 | 276 | return |
paulb@258 | 277 | |
paulb@258 | 278 | m = self.skewY_regexp.match(value) |
paulb@258 | 279 | if m: |
paulb@258 | 280 | a, c, d, e, f = 1, 0, 1, 0, 0 |
paulb@258 | 281 | angle = float(m.group(1).strip()) |
paulb@258 | 282 | b = math.tan(math.radians(angle)) |
paulb@258 | 283 | self.matrix = a, b, c, d, e, f |
paulb@258 | 284 | return |
paulb@258 | 285 | |
paulb@258 | 286 | # Generic. |
paulb@258 | 287 | |
paulb@258 | 288 | m = self.matrix_regexp.match(value) |
paulb@258 | 289 | if m: |
paulb@258 | 290 | self.matrix = self._get_params(m.group(1)) |
paulb@258 | 291 | return |
paulb@258 | 292 | |
paulb@258 | 293 | # Otherwise, complain. |
paulb@258 | 294 | |
paulb@258 | 295 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 296 | |
paulb@258 | 297 | def toNode(self, node, name): |
paulb@258 | 298 | |
paulb@258 | 299 | """ |
paulb@258 | 300 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 301 | this object's attributes. |
paulb@258 | 302 | """ |
paulb@258 | 303 | |
paulb@258 | 304 | a, b, c, d, e, f = self.matrix |
paulb@258 | 305 | |
paulb@258 | 306 | # Translation. |
paulb@258 | 307 | |
paulb@258 | 308 | if (a, b, c, d) == (1, 0, 0, 1): |
paulb@258 | 309 | node.setAttribute(name, "translate(%f, %f)" % (e, f)) |
paulb@258 | 310 | |
paulb@258 | 311 | # Scaling. |
paulb@258 | 312 | |
paulb@258 | 313 | elif (b, c, e, f) == (0, 0, 0, 0): |
paulb@258 | 314 | node.setAttribute(name, "scale(%f, %f)" % (a, d)) |
paulb@258 | 315 | |
paulb@258 | 316 | # Rotation. |
paulb@258 | 317 | |
paulb@258 | 318 | elif a == d and b == -c and (e, f) == (0, 0) and math.degrees(math.acos(a)) == math.degrees(math.asin(b)): |
paulb@258 | 319 | node.setAttribute(name, "rotate(%f)" % math.degrees(math.acos(a))) |
paulb@258 | 320 | |
paulb@258 | 321 | # Skew. |
paulb@258 | 322 | |
paulb@258 | 323 | elif (a, b, d, e, f) == (1, 0, 1, 0, 0) and c != 0: |
paulb@258 | 324 | node.setAttribute(name, "skewX(%f)" % math.degrees(math.atan(c))) |
paulb@258 | 325 | |
paulb@258 | 326 | elif (a, c, d, e, f) == (1, 0, 1, 0, 0) and b != 0: |
paulb@258 | 327 | node.setAttribute(name, "skewX(%f)" % math.degrees(math.atan(b))) |
paulb@258 | 328 | |
paulb@258 | 329 | # Generic matrix. |
paulb@258 | 330 | |
paulb@258 | 331 | else: |
paulb@258 | 332 | node.setAttribute(name, "matrix(%f, %f, %f, %f, %f, %f)" % (a, b, c, d, e, f)) |
paulb@258 | 333 | |
paulb@256 | 334 | def getComponent(self, index): |
paulb@256 | 335 | |
paulb@256 | 336 | """ |
paulb@256 | 337 | Return the component with the given 'index' (starting at zero) from the |
paulb@256 | 338 | sequence a, b, c, d, e, f where each element corresponds to the matrix |
paulb@256 | 339 | as follows: |
paulb@256 | 340 | |
paulb@256 | 341 | [ a c e ] |
paulb@256 | 342 | [ b d f ] |
paulb@256 | 343 | [ 0 0 1 ] |
paulb@256 | 344 | """ |
paulb@256 | 345 | |
paulb@256 | 346 | try: |
paulb@256 | 347 | return self.matrix[index] |
paulb@256 | 348 | except IndexError: |
paulb@256 | 349 | raise xml.dom.DOMException(xml.dom.INDEX_SIZE_ERR) |
paulb@256 | 350 | |
paulb@256 | 351 | def mMultiply(self, secondMatrix): |
paulb@256 | 352 | |
paulb@256 | 353 | """ |
paulb@256 | 354 | Multiply this matrix with 'secondMatrix' and update its contents to the |
paulb@256 | 355 | result of the multiplication operation defined as follows: |
paulb@256 | 356 | |
paulb@256 | 357 | [ a c e ] [ A C E ] |
paulb@256 | 358 | [ b d f ] [ B D F ] |
paulb@256 | 359 | [ 0 0 1 ] [ 0 0 1 ] |
paulb@256 | 360 | |
paulb@256 | 361 | Return this object as a result. |
paulb@256 | 362 | """ |
paulb@256 | 363 | |
paulb@256 | 364 | a, b, c, d, e, f = self.matrix |
paulb@256 | 365 | A, B, C, D, E, F = secondMatrix.matrix |
paulb@256 | 366 | 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 |
paulb@256 | 367 | return self |
paulb@256 | 368 | |
paulb@256 | 369 | def inverse(self): |
paulb@256 | 370 | |
paulb@256 | 371 | """ |
paulb@256 | 372 | det = ad - cb |
paulb@256 | 373 | |
paulb@256 | 374 | See (for example): http://mathworld.wolfram.com/MatrixInverse.html |
paulb@256 | 375 | """ |
paulb@256 | 376 | |
paulb@256 | 377 | det = a*d - c*b |
paulb@256 | 378 | if det != 0: |
paulb@256 | 379 | m = 1/det |
paulb@256 | 380 | a, b, c, d, e, f = self.matrix |
paulb@256 | 381 | self.matrix = m * d, m * -b, m * -c, m * a, m * (c*f - e*d), m * (e*b - a*f) |
paulb@256 | 382 | return self |
paulb@256 | 383 | else: |
paulb@256 | 384 | raise SVGException(SVGException.SVG_MATRIX_NOT_INVERTABLE) |
paulb@256 | 385 | |
paulb@256 | 386 | def mTranslate(self, x, y): |
paulb@256 | 387 | |
paulb@256 | 388 | """ |
paulb@256 | 389 | [ 1 0 x ] |
paulb@256 | 390 | [ 0 1 y ] |
paulb@256 | 391 | [ 0 0 1 ] |
paulb@256 | 392 | """ |
paulb@256 | 393 | |
paulb@256 | 394 | return self.mMultiply(SVGMatrix(1, 0, 0, 1, x, y)) |
paulb@256 | 395 | |
paulb@256 | 396 | def mScale(self, scaleFactor): |
paulb@256 | 397 | |
paulb@256 | 398 | """ |
paulb@256 | 399 | [ scaleFactor 0 0 ] |
paulb@256 | 400 | [ 0 scaleFactor 0 ] |
paulb@256 | 401 | [ 0 0 1 ] |
paulb@256 | 402 | """ |
paulb@256 | 403 | |
paulb@256 | 404 | return self.mMultiply(SVGMatrix(scaleFactor, 0, 0, scaleFactor, 0, 0)) |
paulb@256 | 405 | |
paulb@256 | 406 | def mRotate(self, angle): |
paulb@256 | 407 | |
paulb@256 | 408 | """ |
paulb@256 | 409 | [ cos(angle) -sin(angle) 0 ] |
paulb@256 | 410 | [ sin(angle) cos(angle) 0 ] |
paulb@256 | 411 | [ 0 0 1 ] |
paulb@256 | 412 | """ |
paulb@256 | 413 | |
paulb@258 | 414 | return self.mMultiply( |
paulb@258 | 415 | SVGMatrix( |
paulb@258 | 416 | math.cos(math.radians(angle)), |
paulb@258 | 417 | math.sin(math.radians(angle)), |
paulb@258 | 418 | -math.sin(math.radians(angle)), |
paulb@258 | 419 | math.cos(math.radians(angle)), |
paulb@258 | 420 | 0, 0 |
paulb@258 | 421 | ) |
paulb@258 | 422 | ) |
paulb@256 | 423 | |
paulb@256 | 424 | class SVGPath: |
paulb@256 | 425 | |
paulb@256 | 426 | """ |
paulb@256 | 427 | A path. |
paulb@256 | 428 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__SVGPath |
paulb@256 | 429 | See: http://www.w3.org/TR/SVGMobile12/paths.html |
paulb@256 | 430 | """ |
paulb@256 | 431 | |
paulb@256 | 432 | MOVE_TO = 77 |
paulb@256 | 433 | LINE_TO = 76 |
paulb@256 | 434 | CURVE_TO = 67 |
paulb@256 | 435 | QUAD_TO = 81 |
paulb@256 | 436 | CLOSE = 90 |
paulb@258 | 437 | _CLOSE = 122 # More baggage (name not standard). |
paulb@258 | 438 | |
paulb@258 | 439 | nparams = { |
paulb@258 | 440 | MOVE_TO : 2, |
paulb@258 | 441 | LINE_TO : 2, |
paulb@258 | 442 | CURVE_TO : 6, |
paulb@258 | 443 | QUAD_TO : 4, |
paulb@258 | 444 | CLOSE : 0, |
paulb@258 | 445 | _CLOSE : 0 |
paulb@258 | 446 | } |
paulb@256 | 447 | |
paulb@256 | 448 | def __init__(self): |
paulb@256 | 449 | self.segments = [] |
paulb@256 | 450 | |
paulb@258 | 451 | def __eq__(self, other): |
paulb@258 | 452 | return self.segments == other.segments |
paulb@258 | 453 | |
paulb@258 | 454 | def __ne__(self, other): |
paulb@258 | 455 | return not (self == other) |
paulb@258 | 456 | |
paulb@258 | 457 | def fromNode(self, node, name): |
paulb@258 | 458 | |
paulb@258 | 459 | """ |
paulb@258 | 460 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 461 | 'name'. |
paulb@258 | 462 | """ |
paulb@258 | 463 | |
paulb@258 | 464 | value = node.getAttribute(name) |
paulb@258 | 465 | if value is None: |
paulb@258 | 466 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@258 | 467 | |
paulb@258 | 468 | # Try and unpack the attribute value. |
paulb@258 | 469 | |
paulb@258 | 470 | data = value.split() |
paulb@258 | 471 | self.segments = [] |
paulb@258 | 472 | try: |
paulb@258 | 473 | i = 0 |
paulb@258 | 474 | while i < len(data): |
paulb@258 | 475 | cmd = ord(data[i]) |
paulb@258 | 476 | if cmd == self._CLOSE: |
paulb@258 | 477 | cmd = self.CLOSE |
paulb@258 | 478 | i += 1 |
paulb@258 | 479 | n = self.nparams[cmd] |
paulb@258 | 480 | params = map(float, data[i:i+n]) |
paulb@258 | 481 | self.segments.append((cmd, params)) |
paulb@258 | 482 | i += n |
paulb@258 | 483 | except (IndexError, ValueError): |
paulb@258 | 484 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 485 | |
paulb@258 | 486 | def toNode(self, node, name): |
paulb@258 | 487 | |
paulb@258 | 488 | """ |
paulb@258 | 489 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 490 | this object's attributes. |
paulb@258 | 491 | """ |
paulb@258 | 492 | |
paulb@258 | 493 | try: |
paulb@258 | 494 | l = [] |
paulb@258 | 495 | for cmd, params in self.segments: |
paulb@258 | 496 | l.append(unichr(cmd)) |
paulb@258 | 497 | for param in params: |
paulb@258 | 498 | l.append(str(param)) |
paulb@258 | 499 | node.setAttribute(name, " ".join(l)) |
paulb@258 | 500 | except (IndexError, ValueError): |
paulb@258 | 501 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 502 | |
paulb@258 | 503 | # Interface methods. |
paulb@258 | 504 | |
paulb@256 | 505 | def _numberOfSegments(self): |
paulb@256 | 506 | return len(self.segments) |
paulb@256 | 507 | |
paulb@256 | 508 | numberOfSegments = property(_numberOfSegments) |
paulb@256 | 509 | |
paulb@256 | 510 | def getSegment(self, cmdIndex): |
paulb@256 | 511 | try: |
paulb@256 | 512 | return self.segments[cmdIndex][0] |
paulb@256 | 513 | except IndexError: |
paulb@256 | 514 | raise xml.dom.DOMException(xml.dom.INDEX_SIZE_ERR) |
paulb@256 | 515 | |
paulb@256 | 516 | def getSegmentParam(self, cmdIndex, paramIndex): |
paulb@256 | 517 | try: |
paulb@256 | 518 | return self.segments[cmdIndex][1][paramIndex] |
paulb@256 | 519 | except IndexError: |
paulb@256 | 520 | raise xml.dom.DOMException(xml.dom.INDEX_SIZE_ERR) |
paulb@256 | 521 | |
paulb@256 | 522 | def moveTo(self, x, y): |
paulb@256 | 523 | self.segments.append((self.MOVE_TO, (x, y))) |
paulb@256 | 524 | |
paulb@256 | 525 | def lineTo(self, x, y): |
paulb@256 | 526 | self.segments.append((self.LINE_TO, (x, y))) |
paulb@256 | 527 | |
paulb@256 | 528 | def quadTo(self, x1, y1, x2, y2): |
paulb@256 | 529 | self.segments.append((self.QUAD_TO, (x1, y1, x2, y2))) |
paulb@256 | 530 | |
paulb@256 | 531 | def curveTo(self, x1, y1, x2, y2, x3, y3): |
paulb@256 | 532 | self.segments.append((self.CURVE_TO, (x1, y1, x2, y2, x3, y3))) |
paulb@256 | 533 | |
paulb@256 | 534 | def close(self): |
paulb@256 | 535 | self.segments.append((self.CLOSE,)) |
paulb@256 | 536 | |
paulb@256 | 537 | class SVGPoint: |
paulb@256 | 538 | |
paulb@258 | 539 | "A point used to provide currentTranslate." |
paulb@256 | 540 | |
paulb@256 | 541 | def __init__(self, x, y): |
paulb@256 | 542 | self.x = x |
paulb@256 | 543 | self.y = y |
paulb@256 | 544 | |
paulb@256 | 545 | class SVGRect: |
paulb@256 | 546 | |
paulb@256 | 547 | "A rectangle." |
paulb@256 | 548 | |
paulb@258 | 549 | def __init__(self, x=0, y=0, width=0, height=0): |
paulb@256 | 550 | self.x, self.y, self.width, self.height = x, y, width, height |
paulb@256 | 551 | |
paulb@258 | 552 | def __eq__(self, other): |
paulb@258 | 553 | return (self.x, self.y, self.width, self.height) == (other.x, other.y, other.width, other.height) |
paulb@258 | 554 | |
paulb@258 | 555 | def __ne__(self, other): |
paulb@258 | 556 | return not (self == other) |
paulb@258 | 557 | |
paulb@258 | 558 | def fromNode(self, node, name): |
paulb@258 | 559 | |
paulb@258 | 560 | """ |
paulb@258 | 561 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 562 | 'name'. |
paulb@258 | 563 | """ |
paulb@258 | 564 | |
paulb@258 | 565 | value = node.getAttribute(name) |
paulb@258 | 566 | if value is None: |
paulb@258 | 567 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@258 | 568 | try: |
paulb@258 | 569 | values = map(float, value.split()) |
paulb@258 | 570 | self.x, self.y, self.width, self.height = values |
paulb@258 | 571 | except (IndexError, ValueError): |
paulb@258 | 572 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 573 | |
paulb@258 | 574 | def toNode(self, node, name): |
paulb@258 | 575 | |
paulb@258 | 576 | """ |
paulb@258 | 577 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 578 | this object's attributes. |
paulb@258 | 579 | """ |
paulb@258 | 580 | |
paulb@258 | 581 | try: |
paulb@258 | 582 | values = map(str, [self.x, self.y, self.width, self.height]) |
paulb@258 | 583 | node.setAttribute(name, " ".join(values)) |
paulb@258 | 584 | except (IndexError, ValueError): |
paulb@258 | 585 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 586 | |
paulb@256 | 587 | class SVGRGBColor: |
paulb@256 | 588 | |
paulb@256 | 589 | "A colour." |
paulb@256 | 590 | |
paulb@256 | 591 | def __init__(self, red, green, blue): |
paulb@256 | 592 | self.red, self.green, self.blue = red, green, blue |
paulb@256 | 593 | |
paulb@258 | 594 | class TraitAccess: |
paulb@258 | 595 | |
paulb@258 | 596 | """ |
paulb@258 | 597 | Access to traits stored on elements. |
paulb@258 | 598 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__TraitAccess |
paulb@258 | 599 | """ |
paulb@258 | 600 | |
paulb@258 | 601 | def getPathTrait(self, name): |
paulb@258 | 602 | path = SVGPath() |
paulb@258 | 603 | path.fromNode(self, name) |
paulb@258 | 604 | return path |
paulb@258 | 605 | |
paulb@258 | 606 | def setPathTrait(self, name, path): |
paulb@258 | 607 | path.toNode(self, name) |
paulb@258 | 608 | |
paulb@258 | 609 | def getRectTrait(self, name): |
paulb@258 | 610 | rect = SVGRect() |
paulb@258 | 611 | rect.fromNode(self, name) |
paulb@258 | 612 | return rect |
paulb@258 | 613 | |
paulb@258 | 614 | def setRectTrait(self, name, rect): |
paulb@258 | 615 | rect.toNode(self, name) |
paulb@258 | 616 | |
paulb@258 | 617 | def getMatrixTrait(self, name): |
paulb@258 | 618 | matrix = SVGMatrix() |
paulb@258 | 619 | matrix.fromNode(self, name) |
paulb@258 | 620 | return matrix |
paulb@258 | 621 | |
paulb@258 | 622 | def setMatrixTrait(self, name, matrix): |
paulb@258 | 623 | matrix.toNode(self, name) |
paulb@258 | 624 | |
paulb@256 | 625 | # Node classes. |
paulb@256 | 626 | |
paulb@258 | 627 | class SVGNode(libxml2dom.Node): |
paulb@258 | 628 | |
paulb@258 | 629 | "Convenience modifications to nodes specific to libxml2dom.svg." |
paulb@258 | 630 | |
paulb@258 | 631 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@258 | 632 | |
paulb@258 | 633 | """ |
paulb@258 | 634 | Evaluate the given 'expr' using the optional 'variables' and |
paulb@258 | 635 | 'namespaces'. If not otherwise specified, the "svg" prefix will be bound |
paulb@258 | 636 | to SVG_NAMESPACE as defined in this module. |
paulb@258 | 637 | """ |
paulb@258 | 638 | |
paulb@258 | 639 | namespaces = namespaces or {} |
paulb@258 | 640 | if not namespaces.has_key("svg"): |
paulb@258 | 641 | namespaces["svg"] = SVG_NAMESPACE |
paulb@258 | 642 | return libxml2dom.Node.xpath(self, expr, variables, namespaces) |
paulb@258 | 643 | |
paulb@258 | 644 | class SVGDocument(libxml2dom._Document, SVGNode, DocumentEvent, EventTarget): |
paulb@256 | 645 | |
paulb@256 | 646 | "An SVG-specific document node." |
paulb@256 | 647 | |
paulb@256 | 648 | def __init__(self, node, impl): |
paulb@256 | 649 | |
paulb@256 | 650 | """ |
paulb@256 | 651 | Initialise the document with the given 'node', implementation 'impl', |
paulb@256 | 652 | and global (SVGGlobal) details. |
paulb@256 | 653 | """ |
paulb@256 | 654 | |
paulb@258 | 655 | libxml2dom._Document.__init__(self, node, impl) |
paulb@256 | 656 | self.global_ = SVGGlobal(self) # parent |
paulb@256 | 657 | |
paulb@258 | 658 | class SVGElement(SVGNode, EventTarget, TraitAccess, ElementTraversal): # NOTE: SVGNode instead of Element. |
paulb@235 | 659 | |
paulb@235 | 660 | "An SVG-specific element." |
paulb@235 | 661 | |
paulb@235 | 662 | def _id(self): |
paulb@235 | 663 | return self.getAttribute("id") |
paulb@235 | 664 | |
paulb@235 | 665 | def _setId(self, value): |
paulb@235 | 666 | self.setAttribute("id", value) |
paulb@235 | 667 | |
paulb@235 | 668 | id = property(_id, _setId) |
paulb@235 | 669 | |
paulb@235 | 670 | class SVGLocatableElement(SVGElement, SVGLocatable): |
paulb@235 | 671 | |
paulb@235 | 672 | "A locatable element." |
paulb@235 | 673 | |
paulb@256 | 674 | pass |
paulb@256 | 675 | |
paulb@256 | 676 | class SVGTimedElement(SVGElement): # smil::ElementTimeControl |
paulb@256 | 677 | |
paulb@256 | 678 | "A timed element." |
paulb@256 | 679 | |
paulb@256 | 680 | def __init__(self, *args): |
paulb@256 | 681 | |
paulb@256 | 682 | "Initialise the element with the underlying 'args'." |
paulb@256 | 683 | |
paulb@256 | 684 | SVGElement.__init__(self, *args) |
paulb@256 | 685 | self.document_time = 0 |
paulb@256 | 686 | self.paused = 0 |
paulb@256 | 687 | |
paulb@256 | 688 | def _isPaused(self): |
paulb@256 | 689 | return self.paused |
paulb@256 | 690 | |
paulb@256 | 691 | def pauseElement(self): |
paulb@256 | 692 | self.paused = 1 |
paulb@256 | 693 | |
paulb@256 | 694 | def resumeElement(self): |
paulb@256 | 695 | self.paused = 0 |
paulb@256 | 696 | |
paulb@256 | 697 | class SVGSVGElement(SVGLocatableElement, SVGTimedElement): |
paulb@218 | 698 | |
paulb@218 | 699 | "An SVG-specific top-level element." |
paulb@218 | 700 | |
paulb@218 | 701 | NAV_AUTO = 1 |
paulb@218 | 702 | NAV_NEXT = 2 |
paulb@218 | 703 | NAV_PREV = 3 |
paulb@218 | 704 | NAV_UP = 4 |
paulb@218 | 705 | NAV_UP_RIGHT = 5 |
paulb@218 | 706 | NAV_RIGHT = 6 |
paulb@218 | 707 | NAV_DOWN_RIGHT = 7 |
paulb@218 | 708 | NAV_DOWN = 8 |
paulb@218 | 709 | NAV_DOWN_LEFT = 9 |
paulb@218 | 710 | NAV_LEFT = 10 |
paulb@218 | 711 | NAV_UP_LEFT = 11 |
paulb@218 | 712 | |
paulb@256 | 713 | def __init__(self, *args): |
paulb@256 | 714 | |
paulb@256 | 715 | "Initialise the element with the underlying 'args'." |
paulb@256 | 716 | |
paulb@256 | 717 | SVGTimedElement.__init__(self, *args) |
paulb@256 | 718 | self.scale = 1 |
paulb@256 | 719 | self.rotate = 0 |
paulb@256 | 720 | self.translate = SVGPoint(0, 0) |
paulb@256 | 721 | |
paulb@256 | 722 | def _currentScale(self): |
paulb@256 | 723 | return self.scale |
paulb@256 | 724 | |
paulb@256 | 725 | def _currentRotate(self): |
paulb@256 | 726 | return self.rotate |
paulb@256 | 727 | |
paulb@256 | 728 | def _currentTranslate(self): |
paulb@256 | 729 | return self.translate |
paulb@256 | 730 | |
paulb@256 | 731 | def _viewport(self): |
paulb@258 | 732 | return self.getRectTrait("viewBox") |
paulb@256 | 733 | |
paulb@256 | 734 | def getCurrentTime(self): |
paulb@256 | 735 | return self.document_time |
paulb@235 | 736 | |
paulb@256 | 737 | def setCurrentTime(self, setCurrentTime): |
paulb@256 | 738 | self.document_time = setCurrentTime |
paulb@256 | 739 | |
paulb@256 | 740 | def createSVGMatrixComponents(self, a, b, c, d, e, f): |
paulb@256 | 741 | return SVGMatrix(a, b, c, d, e, f) |
paulb@256 | 742 | |
paulb@256 | 743 | def createSVGRect(self): |
paulb@258 | 744 | return SVGRect() |
paulb@256 | 745 | |
paulb@256 | 746 | def createSVGPath(self): |
paulb@256 | 747 | return SVGPath() |
paulb@235 | 748 | |
paulb@256 | 749 | def createSVGRGBColor(self, red, green, blue): |
paulb@256 | 750 | return SVGRGBColor(red, green, blue) |
paulb@256 | 751 | |
paulb@256 | 752 | def moveFocus(self, motionType): |
paulb@256 | 753 | raise NotImplementedError, "moveFocus" |
paulb@256 | 754 | |
paulb@256 | 755 | def setFocus(self, object): |
paulb@256 | 756 | raise NotImplementedError, "setFocus" |
paulb@256 | 757 | |
paulb@256 | 758 | def getCurrentFocusedObject(self): |
paulb@256 | 759 | raise NotImplementedError, "getCurrentFocusedObject" |
paulb@256 | 760 | |
paulb@256 | 761 | currentScale = property(_currentScale) |
paulb@256 | 762 | currentRotate = property(_currentRotate) |
paulb@256 | 763 | currentTranslate = property(_currentTranslate) |
paulb@256 | 764 | viewport = property(_viewport) |
paulb@235 | 765 | |
paulb@258 | 766 | # Utility functions. |
paulb@258 | 767 | |
paulb@258 | 768 | createDocument = libxml2dom.createDocument |
paulb@258 | 769 | createDocumentType = libxml2dom.createDocumentType |
paulb@258 | 770 | |
paulb@258 | 771 | def createSVGDocument(): |
paulb@258 | 772 | return default_impl.createSVGDocument() |
paulb@235 | 773 | |
paulb@246 | 774 | def parse(stream_or_string, html=0, htmlencoding=None): |
paulb@246 | 775 | return libxml2dom.parse(stream_or_string, html, htmlencoding, default_impl) |
paulb@218 | 776 | |
paulb@246 | 777 | def parseFile(filename, html=0, htmlencoding=None): |
paulb@246 | 778 | return libxml2dom.parseFile(filename, html, htmlencoding, default_impl) |
paulb@218 | 779 | |
paulb@246 | 780 | def parseString(s, html=0, htmlencoding=None): |
paulb@246 | 781 | return libxml2dom.parseString(s, html, htmlencoding, default_impl) |
paulb@218 | 782 | |
paulb@246 | 783 | def parseURI(uri, html=0, htmlencoding=None): |
paulb@246 | 784 | return libxml2dom.parseURI(uri, html, htmlencoding, default_impl) |
paulb@218 | 785 | |
paulb@218 | 786 | # Single instance of the implementation. |
paulb@218 | 787 | |
paulb@218 | 788 | default_impl = SVGImplementation() |
paulb@218 | 789 | |
paulb@218 | 790 | # vim: tabstop=4 expandtab shiftwidth=4 |