~valavanisalex/ubuntu/oneiric/inkscape/inkscape_0.48.1-2ubuntu4

« back to all changes in this revision

Viewing changes to share/extensions/simpletransform.py

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook, Ted Gould, Kees Cook
  • Date: 2009-06-24 14:00:43 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20090624140043-07stp20mry48hqup
Tags: 0.47~pre0-0ubuntu1
* New upstream release

[ Ted Gould ]
* debian/control: Adding libgsl0 and removing version specifics on boost

[ Kees Cook ]
* debian/watch: updated to run uupdate and mangle pre-release versions.
* Dropped patches that have been taken upstream:
  - 01_mips
  - 02-poppler-0.8.3
  - 03-chinese-inkscape
  - 05_fix_latex_patch
  - 06_gcc-4.4
  - 07_cdr2svg
  - 08_skip-bad-utf-on-pdf-import
  - 09_gtk-clist
  - 10_belarussian
  - 11_libpng
  - 12_desktop
  - 13_slider
  - 100_svg_import_improvements
  - 102_sp_pattern_painter_free
  - 103_bitmap_type_print

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
'''
3
 
Copyright (C) 2006 Jean-Francois Barraud, barraud@math.univ-lille1.fr
4
 
 
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.
9
 
 
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.
14
 
 
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
18
 
barraud@math.univ-lille1.fr
19
 
 
20
 
This code defines several functions to make handling of transform
21
 
attribute easier.
22
 
'''
23
 
import inkex, cubicsuperpath, bezmisc, simplestyle
24
 
import copy, math, re
25
 
 
26
 
def parseTransform(transf,mat=[[1.0,0.0,0.0],[0.0,1.0,0.0]]):
27
 
    if transf=="" or transf==None:
28
 
        return(mat)
29
 
    result=re.match("(translate|scale|rotate|skewX|skewY|matrix)\(([^)]*)\)",transf)
30
 
#-- translate --
31
 
    if result.group(1)=="translate":
32
 
        args=result.group(2).split(",")
33
 
        dx=float(args[0])
34
 
        if len(args)==1:
35
 
            dy=0.0
36
 
        else:
37
 
            dy=float(args[1])
38
 
        matrix=[[1,0,dx],[0,1,dy]]
39
 
#-- scale --
40
 
    if result.groups(1)=="scale":
41
 
        args=result.group(2).split(",")
42
 
        sx=float(args[0])
43
 
        if len(args)==1:
44
 
            sy=sx
45
 
        else:
46
 
            sy=float(args[1])
47
 
        matrix=[[sx,0,0],[0,sy,0]]
48
 
#-- rotate --
49
 
    if result.groups(1)=="rotate":
50
 
        args=result.group(2).split(",")
51
 
        a=float(args[0])*math.pi/180
52
 
        if len(args)==1:
53
 
            cx,cy=(0.0,0.0)
54
 
        else:
55
 
            cx,cy=args[1:]
56
 
        matrix=[[math.cos(a),-math.sin(a),cx],[math.sin(a),math.cos(a),cy]]
57
 
#-- skewX --
58
 
    if result.groups(1)=="skewX":
59
 
        a=float(result.group(2))*math.pi/180
60
 
        matrix=[[1,math.tan(a),0],[0,1,0]]
61
 
#-- skewX --
62
 
    if result.groups(1)=="skewX":
63
 
        a=float(result.group(2))*math.pi/180
64
 
        matrix=[[1,0,0],[math.tan(a),1,0]]
65
 
#-- matrix --
66
 
    if result.group(1)=="matrix":
67
 
        a11,a21,a12,a22,v1,v2=result.group(2).split(",")
68
 
        matrix=[[float(a11),float(a12),float(v1)],[float(a21),float(a22),float(v2)]]
69
 
    
70
 
    matrix=composeTransform(mat,matrix)
71
 
    if result.end()<len(transf):
72
 
        return(parseTransform(transf[result.end():],matrix))
73
 
    else:
74
 
        return matrix
75
 
 
76
 
def formatTransform(mat):
77
 
    return("matrix(%f,%f,%f,%f,%f,%f)"%(mat[0][0],mat[1][0],mat[0][1],mat[1][1],mat[0][2],mat[1][2]))
78
 
 
79
 
