Skip to content

Commit

Permalink
SVG stroke-width with explicit unit, fixes py-pdf#526
Browse files Browse the repository at this point in the history
  • Loading branch information
gmischler committed Sep 16, 2022
1 parent 54b0309 commit 65f78a0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
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 == 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

0 comments on commit 65f78a0

Please sign in to comment.