Code

made it callable by other extensions. the workaround is sorta ugly, but it should...
[inkscape.git] / share / extensions / perspective.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 Perspective approach & math by Dmitry Platonov, shadowjack@mail.ru, 2006
20 """
21 import sys, inkex, os, re, simplepath, cubicsuperpath 
22 from ffgeom import *
23 from numpy import *
24 from numpy.linalg import *
26 uuconv = {'in':90.0, 'pt':1.25, 'px':1, 'mm':3.5433070866, 'cm':35.433070866, 'pc':15.0}
27 def unittouu(string):
28     unit = re.compile('(%s)$' % '|'.join(uuconv.keys()))
29     param = re.compile(r'(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)')
31     p = param.match(string)
32     u = unit.search(string)    
33     if p:
34         retval = float(p.string[p.start():p.end()])
35     else:
36         retval = 0.0
37     if u:
38         try:
39             return retval * uuconv[u.string[u.start():u.end()]]
40         except KeyError:
41             pass
42     return retval
44 class Project(inkex.Effect):
45     def __init__(self):
46             inkex.Effect.__init__(self)
47     def effect(self):
48         if len(self.options.ids) < 2:
49             inkex.debug("Requires two selected paths. The second must be exctly four nodes long.")
50             exit()            
51             
52         #obj is selected second
53         obj = self.selected[self.options.ids[0]]
54         envelope = self.selected[self.options.ids[1]]
55         if (obj.tagName == 'path' or obj.tagName == 'g') and envelope.tagName == 'path':
56             path = cubicsuperpath.parsePath(envelope.attributes.getNamedItem('d').value)
57             dp = zeros((4,2), dtype=float64)
58             for i in range(4):
59                 dp[i][0] = path[0][i][1][0]
60                 dp[i][1] = path[0][i][1][1]
62             #query inkscape about the bounding box of obj
63             q = {'x':0,'y':0,'width':0,'height':0}
64             file = self.args[-1]
65             id = self.options.ids[0]
66             for query in q.keys():
67                 f = os.popen("inkscape --query-%s --query-id=%s %s" % (query,id,file))
68                 q[query] = float(f.read())
69                 f.close()
70             sp = array([[q['x'], q['y']+q['height']],[q['x'], q['y']],[q['x']+q['width'], q['y']],[q['x']+q['width'], q['y']+q['height']]], dtype=float64)
72             solmatrix = zeros((8,8), dtype=float64)
73             free_term = zeros((8), dtype=float64)
74             for i in (0,1,2,3):
75                 solmatrix[i][0] = sp[i][0]
76                 solmatrix[i][1] = sp[i][1]
77                 solmatrix[i][2] = 1
78                 solmatrix[i][6] = -dp[i][0]*sp[i][0]
79                 solmatrix[i][7] = -dp[i][0]*sp[i][1]
80                 solmatrix[i+4][3] = sp[i][0]
81                 solmatrix[i+4][4] = sp[i][1]
82                 solmatrix[i+4][5] = 1
83                 solmatrix[i+4][6] = -dp[i][1]*sp[i][0]
84                 solmatrix[i+4][7] = -dp[i][1]*sp[i][1]
85                 free_term[i] = dp[i][0]
86                 free_term[i+4] = dp[i][1]
88             res = solve(solmatrix, free_term)
89             projmatrix = array([[res[0],res[1],res[2]],[res[3],res[4],res[5]],[res[6],res[7],1.0]],dtype=float64)
90             if obj.tagName == "path":
91                 self.process_path(obj,projmatrix)
92             if obj.tagName == "g":
93                 self.process_group(obj,projmatrix)
96     def process_group(self,group,m):
97         for node in group.childNodes:
98             if node.nodeType==node.ELEMENT_NODE:
99                 if node.tagName == 'path':
100                      self.process_path(node,m)
101                 if node.tagName == 'g':
102                      self.process_group(node,m) 
103         
105     def process_path(self,path,m):
106         d = path.attributes.getNamedItem('d')
107         p = cubicsuperpath.parsePath(d.value)
108         for subs in p:
109             for csp in subs:
110                 csp[0] = self.project_point(csp[0],m)
111                 csp[1] = self.project_point(csp[1],m)
112                 csp[2] = self.project_point(csp[2],m)
113         d.value = cubicsuperpath.formatPath(p)
117     def project_point(self,p,m):
118         x = p[0]
119         y = p[1]
120         return [(x*m[0][0] + y*m[0][1] + m[0][2])/(x*m[2][0]+y*m[2][1]+m[2][2]),(x*m[1][0] + y*m[1][1] + m[1][2])/(x*m[2][0]+y*m[2][1]+m[2][2])]
122 e = Project()
123 e.affect()