svg-ultralight 0.48.0__py3-none-any.whl → 0.50.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of svg-ultralight might be problematic. Click here for more details.

Files changed (37) hide show
  1. svg_ultralight/__init__.py +108 -105
  2. svg_ultralight/animate.py +40 -40
  3. svg_ultralight/attrib_hints.py +13 -14
  4. svg_ultralight/bounding_boxes/__init__.py +5 -5
  5. svg_ultralight/bounding_boxes/bound_helpers.py +189 -189
  6. svg_ultralight/bounding_boxes/padded_text_initializers.py +207 -207
  7. svg_ultralight/bounding_boxes/supports_bounds.py +166 -166
  8. svg_ultralight/bounding_boxes/type_bound_collection.py +71 -71
  9. svg_ultralight/bounding_boxes/type_bound_element.py +65 -65
  10. svg_ultralight/bounding_boxes/type_bounding_box.py +396 -396
  11. svg_ultralight/bounding_boxes/type_padded_text.py +411 -411
  12. svg_ultralight/constructors/__init__.py +14 -14
  13. svg_ultralight/constructors/new_element.py +115 -115
  14. svg_ultralight/font_tools/__init__.py +5 -5
  15. svg_ultralight/font_tools/comp_results.py +295 -293
  16. svg_ultralight/font_tools/font_info.py +793 -792
  17. svg_ultralight/image_ops.py +156 -156
  18. svg_ultralight/inkscape.py +261 -261
  19. svg_ultralight/layout.py +290 -291
  20. svg_ultralight/main.py +183 -198
  21. svg_ultralight/metadata.py +122 -122
  22. svg_ultralight/nsmap.py +36 -36
  23. svg_ultralight/py.typed +5 -0
  24. svg_ultralight/query.py +254 -249
  25. svg_ultralight/read_svg.py +58 -0
  26. svg_ultralight/root_elements.py +87 -87
  27. svg_ultralight/string_conversion.py +244 -244
  28. svg_ultralight/strings/__init__.py +21 -13
  29. svg_ultralight/strings/svg_strings.py +106 -67
  30. svg_ultralight/transformations.py +140 -141
  31. svg_ultralight/unit_conversion.py +247 -248
  32. {svg_ultralight-0.48.0.dist-info → svg_ultralight-0.50.1.dist-info}/METADATA +208 -214
  33. svg_ultralight-0.50.1.dist-info/RECORD +34 -0
  34. svg_ultralight-0.50.1.dist-info/WHEEL +4 -0
  35. svg_ultralight-0.48.0.dist-info/RECORD +0 -34
  36. svg_ultralight-0.48.0.dist-info/WHEEL +0 -5
  37. svg_ultralight-0.48.0.dist-info/top_level.txt +0 -1
