diff options
| author | capellancitizen <thecapellancitizen@gmail.com> | 2024-08-14 19:40:42 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-14 19:40:42 -0400 |
| commit | f3a3cde71e9312d1f07c156033de5b7fb4b99f2d (patch) | |
| tree | ff47b266848c0233bca8eca067944a672c9095b8 /lib/svg | |
| parent | 744da960b3c96260243fc54a4f837422f6a4c0ac (diff) | |
Clones now also clone commands attached to element and its children. (#3032, #3121) (#3086)
Diffstat (limited to 'lib/svg')
| -rw-r--r-- | lib/svg/path.py | 2 | ||||
| -rw-r--r-- | lib/svg/svg.py | 29 |
2 files changed, 29 insertions, 2 deletions
diff --git a/lib/svg/path.py b/lib/svg/path.py index 878d2a7c..9d92058b 100644 --- a/lib/svg/path.py +++ b/lib/svg/path.py @@ -31,7 +31,7 @@ def compose_parent_transforms(node, mat): return mat -def get_node_transform(node): +def get_node_transform(node: inkex.BaseElement): """ if getattr(node, "composed_transform", None): return node.composed_transform() diff --git a/lib/svg/svg.py b/lib/svg/svg.py index 2c521098..99f5bf26 100644 --- a/lib/svg/svg.py +++ b/lib/svg/svg.py @@ -3,7 +3,9 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. -from inkex import NSS +import math + +from inkex import NSS, BaseElement, Transform from lxml import etree from ..utils import cache @@ -29,3 +31,28 @@ def find_elements(node, xpath): document = get_document(node) elements = document.xpath(xpath, namespaces=NSS) return elements + + +def copy_no_children(node: BaseElement) -> BaseElement: + return type(node)(attrib=node.attrib) + + +def point_upwards(node: BaseElement) -> None: + """ + Given a node, adjust the transform such that it is in the same spot, but pointing upwards (e.g. for command symbols) + """ + # Adjust the transform of the node so it's face-up and the right way around. + node_transform = node.composed_transform() + compensation = -Transform((node_transform.a, node_transform.b, node_transform.c, node_transform.d, 0, 0)) + scale_vector = compensation.capply_to_point(1+1j) + scale_factor = math.sqrt(2)/math.sqrt(scale_vector.real*scale_vector.real + scale_vector.imag*scale_vector.imag) + compensation.add_scale(scale_factor, scale_factor) + + node_correction = Transform().add_translate(float(node.get('x', 0)), float(node.get('y', 0))) + node_correction @= compensation + # Quick hack to compute the rotational angle - node_transform @ (1,0) = (a, b) + + node.transform = node.transform @ node_correction + # Clear the x and y coords, they've been incorporated to the transform above + node.set('x', None) + node.set('y', None) |
