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 | |
paul@366 | 8 | Copyright (C) 2007, 2008, 2012, 2014 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 | |
paul@366 | 36 | default_ns = { |
paul@366 | 37 | "svg" : SVG_NAMESPACE |
paul@366 | 38 | } |
paul@366 | 39 | |
paul@360 | 40 | comma_wsp = re.compile("\s*,\s*|\s+") |
paul@360 | 41 | |
paul@360 | 42 | TYPE_MISMATCH_ERR = 17 |
paul@360 | 43 | |
paul@360 | 44 | class TypeMismatchErr(xml.dom.DOMException): |
paul@360 | 45 | code = TYPE_MISMATCH_ERR |
paul@360 | 46 | |
paulb@256 | 47 | class _Exception(Exception): |
paulb@256 | 48 | |
paulb@256 | 49 | "A generic SVG exception." |
paulb@256 | 50 | |
paulb@256 | 51 | def __init__(self, code): |
paulb@256 | 52 | Exception.__init__(self, code) |
paulb@256 | 53 | self.code = code |
paulb@256 | 54 | |
paulb@256 | 55 | class SVGException(_Exception): |
paulb@256 | 56 | |
paulb@256 | 57 | "An SVG exception." |
paulb@256 | 58 | |
paulb@256 | 59 | SVG_WRONG_TYPE_ERR = 0 |
paulb@256 | 60 | SVG_INVALID_VALUE_ERR = 1 |
paulb@256 | 61 | SVG_MATRIX_NOT_INVERTABLE = 2 |
paulb@256 | 62 | |
paulb@256 | 63 | class GlobalException(_Exception): |
paulb@256 | 64 | |
paulb@256 | 65 | "A global exception." |
paulb@256 | 66 | |
paulb@256 | 67 | NOT_CONNECTED_ERR = 1 |
paulb@256 | 68 | ENCODING_ERR = 2 |
paulb@256 | 69 | DENIED_ERR = 3 |
paulb@256 | 70 | UNKNOWN_ERR = 4 |
paulb@256 | 71 | |
paulb@218 | 72 | class SVGImplementation(libxml2dom.Implementation): |
paulb@218 | 73 | |
paulb@218 | 74 | "Contains an SVG-specific implementation." |
paulb@218 | 75 | |
paulb@235 | 76 | # Wrapping of documents. |
paulb@235 | 77 | |
paulb@235 | 78 | def adoptDocument(self, node): |
paulb@235 | 79 | return SVGDocument(node, self) |
paulb@235 | 80 | |
paulb@235 | 81 | # Factory functions. |
paulb@235 | 82 | |
paulb@218 | 83 | def get_node(self, _node, context_node): |
paulb@218 | 84 | if Node_nodeType(_node) == context_node.ELEMENT_NODE and \ |
paulb@235 | 85 | Node_namespaceURI(_node) == SVG_NAMESPACE: |
paulb@218 | 86 | |
paulb@235 | 87 | if Node_localName(_node) == "svg": |
paulb@235 | 88 | return SVGSVGElement(_node, self, context_node.ownerDocument) |
paulb@235 | 89 | else: |
paulb@235 | 90 | return SVGElement(_node, self, context_node.ownerDocument) |
paulb@218 | 91 | else: |
paulb@218 | 92 | return libxml2dom.Implementation.get_node(self, _node, context_node) |
paulb@218 | 93 | |
paulb@262 | 94 | def get_global(self, doc): |
paulb@262 | 95 | return SVGGlobal(doc) |
paulb@262 | 96 | |
paulb@258 | 97 | # Convenience functions. |
paulb@258 | 98 | |
paulb@258 | 99 | def createSVGDocument(self): |
paulb@258 | 100 | |
paulb@258 | 101 | "Create a new SVG document." |
paulb@258 | 102 | |
paulb@258 | 103 | return SVGDocument(Node_createDocument(SVG_NAMESPACE, "svg", None), self) |
paulb@258 | 104 | |
paulb@256 | 105 | # Interfaces and helper classes. |
paulb@256 | 106 | |
paulb@256 | 107 | class AsyncStatusCallback: |
paulb@256 | 108 | |
paulb@256 | 109 | "An asynchronous callback interface." |
paulb@256 | 110 | |
paulb@256 | 111 | def operationComplete(self, status): |
paulb@256 | 112 | pass |
paulb@256 | 113 | |
paulb@256 | 114 | class AsyncURLStatus: |
paulb@256 | 115 | |
paulb@256 | 116 | "The status of a URL retrieval operation." |
paulb@256 | 117 | |
paulb@256 | 118 | def __init__(self, success, contentType, content): |
paulb@256 | 119 | self.success, self.contentType, self.content = success, contentType, content |
paulb@256 | 120 | |
paulb@258 | 121 | class ElementTraversal: |
paulb@258 | 122 | |
paulb@258 | 123 | "An interface for element traversal." |
paulb@258 | 124 | |
paulb@258 | 125 | def _firstElementChild(self): |
paulb@258 | 126 | l = self.xpath("*") |
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 _lastElementChild(self): |
paulb@258 | 133 | l = self.xpath("*") |
paulb@258 | 134 | if l: |
paulb@258 | 135 | return l[-1] |
paulb@258 | 136 | else: |
paulb@258 | 137 | return None |
paulb@258 | 138 | |
paulb@258 | 139 | def _nextElementSibling(self): |
paulb@258 | 140 | l = self.xpath("following-sibling::*") |
paulb@258 | 141 | if l: |
paulb@258 | 142 | return l[0] |
paulb@258 | 143 | else: |
paulb@258 | 144 | return None |
paulb@258 | 145 | |
paulb@258 | 146 | def _previousElementSibling(self): |
paulb@258 | 147 | l = self.xpath("preceding-sibling::*") |
paulb@258 | 148 | if l: |
paulb@258 | 149 | return l[0] |
paulb@258 | 150 | else: |
paulb@258 | 151 | return None |
paulb@258 | 152 | |
paulb@258 | 153 | firstElementChild = property(_firstElementChild) |
paulb@258 | 154 | lastElementChild = property(_lastElementChild) |
paulb@258 | 155 | nextElementSibling = property(_nextElementSibling) |
paulb@258 | 156 | previousElementSibling = property(_previousElementSibling) |
paulb@258 | 157 | |
paulb@262 | 158 | class EventListenerInitializer2: |
paulb@262 | 159 | |
paulb@262 | 160 | "An event listener initialisation interface." |
paulb@262 | 161 | |
paulb@262 | 162 | def initializeEventListeners(self, scriptElement): |
paulb@262 | 163 | pass |
paulb@262 | 164 | |
paulb@262 | 165 | def createEventListener(self, handlerElement): |
paulb@262 | 166 | pass |
paulb@262 | 167 | |
paulb@262 | 168 | class Global: |
paulb@262 | 169 | |
paulb@262 | 170 | "An empty global interface." |
paulb@262 | 171 | |
paulb@262 | 172 | pass |
paulb@262 | 173 | |
paulb@262 | 174 | class SVGGlobal(Global, EventListenerInitializer2): |
paulb@256 | 175 | |
paulb@256 | 176 | "An SVG global." |
paulb@256 | 177 | |
paulb@256 | 178 | def __init__(self, document): # parent |
paulb@256 | 179 | |
paulb@256 | 180 | "Initialise the global with the given 'document'." |
paulb@256 | 181 | |
paulb@256 | 182 | self.document = document |
paulb@256 | 183 | |
paulb@262 | 184 | # Listener management. |
paulb@262 | 185 | |
paulb@262 | 186 | self.listeners = {} |
paulb@262 | 187 | |
paulb@256 | 188 | def createConnection(self): |
paulb@256 | 189 | raise NotImplementedError, "createConnection" |
paulb@256 | 190 | |
paulb@256 | 191 | def createTimer(self, initialInterval, repeatInterval): |
paulb@256 | 192 | raise NotImplementedError, "createTimer" |
paulb@256 | 193 | |
paulb@256 | 194 | def gotoLocation(self, newIRI): |
paulb@256 | 195 | raise NotImplementedError, "gotoLocation" |
paulb@256 | 196 | |
paulb@256 | 197 | def binaryToString(self, octets, encoding): |
paulb@262 | 198 | try: |
paulb@262 | 199 | return unicode(octets, encoding) |
paulb@262 | 200 | except UnicodeDecodeError, exc: |
paulb@262 | 201 | raise GlobalException(GlobalException.ENCODING_ERR) |
paulb@256 | 202 | |
paulb@256 | 203 | def stringToBinary(self, data, encoding): |
paulb@262 | 204 | try: |
paulb@262 | 205 | return data.encode(encoding) |
paulb@262 | 206 | except UnicodeEncodeError, exc: |
paulb@262 | 207 | raise GlobalException(GlobalException.ENCODING_ERR) |
paulb@256 | 208 | |
paulb@256 | 209 | def getURL(self, iri, callback): |
paulb@256 | 210 | |
paulb@256 | 211 | # NOTE: Not asynchronous. |
paulb@256 | 212 | # NOTE: The urlopen function may not support IRIs. |
paulb@256 | 213 | # No exceptions are supposed to be raised, which is a bit nasty. |
paulb@256 | 214 | |
paulb@256 | 215 | f = urllib.urlopen(iri) |
paulb@256 | 216 | try: |
paulb@256 | 217 | try: |
paulb@256 | 218 | content = f.read() |
paulb@256 | 219 | contentType = f.headers["Content-Type"] |
paulb@256 | 220 | callback.operationComplete(AsyncURLStatus(1, contentType, content)) |
paulb@256 | 221 | except: |
paulb@256 | 222 | callback.operationComplete(AsyncURLStatus(0, None, None)) |
paulb@256 | 223 | finally: |
paulb@256 | 224 | f.close() |
paulb@256 | 225 | |
paulb@262 | 226 | def postURL(self, iri, data, callback, type=None, encoding=None): |
paulb@262 | 227 | |
paulb@262 | 228 | # NOTE: Not asynchronous. |
paulb@262 | 229 | # NOTE: The urlopen function may not support IRIs. |
paulb@262 | 230 | # No exceptions are supposed to be raised, which is a bit nasty. |
paulb@262 | 231 | |
paulb@262 | 232 | opener = urllib.URLopener() |
paulb@262 | 233 | opener.addheader("Content-Type", type or "text/plain") |
paulb@262 | 234 | if encoding: |
paulb@262 | 235 | opener.addheader("Content-Encoding", encoding) |
paulb@262 | 236 | f = opener.open(iri, data) |
paulb@262 | 237 | try: |
paulb@262 | 238 | try: |
paulb@262 | 239 | content = f.read() |
paulb@262 | 240 | contentType = f.headers["Content-Type"] |
paulb@262 | 241 | callback.operationComplete(AsyncURLStatus(1, contentType, content)) |
paulb@262 | 242 | except: |
paulb@262 | 243 | callback.operationComplete(AsyncURLStatus(0, None, None)) |
paulb@262 | 244 | finally: |
paulb@262 | 245 | f.close() |
paulb@262 | 246 | opener.close() |
paulb@256 | 247 | |
paulb@256 | 248 | def parseXML(self, data, contextDoc): |
paulb@256 | 249 | doc = parseString(data) |
paulb@256 | 250 | return contextDoc.importNode(doc.documentElement, 1) |
paulb@256 | 251 | |
paulb@256 | 252 | class SVGLocatable: |
paulb@256 | 253 | |
paulb@256 | 254 | "A locatable interface." |
paulb@256 | 255 | |
paulb@256 | 256 | pass |
paulb@256 | 257 | |
paulb@256 | 258 | class SVGMatrix: |
paulb@256 | 259 | |
paulb@256 | 260 | """ |
paulb@256 | 261 | A matrix. |
paulb@256 | 262 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__SVGMatrix |
paulb@256 | 263 | """ |
paulb@256 | 264 | |
paul@360 | 265 | transform_regexp = re.compile( |
paul@360 | 266 | "(translate|scale|rotate|skewX|skewY|matrix)\((.*?)\)" |
paul@360 | 267 | ) |
paulb@258 | 268 | |
paulb@258 | 269 | def __init__(self, a=0, b=0, c=0, d=0, e=0, f=0): |
paulb@256 | 270 | self.matrix = a, b, c, d, e, f |
paulb@256 | 271 | |
paul@338 | 272 | def __repr__(self): |
paul@338 | 273 | return "SVGMatrix(%f, %f, %f, %f, %f, %f)" % self.matrix |
paul@338 | 274 | |
paulb@258 | 275 | def __eq__(self, other): |
paulb@258 | 276 | return self.matrix == other.matrix |
paulb@258 | 277 | |
paulb@258 | 278 | def __ne__(self, other): |
paulb@258 | 279 | return not (self == other) |
paulb@258 | 280 | |
paulb@258 | 281 | def _get_params(self, param_string): |
paul@360 | 282 | return [float(s.strip()) for s in comma_wsp.split(param_string.strip())] |
paulb@258 | 283 | |
paulb@258 | 284 | def fromNode(self, node, name): |
paulb@258 | 285 | |
paulb@258 | 286 | """ |
paulb@258 | 287 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 288 | 'name'. |
paulb@258 | 289 | """ |
paulb@258 | 290 | |
paulb@258 | 291 | value = node.getAttribute(name) |
paulb@258 | 292 | if value is None: |
paul@360 | 293 | raise xml.dom.NotSupportedErr() |
paul@360 | 294 | |
paul@360 | 295 | last = 0 |
paul@360 | 296 | |
paul@360 | 297 | matrix = 1, 0, 0, 1, 0, 0 |
paulb@258 | 298 | |
paul@360 | 299 | for m in self.transform_regexp.finditer(value): |
paul@360 | 300 | start, end = m.span() |
paulb@258 | 301 | |
paul@360 | 302 | # Make sure that nothing significant was found between the last |
paul@360 | 303 | # match and this one. |
paul@360 | 304 | |
paul@360 | 305 | if value[last:start].strip(): |
paul@360 | 306 | raise TypeMismatchErr() |
paulb@258 | 307 | |
paul@360 | 308 | last = end |
paul@360 | 309 | transform, arguments = m.groups() |
paulb@258 | 310 | |
paul@360 | 311 | # Translation. |
paulb@258 | 312 | |
paul@360 | 313 | if transform == "translate": |
paul@360 | 314 | a, b, c, d = 1, 0, 0, 1 |
paul@360 | 315 | e, f = self._get_params(arguments) |
paul@360 | 316 | |
paul@360 | 317 | # Scaling. |
paulb@258 | 318 | |
paul@360 | 319 | elif transform == "scale": |
paul@360 | 320 | b, c, e, f = 0, 0, 0, 0 |
paul@360 | 321 | a, d = self._get_params(arguments) |
paul@360 | 322 | |
paul@360 | 323 | # Rotation. |
paulb@258 | 324 | |
paul@360 | 325 | elif transform == "rotate": |
paul@360 | 326 | e, f = 0, 0 |
paul@360 | 327 | angle = float(arguments.strip()) |
paul@360 | 328 | a = d = math.cos(math.radians(angle)) |
paul@360 | 329 | b = math.sin(math.radians(angle)) |
paul@360 | 330 | c = -b |
paul@360 | 331 | |
paul@360 | 332 | # Skew. |
paulb@258 | 333 | |
paul@360 | 334 | elif transform == "skewX": |
paul@360 | 335 | a, b, d, e, f = 1, 0, 1, 0, 0 |
paul@360 | 336 | angle = float(arguments.strip()) |
paul@360 | 337 | c = math.tan(math.radians(angle)) |
paul@360 | 338 | |
paul@360 | 339 | elif transform == "skewY": |
paul@360 | 340 | a, c, d, e, f = 1, 0, 1, 0, 0 |
paul@360 | 341 | angle = float(arguments.strip()) |
paul@360 | 342 | b = math.tan(math.radians(angle)) |
paulb@258 | 343 | |
paul@360 | 344 | # Generic. |
paul@360 | 345 | |
paul@360 | 346 | elif transform == "matrix": |
paul@360 | 347 | a, b, c, d, e, f = self._get_params(arguments) |
paul@360 | 348 | |
paul@360 | 349 | else: |
paul@360 | 350 | raise TypeMismatchErr() |
paul@360 | 351 | |
paul@360 | 352 | # Combine the existing matrix with the new one. |
paulb@258 | 353 | |
paul@360 | 354 | matrix = self._multiply(matrix, (a, b, c, d, e, f)) |
paul@360 | 355 | |
paul@360 | 356 | else: |
paul@360 | 357 | # Make sure that nothing significant was found after the final |
paul@360 | 358 | # match. |
paulb@258 | 359 | |
paul@360 | 360 | if value[last:].strip(): |
paul@360 | 361 | raise TypeMismatchErr() |
paulb@258 | 362 | |
paul@360 | 363 | if last != 0: |
paul@360 | 364 | self.matrix = matrix |
paulb@258 | 365 | |
paulb@258 | 366 | def toNode(self, node, name): |
paulb@258 | 367 | |
paulb@258 | 368 | """ |
paulb@258 | 369 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 370 | this object's attributes. |
paulb@258 | 371 | """ |
paulb@258 | 372 | |
paulb@258 | 373 | a, b, c, d, e, f = self.matrix |
paulb@258 | 374 | |
paulb@258 | 375 | # Translation. |
paulb@258 | 376 | |
paulb@258 | 377 | if (a, b, c, d) == (1, 0, 0, 1): |
paulb@258 | 378 | node.setAttribute(name, "translate(%f, %f)" % (e, f)) |
paulb@258 | 379 | |
paulb@258 | 380 | # Scaling. |
paulb@258 | 381 | |
paulb@258 | 382 | elif (b, c, e, f) == (0, 0, 0, 0): |
paulb@258 | 383 | node.setAttribute(name, "scale(%f, %f)" % (a, d)) |
paulb@258 | 384 | |
paulb@258 | 385 | # Rotation. |
paulb@258 | 386 | |
paulb@258 | 387 | elif a == d and b == -c and (e, f) == (0, 0) and math.degrees(math.acos(a)) == math.degrees(math.asin(b)): |
paulb@258 | 388 | node.setAttribute(name, "rotate(%f)" % math.degrees(math.acos(a))) |
paulb@258 | 389 | |
paulb@258 | 390 | # Skew. |
paulb@258 | 391 | |
paulb@258 | 392 | elif (a, b, d, e, f) == (1, 0, 1, 0, 0) and c != 0: |
paulb@258 | 393 | node.setAttribute(name, "skewX(%f)" % math.degrees(math.atan(c))) |
paulb@258 | 394 | |
paulb@258 | 395 | elif (a, c, d, e, f) == (1, 0, 1, 0, 0) and b != 0: |
paulb@258 | 396 | node.setAttribute(name, "skewX(%f)" % math.degrees(math.atan(b))) |
paulb@258 | 397 | |
paulb@258 | 398 | # Generic matrix. |
paulb@258 | 399 | |
paulb@258 | 400 | else: |
paulb@258 | 401 | node.setAttribute(name, "matrix(%f, %f, %f, %f, %f, %f)" % (a, b, c, d, e, f)) |
paulb@258 | 402 | |
paulb@256 | 403 | def getComponent(self, index): |
paulb@256 | 404 | |
paulb@256 | 405 | """ |
paulb@256 | 406 | Return the component with the given 'index' (starting at zero) from the |
paulb@256 | 407 | sequence a, b, c, d, e, f where each element corresponds to the matrix |
paulb@256 | 408 | as follows: |
paulb@256 | 409 | |
paulb@256 | 410 | [ a c e ] |
paulb@256 | 411 | [ b d f ] |
paulb@256 | 412 | [ 0 0 1 ] |
paulb@256 | 413 | """ |
paulb@256 | 414 | |
paulb@256 | 415 | try: |
paulb@256 | 416 | return self.matrix[index] |
paulb@256 | 417 | except IndexError: |
paul@360 | 418 | raise xml.dom.IndexSizeErr() |
paul@360 | 419 | |
paul@360 | 420 | def _multiply(self, matrix1, matrix2): |
paul@360 | 421 | a1, b1, c1, d1, e1, f1 = matrix1 |
paul@360 | 422 | a2, b2, c2, d2, e2, f2 = matrix2 |
paul@360 | 423 | return a1*a2 + c1*b2, b1*a2 + d1*b2, a1*c2 + c1*d2, b1*c2 + d1*d2, a1*e2 + c1*f2 + e1, b1*e2 + d1*f2 + f1 |
paulb@256 | 424 | |
paulb@256 | 425 | def mMultiply(self, secondMatrix): |
paulb@256 | 426 | |
paulb@256 | 427 | """ |
paul@338 | 428 | Post-multiply this matrix with 'secondMatrix' and update its contents to |
paul@338 | 429 | the result of the multiplication operation defined as follows: |
paulb@256 | 430 | |
paul@338 | 431 | [ A C E ] [ a c e ] |
paul@338 | 432 | [ B D F ] [ b d f ] |
paulb@256 | 433 | [ 0 0 1 ] [ 0 0 1 ] |
paulb@256 | 434 | |
paulb@256 | 435 | Return this object as a result. |
paulb@256 | 436 | """ |
paulb@256 | 437 | |
paul@360 | 438 | self.matrix = self._multiply(secondMatrix.matrix, self.matrix) |
paulb@256 | 439 | return self |
paulb@256 | 440 | |
paulb@256 | 441 | def inverse(self): |
paulb@256 | 442 | |
paulb@256 | 443 | """ |
paulb@256 | 444 | det = ad - cb |
paulb@256 | 445 | |
paulb@256 | 446 | See (for example): http://mathworld.wolfram.com/MatrixInverse.html |
paulb@256 | 447 | """ |
paulb@256 | 448 | |
paulb@256 | 449 | det = a*d - c*b |
paulb@256 | 450 | if det != 0: |
paulb@256 | 451 | m = 1/det |
paulb@256 | 452 | a, b, c, d, e, f = self.matrix |
paulb@256 | 453 | self.matrix = m * d, m * -b, m * -c, m * a, m * (c*f - e*d), m * (e*b - a*f) |
paulb@256 | 454 | return self |
paulb@256 | 455 | else: |
paulb@256 | 456 | raise SVGException(SVGException.SVG_MATRIX_NOT_INVERTABLE) |
paulb@256 | 457 | |
paulb@256 | 458 | def mTranslate(self, x, y): |
paulb@256 | 459 | |
paulb@256 | 460 | """ |
paulb@256 | 461 | [ 1 0 x ] |
paulb@256 | 462 | [ 0 1 y ] |
paulb@256 | 463 | [ 0 0 1 ] |
paulb@256 | 464 | """ |
paulb@256 | 465 | |
paulb@256 | 466 | return self.mMultiply(SVGMatrix(1, 0, 0, 1, x, y)) |
paulb@256 | 467 | |
paulb@256 | 468 | def mScale(self, scaleFactor): |
paulb@256 | 469 | |
paulb@256 | 470 | """ |
paulb@256 | 471 | [ scaleFactor 0 0 ] |
paulb@256 | 472 | [ 0 scaleFactor 0 ] |
paulb@256 | 473 | [ 0 0 1 ] |
paulb@256 | 474 | """ |
paulb@256 | 475 | |
paulb@256 | 476 | return self.mMultiply(SVGMatrix(scaleFactor, 0, 0, scaleFactor, 0, 0)) |
paulb@256 | 477 | |
paulb@256 | 478 | def mRotate(self, angle): |
paulb@256 | 479 | |
paulb@256 | 480 | """ |
paulb@256 | 481 | [ cos(angle) -sin(angle) 0 ] |
paulb@256 | 482 | [ sin(angle) cos(angle) 0 ] |
paulb@256 | 483 | [ 0 0 1 ] |
paulb@256 | 484 | """ |
paulb@256 | 485 | |
paulb@258 | 486 | return self.mMultiply( |
paulb@258 | 487 | SVGMatrix( |
paulb@258 | 488 | math.cos(math.radians(angle)), |
paulb@258 | 489 | math.sin(math.radians(angle)), |
paulb@258 | 490 | -math.sin(math.radians(angle)), |
paulb@258 | 491 | math.cos(math.radians(angle)), |
paulb@258 | 492 | 0, 0 |
paulb@258 | 493 | ) |
paulb@258 | 494 | ) |
paulb@256 | 495 | |
paulb@256 | 496 | class SVGPath: |
paulb@256 | 497 | |
paulb@256 | 498 | """ |
paulb@256 | 499 | A path. |
paulb@256 | 500 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__SVGPath |
paulb@256 | 501 | See: http://www.w3.org/TR/SVGMobile12/paths.html |
paulb@256 | 502 | """ |
paulb@256 | 503 | |
paul@360 | 504 | path_regexp = re.compile("([mMzZlLhHvVcCsSqQtTaA])\s*([^mMzZlLhHvVcCsSqQtTaA]*)") |
paul@360 | 505 | |
paul@360 | 506 | # NOTE: Upper case involves absolute coordinates; lower case involves |
paul@360 | 507 | # NOTE: relative coordinates. The IDL constants only seem to represent the |
paul@360 | 508 | # NOTE: former commands. |
paul@360 | 509 | |
paul@360 | 510 | MOVE_TO = ord("M") |
paul@360 | 511 | LINE_TO = ord("L") |
paul@360 | 512 | CURVE_TO = ord("C") |
paul@360 | 513 | QUAD_TO = ord("Q") |
paul@360 | 514 | CLOSE = ord("Z") |
paulb@258 | 515 | |
paulb@258 | 516 | nparams = { |
paul@360 | 517 | "A" : 7, |
paul@360 | 518 | "C" : 6, |
paul@360 | 519 | "H" : 1, |
paul@360 | 520 | "L" : 2, |
paul@360 | 521 | "M" : 2, |
paul@360 | 522 | "Q" : 4, |
paul@360 | 523 | "S" : 4, |
paul@360 | 524 | "T" : 2, |
paul@360 | 525 | "V" : 1, |
paul@360 | 526 | "Z" : 0, |
paulb@258 | 527 | } |
paulb@256 | 528 | |
paulb@256 | 529 | def __init__(self): |
paulb@256 | 530 | self.segments = [] |
paulb@256 | 531 | |
paulb@258 | 532 | def __eq__(self, other): |
paulb@258 | 533 | return self.segments == other.segments |
paulb@258 | 534 | |
paulb@258 | 535 | def __ne__(self, other): |
paulb@258 | 536 | return not (self == other) |
paulb@258 | 537 | |
paulb@258 | 538 | def fromNode(self, node, name): |
paulb@258 | 539 | |
paulb@258 | 540 | """ |
paulb@258 | 541 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 542 | 'name'. |
paulb@258 | 543 | """ |
paulb@258 | 544 | |
paulb@258 | 545 | value = node.getAttribute(name) |
paulb@258 | 546 | if value is None: |
paul@360 | 547 | raise xml.dom.NotSupportedErr() |
paulb@258 | 548 | |
paulb@258 | 549 | # Try and unpack the attribute value. |
paul@360 | 550 | # NOTE: This does not yet satisfy the permissive parsing behaviour |
paul@360 | 551 | # NOTE: described in the specification. |
paul@360 | 552 | # See: http://www.w3.org/TR/SVG11/paths.html#PathDataBNF |
paulb@258 | 553 | |
paulb@258 | 554 | self.segments = [] |
paul@360 | 555 | last = 0 |
paul@360 | 556 | |
paul@360 | 557 | for m in self.path_regexp.finditer(value): |
paul@360 | 558 | start, end = m.span() |
paul@360 | 559 | if value[last:start].strip(): |
paul@360 | 560 | raise TypeMismatchErr() |
paul@360 | 561 | |
paul@360 | 562 | last = end |
paul@360 | 563 | cmd, arguments = m.groups() |
paul@360 | 564 | |
paul@360 | 565 | try: |
paul@360 | 566 | n = self.nparams[cmd.upper()] |
paul@360 | 567 | |
paul@360 | 568 | if arguments.strip(): |
paul@360 | 569 | params = [float(s.strip()) for s in comma_wsp.split(arguments.strip())] |
paul@360 | 570 | else: |
paul@360 | 571 | params = [] |
paul@360 | 572 | |
paul@360 | 573 | if n != 0 and len(params) % n != 0: |
paul@360 | 574 | raise TypeMismatchErr(cmd, arguments) |
paul@360 | 575 | |
paulb@258 | 576 | self.segments.append((cmd, params)) |
paul@360 | 577 | |
paul@360 | 578 | except (IndexError, ValueError): |
paul@360 | 579 | raise TypeMismatchErr(cmd, arguments) |
paul@360 | 580 | |
paul@360 | 581 | else: |
paul@360 | 582 | if value[last:start].strip(): |
paul@360 | 583 | raise TypeMismatchErr() |
paulb@258 | 584 | |
paulb@258 | 585 | def toNode(self, node, name): |
paulb@258 | 586 | |
paulb@258 | 587 | """ |
paulb@258 | 588 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 589 | this object's attributes. |
paulb@258 | 590 | """ |
paulb@258 | 591 | |
paulb@258 | 592 | try: |
paulb@258 | 593 | l = [] |
paulb@258 | 594 | for cmd, params in self.segments: |
paulb@258 | 595 | l.append(unichr(cmd)) |
paulb@258 | 596 | for param in params: |
paulb@258 | 597 | l.append(str(param)) |
paulb@258 | 598 | node.setAttribute(name, " ".join(l)) |
paulb@258 | 599 | except (IndexError, ValueError): |
paul@360 | 600 | raise TypeMismatchErr() |
paulb@258 | 601 | |
paulb@258 | 602 | # Interface methods. |
paulb@258 | 603 | |
paulb@256 | 604 | def _numberOfSegments(self): |
paulb@256 | 605 | return len(self.segments) |
paulb@256 | 606 | |
paulb@256 | 607 | numberOfSegments = property(_numberOfSegments) |
paulb@256 | 608 | |
paulb@256 | 609 | def getSegment(self, cmdIndex): |
paulb@256 | 610 | try: |
paul@360 | 611 | return ord(self.segments[cmdIndex][0]) |
paulb@256 | 612 | except IndexError: |
paul@360 | 613 | raise xml.dom.IndexSizeErr() |
paulb@256 | 614 | |
paulb@256 | 615 | def getSegmentParam(self, cmdIndex, paramIndex): |
paulb@256 | 616 | try: |
paulb@256 | 617 | return self.segments[cmdIndex][1][paramIndex] |
paulb@256 | 618 | except IndexError: |
paul@360 | 619 | raise xml.dom.IndexSizeErr() |
paulb@256 | 620 | |
paulb@256 | 621 | def moveTo(self, x, y): |
paul@360 | 622 | self.segments.append(("M", (x, y))) |
paulb@256 | 623 | |
paulb@256 | 624 | def lineTo(self, x, y): |
paul@360 | 625 | self.segments.append(("L", (x, y))) |
paulb@256 | 626 | |
paulb@256 | 627 | def quadTo(self, x1, y1, x2, y2): |
paul@360 | 628 | self.segments.append(("Q", (x1, y1, x2, y2))) |
paulb@256 | 629 | |
paulb@256 | 630 | def curveTo(self, x1, y1, x2, y2, x3, y3): |
paul@360 | 631 | self.segments.append(("C", (x1, y1, x2, y2, x3, y3))) |
paulb@256 | 632 | |
paulb@256 | 633 | def close(self): |
paul@360 | 634 | self.segments.append(("Z",)) |
paulb@256 | 635 | |
paulb@256 | 636 | class SVGPoint: |
paulb@256 | 637 | |
paulb@258 | 638 | "A point used to provide currentTranslate." |
paulb@256 | 639 | |
paulb@256 | 640 | def __init__(self, x, y): |
paulb@256 | 641 | self.x = x |
paulb@256 | 642 | self.y = y |
paulb@256 | 643 | |
paul@338 | 644 | def __repr__(self): |
paul@338 | 645 | return "SVGPoint(%f, %f)" % (self.x, self.y) |
paul@338 | 646 | |
paulb@256 | 647 | class SVGRect: |
paulb@256 | 648 | |
paulb@256 | 649 | "A rectangle." |
paulb@256 | 650 | |
paulb@258 | 651 | def __init__(self, x=0, y=0, width=0, height=0): |
paulb@256 | 652 | self.x, self.y, self.width, self.height = x, y, width, height |
paulb@256 | 653 | |
paulb@258 | 654 | def __eq__(self, other): |
paulb@258 | 655 | return (self.x, self.y, self.width, self.height) == (other.x, other.y, other.width, other.height) |
paulb@258 | 656 | |
paulb@258 | 657 | def __ne__(self, other): |
paulb@258 | 658 | return not (self == other) |
paulb@258 | 659 | |
paulb@258 | 660 | def fromNode(self, node, name): |
paulb@258 | 661 | |
paulb@258 | 662 | """ |
paulb@258 | 663 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 664 | 'name'. |
paulb@258 | 665 | """ |
paulb@258 | 666 | |
paulb@258 | 667 | value = node.getAttribute(name) |
paulb@258 | 668 | if value is None: |
paul@360 | 669 | raise xml.dom.NotSupportedErr() |
paulb@258 | 670 | try: |
paulb@258 | 671 | values = map(float, value.split()) |
paulb@258 | 672 | self.x, self.y, self.width, self.height = values |
paulb@258 | 673 | except (IndexError, ValueError): |
paul@360 | 674 | raise TypeMismatchErr() |
paulb@258 | 675 | |
paulb@258 | 676 | def toNode(self, node, name): |
paulb@258 | 677 | |
paulb@258 | 678 | """ |
paulb@258 | 679 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 680 | this object's attributes. |
paulb@258 | 681 | """ |
paulb@258 | 682 | |
paulb@258 | 683 | try: |
paulb@258 | 684 | values = map(str, [self.x, self.y, self.width, self.height]) |
paulb@258 | 685 | node.setAttribute(name, " ".join(values)) |
paulb@258 | 686 | except (IndexError, ValueError): |
paul@360 | 687 | raise TypeMismatchErr() |
paulb@258 | 688 | |
paul@338 | 689 | def __repr__(self): |
paul@338 | 690 | return "SVGRect(%f, %f, %f, %f)" % (self.x, self.y, self.width, self.height) |
paul@338 | 691 | |
paulb@256 | 692 | class SVGRGBColor: |
paulb@256 | 693 | |
paul@338 | 694 | """ |
paul@338 | 695 | A colour. |
paul@338 | 696 | See: http://www.w3.org/TR/SVGMobile12/painting.html#colorSyntax |
paul@338 | 697 | """ |
paulb@256 | 698 | |
paulb@256 | 699 | def __init__(self, red, green, blue): |
paulb@256 | 700 | self.red, self.green, self.blue = red, green, blue |
paulb@256 | 701 | |
paul@338 | 702 | def __repr__(self): |
paul@338 | 703 | return "SVGRGBColor(%f, %f, %f)" % (self.red, self.green, self.blue) |
paul@338 | 704 | |
paulb@258 | 705 | class TraitAccess: |
paulb@258 | 706 | |
paulb@258 | 707 | """ |
paulb@258 | 708 | Access to traits stored on elements. |
paulb@258 | 709 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__TraitAccess |
paulb@258 | 710 | """ |
paulb@258 | 711 | |
paulb@258 | 712 | def getPathTrait(self, name): |
paulb@258 | 713 | path = SVGPath() |
paulb@258 | 714 | path.fromNode(self, name) |
paulb@258 | 715 | return path |
paulb@258 | 716 | |
paulb@258 | 717 | def setPathTrait(self, name, path): |
paulb@258 | 718 | path.toNode(self, name) |
paulb@258 | 719 | |
paulb@258 | 720 | def getRectTrait(self, name): |
paulb@258 | 721 | rect = SVGRect() |
paulb@258 | 722 | rect.fromNode(self, name) |
paulb@258 | 723 | return rect |
paulb@258 | 724 | |
paulb@258 | 725 | def setRectTrait(self, name, rect): |
paulb@258 | 726 | rect.toNode(self, name) |
paulb@258 | 727 | |
paulb@258 | 728 | def getMatrixTrait(self, name): |
paul@338 | 729 | if name != "transform": |
paul@360 | 730 | raise xml.dom.NotSupportedErr() |
paulb@258 | 731 | matrix = SVGMatrix() |
paulb@258 | 732 | matrix.fromNode(self, name) |
paulb@258 | 733 | return matrix |
paulb@258 | 734 | |
paulb@258 | 735 | def setMatrixTrait(self, name, matrix): |
paul@338 | 736 | if name != "transform": |
paul@360 | 737 | raise xml.dom.NotSupportedErr() |
paulb@258 | 738 | matrix.toNode(self, name) |
paulb@258 | 739 | |
paulb@256 | 740 | # Node classes. |
paulb@256 | 741 | |
paulb@258 | 742 | class SVGNode(libxml2dom.Node): |
paulb@258 | 743 | |
paul@366 | 744 | "An SVG-specific node." |
paulb@258 | 745 | |
paul@366 | 746 | pass |
paulb@258 | 747 | |
paulb@265 | 748 | # NOTE: DocumentEvent is from DOM Level 3 Events. |
paulb@265 | 749 | # NOTE: EventSystem is a special libxml2dom.events class. |
paulb@265 | 750 | |
paulb@265 | 751 | class SVGDocument(libxml2dom._Document, SVGNode, EventTarget, DocumentEvent, EventSystem): |
paulb@256 | 752 | |
paulb@256 | 753 | "An SVG-specific document node." |
paulb@256 | 754 | |
paul@366 | 755 | def __init__(self, node, impl, namespaces=None): |
paulb@256 | 756 | |
paulb@256 | 757 | """ |
paulb@256 | 758 | Initialise the document with the given 'node', implementation 'impl', |
paulb@256 | 759 | and global (SVGGlobal) details. |
paulb@256 | 760 | """ |
paulb@256 | 761 | |
paul@366 | 762 | libxml2dom._Document.__init__(self, node, impl, None) |
paul@366 | 763 | self._update_namespaces([default_ns, namespaces]) |
paulb@262 | 764 | self.global_ = self.impl.get_global(self) # parent |
paulb@256 | 765 | |
paulb@258 | 766 | class SVGElement(SVGNode, EventTarget, TraitAccess, ElementTraversal): # NOTE: SVGNode instead of Element. |
paulb@235 | 767 | |
paulb@235 | 768 | "An SVG-specific element." |
paulb@235 | 769 | |
paulb@262 | 770 | def __init__(self, *args, **kw): |
paulb@262 | 771 | SVGNode.__init__(self, *args, **kw) |
paulb@262 | 772 | |
paulb@235 | 773 | def _id(self): |
paulb@235 | 774 | return self.getAttribute("id") |
paulb@235 | 775 | |
paulb@235 | 776 | def _setId(self, value): |
paulb@235 | 777 | self.setAttribute("id", value) |
paulb@235 | 778 | |
paulb@235 | 779 | id = property(_id, _setId) |
paulb@235 | 780 | |
paulb@235 | 781 | class SVGLocatableElement(SVGElement, SVGLocatable): |
paulb@235 | 782 | |
paulb@235 | 783 | "A locatable element." |
paulb@235 | 784 | |
paulb@256 | 785 | pass |
paulb@256 | 786 | |
paulb@256 | 787 | class SVGTimedElement(SVGElement): # smil::ElementTimeControl |
paulb@256 | 788 | |
paulb@256 | 789 | "A timed element." |
paulb@256 | 790 | |
paulb@256 | 791 | def __init__(self, *args): |
paulb@256 | 792 | |
paulb@256 | 793 | "Initialise the element with the underlying 'args'." |
paulb@256 | 794 | |
paulb@256 | 795 | SVGElement.__init__(self, *args) |
paulb@256 | 796 | self.document_time = 0 |
paulb@256 | 797 | self.paused = 0 |
paulb@256 | 798 | |
paulb@256 | 799 | def _isPaused(self): |
paulb@256 | 800 | return self.paused |
paulb@256 | 801 | |
paulb@256 | 802 | def pauseElement(self): |
paulb@256 | 803 | self.paused = 1 |
paulb@256 | 804 | |
paulb@256 | 805 | def resumeElement(self): |
paulb@256 | 806 | self.paused = 0 |
paulb@256 | 807 | |
paulb@256 | 808 | class SVGSVGElement(SVGLocatableElement, SVGTimedElement): |
paulb@218 | 809 | |
paulb@218 | 810 | "An SVG-specific top-level element." |
paulb@218 | 811 | |
paulb@218 | 812 | NAV_AUTO = 1 |
paulb@218 | 813 | NAV_NEXT = 2 |
paulb@218 | 814 | NAV_PREV = 3 |
paulb@218 | 815 | NAV_UP = 4 |
paulb@218 | 816 | NAV_UP_RIGHT = 5 |
paulb@218 | 817 | NAV_RIGHT = 6 |
paulb@218 | 818 | NAV_DOWN_RIGHT = 7 |
paulb@218 | 819 | NAV_DOWN = 8 |
paulb@218 | 820 | NAV_DOWN_LEFT = 9 |
paulb@218 | 821 | NAV_LEFT = 10 |
paulb@218 | 822 | NAV_UP_LEFT = 11 |
paulb@218 | 823 | |
paulb@256 | 824 | def __init__(self, *args): |
paulb@256 | 825 | |
paulb@256 | 826 | "Initialise the element with the underlying 'args'." |
paulb@256 | 827 | |
paulb@256 | 828 | SVGTimedElement.__init__(self, *args) |
paulb@256 | 829 | self.scale = 1 |
paulb@256 | 830 | self.rotate = 0 |
paulb@256 | 831 | self.translate = SVGPoint(0, 0) |
paulb@256 | 832 | |
paul@338 | 833 | # NOTE: The scale, rotate and translate properties are not persistent, and |
paul@338 | 834 | # NOTE: are specific to individual objects. |
paul@338 | 835 | |
paulb@256 | 836 | def _currentScale(self): |
paulb@256 | 837 | return self.scale |
paulb@256 | 838 | |
paulb@256 | 839 | def _currentRotate(self): |
paulb@256 | 840 | return self.rotate |
paulb@256 | 841 | |
paulb@256 | 842 | def _currentTranslate(self): |
paulb@256 | 843 | return self.translate |
paulb@256 | 844 | |
paul@338 | 845 | def _setCurrentScale(self, scale): |
paul@338 | 846 | if scale == 0: |
paul@360 | 847 | raise xml.dom.InvalidAccessErr() |
paul@338 | 848 | self.scale = scale |
paul@338 | 849 | |
paul@338 | 850 | def _setCurrentRotate(self, rotate): |
paul@338 | 851 | self.rotate = rotate |
paul@338 | 852 | |
paulb@256 | 853 | def _viewport(self): |
paul@338 | 854 | if self.hasAttribute("viewBox"): |
paul@338 | 855 | return self.getRectTrait("viewBox") |
paul@360 | 856 | elif self.hasAttribute("width") and self.hasAttribute("height"): |
paul@338 | 857 | return SVGRect(0, 0, self._convertMeasurement(self.getAttribute("width")), |
paul@338 | 858 | self._convertMeasurement(self.getAttribute("height"))) |
paul@360 | 859 | else: |
paul@360 | 860 | return None |
paul@338 | 861 | |
paul@338 | 862 | # Utility methods. |
paul@338 | 863 | |
paul@338 | 864 | units = ["in", "cm", "mm", "pt", "pc", "px", "%"] |
paul@338 | 865 | |
paul@338 | 866 | def _convertMeasurement(self, value): |
paul@338 | 867 | value = value.strip() |
paul@338 | 868 | for unit in self.units: |
paul@338 | 869 | if value.endswith(unit): |
paul@338 | 870 | # NOTE: No conversion yet! |
paul@338 | 871 | return float(value[:-len(unit)].strip()) |
paul@338 | 872 | |
paul@360 | 873 | raise TypeMismatchErr() |
paul@338 | 874 | |
paul@338 | 875 | # Standard methods. |
paulb@256 | 876 | |
paulb@256 | 877 | def getCurrentTime(self): |
paulb@256 | 878 | return self.document_time |
paulb@235 | 879 | |
paulb@256 | 880 | def setCurrentTime(self, setCurrentTime): |
paulb@256 | 881 | self.document_time = setCurrentTime |
paulb@256 | 882 | |
paulb@256 | 883 | def createSVGMatrixComponents(self, a, b, c, d, e, f): |
paulb@256 | 884 | return SVGMatrix(a, b, c, d, e, f) |
paulb@256 | 885 | |
paulb@256 | 886 | def createSVGRect(self): |
paulb@258 | 887 | return SVGRect() |
paulb@256 | 888 | |
paulb@256 | 889 | def createSVGPath(self): |
paulb@256 | 890 | return SVGPath() |
paulb@235 | 891 | |
paulb@256 | 892 | def createSVGRGBColor(self, red, green, blue): |
paulb@256 | 893 | return SVGRGBColor(red, green, blue) |
paulb@256 | 894 | |
paulb@256 | 895 | def moveFocus(self, motionType): |
paulb@256 | 896 | raise NotImplementedError, "moveFocus" |
paulb@256 | 897 | |
paulb@256 | 898 | def setFocus(self, object): |
paulb@256 | 899 | raise NotImplementedError, "setFocus" |
paulb@256 | 900 | |
paulb@256 | 901 | def getCurrentFocusedObject(self): |
paulb@256 | 902 | raise NotImplementedError, "getCurrentFocusedObject" |
paulb@256 | 903 | |
paul@338 | 904 | currentScale = property(_currentScale, _setCurrentScale) |
paul@338 | 905 | currentRotate = property(_currentRotate, _setCurrentRotate) |
paulb@256 | 906 | currentTranslate = property(_currentTranslate) |
paulb@256 | 907 | viewport = property(_viewport) |
paulb@235 | 908 | |
paulb@262 | 909 | # Event handler initialisation. |
paulb@262 | 910 | |
paulb@262 | 911 | def initialiseEvents(doc): |
paulb@262 | 912 | |
paulb@262 | 913 | """ |
paulb@262 | 914 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__EventListenerInitializer2 |
paulb@265 | 915 | See: http://www.w3.org/TR/xml-events/#section-listener-element |
paulb@262 | 916 | """ |
paulb@262 | 917 | |
paulb@265 | 918 | # Initialise script element listeners. |
paulb@265 | 919 | |
paulb@262 | 920 | for script in doc.xpath("//svg:script"): |
paulb@262 | 921 | doc.global_.initializeEventListeners(script) |
paulb@265 | 922 | |
paulb@265 | 923 | # Initialise handler element listeners using XML Events. |
paulb@265 | 924 | |
paulb@262 | 925 | for handler in doc.xpath("//svg:handler"): |
paulb@262 | 926 | listener = doc.global_.createEventListener(handler) |
paulb@262 | 927 | |
paulb@265 | 928 | # Attempt to parameterise the registration using the XML Events attributes. |
paulb@265 | 929 | |
paulb@265 | 930 | phase = handler.getAttributeNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "phase") |
paulb@265 | 931 | |
paulb@265 | 932 | # Add the listener for the appropriate type and phases. |
paulb@262 | 933 | |
paulb@262 | 934 | handler.parentNode.addEventListener( |
paulb@262 | 935 | handler.getAttributeNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "event"), |
paulb@262 | 936 | listener, |
paulb@265 | 937 | phase == "capture" |
paulb@262 | 938 | ) |
paulb@262 | 939 | |
paulb@258 | 940 | # Utility functions. |
paulb@258 | 941 | |
paulb@258 | 942 | createDocument = libxml2dom.createDocument |
paulb@258 | 943 | createDocumentType = libxml2dom.createDocumentType |
paulb@258 | 944 | |
paulb@258 | 945 | def createSVGDocument(): |
paulb@258 | 946 | return default_impl.createSVGDocument() |
paulb@235 | 947 | |
paulb@268 | 948 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@268 | 949 | doc = libxml2dom.parse(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@262 | 950 | initialiseEvents(doc) |
paulb@262 | 951 | return doc |
paulb@218 | 952 | |
paulb@268 | 953 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@268 | 954 | doc = libxml2dom.parseFile(filename, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@262 | 955 | initialiseEvents(doc) |
paulb@262 | 956 | return doc |
paulb@218 | 957 | |
paulb@268 | 958 | def parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@268 | 959 | doc = libxml2dom.parseString(s, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@262 | 960 | initialiseEvents(doc) |
paulb@262 | 961 | return doc |
paulb@218 | 962 | |
paulb@268 | 963 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@268 | 964 | doc = libxml2dom.parseURI(uri, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@262 | 965 | initialiseEvents(doc) |
paulb@262 | 966 | return doc |
paulb@218 | 967 | |
paulb@218 | 968 | # Single instance of the implementation. |
paulb@218 | 969 | |
paulb@218 | 970 | default_impl = SVGImplementation() |
paulb@218 | 971 | |
paulb@218 | 972 | # vim: tabstop=4 expandtab shiftwidth=4 |