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 | |
paul@338 | 264 | def __repr__(self): |
paul@338 | 265 | return "SVGMatrix(%f, %f, %f, %f, %f, %f)" % self.matrix |
paul@338 | 266 | |
paulb@258 | 267 | def __eq__(self, other): |
paulb@258 | 268 | return self.matrix == other.matrix |
paulb@258 | 269 | |
paulb@258 | 270 | def __ne__(self, other): |
paulb@258 | 271 | return not (self == other) |
paulb@258 | 272 | |
paulb@258 | 273 | def _get_params(self, param_string): |
paulb@258 | 274 | return map(float, map(lambda s: s.strip(), param_string.split(","))) |
paulb@258 | 275 | |
paulb@258 | 276 | def fromNode(self, node, name): |
paulb@258 | 277 | |
paulb@258 | 278 | """ |
paulb@258 | 279 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 280 | 'name'. |
paulb@258 | 281 | """ |
paulb@258 | 282 | |
paulb@258 | 283 | value = node.getAttribute(name) |
paulb@258 | 284 | if value is None: |
paulb@258 | 285 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@258 | 286 | |
paulb@258 | 287 | value = value.strip() |
paulb@258 | 288 | |
paulb@258 | 289 | # Translation. |
paulb@258 | 290 | |
paulb@258 | 291 | m = self.translate_regexp.match(value) |
paulb@258 | 292 | if m: |
paulb@258 | 293 | a, b, c, d = 1, 0, 0, 1 |
paulb@258 | 294 | e, f = self._get_params(m.group(1)) |
paulb@258 | 295 | self.matrix = a, b, c, d, e, f |
paulb@258 | 296 | return |
paulb@258 | 297 | |
paulb@258 | 298 | # Scaling. |
paulb@258 | 299 | |
paulb@258 | 300 | m = self.scale_regexp.match(value) |
paulb@258 | 301 | if m: |
paulb@258 | 302 | b, c, e, f = 0, 0, 0, 0 |
paulb@258 | 303 | a, d = self._get_params(m.group(1)) |
paulb@258 | 304 | self.matrix = a, b, c, d, e, f |
paulb@258 | 305 | return |
paulb@258 | 306 | |
paulb@258 | 307 | # Rotation. |
paulb@258 | 308 | |
paulb@258 | 309 | m = self.rotate_regexp.match(value) |
paulb@258 | 310 | if m: |
paulb@258 | 311 | e, f = 0, 0 |
paulb@258 | 312 | angle = float(m.group(1).strip()) |
paulb@258 | 313 | a = d = math.cos(math.radians(angle)) |
paulb@258 | 314 | b = math.sin(math.radians(angle)) |
paulb@258 | 315 | c = -b |
paulb@258 | 316 | self.matrix = a, b, c, d, e, f |
paulb@258 | 317 | return |
paulb@258 | 318 | |
paulb@258 | 319 | # Skew. |
paulb@258 | 320 | |
paulb@258 | 321 | m = self.skewX_regexp.match(value) |
paulb@258 | 322 | if m: |
paulb@258 | 323 | a, b, d, e, f = 1, 0, 1, 0, 0 |
paulb@258 | 324 | angle = float(m.group(1).strip()) |
paulb@258 | 325 | c = math.tan(math.radians(angle)) |
paulb@258 | 326 | self.matrix = a, b, c, d, e, f |
paulb@258 | 327 | return |
paulb@258 | 328 | |
paulb@258 | 329 | m = self.skewY_regexp.match(value) |
paulb@258 | 330 | if m: |
paulb@258 | 331 | a, c, d, e, f = 1, 0, 1, 0, 0 |
paulb@258 | 332 | angle = float(m.group(1).strip()) |
paulb@258 | 333 | b = math.tan(math.radians(angle)) |
paulb@258 | 334 | self.matrix = a, b, c, d, e, f |
paulb@258 | 335 | return |
paulb@258 | 336 | |
paulb@258 | 337 | # Generic. |
paulb@258 | 338 | |
paulb@258 | 339 | m = self.matrix_regexp.match(value) |
paulb@258 | 340 | if m: |
paulb@258 | 341 | self.matrix = self._get_params(m.group(1)) |
paulb@258 | 342 | return |
paulb@258 | 343 | |
paulb@258 | 344 | # Otherwise, complain. |
paulb@258 | 345 | |
paulb@258 | 346 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 347 | |
paulb@258 | 348 | def toNode(self, node, name): |
paulb@258 | 349 | |
paulb@258 | 350 | """ |
paulb@258 | 351 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 352 | this object's attributes. |
paulb@258 | 353 | """ |
paulb@258 | 354 | |
paulb@258 | 355 | a, b, c, d, e, f = self.matrix |
paulb@258 | 356 | |
paulb@258 | 357 | # Translation. |
paulb@258 | 358 | |
paulb@258 | 359 | if (a, b, c, d) == (1, 0, 0, 1): |
paulb@258 | 360 | node.setAttribute(name, "translate(%f, %f)" % (e, f)) |
paulb@258 | 361 | |
paulb@258 | 362 | # Scaling. |
paulb@258 | 363 | |
paulb@258 | 364 | elif (b, c, e, f) == (0, 0, 0, 0): |
paulb@258 | 365 | node.setAttribute(name, "scale(%f, %f)" % (a, d)) |
paulb@258 | 366 | |
paulb@258 | 367 | # Rotation. |
paulb@258 | 368 | |
paulb@258 | 369 | elif a == d and b == -c and (e, f) == (0, 0) and math.degrees(math.acos(a)) == math.degrees(math.asin(b)): |
paulb@258 | 370 | node.setAttribute(name, "rotate(%f)" % math.degrees(math.acos(a))) |
paulb@258 | 371 | |
paulb@258 | 372 | # Skew. |
paulb@258 | 373 | |
paulb@258 | 374 | elif (a, b, d, e, f) == (1, 0, 1, 0, 0) and c != 0: |
paulb@258 | 375 | node.setAttribute(name, "skewX(%f)" % math.degrees(math.atan(c))) |
paulb@258 | 376 | |
paulb@258 | 377 | elif (a, c, d, e, f) == (1, 0, 1, 0, 0) and b != 0: |
paulb@258 | 378 | node.setAttribute(name, "skewX(%f)" % math.degrees(math.atan(b))) |
paulb@258 | 379 | |
paulb@258 | 380 | # Generic matrix. |
paulb@258 | 381 | |
paulb@258 | 382 | else: |
paulb@258 | 383 | node.setAttribute(name, "matrix(%f, %f, %f, %f, %f, %f)" % (a, b, c, d, e, f)) |
paulb@258 | 384 | |
paulb@256 | 385 | def getComponent(self, index): |
paulb@256 | 386 | |
paulb@256 | 387 | """ |
paulb@256 | 388 | Return the component with the given 'index' (starting at zero) from the |
paulb@256 | 389 | sequence a, b, c, d, e, f where each element corresponds to the matrix |
paulb@256 | 390 | as follows: |
paulb@256 | 391 | |
paulb@256 | 392 | [ a c e ] |
paulb@256 | 393 | [ b d f ] |
paulb@256 | 394 | [ 0 0 1 ] |
paulb@256 | 395 | """ |
paulb@256 | 396 | |
paulb@256 | 397 | try: |
paulb@256 | 398 | return self.matrix[index] |
paulb@256 | 399 | except IndexError: |
paulb@256 | 400 | raise xml.dom.DOMException(xml.dom.INDEX_SIZE_ERR) |
paulb@256 | 401 | |
paulb@256 | 402 | def mMultiply(self, secondMatrix): |
paulb@256 | 403 | |
paulb@256 | 404 | """ |
paul@338 | 405 | Post-multiply this matrix with 'secondMatrix' and update its contents to |
paul@338 | 406 | the result of the multiplication operation defined as follows: |
paulb@256 | 407 | |
paul@338 | 408 | [ A C E ] [ a c e ] |
paul@338 | 409 | [ B D F ] [ b d f ] |
paulb@256 | 410 | [ 0 0 1 ] [ 0 0 1 ] |
paulb@256 | 411 | |
paulb@256 | 412 | Return this object as a result. |
paulb@256 | 413 | """ |
paulb@256 | 414 | |
paulb@256 | 415 | a, b, c, d, e, f = self.matrix |
paulb@256 | 416 | A, B, C, D, E, F = secondMatrix.matrix |
paul@338 | 417 | 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 | 418 | return self |
paulb@256 | 419 | |
paulb@256 | 420 | def inverse(self): |
paulb@256 | 421 | |
paulb@256 | 422 | """ |
paulb@256 | 423 | det = ad - cb |
paulb@256 | 424 | |
paulb@256 | 425 | See (for example): http://mathworld.wolfram.com/MatrixInverse.html |
paulb@256 | 426 | """ |
paulb@256 | 427 | |
paulb@256 | 428 | det = a*d - c*b |
paulb@256 | 429 | if det != 0: |
paulb@256 | 430 | m = 1/det |
paulb@256 | 431 | a, b, c, d, e, f = self.matrix |
paulb@256 | 432 | self.matrix = m * d, m * -b, m * -c, m * a, m * (c*f - e*d), m * (e*b - a*f) |
paulb@256 | 433 | return self |
paulb@256 | 434 | else: |
paulb@256 | 435 | raise SVGException(SVGException.SVG_MATRIX_NOT_INVERTABLE) |
paulb@256 | 436 | |
paulb@256 | 437 | def mTranslate(self, x, y): |
paulb@256 | 438 | |
paulb@256 | 439 | """ |
paulb@256 | 440 | [ 1 0 x ] |
paulb@256 | 441 | [ 0 1 y ] |
paulb@256 | 442 | [ 0 0 1 ] |
paulb@256 | 443 | """ |
paulb@256 | 444 | |
paulb@256 | 445 | return self.mMultiply(SVGMatrix(1, 0, 0, 1, x, y)) |
paulb@256 | 446 | |
paulb@256 | 447 | def mScale(self, scaleFactor): |
paulb@256 | 448 | |
paulb@256 | 449 | """ |
paulb@256 | 450 | [ scaleFactor 0 0 ] |
paulb@256 | 451 | [ 0 scaleFactor 0 ] |
paulb@256 | 452 | [ 0 0 1 ] |
paulb@256 | 453 | """ |
paulb@256 | 454 | |
paulb@256 | 455 | return self.mMultiply(SVGMatrix(scaleFactor, 0, 0, scaleFactor, 0, 0)) |
paulb@256 | 456 | |
paulb@256 | 457 | def mRotate(self, angle): |
paulb@256 | 458 | |
paulb@256 | 459 | """ |
paulb@256 | 460 | [ cos(angle) -sin(angle) 0 ] |
paulb@256 | 461 | [ sin(angle) cos(angle) 0 ] |
paulb@256 | 462 | [ 0 0 1 ] |
paulb@256 | 463 | """ |
paulb@256 | 464 | |
paulb@258 | 465 | return self.mMultiply( |
paulb@258 | 466 | SVGMatrix( |
paulb@258 | 467 | math.cos(math.radians(angle)), |
paulb@258 | 468 | math.sin(math.radians(angle)), |
paulb@258 | 469 | -math.sin(math.radians(angle)), |
paulb@258 | 470 | math.cos(math.radians(angle)), |
paulb@258 | 471 | 0, 0 |
paulb@258 | 472 | ) |
paulb@258 | 473 | ) |
paulb@256 | 474 | |
paulb@256 | 475 | class SVGPath: |
paulb@256 | 476 | |
paulb@256 | 477 | """ |
paulb@256 | 478 | A path. |
paulb@256 | 479 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__SVGPath |
paulb@256 | 480 | See: http://www.w3.org/TR/SVGMobile12/paths.html |
paulb@256 | 481 | """ |
paulb@256 | 482 | |
paulb@256 | 483 | MOVE_TO = 77 |
paulb@256 | 484 | LINE_TO = 76 |
paulb@256 | 485 | CURVE_TO = 67 |
paulb@256 | 486 | QUAD_TO = 81 |
paulb@256 | 487 | CLOSE = 90 |
paulb@258 | 488 | _CLOSE = 122 # More baggage (name not standard). |
paulb@258 | 489 | |
paulb@258 | 490 | nparams = { |
paulb@258 | 491 | MOVE_TO : 2, |
paulb@258 | 492 | LINE_TO : 2, |
paulb@258 | 493 | CURVE_TO : 6, |
paulb@258 | 494 | QUAD_TO : 4, |
paulb@258 | 495 | CLOSE : 0, |
paulb@258 | 496 | _CLOSE : 0 |
paulb@258 | 497 | } |
paulb@256 | 498 | |
paulb@256 | 499 | def __init__(self): |
paulb@256 | 500 | self.segments = [] |
paulb@256 | 501 | |
paulb@258 | 502 | def __eq__(self, other): |
paulb@258 | 503 | return self.segments == other.segments |
paulb@258 | 504 | |
paulb@258 | 505 | def __ne__(self, other): |
paulb@258 | 506 | return not (self == other) |
paulb@258 | 507 | |
paulb@258 | 508 | def fromNode(self, node, name): |
paulb@258 | 509 | |
paulb@258 | 510 | """ |
paulb@258 | 511 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 512 | 'name'. |
paulb@258 | 513 | """ |
paulb@258 | 514 | |
paulb@258 | 515 | value = node.getAttribute(name) |
paulb@258 | 516 | if value is None: |
paulb@258 | 517 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@258 | 518 | |
paulb@258 | 519 | # Try and unpack the attribute value. |
paulb@258 | 520 | |
paulb@258 | 521 | data = value.split() |
paulb@258 | 522 | self.segments = [] |
paulb@258 | 523 | try: |
paulb@258 | 524 | i = 0 |
paulb@258 | 525 | while i < len(data): |
paulb@258 | 526 | cmd = ord(data[i]) |
paulb@258 | 527 | if cmd == self._CLOSE: |
paulb@258 | 528 | cmd = self.CLOSE |
paulb@258 | 529 | i += 1 |
paulb@258 | 530 | n = self.nparams[cmd] |
paulb@258 | 531 | params = map(float, data[i:i+n]) |
paulb@258 | 532 | self.segments.append((cmd, params)) |
paulb@258 | 533 | i += n |
paulb@258 | 534 | except (IndexError, ValueError): |
paulb@258 | 535 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 536 | |
paulb@258 | 537 | def toNode(self, node, name): |
paulb@258 | 538 | |
paulb@258 | 539 | """ |
paulb@258 | 540 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 541 | this object's attributes. |
paulb@258 | 542 | """ |
paulb@258 | 543 | |
paulb@258 | 544 | try: |
paulb@258 | 545 | l = [] |
paulb@258 | 546 | for cmd, params in self.segments: |
paulb@258 | 547 | l.append(unichr(cmd)) |
paulb@258 | 548 | for param in params: |
paulb@258 | 549 | l.append(str(param)) |
paulb@258 | 550 | node.setAttribute(name, " ".join(l)) |
paulb@258 | 551 | except (IndexError, ValueError): |
paulb@258 | 552 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 553 | |
paulb@258 | 554 | # Interface methods. |
paulb@258 | 555 | |
paulb@256 | 556 | def _numberOfSegments(self): |
paulb@256 | 557 | return len(self.segments) |
paulb@256 | 558 | |
paulb@256 | 559 | numberOfSegments = property(_numberOfSegments) |
paulb@256 | 560 | |
paulb@256 | 561 | def getSegment(self, cmdIndex): |
paulb@256 | 562 | try: |
paulb@256 | 563 | return self.segments[cmdIndex][0] |
paulb@256 | 564 | except IndexError: |
paulb@256 | 565 | raise xml.dom.DOMException(xml.dom.INDEX_SIZE_ERR) |
paulb@256 | 566 | |
paulb@256 | 567 | def getSegmentParam(self, cmdIndex, paramIndex): |
paulb@256 | 568 | try: |
paulb@256 | 569 | return self.segments[cmdIndex][1][paramIndex] |
paulb@256 | 570 | except IndexError: |
paulb@256 | 571 | raise xml.dom.DOMException(xml.dom.INDEX_SIZE_ERR) |
paulb@256 | 572 | |
paulb@256 | 573 | def moveTo(self, x, y): |
paulb@256 | 574 | self.segments.append((self.MOVE_TO, (x, y))) |
paulb@256 | 575 | |
paulb@256 | 576 | def lineTo(self, x, y): |
paulb@256 | 577 | self.segments.append((self.LINE_TO, (x, y))) |
paulb@256 | 578 | |
paulb@256 | 579 | def quadTo(self, x1, y1, x2, y2): |
paulb@256 | 580 | self.segments.append((self.QUAD_TO, (x1, y1, x2, y2))) |
paulb@256 | 581 | |
paulb@256 | 582 | def curveTo(self, x1, y1, x2, y2, x3, y3): |
paulb@256 | 583 | self.segments.append((self.CURVE_TO, (x1, y1, x2, y2, x3, y3))) |
paulb@256 | 584 | |
paulb@256 | 585 | def close(self): |
paulb@256 | 586 | self.segments.append((self.CLOSE,)) |
paulb@256 | 587 | |
paulb@256 | 588 | class SVGPoint: |
paulb@256 | 589 | |
paulb@258 | 590 | "A point used to provide currentTranslate." |
paulb@256 | 591 | |
paulb@256 | 592 | def __init__(self, x, y): |
paulb@256 | 593 | self.x = x |
paulb@256 | 594 | self.y = y |
paulb@256 | 595 | |
paul@338 | 596 | def __repr__(self): |
paul@338 | 597 | return "SVGPoint(%f, %f)" % (self.x, self.y) |
paul@338 | 598 | |
paulb@256 | 599 | class SVGRect: |
paulb@256 | 600 | |
paulb@256 | 601 | "A rectangle." |
paulb@256 | 602 | |
paulb@258 | 603 | def __init__(self, x=0, y=0, width=0, height=0): |
paulb@256 | 604 | self.x, self.y, self.width, self.height = x, y, width, height |
paulb@256 | 605 | |
paulb@258 | 606 | def __eq__(self, other): |
paulb@258 | 607 | return (self.x, self.y, self.width, self.height) == (other.x, other.y, other.width, other.height) |
paulb@258 | 608 | |
paulb@258 | 609 | def __ne__(self, other): |
paulb@258 | 610 | return not (self == other) |
paulb@258 | 611 | |
paulb@258 | 612 | def fromNode(self, node, name): |
paulb@258 | 613 | |
paulb@258 | 614 | """ |
paulb@258 | 615 | Initialise this object from the trait on the 'node' having the given |
paulb@258 | 616 | 'name'. |
paulb@258 | 617 | """ |
paulb@258 | 618 | |
paulb@258 | 619 | value = node.getAttribute(name) |
paulb@258 | 620 | if value is None: |
paulb@258 | 621 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@258 | 622 | try: |
paulb@258 | 623 | values = map(float, value.split()) |
paulb@258 | 624 | self.x, self.y, self.width, self.height = values |
paulb@258 | 625 | except (IndexError, ValueError): |
paulb@258 | 626 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 627 | |
paulb@258 | 628 | def toNode(self, node, name): |
paulb@258 | 629 | |
paulb@258 | 630 | """ |
paulb@258 | 631 | Set the trait on the given 'node' using the given 'name' according to |
paulb@258 | 632 | this object's attributes. |
paulb@258 | 633 | """ |
paulb@258 | 634 | |
paulb@258 | 635 | try: |
paulb@258 | 636 | values = map(str, [self.x, self.y, self.width, self.height]) |
paulb@258 | 637 | node.setAttribute(name, " ".join(values)) |
paulb@258 | 638 | except (IndexError, ValueError): |
paulb@258 | 639 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paulb@258 | 640 | |
paul@338 | 641 | def __repr__(self): |
paul@338 | 642 | return "SVGRect(%f, %f, %f, %f)" % (self.x, self.y, self.width, self.height) |
paul@338 | 643 | |
paulb@256 | 644 | class SVGRGBColor: |
paulb@256 | 645 | |
paul@338 | 646 | """ |
paul@338 | 647 | A colour. |
paul@338 | 648 | See: http://www.w3.org/TR/SVGMobile12/painting.html#colorSyntax |
paul@338 | 649 | """ |
paulb@256 | 650 | |
paulb@256 | 651 | def __init__(self, red, green, blue): |
paulb@256 | 652 | self.red, self.green, self.blue = red, green, blue |
paulb@256 | 653 | |
paul@338 | 654 | def __repr__(self): |
paul@338 | 655 | return "SVGRGBColor(%f, %f, %f)" % (self.red, self.green, self.blue) |
paul@338 | 656 | |
paulb@258 | 657 | class TraitAccess: |
paulb@258 | 658 | |
paulb@258 | 659 | """ |
paulb@258 | 660 | Access to traits stored on elements. |
paulb@258 | 661 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__TraitAccess |
paulb@258 | 662 | """ |
paulb@258 | 663 | |
paulb@258 | 664 | def getPathTrait(self, name): |
paulb@258 | 665 | path = SVGPath() |
paulb@258 | 666 | path.fromNode(self, name) |
paulb@258 | 667 | return path |
paulb@258 | 668 | |
paulb@258 | 669 | def setPathTrait(self, name, path): |
paulb@258 | 670 | path.toNode(self, name) |
paulb@258 | 671 | |
paulb@258 | 672 | def getRectTrait(self, name): |
paulb@258 | 673 | rect = SVGRect() |
paulb@258 | 674 | rect.fromNode(self, name) |
paulb@258 | 675 | return rect |
paulb@258 | 676 | |
paulb@258 | 677 | def setRectTrait(self, name, rect): |
paulb@258 | 678 | rect.toNode(self, name) |
paulb@258 | 679 | |
paulb@258 | 680 | def getMatrixTrait(self, name): |
paul@338 | 681 | if name != "transform": |
paul@338 | 682 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@258 | 683 | matrix = SVGMatrix() |
paulb@258 | 684 | matrix.fromNode(self, name) |
paulb@258 | 685 | return matrix |
paulb@258 | 686 | |
paulb@258 | 687 | def setMatrixTrait(self, name, matrix): |
paul@338 | 688 | if name != "transform": |
paul@338 | 689 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@258 | 690 | matrix.toNode(self, name) |
paulb@258 | 691 | |
paulb@256 | 692 | # Node classes. |
paulb@256 | 693 | |
paulb@258 | 694 | class SVGNode(libxml2dom.Node): |
paulb@258 | 695 | |
paulb@258 | 696 | "Convenience modifications to nodes specific to libxml2dom.svg." |
paulb@258 | 697 | |
paulb@258 | 698 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@258 | 699 | |
paulb@258 | 700 | """ |
paulb@258 | 701 | Evaluate the given 'expr' using the optional 'variables' and |
paulb@258 | 702 | 'namespaces'. If not otherwise specified, the "svg" prefix will be bound |
paulb@258 | 703 | to SVG_NAMESPACE as defined in this module. |
paulb@258 | 704 | """ |
paulb@258 | 705 | |
paulb@258 | 706 | namespaces = namespaces or {} |
paulb@258 | 707 | if not namespaces.has_key("svg"): |
paulb@258 | 708 | namespaces["svg"] = SVG_NAMESPACE |
paulb@258 | 709 | return libxml2dom.Node.xpath(self, expr, variables, namespaces) |
paulb@258 | 710 | |
paulb@265 | 711 | # NOTE: DocumentEvent is from DOM Level 3 Events. |
paulb@265 | 712 | # NOTE: EventSystem is a special libxml2dom.events class. |
paulb@265 | 713 | |
paulb@265 | 714 | class SVGDocument(libxml2dom._Document, SVGNode, EventTarget, DocumentEvent, EventSystem): |
paulb@256 | 715 | |
paulb@256 | 716 | "An SVG-specific document node." |
paulb@256 | 717 | |
paulb@256 | 718 | def __init__(self, node, impl): |
paulb@256 | 719 | |
paulb@256 | 720 | """ |
paulb@256 | 721 | Initialise the document with the given 'node', implementation 'impl', |
paulb@256 | 722 | and global (SVGGlobal) details. |
paulb@256 | 723 | """ |
paulb@256 | 724 | |
paulb@258 | 725 | libxml2dom._Document.__init__(self, node, impl) |
paulb@262 | 726 | self.global_ = self.impl.get_global(self) # parent |
paulb@256 | 727 | |
paulb@258 | 728 | class SVGElement(SVGNode, EventTarget, TraitAccess, ElementTraversal): # NOTE: SVGNode instead of Element. |
paulb@235 | 729 | |
paulb@235 | 730 | "An SVG-specific element." |
paulb@235 | 731 | |
paulb@262 | 732 | def __init__(self, *args, **kw): |
paulb@262 | 733 | SVGNode.__init__(self, *args, **kw) |
paulb@262 | 734 | |
paulb@235 | 735 | def _id(self): |
paulb@235 | 736 | return self.getAttribute("id") |
paulb@235 | 737 | |
paulb@235 | 738 | def _setId(self, value): |
paulb@235 | 739 | self.setAttribute("id", value) |
paulb@235 | 740 | |
paulb@235 | 741 | id = property(_id, _setId) |
paulb@235 | 742 | |
paulb@235 | 743 | class SVGLocatableElement(SVGElement, SVGLocatable): |
paulb@235 | 744 | |
paulb@235 | 745 | "A locatable element." |
paulb@235 | 746 | |
paulb@256 | 747 | pass |
paulb@256 | 748 | |
paulb@256 | 749 | class SVGTimedElement(SVGElement): # smil::ElementTimeControl |
paulb@256 | 750 | |
paulb@256 | 751 | "A timed element." |
paulb@256 | 752 | |
paulb@256 | 753 | def __init__(self, *args): |
paulb@256 | 754 | |
paulb@256 | 755 | "Initialise the element with the underlying 'args'." |
paulb@256 | 756 | |
paulb@256 | 757 | SVGElement.__init__(self, *args) |
paulb@256 | 758 | self.document_time = 0 |
paulb@256 | 759 | self.paused = 0 |
paulb@256 | 760 | |
paulb@256 | 761 | def _isPaused(self): |
paulb@256 | 762 | return self.paused |
paulb@256 | 763 | |
paulb@256 | 764 | def pauseElement(self): |
paulb@256 | 765 | self.paused = 1 |
paulb@256 | 766 | |
paulb@256 | 767 | def resumeElement(self): |
paulb@256 | 768 | self.paused = 0 |
paulb@256 | 769 | |
paulb@256 | 770 | class SVGSVGElement(SVGLocatableElement, SVGTimedElement): |
paulb@218 | 771 | |
paulb@218 | 772 | "An SVG-specific top-level element." |
paulb@218 | 773 | |
paulb@218 | 774 | NAV_AUTO = 1 |
paulb@218 | 775 | NAV_NEXT = 2 |
paulb@218 | 776 | NAV_PREV = 3 |
paulb@218 | 777 | NAV_UP = 4 |
paulb@218 | 778 | NAV_UP_RIGHT = 5 |
paulb@218 | 779 | NAV_RIGHT = 6 |
paulb@218 | 780 | NAV_DOWN_RIGHT = 7 |
paulb@218 | 781 | NAV_DOWN = 8 |
paulb@218 | 782 | NAV_DOWN_LEFT = 9 |
paulb@218 | 783 | NAV_LEFT = 10 |
paulb@218 | 784 | NAV_UP_LEFT = 11 |
paulb@218 | 785 | |
paulb@256 | 786 | def __init__(self, *args): |
paulb@256 | 787 | |
paulb@256 | 788 | "Initialise the element with the underlying 'args'." |
paulb@256 | 789 | |
paulb@256 | 790 | SVGTimedElement.__init__(self, *args) |
paulb@256 | 791 | self.scale = 1 |
paulb@256 | 792 | self.rotate = 0 |
paulb@256 | 793 | self.translate = SVGPoint(0, 0) |
paulb@256 | 794 | |
paul@338 | 795 | # NOTE: The scale, rotate and translate properties are not persistent, and |
paul@338 | 796 | # NOTE: are specific to individual objects. |
paul@338 | 797 | |
paulb@256 | 798 | def _currentScale(self): |
paulb@256 | 799 | return self.scale |
paulb@256 | 800 | |
paulb@256 | 801 | def _currentRotate(self): |
paulb@256 | 802 | return self.rotate |
paulb@256 | 803 | |
paulb@256 | 804 | def _currentTranslate(self): |
paulb@256 | 805 | return self.translate |
paulb@256 | 806 | |
paul@338 | 807 | def _setCurrentScale(self, scale): |
paul@338 | 808 | if scale == 0: |
paul@338 | 809 | raise xml.dom.DOMException(xml.dom.INVALID_ACCESS_ERR) |
paul@338 | 810 | self.scale = scale |
paul@338 | 811 | |
paul@338 | 812 | def _setCurrentRotate(self, rotate): |
paul@338 | 813 | self.rotate = rotate |
paul@338 | 814 | |
paulb@256 | 815 | def _viewport(self): |
paul@338 | 816 | if self.hasAttribute("viewBox"): |
paul@338 | 817 | return self.getRectTrait("viewBox") |
paul@338 | 818 | else: |
paul@338 | 819 | return SVGRect(0, 0, self._convertMeasurement(self.getAttribute("width")), |
paul@338 | 820 | self._convertMeasurement(self.getAttribute("height"))) |
paul@338 | 821 | |
paul@338 | 822 | # Utility methods. |
paul@338 | 823 | |
paul@338 | 824 | units = ["in", "cm", "mm", "pt", "pc", "px", "%"] |
paul@338 | 825 | |
paul@338 | 826 | def _convertMeasurement(self, value): |
paul@338 | 827 | value = value.strip() |
paul@338 | 828 | for unit in self.units: |
paul@338 | 829 | if value.endswith(unit): |
paul@338 | 830 | # NOTE: No conversion yet! |
paul@338 | 831 | return float(value[:-len(unit)].strip()) |
paul@338 | 832 | |
paul@338 | 833 | raise xml.dom.DOMException(xml.dom.TYPE_MISMATCH_ERR) |
paul@338 | 834 | |
paul@338 | 835 | # Standard methods. |
paulb@256 | 836 | |
paulb@256 | 837 | def getCurrentTime(self): |
paulb@256 | 838 | return self.document_time |
paulb@235 | 839 | |
paulb@256 | 840 | def setCurrentTime(self, setCurrentTime): |
paulb@256 | 841 | self.document_time = setCurrentTime |
paulb@256 | 842 | |
paulb@256 | 843 | def createSVGMatrixComponents(self, a, b, c, d, e, f): |
paulb@256 | 844 | return SVGMatrix(a, b, c, d, e, f) |
paulb@256 | 845 | |
paulb@256 | 846 | def createSVGRect(self): |
paulb@258 | 847 | return SVGRect() |
paulb@256 | 848 | |
paulb@256 | 849 | def createSVGPath(self): |
paulb@256 | 850 | return SVGPath() |
paulb@235 | 851 | |
paulb@256 | 852 | def createSVGRGBColor(self, red, green, blue): |
paulb@256 | 853 | return SVGRGBColor(red, green, blue) |
paulb@256 | 854 | |
paulb@256 | 855 | def moveFocus(self, motionType): |
paulb@256 | 856 | raise NotImplementedError, "moveFocus" |
paulb@256 | 857 | |
paulb@256 | 858 | def setFocus(self, object): |
paulb@256 | 859 | raise NotImplementedError, "setFocus" |
paulb@256 | 860 | |
paulb@256 | 861 | def getCurrentFocusedObject(self): |
paulb@256 | 862 | raise NotImplementedError, "getCurrentFocusedObject" |
paulb@256 | 863 | |
paul@338 | 864 | currentScale = property(_currentScale, _setCurrentScale) |
paul@338 | 865 | currentRotate = property(_currentRotate, _setCurrentRotate) |
paulb@256 | 866 | currentTranslate = property(_currentTranslate) |
paulb@256 | 867 | viewport = property(_viewport) |
paulb@235 | 868 | |
paulb@262 | 869 | # Event handler initialisation. |
paulb@262 | 870 | |
paulb@262 | 871 | def initialiseEvents(doc): |
paulb@262 | 872 | |
paulb@262 | 873 | """ |
paulb@262 | 874 | See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__EventListenerInitializer2 |
paulb@265 | 875 | See: http://www.w3.org/TR/xml-events/#section-listener-element |
paulb@262 | 876 | """ |
paulb@262 | 877 | |
paulb@265 | 878 | # Initialise script element listeners. |
paulb@265 | 879 | |
paulb@262 | 880 | for script in doc.xpath("//svg:script"): |
paulb@262 | 881 | doc.global_.initializeEventListeners(script) |
paulb@265 | 882 | |
paulb@265 | 883 | # Initialise handler element listeners using XML Events. |
paulb@265 | 884 | |
paulb@262 | 885 | for handler in doc.xpath("//svg:handler"): |
paulb@262 | 886 | listener = doc.global_.createEventListener(handler) |
paulb@262 | 887 | |
paulb@265 | 888 | # Attempt to parameterise the registration using the XML Events attributes. |
paulb@265 | 889 | |
paulb@265 | 890 | phase = handler.getAttributeNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "phase") |
paulb@265 | 891 | |
paulb@265 | 892 | # Add the listener for the appropriate type and phases. |
paulb@262 | 893 | |
paulb@262 | 894 | handler.parentNode.addEventListener( |
paulb@262 | 895 | handler.getAttributeNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "event"), |
paulb@262 | 896 | listener, |
paulb@265 | 897 | phase == "capture" |
paulb@262 | 898 | ) |
paulb@262 | 899 | |
paulb@258 | 900 | # Utility functions. |
paulb@258 | 901 | |
paulb@258 | 902 | createDocument = libxml2dom.createDocument |
paulb@258 | 903 | createDocumentType = libxml2dom.createDocumentType |
paulb@258 | 904 | |
paulb@258 | 905 | def createSVGDocument(): |
paulb@258 | 906 | return default_impl.createSVGDocument() |
paulb@235 | 907 | |
paulb@268 | 908 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@268 | 909 | doc = libxml2dom.parse(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@262 | 910 | initialiseEvents(doc) |
paulb@262 | 911 | return doc |
paulb@218 | 912 | |
paulb@268 | 913 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@268 | 914 | doc = libxml2dom.parseFile(filename, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@262 | 915 | initialiseEvents(doc) |
paulb@262 | 916 | return doc |
paulb@218 | 917 | |
paulb@268 | 918 | def parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@268 | 919 | doc = libxml2dom.parseString(s, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@262 | 920 | initialiseEvents(doc) |
paulb@262 | 921 | return doc |
paulb@218 | 922 | |
paulb@268 | 923 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@268 | 924 | doc = libxml2dom.parseURI(uri, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@262 | 925 | initialiseEvents(doc) |
paulb@262 | 926 | return doc |
paulb@218 | 927 | |
paulb@218 | 928 | # Single instance of the implementation. |
paulb@218 | 929 | |
paulb@218 | 930 | default_impl = SVGImplementation() |
paulb@218 | 931 | |
paulb@218 | 932 | # vim: tabstop=4 expandtab shiftwidth=4 |