From 04a43d3d4087906a248986544d46e41a717a937c Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Tue, 22 Oct 2024 22:11:28 +0200 Subject: [PATCH] add rect support for applytransforms (https://github.com/Klowner/inkscape-applytransforms/pull/69/commits/2e4eabaceb04544b056d95435414945d3f81f1d6) --- .../apply_transformations.py | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/extensions/fablabchemnitz/apply_transformations/apply_transformations.py b/extensions/fablabchemnitz/apply_transformations/apply_transformations.py index 1458a96..1a60683 100644 --- a/extensions/fablabchemnitz/apply_transformations/apply_transformations.py +++ b/extensions/fablabchemnitz/apply_transformations/apply_transformations.py @@ -68,6 +68,47 @@ class ApplyTransformations(inkex.EffectExtension): except AttributeError as e: pass + 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, element, transf=[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]): transf = Transform(transf) @ Transform(element.get("transform", None)) #a, b, c, d = linear transformations / e, f = translations @@ -197,9 +238,14 @@ class ApplyTransformations(inkex.EffectExtension): #self.recursiveFuseTransform(linkedObjectCopy, transf) self.recursiveFuseTransform(element.unlink(), transf) - elif element.tag in [inkex.addNS('rect', 'svg'), - inkex.addNS('text', 'svg'), - inkex.addNS('image', 'svg')]: + elif element.tag == inkex.addNS('rect', 'svg'): + self.transformRectangle(element, transf) + self.scaleStrokeWidth(element, transf) + + elif element.tag in [inkex.addNS('text', 'svg'), + inkex.addNS('image', 'svg'), + inkex.addNS('use', 'svg')]: + element.attrib['transform'] = str(transf) inkex.utils.errormsg(f"Shape {element.TAG} ({element.get('id')}) not yet supported. Not all transforms will be applied. Try Object to path first") else: