svg-ultralight 0.30.1__py3-none-any.whl → 0.32.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.

@@ -38,7 +38,12 @@ from svg_ultralight.string_conversion import (
38
38
  format_numbers,
39
39
  format_numbers_in_string,
40
40
  )
41
- from svg_ultralight.transformations import transform_element
41
+ from svg_ultralight.transformations import (
42
+ mat_apply,
43
+ mat_dot,
44
+ mat_invert,
45
+ transform_element,
46
+ )
42
47
 
43
48
  __all__ = [
44
49
  "BoundCollection",
@@ -52,6 +57,9 @@ __all__ = [
52
57
  "format_number",
53
58
  "format_numbers",
54
59
  "format_numbers_in_string",
60
+ "mat_apply",
61
+ "mat_dot",
62
+ "mat_invert",
55
63
  "new_bbox_union",
56
64
  "new_bound_union",
57
65
  "new_element",
@@ -0,0 +1,50 @@
1
+ """Import an svg file as a BoundElement.
2
+
3
+ :author: Shay Hill
4
+ :created: 2024-05-28
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TYPE_CHECKING
10
+
11
+ from lxml import etree
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.constructors import new_element
16
+
17
+ if TYPE_CHECKING:
18
+ import os
19
+
20
+ from lxml.etree import _Element as EtreeElement # type: ignore
21
+
22
+
23
+ def _get_bounds_from_viewbox(root: EtreeElement) -> BoundingBox:
24
+ """Get the BoundingBox from the viewbox attribute of the root element.
25
+
26
+ :param root: The root element of the svg.
27
+ :return: The BoundingBox of the svg.
28
+ """
29
+ viewbox = root.attrib.get("viewBox")
30
+ if viewbox is None:
31
+ msg = "SVG file has no viewBox attribute. Failed to create BoundingBox."
32
+ raise ValueError(msg)
33
+ x, y, width, height = map(float, viewbox.split())
34
+ return BoundingBox(x, y, width, height)
35
+
36
+
37
+ def import_svg(svg: str | os.PathLike[str]) -> BoundElement:
38
+ """Import an svg file as a BoundElement.
39
+
40
+ :param svg: The path to the svg file.
41
+ :return: The BoundElement representation of the svg.
42
+
43
+ The viewbox of the svg is used to create the BoundingBox of the BoundElement.
44
+ """
45
+ tree = etree.parse(svg)
46
+ root = tree.getroot()
47
+ bbox = _get_bounds_from_viewbox(root)
48
+ root_as_elem = new_element("g")
49
+ root_as_elem.extend(root)
50
+ return BoundElement(root_as_elem, bbox)
svg_ultralight/layout.py CHANGED
@@ -99,7 +99,7 @@ def _infer_scale(
99
99
  * print_h == 0 / viewbox_h > 0
100
100
 
101
101
  The print area is invalid, but there is special handling for this. Interpret
102
- viewbox units as print_w.native_unit and determe print area from viewbox area 1
102
+ viewbox units as print_w.native_unit and determine print area from viewbox area 1
103
103
  to 1.
104
104
 
105
105
  >>> _infer_scale(Measurement("in"), Measurement("in"), 1, 2)
@@ -59,6 +59,20 @@ def mat_apply(mat1: _Matrix, mat2: tuple[float, float]) -> tuple[float, float]:
59
59
  return mat1[0] * mat2[0] + mat1[4], mat1[3] * mat2[1] + mat1[5]
60
60
 
61
61
 
62
+ def mat_invert(tmat: _Matrix) -> _Matrix:
63
+ """Invert a 2D transformation matrix in svg format."""
64
+ a, b, c, d, e, f = tmat
65
+ det = a * d - b * c
66
+ return (
67
+ d / det,
68
+ -b / det,
69
+ -c / det,
70
+ a / det,
71
+ (c * f - d * e) / det,
72
+ (b * e - a * f) / det,
73
+ )
74
+
75
+
62
76
  def get_transform_matrix(elem: EtreeElement) -> _Matrix:
63
77
  """Get the transformation matrix from an svg element.
64
78
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: svg-ultralight
3
- Version: 0.30.1
3
+ Version: 0.32.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,7 +1,8 @@
1
- svg_ultralight/__init__.py,sha256=5lSOJM2dshB_cG3uNZbCp0q114mJ6wgJ59Kg4TYiiBU,2070
1
+ svg_ultralight/__init__.py,sha256=iuI06A3lFqv0L7EoUfaKqYDg-z0HUlxPATBavtOmexs,2181
2
2
  svg_ultralight/animate.py,sha256=fE-zRU_uFrZIL9W78fcGz7qmrts8fz5UoWEy7b4xb44,1144
3
+ svg_ultralight/import_svg.py,sha256=TVLbinch2g0RN-OlWyGXoQEO6vc2I_LsMXpOchwTJzE,1548
3
4
  svg_ultralight/inkscape.py,sha256=M8yTxXOu4NlXnhsMycvEJiIDpnDeiZ_bZakJBM38ZoU,9152
4
- svg_ultralight/layout.py,sha256=TTETT_8WLBXnQxDGXdAeczCFN5pFo5kKY3Q6zv4FPX4,12238
5
+ svg_ultralight/layout.py,sha256=RBFJl6cIN6BYqXea5powEl2A4ZIxUJ0mtVoURkSZy_E,12240
5
6
  svg_ultralight/main.py,sha256=6oNkZfD27UMdP-oYqp5agS_IGcYb8NkUZwM9Zdyb3SA,7287
6
7
  svg_ultralight/metadata.py,sha256=Mxgxrxe1Ar4kp2wTT29aadxgHNFaaNLABo29jStiWDg,4201
7
8
  svg_ultralight/nsmap.py,sha256=y63upO78Rr-JJT56RWWZuyrsILh6HPoY4GhbYnK1A0g,1244
@@ -9,7 +10,7 @@ svg_ultralight/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
10
  svg_ultralight/query.py,sha256=_KQuk4IwhVVDgTT0GZm_gbqNUGp7lMEDM32b_CTTSUA,7320
10
11
  svg_ultralight/root_elements.py,sha256=pt9J6mPrnoTAZVF6vKTZoM_o947I8UCj6MbGcD2JUCk,2869
11
12
  svg_ultralight/string_conversion.py,sha256=WEmpf75RJmJ2lfJluagAz2wPsz6wM8XvTEwkq4U0vEc,7353
12
- svg_ultralight/transformations.py,sha256=CT43zaWSmEoWf-rQpFgP_1KX9wL6AzpR0cF8E1fIhAU,3625
13
+ svg_ultralight/transformations.py,sha256=64BN_UupvOZk4nbiKLKv09P3j0TPFhH6mKro_3QBqeY,3945
13
14
  svg_ultralight/unit_conversion.py,sha256=g07nhzXdjPvGcJmkhLdFbeDLrSmbI8uFoVgPo7G62Bg,9258
14
15
  svg_ultralight/bounding_boxes/__init__.py,sha256=qUEn3r4s-1QNHaguhWhhaNfdP4tl_B6YEqxtiTFuzhQ,78
15
16
  svg_ultralight/bounding_boxes/bound_helpers.py,sha256=YMClhdekeYbzD_ijXDAer-H3moWKN3lUNnZs1UNFFKc,3458
@@ -22,7 +23,7 @@ svg_ultralight/constructors/__init__.py,sha256=YcnO0iBQc19aL8Iemw0Y452MBMBIT2AN5
22
23
  svg_ultralight/constructors/new_element.py,sha256=VtMz9sPn9rMk6rui5Poysy3vezlOaS-tGIcGbu-SXmY,3406
23
24
  svg_ultralight/strings/__init__.py,sha256=Zalrf-ThFz7b7xKELx5lb2gOlBgV-6jk_k_EeSdVCVk,295
24
25
  svg_ultralight/strings/svg_strings.py,sha256=RYKMxOHq9abbZyGcFqsElBGLrBX-EjjNxln3s_ibi30,1296
25
- svg_ultralight-0.30.1.dist-info/METADATA,sha256=j2bSJ8HE0Y3JZv7S5ur92kpmbMd7FVeDe56aB9o3VTU,8871
26
- svg_ultralight-0.30.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
27
- svg_ultralight-0.30.1.dist-info/top_level.txt,sha256=se-6yqM_0Yg5orJKvKWdjQZ4iR4G_EjhL7oRgju-fdY,15
28
- svg_ultralight-0.30.1.dist-info/RECORD,,
26
+ svg_ultralight-0.32.0.dist-info/METADATA,sha256=9WclLO21GeJDItp9VbWfeYHgzJ60q-w0-aEjjmnPf6A,8871
27
+ svg_ultralight-0.32.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
28
+ svg_ultralight-0.32.0.dist-info/top_level.txt,sha256=se-6yqM_0Yg5orJKvKWdjQZ4iR4G_EjhL7oRgju-fdY,15
29
+ svg_ultralight-0.32.0.dist-info/RECORD,,