-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
SVGGraphics2D: drawRect and fillRect misaligned compared to Graphics2D #24
Comments
Thanks for the code, I can reproduce this: package org.jfree.svg; import java.awt.Color; public class Test { The thing to figure out (when I have time) is whether it is the filled rectangle or the lines that are offset incorrectly. |
Initial analysis is that the output is correctly matching the SVG specification. So now I will look at what is the specified behaviour for Java2D (drawing of shape outline versus filling shape). |
Thank you for looking into it. Maybe the following code block helps to show my initial finding:
-> graphics.drawRect() is creating 4 svg-line tags instead of a rect-tag with a fill color. Note that the same error is in the OrsonPdf library as well. |
FYI: A workaround for a 1-pixel line stroke width using a half pixel offset in the drawLine() method of SVGGraphics2D worked for our svg exports, but potentially not in general:
|
I've read this over a few times now, and am not sure if this developer note that I wrote years ago is pertinent, but it at least provides a hint for something else to try to see if it also accomplishes more predictable results. For me it is dead code as I converted to JavaFX and mostly transcode to AWT as-needed, so I don't have a working context for quickly testing it for SVG. "The stroke width should be greater than 1.0, to force the internal line widening algorithm to kick in; otherwise there may be side effects causing a solid line." So my own custom BasicStroke derived classes (such as dashed lines for highlighting), set the stroke width to the maximum of the desired width (2, 4, etc.) and 1.01. This may have related to dash patterns being honoured vs. details of how the internal AWT code (the parts that are exposed in the Java sources JAR) handles interior/exterior criteria. My guess is that different code might kick in when the floating-point accuracy is needed, and that the 0.5 pixels you add may be a bit much for general use. Not sure if you tried just adding ".01" to the Line end points for drawLine(), but I'd be curious whether this also gives the preferred results in the SVG conversion from AWT. It might avoid some unwanted round-down side effects. I didn't see any obvious issues in publicly exposed AWT source code though. Apologies if this ancient code that I haven't used in years, was merely compensating for cases where the Rendering Hints had set anti-aliasing to "off". But I think it did have to do with some quirks in the internals of AWT, which aren't really documented as fully as many of us would like. The SVG implementation itself however, looks correct to me as well, based on what I see in my copy of the SVG spec. |
Using JFreeSVG 5.0 I can reproduce this issue no longer. Btw. thank you very much for 5.0 - currently there is no longer a need for my "local workarrounds", as this adresses all issues I have previously found. Time to purchase the commercial license. :) |
Thank you for the new release. |
In Graphics2D drawing a filled rectangle with a 1-pixel border shows a correctly filled rectangle, applying the same code with SVGGraphics2D results in a misalignment of the border and the filled rectangle:
SVGGraphics2D graphics = new SVGGraphics2D(40, 40);
graphics.drawRect(10, 10, 20, 10); // 1- pixel border
graphics.setColor(Color.BLUE);
graphics.fillRect(11, 11, 19, 9);
The text was updated successfully, but these errors were encountered: