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

@@ -383,7 +383,7 @@ class FTTextInfo:
383
383
  @property
384
384
  def bpad(self) -> float:
385
385
  """Return the bottom padding for the text."""
386
- return self.descent - self.bbox.y2
386
+ return -self.descent - self.bbox.y2
387
387
 
388
388
  @property
389
389
  def lpad(self) -> float:
@@ -433,8 +433,8 @@ def get_padded_text_info(
433
433
  :param font_size: the font size to use.
434
434
  :param ascent: the ascent of the font. If not provided, it will be calculated
435
435
  from the font file.
436
- :param descent: the descent of the font. If not provided, it will be calculated
437
- from the font file.
436
+ :param descent: the descent of the font, usually a negative number. If not
437
+ provided, it will be calculated from the font file.
438
438
  :param y_bounds_reference: optional character or string to use as a reference
439
439
  for the ascent and descent. If provided, the ascent and descent will be the y
440
440
  extents of the capline reference. This argument is provided to mimic the
@@ -447,7 +447,7 @@ def get_padded_text_info(
447
447
  if y_bounds_reference:
448
448
  capline_info = FTTextInfo(font_info, y_bounds_reference, font_size)
449
449
  ascent = -capline_info.bbox.y
450
- descent = capline_info.bbox.y2
450
+ descent = -capline_info.bbox.y2
451
451
 
452
452
  return FTTextInfo(font_info, text, font_size, ascent, descent)
453
453
 
@@ -27,10 +27,88 @@ if TYPE_CHECKING:
27
27
  )
28
28
 
29
29
 
30
- def format_number(num: float | str) -> str:
30
+ _MAYBE_NEG = r"(?:(?P<negative>-?))"
31
+ _MAYBE_INT = r"(?:(?P<integer>\d+?))"
32
+ _MAYBE_FRACTION = r"(?:\.(?P<fraction>\d+))?"
33
+ _MAYBE_EXP = r"(?:[eE](?P<exponent>[+-]?\d+))?"
34
+
35
+ # Split a float (fp or exponential) into its components. All components are optional.
36
+ FLOAT_PATTERN = re.compile(rf"{_MAYBE_NEG}{_MAYBE_INT}{_MAYBE_FRACTION}{_MAYBE_EXP}")
37
+
38
+
39
+ def _split_float_str(num: str | float) -> tuple[str, str, str, int]:
40
+ """Split a float string into its sign, integer part, fractional part, and exponent.
41
+
42
+ :param num_str: A string representing the number (e.g., '1.23e+03').
43
+ :return: A tuple containing the integer part, fractional part, and exponent.
44
+ """
45
+ if float(num) == 0:
46
+ return "", "", "", 0
47
+ num_str = str(num)
48
+ groups = FLOAT_PATTERN.fullmatch(num_str)
49
+ if not groups:
50
+ msg = "Invalid number string: {num_str}."
51
+ raise ValueError(msg.format(num_str=num_str))
52
+
53
+ sign = groups["negative"] or ""
54
+ integer = (groups["integer"] or "").lstrip("0")
55
+ fraction = (groups["fraction"] or "").rstrip("0")
56
+ exponent = int(groups["exponent"] or 0)
57
+ return sign, integer, fraction, exponent
58
+
59
+
60
+ def _format_as_fixed_point(num: str | float) -> str:
61
+ """Format a number in fixed-point notation.
62
+
63
+ :param exp_str: A string representing the number in exponential notation
64
+ (e.g., '1.23e+03') or just a number.
65
+ :return: A string representing the number in fixed-point notation.
66
+ """
67
+ sign, integer, fraction, exponent = _split_float_str(num)
68
+ if exponent > 0:
69
+ fraction = fraction.ljust(exponent, "0")
70
+ integer += fraction[:exponent]
71
+ fraction = fraction[exponent:]
72
+ elif exponent < 0:
73
+ integer = integer.rjust(-exponent, "0")
74
+ fraction = integer[exponent:] + fraction
75
+ integer = integer[:exponent]
76
+
77
+ fraction = "." + fraction if fraction else ""
78
+ return f"{sign}{integer}{fraction}" or "0"
79
+
80
+
81
+ def _format_as_exponential(num: str | float) -> str:
82
+ """Convert a number in fixed-point notation (as a string) to exponential notation.
83
+
84
+ :param num_str: A string representing the number in fixed-point notation
85
+ (e.g., '123000') or just a number.
86
+ :return: A string representing the number in exponential notation.
87
+ """
88
+ sign, integer, fraction, exponent = _split_float_str(num)
89
+ if len(integer) > 1:
90
+ exponent += len(integer) - 1
91
+ fraction = (integer[1:] + fraction).rstrip("0")
92
+ integer = integer[0]
93
+ elif not integer and fraction:
94
+ leading_zeroes = len(fraction) - len(fraction.lstrip("0"))
95
+ exponent -= leading_zeroes + 1
96
+ integer = fraction[leading_zeroes]
97
+ fraction = fraction[leading_zeroes + 1 :]
98
+
99
+ fraction = "." + fraction if fraction else ""
100
+ exp_str = f"e{exponent}" if exponent else ""
101
+ return f"{sign}{integer}{fraction}{exp_str}" or "0"
102
+
103
+
104
+ def format_number(num: float | str, precision: float | None = 6) -> str:
31
105
  """Format strings at limited precision.
32
106
 
33
107
  :param num: anything that can print as a float.
108
+ :param precision: number of digits after the decimal point, default 6. You can
109
+ also pass None for no precision limit. This may produce some long strings,
110
+ but will retain as much information as possible when converting between
111
+ floats and strings.
34
112
  :return: str
35
113
 
36
114
  I've read articles that recommend no more than four digits before and two digits
@@ -38,15 +116,20 @@ def format_number(num: float | str) -> str:
38
116
  giving six. Mostly to eliminate exponential notation, but I'm "rstripping" the
39
117
  strings to reduce filesize and increase readability
40
118
 
41
- * reduce fp precision to 6 digits
119
+ * reduce fp precision to (default) 6 digits
42
120
  * remove trailing zeros
43
121
  * remove trailing decimal point
122
+ * remove leading 0 in "0.123"
44
123
  * convert "-0" to "0"
124
+ * use shorter of exponential or fixed-point notation
45
125
  """
46
- as_str = f"{float(num):0.6f}".rstrip("0").rstrip(".")
47
- if as_str == "-0":
48
- as_str = "0"
49
- return as_str
126
+ if precision is not None:
127
+ num = f"{float(num):.{precision}f}"
128
+ exponential_str = _format_as_exponential(num)
129
+ fixed_point_str = _format_as_fixed_point(num)
130
+ if len(exponential_str) < len(fixed_point_str):
131
+ return exponential_str
132
+ return fixed_point_str
50
133
 
51
134
 
52
135
  def format_numbers(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: svg-ultralight
3
- Version: 0.40.1
3
+ Version: 0.42.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
@@ -9,7 +9,7 @@ svg_ultralight/nsmap.py,sha256=y63upO78Rr-JJT56RWWZuyrsILh6HPoY4GhbYnK1A0g,1244
9
9
  svg_ultralight/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  svg_ultralight/query.py,sha256=iFFsK78TuUT6h3iv_V8_fwCggIAdYa97K7oOPhhoPuY,9711
11
11
  svg_ultralight/root_elements.py,sha256=E_H7HXk0M5F3IyFVOxO8PQmhww1-sHTzJhx8hBJPZvg,2911
12
- svg_ultralight/string_conversion.py,sha256=ZUehoKh1lzys1jqRDIhHWfpxi4TSUhNFHiQl57OCAr8,10324
12
+ svg_ultralight/string_conversion.py,sha256=U5IBhG05DtfSkJlbQ7BKHHbob78CHG6cF_Yr1EdB20U,13829
13
13
  svg_ultralight/transformations.py,sha256=T3vSxcTWOwWnwu3OF610LHMbKScUIVWICUAvru5zLnU,4488
14
14
  svg_ultralight/unit_conversion.py,sha256=g07nhzXdjPvGcJmkhLdFbeDLrSmbI8uFoVgPo7G62Bg,9258
15
15
  svg_ultralight/bounding_boxes/__init__.py,sha256=qUEn3r4s-1QNHaguhWhhaNfdP4tl_B6YEqxtiTFuzhQ,78
@@ -24,12 +24,11 @@ svg_ultralight/constructors/__init__.py,sha256=XLOInLhzMERWNnFAs-itMs-OZrBOpvQth
24
24
  svg_ultralight/constructors/new_element.py,sha256=hRUW2hR_BTkthEqPClYV7-IeFe9iv2zwb6ehp1k1xDk,3475
25
25
  svg_ultralight/font_tools/__init__.py,sha256=NX3C0vvoB-G4S-h1f0NLWePjYAMMR37D1cl_G4WBjHc,83
26
26
  svg_ultralight/font_tools/comp_results.py,sha256=iEDbExO3D7ffo0NZxrqf-WjtzUCJZbdHmqwjjac4laY,10444
27
- svg_ultralight/font_tools/font_css.py,sha256=oKChLEwE18q-rKrt7hEkpBzGQBp8u9VWFxPpIKH67o4,2780
28
- svg_ultralight/font_tools/font_info.py,sha256=8bXdsbIY2rbZ3Q1UnxR3B4nPf31MZivS40mEOcCsb44,20849
27
+ svg_ultralight/font_tools/font_info.py,sha256=00iNrJwWH2-de9CbaG1TqulRFmp_3aOyjRAlqOpT0II,20878
29
28
  svg_ultralight/font_tools/globs.py,sha256=JdrrGMqDtD4WcY7YGUWV43DUW63RVev-x9vWqsQUhxU,119
30
29
  svg_ultralight/strings/__init__.py,sha256=BMGhF1pulscIgkiYvZLr6kPRR0L4lW0jUNFxkul4_EM,295
31
30
  svg_ultralight/strings/svg_strings.py,sha256=FQNxNmMkR2M-gCFo_woQKXLgCHi3ncUlRMiaRR_a9nQ,1978
32
- svg_ultralight-0.40.1.dist-info/METADATA,sha256=ApD3fiKFCGDJNCBHNNo5wRrzoh5y_vz124GmnZnkAnQ,9022
33
- svg_ultralight-0.40.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
34
- svg_ultralight-0.40.1.dist-info/top_level.txt,sha256=se-6yqM_0Yg5orJKvKWdjQZ4iR4G_EjhL7oRgju-fdY,15
35
- svg_ultralight-0.40.1.dist-info/RECORD,,
31
+ svg_ultralight-0.42.0.dist-info/METADATA,sha256=Ey-v7B_Us35-JyL8y1p52yjkIZgqNT405c54lDcpGpU,9022
32
+ svg_ultralight-0.42.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
33
+ svg_ultralight-0.42.0.dist-info/top_level.txt,sha256=se-6yqM_0Yg5orJKvKWdjQZ4iR4G_EjhL7oRgju-fdY,15
34
+ svg_ultralight-0.42.0.dist-info/RECORD,,
@@ -1,82 +0,0 @@
1
- """Link local fonts as css in an svg file.
2
-
3
- :author: Shay Hill
4
- :created: 2025-06-04
5
- """
6
-
7
- # pyright: reportUnknownMemberType = false
8
- # pyright: reportAttributeAccessIssue = false
9
- # pyright: reportUnknownArgumentType = false
10
- # pyright: reportUnknownVariableType = false
11
- # pyright: reportUnknownParameterType = false
12
- # pyright: reportMissingTypeStubs = false
13
-
14
- from __future__ import annotations
15
-
16
- from pathlib import Path
17
- from typing import TYPE_CHECKING
18
-
19
- import cssutils
20
-
21
- from svg_ultralight.constructors import new_element
22
- from svg_ultralight.string_conversion import encode_to_css_class_name
23
-
24
- if TYPE_CHECKING:
25
- import os
26
-
27
- from lxml.etree import (
28
- _Element as EtreeElement, # pyright: ignore[reportPrivateUsage]
29
- )
30
-
31
-
32
- def _get_class_names_from_stylesheet(
33
- stylesheet: cssutils.css.CSSStyleSheet,
34
- ) -> list[str]:
35
- """Extract all class names from a given CSS stylesheet.
36
-
37
- :param stylesheet: A cssutils.css.CSSStyleSheet object.
38
- :return: A list of class names (without the leading dot).
39
- """
40
- class_names: list[str] = []
41
- for rule in stylesheet.cssRules:
42
- if rule.type == rule.STYLE_RULE:
43
- selectors = (s.strip() for s in rule.selectorText.split(","))
44
- class_names.extend(s[1:] for s in selectors if s.startswith("."))
45
- return class_names
46
-
47
-
48
- def add_svg_font_class(root: EtreeElement, font: str | os.PathLike[str]) -> str:
49
- """Add a css class for the font to the root element.
50
-
51
- :param root: The root element of the SVG document.
52
- :param font: Path to the font file.
53
- :return: The class name for the font, e.g., "bahnschrift_2e_ttf"
54
- """
55
- assert Path(font).exists()
56
- family_name = encode_to_css_class_name(Path(font).stem)
57
- class_name = encode_to_css_class_name(Path(font).name)
58
- style = root.find("style")
59
- if style is None:
60
- style = new_element("style", type="text/css")
61
- root.insert(0, style)
62
- css = style.text or ""
63
-
64
- stylesheet = cssutils.parseString(css)
65
- existing_class_names = _get_class_names_from_stylesheet(stylesheet)
66
- if class_name in existing_class_names:
67
- return class_name
68
-
69
- font_face_rule = cssutils.css.CSSFontFaceRule()
70
- font_face_rule.style = cssutils.css.CSSStyleDeclaration()
71
- font_face_rule.style["font-family"] = f'"{family_name}"'
72
- font_face_rule.style["src"] = rf"url('{Path(font).as_posix()}')"
73
- stylesheet.add(font_face_rule)
74
-
75
- style_rule = cssutils.css.CSSStyleRule(selectorText=f".{class_name}")
76
- style_rule.style = cssutils.css.CSSStyleDeclaration()
77
- style_rule.style["font-family"] = f'"{family_name}"'
78
- stylesheet.add(style_rule)
79
-
80
- style.text = stylesheet.cssText.decode("utf-8")
81
-
82
- return class_name