-
Notifications
You must be signed in to change notification settings - Fork 360
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
Support for setting SVG dimensions via CSS #128
Comments
I've investigated this issue a bit further, and I've found two ways of solving it. Both approach try to read the Option 1: minimal changes to existing interfacesThis approach more or less duplicates the logic being applied to It leaves My aim with this was to minimise API changes. However, I think it leads to some code duplication and spread the logic related to the scaling across multiple classes (and specific implementations). Option 2: relying on Batik for scaling, and possibly better encapsulationPart of the problem with the existing implementation is that we try to get the dimensions from Batik's To address this, I've added an (Note: I have not tested this with Java2D.) Example <h2>Text</h2>
<div style="width: 25%; border: 1px solid black;">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Donec venenatis velit enim, a placerat lectus viverra non.
Proin varius porta ligula, in fringilla erat suscipit a.</p>
</div>
<h2>SVG</h2>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 20"
style="width: 25%; border: black 1px solid;">
<rect x="1" y="1" rx="5" ry="5" width="28" height="18"
stroke="blue" fill="yellow" stroke-width="2" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 20"
style="width: 80%; max-width: 8cm; border: black 1px solid;">
<rect x="1" y="1" rx="5" ry="5" width="28" height="18"
stroke="blue" fill="yellow" stroke-width="2" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 20" width="300" height="200"
style="height: 4cm; border: black 1px solid;">
<rect x="1" y="1" rx="5" ry="5" width="28" height="18"
stroke="blue" fill="yellow" stroke-width="2" />
</svg> The Feedback welcome, please let me know if you're interested in a pull request. |
@danfickle I don't really know the "get the size calculation right" stuff. I mostly did the PDFBoxGraphics2D bridge to make using Batik possible. You debugged and fixed the sizing stuff :) @harbulot You could run the TestcaseRunner and compare the written png files before and after your change. If they match everything should be fine. The TestcaseRunner writes both PDFs and PNG files, so that you can compare how different the output is. From my point of view a pullrequest for svg_issue128_option2 would be nice. If your changes breaks something in the Java2D side I will investigate it. (I should have time on Tuesday - it's a holiday here in Germany) |
Cleaning up old issues. I think we can close this now. If anyone wants information on how SVG sizing is handled see the SVG wiki page. |
I'm trying to set the size of an SVG image using CSS styles (e.g.
style="width: 25%"
), but this is ignore.Only the
width
andheight
attributes of the<svg />
element seem to have an effect. The default values seem to be 400, but I'm not sure what unit this uses, or what it is in relation to the page width.Example
Here is a simple HTML example:
Here is the output in a browser:
Here is the PDF output:
Here is the code I've used:
Improvement attempt 1
I've looked into the SVG support code to investigate, and here is a partial fix:
This seems to get the correct width at least:
The height of the containing element still seems to be a problem.
This seems to come from getting the height from
PdfBoxSVGReplacedElement.getIntrinsicHeight()
(which in this case is the default value of 400).Improvement attempt 2
This consists of 3 steps:
svgDraw
.viewBox
values.The result is slightly better in that the image is now rendered at the top of its container.
One of the main outcomes is that the
transcoder
'swidth
andheight
fields are now correct, as set viaSVGAbstractTranscoder.setImageSize(...)
. (In this particular example, they're now 175.55 and 117.03, which is the correct aspect ratio.)Remaining problem
Being able to get
transformer.width
andtransformer.height
after those calculations would be great, but it's too late for the first call toPdfBoxSVGReplacedElement.getIntrinsicHeight()
.I'm not suggesting the above patches should be integrated in the existing code base, but hopefully, they might help investigate the issue, if possible.
The text was updated successfully, but these errors were encountered: