Code

move user unit conversion into inkex.py
[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, simplepath, cubicsuperpath
21 from ffgeom import *
23 class Project(inkex.Effect):
24     def __init__(self):
25             inkex.Effect.__init__(self)
26     def effect(self):
27         if len(self.options.ids) < 2:
28             inkex.debug("Requires two selected paths. The second must be exctly four nodes long.")
29             exit()            
30             
31         #obj is selected second
32         obj = self.selected[self.options.ids[0]]
33         trafo = self.selected[self.options.ids[1]]
34         if obj.tagName == 'path' and trafo.tagName == 'path':
35             #distil trafo into four node points
36             trafo = cubicsuperpath.parsePath(trafo.attributes.getNamedItem('d').value)
37             trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4]
39             #vectors pointing away from the trafo origin
40             self.t1 = Segment(trafo[0],trafo[1])
41             self.t2 = Segment(trafo[1],trafo[2])
42             self.t3 = Segment(trafo[3],trafo[2])
43             self.t4 = Segment(trafo[0],trafo[3])
44     
45             #query inkscape about the bounding box of obj
46             self.q = {'x':0,'y':0,'width':0,'height':0}
47             file = self.args[-1]
48             id = self.options.ids[0]
49             for query in self.q.keys():
50                 f = os.popen("inkscape --query-%s --query-id=%s %s" % (query,id,file))
51                 self.q[query] = float(f.read())
52                 f.close()
53             #glean document height from the SVG
54             docheight = inkex.unittouu(inkex.xml.xpath.Evaluate('/svg/@height',self.document)[0].value)
55             #Flip inkscapes transposed renderer coords
56             self.q['y'] = docheight - self.q['y'] - self.q['height']
58             #process path
59             d = obj.attributes.getNamedItem('d')
60             p = cubicsuperpath.parsePath(d.value)
61             for subs in p:
62                 for csp in subs:
63                     csp[0] = self.trafopoint(csp[0])
64                     csp[1] = self.trafopoint(csp[1])
65                     csp[2] = self.trafopoint(csp[2])
66             d.value = cubicsuperpath.formatPath(p)
68     def trafopoint(self,(x,y)):
69         #Transform algorithm thanks to Jose Hevia (freon)
70         vector = Segment(Point(self.q['x'],self.q['y']),Point(x,y))
71         xratio = abs(vector.delta_x())/self.q['width']
72         yratio = abs(vector.delta_y())/self.q['height']
73     
74         horz = Segment(self.t1.pointAtRatio(xratio),self.t3.pointAtRatio(xratio))
75         vert = Segment(self.t4.pointAtRatio(yratio),self.t2.pointAtRatio(yratio))
77         p = intersectSegments(vert,horz)
78         return [p['x'],p['y']]    
80 e = Project()
81 e.affect()