Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG stroke-width with explicit unit, fixes #526 #542

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
- `fpdf2` now uses [fontTools](https://fonttools.readthedocs.io/en/latest/) to read and embed fonts in the PDF, thanks to @gmischler and @RedShy

### Fixed
- The SVG parser now accepts stroke-width attribute values with an explicit unit, thanks to @gmischler; [#526](https://github.com/PyFPDF/fpdf2/issues/526)
- Text following a HTML heading can't overlap with that heading anymore, thanks to @gmischler
- `arc()` not longer renders artefacts at intersection point, thanks to @Jmillan-Dev; [#488](https://github.com/PyFPDF/fpdf2/issues/488)
- [`write_html()`](https://pyfpdf.github.io/fpdf2/HTML.html):
Expand Down
7 changes: 5 additions & 2 deletions fpdf/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ class Percent(float):
@force_nodocument
def resolve_length(length_str, default_unit="pt"):
"""Convert a length unit to our canonical length unit, pt."""
value, unit = unit_splitter.match(length_str).groups()
match = unit_splitter.match(length_str)
if match is None:
raise ValueError(f"Unable to parse '{length_str}' as a length") from None
value, unit = match.groups()
if not unit:
unit = default_unit

Expand Down Expand Up @@ -193,7 +196,7 @@ def svgcolor(colorstr):

@force_nodocument
def convert_stroke_width(incoming):
val = float(incoming)
val = resolve_length(incoming)
if val < 0:
raise ValueError(f"stroke width {incoming} cannot be negative")
if val == 0:
Expand Down
6 changes: 6 additions & 0 deletions test/svg/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ def Gs(**kwargs):
no_error(),
id="stroke-width number",
),
pytest.param( # issue #526
'<path stroke-width="2px"/>',
Gs(stroke_width=2 * 0.75),
no_error(),
id="stroke-width number",
),
pytest.param(
'<path stroke-width="inherit"/>',
Gs(),
Expand Down