X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=share%2Fextensions%2Finkex.py;h=2c49f32f8f0b0ed5f569618442e9e4a3f59a47c2;hb=10b12a56e54aab63863f2484a8295001563b1c67;hp=e210e90c2d0dbf3e29615664ca702415f77bed40;hpb=635bc6783b4541149e6a1693e50eed4df4eb85ff;p=inkscape.git diff --git a/share/extensions/inkex.py b/share/extensions/inkex.py index e210e90c2..2c49f32f8 100755 --- a/share/extensions/inkex.py +++ b/share/extensions/inkex.py @@ -19,7 +19,7 @@ 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 sys, copy, optparse, random +import sys, copy, optparse, random, re #a dictionary of all of the xmlns prefixes in a standard inkscape doc NSS = { @@ -32,8 +32,29 @@ u'inkscape' :u'http://www.inkscape.org/namespaces/inkscape', u'xlink' :u'http://www.w3.org/1999/xlink' } +#a dictionary of unit to user unit conversion factors +uuconv = {'in':90.0, 'pt':1.25, 'px':1, 'mm':3.5433070866, 'cm':35.433070866, 'pc':15.0} +def unittouu(string): + '''Returns returns userunits given a string representation of units in another system''' + unit = re.compile('(%s)$' % '|'.join(uuconv.keys())) + param = re.compile(r'(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)') + + p = param.match(string) + u = unit.search(string) + if p: + retval = float(p.string[p.start():p.end()]) + else: + retval = 0.0 + if u: + try: + return retval * uuconv[u.string[u.start():u.end()]] + except KeyError: + pass + return retval + try: import xml.dom.ext + import xml.dom.minidom import xml.dom.ext.reader.Sax2 import xml.xpath except: @@ -59,7 +80,7 @@ class InkOption(optparse.Option): class Effect: """A class for creating Inkscape SVG Effects""" - def __init__(self): + def __init__(self, *args, **kwargs): self.id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ' self.document=None self.ctx=None @@ -67,6 +88,7 @@ class Effect: self.doc_ids={} self.options=None self.args=None + self.use_minidom=kwargs.pop("use_minidom", False) self.OptionParser = optparse.OptionParser(usage="usage: %prog [options] SVGfile",option_class=InkOption) self.OptionParser.add_option("--id", action="append", type="string", dest="ids", default=[], @@ -86,7 +108,10 @@ class Effect: stream = open(self.args[-1],'r') except: stream = sys.stdin - self.document = reader.fromStream(stream) + if self.use_minidom: + self.document = xml.dom.minidom.parse(stream) + else: + self.document = reader.fromStream(stream) self.ctx = xml.xpath.Context.Context(self.document,processorNss=NSS) stream.close() def getposinlayer(self):