def composeTransform(M1,M2):
80
 
    a11=M1[0][0]*M2[0][0]+M1[0][1]*M2[1][0]
81
 
    a12=M1[0][0]*M2[0][1]+M1[0][1]*M2[1][1]
82
 
    a21=M1[1][0]*M2[0][0]+M1[1][1]*M2[1][0]
83
 
    a22=M1[1][0]*M2[0][1]+M1[1][1]*M2[1][1]
84
 
 
85
 
    v1=M1[0][0]*M2[0][2]+M1[0][1]*M2[1][2]+M1[0][2]
86
 
    v2=M1[1][0]*M2[0][2]+M1[1][1]*M2[1][2]+M1[1][2]
87
 
    return [[a11,a12,v1],[a21,a22,v2]]
88
 
 
89
 
def applyTransformToNode(mat,node):
90
 
    m=parseTransform(node.get("transform"))
91
 
    newtransf=formatTransform(composeTransform(mat,m))
92
 
    node.set("transform", newtransf)
93
 
 
94
 
def applyTransformToPoint(mat,pt):
95
 
    x=mat[0][0]*pt[0]+mat[0][1]*pt[1]+mat[0][2]
96
 
    y=mat[1][0]*pt[0]+mat[1][1]*pt[1]+mat[1][2]
97
 
    pt[0]=x
98
 
    pt[1]=y
99
 
 
100
 
def applyTransformToPath(mat,path):
101
 
    for comp in path:
102
 
        for ctl in comp:
103
 
            for pt in ctl:
104
 
                applyTransformToPoint(mat,pt)
105
 
 
106
 
def fuseTransform(node):
107
 
    if node.get('d')==None:
108
 
        #FIX ME: how do you raise errors?
109
 
        raise AssertionError, 'can not fuse "transform" of elements that have no "d" attribute'
110
 
    t = node.get("transform")
111
 
    if t == None:
112
 
        return
113
 
    m = parseTransform(t)
114
 
    d = node.get('d')
115
 
    p = cubicsuperpath.parsePath(d)
116
 
    applyTransformToPath(m,p)
117
 
    node.set('d', cubicsuperpath.formatPath(p))
118
 
    del node.attrib["transform"]
119
 
 
120
 
####################################################################
121
 
##-- Some functions to compute a rough bbox of a given list of objects.
122
 
##-- this should be shipped out in an separate file...
123
 
 
124
 
def boxunion(b1,b2):
125
 
    if b1 is None:
126
 
        return b2
127
 
    elif b2 is None:
128
 
        return b1    
129
 
    else:
130
 
        return((min(b1[0],b2[0]),max(b1[1],b2[1]),min(b1[2],b2[2]),max(b1[3],b2[3])))
131
 
 
132
 
def roughBBox(path):
133
 
    xmin,xMax,ymin,yMax=path[0][0][0][0],path[0][0][0][0],path[0][0][0][1],path[0][0][0][1]
134
 
    for pathcomp in path:
135
 
        for ctl in pathcomp:
136
 
           for pt in ctl:
137
 
               xmin=min(xmin,pt[0])
138
 
               xMax=max(xMax,pt[0])
139
 
               ymin=min(ymin,pt[1])
140
 
               yMax=max(yMax,pt[1])
141
 
    return xmin,xMax,ymin,yMax
142
 
 
143
 
def computeBBox(aList,mat=[[1,0,0],[0,1,0]]):
144
 
    bbox=None
145
 
    for node in aList:
146
 
        m = parseTransform(node.get('transform'))
147
 
        m = composeTransform(mat,m)
148
 
        #TODO: text not supported!
149
 
        if node.get("d"):
150
 
            d = node.get('d')
151
 
            p = cubicsuperpath.parsePath(d)
152
 
            applyTransformToPath(m,p)
153
 
            bbox=boxunion(roughBBox(p),bbox)
154
 
 
155
 
        if  node.tag == inkex.addNS('use','svg') or node.tag=='use':
156
 
            refid=node.get(inkex.addNS('href','xlink'))
157
 
            path = '//*[@id="%s"]' % refid[1:]
158
 
            refnode = node.getroottree().xpath(path, namespaces=inkex.NSS)
159
 
            bbox=boxunion(computeBBox(refnode,m),bbox)
