-
Notifications
You must be signed in to change notification settings - Fork 1
/
T.py
executable file
·162 lines (142 loc) · 5.67 KB
/
T.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
#Note: taken from inkscape/extensions/simpletransform.py (with minor changes)
'''
Copyright (C) 2006 Jean-Francois Barraud, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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
This code defines several functions to make handling of transform
attribute easier.
'''
import inkex, cubicsuperpath
import math, re
def parseTransform(transf,mat=[[1.0,0.0,0.0],[0.0,1.0,0.0]]):
if transf=="" or transf==None:
return(mat)
result=re.match("(translate|scale|rotate|skewX|skewY|matrix)\(([^)]*)\)",transf)
#-- translate --
if result.group(1)=="translate":
args=result.group(2).split(",")
dx=float(args[0])
if len(args)==1:
dy=0.0
else:
dy=float(args[1])
matrix=[[1,0,dx],[0,1,dy]]
#-- scale --
if result.groups(1)=="scale":
args=result.group(2).split(",")
sx=float(args[0])
if len(args)==1:
sy=sx
else:
sy=float(args[1])
matrix=[[sx,0,0],[0,sy,0]]
#-- rotate --
if result.groups(1)=="rotate":
args=result.group(2).split(",")
a=float(args[0])*math.pi/180
if len(args)==1:
cx,cy=(0.0,0.0)
else:
cx,cy=args[1:]
matrix=[[math.cos(a),-math.sin(a),cx],[math.sin(a),math.cos(a),cy]]
#-- skewX --
if result.groups(1)=="skewX":
a=float(result.group(2))*math.pi/180
matrix=[[1,math.tan(a),0],[0,1,0]]
#-- skewX --
if result.groups(1)=="skewX":
a=float(result.group(2))*math.pi/180
matrix=[[1,0,0],[math.tan(a),1,0]]
#-- matrix --
if result.group(1)=="matrix":
a11,a21,a12,a22,v1,v2=result.group(2).split(",")
matrix=[[float(a11),float(a12),float(v1)],[float(a21),float(a22),float(v2)]]
matrix=composeTransform(mat,matrix)
if result.end()<len(transf):
return(parseTransform(transf[result.end():],matrix))
else:
return matrix
def formatTransform(mat):
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]))
def composeTransform(M1,M2):
a11=M1[0][0]*M2[0][0]+M1[0][1]*M2[1][0]
a12=M1[0][0]*M2[0][1]+M1[0][1]*M2[1][1]
a21=M1[1][0]*M2[0][0]+M1[1][1]*M2[1][0]
a22=M1[1][0]*M2[0][1]+M1[1][1]*M2[1][1]
v1=M1[0][0]*M2[0][2]+M1[0][1]*M2[1][2]+M1[0][2]
v2=M1[1][0]*M2[0][2]+M1[1][1]*M2[1][2]+M1[1][2]
return [[a11,a12,v1],[a21,a22,v2]]
def writeTransformToNode(mat,node):
node.set("transform", formatTransform(mat))
def applyTransformToNode(mat,node):
m=parseTransform(node.get("transform"))
writeTransformToNode(composeTransform(mat,m), node)
def applyTransformToPoint(mat,pt):
x=mat[0][0]*pt[0]+mat[0][1]*pt[1]+mat[0][2]
y=mat[1][0]*pt[0]+mat[1][1]*pt[1]+mat[1][2]
pt[0]=x
pt[1]=y
def applyTransformToPath(mat,path):
for comp in path:
for ctl in comp:
for pt in ctl:
applyTransformToPoint(mat,pt)
####################################################################
##-- Some functions to compute a rough bbox of a given list of objects.
##-- this should be shipped out in an separate file...
def boxunion(b1,b2):
if b1 is None:
return b2
elif b2 is None:
return b1
else:
return((min(b1[0],b2[0]),max(b1[1],b2[1]),min(b1[2],b2[2]),max(b1[3],b2[3])))
def roughBBox(path):
xmin,xMax,ymin,yMax=path[0][0][0][0],path[0][0][0][0],path[0][0][0][1],path[0][0][0][1]
for pathcomp in path:
for ctl in pathcomp:
for pt in ctl:
xmin=min(xmin,pt[0])
xMax=max(xMax,pt[0])
ymin=min(ymin,pt[1])
yMax=max(yMax,pt[1])
return xmin,xMax,ymin,yMax
def computeBBox(aList,mat=[[1,0,0],[0,1,0]]):
bbox=None
for node in aList:
m = parseTransform(node.get('transform'))
m = composeTransform(mat,m)
#TODO: text not supported!
if node.tag == inkex.addNS('rect','svg'):
A=[0,0]
B=[0,0]
A[0] = float(node.get('x'))
A[1] = float(node.get('y'))
B[0] = A[0]+float(node.get('width'))
B[1] = A[1]+float(node.get('height'))
applyTransformToPoint(mat, A)
applyTransformToPoint(mat, B)
bbox = min(A[0], B[0]), max(A[0], B[0]), min(A[1], B[1]), max(A[1], B[1])
if node.get("d"):
d = node.get('d')
p = cubicsuperpath.parsePath(d)
applyTransformToPath(m,p)
bbox=boxunion(roughBBox(p),bbox)
if node.tag == inkex.addNS('use','svg') or node.tag=='use':
refid=node.get(inkex.addNS('href','xlink'))
path = '//*[@id="%s"]' % refid[1:]
refnode = node.getroottree().xpath(path, namespaces=inkex.NSS)
bbox=boxunion(computeBBox(refnode,m),bbox)
bbox=boxunion(computeBBox(node,m),bbox)
return bbox