svg-ultralight 0.25.0__py3-none-any.whl → 0.27.0__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.

@@ -4,6 +4,11 @@
4
4
  :created: 12/22/2019.
5
5
  """
6
6
 
7
+ from svg_ultralight.bounding_boxes.bound_helpers import (
8
+ new_bbox_union,
9
+ new_bound_union,
10
+ new_element_union,
11
+ )
7
12
  from svg_ultralight.bounding_boxes.supports_bounds import SupportsBounds
8
13
  from svg_ultralight.bounding_boxes.type_bound_element import BoundElement
9
14
  from svg_ultralight.bounding_boxes.type_bounding_box import BoundingBox
@@ -27,6 +32,7 @@ from svg_ultralight.nsmap import NSMAP, new_qname
27
32
  from svg_ultralight.query import pad_text
28
33
  from svg_ultralight.root_elements import new_svg_root_around_bounds
29
34
  from svg_ultralight.string_conversion import (
35
+ format_attr_dict,
30
36
  format_number,
31
37
  format_numbers,
32
38
  format_numbers_in_string,
@@ -39,10 +45,14 @@ __all__ = [
39
45
  "PaddedText",
40
46
  "SupportsBounds",
41
47
  "deepcopy_element",
48
+ "format_attr_dict",
42
49
  "format_number",
43
50
  "format_numbers",
44
51
  "format_numbers_in_string",
52
+ "new_bbox_union",
53
+ "new_bound_union",
45
54
  "new_element",
55
+ "new_element_union",
46
56
  "new_metadata",
47
57
  "new_qname",
48
58
  "new_sub_element",
@@ -0,0 +1,95 @@
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.etree import _Element as EtreeElement # type: ignore
12
+
13
+ from svg_ultralight.bounding_boxes.type_bound_element import BoundElement
14
+ from svg_ultralight.bounding_boxes.type_bounding_box import BoundingBox
15
+ from svg_ultralight.bounding_boxes.type_padded_text import PaddedText
16
+ from svg_ultralight.constructors import new_element
17
+
18
+ if TYPE_CHECKING:
19
+ from svg_ultralight.bounding_boxes.supports_bounds import SupportsBounds
20
+
21
+
22
+ def new_element_union(
23
+ *elems: EtreeElement | SupportsBounds, **attributes: float | str
24
+ ) -> EtreeElement:
25
+ """Get the union of any elements found in the given arguments.
26
+
27
+ :param elems: BoundElements, PaddedTexts, or EtreeElements.
28
+ Other arguments will be ignored.
29
+ :return: a new group element containing all elements.
30
+
31
+ This does not support consolidating attributes. E.g., if all elements have the
32
+ same fill color, this will not be recognized and consilidated into a single
33
+ attribute for the group. Too many attributes change their behavior when applied
34
+ to a group.
35
+ """
36
+ elements_found: list[EtreeElement] = []
37
+ for elem in elems:
38
+ if isinstance(elem, (BoundElement, PaddedText)):
39
+ elements_found.append(elem.elem)
40
+ elif isinstance(elem, EtreeElement):
41
+ elements_found.append(elem)
42
+
43
+ if not elements_found:
44
+ msg = (
45
+ "Cannot find any elements to union. "
46
+ + "At least one argument must be a "
47
+ + "BoundElement, PaddedText, or EtreeElement."
48
+ )
49
+ raise ValueError(msg)
50
+ group = new_element("g", **attributes)
51
+ group.extend(elements_found)
52
+ return group
53
+
54
+
55
+ def new_bbox_union(*blems: SupportsBounds | EtreeElement) -> BoundingBox:
56
+ """Get the union of the bounding boxes of the given elements.
57
+
58
+ :param blems: BoundElements, BoundingBoxes, or PaddedTexts.
59
+ Other arguments will be ignored.
60
+ :return: the union of all bounding boxes as a BoundingBox instance.
61
+
62
+ Will used the padded_box attribute of PaddedText instances.
63
+ """
64
+ bboxes: list[BoundingBox] = []
65
+ for blem in blems:
66
+ if isinstance(blem, BoundingBox):
67
+ bboxes.append(blem)
68
+ elif isinstance(blem, BoundElement):
69
+ bboxes.append(blem.bbox)
70
+ elif isinstance(blem, PaddedText):
71
+ bboxes.append(blem.padded_bbox)
72
+
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)
@@ -6,7 +6,6 @@
6
6
 
7
7
  from __future__ import annotations
8
8
 
9
- import warnings
10
9
  from dataclasses import dataclass
11
10
 
12
11
  from svg_ultralight.bounding_boxes.supports_bounds import SupportsBounds
@@ -286,12 +285,6 @@ class BoundingBox(SupportsBounds):
286
285
  :return: a bounding box around self and other bounding boxes
287
286
  :raises DeprecationWarning:
288
287
  """
