Skip to content

SVG: read the x, y, width and height of an <image> as lengths, so that they may have a unit - #2161

Open
RavSinghChandan wants to merge 1 commit into
py-pdf:masterfrom
RavSinghChandan:svg-image-length-units
Open

RavSinghChandan wants to merge 1 commit into
py-pdf:masterfrom
RavSinghChandan:svg-image-length-units

Conversation

@RavSinghChandan

Copy link
Copy Markdown

An <image> whose x, y, width or height has a unit fails:

ValueError: could not convert string to float: '342px'

build_image() reads these attributes with float(), while the other shapes (<rect>, <circle>, ...) and the root <svg> read their lengths with resolve_length(). The logo of narwhals (docs/assets/logo.svg) is such a file: <svg width="342px" height="342px"> with <image x="0px" y="0px" width="342px" height="342px">.

build_image() now uses resolve_length() for the four attributes too, so 342px becomes 256.5, the same size as the <svg> it fills, and a unitless value is unchanged. The narwhals logo renders as it does in a browser.

test_svg_image_length_units checks the position and size of an <image> with no unit, px, pt and mm; the three cases with a unit fail on master. The SVG tests, black, pylint, mypy and pyright pass

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant