summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Dufour <nicoduf@yahoo.fr>2011-04-18 12:53:28 +0000
committerJazzyNico <nicoduf@yahoo.fr>2011-04-18 12:53:28 +0000
commitbd80c4e617ca468a3eeb6caefb9f7da38630c95c (patch)
treefbebf41cff6488a7c2ea952935fe9f54fc42c487
parentRestore comment needed in *shared* code file. (diff)
downloadinkscape-bd80c4e617ca468a3eeb6caefb9f7da38630c95c.tar.gz
inkscape-bd80c4e617ca468a3eeb6caefb9f7da38630c95c.zip
Extensions. Optimized SVG export update (scour r210 + custom inx file).
(bzr r10180)
-rw-r--r--share/extensions/Makefile.am1
-rwxr-xr-xshare/extensions/scour.inkscape.py25
-rw-r--r--share/extensions/scour.inx50
-rwxr-xr-xshare/extensions/scour.py1973
-rw-r--r--share/extensions/svg_regex.py47
-rw-r--r--share/extensions/svg_transform.py233
6 files changed, 1664 insertions, 665 deletions
diff --git a/share/extensions/Makefile.am b/share/extensions/Makefile.am
index 01ecdb1f0..2b2c72460 100644
--- a/share/extensions/Makefile.am
+++ b/share/extensions/Makefile.am
@@ -141,6 +141,7 @@ extensions = \
svgcalendar.py \
svg_regex.py \
svg_and_media_zip_output.py \
+ svg_transform.py \
text_uppercase.py \
text_lowercase.py \
text_sentencecase.py \
diff --git a/share/extensions/scour.inkscape.py b/share/extensions/scour.inkscape.py
index 69c2dda5d..1cc7744d9 100755
--- a/share/extensions/scour.inkscape.py
+++ b/share/extensions/scour.inkscape.py
@@ -19,15 +19,24 @@ class ScourInkscape (inkex.Effect):
self.OptionParser.add_option("--group-collapsing", type="inkbool",
action="store", dest="group_collapse", default=True,
help="won't collapse <g> elements")
+ self.OptionParser.add_option("--create-groups", type="inkbool",
+ action="store", dest="group_create", default=False,
+ help="create <g> elements for runs of elements with identical attributes")
self.OptionParser.add_option("--enable-id-stripping", type="inkbool",
action="store", dest="strip_ids", default=False,
help="remove all un-referenced ID attributes")
+ self.OptionParser.add_option("--shorten-ids", type="inkbool",
+ action="store", dest="shorten_ids", default=False,
+ help="shorten all ID attributes to the least number of letters possible")
self.OptionParser.add_option("--embed-rasters", type="inkbool",
action="store", dest="embed_rasters", default=True,
help="won't embed rasters as base64-encoded data")
self.OptionParser.add_option("--keep-editor-data", type="inkbool",
action="store", dest="keep_editor_data", default=False,
help="won't remove Inkscape, Sodipodi or Adobe Illustrator elements and attributes")
+ self.OptionParser.add_option("--remove-metadata", type="inkbool",
+ action="store", dest="remove_metadata", default=False,
+ help="remove <metadata> elements (which may contain license metadata etc.)")
self.OptionParser.add_option("--strip-xml-prolog", type="inkbool",
action="store", dest="strip_xml_prolog", default=False,
help="won't output the <?xml ?> prolog")
@@ -37,10 +46,24 @@ class ScourInkscape (inkex.Effect):
self.OptionParser.add_option("--indent",
action="store", type="string", dest="indent_type", default="space",
help="indentation of the output: none, space, tab (default: %default)")
+ self.OptionParser.add_option("--protect-ids-noninkscape", type="inkbool",
+ action="store", dest="protect_ids_noninkscape", default=False,
+ help="don't change IDs not ending with a digit")
+ self.OptionParser.add_option("--protect-ids-list",
+ action="store", type="string", dest="protect_ids_list", default=None,
+ help="don't change IDs given in a comma-separated list")
+ self.OptionParser.add_option("--protect-ids-prefix",
+ action="store", type="string", dest="protect_ids_prefix", default=None,
+ help="don't change IDs starting with the given prefix")
self.OptionParser.add_option("--enable-viewboxing", type="inkbool",
action="store", dest="enable_viewboxing", default=False,
help="changes document width/height to 100%/100% and creates viewbox coordinates")
-
+ self.OptionParser.add_option("--enable-comment-stripping", type="inkbool",
+ action="store", dest="strip_comments", default=False,
+ help="remove all <!-- --> comments")
+ self.OptionParser.add_option("--renderer-workaround", type="inkbool",
+ action="store", dest="renderer_workaround", default=False,
+ help="work around various renderer bugs (currently only librsvg)")
def effect(self):
input = file(self.args[0], "r")
diff --git a/share/extensions/scour.inx b/share/extensions/scour.inx
index e5b6e90f5..b97dc0b9c 100644
--- a/share/extensions/scour.inx
+++ b/share/extensions/scour.inx
@@ -7,33 +7,53 @@
<dependency type="executable" location="extensions">yocto_css.py</dependency>
<param name="tab" type="notebook">
<page name="Options" _gui-text="Options">
- <param name="simplify-colors" type="boolean" _gui-text="Simplify colors">true</param>
- <param name="style-to-xml" type="boolean" _gui-text="Style to xml">true</param>
+ <param name="simplify-colors" type="boolean" _gui-text="Shorten color values">true</param>
+ <param name="style-to-xml" type="boolean" _gui-text="Convert CSS attributes to XML attributes">true</param>
<param name="group-collapsing" type="boolean" _gui-text="Group collapsing">true</param>
- <param name="enable-id-stripping" type="boolean" _gui-text="Enable id stripping">false</param>
+ <param name="create-groups" type="boolean" _gui-text="Create groups for similar attributes">true</param>
<param name="embed-rasters" type="boolean" _gui-text="Embed rasters">true</param>
<param name="keep-editor-data" type="boolean" _gui-text="Keep editor data">false</param>
+ <param name="remove-metadata" type="boolean" _gui-text="Remove metadata">false</param>
+ <param name="enable-comment-stripping" type="boolean" _gui-text="Remove comments">false</param>
+ <param name="renderer-workaround" type="boolean" _gui-text="Work around renderer bugs">true</param>
<param name="enable-viewboxing" type="boolean" _gui-text="Enable viewboxing">false</param>
- <param name="strip-xml-prolog" type="boolean" _gui-text="Strip xml prolog">false</param>
- <param name="set-precision" type="int" _gui-text="Set precision">5</param>
- <param name="indent" type="enum" _gui-text="Indent">
+ <param name="strip-xml-prolog" type="boolean" _gui-text="Remove the &lt;?xml?&gt; declaration">false</param>
+ <param name="set-precision" type="int" _gui-text="Number of significant digits for coords">5</param>
+ <param name="indent" type="enum" _gui-text="XML indentation (pretty-printing)">
<_item value="space">Space</_item>
<_item value="tab">Tab</_item>
<_item value="none">None</_item>
</param>
</page>
- <page name="Help" _gui-text="Help">
+ <page name="Ids" _gui-text="Ids">
+ <param name="enable-id-stripping" type="boolean" _gui-text="Remove unused ID names for elements">false</param>
+ <param name="shorten-ids" type="boolean" _gui-text="Shorten IDs">false</param>
+ <param name="protect-ids-noninkscape" type="boolean" _gui-text="Preserve manually created ID names not ending with digits">false</param>
+ <param name="protect-ids-list" type="string" _gui-text="Preserve these ID names, comma-separated:"></param>
+ <param name="protect-ids-prefix" type="string" _gui-text="Preserve ID names starting with:"></param>
+ </page>
+ <page name="OptionHelp" _gui-text="Help (Options)">
<_param name="instructions" type="description" xml:space="preserve">This extension optimizes the SVG file according to the following options:
- * Simplify colors: convert all colors to #RRGGBB format.
- * Style to xml: convert styles into XML attributes.
- * Group collapsing: collapse group elements.
- * Enable id stripping: remove all un-referenced ID attributes.
- * Embed rasters: embed rasters as base64-encoded data.
+ * Shorten color names: convert all colors to #RRGGBB or #RGB format.
+ * Convert CSS attributes to XML attributes: convert styles from &lt;style&gt; tags and inline style="" declarations into XML attributes.
+ * Group collapsing: removes useless &lt;g&gt; elements, promoting their contents up one level. Requires "Remove unused ID names for elements" to be set.
+ * Create groups for similar attributes: create &lt;g&gt; elements for runs of elements having at least one attribute in common (e.g. fill color, stroke opacity, ...).
+ * Embed rasters: embed raster images as base64-encoded data URLs.
* Keep editor data: don't remove Inkscape, Sodipodi or Adobe Illustrator elements and attributes.
+ * Remove metadata: remove &lt;metadata&gt; tags along with all the information in them, which may include license metadata, alternate versions for non-SVG-enabled browsers, etc.
+ * Remove comments: remove &lt;!-- --&gt; tags.
+ * Work around renderer bugs: emits slightly larger SVG data, but works around a bug in librsvg's renderer, which is used in Eye of GNOME and other various applications.
* Enable viewboxing: size image to 100%/100% and introduce a viewBox.
- * Strip xml prolog: don't output the xml prolog.
- * Set precision: set number of significant digits (default: 5).
- * Indent: indentation of the output: none, space, tab (default: space).</_param>
+ * Number of significant digits for coords: all coordinates are output with that number of significant digits. For example, if 3 is specified, the coordinate 3.5153 is output as 3.51 and the coordinate 471.55 is output as 472.
+ * XML indentation (pretty-printing): either None for no indentation, Space to use one space per nesting level, or Tab to use one tab per nesting level.</_param>
+ </page>
+ <page name="IdHelp" _gui-text="Help (Ids)">
+ <_param name="instructions" type="description" xml:space="preserve">Ids specific options:
+ * Remove unused ID names for elements: remove all unreferenced ID attributes.
+ * Shorten IDs: reduce the length of all ID attributes, assigning the shortest to the most-referenced elements. For instance, #linearGradient5621, referenced 100 times, can become #a.
+ * Preserve manually created ID names not ending with digits: usually, optimised SVG output removes these, but if they're needed for referencing (e.g. #middledot), you may use this option.
+ * Preserve these ID names, comma-separated: you can use this in conjunction with the other preserve options if you wish to preserve some more specific ID names.
+ * Preserve ID names starting with: usually, optimised SVG output removes all unused ID names, but if all of your preserved ID names start with the same prefix (e.g. #flag-mx, #flag-pt), you may use this option.</_param>
</page>
</param>
<output>
diff --git a/share/extensions/scour.py b/share/extensions/scour.py
index 3a5392ad2..88a65cb39 100755
--- a/share/extensions/scour.py
+++ b/share/extensions/scour.py
@@ -4,6 +4,7 @@
# Scour
#
# Copyright 2010 Jeff Schiller
+# Copyright 2010 Louis Simard
#
# This file is part of Scour, http://www.codedread.com/scour/
#
@@ -34,12 +35,13 @@
# at rounded corners)
# Next Up:
+# - why are marker-start, -end not removed from the style attribute?
+# - why are only overflow style properties considered and not attributes?
# - only remove unreferenced elements if they are not children of a referenced element
# - add an option to remove ids if they match the Inkscape-style of IDs
# - investigate point-reducing algorithms
# - parse transform attribute
# - if a <g> has only one element in it, collapse the <g> (ensure transform, etc are carried down)
-# - option to remove metadata
# necessary to get true division
from __future__ import division
@@ -49,10 +51,8 @@ import sys
import xml.dom.minidom
import re
import math
-import base64
-import urllib
from svg_regex import svg_parser
-import gzip
+from svg_transform import svg_transform_parser
import optparse
from yocto_css import parseCssString
@@ -60,8 +60,7 @@ from yocto_css import parseCssString
try:
from decimal import *
except ImportError:
- from fixedpoint import *
- Decimal = FixedPoint
+ print >>sys.stderr, "Scour requires Python 2.4."
# Import Psyco if available
try:
@@ -71,8 +70,8 @@ except ImportError:
pass
APP = 'scour'
-VER = '0.25r171'
-COPYRIGHT = 'Copyright Jeff Schiller, 2010'
+VER = '0.25'
+COPYRIGHT = 'Copyright Jeff Schiller, Louis Simard, 2010'
NS = { 'SVG': 'http://www.w3.org/2000/svg',
'XLINK': 'http://www.w3.org/1999/xlink',
@@ -110,6 +109,9 @@ svgAttributes = [
'font-weight',
'line-height',
'marker',
+ 'marker-end',
+ 'marker-mid',
+ 'marker-start',
'opacity',
'overflow',
'stop-color',
@@ -274,16 +276,75 @@ colors = {
'yellow': 'rgb(255, 255, 0)',
'yellowgreen': 'rgb(154, 205, 50)',
}
-
+
+default_attributes = { # excluded all attributes with 'auto' as default
+ # SVG 1.1 presentation attributes
+ 'baseline-shift': 'baseline',
+ 'clip-path': 'none',
+ 'clip-rule': 'nonzero',
+ 'color': '#000',
+ 'color-interpolation-filters': 'linearRGB',
+ 'color-interpolation': 'sRGB',
+ 'direction': 'ltr',
+ 'display': 'inline',
+ 'enable-background': 'accumulate',
+ 'fill': '#000',
+ 'fill-opacity': '1',
+ 'fill-rule': 'nonzero',
+ 'filter': 'none',
+ 'flood-color': '#000',
+ 'flood-opacity': '1',
+ 'font-size-adjust': 'none',
+ 'font-size': 'medium',
+ 'font-stretch': 'normal',
+ 'font-style': 'normal',
+ 'font-variant': 'normal',
+ 'font-weight': 'normal',
+ 'glyph-orientation-horizontal': '0deg',
+ 'letter-spacing': 'normal',
+ 'lighting-color': '#fff',
+ 'marker': 'none',
+ 'marker-start': 'none',
+ 'marker-mid': 'none',
+ 'marker-end': 'none',
+ 'mask': 'none',
+ 'opacity': '1',
+ 'pointer-events': 'visiblePainted',
+ 'stop-color': '#000',
+ 'stop-opacity': '1',
+ 'stroke': 'none',
+ 'stroke-dasharray': 'none',
+ 'stroke-dashoffset': '0',
+ 'stroke-linecap': 'butt',
+ 'stroke-linejoin': 'miter',
+ 'stroke-miterlimit': '4',
+ 'stroke-opacity': '1',
+ 'stroke-width': '1',
+ 'text-anchor': 'start',
+ 'text-decoration': 'none',
+ 'unicode-bidi': 'normal',
+ 'visibility': 'visible',
+ 'word-spacing': 'normal',
+ 'writing-mode': 'lr-tb',
+ # SVG 1.2 tiny properties
+ 'audio-level': '1',
+ 'solid-color': '#000',
+ 'solid-opacity': '1',
+ 'text-align': 'start',
+ 'vector-effect': 'none',
+ 'viewport-fill': 'none',
+ 'viewport-fill-opacity': '1',
+ }
+
def isSameSign(a,b): return (a <= 0 and b <= 0) or (a >= 0 and b >= 0)
-
-coord = re.compile("\\-?\\d+\\.?\\d*")
-scinumber = re.compile("[\\-\\+]?(\\d*\\.?)?\\d+[eE][\\-\\+]?\\d+")
-number = re.compile("[\\-\\+]?(\\d*\\.?)?\\d+")
-sciExponent = re.compile("[eE]([\\-\\+]?\\d+)")
-unit = re.compile("(em|ex|px|pt|pc|cm|mm|in|\\%){1,1}$")
+
+scinumber = re.compile(r"[-+]?(\d*\.?)?\d+[eE][-+]?\d+")
+number = re.compile(r"[-+]?(\d*\.?)?\d+")
+sciExponent = re.compile(r"[eE]([-+]?\d+)")
+unit = re.compile("(em|ex|px|pt|pc|cm|mm|in|%){1,1}$")
class Unit(object):
+ # Integer constants for units.
INVALID = -1
NONE = 0
PCT = 1
@@ -296,35 +357,48 @@ class Unit(object):
MM = 8
IN = 9
+ # String to Unit. Basically, converts unit strings to their integer constants.
+ s2u = {
+ '': NONE,
+ '%': PCT,
+ 'px': PX,
+ 'pt': PT,
+ 'pc': PC,
+ 'em': EM,
+ 'ex': EX,
+ 'cm': CM,
+ 'mm': MM,
+ 'in': IN,
+ }
+
+ # Unit to String. Basically, converts unit integer constants to their corresponding strings.
+ u2s = {
+ NONE: '',
+ PCT: '%',
+ PX: 'px',
+ PT: 'pt',
+ PC: 'pc',
+ EM: 'em',
+ EX: 'ex',
+ CM: 'cm',
+ MM: 'mm',
+ IN: 'in',
+ }
+
# @staticmethod
- def get(str):
- # GZ: shadowing builtins like 'str' is generally bad form
- # GZ: encoding stuff like this in a dict makes for nicer code
- if str == None or str == '': return Unit.NONE
- elif str == '%': return Unit.PCT
- elif str == 'px': return Unit.PX
- elif str == 'pt': return Unit.PT
- elif str == 'pc': return Unit.PC
- elif str == 'em': return Unit.EM
- elif str == 'ex': return Unit.EX
- elif str == 'cm': return Unit.CM
- elif str == 'mm': return Unit.MM
- elif str == 'in': return Unit.IN
- return Unit.INVALID
+ def get(unitstr):
+ if unitstr is None: return Unit.NONE
+ try:
+ return Unit.s2u[unitstr]
+ except KeyError:
+ return Unit.INVALID
# @staticmethod
- def str(u):
- if u == Unit.NONE: return ''
- elif u == Unit.PCT: return '%'
- elif u == Unit.PX: return 'px'
- elif u == Unit.PT: return 'pt'
- elif u == Unit.PC: return 'pc'
- elif u == Unit.EM: return 'em'
- elif u == Unit.EX: return 'ex'
- elif u == Unit.CM: return 'cm'
- elif u == Unit.MM: return 'mm'
- elif u == Unit.IN: return 'in'
- return 'INVALID'
+ def str(unitint):
+ try:
+ return Unit.u2s[unitint]
+ except KeyError:
+ return 'INVALID'
get = staticmethod(get)
str = staticmethod(str)
@@ -371,26 +445,6 @@ class SVGLength(object):
self.value = 0
self.units = Unit.INVALID
-# returns the length of a property
-# TODO: eventually use the above class once it is complete
-def getSVGLength(value):
- try:
- v = float(value)
- except ValueError:
- coordMatch = coord.match(value)
- if coordMatch != None:
- unitMatch = unit.search(value, coordMatch.start(0))
- v = value
- return v
-
-def findElementById(node, id):
- if node == None or node.nodeType != 1: return None
- if node.getAttribute('id') == id: return node
- for child in node.childNodes :
- e = findElementById(child,id)
- if e != None: return e
- return None
-
def findElementsWithId(node, elems=None):
"""
Returns all elements with id attributes
@@ -414,7 +468,10 @@ referencingProps = ['fill', 'stroke', 'filter', 'clip-path', 'mask', 'marker-st
def findReferencedElements(node, ids=None):
"""
Returns the number of times an ID is referenced as well as all elements
- that reference it.
+ that reference it. node is the node at which to start the search. The
+ return value is a map which has the id as key and each value is an array
+ where the first value is a count and the second value is a list of nodes
+ that referenced it.
Currently looks at fill, stroke, clip-path, mask, marker, and
xlink:href attributes.
@@ -427,9 +484,12 @@ def findReferencedElements(node, ids=None):
# if this node is a style element, parse its text into CSS
if node.nodeName == 'style' and node.namespaceURI == NS['SVG']:
- # node.firstChild will be either a CDATA or a Text node
- if node.firstChild != None:
- cssRules = parseCssString(node.firstChild.nodeValue)
+ # one stretch of text, please! (we could use node.normalize(), but
+ # this actually modifies the node, and we don't want to keep
+ # whitespace around if there's any)
+ stylesheet = "".join([child.nodeValue for child in node.childNodes])
+ if stylesheet != '':
+ cssRules = parseCssString(stylesheet)
for rule in cssRules:
for propname in rule['properties']:
propval = rule['properties'][propname]
@@ -499,7 +559,11 @@ numPathSegmentsReduced = 0
numCurvesStraightened = 0
numBytesSavedInPathData = 0
numBytesSavedInColors = 0
+numBytesSavedInIDs = 0
+numBytesSavedInLengths = 0
+numBytesSavedInTransforms = 0
numPointsRemovedFromPolygon = 0
+numCommentBytes = 0
def removeUnusedDefs(doc, defElem, elemsToRemove=None):
if elemsToRemove is None:
@@ -532,21 +596,21 @@ def removeUnreferencedElements(doc):
"""
global numElemsRemoved
num = 0
+
+ # Remove certain unreferenced elements outside of defs
removeTags = ['linearGradient', 'radialGradient', 'pattern']
-
identifiedElements = findElementsWithId(doc.documentElement)
referencedIDs = findReferencedElements(doc.documentElement)
for id in identifiedElements:
if not id in referencedIDs:
- goner = findElementById(doc.documentElement, id)
+ goner = identifiedElements[id]
if goner != None and goner.parentNode != None and goner.nodeName in removeTags:
goner.parentNode.removeChild(goner)
num += 1
numElemsRemoved += 1
- # TODO: should also go through defs and vacuum it
- num = 0
+ # Remove most unreferenced elements inside defs
defs = doc.documentElement.getElementsByTagName('defs')
for aDef in defs:
elemsToRemove = removeUnusedDefs(doc, aDef)
@@ -556,6 +620,160 @@ def removeUnreferencedElements(doc):
num += 1
return num
+def shortenIDs(doc, unprotectedElements=None):
+ """
+ Shortens ID names used in the document. ID names referenced the most often are assigned the
+ shortest ID names.
+ If the list unprotectedElements is provided, only IDs from this list will be shortened.
+
+ Returns the number of bytes saved by shortening ID names in the document.
+ """
+ num = 0
+
+ identifiedElements = findElementsWithId(doc.documentElement)
+ if unprotectedElements is None:
+ unprotectedElements = identifiedElements
+ referencedIDs = findReferencedElements(doc.documentElement)
+
+ # Make idList (list of idnames) sorted by reference count
+ # descending, so the highest reference count is first.
+ # First check that there's actually a defining element for the current ID name.
+ # (Cyn: I've seen documents with #id references but no element with that ID!)
+ idList = [(referencedIDs[rid][0], rid) for rid in referencedIDs
+ if rid in unprotectedElements]
+ idList.sort(reverse=True)
+ idList = [rid for count, rid in idList]
+
+ curIdNum = 1
+
+ for rid in idList:
+ curId = intToID(curIdNum)
+ # First make sure that *this* element isn't already using
+ # the ID name we want to give it.
+ if curId != rid:
+ # Then, skip ahead if the new ID is already in identifiedElement.
+ while curId in identifiedElements:
+ curIdNum += 1
+ curId = intToID(curIdNum)
+ # Then go rename it.
+ num += renameID(doc, rid, curId, identifiedElements, referencedIDs)
+ curIdNum += 1
+
+ return num
+
+def intToID(idnum):
+ """
+ Returns the ID name for the given ID number, spreadsheet-style, i.e. from a to z,
+ then from aa to az, ba to bz, etc., until zz.
+ """
+ rid = ''
+
+ while idnum > 0:
+ idnum -= 1
+ rid = chr((idnum % 26) + ord('a')) + rid
+ idnum = int(idnum / 26)
+
+ return rid
+
+def renameID(doc, idFrom, idTo, identifiedElements, referencedIDs):
+ """
+ Changes the ID name from idFrom to idTo, on the declaring element
+ as well as all references in the document doc.
+
+ Updates identifiedElements and referencedIDs.
+ Does not handle the case where idTo is already the ID name
+ of another element in doc.
+
+ Returns the number of bytes saved by this replacement.
+ """
+
+ num = 0
+
+ definingNode = identifiedElements[idFrom]
+ definingNode.setAttribute("id", idTo)
+ del identifiedElements[idFrom]
+ identifiedElements[idTo] = definingNode
+
+ referringNodes = referencedIDs[idFrom]
+
+ # Look for the idFrom ID name in each of the referencing elements,
+ # exactly like findReferencedElements would.
+ # Cyn: Duplicated processing!
+
+ for node in referringNodes[1]:
+ # if this node is a style element, parse its text into CSS
+ if node.nodeName == 'style' and node.namespaceURI == NS['SVG']:
+ # node.firstChild will be either a CDATA or a Text node now
+ if node.firstChild != None:
+ # concatenate the value of all children, in case
+ # there's a CDATASection node surrounded by whitespace
+ # nodes
+ # (node.normalize() will NOT work here, it only acts on Text nodes)
+ oldValue = "".join([child.nodeValue for child in node.childNodes])
+ # not going to reparse the whole thing
+ newValue = oldValue.replace('url(#' + idFrom + ')', 'url(#' + idTo + ')')
+ newValue = newValue.replace("url(#'" + idFrom + "')", 'url(#' + idTo + ')')
+ newValue = newValue.replace('url(#"' + idFrom + '")', 'url(#' + idTo + ')')
+ # and now replace all the children with this new stylesheet.
+ # again, this is in case the stylesheet was a CDATASection
+ node.childNodes[:] = [node.ownerDocument.createTextNode(newValue)]
+ num += len(oldValue) - len(newValue)
+
+ # if xlink:href is set to #idFrom, then change the id
+ href = node.getAttributeNS(NS['XLINK'],'href')
+ if href == '#' + idFrom:
+ node.setAttributeNS(NS['XLINK'],'href', '#' + idTo)
+ num += len(idFrom) - len(idTo)
+
+ # if the style has url(#idFrom), then change the id
+ styles = node.getAttribute('style')
+ if styles != '':
+ newValue = styles.replace('url(#' + idFrom + ')', 'url(#' + idTo + ')')
+ newValue = newValue.replace("url('#" + idFrom + "')", 'url(#' + idTo + ')')
+ newValue = newValue.replace('url("#' + idFrom + '")', 'url(#' + idTo + ')')
+ node.setAttribute('style', newValue)
+ num += len(styles) - len(newValue)
+
+ # now try the fill, stroke, filter attributes
+ for attr in referencingProps:
+ oldValue = node.getAttribute(attr)
+ if oldValue != '':
+ newValue = oldValue.replace('url(#' + idFrom + ')', 'url(#' + idTo + ')')
+ newValue = newValue.replace("url('#" + idFrom + "')", 'url(#' + idTo + ')')
+ newValue = newValue.replace('url("#' + idFrom + '")', 'url(#' + idTo + ')')
+ node.setAttribute(attr, newValue)
+ num += len(oldValue) - len(newValue)
+
+ del referencedIDs[idFrom]
+ referencedIDs[idTo] = referringNodes
+
+ return num
+
+def unprotected_ids(doc, options):
+ u"""Returns a list of unprotected IDs within the document doc."""
+ identifiedElements = findElementsWithId(doc.documentElement)
+ if not (options.protect_ids_noninkscape or
+ options.protect_ids_list or
+ options.protect_ids_prefix):
+ return identifiedElements
+ if options.protect_ids_list:
+ protect_ids_list = options.protect_ids_list.split(",")
+ if options.protect_ids_prefix:
+ protect_ids_prefixes = options.protect_ids_prefix.split(",")
+ for id in identifiedElements.keys():
+ protected = False
+ if options.protect_ids_noninkscape and not id[-1].isdigit():
+ protected = True
+ if options.protect_ids_list and id in protect_ids_list:
+ protected = True
+ if options.protect_ids_prefix:
+ for prefix in protect_ids_prefixes:
+ if id.startswith(prefix):
+ protected = True
+ if protected:
+ del identifiedElements[id]
+ return identifiedElements
+
def removeUnreferencedIDs(referencedIDs, identifiedElements):
"""
Removes the unreferenced ID attributes.
@@ -580,7 +798,7 @@ def removeNamespacedAttributes(node, namespaces):
# remove all namespace'd attributes from this element
attrList = node.attributes
attrsToRemove = []
- for attrNum in range(attrList.length):
+ for attrNum in xrange(attrList.length):
attr = attrList.item(attrNum)
if attr != None and attr.namespaceURI in namespaces:
attrsToRemove.append(attr.nodeName)
@@ -613,6 +831,19 @@ def removeNamespacedElements(node, namespaces):
for child in node.childNodes:
num += removeNamespacedElements(child, namespaces)
return num
+
+def removeMetadataElements(doc):
+ global numElemsRemoved
+ num = 0
+ # clone the list, as the tag list is live from the DOM
+ elementsToRemove = [element for element in doc.documentElement.getElementsByTagName('metadata')]
+
+ for element in elementsToRemove:
+ element.parentNode.removeChild(element)
+ num += 1
+ numElemsRemoved += 1
+
+ return num
def removeNestedGroups(node):
"""
@@ -624,15 +855,18 @@ def removeNestedGroups(node):
num = 0
groupsToRemove = []
- for child in node.childNodes:
- if child.nodeName == 'g' and child.namespaceURI == NS['SVG'] and len(child.attributes) == 0:
- # only collapse group if it does not have a title or desc as a direct descendant
- for grandchild in child.childNodes:
- if grandchild.nodeType == 1 and grandchild.namespaceURI == NS['SVG'] and \
- grandchild.nodeName in ['title','desc']:
- break
- else:
- groupsToRemove.append(child)
+ # Only consider <g> elements for promotion if this element isn't a <switch>.
+ # (partial fix for bug 594930, required by the SVG spec however)
+ if not (node.nodeType == 1 and node.nodeName == 'switch'):
+ for child in node.childNodes:
+ if child.nodeName == 'g' and child.namespaceURI == NS['SVG'] and len(child.attributes) == 0:
+ # only collapse group if it does not have a title or desc as a direct descendant,
+ for grandchild in child.childNodes:
+ if grandchild.nodeType == 1 and grandchild.namespaceURI == NS['SVG'] and \
+ grandchild.nodeName in ['title','desc']:
+ break
+ else:
+ groupsToRemove.append(child)
for g in groupsToRemove:
while g.childNodes.length > 0:
@@ -647,21 +881,24 @@ def removeNestedGroups(node):
num += removeNestedGroups(child)
return num
-def moveCommonAttributesToParentGroup(elem):
+def moveCommonAttributesToParentGroup(elem, referencedElements):
"""
This recursively calls this function on all children of the passed in element
and then iterates over all child elements and removes common inheritable attributes
from the children and places them in the parent group. But only if the parent contains
- nothing but element children and whitespace.
+ nothing but element children and whitespace. The attributes are only removed from the
+ children if the children are not referenced by other elements in the document.
"""
num = 0
childElements = []
# recurse first into the children (depth-first)
for child in elem.childNodes:
- if child.nodeType == 1:
- childElements.append(child)
- num += moveCommonAttributesToParentGroup(child)
+ if child.nodeType == 1:
+ # only add and recurse if the child is not referenced elsewhere
+ if not child.getAttribute('id') in referencedElements:
+ childElements.append(child)
+ num += moveCommonAttributesToParentGroup(child, referencedElements)
# else if the parent has non-whitespace text children, do not
# try to move common attributes
elif child.nodeType == 3 and child.nodeValue.strip():
@@ -676,7 +913,7 @@ def moveCommonAttributesToParentGroup(elem):
# its fill attribute is not what we want to look at, we should look for the first
# non-animate/set element
attrList = childElements[0].attributes
- for num in range(attrList.length):
+ for num in xrange(attrList.length):
attr = attrList.item(num)
# this is most of the inheritable properties from http://www.w3.org/TR/SVG11/propidx.html
# and http://www.w3.org/TR/SVGTiny12/attributeTable.html
@@ -695,7 +932,7 @@ def moveCommonAttributesToParentGroup(elem):
commonAttrs[attr.nodeName] = attr.nodeValue
# for each subsequent child element
- for childNum in range(len(childElements)):
+ for childNum in xrange(len(childElements)):
# skip first child
if childNum == 0:
continue
@@ -725,6 +962,110 @@ def moveCommonAttributesToParentGroup(elem):
num += (len(childElements)-1) * len(commonAttrs)
return num
+def createGroupsForCommonAttributes(elem):
+ """
+ Creates <g> elements to contain runs of 3 or more
+ consecutive child elements having at least one common attribute.
+
+ Common attributes are not promoted to the <g> by this function.
+ This is handled by moveCommonAttributesToParentGroup.
+
+ If all children have a common attribute, an extra <g> is not created.
+
+ This function acts recursively on the given element.
+ """
+ num = 0
+ global numElemsRemoved
+
+ # TODO perhaps all of the Presentation attributes in http://www.w3.org/TR/SVG/struct.html#GElement
+ # could be added here
+ # Cyn: These attributes are the same as in moveAttributesToParentGroup, and must always be
+ for curAttr in ['clip-rule',
+ 'display-align',
+ 'fill', 'fill-opacity', 'fill-rule',
+ 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch',
+ 'font-style', 'font-variant', 'font-weight',
+ 'letter-spacing',
+ 'pointer-events', 'shape-rendering',
+ 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin',
+ 'stroke-miterlimit', 'stroke-opacity', 'stroke-width',
+ 'text-anchor', 'text-decoration', 'text-rendering', 'visibility',
+ 'word-spacing', 'writing-mode']:
+ # Iterate through the children in reverse order, so item(i) for
+ # items we have yet to visit still returns the correct nodes.
+ curChild = elem.childNodes.length - 1
+ while curChild >= 0:
+ childNode = elem.childNodes.item(curChild)
+
+ if childNode.nodeType == 1 and childNode.getAttribute(curAttr) != '':
+ # We're in a possible run! Track the value and run length.
+ value = childNode.getAttribute(curAttr)
+ runStart, runEnd = curChild, curChild
+ # Run elements includes only element tags, no whitespace/comments/etc.
+ # Later, we calculate a run length which includes these.
+ runElements = 1
+
+ # Backtrack to get all the nodes having the same
+ # attribute value, preserving any nodes in-between.
+ while runStart > 0:
+ nextNode = elem.childNodes.item(runStart - 1)
+ if nextNode.nodeType == 1:
+ if nextNode.getAttribute(curAttr) != value: break
+ else:
+ runElements += 1
+ runStart -= 1
+ else: runStart -= 1
+
+ if runElements >= 3:
+ # Include whitespace/comment/etc. nodes in the run.
+ while runEnd < elem.childNodes.length - 1:
+ if elem.childNodes.item(runEnd + 1).nodeType == 1: break
+ else: runEnd += 1
+
+ runLength = runEnd - runStart + 1
+ if runLength == elem.childNodes.length: # Every child has this
+ # If the current parent is a <g> already,
+ if elem.nodeName == 'g' and elem.namespaceURI == NS['SVG']:
+ # do not act altogether on this attribute; all the
+ # children have it in common.
+ # Let moveCommonAttributesToParentGroup do it.
+ curChild = -1
+ continue
+ # otherwise, it might be an <svg> element, and
+ # even if all children have the same attribute value,
+ # it's going to be worth making the <g> since
+ # <svg> doesn't support attributes like 'stroke'.
+ # Fall through.
+
+ # Create a <g> element from scratch.
+ # We need the Document for this.
+ document = elem.ownerDocument
+ group = document.createElementNS(NS['SVG'], 'g')
+ # Move the run of elements to the group.
+ # a) ADD the nodes to the new group.
+ group.childNodes[:] = elem.childNodes[runStart:runEnd + 1]
+ for child in group.childNodes:
+ child.parentNode = group
+ # b) REMOVE the nodes from the element.
+ elem.childNodes[runStart:runEnd + 1] = []
+ # Include the group in elem's children.
+ elem.childNodes.insert(runStart, group)
+ group.parentNode = elem
+ num += 1
+ curChild = runStart - 1
+ numElemsRemoved -= 1
+ else:
+ curChild -= 1
+ else:
+ curChild -= 1
+
+ # each child gets the same treatment, recursively
+ for childNode in elem.childNodes:
+ if childNode.nodeType == 1:
+ num += createGroupsForCommonAttributes(childNode)
+
+ return num
+
def removeUnusedAttributesOnParent(elem):
"""
This recursively calls this function on all children of the element passed in,
@@ -745,7 +1086,7 @@ def removeUnusedAttributesOnParent(elem):
# get all attribute values on this parent
attrList = elem.attributes
unusedAttrs = {}
- for num in range(attrList.length):
+ for num in xrange(attrList.length):
attr = attrList.item(num)
if attr.nodeName in ['clip-rule',
'display-align',
@@ -761,7 +1102,7 @@ def removeUnusedAttributesOnParent(elem):
unusedAttrs[attr.nodeName] = attr.nodeValue
# for each child, if at least one child inherits the parent's attribute, then remove
- for childNum in range(len(childElements)):
+ for childNum in xrange(len(childElements)):
child = childElements[childNum]
inheritedAttrs = []
for name in unusedAttrs.keys():
@@ -801,11 +1142,12 @@ def removeDuplicateGradientStops(doc):
color = stop.getAttribute('stop-color')
opacity = stop.getAttribute('stop-opacity')
+ style = stop.getAttribute('style')
if stops.has_key(offset) :
oldStop = stops[offset]
- if oldStop[0] == color and oldStop[1] == opacity:
+ if oldStop[0] == color and oldStop[1] == opacity and oldStop[2] == style:
stopsToRemove.append(stop)
- stops[offset] = [color, opacity]
+ stops[offset] = [color, opacity, style]
for stop in stopsToRemove:
stop.parentNode.removeChild(stop)
@@ -819,12 +1161,16 @@ def collapseSinglyReferencedGradients(doc):
global numElemsRemoved
num = 0
+ identifiedElements = findElementsWithId(doc.documentElement)
+
# make sure to reset the ref'ed ids for when we are running this in testscour
for rid,nodeCount in findReferencedElements(doc.documentElement).iteritems():
count = nodeCount[0]
nodes = nodeCount[1]
- if count == 1:
- elem = findElementById(doc.documentElement,rid)
+ # Make sure that there's actually a defining element for the current ID name.
+ # (Cyn: I've seen documents with #id references but no element with that ID!)
+ if count == 1 and rid in identifiedElements:
+ elem = identifiedElements[rid]
if elem != None and elem.nodeType == 1 and elem.nodeName in ['linearGradient', 'radialGradient'] \
and elem.namespaceURI == NS['SVG']:
# found a gradient that is referenced by only 1 other element
@@ -905,11 +1251,11 @@ def removeDuplicateGradients(doc):
# now compare stops
stopsNotEqual = False
- for i in range(stops.length):
+ for i in xrange(stops.length):
if stopsNotEqual: break
stop = stops.item(i)
ostop = ostops.item(i)
- for attr in ['offset', 'stop-color', 'stop-opacity']:
+ for attr in ['offset', 'stop-color', 'stop-opacity', 'style']:
if stop.getAttribute(attr) != ostop.getAttribute(attr):
stopsNotEqual = True
break
@@ -945,6 +1291,12 @@ def removeDuplicateGradients(doc):
elem.setAttribute(attr, 'url(#'+master_id+')')
if elem.getAttributeNS(NS['XLINK'], 'href') == '#'+dup_id:
elem.setAttributeNS(NS['XLINK'], 'href', '#'+master_id)
+ styles = _getStyle(elem)
+ for style in styles:
+ v = styles[style]
+ if v == 'url(#'+dup_id+')' or v == 'url("#'+dup_id+'")' or v == "url('#"+dup_id+"')":
+ styles[style] = 'url(#'+master_id+')'
+ _setStyle(elem, styles)
# now that all referencing elements have been re-mapped to the master
# it is safe to remove this gradient from the document
@@ -953,16 +1305,32 @@ def removeDuplicateGradients(doc):
num += 1
return num
-def repairStyle(node, options):
- num = 0
+def _getStyle(node):
+ u"""Returns the style attribute of a node as a dictionary."""
if node.nodeType == 1 and len(node.getAttribute('style')) > 0 :
- # get all style properties and stuff them into a dictionary
styleMap = { }
rawStyles = node.getAttribute('style').split(';')
for style in rawStyles:
propval = style.split(':')
if len(propval) == 2 :
styleMap[propval[0].strip()] = propval[1].strip()
+ return styleMap
+ else:
+ return {}
+
+def _setStyle(node, styleMap):
+ u"""Sets the style attribute of a node to the dictionary ``styleMap``."""
+ fixedStyle = ';'.join([prop + ':' + styleMap[prop] for prop in styleMap.keys()])
+ if fixedStyle != '' :
+ node.setAttribute('style', fixedStyle)
+ elif node.getAttribute('style'):
+ node.removeAttribute('style')
+ return node
+
+def repairStyle(node, options):
+ num = 0
+ styleMap = _getStyle(node)
+ if styleMap:
# I've seen this enough to know that I need to correct it:
# fill: url(#linearGradient4918) rgb(0, 0, 0);
@@ -977,13 +1345,8 @@ def repairStyle(node, options):
# opacity:1
if styleMap.has_key('opacity') :
opacity = float(styleMap['opacity'])
- # opacity='1.0' is useless, remove it
- if opacity == 1.0 :
- del styleMap['opacity']
- num += 1
-
# if opacity='0' then all fill and stroke properties are useless, remove them
- elif opacity == 0.0 :
+ if opacity == 0.0 :
for uselessStyle in ['fill', 'fill-opacity', 'fill-rule', 'stroke', 'stroke-linejoin',
'stroke-opacity', 'stroke-miterlimit', 'stroke-linecap', 'stroke-dasharray',
'stroke-dashoffset', 'stroke-opacity'] :
@@ -1010,33 +1373,19 @@ def repairStyle(node, options):
del styleMap[fillstyle]
num += 1
- # stop-opacity: 1
- if styleMap.has_key('stop-opacity') :
- if float(styleMap['stop-opacity']) == 1.0 :
- del styleMap['stop-opacity']
- num += 1
-
- # fill-opacity: 1 or 0
+ # fill-opacity: 0
if styleMap.has_key('fill-opacity') :
fillOpacity = float(styleMap['fill-opacity'])
- # TODO: This is actually a problem if the parent element does not have fill-opacity=1
- if fillOpacity == 1.0 :
- del styleMap['fill-opacity']
- num += 1
- elif fillOpacity == 0.0 :
+ if fillOpacity == 0.0 :
for uselessFillStyle in [ 'fill', 'fill-rule' ] :
if styleMap.has_key(uselessFillStyle):
del styleMap[uselessFillStyle]
num += 1
- # stroke-opacity: 1 or 0
+ # stroke-opacity: 0
if styleMap.has_key('stroke-opacity') :
strokeOpacity = float(styleMap['stroke-opacity'])
- # TODO: This is actually a problem if the parent element does not have stroke-opacity=1
- if strokeOpacity == 1.0 :
- del styleMap['stroke-opacity']
- num += 1
- elif strokeOpacity == 0.0 :
+ if strokeOpacity == 0.0 :
for uselessStrokeStyle in [ 'stroke', 'stroke-width', 'stroke-linejoin', 'stroke-linecap',
'stroke-dasharray', 'stroke-dashoffset' ] :
if styleMap.has_key(uselessStrokeStyle):
@@ -1045,8 +1394,8 @@ def repairStyle(node, options):
# stroke-width: 0
if styleMap.has_key('stroke-width') :
- strokeWidth = getSVGLength(styleMap['stroke-width'])
- if strokeWidth == 0.0 :
+ strokeWidth = SVGLength(styleMap['stroke-width'])
+ if strokeWidth.value == 0.0 :
for uselessStrokeStyle in [ 'stroke', 'stroke-linejoin', 'stroke-linecap',
'stroke-dasharray', 'stroke-dashoffset', 'stroke-opacity' ] :
if styleMap.has_key(uselessStrokeStyle):
@@ -1055,12 +1404,13 @@ def repairStyle(node, options):
# remove font properties for non-text elements
# I've actually observed this in real SVG content
- if node.nodeName in ['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path']:
+ if not mayContainTextNodes(node):
for fontstyle in [ 'font-family', 'font-size', 'font-stretch', 'font-size-adjust',
'font-style', 'font-variant', 'font-weight',
'letter-spacing', 'line-height', 'kerning',
- 'text-anchor', 'text-decoration', 'text-rendering',
- 'unicode-bidi', 'word-spacing', 'writing-mode'] :
+ 'text-align', 'text-anchor', 'text-decoration',
+ 'text-rendering', 'unicode-bidi',
+ 'word-spacing', 'writing-mode'] :
if styleMap.has_key(fontstyle) :
del styleMap[fontstyle]
num += 1
@@ -1072,29 +1422,23 @@ def repairStyle(node, options):
del styleMap[inkscapeStyle]
num += 1
- # visibility: visible
- if styleMap.has_key('visibility') :
- if styleMap['visibility'] == 'visible':
- del styleMap['visibility']
- num += 1
-
- # display: inline
- if styleMap.has_key('display') :
- if styleMap['display'] == 'inline':
- del styleMap['display']
- num += 1
-
- # overflow: visible or overflow specified on element other than svg, marker, pattern
if styleMap.has_key('overflow') :
- if styleMap['overflow'] == 'visible' or node.nodeName in ['svg','marker','pattern']:
+ # overflow specified on element other than svg, marker, pattern
+ if not node.nodeName in ['svg','marker','pattern']:
del styleMap['overflow']
num += 1
-
- # marker: none
- if styleMap.has_key('marker') :
- if styleMap['marker'] == 'none':
- del styleMap['marker']
- num += 1
+ # it is a marker, pattern or svg
+ # as long as this node is not the document <svg>, then only
+ # remove overflow='hidden'. See
+ # http://www.w3.org/TR/2010/WD-SVG11-20100622/masking.html#OverflowProperty
+ elif node != node.ownerDocument.documentElement:
+ if styleMap['overflow'] == 'hidden':
+ del styleMap['overflow']
+ num += 1
+ # else if outer svg has a overflow="visible", we can remove it
+ elif styleMap['overflow'] == 'visible':
+ del styleMap['overflow']
+ num += 1
# now if any of the properties match known SVG attributes we prefer attributes
# over style so emit them and remove them from the style map
@@ -1103,24 +1447,72 @@ def repairStyle(node, options):
if propName in svgAttributes :
node.setAttribute(propName, styleMap[propName])
del styleMap[propName]
-
- # sew our remaining style properties back together into a style attribute
- fixedStyle = ''
- for prop in styleMap.keys() :
- fixedStyle += prop + ':' + styleMap[prop] + ';'
-
- if fixedStyle != '' :
- node.setAttribute('style', fixedStyle)
- else:
- node.removeAttribute('style')
-
+
+ _setStyle(node, styleMap)
+
# recurse for our child elements
for child in node.childNodes :
num += repairStyle(child,options)
return num
-def removeDefaultAttributeValues(node, options):
+def mayContainTextNodes(node):
+ """
+ Returns True if the passed-in node is probably a text element, or at least
+ one of its descendants is probably a text element.
+
+ If False is returned, it is guaranteed that the passed-in node has no
+ business having text-based attributes.
+
+ If True is returned, the passed-in node should not have its text-based
+ attributes removed.
+ """
+ # Cached result of a prior call?
+ try:
+ return node.mayContainTextNodes
+ except AttributeError:
+ pass
+
+ result = True # Default value
+ # Comment, text and CDATA nodes don't have attributes and aren't containers
+ if node.nodeType != 1:
+ result = False
+ # Non-SVG elements? Unknown elements!
+ elif node.namespaceURI != NS['SVG']:
+ result = True
+ # Blacklisted elements. Those are guaranteed not to be text elements.
+ elif node.nodeName in ['rect', 'circle', 'ellipse', 'line', 'polygon',
+ 'polyline', 'path', 'image', 'stop']:
+ result = False
+ # Group elements. If we're missing any here, the default of True is used.
+ elif node.nodeName in ['g', 'clipPath', 'marker', 'mask', 'pattern',
+ 'linearGradient', 'radialGradient', 'symbol']:
+ result = False
+ for child in node.childNodes:
+ if mayContainTextNodes(child):
+ result = True
+ # Everything else should be considered a future SVG-version text element
+ # at best, or an unknown element at worst. result will stay True.
+
+ # Cache this result before returning it.
+ node.mayContainTextNodes = result
+ return result
+
+def taint(taintedSet, taintedAttribute):
+ u"""Adds an attribute to a set of attributes.
+
+ Related attributes are also included."""
+ taintedSet.add(taintedAttribute)
+ if taintedAttribute == 'marker':
+ taintedSet |= set(['marker-start', 'marker-mid', 'marker-end'])
+ if taintedAttribute in ['marker-start', 'marker-mid', 'marker-end']:
+ taintedSet.add('marker')
+ return taintedSet
+
+def removeDefaultAttributeValues(node, options, tainted=set()):
+ u"""'tainted' keeps a set of attributes defined in parent nodes.
+
+ For such attributes, we don't delete attributes with default values."""
num = 0
if node.nodeType != 1: return 0
@@ -1194,15 +1586,38 @@ def removeDefaultAttributeValues(node, options):
if (r.value == 50 and r.units == Unit.PCT) or (r.value == 0.5 and r.units == Unit.NONE):
node.removeAttribute('r')
num += 1
+
+ # Summarily get rid of some more attributes
+ attributes = [node.attributes.item(i).nodeName
+ for i in range(node.attributes.length)]
+ for attribute in attributes:
+ if attribute not in tainted:
+ if attribute in default_attributes.keys():
+ if node.getAttribute(attribute) == default_attributes[attribute]:
+ node.removeAttribute(attribute)
+ num += 1
+ else:
+ tainted = taint(tainted, attribute)
+ # These attributes might also occur as styles
+ styles = _getStyle(node)
+ for attribute in styles.keys():
+ if attribute not in tainted:
+ if attribute in default_attributes.keys():
+ if styles[attribute] == default_attributes[attribute]:
+ del styles[attribute]
+ num += 1
+ else:
+ tainted = taint(tainted, attribute)
+ _setStyle(node, styles)
# recurse for our child elements
for child in node.childNodes :
- num += removeDefaultAttributeValues(child,options)
+ num += removeDefaultAttributeValues(child, options, tainted.copy())
return num
-rgb = re.compile("\\s*rgb\\(\\s*(\\d+)\\s*\\,\\s*(\\d+)\\s*\\,\\s*(\\d+)\\s*\\)\\s*")
-rgbp = re.compile("\\s*rgb\\(\\s*(\\d*\\.?\\d+)\\%\\s*\\,\\s*(\\d*\\.?\\d+)\\%\\s*\\,\\s*(\\d*\\.?\\d+)\\%\\s*\\)\\s*")
+rgb = re.compile(r"\s*rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*")
+rgbp = re.compile(r"\s*rgb\(\s*(\d*\.?\d+)%\s*,\s*(\d*\.?\d+)%\s*,\s*(\d*\.?\d+)%\s*\)\s*")
def convertColor(value):
"""
Converts the input color string and returns a #RRGGBB (or #RGB if possible) string
@@ -1217,21 +1632,19 @@ def convertColor(value):
r = int(float(rgbpMatch.group(1)) * 255.0 / 100.0)
g = int(float(rgbpMatch.group(2)) * 255.0 / 100.0)
b = int(float(rgbpMatch.group(3)) * 255.0 / 100.0)
- s = 'rgb(%d,%d,%d)' % (r,g,b)
-
- rgbMatch = rgb.match(s)
- if rgbMatch != None :
- r = hex( int( rgbMatch.group(1) ) )[2:].upper()
- g = hex( int( rgbMatch.group(2) ) )[2:].upper()
- b = hex( int( rgbMatch.group(3) ) )[2:].upper()
- if len(r) == 1: r='0'+r
- if len(g) == 1: g='0'+g
- if len(b) == 1: b='0'+b
- s = '#'+r+g+b
-
- if s[0] == '#' and len(s)==7 and s[1]==s[2] and s[3]==s[4] and s[5]==s[6]:
- s = s.upper()
- s = '#'+s[1]+s[3]+s[5]
+ s = '#%02x%02x%02x' % (r, g, b)
+ else:
+ rgbMatch = rgb.match(s)
+ if rgbMatch != None :
+ r = int( rgbMatch.group(1) )
+ g = int( rgbMatch.group(2) )
+ b = int( rgbMatch.group(3) )
+ s = '#%02x%02x%02x' % (r, g, b)
+
+ if s[0] == '#':
+ s = s.lower()
+ if len(s)==7 and s[1]==s[2] and s[3]==s[4] and s[5]==s[6]:
+ s = '#'+s[1]+s[3]+s[5]
return s
@@ -1254,6 +1667,7 @@ def convertColors(element) :
attrsToConvert = ['solid-color']
# now convert all the color formats
+ styles = _getStyle(element)
for attr in attrsToConvert:
oldColorValue = element.getAttribute(attr)
if oldColorValue != '':
@@ -1263,6 +1677,16 @@ def convertColors(element) :
if oldBytes > newBytes:
element.setAttribute(attr, newColorValue)
numBytes += (oldBytes - len(element.getAttribute(attr)))
+ # colors might also hide in styles
+ if attr in styles.keys():
+ oldColorValue = styles[attr]
+ newColorValue = convertColor(oldColorValue)
+ oldBytes = len(oldColorValue)
+ newBytes = len(newColorValue)
+ if oldBytes > newBytes:
+ styles[attr] = newColorValue
+ numBytes += (oldBytes - len(element.getAttribute(attr)))
+ _setStyle(element, styles)
# now recurse for our child elements
for child in element.childNodes :
@@ -1273,7 +1697,7 @@ def convertColors(element) :
# TODO: go over what this method does and see if there is a way to optimize it
# TODO: go over the performance of this method and see if I can save memory/speed by
# reusing data structures, etc
-def cleanPath(element) :
+def cleanPath(element, options) :
"""
Cleans the path string (d attribute) of the element
"""
@@ -1283,292 +1707,168 @@ def cleanPath(element) :
# this gets the parser object from svg_regex.py
oldPathStr = element.getAttribute('d')
- pathObj = svg_parser.parse(oldPathStr)
-
- # however, this parser object has some ugliness in it (lists of tuples of tuples of
- # numbers and booleans). we just need a list of (cmd,[numbers]):
- path = []
- for (cmd,dataset) in pathObj:
- if cmd in ['M','m','L','l','T','t']:
- # one or more tuples, each containing two numbers
- nums = []
- for t in dataset:
- # convert to a Decimal
- nums.append(Decimal(str(t[0])) * Decimal(1))
- nums.append(Decimal(str(t[1])) * Decimal(1))
-
- # only create this segment if it is not empty
- if nums:
- path.append( (cmd, nums) )
-
- elif cmd in ['V','v','H','h']:
- # one or more numbers
- nums = []
- for n in dataset:
- nums.append(Decimal(str(n)))
- if nums:
- path.append( (cmd, nums) )
-
- elif cmd in ['C','c']:
- # one or more tuples, each containing three tuples of two numbers each
- nums = []
- for t in dataset:
- for pair in t:
- nums.append(Decimal(str(pair[0])) * Decimal(1))
- nums.append(Decimal(str(pair[1])) * Decimal(1))
- path.append( (cmd, nums) )
-
- elif cmd in ['S','s','Q','q']:
- # one or more tuples, each containing two tuples of two numbers each
- nums = []
- for t in dataset:
- for pair in t:
- nums.append(Decimal(str(pair[0])) * Decimal(1))
- nums.append(Decimal(str(pair[1])) * Decimal(1))
- path.append( (cmd, nums) )
-
- elif cmd in ['A','a']:
- # one or more tuples, each containing a tuple of two numbers, a number, a boolean,
- # another boolean, and a tuple of two numbers
- nums = []
- for t in dataset:
- nums.append( Decimal(str(t[0][0])) * Decimal(1) )
- nums.append( Decimal(str(t[0][1])) * Decimal(1) )
- nums.append( Decimal(str(t[1])) * Decimal(1))
-
- if t[2]: nums.append( Decimal(1) )
- else: nums.append( Decimal(0) )
+ path = svg_parser.parse(oldPathStr)
- if t[3]: nums.append( Decimal(1) )
- else: nums.append( Decimal(0) )
-
- nums.append( Decimal(str(t[4][0])) * Decimal(1) )
- nums.append( Decimal(str(t[4][1])) * Decimal(1) )
- path.append( (cmd, nums) )
-
- elif cmd in ['Z','z']:
- path.append( (cmd, []) )
+ # This determines whether the stroke has round linecaps. If it does,
+ # we do not want to collapse empty segments, as they are actually rendered.
+ withRoundLineCaps = element.getAttribute('stroke-linecap') == 'round'
- # calculate the starting x,y coord for the second path command
- if len(path[0][1]) == 2:
- (x,y) = path[0][1]
- else:
- # we have a move and then 1 or more coords for lines
- N = len(path[0][1])
- if path[0][0] == 'M':
- # take the last pair of coordinates for the starting point
- x = path[0][1][N-2]
- y = path[0][1][N-1]
- else: # relative move, accumulate coordinates for the starting point
- (x,y) = path[0][1][0],path[0][1][1]
- n = 2
- while n < N:
- x += path[0][1][n]
- y += path[0][1][n+1]
- n += 2
-
- # now we have the starting point at x,y so let's save it
- (startx,starty) = (x,y)
-
- # convert absolute coordinates into relative ones (start with the second subcommand
- # and leave the first M as absolute)
- newPath = [path[0]]
- for (cmd,data) in path[1:]:
+ # The first command must be a moveto, and whether it's relative (m)
+ # or absolute (M), the first set of coordinates *is* absolute. So
+ # the first iteration of the loop below will get x,y and startx,starty.
+
+ # convert absolute coordinates into relative ones.
+ # Reuse the data structure 'path', since we're not adding or removing subcommands.
+ # Also reuse the coordinate lists since we're not adding or removing any.
+ for pathIndex in xrange(0, len(path)):
+ cmd, data = path[pathIndex] # Changes to cmd don't get through to the data structure
i = 0
- newCmd = cmd
- newData = data
# adjust abs to rel
# only the A command has some values that we don't want to adjust (radii, rotation, flags)
if cmd == 'A':
- newCmd = 'a'
- newData = []
- while i < len(data):
- newData.append(data[i])
- newData.append(data[i+1])
- newData.append(data[i+2])
- newData.append(data[i+3])
- newData.append(data[i+4])
- newData.append(data[i+5]-x)
- newData.append(data[i+6]-y)
- x = data[i+5]
- y = data[i+6]
- i += 7
- elif cmd == 'a':
- while i < len(data):
+ for i in xrange(i, len(data), 7):
+ data[i+5] -= x
+ data[i+6] -= y
x += data[i+5]
y += data[i+6]
- i += 7
+ path[pathIndex] = ('a', data)
+ elif cmd == 'a':
+ x += sum(data[5::7])
+ y += sum(data[6::7])
elif cmd == 'H':
- newCmd = 'h'
- newData = []
- while i < len(data):
- newData.append(data[i]-x)
- x = data[i]
- i += 1
- elif cmd == 'h':
- while i < len(data):
+ for i in xrange(i, len(data)):
+ data[i] -= x
x += data[i]
- i += 1
+ path[pathIndex] = ('h', data)
+ elif cmd == 'h':
+ x += sum(data)
elif cmd == 'V':
- newCmd = 'v'
- newData = []
- while i < len(data):
- newData.append(data[i] - y)
- y = data[i]
- i += 1
- elif cmd == 'v':
- while i < len(data):
+ for i in xrange(i, len(data)):
+ data[i] -= y
y += data[i]
- i += 1
- elif cmd in ['M']:
- newCmd = cmd.lower()
- newData = []
- startx = data[0]
- starty = data[1]
- while i < len(data):
- newData.append( data[i] - x )
- newData.append( data[i+1] - y )
- x = data[i]
- y = data[i+1]
- i += 2
+ path[pathIndex] = ('v', data)
+ elif cmd == 'v':
+ y += sum(data)
+ elif cmd == 'M':
+ startx, starty = data[0], data[1]
+ # If this is a path starter, don't convert its first
+ # coordinate to relative; that would just make it (0, 0)
+ if pathIndex != 0:
+ data[0] -= x
+ data[1] -= y
+
+ x, y = startx, starty
+ i = 2
+ for i in xrange(i, len(data), 2):
+ data[i] -= x
+ data[i+1] -= y
+ x += data[i]
+ y += data[i+1]
+ path[pathIndex] = ('m', data)
elif cmd in ['L','T']:
- newCmd = cmd.lower()
- newData = []
- while i < len(data):
- newData.append( data[i] - x )
- newData.append( data[i+1] - y )
- x = data[i]
- y = data[i+1]
- i += 2
- elif cmd in ['m']:
- startx += data[0]
- starty += data[1]
- while i < len(data):
+ for i in xrange(i, len(data), 2):
+ data[i] -= x
+ data[i+1] -= y
x += data[i]
y += data[i+1]
- i += 2
- elif cmd in ['l','t']:
- while i < len(data):
+ path[pathIndex] = (cmd.lower(), data)
+ elif cmd in ['m']:
+ if pathIndex == 0:
+ # START OF PATH - this is an absolute moveto
+ # followed by relative linetos
+ startx, starty = data[0], data[1]
+ x, y = startx, starty
+ i = 2
+ else:
+ startx = x + data[0]
+ starty = y + data[1]
+ for i in xrange(i, len(data), 2):
x += data[i]
y += data[i+1]
- i += 2
+ elif cmd in ['l','t']:
+ x += sum(data[0::2])
+ y += sum(data[1::2])
elif cmd in ['S','Q']:
- newCmd = cmd.lower()
- newData = []
- while i < len(data):
- newData.append( data[i] - x )
- newData.append( data[i+1] - y )
- newData.append( data[i+2] - x )
- newData.append( data[i+3] - y )
- x = data[i+2]
- y = data[i+3]
- i += 4
- elif cmd in ['s','q']:
- while i < len(data):
+ for i in xrange(i, len(data), 4):
+ data[i] -= x
+ data[i+1] -= y
+ data[i+2] -= x
+ data[i+3] -= y
x += data[i+2]
y += data[i+3]
- i += 4
+ path[pathIndex] = (cmd.lower(), data)
+ elif cmd in ['s','q']:
+ x += sum(data[2::4])
+ y += sum(data[3::4])
elif cmd == 'C':
- newCmd = 'c'
- newData = []
- while i < len(data):
- newData.append( data[i] - x )
- newData.append( data[i+1] - y )
- newData.append( data[i+2] - x )
- newData.append( data[i+3] - y )
- newData.append( data[i+4] - x )
- newData.append( data[i+5] - y )
- x = data[i+4]
- y = data[i+5]
- i += 6
- elif cmd == 'c':
- while i < len(data):
+ for i in xrange(i, len(data), 6):
+ data[i] -= x
+ data[i+1] -= y
+ data[i+2] -= x
+ data[i+3] -= y
+ data[i+4] -= x
+ data[i+5] -= y
x += data[i+4]
y += data[i+5]
- i += 6
+ path[pathIndex] = ('c', data)
+ elif cmd == 'c':
+ x += sum(data[4::6])
+ y += sum(data[5::6])
elif cmd in ['z','Z']:
- x = startx
- y = starty
- newCmd = 'z'
- newPath.append( (newCmd, newData) )
- path = newPath
+ x, y = startx, starty
+ path[pathIndex] = ('z', data)
# remove empty segments
- newPath = [path[0]]
- for (cmd,data) in path[1:]:
- if cmd in ['m','l','t']:
- newData = []
+ # Reuse the data structure 'path' and the coordinate lists, even if we're
+ # deleting items, because these deletions are relatively cheap.
+ if not withRoundLineCaps:
+ for pathIndex in xrange(0, len(path)):
+ cmd, data = path[pathIndex]
i = 0
- while i < len(data):
- if data[i] != 0 or data[i+1] != 0:
- newData.append(data[i])
- newData.append(data[i+1])
- else:
- numPathSegmentsReduced += 1
- i += 2
- if newData:
- newPath.append( (cmd,newData) )
- elif cmd == 'c':
- newData = []
- i = 0
- while i < len(data):
- if data[i+4] != 0 or data[i+5] != 0:
- newData.append(data[i])
- newData.append(data[i+1])
- newData.append(data[i+2])
- newData.append(data[i+3])
- newData.append(data[i+4])
- newData.append(data[i+5])
- else:
- numPathSegmentsReduced += 1
- i += 6
- if newData:
- newPath.append( (cmd,newData) )
- elif cmd == 'a':
- newData = []
- i = 0
- while i < len(data):
- if data[i+5] != 0 or data[i+6] != 0:
- newData.append(data[i])
- newData.append(data[i+1])
- newData.append(data[i+2])
- newData.append(data[i+3])
- newData.append(data[i+4])
- newData.append(data[i+5])
- newData.append(data[i+6])
- else:
- numPathSegmentsReduced += 1
- i += 7
- if newData:
- newPath.append( (cmd,newData) )
- elif cmd == 'q':
- newData = []
- i = 0
- while i < len(data):
- if data[i+2] != 0 or data[i+3] != 0:
- newData.append(data[i])
- newData.append(data[i+1])
- newData.append(data[i+2])
- newData.append(data[i+3])
- else:
- numPathSegmentsReduced += 1
- i += 4
- if newData:
- newPath.append( (cmd,newData) )
- elif cmd in ['h','v']:
- newData = []
- i = 0
- while i < len(data):
- if data[i] != 0:
- newData.append(data[i])
- else:
- numPathSegmentsReduced += 1
- i += 1
- if newData:
- newPath.append( (cmd,newData) )
- else:
- newPath.append( (cmd,data) )
- path = newPath
+ if cmd in ['m','l','t']:
+ if cmd == 'm':
+ # remove m0,0 segments
+ if pathIndex > 0 and data[0] == data[i+1] == 0:
+ # 'm0,0 x,y' can be replaces with 'lx,y',
+ # except the first m which is a required absolute moveto
+ path[pathIndex] = ('l', data[2:])
+ numPathSegmentsReduced += 1
+ else: # else skip move coordinate
+ i = 2
+ while i < len(data):
+ if data[i] == data[i+1] == 0:
+ del data[i:i+2]
+ numPathSegmentsReduced += 1
+ else:
+ i += 2
+ elif cmd == 'c':
+ while i < len(data):
+ if data[i] == data[i+1] == data[i+2] == data[i+3] == data[i+4] == data[i+5] == 0:
+ del data[i:i+6]
+ numPathSegmentsReduced += 1
+ else:
+ i += 6
+ elif cmd == 'a':
+ while i < len(data):
+ if data[i+5] == data[i+6] == 0:
+ del data[i:i+7]
+ numPathSegmentsReduced += 1
+ else:
+ i += 7
+ elif cmd == 'q':
+ while i < len(data):
+ if data[i] == data[i+1] == data[i+2] == data[i+3] == 0:
+ del data[i:i+4]
+ numPathSegmentsReduced += 1
+ else:
+ i += 4
+ elif cmd in ['h','v']:
+ oldLen = len(data)
+ path[pathIndex] = (cmd, [coord for coord in data if coord != 0])
+ numPathSegmentsReduced += len(path[pathIndex][1]) - oldLen
+
+ # fixup: Delete subcommands having no coordinates.
+ path = [elem for elem in path if len(elem[1]) > 0 or elem[0] == 'z']
# convert straight curves into lines
newPath = [path[0]]
@@ -1593,7 +1893,7 @@ def cleanPath(element) :
foundStraightCurve = True
else:
m = dy/dx
- if p1y == m*p1x and p2y == m*p2y:
+ if p1y == m*p1x and p2y == m*p2x:
foundStraightCurve = True
if foundStraightCurve:
@@ -1605,12 +1905,7 @@ def cleanPath(element) :
newPath.append( ('l', [dx,dy]) )
numCurvesStraightened += 1
else:
- newData.append(data[i])
- newData.append(data[i+1])
- newData.append(data[i+2])
- newData.append(data[i+3])
- newData.append(data[i+4])
- newData.append(data[i+5])
+ newData.extend(data[i:i+6])
i += 6
if newData or cmd == 'z' or cmd == 'Z':
@@ -1620,8 +1915,8 @@ def cleanPath(element) :
# collapse all consecutive commands of the same type into one command
prevCmd = ''
prevData = []
- newPath = [path[0]]
- for (cmd,data) in path[1:]:
+ newPath = []
+ for (cmd,data) in path:
# flush the previous command if it is not the same type as the current command
if prevCmd != '':
if cmd != prevCmd or cmd == 'm':
@@ -1629,11 +1924,11 @@ def cleanPath(element) :
prevCmd = ''
prevData = []
- # if the previous and current commands are the same type, collapse
+ # if the previous and current commands are the same type,
+ # or the previous command is moveto and the current is lineto, collapse,
# but only if they are not move commands (since move can contain implicit lineto commands)
- if cmd == prevCmd and cmd != 'm':
- for coord in data:
- prevData.append(coord)
+ if (cmd == prevCmd or (cmd == 'l' and prevCmd == 'm')) and cmd != 'm':
+ prevData.extend(data)
# save last command and data
else:
@@ -1645,9 +1940,9 @@ def cleanPath(element) :
path = newPath
# convert to shorthand path segments where possible
- newPath = [path[0]]
- for (cmd,data) in path[1:]:
- # convert line segments into h,v where possible
+ newPath = []
+ for (cmd,data) in path:
+ # convert line segments into h,v where possible
if cmd == 'l':
i = 0
lineTuples = []
@@ -1669,11 +1964,38 @@ def cleanPath(element) :
newPath.append( ('h', [data[i]]) )
numPathSegmentsReduced += 1
else:
- lineTuples.append(data[i])
- lineTuples.append(data[i+1])
+ lineTuples.extend(data[i:i+2])
i += 2
if lineTuples:
newPath.append( ('l', lineTuples) )
+ # also handle implied relative linetos
+ elif cmd == 'm':
+ i = 2
+ lineTuples = [data[0], data[1]]
+ while i < len(data):
+ if data[i] == 0:
+ # vertical
+ if lineTuples:
+ # flush the existing m/l command
+ newPath.append( (cmd, lineTuples) )
+ lineTuples = []
+ cmd = 'l' # dealing with linetos now
+ # append the v and then the remaining line coords
+ newPath.append( ('v', [data[i+1]]) )
+ numPathSegmentsReduced += 1
+ elif data[i+1] == 0:
+ if lineTuples:
+ # flush the m/l command, then append the h and then the remaining line coords
+ newPath.append( (cmd, lineTuples) )
+ lineTuples = []
+ cmd = 'l' # dealing with linetos now
+ newPath.append( ('h', [data[i]]) )
+ numPathSegmentsReduced += 1
+ else:
+ lineTuples.extend(data[i:i+2])
+ i += 2
+ if lineTuples:
+ newPath.append( (cmd, lineTuples) )
# convert Bézier curve segments into s where possible
elif cmd == 'c':
bez_ctl_pt = (0,0)
@@ -1732,23 +2054,20 @@ def cleanPath(element) :
# for each h or v, collapse unnecessary coordinates that run in the same direction
# i.e. "h-100-100" becomes "h-200" but "h300-100" does not change
- newPath = [path[0]]
- for (cmd,data) in path[1:]:
+ # Reuse the data structure 'path', since we're not adding or removing subcommands.
+ # Also reuse the coordinate lists, even if we're deleting items, because these
+ # deletions are relatively cheap.
+ for pathIndex in xrange(1, len(path)):
+ cmd, data = path[pathIndex]
if cmd in ['h','v'] and len(data) > 1:
- newData = []
- prevCoord = data[0]
- for coord in data[1:]:
- if isSameSign(prevCoord, coord):
- prevCoord += coord
+ coordIndex = 1
+ while coordIndex < len(data):
+ if isSameSign(data[coordIndex - 1], data[coordIndex]):
+ data[coordIndex - 1] += data[coordIndex]
+ del data[coordIndex]
numPathSegmentsReduced += 1
else:
- newData.append(prevCoord)
- prevCoord = coord
- newData.append(prevCoord)
- newPath.append( (cmd, newData) )
- else:
- newPath.append( (cmd, data) )
- path = newPath
+ coordIndex += 1
# it is possible that we have consecutive h, v, c, t commands now
# so again collapse all consecutive commands of the same type into one command
@@ -1765,8 +2084,7 @@ def cleanPath(element) :
# if the previous and current commands are the same type, collapse
if cmd == prevCmd and cmd != 'm':
- for coord in data:
- prevData.append(coord)
+ prevData.extend(data)
# save last command and data
else:
@@ -1777,7 +2095,7 @@ def cleanPath(element) :
newPath.append( (prevCmd, prevData) )
path = newPath
- newPathStr = serializePath(path)
+ newPathStr = serializePath(path, options)
numBytesSavedInPathData += ( len(oldPathStr) - len(newPathStr) )
element.setAttribute('d', newPathStr)
@@ -1788,26 +2106,25 @@ def parseListOfPoints(s):
Returns a list of containing an even number of coordinate strings
"""
i = 0
- points = []
# (wsp)? comma-or-wsp-separated coordinate pairs (wsp)?
# coordinate-pair = coordinate comma-or-wsp coordinate
# coordinate = sign? integer
# comma-wsp: (wsp+ comma? wsp*) | (comma wsp*)
- ws_nums = re.split("\\s*\\,?\\s*", s.strip())
+ ws_nums = re.split(r"\s*,?\s*", s.strip())
nums = []
# also, if 100-100 is found, split it into two also
- # <polygon points="100,-100,100-100,100-100-100,-100-100" />
- for i in range(len(ws_nums)):
- negcoords = re.split("\\-", ws_nums[i]);
+ # <polygon points="100,-100,100-100,100-100-100,-100-100" />
+ for i in xrange(len(ws_nums)):
+ negcoords = ws_nums[i].split("-")
# this string didn't have any negative coordinates
if len(negcoords) == 1:
nums.append(negcoords[0])
# we got negative coords
else:
- for j in range(len(negcoords)):
+ for j in xrange(len(negcoords)):
# first number could be positive
if j == 0:
if negcoords[0] != '':
@@ -1817,28 +2134,28 @@ def parseListOfPoints(s):
# unless we accidentally split a number that was in scientific notation
# and had a negative exponent (500.00e-1)
prev = nums[len(nums)-1]
- if prev[len(prev)-1] == 'e' or prev[len(prev)-1] == 'E':
+ if prev[len(prev)-1] in ['e', 'E']:
nums[len(nums)-1] = prev + '-' + negcoords[j]
else:
nums.append( '-'+negcoords[j] )
- # now resolve into SVGLength values
+ # if we have an odd number of points, return empty
+ if len(nums) % 2 != 0: return []
+
+ # now resolve into Decimal values
i = 0
while i < len(nums):
- x = SVGLength(nums[i])
- # if we had an odd number of points, return empty
- if i == len(nums)-1: return []
- else: y = SVGLength(nums[i+1])
+ try:
+ nums[i] = getcontext().create_decimal(nums[i])
+ nums[i + 1] = getcontext().create_decimal(nums[i + 1])
+ except decimal.InvalidOperation: # one of the lengths had a unit or is an invalid number
+ return []
- # if the coordinates were not unitless, return empty
- if x.units != Unit.NONE or y.units != Unit.NONE: return []
- points.append( str(x.value) )
- points.append( str(y.value) )
i += 2
- return points
+ return nums
-def cleanPolygon(elem):
+def cleanPolygon(elem, options):
"""
Remove unnecessary closing point of polygon points attribute
"""
@@ -1847,84 +2164,411 @@ def cleanPolygon(elem):
pts = parseListOfPoints(elem.getAttribute('points'))
N = len(pts)/2
if N >= 2:
- (startx,starty) = (pts[0],pts[0])
- (endx,endy) = (pts[len(pts)-2],pts[len(pts)-1])
+ (startx,starty) = pts[:2]
+ (endx,endy) = pts[-2:]
if startx == endx and starty == endy:
- pts = pts[:-2]
- numPointsRemovedFromPolygon += 1
- elem.setAttribute('points', scourCoordinates(pts,True))
+ del pts[-2:]
+ numPointsRemovedFromPolygon += 1
+ elem.setAttribute('points', scourCoordinates(pts, options, True))
-def cleanPolyline(elem):
+def cleanPolyline(elem, options):
"""
Scour the polyline points attribute
"""
pts = parseListOfPoints(elem.getAttribute('points'))
- elem.setAttribute('points', scourCoordinates(pts,True))
-
-def serializePath(pathObj):
+ elem.setAttribute('points', scourCoordinates(pts, options, True))
+
+def serializePath(pathObj, options):
"""
Reserializes the path data with some cleanups.
"""
- pathStr = ""
- for (cmd,data) in pathObj:
- pathStr += cmd
- # elliptical arc commands must have comma/wsp separating the coordinates
- # this fixes an issue outlined in Fix https://bugs.launchpad.net/scour/+bug/412754
- pathStr += scourCoordinates(data, (cmd == 'a'))
- return pathStr
-
-def scourCoordinates(data, forceCommaWsp = False):
+ # elliptical arc commands must have comma/wsp separating the coordinates
+ # this fixes an issue outlined in Fix https://bugs.launchpad.net/scour/+bug/412754
+ return ''.join([cmd + scourCoordinates(data, options, (cmd == 'a')) for cmd, data in pathObj])
+
+def serializeTransform(transformObj):
+ """
+ Reserializes the transform data with some cleanups.
+ """
+ return ' '.join(
+ [command + '(' + ' '.join(
+ [scourUnitlessLength(number) for number in numbers]
+ ) + ')'
+ for command, numbers in transformObj]
+ )
+
+def scourCoordinates(data, options, forceCommaWsp = False):
"""
Serializes coordinate data with some cleanups:
- removes all trailing zeros after the decimal
- integerize coordinates if possible
- removes extraneous whitespace
- - adds commas between values in a subcommand if required (or if forceCommaWsp is True)
+ - adds spaces between values in a subcommand if required (or if forceCommaWsp is True)
"""
- coordsStr = ""
if data != None:
+ newData = []
c = 0
+ previousCoord = ''
for coord in data:
+ scouredCoord = scourUnitlessLength(coord, needsRendererWorkaround=options.renderer_workaround)
+ # only need the comma if the current number starts with a digit
+ # (numbers can start with - without needing a comma before)
+ # or if forceCommaWsp is True
+ # or if this number starts with a dot and the previous number
+ # had *no* dot or exponent (so we can go like -5.5.5 for -5.5,0.5
+ # and 4e4.5 for 40000,0.5)
+ if c > 0 and (forceCommaWsp
+ or scouredCoord[0].isdigit()
+ or (scouredCoord[0] == '.' and not ('.' in previousCoord or 'e' in previousCoord))
+ ):
+ newData.append( ' ' )
+
# add the scoured coordinate to the path string
- coordsStr += scourLength(coord)
-
- # only need the comma if the next number is non-negative or if forceCommaWsp is True
- if c < len(data)-1 and (forceCommaWsp or Decimal(data[c+1]) >= 0):
- coordsStr += ','
+ newData.append( scouredCoord )
+ previousCoord = scouredCoord
c += 1
- return coordsStr
+
+ # What we need to do to work around GNOME bugs 548494, 563933 and
+ # 620565, which are being fixed and unfixed in Ubuntu, is
+ # to make sure that a dot doesn't immediately follow a command
+ # (so 'h50' and 'h0.5' are allowed, but not 'h.5').
+ # Then, we need to add a space character after any coordinates
+ # having an 'e' (scientific notation), so as to have the exponent
+ # separate from the next number.
+ if options.renderer_workaround:
+ if len(newData) > 0:
+ for i in xrange(1, len(newData)):
+ if newData[i][0] == '-' and 'e' in newData[i - 1]:
+ newData[i - 1] += ' '
+ return ''.join(newData)
+ else:
+ return ''.join(newData)
+
+ return ''
+
+def scourLength(length):
+ """
+ Scours a length. Accepts units.
+ """
+ length = SVGLength(length)
+
+ return scourUnitlessLength(length.value) + Unit.str(length.units)
-def scourLength(str):
- length = SVGLength(str)
- coord = length.value
+def scourUnitlessLength(length, needsRendererWorkaround=False): # length is of a numeric type
+ """
+ Scours the numeric part of a length only. Does not accept units.
+ This is faster than scourLength on elements guaranteed not to
+ contain units.
+ """
# reduce to the proper number of digits
- coord = Decimal(unicode(coord)) * Decimal(1)
+ if not isinstance(length, Decimal):
+ length = getcontext().create_decimal(str(length))
+ # if the value is an integer, it may still have .0[...] attached to it for some reason
+ # remove those
+ if int(length) == length:
+ length = getcontext().create_decimal(int(length))
+
+ # gather the non-scientific notation version of the coordinate.
+ # this may actually be in scientific notation if the value is
+ # sufficiently large or small, so this is a misnomer.
+ nonsci = unicode(length).lower().replace("e+", "e")
+ if not needsRendererWorkaround:
+ if len(nonsci) > 2 and nonsci[:2] == '0.':
+ nonsci = nonsci[1:] # remove the 0, leave the dot
+ elif len(nonsci) > 3 and nonsci[:3] == '-0.':
+ nonsci = '-' + nonsci[2:] # remove the 0, leave the minus and dot
+
+ if len(nonsci) > 3: # avoid calling normalize unless strictly necessary
+ # and then the scientific notation version, with E+NUMBER replaced with
+ # just eNUMBER, since SVG accepts this.
+ sci = unicode(length.normalize()).lower().replace("e+", "e")
+
+ if len(sci) < len(nonsci): return sci
+ else: return nonsci
+ else: return nonsci
+
+def reducePrecision(element) :
+ """
+ Because opacities, letter spacings, stroke widths and all that don't need
+ to be preserved in SVG files with 9 digits of precision.
+
+ Takes all of these attributes, in the given element node and its children,
+ and reduces their precision to the current Decimal context's precision.
+ Also checks for the attributes actually being lengths, not 'inherit', 'none'
+ or anything that isn't an SVGLength.
- # integerize if we can
- if int(coord) == coord: coord = Decimal(unicode(int(coord)))
+ Returns the number of bytes saved after performing these reductions.
+ """
+ num = 0
+
+ styles = _getStyle(element)
+ for lengthAttr in ['opacity', 'flood-opacity', 'fill-opacity',
+ 'stroke-opacity', 'stop-opacity', 'stroke-miterlimit',
+ 'stroke-dashoffset', 'letter-spacing', 'word-spacing',
+ 'kerning', 'font-size-adjust', 'font-size',
+ 'stroke-width']:
+ val = element.getAttribute(lengthAttr)
+ if val != '':
+ valLen = SVGLength(val)
+ if valLen.units != Unit.INVALID: # not an absolute/relative size or inherit, can be % though
+ newVal = scourLength(val)
+ if len(newVal) < len(val):
+ num += len(val) - len(newVal)
+ element.setAttribute(lengthAttr, newVal)
+ # repeat for attributes hidden in styles
+ if lengthAttr in styles.keys():
+ val = styles[lengthAttr]
+ valLen = SVGLength(val)
+ if valLen.units != Unit.INVALID:
+ newVal = scourLength(val)
+ if len(newVal) < len(val):
+ num += len(val) - len(newVal)
+ styles[lengthAttr] = newVal
+ _setStyle(element, styles)
+
+ for child in element.childNodes:
+ if child.nodeType == 1:
+ num += reducePrecision(child)
+
+ return num
- # Decimal.trim() is available in Python 2.6+ to trim trailing zeros
- try:
- coord = coord.trim()
- except AttributeError:
- # trim it ourselves
- s = unicode(coord)
- dec = s.find('.')
- if dec != -1:
- while s[-1] == '0':
- s = s[:-1]
- coord = Decimal(s)
-
- # Decimal.normalize() will uses scientific notation - if that
- # string is smaller, then use it
- normd = coord.normalize()
- if len(unicode(normd)) < len(unicode(coord)):
- coord = normd
-
- return unicode(coord)+Unit.str(length.units)
+def optimizeAngle(angle):
+ """
+ Because any rotation can be expressed within 360 degrees
+ of any given number, and since negative angles sometimes
+ are one character longer than corresponding positive angle,
+ we shorten the number to one in the range to [-90, 270[.
+ """
+ # First, we put the new angle in the range ]-360, 360[.
+ # The modulo operator yields results with the sign of the
+ # divisor, so for negative dividends, we preserve the sign
+ # of the angle.
+ if angle < 0: angle %= -360
+ else: angle %= 360
+ # 720 degrees is unneccessary, as 360 covers all angles.
+ # As "-x" is shorter than "35x" and "-xxx" one character
+ # longer than positive angles <= 260, we constrain angle
+ # range to [-90, 270[ (or, equally valid: ]-100, 260]).
+ if angle >= 270: angle -= 360
+ elif angle < -90: angle += 360
+ return angle
+
+
+def optimizeTransform(transform):
+ """
+ Optimises a series of transformations parsed from a single
+ transform="" attribute.
+
+ The transformation list is modified in-place.
+ """
+ # FIXME: reordering these would optimize even more cases:
+ # first: Fold consecutive runs of the same transformation
+ # extra: Attempt to cast between types to create sameness:
+ # "matrix(0 1 -1 0 0 0) rotate(180) scale(-1)" all
+ # are rotations (90, 180, 180) -- thus "rotate(90)"
+ # second: Simplify transforms where numbers are optional.
+ # third: Attempt to simplify any single remaining matrix()
+ #
+ # if there's only one transformation and it's a matrix,
+ # try to make it a shorter non-matrix transformation
+ # NOTE: as matrix(a b c d e f) in SVG means the matrix:
+ # |¯ a c e ¯| make constants |¯ A1 A2 A3 ¯|
+ # | b d f | translating them | B1 B2 B3 |
+ # |_ 0 0 1 _| to more readable |_ 0 0 1 _|
+ if len(transform) == 1 and transform[0][0] == 'matrix':
+ matrix = A1, B1, A2, B2, A3, B3 = transform[0][1]
+ # |¯ 1 0 0 ¯|
+ # | 0 1 0 | Identity matrix (no transformation)
+ # |_ 0 0 1 _|
+ if matrix == [1, 0, 0, 1, 0, 0]:
+ del transform[0]
+ # |¯ 1 0 X ¯|
+ # | 0 1 Y | Translation by (X, Y).
+ # |_ 0 0 1 _|
+ elif (A1 == 1 and A2 == 0
+ and B1 == 0 and B2 == 1):
+ transform[0] = ('translate', [A3, B3])
+ # |¯ X 0 0 ¯|
+ # | 0 Y 0 | Scaling by (X, Y).
+ # |_ 0 0 1 _|
+ elif ( A2 == 0 and A3 == 0
+ and B1 == 0 and B3 == 0):
+ transform[0] = ('scale', [A1, B2])
+ # |¯ cos(A) -sin(A) 0 ¯| Rotation by angle A,
+ # | sin(A) cos(A) 0 | clockwise, about the origin.
+ # |_ 0 0 1 _| A is in degrees, [-180...180].
+ elif (A1 == B2 and -1 <= A1 <= 1 and A3 == 0
+ and -B1 == A2 and -1 <= B1 <= 1 and B3 == 0
+ # as cos² A + sin² A == 1 and as decimal trig is approximate:
+ # FIXME: the "epsilon" term here should really be some function
+ # of the precision of the (sin|cos)_A terms, not 1e-15:
+ and abs((B1 ** 2) + (A1 ** 2) - 1) < Decimal("1e-15")):
+ sin_A, cos_A = B1, A1
+ # while asin(A) and acos(A) both only have an 180° range
+ # the sign of sin(A) and cos(A) varies across quadrants,
+ # letting us hone in on the angle the matrix represents:
+ # -- => < -90 | -+ => -90..0 | ++ => 0..90 | +- => >= 90
+ #
+ # http://en.wikipedia.org/wiki/File:Sine_cosine_plot.svg
+ # shows asin has the correct angle the middle quadrants:
+ A = Decimal(str(math.degrees(math.asin(float(sin_A)))))
+ if cos_A < 0: # otherwise needs adjusting from the edges
+ if sin_A < 0:
+ A = -180 - A
+ else:
+ A = 180 - A
+ transform[0] = ('rotate', [A])
+
+ # Simplify transformations where numbers are optional.
+ for type, args in transform:
+ if type == 'translate':
+ # Only the X coordinate is required for translations.
+ # If the Y coordinate is unspecified, it's 0.
+ if len(args) == 2 and args[1] == 0:
+ del args[1]
+ elif type == 'rotate':
+ args[0] = optimizeAngle(args[0]) # angle
+ # Only the angle is required for rotations.
+ # If the coordinates are unspecified, it's the origin (0, 0).
+ if len(args) == 3 and args[1] == args[2] == 0:
+ del args[1:]
+ elif type == 'scale':
+ # Only the X scaling factor is required.
+ # If the Y factor is unspecified, it's the same as X.
+ if len(args) == 2 and args[0] == args[1]:
+ del args[1]
+
+ # Attempt to coalesce runs of the same transformation.
+ # Translations followed immediately by other translations,
+ # rotations followed immediately by other rotations,
+ # scaling followed immediately by other scaling,
+ # are safe to add.
+ # Identity skewX/skewY are safe to remove, but how do they accrete?
+ # |¯ 1 0 0 ¯|
+ # | tan(A) 1 0 | skews X coordinates by angle A
+ # |_ 0 0 1 _|
+ #
+ # |¯ 1 tan(A) 0 ¯|
+ # | 0 1 0 | skews Y coordinates by angle A
+ # |_ 0 0 1 _|
+ #
+ # FIXME: A matrix followed immediately by another matrix
+ # would be safe to multiply together, too.
+ i = 1
+ while i < len(transform):
+ currType, currArgs = transform[i]
+ prevType, prevArgs = transform[i - 1]
+ if currType == prevType == 'translate':
+ prevArgs[0] += currArgs[0] # x
+ # for y, only add if the second translation has an explicit y
+ if len(currArgs) == 2:
+ if len(prevArgs) == 2:
+ prevArgs[1] += currArgs[1] # y
+ elif len(prevArgs) == 1:
+ prevArgs.append(currArgs[1]) # y
+ del transform[i]
+ if prevArgs[0] == prevArgs[1] == 0:
+ # Identity translation!
+ i -= 1
+ del transform[i]
+ elif (currType == prevType == 'rotate'
+ and len(prevArgs) == len(currArgs) == 1):
+ # Only coalesce if both rotations are from the origin.
+ prevArgs[0] = optimizeAngle(prevArgs[0] + currArgs[0])
+ del transform[i]
+ elif currType == prevType == 'scale':
+ prevArgs[0] *= currArgs[0] # x
+ # handle an implicit y
+ if len(prevArgs) == 2 and len(currArgs) == 2:
+ # y1 * y2
+ prevArgs[1] *= currArgs[1]
+ elif len(prevArgs) == 1 and len(currArgs) == 2:
+ # create y2 = uniformscalefactor1 * y2
+ prevArgs.append(prevArgs[0] * currArgs[1])
+ elif len(prevArgs) == 2 and len(currArgs) == 1:
+ # y1 * uniformscalefactor2
+ prevArgs[1] *= currArgs[0]
+ del transform[i]
+ if prevArgs[0] == prevArgs[1] == 1:
+ # Identity scale!
+ i -= 1
+ del transform[i]
+ else:
+ i += 1
+
+ # Some fixups are needed for single-element transformation lists, since
+ # the loop above was to coalesce elements with their predecessors in the
+ # list, and thus it required 2 elements.
+ i = 0
+ while i < len(transform):
+ currType, currArgs = transform[i]
+ if ((currType == 'skewX' or currType == 'skewY')
+ and len(currArgs) == 1 and currArgs[0] == 0):
+ # Identity skew!
+ del transform[i]
+ elif ((currType == 'rotate')
+ and len(currArgs) == 1 and currArgs[0] == 0):
+ # Identity rotation!
+ del transform[i]
+ else:
+ i += 1
+
+def optimizeTransforms(element, options) :
+ """
+ Attempts to optimise transform specifications on the given node and its children.
+
+ Returns the number of bytes saved after performing these reductions.
+ """
+ num = 0
+
+ for transformAttr in ['transform', 'patternTransform', 'gradientTransform']:
+ val = element.getAttribute(transformAttr)
+ if val != '':
+ transform = svg_transform_parser.parse(val)
+
+ optimizeTransform(transform)
+
+ newVal = serializeTransform(transform)
+
+ if len(newVal) < len(val):
+ if len(newVal):
+ element.setAttribute(transformAttr, newVal)
+ else:
+ element.removeAttribute(transformAttr)
+ num += len(val) - len(newVal)
+
+ for child in element.childNodes:
+ if child.nodeType == 1:
+ num += optimizeTransforms(child, options)
+
+ return num
+
+def removeComments(element) :
+ """
+ Removes comments from the element and its children.
+ """
+ global numCommentBytes
+
+ if isinstance(element, xml.dom.minidom.Document):
+ # must process the document object separately, because its
+ # documentElement's nodes have None as their parentNode
+ for subelement in element.childNodes:
+ if isinstance(element, xml.dom.minidom.Comment):
+ numCommentBytes += len(element.data)
+ element.documentElement.removeChild(subelement)
+ else:
+ removeComments(subelement)
+ elif isinstance(element, xml.dom.minidom.Comment):
+ numCommentBytes += len(element.data)
+ element.parentNode.removeChild(element)
+ else:
+ for subelement in element.childNodes:
+ removeComments(subelement)
def embedRasters(element, options) :
+ import base64
+ import urllib
"""
Converts raster references to inline images.
NOTE: there are size limits to base64-encoding handling in browsers
@@ -1941,24 +2585,24 @@ def embedRasters(element, options) :
# look for 'png', 'jpg', and 'gif' extensions
if ext == 'png' or ext == 'jpg' or ext == 'gif':
- # check if href resolves to an existing file
- if os.path.isfile(href) == False :
- if href[:7] != 'http://' and os.path.isfile(href) == False :
- # if this is not an absolute path, set path relative
- # to script file based on input arg
- infilename = '.'
- if options.infilename: infilename = options.infilename
- href = os.path.join(os.path.dirname(infilename), href)
-
+ # file:// URLs denote files on the local system too
+ if href[:7] == 'file://':
+ href = href[7:]
+ # does the file exist?
+ if os.path.isfile(href):
+ # if this is not an absolute path, set path relative
+ # to script file based on input arg
+ infilename = '.'
+ if options.infilename: infilename = options.infilename
+ href = os.path.join(os.path.dirname(infilename), href)
+
rasterdata = ''
# test if file exists locally
- if os.path.isfile(href) == True :
+ if os.path.isfile(href):
# open raster file as raw binary
raster = open( href, "rb")
rasterdata = raster.read()
-
elif href[:7] == 'http://':
- # raster = open( href, "rb")
webFile = urllib.urlopen( href )
rasterdata = webFile.read()
webFile.close()
@@ -1979,15 +2623,17 @@ def embedRasters(element, options) :
numRastersEmbedded += 1
del b64eRaster
-def properlySizeDoc(docElement):
+def properlySizeDoc(docElement, options):
# get doc width and height
w = SVGLength(docElement.getAttribute('width'))
h = SVGLength(docElement.getAttribute('height'))
- # if width/height are not unitless or px then it is not ok to rewrite them into a viewBox
- if ((w.units != Unit.NONE and w.units != Unit.PX) or
- (w.units != Unit.NONE and w.units != Unit.PX)):
- return
+ # if width/height are not unitless or px then it is not ok to rewrite them into a viewBox.
+ # well, it may be OK for Web browsers and vector editors, but not for librsvg.
+ if options.renderer_workaround:
+ if ((w.units != Unit.NONE and w.units != Unit.PX) or
+ (h.units != Unit.NONE and h.units != Unit.PX)):
+ return
# else we have a statically sized image and we should try to remedy that
@@ -2035,7 +2681,7 @@ def remapNamespacePrefix(node, oldprefix, newprefix):
# add all the attributes
attrList = node.attributes
- for i in range(attrList.length):
+ for i in xrange(attrList.length):
attr = attrList.item(i)
newNode.setAttributeNS( attr.namespaceURI, attr.localName, attr.nodeValue)
@@ -2070,12 +2716,14 @@ def makeWellFormed(str):
# - somewhat judicious use of whitespace
# - ensure id attributes are first
def serializeXML(element, options, ind = 0, preserveWhitespace = False):
+ outParts = []
+
indent = ind
I=''
if options.indent_type == 'tab': I='\t'
elif options.indent_type == 'space': I=' '
- outString = (I * ind) + '<' + element.nodeName
+ outParts.extend([(I * ind), '<', element.nodeName])
# always serialize the id or xml:id attributes first
if element.getAttribute('id') != '':
@@ -2083,17 +2731,17 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
quot = '"'
if id.find('"') != -1:
quot = "'"
- outString += ' ' + 'id=' + quot + id + quot
+ outParts.extend([' id=', quot, id, quot])
if element.getAttribute('xml:id') != '':
id = element.getAttribute('xml:id')
quot = '"'
if id.find('"') != -1:
quot = "'"
- outString += ' ' + 'xml:id=' + quot + id + quot
+ outParts.extend([' xml:id=', quot, id, quot])
# now serialize the other attributes
attrList = element.attributes
- for num in range(attrList.length) :
+ for num in xrange(attrList.length) :
attr = attrList.item(num)
if attr.nodeName == 'id' or attr.nodeName == 'xml:id': continue
# if the attribute value contains a double-quote, use single-quotes
@@ -2103,16 +2751,16 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
attrValue = makeWellFormed( attr.nodeValue )
- outString += ' '
+ outParts.append(' ')
# preserve xmlns: if it is a namespace prefix declaration
if attr.prefix != None:
- outString += attr.prefix + ':'
+ outParts.extend([attr.prefix, ':'])
elif attr.namespaceURI != None:
if attr.namespaceURI == 'http://www.w3.org/2000/xmlns/' and attr.nodeName.find('xmlns') == -1:
- outString += 'xmlns:'
+ outParts.append('xmlns:')
elif attr.namespaceURI == 'http://www.w3.org/1999/xlink':
- outString += 'xlink:'
- outString += attr.localName + '=' + quot + attrValue + quot
+ outParts.append('xlink:')
+ outParts.extend([attr.localName, '=', quot, attrValue, quot])
if attr.nodeName == 'xml:space':
if attrValue == 'preserve':
@@ -2123,43 +2771,43 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
# if no children, self-close
children = element.childNodes
if children.length > 0:
- outString += '>'
+ outParts.append('>')
onNewLine = False
for child in element.childNodes:
# element node
if child.nodeType == 1:
if preserveWhitespace:
- outString += serializeXML(child, options, 0, preserveWhitespace)
+ outParts.append(serializeXML(child, options, 0, preserveWhitespace))
else:
- outString += os.linesep + serializeXML(child, options, indent + 1, preserveWhitespace)
+ outParts.extend(['\n', serializeXML(child, options, indent + 1, preserveWhitespace)])
onNewLine = True
# text node
elif child.nodeType == 3:
# trim it only in the case of not being a child of an element
# where whitespace might be important
if preserveWhitespace:
- outString += makeWellFormed(child.nodeValue)
+ outParts.append(makeWellFormed(child.nodeValue))
else:
- outString += makeWellFormed(child.nodeValue.strip())
+ outParts.append(makeWellFormed(child.nodeValue.strip()))
# CDATA node
elif child.nodeType == 4:
- outString += '<![CDATA[' + child.nodeValue + ']]>'
+ outParts.extend(['<![CDATA[', child.nodeValue, ']]>'])
# Comment node
elif child.nodeType == 8:
- outString += '<!--' + child.nodeValue + '-->'
+ outParts.extend(['<!--', child.nodeValue, '-->'])
# TODO: entities, processing instructions, what else?
else: # ignore the rest
pass
- if onNewLine: outString += (I * ind)
- outString += '</' + element.nodeName + '>'
- if indent > 0: outString += os.linesep
+ if onNewLine: outParts.append(I * ind)
+ outParts.extend(['</', element.nodeName, '>'])
+ if indent > 0: outParts.append('\n')
else:
- outString += '/>'
- if indent > 0: outString += os.linesep
+ outParts.append('/>')
+ if indent > 0: outParts.append('\n')
- return outString
+ return "".join(outParts)
# this is the main method
# input is a string representation of the input XML
@@ -2172,6 +2820,10 @@ def scourString(in_string, options=None):
global numStylePropsFixed
global numElemsRemoved
global numBytesSavedInColors
+ global numCommentsRemoved
+ global numBytesSavedInIDs
+ global numBytesSavedInLengths
+ global numBytesSavedInTransforms
doc = xml.dom.minidom.parseString(in_string)
# for whatever reason this does not always remove all inkscape/sodipodi attributes/elements
@@ -2186,7 +2838,7 @@ def scourString(in_string, options=None):
# remove the xmlns: declarations now
xmlnsDeclsToRemove = []
attrList = doc.documentElement.attributes
- for num in range(attrList.length) :
+ for num in xrange(attrList.length) :
if attrList.item(num).nodeValue in unwanted_ns :
xmlnsDeclsToRemove.append(attrList.item(num).nodeName)
@@ -2204,7 +2856,7 @@ def scourString(in_string, options=None):
attrList = doc.documentElement.attributes
xmlnsDeclsToRemove = []
redundantPrefixes = []
- for i in range(attrList.length):
+ for i in xrange(attrList.length):
attr = attrList.item(i)
name = attr.nodeName
val = attr.nodeValue
@@ -2218,6 +2870,9 @@ def scourString(in_string, options=None):
for prefix in redundantPrefixes:
remapNamespacePrefix(doc.documentElement, prefix, '')
+ if options.strip_comments:
+ numCommentsRemoved = removeComments(doc)
+
# repair style (remove unnecessary style properties and change them into XML attributes)
numStylePropsFixed = repairStyle(doc.documentElement, options)
@@ -2225,15 +2880,25 @@ def scourString(in_string, options=None):
if options.simple_colors:
numBytesSavedInColors = convertColors(doc.documentElement)
+ # remove <metadata> if the user wants to
+ if options.remove_metadata:
+ removeMetadataElements(doc)
+
+ # remove unreferenced gradients/patterns outside of defs
+ # and most unreferenced elements inside of defs
+ while removeUnreferencedElements(doc) > 0:
+ pass
+
# remove empty defs, metadata, g
- # NOTE: these elements will be removed even if they have (invalid) text nodes
- elemsToRemove = []
+ # NOTE: these elements will be removed if they just have whitespace-only text nodes
for tag in ['defs', 'metadata', 'g'] :
for elem in doc.documentElement.getElementsByTagName(tag) :
removeElem = not elem.hasChildNodes()
if removeElem == False :
for child in elem.childNodes :
- if child.nodeType in [1, 3, 4, 8] :
+ if child.nodeType in [1, 4, 8]:
+ break
+ elif child.nodeType == 3 and not child.nodeValue.isspace():
break
else:
removeElem = True
@@ -2241,20 +2906,12 @@ def scourString(in_string, options=None):
elem.parentNode.removeChild(elem)
numElemsRemoved += 1
- # remove unreferenced gradients/patterns outside of defs
- while removeUnreferencedElements(doc) > 0:
- pass
-
if options.strip_ids:
bContinueLooping = True
while bContinueLooping:
- identifiedElements = findElementsWithId(doc.documentElement)
+ identifiedElements = unprotected_ids(doc, options)
referencedIDs = findReferencedElements(doc.documentElement)
bContinueLooping = (removeUnreferencedIDs(referencedIDs, identifiedElements) > 0)
-
- if options.group_collapse:
- while removeNestedGroups(doc.documentElement) > 0:
- pass
while removeDuplicateGradientStops(doc) > 0:
pass
@@ -2267,38 +2924,64 @@ def scourString(in_string, options=None):
while removeDuplicateGradients(doc) > 0:
pass
+ # create <g> elements if there are runs of elements with the same attributes.
+ # this MUST be before moveCommonAttributesToParentGroup.
+ if options.group_create:
+ createGroupsForCommonAttributes(doc.documentElement)
+
# move common attributes to parent group
- numAttrsRemoved += moveCommonAttributesToParentGroup(doc.documentElement)
+ # NOTE: the if the <svg> element's immediate children
+ # all have the same value for an attribute, it must not
+ # get moved to the <svg> element. The <svg> element
+ # doesn't accept fill=, stroke= etc.!
+ referencedIds = findReferencedElements(doc.documentElement)
+ for child in doc.documentElement.childNodes:
+ numAttrsRemoved += moveCommonAttributesToParentGroup(child, referencedIds)
# remove unused attributes from parent
numAttrsRemoved += removeUnusedAttributesOnParent(doc.documentElement)
-
- # clean path data
- for elem in doc.documentElement.getElementsByTagName('path') :
- if elem.getAttribute('d') == '':
- elem.parentNode.removeChild(elem)
- else:
- cleanPath(elem)
+
+ # Collapse groups LAST, because we've created groups. If done before
+ # moveAttributesToParentGroup, empty <g>'s may remain.
+ if options.group_collapse:
+ while removeNestedGroups(doc.documentElement) > 0:
+ pass
# remove unnecessary closing point of polygons and scour points
for polygon in doc.documentElement.getElementsByTagName('polygon') :
- cleanPolygon(polygon)
+ cleanPolygon(polygon, options)
# scour points of polyline
for polyline in doc.documentElement.getElementsByTagName('polyline') :
- cleanPolygon(polyline)
+ cleanPolyline(polyline, options)
+
+ # clean path data
+ for elem in doc.documentElement.getElementsByTagName('path') :
+ if elem.getAttribute('d') == '':
+ elem.parentNode.removeChild(elem)
+ else:
+ cleanPath(elem, options)
+ # shorten ID names as much as possible
+ if options.shorten_ids:
+ numBytesSavedInIDs += shortenIDs(doc, unprotected_ids(doc, options))
+
# scour lengths (including coordinates)
- for type in ['svg', 'image', 'rect', 'circle', 'ellipse', 'line', 'linearGradient', 'radialGradient', 'stop']:
+ for type in ['svg', 'image', 'rect', 'circle', 'ellipse', 'line', 'linearGradient', 'radialGradient', 'stop', 'filter']:
for elem in doc.getElementsByTagName(type):
for attr in ['x', 'y', 'width', 'height', 'cx', 'cy', 'r', 'rx', 'ry',
- 'x1', 'y1', 'x2', 'y2', 'fx', 'fy', 'offset', 'opacity',
- 'fill-opacity', 'stroke-opacity', 'stroke-width', 'stroke-miterlimit']:
+ 'x1', 'y1', 'x2', 'y2', 'fx', 'fy', 'offset']:
if elem.getAttribute(attr) != '':
- elem.setAttribute(attr, scourLength(elem.getAttribute(attr)))
-
+ elem.setAttribute(attr, scourLength(elem.getAttribute(attr)))
+
+ # more length scouring in this function
+ numBytesSavedInLengths = reducePrecision(doc.documentElement)
+
# remove default values of attributes
- numAttrsRemoved += removeDefaultAttributeValues(doc.documentElement, options)
+ numAttrsRemoved += removeDefaultAttributeValues(doc.documentElement, options)
+
+ # reduce the length of transformation attributes
+ numBytesSavedInTransforms = optimizeTransforms(doc.documentElement, options)
# convert rasters references to base64-encoded strings
if options.embed_rasters:
@@ -2307,14 +2990,14 @@ def scourString(in_string, options=None):
# properly size the SVG document (ideally width/height should be 100% with a viewBox)
if options.enable_viewboxing:
- properlySizeDoc(doc.documentElement)
+ properlySizeDoc(doc.documentElement, options)
# output the document as a pretty string with a single space for indent
# NOTE: removed pretty printing because of this problem:
# http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/
# rolled our own serialize function here to save on space, put id first, customize indentation, etc
# out_string = doc.documentElement.toprettyxml(' ')
- out_string = serializeXML(doc.documentElement, options) + os.linesep
+ out_string = serializeXML(doc.documentElement, options) + '\n'
# now strip out empty lines
lines = []
@@ -2325,7 +3008,7 @@ def scourString(in_string, options=None):
# return the string with its XML prolog and surrounding comments
if options.strip_xml_prolog == False:
- total_output = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' + os.linesep
+ total_output = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n'
else:
total_output = ""
@@ -2333,7 +3016,7 @@ def scourString(in_string, options=None):
if child.nodeType == 1:
total_output += "".join(lines)
else: # doctypes, entities, comments
- total_output += child.toxml() + os.linesep
+ total_output += child.toxml() + '\n'
return total_output
@@ -2375,15 +3058,33 @@ _options_parser.add_option("--disable-style-to-xml",
_options_parser.add_option("--disable-group-collapsing",
action="store_false", dest="group_collapse", default=True,
help="won't collapse <g> elements")
+_options_parser.add_option("--create-groups",
+ action="store_true", dest="group_create", default=False,
+ help="create <g> elements for runs of elements with identical attributes")
_options_parser.add_option("--enable-id-stripping",
action="store_true", dest="strip_ids", default=False,
help="remove all un-referenced ID attributes")
+_options_parser.add_option("--enable-comment-stripping",
+ action="store_true", dest="strip_comments", default=False,
+ help="remove all <!-- --> comments")
+_options_parser.add_option("--shorten-ids",
+ action="store_true", dest="shorten_ids", default=False,
+ help="shorten all ID attributes to the least number of letters possible")
_options_parser.add_option("--disable-embed-rasters",
action="store_false", dest="embed_rasters", default=True,
help="won't embed rasters as base64-encoded data")
_options_parser.add_option("--keep-editor-data",
action="store_true", dest="keep_editor_data", default=False,
help="won't remove Inkscape, Sodipodi or Adobe Illustrator elements and attributes")
+_options_parser.add_option("--remove-metadata",
+ action="store_true", dest="remove_metadata", default=False,
+ help="remove <metadata> elements (which may contain license metadata etc.)")
+_options_parser.add_option("--renderer-workaround",
+ action="store_true", dest="renderer_workaround", default=True,
+ help="work around various renderer bugs (currently only librsvg) (default)")
+_options_parser.add_option("--no-renderer-workaround",
+ action="store_false", dest="renderer_workaround", default=True,
+ help="do not work around various renderer bugs (currently only librsvg)")
_options_parser.add_option("--strip-xml-prolog",
action="store_true", dest="strip_xml_prolog", default=False,
help="won't output the <?xml ?> prolog")
@@ -2400,12 +3101,25 @@ _options_parser.add_option("-i",
action="store", dest="infilename", help=optparse.SUPPRESS_HELP)
_options_parser.add_option("-o",
action="store", dest="outfilename", help=optparse.SUPPRESS_HELP)
+_options_parser.add_option("-q", "--quiet",
+ action="store_true", dest="quiet", default=False,
+ help="suppress non-error output")
_options_parser.add_option("--indent",
action="store", type="string", dest="indent_type", default="space",
help="indentation of the output: none, space, tab (default: %default)")
+_options_parser.add_option("--protect-ids-noninkscape",
+ action="store_true", dest="protect_ids_noninkscape", default=False,
+ help="Don't change IDs not ending with a digit")
+_options_parser.add_option("--protect-ids-list",
+ action="store", type="string", dest="protect_ids_list", default=None,
+ help="Don't change IDs given in a comma-separated list")
+_options_parser.add_option("--protect-ids-prefix",
+ action="store", type="string", dest="protect_ids_prefix", default=None,
+ help="Don't change IDs starting with the given prefix")
def maybe_gziped_file(filename, mode="r"):
if os.path.splitext(filename)[1].lower() in (".svgz", ".gz"):
+ import gzip
return gzip.GzipFile(filename, mode)
return file(filename, mode)
@@ -2428,7 +3142,7 @@ def parse_args(args=None):
# GZ: could sniff for gzip compression here
infile = sys.stdin
if options.outfilename:
- outfile = maybe_gziped_file(options.outfilename, "w")
+ outfile = maybe_gziped_file(options.outfilename, "wb")
else:
outfile = sys.stdout
@@ -2443,7 +3157,11 @@ def getReport():
' Number of path segments reduced/removed: ' + str(numPathSegmentsReduced) + os.linesep + \
' Number of bytes saved in path data: ' + str(numBytesSavedInPathData) + os.linesep + \
' Number of bytes saved in colors: ' + str(numBytesSavedInColors) + os.linesep + \
- ' Number of points removed from polygons: ' + str(numPointsRemovedFromPolygon)
+ ' Number of points removed from polygons: ' + str(numPointsRemovedFromPolygon) + os.linesep + \
+ ' Number of bytes saved in comments: ' + str(numCommentBytes) + os.linesep + \
+ ' Number of bytes saved in id attributes: ' + str(numBytesSavedInIDs) + os.linesep + \
+ ' Number of bytes saved in lengths: ' + str(numBytesSavedInLengths) + os.linesep + \
+ ' Number of bytes saved in transformations: ' + str(numBytesSavedInTransforms)
if __name__ == '__main__':
if sys.platform == "win32":
@@ -2457,7 +3175,8 @@ if __name__ == '__main__':
options, (input, output) = parse_args()
- print >>sys.stderr, "%s %s\n%s" % (APP, VER, COPYRIGHT)
+ if not options.quiet:
+ print >>sys.stderr, "%s %s\n%s" % (APP, VER, COPYRIGHT)
# do the work
in_string = input.read()
@@ -2470,14 +3189,14 @@ if __name__ == '__main__':
end = get_tick()
- # GZ: unless silenced by -q or something?
# GZ: not using globals would be good too
- print >>sys.stderr, ' File:', input.name, \
- os.linesep + ' Time taken:', str(end-start) + 's' + os.linesep, \
- getReport()
-
- oldsize = len(in_string)
- newsize = len(out_string)
- sizediff = (newsize / oldsize) * 100
- print >>sys.stderr, ' Original file size:', oldsize, 'bytes;', \
- 'new file size:', newsize, 'bytes (' + str(sizediff)[:5] + '%)'
+ if not options.quiet:
+ print >>sys.stderr, ' File:', input.name, \
+ os.linesep + ' Time taken:', str(end-start) + 's' + os.linesep, \
+ getReport()
+
+ oldsize = len(in_string)
+ newsize = len(out_string)
+ sizediff = (newsize / oldsize) * 100
+ print >>sys.stderr, ' Original file size:', oldsize, 'bytes;', \
+ 'new file size:', newsize, 'bytes (' + str(sizediff)[:5] + '%)'
diff --git a/share/extensions/svg_regex.py b/share/extensions/svg_regex.py
index 3d0a21520..00b615873 100644
--- a/share/extensions/svg_regex.py
+++ b/share/extensions/svg_regex.py
@@ -1,4 +1,3 @@
-#!/usr/bin/env python
# This software is OSI Certified Open Source Software.
# OSI Certified is a certification mark of the Open Source Initiative.
#
@@ -44,6 +43,7 @@ Out[5]: [('M', [(100.0, -200.0)])]
"""
import re
+from decimal import *
# Sentinel.
@@ -53,8 +53,8 @@ class _EOF(object):
EOF = _EOF()
lexicon = [
- ('float', r'[-\+]?(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+\.?))(?:[Ee][-\+]?[0-9]+)?'),
- ('int', r'[-\+]?[0-9]+'),
+ ('float', r'[-+]?(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+\.?))(?:[Ee][-+]?[0-9]+)?'),
+ ('int', r'[-+]?[0-9]+'),
('command', r'[AaCcHhLlMmQqSsTtVvZz]'),
]
@@ -162,7 +162,7 @@ class SVGPathParser(object):
def rule_closepath(self, next, token):
command = token[1]
token = next()
- return (command, None), token
+ return (command, []), token
def rule_moveto_or_lineto(self, next, token):
command = token[1]
@@ -170,7 +170,7 @@ class SVGPathParser(object):
coordinates = []
while token[0] in self.number_tokens:
pair, token = self.rule_coordinate_pair(next, token)
- coordinates.append(pair)
+ coordinates.extend(pair)
return (command, coordinates), token
def rule_orthogonal_lineto(self, next, token):
@@ -190,7 +190,9 @@ class SVGPathParser(object):
pair1, token = self.rule_coordinate_pair(next, token)
pair2, token = self.rule_coordinate_pair(next, token)
pair3, token = self.rule_coordinate_pair(next, token)
- coordinates.append((pair1, pair2, pair3))
+ coordinates.extend(pair1)
+ coordinates.extend(pair2)
+ coordinates.extend(pair3)
return (command, coordinates), token
def rule_curveto2(self, next, token):
@@ -200,7 +202,8 @@ class SVGPathParser(object):
while token[0] in self.number_tokens:
pair1, token = self.rule_coordinate_pair(next, token)
pair2, token = self.rule_coordinate_pair(next, token)
- coordinates.append((pair1, pair2))
+ coordinates.extend(pair1)
+ coordinates.extend(pair2)
return (command, coordinates), token
def rule_curveto1(self, next, token):
@@ -209,7 +212,7 @@ class SVGPathParser(object):
coordinates = []
while token[0] in self.number_tokens:
pair1, token = self.rule_coordinate_pair(next, token)
- coordinates.append(pair1)
+ coordinates.extend(pair1)
return (command, coordinates), token
def rule_elliptical_arc(self, next, token):
@@ -217,51 +220,51 @@ class SVGPathParser(object):
token = next()
arguments = []
while token[0] in self.number_tokens:
- rx = float(token[1])
- if rx < 0.0:
+ rx = Decimal(token[1]) * 1
+ if rx < Decimal("0.0"):
raise SyntaxError("expecting a nonnegative number; got %r" % (token,))
token = next()
if token[0] not in self.number_tokens:
raise SyntaxError("expecting a number; got %r" % (token,))
- ry = float(token[1])
- if ry < 0.0:
+ ry = Decimal(token[1]) * 1
+ if ry < Decimal("0.0"):
raise SyntaxError("expecting a nonnegative number; got %r" % (token,))
token = next()
if token[0] not in self.number_tokens:
raise SyntaxError("expecting a number; got %r" % (token,))
- axis_rotation = float(token[1])
+ axis_rotation = Decimal(token[1]) * 1
token = next()
if token[1] not in ('0', '1'):
raise SyntaxError("expecting a boolean flag; got %r" % (token,))
- large_arc_flag = bool(int(token[1]))
+ large_arc_flag = Decimal(token[1]) * 1
token = next()
if token[1] not in ('0', '1'):
raise SyntaxError("expecting a boolean flag; got %r" % (token,))
- sweep_flag = bool(int(token[1]))
+ sweep_flag = Decimal(token[1]) * 1
token = next()
if token[0] not in self.number_tokens:
raise SyntaxError("expecting a number; got %r" % (token,))
- x = float(token[1])
+ x = Decimal(token[1]) * 1
token = next()
if token[0] not in self.number_tokens:
raise SyntaxError("expecting a number; got %r" % (token,))
- y = float(token[1])
+ y = Decimal(token[1]) * 1
token = next()
- arguments.append(((rx,ry), axis_rotation, large_arc_flag, sweep_flag, (x,y)))
+ arguments.extend([rx, ry, axis_rotation, large_arc_flag, sweep_flag, x, y])
return (command, arguments), token
def rule_coordinate(self, next, token):
if token[0] not in self.number_tokens:
raise SyntaxError("expecting a number; got %r" % (token,))
- x = float(token[1])
+ x = getcontext().create_decimal(token[1])
token = next()
return x, token
@@ -270,13 +273,13 @@ class SVGPathParser(object):
# Inline these since this rule is so common.
if token[0] not in self.number_tokens:
raise SyntaxError("expecting a number; got %r" % (token,))
- x = float(token[1])
+ x = getcontext().create_decimal(token[1])
token = next()
if token[0] not in self.number_tokens:
raise SyntaxError("expecting a number; got %r" % (token,))
- y = float(token[1])
+ y = getcontext().create_decimal(token[1])
token = next()
- return (x,y), token
+ return [x, y], token
svg_parser = SVGPathParser()
diff --git a/share/extensions/svg_transform.py b/share/extensions/svg_transform.py
new file mode 100644
index 000000000..07b523cc8
--- /dev/null
+++ b/share/extensions/svg_transform.py
@@ -0,0 +1,233 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# SVG transformation list parser
+#
+# Copyright 2010 Louis Simard
+#
+# This file is part of Scour, http://www.codedread.com/scour/
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+""" Small recursive descent parser for SVG transform="" data.
+
+
+In [1]: from svg_transform import svg_transform_parser
+
+In [3]: svg_transform_parser.parse('translate(50, 50)')
+Out[3]: [('translate', [50.0, 50.0])]
+
+In [4]: svg_transform_parser.parse('translate(50)')
+Out[4]: [('translate', [50.0])]
+
+In [5]: svg_transform_parser.parse('rotate(36 50,50)')
+Out[5]: [('rotate', [36.0, 50.0, 50.0])]
+
+In [6]: svg_transform_parser.parse('rotate(36)')
+Out[6]: [('rotate', [36.0])]
+
+In [7]: svg_transform_parser.parse('skewX(20)')
+Out[7]: [('skewX', [20.0])]
+
+In [8]: svg_transform_parser.parse('skewY(40)')
+Out[8]: [('skewX', [20.0])]
+
+In [9]: svg_transform_parser.parse('scale(2 .5)')
+Out[9]: [('scale', [2.0, 0.5])]
+
+In [10]: svg_transform_parser.parse('scale(.5)')
+Out[10]: [('scale', [0.5])]
+
+In [11]: svg_transform_parser.parse('matrix(1 0 50 0 1 80)')
+Out[11]: [('matrix', [1.0, 0.0, 50.0, 0.0, 1.0, 80.0])]
+
+Multiple transformations are supported:
+
+In [12]: svg_transform_parser.parse('translate(30 -30) rotate(36)')
+Out[12]: [('translate', [30.0, -30.0]), ('rotate', [36.0])]
+"""
+
+import re
+from decimal import *
+
+
+# Sentinel.
+class _EOF(object):
+ def __repr__(self):
+ return 'EOF'
+EOF = _EOF()
+
+lexicon = [
+ ('float', r'[-+]?(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+\.?))(?:[Ee][-+]?[0-9]+)?'),
+ ('int', r'[-+]?[0-9]+'),
+ ('command', r'(?:matrix|translate|scale|rotate|skew[XY])'),
+ ('coordstart', r'\('),
+ ('coordend', r'\)'),
+]
+
+
+class Lexer(object):
+ """ Break SVG path data into tokens.
+
+ The SVG spec requires that tokens are greedy. This lexer relies on Python's
+ regexes defaulting to greediness.
+
+ This style of implementation was inspired by this article:
+
+ http://www.gooli.org/blog/a-simple-lexer-in-python/
+ """
+ def __init__(self, lexicon):
+ self.lexicon = lexicon
+ parts = []
+ for name, regex in lexicon:
+ parts.append('(?P<%s>%s)' % (name, regex))
+ self.regex_string = '|'.join(parts)
+ self.regex = re.compile(self.regex_string)
+
+ def lex(self, text):
+ """ Yield (token_type, str_data) tokens.
+
+ The last token will be (EOF, None) where EOF is the singleton object
+ defined in this module.
+ """
+ for match in self.regex.finditer(text):
+ for name, _ in self.lexicon:
+ m = match.group(name)
+ if m is not None:
+ yield (name, m)
+ break
+ yield (EOF, None)
+
+svg_lexer = Lexer(lexicon)
+
+
+class SVGTransformationParser(object):
+ """ Parse SVG transform="" data into a list of commands.
+
+ Each distinct command will take the form of a tuple (type, data). The
+ `type` is the character string that defines the type of transformation in the
+ transform data, so either of "translate", "rotate", "scale", "matrix",
+ "skewX" and "skewY". Data is always a list of numbers contained within the
+ transformation's parentheses.
+
+ See the SVG documentation for the interpretation of the individual elements
+ for each transformation.
+
+ The main method is `parse(text)`. It can only consume actual strings, not
+ filelike objects or iterators.
+ """
+
+ def __init__(self, lexer=svg_lexer):
+ self.lexer = lexer
+
+ self.command_dispatch = {
+ 'translate': self.rule_1or2numbers,
+ 'scale': self.rule_1or2numbers,
+ 'skewX': self.rule_1number,
+ 'skewY': self.rule_1number,
+ 'rotate': self.rule_1or3numbers,
+ 'matrix': self.rule_6numbers,
+ }
+
+# self.number_tokens = set(['int', 'float'])
+ self.number_tokens = list(['int', 'float'])
+
+ def parse(self, text):
+ """ Parse a string of SVG transform="" data.
+ """
+ next = self.lexer.lex(text).next
+ commands = []
+ token = next()
+ while token[0] is not EOF:
+ command, token = self.rule_svg_transform(next, token)
+ commands.append(command)
+ return commands
+
+ def rule_svg_transform(self, next, token):
+ if token[0] != 'command':
+ raise SyntaxError("expecting a transformation type; got %r" % (token,))
+ command = token[1]
+ rule = self.command_dispatch[command]
+ token = next()
+ if token[0] != 'coordstart':
+ raise SyntaxError("expecting '('; got %r" % (token,))
+ numbers, token = rule(next, token)
+ if token[0] != 'coordend':
+ raise SyntaxError("expecting ')'; got %r" % (token,))
+ token = next()
+ return (command, numbers), token
+
+ def rule_1or2numbers(self, next, token):
+ numbers = []
+ # 1st number is mandatory
+ token = next()
+ number, token = self.rule_number(next, token)
+ numbers.append(number)
+ # 2nd number is optional
+ number, token = self.rule_optional_number(next, token)
+ if number is not None:
+ numbers.append(number)
+
+ return numbers, token
+
+ def rule_1number(self, next, token):
+ # this number is mandatory
+ token = next()
+ number, token = self.rule_number(next, token)
+ numbers = [number]
+ return numbers, token
+
+ def rule_1or3numbers(self, next, token):
+ numbers = []
+ # 1st number is mandatory
+ token = next()
+ number, token = self.rule_number(next, token)
+ numbers.append(number)
+ # 2nd number is optional
+ number, token = self.rule_optional_number(next, token)
+ if number is not None:
+ # but, if the 2nd number is provided, the 3rd is mandatory.
+ # we can't have just 2.
+ numbers.append(number)
+
+ number, token = self.rule_number(next, token)
+ numbers.append(number)
+
+ return numbers, token
+
+ def rule_6numbers(self, next, token):
+ numbers = []
+ token = next()
+ # all numbers are mandatory
+ for i in xrange(6):
+ number, token = self.rule_number(next, token)
+ numbers.append(number)
+ return numbers, token
+
+ def rule_number(self, next, token):
+ if token[0] not in self.number_tokens:
+ raise SyntaxError("expecting a number; got %r" % (token,))
+ x = Decimal(token[1]) * 1
+ token = next()
+ return x, token
+
+ def rule_optional_number(self, next, token):
+ if token[0] not in self.number_tokens:
+ return None, token
+ else:
+ x = Decimal(token[1]) * 1
+ token = next()
+ return x, token
+
+
+svg_transform_parser = SVGTransformationParser()