-
Notifications
You must be signed in to change notification settings - Fork 49
/
applytransform.py
198 lines (161 loc) · 6.9 KB
/
applytransform.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
#
# License: GPL2
# Copyright Mark "Klowner" Riedesel
# https://github.com/Klowner/inkscape-applytransforms
#
import inkex
import math
from inkex.paths import CubicSuperPath, Path
from inkex.transforms import Transform
from inkex.styles import Style
NULL_TRANSFORM = Transform([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
class ApplyTransform(inkex.EffectExtension):
def __init__(self):
super(ApplyTransform, self).__init__()
def effect(self):
if self.svg.selected:
for (_, shape) in self.svg.selected.items():
self.recursiveFuseTransform(shape)
else:
self.recursiveFuseTransform(self.document.getroot())
@staticmethod
def objectToPath(node):
if node.tag == inkex.addNS('g', 'svg'):
return node
if node.tag == inkex.addNS('path', 'svg') or node.tag == 'path':
for attName in node.attrib.keys():
if ("sodipodi" in attName) or ("inkscape" in attName):
del node.attrib[attName]
return node
return node
def scaleStrokeWidth(self, node, transf):
if 'style' in node.attrib:
style = node.attrib.get('style')
style = dict(Style.parse_str(style))
update = False
if 'stroke-width' in style:
try:
stroke_width = self.svg.unittouu(style.get('stroke-width')) / self.svg.unittouu("1px")
stroke_width *= math.sqrt(abs(transf.a * transf.d - transf.b * transf.c))
style['stroke-width'] = str(stroke_width)
update = True
except AttributeError as e:
pass
if update:
node.attrib['style'] = Style(style).to_str()
def transformRectangle(self, node, transf: Transform):
x = float(node.get('x', '0'))
y = float(node.get('y', '0'))
width = float(node.get('width', '0'))
height = float(node.get('height', '0'))
rx = float(node.get('rx', '0'))
ry = float(node.get('ry', '0'))
# Extract translation, scaling and rotation
a, b, c, d = transf.a, transf.b, transf.c, transf.d
tx, ty = transf.e, transf.f
sx = math.sqrt(a**2 + c**2)
sy = math.sqrt(b**2 + d**2)
angle = math.degrees(math.atan2(b, a))
# Calculate the center of the rectangle
cx = x + width / 2
cy = y + height / 2
# Apply the transformation to the center point
new_cx, new_cy = transf.apply_to_point((cx, cy))
new_x = new_cx - (width * sx) / 2
new_y = new_cy - (height * sy) / 2
# Update rectangle attributes
node.set('x', str(new_x))
node.set('y', str(new_y))
node.set('width', str(width * sx))
node.set('height', str(height * sy))
# Apply scale to rx and ry if they exist
if rx > 0:
node.set('rx', str(rx * sx))
if ry > 0:
node.set('ry', str(ry * sy))
# Add rotation if it exists
if abs(angle) > 1e-6:
tr = Transform(f"rotate({angle:.6f},{new_cx:.6f},{new_cy:.6f})")
node.set('transform',tr)
def recursiveFuseTransform(self, node, transf=[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]):
transf = Transform(transf) @ Transform(node.get("transform", None))
if 'transform' in node.attrib:
del node.attrib['transform']
node = ApplyTransform.objectToPath(node)
if transf == NULL_TRANSFORM:
# Don't do anything if there is effectively no transform applied
# reduces alerts for unsupported nodes
pass
elif 'd' in node.attrib:
d = node.get('d')
p = CubicSuperPath(d)
p = Path(p).to_absolute().transform(transf, True)
node.set('d', str(Path(CubicSuperPath(p).to_path())))
self.scaleStrokeWidth(node, transf)
elif node.tag in [inkex.addNS('polygon', 'svg'),
inkex.addNS('polyline', 'svg')]:
points = node.get('points')
points = points.strip().split(' ')
for k, p in enumerate(points):
if ',' in p:
p = p.split(',')
p = [float(p[0]), float(p[1])]
p = transf.apply_to_point(p)
p = [str(p[0]), str(p[1])]
p = ','.join(p)
points[k] = p
points = ' '.join(points)
node.set('points', points)
self.scaleStrokeWidth(node, transf)
elif node.tag in [inkex.addNS("ellipse", "svg"), inkex.addNS("circle", "svg")]:
def isequal(a, b):
return abs(a - b) <= transf.absolute_tolerance
if node.TAG == "ellipse":
rx = float(node.get("rx"))
ry = float(node.get("ry"))
else:
rx = float(node.get("r"))
ry = rx
cx = float(node.get("cx"))
cy = float(node.get("cy"))
sqxy1 = (cx - rx, cy - ry)
sqxy2 = (cx + rx, cy - ry)
sqxy3 = (cx + rx, cy + ry)
newxy1 = transf.apply_to_point(sqxy1)
newxy2 = transf.apply_to_point(sqxy2)
newxy3 = transf.apply_to_point(sqxy3)
node.set("cx", (newxy1[0] + newxy3[0]) / 2)
node.set("cy", (newxy1[1] + newxy3[1]) / 2)
edgex = math.sqrt(
abs(newxy1[0] - newxy2[0]) ** 2 + abs(newxy1[1] - newxy2[1]) ** 2
)
edgey = math.sqrt(
abs(newxy2[0] - newxy3[0]) ** 2 + abs(newxy2[1] - newxy3[1]) ** 2
)
if not isequal(edgex, edgey) and (
node.TAG == "circle"
or not isequal(newxy2[0], newxy3[0])
or not isequal(newxy1[1], newxy2[1])
):
inkex.utils.errormsg(f"Warning: Shape {node.TAG} ({node.get('id')}) is approximate only, try Object to path first for better results")
if node.TAG == "ellipse":
node.set("rx", edgex / 2)
node.set("ry", edgey / 2)
else:
node.set("r", edgex / 2)
elif node.tag == inkex.addNS('rect', 'svg'):
self.transformRectangle(node, transf)
self.scaleStrokeWidth(node, transf)
elif node.tag in [inkex.addNS('text', 'svg'),
inkex.addNS('image', 'svg'),
inkex.addNS('use', 'svg')]:
node.attrib['transform'] = str(transf)
inkex.utils.errormsg(f"Shape {node.TAG} ({node.get('id')}) not yet supported. Not all transforms will be applied. Try Object to path first")
else:
# e.g. <g style="...">
self.scaleStrokeWidth(node, transf)
for child in node.getchildren():
self.recursiveFuseTransform(child, transf)
if __name__ == '__main__':
ApplyTransform().run()