160
 
            
161
 
        bbox=boxunion(computeBBox(node,m),bbox)
162
 
    return bbox
163
 
 
164
 
 
 
1
#!/usr/bin/env python
 
2
'''
 
3
Copyright (C) 2006 Jean-Francois Barraud, barraud@math.univ-lille1.fr
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
barraud@math.univ-lille1.fr
 
19
 
 
20
This code defines several functions to make handling of transform
 
21
attribute easier.
 
22
'''
 
23
import inkex, cubicsuperpath, bezmisc, simplestyle
 
24
import copy, math, re
 
25
 
 
26
def parseTransform(transf,mat=[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]):
 
27
    if transf=="" or transf==None:
 
28
        return(mat)
 
29
    stransf = transf.strip()
 
30
    result=re.match("(translate|scale|rotate|skewX|skewY|matrix)\s*\(([^)]*)\)\s*,?",stransf)
 
31
#-- translate --
 
32
    if result.group(1)=="translate":
 
33
        args=result.group(2).replace(',',' ').split()
 
34
        dx=float(args[0])
 
35
        if len(args)==1:
 
36
            dy=0.0
 
37
        else:
 
38
            dy=float(args[1])
 
39
        matrix=[[1,0,dx],[0,1,dy]]
 
40
#-- scale --
 
41
    if result.group(1)=="scale":
 
42
        args=result.group(2).replace(',',' ').split()
 
43
        sx=float(args[0])
 
44
        if len(args)==1:
 
45
            sy=sx
 
46
        else:
 
47
            sy=float(args[1])
 
48
        matrix=[[sx,0,0],[0,sy,0]]
 
49
#-- rotate --
 
50
    if result.group(1)=="rotate":
 
51
        args=result.group(2).replace(',',' ').split()
 
52
        a=float(args[0])*math.pi/180
 
53
        if len(args)==1:
 
54
            cx,cy=(0.0,0.0)
 
55
        else:
 
56
            cx,cy=map(float,args[1:])
 
57
        matrix=[[math.cos(a),-math.sin(a),cx],[math.sin(a),math.cos(a),cy]]
 
58
#-- skewX --
 
59
    if result.group(1)=="skewX":
 
60
        a=float(result.group(2))*math.pi/180
 
61
        matrix=[[1,math.tan(a),0],[0,1,0]]
 
62
#-- skewY --
 
63
    if result.group(1)=="skewY":
 
64
        a=float(result.group(2))*math.pi/180
 
65
        matrix=[[1,0,0],[math.tan(a),1,0]]
 
66
#-- matrix --
 
67
    if result.group(1)=="matrix":
 
68
        a11,a21,a12,a22,v1,v2=result.group(2).replace(',',' ').split()
 
69
        matrix=[[float(a11),float(a12),float(v1)], [float(a21),float(a22),float(v2)]]
 
70
 
 
71
    matrix=composeTransform(mat,matrix)
 
72
    if result.end() < len(stransf):
 
73
        return(parseTransform(stransf[result.end():], matrix))
 
74
    else:
 
75
        return matrix
 
76
 
 
77
def formatTransform(mat):
 
78
    return ("matrix(%f,%f,%f,%f,%f,%f)" % (mat[0][0], mat[1][0], mat[0][1], mat[1][1], mat[0][2], mat[1][2]))
 
79
 
 
80
def composeTransform(M1,M2):
 
81
    a11 = M1[0][0]*M2[0][0] + M1[0][1]*M2[1][0]
 
82
    a12 = M1[0][0]*M2[0][1] + M1[0][1]*M2[1][1]
 
83
    a21 = M1[1][0]*M2[0][0] + M1[1][1]*M2[1][0]
 
84
    a22 = M1[1][0]*M2[0][1] + M1[1][1]*M2[1][1]
 
85
 
 
86
    v1 = M1[0][0]*M2[0][2] + M1[0][1]*M2[1][2] + M1[0][2]
 
87
    v2 = M1[1][0]*M2[0][2] + M1[1][1]*M2[1][2] + M1[1][2]
 
88
    return [[a11,a12,v1],[a21,a22,v2]]
 
89
 
 
90
def applyTransformToNode(mat,node):
 
91
    m=parseTransform(node.get("transform"))
 
92
    newtransf=formatTransform(composeTransform(mat,m))
 