@@ -1,189 +1,189 @@
1
- """Helper functions for dealing with BoundElements.
2
-
3
- :author: Shay Hill
4
- :created: 2024-05-03
5
- """
6
-
7
- from __future__ import annotations
8
-
9
- from typing import TYPE_CHECKING
10
-
11
- from lxml import etree
12
- from lxml.etree import _Element as EtreeElement # pyright: ignore[reportPrivateUsage]
13
-
14
- from svg_ultralight.bounding_boxes.supports_bounds import SupportsBounds
15
- from svg_ultralight.bounding_boxes.type_bound_element import BoundElement
16
- from svg_ultralight.bounding_boxes.type_bounding_box import BoundingBox, HasBoundingBox
17
- from svg_ultralight.bounding_boxes.type_padded_text import PaddedText
18
- from svg_ultralight.constructors import new_element
19
- from svg_ultralight.layout import PadArg, expand_pad_arg
20
-
21
- if TYPE_CHECKING:
22
- import os
23
-
24
- from svg_ultralight.attrib_hints import ElemAttrib
25
- from svg_ultralight.bounding_boxes.supports_bounds import SupportsBounds
26
-
27
- _Matrix = tuple[float, float, float, float, float, float]
28
-
29
-
30
- def new_element_union(
31
- *elems: EtreeElement | SupportsBounds, **attributes: ElemAttrib
32
- ) -> EtreeElement:
33
- """Get the union of any elements found in the given arguments.
34
-
35
- :param elems: BoundElements, PaddedTexts, or EtreeElements.
36
- Other arguments will be ignored.
37
- :return: a new group element containing all elements.
38
-
39
- This does not support consolidating attributes. E.g., if all elements have the
40
- same fill color, this will not be recognized and consilidated into a single
41
- attribute for the group. Too many attributes change their behavior when applied
42
- to a group.
43
- """
44
- elements_found: list[EtreeElement] = []
45
- for elem in elems:
46
- if isinstance(elem, (BoundElement, PaddedText)):
47
- elements_found.append(elem.elem)
48
- elif isinstance(elem, EtreeElement):
49
- elements_found.append(elem)
50
-
51
- if not elements_found:
52
- msg = (
53
- "Cannot find any elements to union. "
54
- + "At least one argument must be a "
55
- + "BoundElement, PaddedText, or EtreeElement."
56
- )
57
- raise ValueError(msg)
58
- group = new_element("g", **attributes)
59
- group.extend(elements_found)
60
- return group
61
-
62
-
63
- def new_bbox_union(*blems: SupportsBounds | EtreeElement) -> BoundingBox:
64
- """Get the union of the bounding boxes of the given elements.
65
-
66
- :param blems: BoundElements, BoundingBoxes, or PaddedTexts.
67
- Other arguments will be ignored.
68
- :return: the union of all bounding boxes as a BoundingBox instance.
69
-
70
- Will used the padded_box attribute of PaddedText instances.
71
- """
72
- bboxes = [x.bbox for x in blems if isinstance(x, HasBoundingBox)]
73
- if not bboxes:
74
- msg = (
75
- "Cannot find any bounding boxes to union. "
76
- + "At least one argument must be a "
77
- + "BoundElement, BoundingBox, or PaddedText."
78
- )
79
- raise ValueError(msg)
80
-
81
- return BoundingBox.merged(*bboxes)
82
-
83
-
84
- def new_bound_union(*blems: SupportsBounds | EtreeElement) -> BoundElement:
85
- """Get the union of the bounding boxes of the given elements.
86
-
87
- :param blems: BoundElements or EtreeElements.
88
- At least one argument must be a BoundElement, BoundingBox, or PaddedText.
89
- :return: the union of all arguments as a BoundElement instance.
90
-
91
- Will used the padded_box attribute of PaddedText instances.
92
- """
93
- group = new_element_union(*blems)
94
- bbox = new_bbox_union(*blems)
95
- return BoundElement(group, bbox)
96
-
97
-
98
- def cut_bbox(
99
- bbox: SupportsBounds,
100
- *,
101
- x: float | None = None,
102
- y: float | None = None,
103
- x2: float | None = None,
104
- y2: float | None = None,
105
- ) -> BoundingBox:
106
- """Return a new bounding box with updated limits.
107
-
108
- :param bbox: the original bounding box or bounded element.
109
- :param x: the new x-coordinate.
110
- :param y: the new y-coordinate.
111
- :param x2: the new x2-coordinate.
112
- :param y2: the new y2-coordinate.
113
- :return: a new bounding box with the updated limits.
114
- """
115
- x = bbox.x if x is None else x
116
- y = bbox.y if y is None else y
117
- x2 = bbox.x2 if x2 is None else x2
118
- y2 = bbox.y2 if y2 is None else y2
119
- width = x2 - x
120
- height = y2 - y
121
- return BoundingBox(x, y, width, height)
122
-
123
-
124
- def pad_bbox(bbox: SupportsBounds, pad: PadArg) -> BoundingBox:
125
- """Return a new bounding box with padding.
126
-
127
- :param bbox: the original bounding box or bounded element.
128
- :param pad: the padding to apply.
129
- If a single number, the same padding will be applied to all sides.
130
- If a tuple, will be applied per css rules.
131
- len = 1 : 0, 0, 0, 0
132
- len = 2 : 0, 1, 0, 1
133
- len = 3 : 0, 1, 2, 1
134
- len = 4 : 0, 1, 2, 3
135
- :return: a new bounding box with padding applied.
136
- """
137
- top, right, bottom, left = expand_pad_arg(pad)
138
- return cut_bbox(
139
- bbox, x=bbox.x - left, y=bbox.y - top, x2=bbox.x2 + right, y2=bbox.y2 + bottom
140
- )
141
-
142
-
143
- def bbox_dict(bbox: SupportsBounds) -> dict[str, float]:
144
- """Return a dictionary representation of a bounding box.
145
-
146
- :param bbox: the bounding box or bound element from which to extract dimensions.
147
- :return: a dictionary with keys x, y, width, and height.
148
- """
149
- return {"x": bbox.x, "y": bbox.y, "width": bbox.width, "height": bbox.height}
150
-
151
-
152
- def new_bbox_rect(bbox: BoundingBox, **kwargs: float | str) -> EtreeElement:
153
- """Return a new rect element with the same dimensions as the bounding box.
154
-
155
- :param bbox: the bounding box or bound element from which to extract dimensions.
156
- :param kwargs: additional attributes for the rect element.
157
- """
158
- return new_element("rect", **bbox_dict(bbox), **kwargs)
159
-
160
-
161
- def _get_view_box(elem: EtreeElement) -> tuple[float, float, float, float]:
162
- """Return the view box of an element as a tuple of floats.
163
-
164
- :param elem: the element from which to extract the view box.
165
- :return: a tuple of floats representing the view box.
166
-
167
- This will work on svg files created by this library and some others. Not all svg
168
- files have a viewBox attribute.
169
- """
170
- view_box = elem.get("viewBox")
171
- if view_box is None:
172
- msg = "Element does not have a viewBox attribute."
173
- raise ValueError(msg)
174
- x, y, width, height = map(float, view_box.split())
175
- return x, y, width, height
176
-
177
-
178
- def parse_bound_element(svg_file: str | os.PathLike[str]) -> BoundElement:
179
- """Import an element as a BoundElement.
180
-
181
- :param elem: the element to import.
182
- :return: a BoundElement instance.
183
- """
184
- tree = etree.parse(svg_file)
185
- root = tree.getroot()
186
- elem = new_element("g")
187
- elem.extend(list(root))
188
- bbox = BoundingBox(*_get_view_box(root))
189
- return BoundElement(elem, bbox)
1
+ """Helper functions for dealing with BoundElements.
2
+
3
+ :author: Shay Hill
4
+ :created: 2024-05-03
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TYPE_CHECKING
10
+
11
+ from lxml import etree
12
+ from lxml.etree import _Element as EtreeElement # pyright: ignore[reportPrivateUsage]
13
+
14
+ from svg_ultralight.bounding_boxes.supports_bounds import SupportsBounds
15
+ from svg_ultralight.bounding_boxes.type_bound_element import BoundElement
16
+ from svg_ultralight.bounding_boxes.type_bounding_box import BoundingBox, HasBoundingBox
17
+ from svg_ultralight.bounding_boxes.type_padded_text import PaddedText
18
+ from svg_ultralight.constructors import new_element
19
+ from svg_ultralight.layout import PadArg, expand_pad_arg
20
+
21
+ if TYPE_CHECKING:
22
+ import os
23
+
24
+ from svg_ultralight.attrib_hints import ElemAttrib
25
+ from svg_ultralight.bounding_boxes.supports_bounds import SupportsBounds
26
+
27
+ _Matrix = tuple[float, float, float, float, float, float]
28
+
29
+
30
+ def new_element_union(
31
+ *elems: EtreeElement | SupportsBounds, **attributes: ElemAttrib
32
+ ) -> EtreeElement:
33
+ """Get the union of any elements found in the given arguments.
34
+
35
+ :param elems: BoundElements, PaddedTexts, or EtreeElements.
36
+ Other arguments will be ignored.
37
+ :return: a new group element containing all elements.
38
+
39
+ This does not support consolidating attributes. E.g., if all elements have the
40
+ same fill color, this will not be recognized and consilidated into a single
41
+ attribute for the group. Too many attributes change their behavior when applied
42
+ to a group.
43
+ """
44
+ elements_found: list[EtreeElement] = []
45
+ for elem in elems:
46
+ if isinstance(elem, (BoundElement, PaddedText)):
47
+ elements_found.append(elem.elem)
48
+ elif isinstance(elem, EtreeElement):
49
+ elements_found.append(elem)
50
+
51
+ if not elements_found:
52
+ msg = (
53
+ "Cannot find any elements to union. "
54
+ + "At least one argument must be a "
55
+ + "BoundElement, PaddedText, or EtreeElement."
56
+ )
57
+ raise ValueError(msg)
58
+ group = new_element("g", **attributes)
59
+ group.extend(elements_found)
60
+ return group
61
+
62
+
63
+ def new_bbox_union(*blems: SupportsBounds | EtreeElement) -> BoundingBox:
64
+ """Get the union of the bounding boxes of the given elements.
65
+
66
+ :param blems: BoundElements, BoundingBoxes, or PaddedTexts.
67
+ Other arguments will be ignored.
68
+ :return: the union of all bounding boxes as a BoundingBox instance.
69
+
70
+ Will used the padded_box attribute of PaddedText instances.
71
+ """
72
+ bboxes = [x.bbox for x in blems if isinstance(x, HasBoundingBox)]
73
+ if not bboxes:
74
+ msg = (
75
+ "Cannot find any bounding boxes to union. "
76
+ + "At least one argument must be a "
77
+ + "BoundElement, BoundingBox, or PaddedText."
78
+ )
79
+ raise ValueError(msg)
80
+
81
+ return BoundingBox.merged(*bboxes)
82
+
83
+
84
+ def new_bound_union(*blems: SupportsBounds | EtreeElement) -> BoundElement:
85
+ """Get the union of the bounding boxes of the given elements.
86
+
87
+ :param blems: BoundElements or EtreeElements.
88
+ At least one argument must be a BoundElement, BoundingBox, or PaddedText.
89
+ :return: the union of all arguments as a BoundElement instance.
90
+
91
+ Will used the padded_box attribute of PaddedText instances.
92
+ """
93
+ group = new_element_union(*blems)
94
+ bbox = new_bbox_union(*blems)
95
+ return BoundElement(group, bbox)
96
+
97
+
98
+ def cut_bbox(
99
+ bbox: SupportsBounds,
100
+ *,
101
+ x: float | None = None,
102
+ y: float | None = None,
103
+ x2: float | None = None,
104
+ y2: float | None = None,
105
+ ) -> BoundingBox:
106
+ """Return a new bounding box with updated limits.
107
+
108
+ :param bbox: the original bounding box or bounded element.
109
+ :param x: the new x-coordinate.
110
+ :param y: the new y-coordinate.
111
+ :param x2: the new x2-coordinate.
112
+ :param y2: the new y2-coordinate.
113
+ :return: a new bounding box with the updated limits.
114
+ """
115
+ x = bbox.x if x is None else x
116
+ y = bbox.y if y is None else y
117
+ x2 = bbox.x2 if x2 is None else x2
118
+ y2 = bbox.y2 if y2 is None else y2
119
+ width = x2 - x
120
+ height = y2 - y
121
+ return BoundingBox(x, y, width, height)
122
+
123
+
124
+ def pad_bbox(bbox: SupportsBounds, pad: PadArg) -> BoundingBox:
125
+ """Return a new bounding box with padding.
126
+
127
+ :param bbox: the original bounding box or bounded element.
128
+ :param pad: the padding to apply.
129
+ If a single number, the same padding will be applied to all sides.
130
+ If a tuple, will be applied per css rules.
131
+ len = 1 : 0, 0, 0, 0
132
+ len = 2 : 0, 1, 0, 1
133
+ len = 3 : 0, 1, 2, 1
134
+ len = 4 : 0, 1, 2, 3
135
+ :return: a new bounding box with padding applied.
136
+ """
137
+ top, right, bottom, left = expand_pad_arg(pad)
138
+ return cut_bbox(
139
+ bbox, x=bbox.x - left, y=bbox.y - top, x2=bbox.x2 + right, y2=bbox.y2 + bottom
140
+ )
141
+
142
+
143
+ def bbox_dict(bbox: SupportsBounds) -> dict[str, float]:
144
+ """Return a dictionary representation of a bounding box.
145
+
146
+ :param bbox: the bounding box or bound element from which to extract dimensions.
147
+ :return: a dictionary with keys x, y, width, and height.
148
+ """
149
+ return {"x": bbox.x, "y": bbox.y, "width": bbox.width, "height": bbox.height}
150
+
151
+
152
+ def new_bbox_rect(bbox: BoundingBox, **kwargs: float | str) -> EtreeElement:
153
+ """Return a new rect element with the same dimensions as the bounding box.
154
+
155
+ :param bbox: the bounding box or bound element from which to extract dimensions.
156
+ :param kwargs: additional attributes for the rect element.
157
+ """
158
+ return new_element("rect", **bbox_dict(bbox), **kwargs)
159
+
160
+
161
+ def _get_view_box(elem: EtreeElement) -> tuple[float, float, float, float]:
162
+ """Return the view box of an element as a tuple of floats.
163
+
164
+ :param elem: the element from which to extract the view box.
165
+ :return: a tuple of floats representing the view box.
166
+
167
+ This will work on svg files created by this library and some others. Not all svg
168
+ files have a viewBox attribute.
169
+ """
170
+ view_box = elem.get("viewBox")
171
+ if view_box is None:
172
+ msg = "Element does not have a viewBox attribute."
173
+ raise ValueError(msg)
174
+ x, y, width, height = map(float, view_box.split())
175
+ return x, y, width, height
176
+
177
+
178
+ def parse_bound_element(svg_file: str | os.PathLike[str]) -> BoundElement:
179
+ """Import an element as a BoundElement.
180
+
181
+ :param elem: the element to import.
182
+ :return: a BoundElement instance.
183
+ """
184
+ tree = etree.parse(svg_file)
185
+ root = tree.getroot()
186
+ elem = new_element("g")
187
+ elem.extend(list(root))
188
+ bbox = BoundingBox(*_get_view_box(root))
189
+ return BoundElement(elem, bbox)