scour.py revision dae6f8bc7f695949560f0ae37c783a9cd91f1b50
# -*- coding: utf-8 -*-
# Scour
#
# Copyright 2009 Jeff Schiller
#
# 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
#
#
# 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.
# Notes:
# rubys' path-crunching ideas here: http://intertwingly.net/code/svgtidy/spec.rb
# (and implemented here: http://intertwingly.net/code/svgtidy/svgtidy.rb )
# Yet more ideas here: http://wiki.inkscape.org/wiki/index.php/Save_Cleaned_SVG
#
# * Process Transformations
# * Collapse all group based transformations
# Even more ideas here: http://esw.w3.org/topic/SvgTidy
# * analysis of path elements to see if rect can be used instead? (must also need to look
# at rounded corners)
# Next Up:
# + remove unused attributes in parent elements
# + prevent elements from being stripped if they are referenced in a <style> element
# (for instance, filter, marker, pattern) - need a crude CSS parser
# - 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
import os
import sys
import re
import math
import base64
import urllib
from svg_regex import svg_parser
import gzip
import optparse
from yocto_css import parseCssString
# Python 2.3- did not have Decimal
try:
from decimal import *
except ImportError:
from fixedpoint import *
APP = 'scour'
VER = '0.20'
COPYRIGHT = 'Copyright Jeff Schiller, 2009'
'XLINK': 'http://www.w3.org/1999/xlink',
'SODIPODI': 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
'INKSCAPE': 'http://www.inkscape.org/namespaces/inkscape',
'ADOBE_ILLUSTRATOR': 'http://ns.adobe.com/AdobeIllustrator/10.0/',
'ADOBE_GRAPHS': 'http://ns.adobe.com/Graphs/1.0/',
'ADOBE_SVG_VIEWER': 'http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/',
'ADOBE_VARIABLES': 'http://ns.adobe.com/Variables/1.0/',
'ADOBE_SFW': 'http://ns.adobe.com/SaveForWeb/1.0/',
'ADOBE_EXTENSIBILITY': 'http://ns.adobe.com/Extensibility/1.0/',
'ADOBE_FLOWS': 'http://ns.adobe.com/Flows/1.0/',
'ADOBE_IMAGE_REPLACEMENT': 'http://ns.adobe.com/ImageReplacement/1.0/',
'ADOBE_CUSTOM': 'http://ns.adobe.com/GenericCustomNamespace/1.0/',
'ADOBE_XPATH': 'http://ns.adobe.com/XPath/1.0/'
}
svgAttributes = [
'clip-rule',
'display',
'fill',
'fill-opacity',
'fill-rule',
'filter',
'font-family',
'font-size',
'font-stretch',
'font-style',
'font-variant',
'font-weight',
'line-height',
'marker',
'opacity',
'overflow',
'stop-color',
'stop-opacity',
'stroke',
'stroke-dashoffset',
'stroke-linecap',
'stroke-linejoin',
'stroke-miterlimit',
'stroke-opacity',
'stroke-width',
'visibility'
]
colors = {
'aliceblue': 'rgb(240, 248, 255)',
'antiquewhite': 'rgb(250, 235, 215)',
'aqua': 'rgb( 0, 255, 255)',
'aquamarine': 'rgb(127, 255, 212)',
'azure': 'rgb(240, 255, 255)',
'beige': 'rgb(245, 245, 220)',
'bisque': 'rgb(255, 228, 196)',
'black': 'rgb( 0, 0, 0)',
'blanchedalmond': 'rgb(255, 235, 205)',
'blue': 'rgb( 0, 0, 255)',
'blueviolet': 'rgb(138, 43, 226)',
'brown': 'rgb(165, 42, 42)',
'burlywood': 'rgb(222, 184, 135)',
'cadetblue': 'rgb( 95, 158, 160)',
'chartreuse': 'rgb(127, 255, 0)',
'chocolate': 'rgb(210, 105, 30)',
'coral': 'rgb(255, 127, 80)',
'cornflowerblue': 'rgb(100, 149, 237)',
'cornsilk': 'rgb(255, 248, 220)',
'crimson': 'rgb(220, 20, 60)',
'cyan': 'rgb( 0, 255, 255)',
'darkblue': 'rgb( 0, 0, 139)',
'darkcyan': 'rgb( 0, 139, 139)',
'darkgoldenrod': 'rgb(184, 134, 11)',
'darkgray': 'rgb(169, 169, 169)',
'darkgreen': 'rgb( 0, 100, 0)',
'darkgrey': 'rgb(169, 169, 169)',
'darkkhaki': 'rgb(189, 183, 107)',
'darkmagenta': 'rgb(139, 0, 139)',
'darkolivegreen': 'rgb( 85, 107, 47)',
'darkorange': 'rgb(255, 140, 0)',
'darkorchid': 'rgb(153, 50, 204)',
'darkred': 'rgb(139, 0, 0)',
'darksalmon': 'rgb(233, 150, 122)',
'darkseagreen': 'rgb(143, 188, 143)',
'darkslateblue': 'rgb( 72, 61, 139)',
'darkslategray': 'rgb( 47, 79, 79)',
'darkslategrey': 'rgb( 47, 79, 79)',
'darkturquoise': 'rgb( 0, 206, 209)',
'darkviolet': 'rgb(148, 0, 211)',
'deeppink': 'rgb(255, 20, 147)',
'deepskyblue': 'rgb( 0, 191, 255)',
'dimgray': 'rgb(105, 105, 105)',
'dimgrey': 'rgb(105, 105, 105)',
'dodgerblue': 'rgb( 30, 144, 255)',
'firebrick': 'rgb(178, 34, 34)',
'floralwhite': 'rgb(255, 250, 240)',
'forestgreen': 'rgb( 34, 139, 34)',
'fuchsia': 'rgb(255, 0, 255)',
'gainsboro': 'rgb(220, 220, 220)',
'ghostwhite': 'rgb(248, 248, 255)',
'gold': 'rgb(255, 215, 0)',
'goldenrod': 'rgb(218, 165, 32)',
'gray': 'rgb(128, 128, 128)',
'grey': 'rgb(128, 128, 128)',
'green': 'rgb( 0, 128, 0)',
'greenyellow': 'rgb(173, 255, 47)',
'honeydew': 'rgb(240, 255, 240)',
'hotpink': 'rgb(255, 105, 180)',
'indianred': 'rgb(205, 92, 92)',
'indigo': 'rgb( 75, 0, 130)',
'ivory': 'rgb(255, 255, 240)',
'khaki': 'rgb(240, 230, 140)',
'lavender': 'rgb(230, 230, 250)',
'lavenderblush': 'rgb(255, 240, 245)',
'lawngreen': 'rgb(124, 252, 0)',
'lemonchiffon': 'rgb(255, 250, 205)',
'lightblue': 'rgb(173, 216, 230)',
'lightcoral': 'rgb(240, 128, 128)',
'lightcyan': 'rgb(224, 255, 255)',
'lightgoldenrodyellow': 'rgb(250, 250, 210)',
'lightgray': 'rgb(211, 211, 211)',
'lightgreen': 'rgb(144, 238, 144)',
'lightgrey': 'rgb(211, 211, 211)',
'lightpink': 'rgb(255, 182, 193)',
'lightsalmon': 'rgb(255, 160, 122)',
'lightseagreen': 'rgb( 32, 178, 170)',
'lightskyblue': 'rgb(135, 206, 250)',
'lightslategray': 'rgb(119, 136, 153)',
'lightslategrey': 'rgb(119, 136, 153)',
'lightsteelblue': 'rgb(176, 196, 222)',
'lightyellow': 'rgb(255, 255, 224)',
'lime': 'rgb( 0, 255, 0)',
'limegreen': 'rgb( 50, 205, 50)',
'linen': 'rgb(250, 240, 230)',
'magenta': 'rgb(255, 0, 255)',
'maroon': 'rgb(128, 0, 0)',
'mediumaquamarine': 'rgb(102, 205, 170)',
'mediumblue': 'rgb( 0, 0, 205)',
'mediumorchid': 'rgb(186, 85, 211)',
'mediumpurple': 'rgb(147, 112, 219)',
'mediumseagreen': 'rgb( 60, 179, 113)',
'mediumslateblue': 'rgb(123, 104, 238)',
'mediumspringgreen': 'rgb( 0, 250, 154)',
'mediumturquoise': 'rgb( 72, 209, 204)',
'mediumvioletred': 'rgb(199, 21, 133)',
'midnightblue': 'rgb( 25, 25, 112)',
'mintcream': 'rgb(245, 255, 250)',
'mistyrose': 'rgb(255, 228, 225)',
'moccasin': 'rgb(255, 228, 181)',
'navajowhite': 'rgb(255, 222, 173)',
'navy': 'rgb( 0, 0, 128)',
'oldlace': 'rgb(253, 245, 230)',
'olive': 'rgb(128, 128, 0)',
'olivedrab': 'rgb(107, 142, 35)',
'orange': 'rgb(255, 165, 0)',
'orangered': 'rgb(255, 69, 0)',
'orchid': 'rgb(218, 112, 214)',
'palegoldenrod': 'rgb(238, 232, 170)',
'palegreen': 'rgb(152, 251, 152)',
'paleturquoise': 'rgb(175, 238, 238)',
'palevioletred': 'rgb(219, 112, 147)',
'papayawhip': 'rgb(255, 239, 213)',
'peachpuff': 'rgb(255, 218, 185)',
'peru': 'rgb(205, 133, 63)',
'pink': 'rgb(255, 192, 203)',
'plum': 'rgb(221, 160, 221)',
'powderblue': 'rgb(176, 224, 230)',
'purple': 'rgb(128, 0, 128)',
'red': 'rgb(255, 0, 0)',
'rosybrown': 'rgb(188, 143, 143)',
'royalblue': 'rgb( 65, 105, 225)',
'saddlebrown': 'rgb(139, 69, 19)',
'salmon': 'rgb(250, 128, 114)',
'sandybrown': 'rgb(244, 164, 96)',
'seagreen': 'rgb( 46, 139, 87)',
'seashell': 'rgb(255, 245, 238)',
'sienna': 'rgb(160, 82, 45)',
'silver': 'rgb(192, 192, 192)',
'skyblue': 'rgb(135, 206, 235)',
'slateblue': 'rgb(106, 90, 205)',
'slategray': 'rgb(112, 128, 144)',
'slategrey': 'rgb(112, 128, 144)',
'snow': 'rgb(255, 250, 250)',
'springgreen': 'rgb( 0, 255, 127)',
'steelblue': 'rgb( 70, 130, 180)',
'tan': 'rgb(210, 180, 140)',
'teal': 'rgb( 0, 128, 128)',
'thistle': 'rgb(216, 191, 216)',
'tomato': 'rgb(255, 99, 71)',
'turquoise': 'rgb( 64, 224, 208)',
'violet': 'rgb(238, 130, 238)',
'wheat': 'rgb(245, 222, 179)',
'white': 'rgb(255, 255, 255)',
'whitesmoke': 'rgb(245, 245, 245)',
'yellow': 'rgb(255, 255, 0)',
'yellowgreen': 'rgb(154, 205, 50)',
}
INVALID = -1
NONE = 0
PCT = 1
PX = 2
PT = 3
PC = 4
EM = 5
EX = 6
CM = 7
MM = 8
IN = 9
# @staticmethod
# GZ: shadowing builtins like 'str' is generally bad form
# GZ: encoding stuff like this in a dict makes for nicer code
# @staticmethod
def str(u):
return 'INVALID'
try: # simple unitless and no scientific notation
except ValueError:
# we know that the length string has an exponent, a unit, both or is invalid
# parse out number, exponent and unit
unitBegin = 0
if scinum != None:
# this will always match, no need to check it
else:
# unit or invalid
if numMatch != None:
if unitBegin != 0 :
if unitMatch != None :
# invalid
else:
# TODO: this needs to set the default for the given attribute (how?)
# returns the length of a property
# TODO: eventually use the above class once it is complete
def getSVGLength(value):
try:
except ValueError:
if coordMatch != None:
v = value
return v
if e != None: return e
return None
"""
Returns all elements with id attributes
"""
if elems is None:
elems = {}
if id != '' :
if node.hasChildNodes() :
# we are only really interested in nodes of type Element (1)
return elems
'marker-end', 'marker-mid']
"""
Returns the number of times an ID is referenced as well as all elements
that reference it.
Currently looks at fill, stroke, clip-path, mask, marker, and
xlink:href attributes.
"""
global referencingProps
if ids is None:
ids = {}
# TODO: input argument ids is clunky here (see below how it is called)
# GZ: alternative to passing dict, use **kwargs
# if this node is a style element, parse its text into CSS
# node.firstChild will be either a CDATA or a Text node
return ids
# else if xlink:href is set, then grab the id
# we remove the hash mark from the beginning of the id
else:
# now get all style properties and the fill, stroke, filter attributes
for attr in referencingProps:
if node.hasChildNodes() :
return ids
global referencingProps
else:
# if the url has a quote in it, we need to compensate
id = None
# double-quote
# single-quote
if id != None:
else:
numIDsRemoved = 0
numElemsRemoved = 0
numAttrsRemoved = 0
if elemsToRemove is None:
elemsToRemove = []
continue
return elemsToRemove
"""
Removes all unreferenced elements except for <svg>, <font>, <metadata>, <title>, and <desc>.
Also vacuums the defs of any non-referenced renderable elements.
Returns the number of unreferenced elements removed from the document.
"""
global numElemsRemoved
num = 0
for id in identifiedElements:
if not id in referencedIDs:
num += 1
numElemsRemoved += 1
# TODO: should also go through defs and vacuum it
num = 0
for elem in elemsToRemove:
numElemsRemoved += 1
num += 1
return num
"""
Removes the unreferenced ID attributes.
Returns the number of ID attributes removed
"""
global numIDsRemoved
keepTags = ['font']
num = 0;
numIDsRemoved += 1
num += 1
return num
global numAttrsRemoved
num = 0
# remove all namespace'd attributes from this element
attrsToRemove = []
for attrName in attrsToRemove :
num += 1
numAttrsRemoved += 1
# now recurse for children
return num
global numElemsRemoved
num = 0
# remove all namespace'd child nodes from this element
childrenToRemove = []
for child in childrenToRemove :
num += 1
numElemsRemoved += 1
# now recurse for children
return num
def removeNestedGroups(node):
"""
This walks further and further down the tree, removing groups
promoting their children up one level
"""
global numElemsRemoved
num = 0
groupsToRemove = []
# only collapse group if it does not have a title or desc as a direct descendant
break
else:
for g in groupsToRemove:
g.parentNode.removeChild(g)
numElemsRemoved += 1
num += 1
# now recurse for children
return num
"""
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.
"""
num = 0
childElements = []
# recurse first into the children (depth-first)
# only process the children if there are more than one element
commonAttrs = {}
# add all inheritable properties of the first child element
# its fill attribute is not what we want to look at, we should look for the first
# non-animate/set element
# this is most of the inheritable properties from http://www.w3.org/TR/SVG11/propidx.html
'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']:
# we just add all the attributes from the first child
# for each subsequent child element
# skip first child
if childNum == 0:
continue
# if we are on an animateXXX/set element, ignore it (due to the 'fill' attribute)
continue
distinctAttrs = []
# loop through all current 'common' attributes
# if this child doesn't match that attribute, schedule it for removal
# remove those attributes which are not common
for name in distinctAttrs:
del commonAttrs[name]
# commonAttrs now has all the inheritable attributes which are common among all child elements
for child in childElements:
# update our statistic (we remove N*M attributes and add back in M attributes)
return num
"""
This recursively calls this function on all children of the element passed in,
then removes any unused attributes on this elem if none of the children inherit it
"""
num = 0
childElements = []
# recurse first into the children (depth-first)
# only process the children if there are more than one element
# get all attribute values on this parent
unusedAttrs = {}
'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']:
# for each child, if at least one child inherits the parent's attribute, then remove
inheritedAttrs = []
for a in inheritedAttrs:
del unusedAttrs[a]
# unusedAttrs now has all the parent attributes that are unused
num += 1
return num
global numElemsRemoved
num = 0
stops = {}
stopsToRemove = []
# convert percentages into a floating point number
else:
offset = 0
# set the stop offset value to the integer or floating point equivalent
for stop in stopsToRemove:
num += 1
numElemsRemoved += 1
# linear gradients
return num
global numElemsRemoved
num = 0
# make sure to reset the ref'ed ids for when we are running this in testscour
if count == 1:
# found a gradient that is referenced by only 1 other element
# elem is a gradient referenced by only one other gradient (refElem)
# add the stops to the referencing gradient (this removes them from elem)
for stop in stopsToAdd:
# adopt the gradientUnits, spreadMethod, gradientTransform attributes if
# they are unspecified on refElem
# if both are radialGradients, adopt elem's fx,fy,cx,cy,r attributes if
# they are unspecified on refElem
# if both are linearGradients, adopt elem's x1,y1,x2,y2 attributes if
# they are unspecified on refElem
# now remove the xlink:href from refElem
# now delete elem
numElemsRemoved += 1
num += 1
return num
def removeDuplicateGradients(doc):
global numElemsRemoved
num = 0
gradientsToRemove = {}
duplicateToMaster = {}
# TODO: should slice grads from 'grad' here to optimize
# do not compare gradient to itself
# compare grad to ograd (all properties, then all stops)
# if attributes do not match, go to next gradient
for attr in ['gradientUnits','spreadMethod','gradientTransform','x1','y1','x2','y2','cx','cy','fx','fy','r']:
break;
if someGradAttrsDoNotMatch: continue
# compare xlink:href values too
continue
# all gradient properties match, now time to compare stops
# now compare stops
if stopsNotEqual: break
break
if stopsNotEqual: continue
# ograd is a duplicate of grad, we schedule it to be removed UNLESS
# ograd is ALREADY considered a 'master' element
gradientsToRemove[grad] = []
# get a collection of all elements that are referenced and their referencing elements
# if the duplicate gradient no longer has a parent that means it was
# already re-mapped to another master gradient
if not dupGrad.parentNode: continue
# for each element that referenced the gradient we are going to remove
# find out which attribute referenced the duplicate gradient
# now that all referencing elements have been re-mapped to the master
# it is safe to remove this gradient from the document
numElemsRemoved += 1
num += 1
return num
num = 0
# get all style properties and stuff them into a dictionary
styleMap = { }
# I've seen this enough to know that I need to correct it:
# fill: url(#linearGradient4918) rgb(0, 0, 0);
if len(chunk) == 2 and (chunk[0][:5] == 'url(#' or chunk[0][:6] == 'url("#' or chunk[0][:6] == "url('#") and chunk[1] == 'rgb(0, 0, 0)' :
num += 1
# Here is where we can weed out unnecessary styles like:
# opacity:1
# 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 :
'stroke-opacity', 'stroke-miterlimit', 'stroke-linecap', 'stroke-dasharray',
'stroke-dashoffset', 'stroke-opacity'] :
del styleMap[uselessStyle]
num += 1
# if stroke:none, then remove all stroke-related properties (stroke-width, etc)
# TODO: should also detect if the computed value of this element is stroke="none"
'stroke-linecap', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-opacity'] :
del styleMap[strokestyle]
num += 1
# TODO: This is actually a problem if a parent element has a specified stroke
# we need to properly calculate computed values
del styleMap['stroke']
# if fill:none, then remove all fill-related properties (fill-rule, etc)
num += 1
# stop-opacity: 1
del styleMap['stop-opacity']
num += 1
# fill-opacity: 1 or 0
# 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 :
del styleMap[uselessFillStyle]
num += 1
# stroke-opacity: 1 or 0
# 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 :
'stroke-dasharray', 'stroke-dashoffset' ] :
num += 1
# stroke-width: 0
if strokeWidth == 0.0 :
'stroke-dasharray', 'stroke-dashoffset', 'stroke-opacity' ] :
num += 1
# remove font properties for non-text elements
# I've actually observed this in real SVG content
'font-style', 'font-variant', 'font-weight',
'letter-spacing', 'line-height', 'kerning',
'text-anchor', 'text-decoration', 'text-rendering',
'unicode-bidi', 'word-spacing', 'writing-mode'] :
num += 1
# remove inkscape-specific styles
# TODO: need to get a full list of these
for inkscapeStyle in ['-inkscape-font-specification']:
del styleMap[inkscapeStyle]
num += 1
# visibility: visible
del styleMap['visibility']
num += 1
# display: inline
del styleMap['display']
num += 1
# overflow: visible or overflow specified on element other than svg, marker, pattern
del styleMap['overflow']
num += 1
# marker: none
del styleMap['marker']
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
if options.style_to_xml:
if propName in svgAttributes :
# sew our remaining style properties back together into a style attribute
fixedStyle = ''
if fixedStyle != '' :
else:
# recurse for our child elements
return num
num = 0
# gradientUnits: objectBoundingBox
num += 1
# spreadMethod: pad
num += 1
# x1: 0%
num += 1
# y1: 0%
num += 1
# x2: 100%
num += 1
# y2: 0%
num += 1
# fx: equal to rx
num += 1
# fy: equal to ry
num += 1
# cx: 50%
num += 1
# cy: 50%
num += 1
# r: 50%
num += 1
# recurse for our child elements
return num
rgbp = re.compile("\\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
"""
s = value
s = colors[s]
if rgbpMatch != None :
s = 'rgb(%d,%d,%d)' % (r,g,b)
if rgbMatch != None :
s = '#'+r+g+b
s = s.upper()
s = '#'+s[1]+s[3]+s[5]
return s
def convertColors(element) :
"""
Recursively converts all color properties into #RRGGBB format if shorter
"""
numBytes = 0
# set up list of color attributes for each element type
attrsToConvert = []
'line', 'polyline', 'path', 'g', 'a']:
attrsToConvert = ['stop-color']
attrsToConvert = ['solid-color']
# now convert all the color formats
for attr in attrsToConvert:
if oldColorValue != '':
# now recurse for our child elements
return numBytes
# TODO: go over what this method does and see if there is a way to optimize it
# reusing data structures, etc
"""
Cleans the path string (d attribute) of the element
"""
global numBytesSavedInPathData
global numPathSegmentsReduced
global numCurvesStraightened
# this gets the parser object from svg_regex.py
# 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 = []
# one or more tuples, each containing two numbers
nums = []
for t in dataset:
# convert to a Decimal
# only create this segment if it is not empty
if nums:
# one or more numbers
nums = []
for n in dataset:
if nums:
# one or more tuples, each containing three tuples of two numbers each
nums = []
for t in dataset:
for pair in t:
# one or more tuples, each containing two tuples of two numbers each
nums = []
for t in dataset:
for pair in t:
# 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:
# calculate the starting x,y coord for the second path command
else:
# we have a move and then 1 or more coords for lines
# take the last pair of coordinates for the starting point
else: # relative move, accumulate coordinates for the starting point
n = 2
while n < N:
n += 2
# now we have the starting point at x,y so let's save it
# convert absolute coordinates into relative ones (start with the second subcommand
# and leave the first M as absolute)
i = 0
# 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 = []
x = data[i+5]
y = data[i+6]
i += 7
elif cmd == 'a':
x += data[i+5]
y += data[i+6]
i += 7
elif cmd == 'H':
newCmd = 'h'
newData = []
x = data[i]
i += 1
elif cmd == 'h':
x += data[i]
i += 1
elif cmd == 'V':
newCmd = 'v'
newData = []
y = data[i]
i += 1
elif cmd == 'v':
y += data[i]
i += 1
elif cmd in ['M']:
newData = []
x = data[i]
y = data[i+1]
i += 2
newData = []
x = data[i]
y = data[i+1]
i += 2
elif cmd in ['m']:
x += data[i]
y += data[i+1]
i += 2
x += data[i]
y += data[i+1]
i += 2
newData = []
x = data[i+2]
y = data[i+3]
i += 4
x += data[i+2]
y += data[i+3]
i += 4
elif cmd == 'C':
newCmd = 'c'
newData = []
x = data[i+4]
y = data[i+5]
i += 6
elif cmd == 'c':
x += data[i+4]
y += data[i+5]
i += 6
x = startx
y = starty
newCmd = 'z'
# remove empty segments
newData = []
i = 0
else:
i += 2
if newData:
elif cmd == 'c':
newData = []
i = 0
else:
i += 6
if newData:
elif cmd == 'a':
newData = []
i = 0
else:
i += 7
if newData:
elif cmd == 'q':
newData = []
i = 0
else:
i += 4
if newData:
newData = []
i = 0
if data[i] != 0:
else:
i += 1
if newData:
else:
# convert straight curves into lines
i = 0
if cmd == 'c':
newData = []
# since all commands are now relative, we can think of previous point as (0,0)
# and new point (dx,dy) is (data[i+4],data[i+5])
if dx == 0:
else:
# flush any existing curve coords first
if newData:
newData = []
# now create a straight line segment
else:
i += 6
# collapse all consecutive commands of the same type into one command
prevCmd = ''
prevData = []
# flush the previous command if it is not the same type as the current command
if prevCmd != '':
prevCmd = ''
prevData = []
# if the previous and current commands are the same type, collapse
# save last command and data
else:
# flush last command and data
if prevCmd != '':
# convert to shorthand path segments where possible
# convert line segments into h,v where possible
if cmd == 'l':
i = 0
lineTuples = []
if data[i] == 0:
# vertical
if lineTuples:
# flush the existing line command
lineTuples = []
# append the v and then the remaining line coords
if lineTuples:
# flush the line command, then append the h and then the remaining line coords
lineTuples = []
else:
i += 2
if lineTuples:
# convert Bézier curve segments into s where possible
elif cmd == 'c':
i = 0
curveTuples = []
# rotate by 180deg means negate both coordinates
# if the previous control point is equal then we can substitute a
# shorthand bezier command
if curveTuples:
curveTuples = []
# append the s command
else:
j = 0
while j <= 5:
j += 1
# set up control point for next curve segment
i += 6
if curveTuples:
# convert quadratic curve segments into t where possible
elif cmd == 'q':
i = 0
curveTuples = []
if curveTuples:
curveTuples = []
# append the t command
else:
j = 0;
while j <= 3:
j += 1
i += 4
if curveTuples:
else:
# 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
newData = []
else:
else:
# 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
prevCmd = ''
prevData = []
# flush the previous command if it is not the same type as the current command
if prevCmd != '':
prevCmd = ''
prevData = []
# if the previous and current commands are the same type, collapse
# save last command and data
else:
# flush last command and data
if prevCmd != '':
def parseListOfPoints(s):
"""
Parse string into a list of points.
Returns a list of containing an even number of coordinate strings
"""
# (wsp)? comma-or-wsp-separated coordinate pairs (wsp)?
# coordinate-pair = coordinate comma-or-wsp coordinate
# coordinate = sign? integer
i = 0
points = []
# if we had an odd number of points, return empty
# if the coordinates were not unitless, return empty
i += 2
return points
def cleanPolygon(elem):
"""
Remove unnecessary closing point of polygon points attribute
"""
global numPointsRemovedFromPolygon
if N >= 2:
def cleanPolyline(elem):
"""
Scour the polyline points attribute
"""
def serializePath(pathObj):
"""
Reserializes the path data with some cleanups.
"""
pathStr = ""
# this fixes an issue outlined in Fix https://bugs.launchpad.net/scour/+bug/412754
return pathStr
"""
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)
"""
coordsStr = ""
if data != None:
c = 0
# add the scoured coordinate to the path string
# only need the comma if the next number is non-negative or if forceCommaWsp is True
coordsStr += ','
c += 1
return coordsStr
def scourLength(str):
# reduce to the proper number of digits
# integerize if we can
# Decimal.trim() is available in Python 2.6+ to trim trailing zeros
try:
except AttributeError:
# trim it ourselves
if dec != -1:
while s[-1] == '0':
s = s[:-1]
# Decimal.normalize() will uses scientific notation - if that
# string is smaller, then use it
"""
Converts raster references to inline images.
NOTE: there are size limits to base64-encoding handling in browsers
"""
global numRastersEmbedded
# if xlink:href is set, then grab the id
# find if href value has filename ext
# look for 'png', 'jpg', and 'gif' extensions
# check if href resolves to an existing file
# if this is not an absolute path, set path relative
# to script file based on input arg
infilename = '.'
rasterdata = ''
# test if file exists locally
# open raster file as raw binary
# raster = open( href, "rb")
# ... should we remove all images which don't resolve?
if rasterdata != '' :
# base64-encode raster
# set href attribute to base64-encoded equivalent
if b64eRaster != '':
# PNG and GIF both have MIME Type 'image/[ext]', but
if ext == 'jpg':
ext = 'jpeg'
numRastersEmbedded += 1
del b64eRaster
def properlySizeDoc(docElement):
# get doc width and height
return
# else we have a statically sized image and we should try to remedy that
# parse viewBox attribute
# if we have a valid viewBox we need to check it
try:
# if x or y are specified and non-zero then it is not ok to overwrite it
return
return
# if the viewBox did not parse properly it is invalid and ok to overwrite it
except ValueError:
pass
# create a replacement node
newNode = None
if newprefix != '':
else:
# add all the attributes
# clone and add all the child nodes
# replace old node with new node
# set the node to the new node in the remapped namespace prefix
# now do all child nodes
def makeWellFormed(str):
# encode & as & ( must do this first so that < does not become &lt; )
# encode < as <
# encode > as > (TODO: is this necessary?)
return newstr
# hand-rolled serialization function that has the following benefits:
# - pretty printing
# - somewhat judicious use of whitespace
# - ensure id attributes are first
I=''
# always serialize the id or xml:id attributes first
quot = '"'
quot = "'"
quot = '"'
quot = "'"
# now serialize the other attributes
# if the attribute value contains a double-quote, use single-quotes
quot = '"'
quot = "'"
outString += ' '
# preserve xmlns: if it is a namespace prefix declaration
outString += 'xmlns:'
# if no children, self-close
outString += '>'
# element node
# text node
# trim it only in the case of not being a child of an element
# where whitespace might be important
else:
# CDATA node
# Comment node
# TODO: entities, processing instructions, what else?
else: # ignore the rest
pass
else:
outString += '/>'
return outString
# this is the main method
# input is a string representation of the input XML
# returns a string representation of the output XML
if options is None:
global numAttrsRemoved
global numStylePropsFixed
global numElemsRemoved
global numBytesSavedInColors
# for whatever reason this does not always remove all inkscape/sodipodi attributes/elements
# on the first pass, so we do it multiple times
# does it have to do with removal of children affecting the childlist?
pass
pass
# remove the xmlns: declarations now
xmlnsDeclsToRemove = []
for attr in xmlnsDeclsToRemove :
numAttrsRemoved += 1
# ensure namespace for SVG is declared
# TODO: what if the default namespace is something else (i.e. some valid namespace)?
# TODO: throw error or warning?
# check for redundant SVG namespace declaration
xmlnsDeclsToRemove = []
redundantPrefixes = []
for attrName in xmlnsDeclsToRemove:
for prefix in redundantPrefixes:
# repair style (remove unnecessary style properties and change them into XML attributes)
# convert colors to #RRGGBB format
if options.simple_colors:
# remove empty defs, metadata, g
# NOTE: these elements will be removed even if they have (invalid) text nodes
elemsToRemove = []
if removeElem == False :
break
else:
if removeElem :
numElemsRemoved += 1
pass
while bContinueLooping:
pass
# move common attributes to parent group
# remove unused attributes from parent
pass
# remove gradients that are only referenced by one other gradient
pass
# remove duplicate gradients
pass
# clean path data
else:
# remove unnecessary closing point of polygons and scour points
# scour points of polyline
# scour lengths (including coordinates)
for type in ['svg', 'image', 'rect', 'circle', 'ellipse', 'line', 'linearGradient', 'radialGradient', 'stop']:
'x1', 'y1', 'x2', 'y2', 'fx', 'fy', 'offset', 'opacity',
'fill-opacity', 'stroke-opacity', 'stroke-width', 'stroke-miterlimit']:
# remove default values of attributes
# convert rasters references to base64-encoded strings
if options.embed_rasters:
# output the document as a pretty string with a single space for indent
# NOTE: removed pretty printing because of this problem:
# rolled our own serialize function here to save on space, put id first, customize indentation, etc
# out_string = doc.documentElement.toprettyxml(' ')
# now strip out empty lines
lines = []
# Get rid of empty lines
# return the string stripped of empty lines
xmlprolog = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n'
else:
xmlprolog = ""
# used mostly by unit tests
# input is a filename
# returns the minidom doc representation of the SVG
# GZ: Seems most other commandline tools don't do this, is it really wanted?
"""
Show application name, version number, and copyright statement
above usage information.
"""
# GZ: would prefer this to be in a function or class scope, but tests etc need
# access to the defaults anyway
usage="%prog [-i input.svg] [-o output.svg] [OPTIONS]",
description=("If the input/output files are specified with a svgz"
" extension, then compressed SVG is assumed. If the input file is not"
" specified, stdin is used. If the output file is not specified, "
" stdout is used."),
help="won't convert all colors to #RRGGBB format")
help="won't convert styles into XML attributes")
help="won't collapse <g> elements")
help="remove all un-referenced ID attributes")
help="won't embed rasters as base64-encoded data")
help="won't remove Inkscape, Sodipodi or Adobe Illustrator elements and attributes")
help="won't output the <?xml ?> prolog")
# GZ: this is confusing, most people will be thinking in terms of
# decimal places, which is not what decimal precision is doing
help="set number of significant digits (default: %default)")
help="indentation of the output: none, space, tab (default: %default)")
def parse_args(args=None):
if rargs:
if options.infilename:
# GZ: could catch a raised IOError here and report
else:
# GZ: could sniff for gzip compression here
if options.outfilename:
else:
def getReport():
if __name__ == '__main__':
else:
# GZ: is this different from time.time() in any way?
def get_tick():
# do the work
# Close input and output files
# GZ: unless silenced by -q or something?
# GZ: not using globals would be good too