289
- warnings.warn(
290
- "Method a.merge(b, c) is deprecated. "
291
- + "Use classmethod BoundingBox.merged(a, b, c) instead.",
292
- category=DeprecationWarning,
293
- stacklevel=1,
294
- )
295
288
  return BoundingBox.merged(self, *others)
296
289
 
297
290
  @classmethod
@@ -8,9 +8,8 @@ from __future__ import annotations
8
8
 
9
9
  from typing import TYPE_CHECKING
10
10
 
11
- from svg_ultralight.bounding_boxes.type_bound_element import BoundElement
11
+ from svg_ultralight.bounding_boxes import bound_helpers as bound
12
12
  from svg_ultralight.bounding_boxes.type_bounding_box import BoundingBox
13
- from svg_ultralight.bounding_boxes.type_padded_text import PaddedText
14
13
  from svg_ultralight.main import new_svg_root
15
14
 
16
15
  if TYPE_CHECKING:
@@ -64,15 +63,8 @@ def new_svg_root_around_bounds(
64
63
  :return: root svg element
65
64
  :raise ValueError: if no bounding boxes are found in bounded
66
65
  """
67
- bboxes = [x for x in bounded if isinstance(x, BoundingBox)]
68
- bboxes += [x.bbox for x in bounded if isinstance(x, BoundElement)]
69
- bboxes += [x.padded_bbox for x in bounded if isinstance(x, PaddedText)]
70
-
71
- if not bboxes:
72
- msg = "no bounding boxes found"
73
- raise ValueError(msg)
74
-
75
- viewbox = _viewbox_args_from_bboxes(*bboxes)
66
+ bbox = bound.new_bbox_union(*bounded)
67
+ viewbox = _viewbox_args_from_bboxes(bbox)
76
68
  return new_svg_root(
77
69
  x_=viewbox["x_"],
78
70
  y_=viewbox["y_"],
@@ -79,15 +79,18 @@ def format_numbers_in_string(data: float | str) -> str:
79
79
  :return: string with floats formatted to limited precision
80
80
 
81
81
  Works as a more robust version of format_number. Will correctly handle input
82
- floats in exponential notation. This should work for and parameter value in an
83
- svg except 'text'. The function will fail with input strings like
84
- 'ice3.14bucket', because 'e3.14' will be identified as a float. SVG param values
85
- will not have such strings, but the 'text' attribute could. This function will
86
- not handle that case. Do not attempt to reformat 'text' attribute values.
82
+ floats in exponential notation. This should work for any parameter value in an
83
+ svg except 'text', 'id' and for any other value except hex color codes. The
84
+ function will fail with input strings like 'ice3.14bucket', because 'e3.14' will
85
+ be identified as a float. SVG param values will not have such strings, but the
86
+ 'text' attribute could. This function will not handle that case. Do not attempt
87
+ to reformat 'text' attribute values.
87
88
  """
88
89
  with suppress(ValueError):
89
90
  # try as a regular number to strip spaces from simple float strings
90
91
  return format_number(data)
92
+ if str(data).startswith("#"):
93
+ return str(data)
91
94
  words = re.split(r"([^\d.eE-]+)", str(data))
92
95
  words = [format_number(w) if _is_float_or_float_str(w) else w for w in words]
93
96
  return "".join(words)
@@ -123,7 +126,7 @@ def _fix_key_and_format_val(key: str, val: str | float) -> tuple[str, str]:
123
126
  else:
124
127
  key_ = key.rstrip("_").replace("_", "-")
125
128
 
126
- if key_ == "text":
129
+ if key_ in {"id", "text"}:
127
130
  return key_, str(val)
128
131
 
129
132
  return key_, format_numbers_in_string(val)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: svg-ultralight
3
- Version: 0.25.0
3
+ Version: 0.27.0
4
4
  Summary: a sensible way to create svg files with Python
5
5
  Author-email: Shay Hill <shay_public@hotmail.com>
6
6
  License: MIT
@@ -1,4 +1,4 @@
1
- svg_ultralight/__init__.py,sha256=5n-7C9idISTR56-AqZ834DpmYGgPUBdtFK7iIekiCqc,1627
1
+ svg_ultralight/__init__.py,sha256=Xyi-aBOAfVNoUg9rHXYVMELehe1kReEyQYL682AcQ1A,1877
2
2
  svg_ultralight/animate.py,sha256=JSrBm-59BcNXDF0cGgl4-C89eBunjevZnwZxIWt48TU,1112
3
3
  svg_ultralight/inkscape.py,sha256=M8yTxXOu4NlXnhsMycvEJiIDpnDeiZ_bZakJBM38ZoU,9152
4
4
  svg_ultralight/layout.py,sha256=TTETT_8WLBXnQxDGXdAeczCFN5pFo5kKY3Q6zv4FPX4,12238
@@ -7,19 +7,20 @@ svg_ultralight/metadata.py,sha256=Mxgxrxe1Ar4kp2wTT29aadxgHNFaaNLABo29jStiWDg,42
7
7
  svg_ultralight/nsmap.py,sha256=y63upO78Rr-JJT56RWWZuyrsILh6HPoY4GhbYnK1A0g,1244
8
8
  svg_ultralight/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  svg_ultralight/query.py,sha256=PFyhR9v60JkuksRb0LVkml67nmOxfZW9eCtL-JoH52Y,7270
10
- svg_ultralight/root_elements.py,sha256=rUc26ec-FLAUgO5iOF3aOeKZC7CjiTw7TGyGAj33yNU,3219
11
- svg_ultralight/string_conversion.py,sha256=bIzhzF_T92VfceNAz27WmLP1YSSgq__-YCF25Dm0D0I,7226
10
+ svg_ultralight/root_elements.py,sha256=pt9J6mPrnoTAZVF6vKTZoM_o947I8UCj6MbGcD2JUCk,2869
11
+ svg_ultralight/string_conversion.py,sha256=WEmpf75RJmJ2lfJluagAz2wPsz6wM8XvTEwkq4U0vEc,7353
12
12
  svg_ultralight/unit_conversion.py,sha256=g07nhzXdjPvGcJmkhLdFbeDLrSmbI8uFoVgPo7G62Bg,9258
13
13
  svg_ultralight/bounding_boxes/__init__.py,sha256=qUEn3r4s-1QNHaguhWhhaNfdP4tl_B6YEqxtiTFuzhQ,78
14
+ svg_ultralight/bounding_boxes/bound_helpers.py,sha256=D-Qp8yDmn5vIBdaSlBvaWR0F_iRCdotgQqjaRHXBq-8,3397
14
15
  svg_ultralight/bounding_boxes/supports_bounds.py,sha256=1yqmZ7PH1bBH-LTIUDzSvKFXkPLXfJM7jJNz0bXurZ4,3926
15
16
  svg_ultralight/bounding_boxes/type_bound_element.py,sha256=VKiN4UnC2XlPKapWRHxgtqhO4BuVoe6YkLrirlEP09w,5714
16
- svg_ultralight/bounding_boxes/type_bounding_box.py,sha256=9qhbhVCjwBXTeo7f7nNKJH5LXliQBJb80r8mXtVuUgc,10622
17
+ svg_ultralight/bounding_boxes/type_bounding_box.py,sha256=OqvS6LhfMwnoE4PUwAmHlY0tjY-2YQZQiLY6O2U3vfs,10378
17
18
  svg_ultralight/bounding_boxes/type_padded_text.py,sha256=FnwHD54qPMlyOB0yhRgZShQWA_riaaTS9GwD9HnbPm4,14119
18
19
  svg_ultralight/constructors/__init__.py,sha256=YcnO0iBQc19aL8Iemw0Y452MBMBIT2AN5nZCnoGxpn0,327
19
20
  svg_ultralight/constructors/new_element.py,sha256=VtMz9sPn9rMk6rui5Poysy3vezlOaS-tGIcGbu-SXmY,3406
20
21
  svg_ultralight/strings/__init__.py,sha256=Zalrf-ThFz7b7xKELx5lb2gOlBgV-6jk_k_EeSdVCVk,295
21
22
  svg_ultralight/strings/svg_strings.py,sha256=RYKMxOHq9abbZyGcFqsElBGLrBX-EjjNxln3s_ibi30,1296
22
- svg_ultralight-0.25.0.dist-info/METADATA,sha256=_VUmlE9QVt_qiMGAn8OlUAki-KpIJOyDjoO94_Nl7Xo,8871
23
- svg_ultralight-0.25.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
24
- svg_ultralight-0.25.0.dist-info/top_level.txt,sha256=se-6yqM_0Yg5orJKvKWdjQZ4iR4G_EjhL7oRgju-fdY,15
25
- svg_ultralight-0.25.0.dist-info/RECORD,,
23
+ svg_ultralight-0.27.0.dist-info/METADATA,sha256=6GeOT7ohzqEuzvgofE7pKbI2FnP9H_DRfwV4pNfb5K4,8871
24
+ svg_ultralight-0.27.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
25
+ svg_ultralight-0.27.0.dist-info/top_level.txt,sha256=se-6yqM_0Yg5orJKvKWdjQZ4iR4G_EjhL7oRgju-fdY,15
26
+ svg_ultralight-0.27.0.dist-info/RECORD,,