Skip to content
Open
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 @@ -21,6 +21,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
* Chinese translation of the [Tuto 7 - Creating PDF/A Documents](https://py-pdf.github.io/fpdf2/Tutorial-zh.html#-pdfa) tutorial section - _cf._ [issue #267](https://github.com/py-pdf/fpdf2/issues/267) - _cf._ [PR #2138](https://github.com/py-pdf/fpdf2/pull/2138) - thanks to @zheng-guannan577*
* `FPDF.write_html()` makes the `id` of an element, and the `name` of an `<a>`, a named destination, so that `<a href="#...">` links within the document work, like in browsers, instead of raising `FPDFException: Named destination ... was referenced but never set` - thanks to @RavSinghChandan
### Fixed
* SVG: an `<image>` with `width="auto"` or `height="auto"`, the initial value in SVG 2, now takes the size of the image itself, or the size that keeps its ratio, as when the attribute is missing, instead of failing with `ValueError: Unable to parse 'auto' as a length` - thanks to @RavSinghChandan
* the generic font families of CSS Fonts 4, `system-ui`, `ui-sans-serif`, `ui-rounded`, `ui-serif` and `ui-monospace`, are now rendered with the matching core font in SVG text and `FPDF.write_html()`, as in the `og.svg` images of react-window, instead of failing with `KeyError: No suitable font for family='system-ui'` - thanks to @RavSinghChandan
* SVG: a root `<svg>` with `width="auto"` or `height="auto"`, as Figma writes it, for instance in the country flags of the ProtonVPN apps, is now sized as if it had no `width` or `height`, from its `viewBox`, instead of failing with `ValueError: Unable to parse 'auto' as a length` - thanks to @RavSinghChandan
* SVG: `hsl()` and `hsla()` colors are now read, with commas or space-separated, with a hue that may have an angle unit (`deg`, `grad`, `rad`, `turn`) and an alpha, as Mermaid diagrams and many stylesheets write them, instead of failing with `ValueError: unsupported color specification` - thanks to @RavSinghChandan
Expand Down
11 changes: 9 additions & 2 deletions fpdf/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2770,8 +2770,15 @@ def build_image(self, image: "Element") -> "SVGImage | GraphicsContext":
break
if not href:
raise ValueError("<image> is missing a href attribute")
width = resolve_length(image.attrib.get("width", "0"))
height = resolve_length(image.attrib.get("height", "0"))
# "auto", the initial value in SVG 2, is like a missing size, here 0: the
# size of the image itself, or the one keeping its ratio with the other side
width, height = (
resolve_length("0" if value.strip().lower() == "auto" else value)
for value in (
image.attrib.get("width", "0"),
image.attrib.get("height", "0"),
)
)

keep_aspect_ratio = True
preserve_aspect_ratio = image.attrib.get("preserveAspectRatio")
Expand Down
22 changes: 22 additions & 0 deletions test/svg/test_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,28 @@ def test_svg_image_length_units(unit, scale):
)


@pytest.mark.parametrize(
"size, expected",
[
('width="auto" height="30"', (0, 30)),
('width="AUTO" height="auto"', (0, 0)),
('width="40" height=" auto "', (40, 0)),
],
)
def test_svg_image_size_auto(size, expected):
# as in SVG 2, auto is the size of the image itself: 0, as when it is missing,
# which size_in_document_units() reads as the image size or ratio
svg = fpdf.svg.SVGObject(
'<svg xmlns="http://www.w3.org/2000/svg">'
f'<image href="{TINY_PNG_DATA_URI}" {size}/></svg>'
)
image = svg.base_group.path_items[0]
assert (image.width, image.height) == expected
pdf = fpdf.FPDF()
pdf.add_page()
svg.draw_to_page(pdf)


def test_svg_image_transform(caplog):
pdf = fpdf.FPDF()
pdf.add_page()
Expand Down
Loading