93
    node.set("transform", newtransf)
 
94
 
 
95
def applyTransformToPoint(mat,pt):
 
96
    x = mat[0][0]*pt[0] + mat[0][1]*pt[1] + mat[0][2]
 
97
    y = mat[1][0]*pt[0] + mat[1][1]*pt[1] + mat[1][2]
 
98
    pt[0]=x
 
99
    pt[1]=y
 
100
 
 
101
def applyTransformToPath(mat,path):
 
102
    for comp in path:
 
103
        for ctl in comp:
 
104
            for pt in ctl:
 
105
                applyTransformToPoint(mat,pt)
 
106
 
 
107
def fuseTransform(node):
 
108
    if node.get('d')==None:
 
109
        #FIXME: how do you raise errors?
 
110
        raise AssertionError, 'can not fuse "transform" of elements that have no "d" attribute'
 
111
    t = node.get("transform")
 
112
    if t == None:
 
113
        return
 
114
    m = parseTransform(t)
 
115
    d = node.get('d')
 
116
    p = cubicsuperpath.parsePath(d)
 
117
    applyTransformToPath(m,p)
 
118
    node.set('d', cubicsuperpath.formatPath(p))
 
119
    del node.attrib["transform"]
 
120
 
 
121
####################################################################
 
122
##-- Some functions to compute a rough bbox of a given list of objects.
 
123
##-- this should be shipped out in an separate file...
 
124
 
 
125
def boxunion(b1,b2):
 
126
    if b1 is None:
 
127
        return b2
 
128
    elif b2 is None:
 
129
        return b1    
 
130
    else:
 
131
        return((min(b1[0],b2[0]), max(b1[1],b2[1]), min(b1[2],b2[2]), max(b1[3],b2[3])))
 
132
 
 
133
def roughBBox(path):
 
134
    xmin,xMax,ymin,yMax = path[0][0][0][0],path[0][0][0][0],path[0][0][0][1],path[0][0][0][1]
 
135
    for pathcomp in path:
 
136
        for ctl in pathcomp:
 
137
            for pt in ctl:
 
138
                xmin = min(xmin,pt[0])
 
139
                xMax = max(xMax,pt[0])
 
140
                ymin = min(ymin,pt[1])
 
141
                yMax = max(yMax,pt[1])
 
142
    return xmin,xMax,ymin,yMax
 
143
 
 
144
def computeBBox(aList,mat=[[1,0,0],[0,1,0]]):
 
145
    bbox=None
 
146
    for node in aList:
 
147
        m = parseTransform(node.get('transform'))
 
148
        m = composeTransform(mat,m)
 
149
        #TODO: text not supported!
 
150
        if node.get("d"):
 
151
            d = node.get('d')
 
152
            p = cubicsuperpath.parsePath(d)
 
153
            applyTransformToPath(m,p)
 
154
            bbox=boxunion(roughBBox(p),bbox)
 
155
 
 
156
        elif node.tag == inkex.addNS('rect','svg') or node.tag=='rect':
 
157
            w = float(node.get('width'))/2.
 
158
            h = float(node.get('height'))/2.
 
159
            x = float(node.get('x'))
 
160
            y = float(node.get('y'))
 
161
            C = [x + w , y + h ]
 
162
            applyTransformToPoint(mat,C)
 
163
            xmin = C[0] - abs(m[0][0]) * w - abs(m[0][1]) * h
 
164
            xmax = C[0] + abs(m[0][0]) * w + abs(m[0][1]) * h
 
165
            ymin = C[1] - abs(m[1][0]) * w - abs(m[1][1]) * h
 
166
            ymax = C[1] + abs(m[1][0]) * w + abs(m[1][1]) * h
 
167
            bbox = xmin,xmax,ymin,ymax
 
168
            
 
169
        elif node.tag == inkex.addNS('use','svg') or node.tag=='use':
 
170
            refid=node.get(inkex.addNS('href','xlink'))
 
171
            path = '//*[@id="%s"]' % refid[1:]
 
172
            refnode = node.xpath(path)
 
173
            bbox=boxunion(computeBBox(refnode,m),bbox)
 
174
            
 
175
        bbox=boxunion(computeBBox(node,m),bbox)
 
176
    return bbox
 
177
 
 
178
 
 
179
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99