[OpenJDK 2D-Dev] More incompatibilities
Roman Kennke
roman.kennke at aicas.com
Tue Mar 3 11:08:11 UTC 2009
Hi,
I think we have at least two problems here.
For one, this appears to be a rounding problem. In
SunGraphics2D.validateBasicStroke(), we calculate a widthsquared of
0.24999999999999997, and compare it to a MinPenSizeSquared of 0.25, this
triggers the thin logic where it probably should not.
OTOH, in PiscesRenderingEngine.strokeTo(), in our case we get a
lineWidth of 381 from the basic stroke because the target is scaled by
1/(381*0.5), where the thin logic wants to use 0.5. I suspect that this
could be another problem for cases where we actually want the thin
logic.
So what happens is, we trigger the thin logic, although we should not,
and then it picks up a line width which is far too small considering the
scale factor.
I don't know if we need to fix the rounding problem. I guess when we can
fix the thin code path, this should be ok anyway (we end up with a
minimum-width line anyway). Fixing the line width is more difficult
though. We would need to scale the minimum line width (0.5 or 1.0) using
the passed in transform. The attached patch does that. I more or less
copied the code from SunGraphics2D.validateBasicStroke() to calculate
the scale factor to apply. Maybe we should pull this out and reuse some
shared routine in both cases.
/Roman
Am Dienstag, den 24.02.2009, 11:40 -0800 schrieb Hiroshi Yamauchi:
> The following change appears to make the ThinLineTest pass. I tried a
> couple of variations of it, with and without antialiasing, with and
> without scaling, and with different scaling factors and line widths.
> The looked okay. Btw, bear with me as I am not really a Java 2D person
> and I may not know what I'm talking about :) But I got the feeling
> that the 'thin' logic (to do with the thin boolean parameter to
> PiscesRenderingEngine.strokeTo() and the logic in
> SunGraphics2D.validateBasicStroke()) isn't compatible with
> Stroker.computeOffset().
>
> +++ jdk/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java
> 2009-02-24 11:17:48.000000000 -0800
> @@ -169,7 +169,7 @@
> LineSink lsink)
> {
> float lw;
> - if (thin) {
> + if (false && thin) {
> if (antialias) {
> lw = 0.5f;
> } else {
>
>
> On Mon, Feb 23, 2009 at 5:32 PM, Hiroshi Yamauchi
> <yamauchi at google.com> wrote:
> Hi all,
>
> We found four more 2D rendering OpenJDK incompatibilities (the
> examples are attached):
>
> 1. ScaleTest: A circle is rendered in a 'C' shape.
> 2. ThinLineTest: A line < 1 pixel disappears.
> 3. NotANumberTest: Double.NaN isn't handled gracefully.
> 4. StrokeShapeTest: createStrokedShape() behaves differently.
>
> Some of the examples may be caused by a single bug. I don't
> have an idea what the causes are.
> By any chance, have any of the them already fixed?
>
> Thanks,
> Hiroshi
>
>
>
--
Dipl.-Inform. (FH) Roman Kennke, Software Engineer, http://kennke.org
aicas Allerton Interworks Computer Automated Systems GmbH
Haid-und-Neu-Straße 18 * D-76131 Karlsruhe * Germany
http://www.aicas.com * Tel: +49-721-663 968-48
USt-Id: DE216375633, Handelsregister HRB 109481, AG Karlsruhe
Geschäftsführer: Dr. James J. Hunt
-------------- next part --------------
diff --git a/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java b/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java
--- a/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java
+++ b/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java
@@ -171,9 +171,9 @@
float lw;
if (thin) {
if (antialias) {
- lw = 0.5f;
+ lw = userSpaceLineWidth(at, 0.5f);
} else {
- lw = 1.0f;
+ lw = userSpaceLineWidth(at, 1.0f);
}
} else {
lw = bs.getLineWidth();
@@ -187,6 +187,67 @@
bs.getDashArray(),
bs.getDashPhase(),
lsink);
+ }
+
+ private float userSpaceLineWidth(AffineTransform at, float lw) {
+ double widthScale;
+ if ((at.getType() & (AffineTransform.TYPE_GENERAL_TRANSFORM |
+ AffineTransform.TYPE_GENERAL_SCALE)) != 0) {
+ widthScale = Math.sqrt(at.getDeterminant());
+ } else {
+ /* First calculate the "maximum scale" of this transform. */
+ double A = at.getScaleX(); // m00
+ double C = at.getShearX(); // m01
+ double B = at.getShearY(); // m10
+ double D = at.getScaleY(); // m11
+
+ /*
+ * Given a 2 x 2 affine matrix [ A B ] such that
+ * [ C D ]
+ * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
+ * find the maximum magnitude (norm) of the vector v'
+ * with the constraint (x^2 + y^2 = 1).
+ * The equation to maximize is
+ * |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
+ * or |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
+ * Since sqrt is monotonic we can maximize |v'|^2
+ * instead and plug in the substitution y = sqrt(1 - x^2).
+ * Trigonometric equalities can then be used to get
+ * rid of most of the sqrt terms.
+ */
+ double EA = A*A + B*B; // x^2 coefficient
+ double EB = 2*(A*C + B*D); // xy coefficient
+ double EC = C*C + D*D; // y^2 coefficient
+
+ /*
+ * There is a lot of calculus omitted here.
+ *
+ * Conceptually, in the interests of understanding the
+ * terms that the calculus produced we can consider
+ * that EA and EC end up providing the lengths along
+ * the major axes and the hypot term ends up being an
+ * adjustment for the additional length along the off-axis
+ * angle of rotated or sheared ellipses as well as an
+ * adjustment for the fact that the equation below
+ * averages the two major axis lengths. (Notice that
+ * the hypot term contains a part which resolves to the
+ * difference of these two axis lengths in the absence
+ * of rotation.)
+ *
+ * In the calculus, the ratio of the EB and (EA-EC) terms
+ * ends up being the tangent of 2*theta where theta is
+ * the angle that the long axis of the ellipse makes
+ * with the horizontal axis. Thus, this equation is
+ * calculating the length of the hypotenuse of a triangle
+ * along that axis.
+ */
+ double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));
+
+ /* sqrt omitted, compare to squared limits below. */
+ double widthsquared = ((EA + EC + hypot)/2.0);
+ widthScale = Math.sqrt(widthsquared);
+ }
+ return (float) (lw / widthScale);
}
void strokeTo(Shape src,
More information about the 2d-dev
mailing list