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