inkex.py revision 1d7baac718ad43ee1a8621a7c871024879e6f8e0
# -*- coding: utf-8 -*-
"""
A helper module for creating Inkscape extensions
Copyright (C) 2005,2010 Aaron Spike <aaron@ekips.org> and contributors
Contributors:
Aurélio A. Heckert <aurium(a)gmail.com>
Bulia Byak <buliabyak@users.sf.net>
Nicolas Dufour, nicoduf@yahoo.fr
Peter J. R. Moulder <pjrm@users.sourceforge.net>
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
import copy
import gettext
import optparse
import os
import random
import re
import sys
from math import *
#a dictionary of all of the xmlns prefixes in a standard inkscape doc
NSS = {
u'sodipodi' :u'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
u'cc' :u'http://creativecommons.org/ns#',
u'ccOLD' :u'http://web.resource.org/cc/',
u'svg' :u'http://www.w3.org/2000/svg',
u'dc' :u'http://purl.org/dc/elements/1.1/',
u'rdf' :u'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
u'inkscape' :u'http://www.inkscape.org/namespaces/inkscape',
u'xlink' :u'http://www.w3.org/1999/xlink',
u'xml' :u'http://www.w3.org/XML/1998/namespace'
}
def localize():
domain = 'inkscape'
import locale
try:
except KeyError:
try:
except KeyError:
try:
except KeyError:
else:
try:
except KeyError:
#sys.stderr.write(str(localdir) + "\n")
return what
"""Intended for end-user-visible error messages.
(Currently just writes to stderr with an appended newline, but could do
something better in future: e.g. could add markup to distinguish error
messages from status messages or debugging output.)
Note that this should always be combined with translation:
import inkex
inkex.localize()
...
inkex.errormsg(_("This extension requires two selected paths."))
"""
else:
def are_near_relative(a, b, eps):
return True
else:
return False
# third party library
try:
except Exception, e:
localize()
errormsg(_("The fantastic lxml wrapper for libxml2 is required by inkex.py and therefore this extension. Please download and install the latest version from http://cheeseshop.python.org/pypi/lxml/, or install it through your package manager by a command like: sudo apt-get install python-lxml\n\nTechnical details:\n%s" % (e,)))
return True
return False
else:
return val
class Effect:
"""A class for creating Inkscape SVG Effects"""
self.original_document=None
self.OptionParser = optparse.OptionParser(usage="usage: %prog [options] SVGfile",option_class=InkOption)
help="id attribute of object to manipulate")
pass
"""Collect command line arguments"""
"""Parse document in specified file or on stdin"""
# First try to open the file from the function argument
if filename != None:
try:
except Exception:
# If it wasn't specified, try to open the file specified as
# an object member
try:
except Exception:
# Finally, if the filename was not specified anywhere, use
# standard input stream
else:
# defines view_center in terms of document units
def getposinlayer(self):
#defaults
if layerattr:
if layer:
if x and y and doc_height is not None:
self.view_center = (float(x), doc_height - float(y)) # FIXME: y-coordinate flip, eliminate it when it's gone in Inkscape
def getselected(self):
"""Collect selected nodes"""
path = '//*[@id="%s"]' % i
if el_list:
return el_list[0]
else:
return None
return parent
break
for m in docIdNodes:
def getNamedView(self):
atts = {
}
self.getNamedView(),
return guide
"""Serialize document into XML on stdout"""
"""Affect an SVG document with a callback effect"""
if make_new_id:
return new_id
try:
except:
retval = None
return retval
#a dictionary of unit to user unit conversion factors
'm':3779.52755913, 'km':3779527.55913, 'pc':16.0, 'yd':3456.0 , 'ft':1152.0}
# Fault tolerance for lazily defined SVG
def getDocumentWidth(self):
if width:
return width
else:
if viewbox:
else:
return '0'
# Fault tolerance for lazily defined SVG
def getDocumentHeight(self):
if height:
return height
else:
if viewbox:
else:
return '0'
# Function returns the unit used for the values in SVG.
# For lack of an attribute in SVG that explicitly defines what units are used for SVG coordinates,
# try to calculate the unit from the SVG width and SVG viewbox.
# Defaults to 'px' units.
def getDocumentUnit(self):
if viewboxstr and svgwidth is not None:
if p:
else:
errormsg(_("SVG Width not set correctly! Assuming width = 100"))
if u:
viewboxnumbers = []
for t in viewboxstr.split():
try:
except ValueError:
pass
# try to find the svgunitfactor in the list of units known. If we don't find something, ...
#found match!
return svgunit
'''Returns userunits given a string representation of units in another system'''
if string is not None:
if p:
else:
retval = 0.0
if u:
try:
return retval * (self.__uuconv[u.string[u.start():u.end()]] / self.__uuconv[self.getDocumentUnit()])
except KeyError:
pass
else: # default assume 'px' unit
else:
retval = None
return retval
''' Add document unit when no unit is specified in the string '''
try:
except ValueError:
return value
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99