Code

moving trunk for module inkscape
[inkscape.git] / share / extensions / summersnight.py
1 #!/usr/bin/env python
2 """
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 """
20 import inkex, os, re, simplepath, cubicsuperpath
21 from ffgeom import *
23 uuconv = {'in':90.0, 'pt':1.25, 'px':1, 'mm':3.5433070866, 'cm':35.433070866, 'pc':15.0}
24 def unittouu(string):
25         unit = re.compile('(%s)$' % '|'.join(uuconv.keys()))
26         param = re.compile(r'(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)')
28         p = param.match(string)
29         u = unit.search(string) 
30         if p:
31                 retval = float(p.string[p.start():p.end()])
32         else:
33                 retval = 0.0
34         if u:
35                 try:
36                         return retval * uuconv[u.string[u.start():u.end()]]
37                 except KeyError:
38                         pass
39         return retval
41 class Project(inkex.Effect):
42         def __init__(self):
43                 inkex.Effect.__init__(self)
44         def effect(self):
45                         if len(self.options.ids) < 2:
46                                 inkex.debug("Requires two selected paths. The second must be exctly four nodes long.")
47                                 exit()
48                         
49                         #obj is selected second
50                         obj = self.selected[self.options.ids[0]]
51                         trafo = self.selected[self.options.ids[1]]
52                         if obj.tagName == 'path' and trafo.tagName == 'path':
53                                 #distil trafo into four node points
54                                 trafo = cubicsuperpath.parsePath(trafo.attributes.getNamedItem('d').value)
55                                 trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4]
57                                 #vectors pointing away from the trafo origin
58                                 self.t1 = Segment(trafo[0],trafo[1])
59                                 self.t2 = Segment(trafo[1],trafo[2])
60                                 self.t3 = Segment(trafo[3],trafo[2])
61                                 self.t4 = Segment(trafo[0],trafo[3])
62         
63                                 #query inkscape about the bounding box of obj
64                                 self.q = {'x':0,'y':0,'width':0,'height':0}
65                                 file = self.args[-1]
66                                 id = self.options.ids[0]
67                                 for query in self.q.keys():
68                                         f = os.popen("inkscape --query-%s --query-id=%s %s" % (query,id,file))
69                                         self.q[query] = float(f.read())
70                                         f.close()
71                                 #glean document height from the SVG
72                                 docheight = unittouu(inkex.xml.xpath.Evaluate('/svg/@height',self.document)[0].value)
73                                 #Flip inkscapes transposed renderer coords
74                                 self.q['y'] = docheight - self.q['y'] - self.q['height']
76                                 #process path
77                                 d = obj.attributes.getNamedItem('d')
78                                 p = cubicsuperpath.parsePath(d.value)
79                                 for subs in p:
80                                         for csp in subs:
81                                                 csp[0] = self.trafopoint(csp[0])
82                                                 csp[1] = self.trafopoint(csp[1])
83                                                 csp[2] = self.trafopoint(csp[2])
84                                 d.value = cubicsuperpath.formatPath(p)
86         def trafopoint(self,(x,y)):
87                 #Transform algorithm thanks to Jose Hevia (freon)
88                 vector = Segment(Point(self.q['x'],self.q['y']),Point(x,y))
89                 xratio = abs(vector.delta_x())/self.q['width']
90                 yratio = abs(vector.delta_y())/self.q['height']
91         
92                 horz = Segment(self.t1.pointAtRatio(xratio),self.t3.pointAtRatio(xratio))
93                 vert = Segment(self.t4.pointAtRatio(yratio),self.t2.pointAtRatio(yratio))
95                 p = intersectSegments(vert,horz)
96                 return [p['x'],p['y']]  
98 e = Project()
99 e.affect()