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