< prev index next >

## openjfx9/modules/javafx.graphics/src/main/java/com/sun/marlin/Stroker.java

```@@ -21,24 +21,20 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

-package sun.java2d.marlin;
+package com.sun.marlin;

import java.util.Arrays;
-import static java.lang.Math.ulp;
-import static java.lang.Math.sqrt;
-
-import sun.awt.geom.PathConsumer2D;
-import sun.java2d.marlin.Curve.BreakPtrIterator;

+import com.sun.javafx.geom.PathConsumer2D;

// TODO: some of the arithmetic here is too verbose and prone to hard to
// debug typos. We should consider making a small Point/Vector class that
// has methods like plus(Point), minus(Point), dot(Point), cross(Point)and such
-final class Stroker implements PathConsumer2D, MarlinConst {
+public final class Stroker implements PathConsumer2D, MarlinConst {

private static final int MOVE_TO = 0;
private static final int DRAWING_OP_TO = 1; // ie. curve, line, or quad
private static final int CLOSE = 2;

```

```@@ -110,13 +106,12 @@
private float smx, smy, cmx, cmy;

private final PolyStack reverse;

// This is where the curve to be processed is put. We give it
-    // enough room to store 2 curves: one for the current subdivision, the
-    // other for the rest of the curve.
-    private final float[] middle = new float[2 * 8];
+    // enough room to store all curves.
+    private final float[] middle = new float[MAX_N_CURVES * 8];
private final float[] lp = new float;
private final float[] rp = new float;
private final float[] subdivTs = new float[MAX_N_CURVES - 1];

```

```@@ -148,11 +143,11 @@
* <code>JOIN_MITER</code>, <code>JOIN_ROUND</code> or
* <code>JOIN_BEVEL</code>.
* @param miterLimit the desired miter limit
* @return this instance
*/
-    Stroker init(PathConsumer2D pc2d,
+    public Stroker init(PathConsumer2D pc2d,
float lineWidth,
int capStyle,
int joinStyle,
float miterLimit)
{
```

```@@ -199,11 +194,11 @@
float len = lx*lx + ly*ly;
if (len == 0f) {
m = 0f;
m = 0f;
} else {
-            len = (float) sqrt(len);
+            len = (float) Math.sqrt(len);
m =  (ly * w) / len;
m = -(lx * w) / len;
}
}

```

```@@ -278,11 +273,11 @@
// perpendicular bisector goes through the origin). This scaling doesn't
// have numerical problems because we know that lineWidth2 divided by
// this normal's length is at least 0.5 and at most sqrt(2)/2 (because
// we know the angle of the arc is > 90 degrees).
float nx = my - omy, ny = omx - mx;
-            float nlen = (float) sqrt(nx*nx + ny*ny);
+            float nlen = (float) Math.sqrt(nx*nx + ny*ny);
float scale = lineWidth2/nlen;
float mmx = nx * scale, mmy = ny * scale;

// if (isCW(omx, omy, mx, my) != isCW(mmx, mmy, mx, my)) then we've
// computed the wrong intersection so we get the other one.
```

```@@ -316,12 +311,12 @@
// cv is the length of P1-P0 and P2-P3 divided by the radius of the arc
// (so, cv assumes the arc has radius 1). P0, P1, P2, P3 are the points that
// define the bezier curve we're computing.
// It is computed using the constraints that P1-P0 and P3-P2 are parallel
// to the arc tangents at the endpoints, and that |P1-P0|=|P3-P2|.
-        float cv = (float) ((4.0 / 3.0) * sqrt(0.5 - cosext2) /
-                            (1.0 + sqrt(cosext2 + 0.5)));
+        float cv = (float) ((4.0 / 3.0) * Math.sqrt(0.5 - cosext2) /
+                            (1.0 + Math.sqrt(cosext2 + 0.5)));
// if clockwise, we need to negate cv.
if (rev) { // rev is equivalent to isCW(omx, omy, mx, my)
cv = -cv;
}
final float x1 = cx + omx;
```

```@@ -346,31 +341,73 @@
emitCurveTo(cx - my - Cmx, cy + mx - Cmy,
cx - mx - Cmy, cy - my + Cmx,
cx - mx,       cy - my);
}

-    // Put the intersection point of the lines (x0, y0) -> (x1, y1)
-    // and (x0p, y0p) -> (x1p, y1p) in m[off] and m[off+1].
-    // If the lines are parallel, it will put a non finite number in m.
-    private static void computeIntersection(final float x0, final float y0,
-                                            final float x1, final float y1,
-                                            final float x0p, final float y0p,
-                                            final float x1p, final float y1p,
-                                            final float[] m, int off)
+    // Return the intersection point of the lines (x0, y0) -> (x1, y1)
+    // and (x0p, y0p) -> (x1p, y1p) in m and m
+    private static void computeMiter(final float x0, final float y0,
+                                     final float x1, final float y1,
+                                     final float x0p, final float y0p,
+                                     final float x1p, final float y1p,
+                                     final float[] m, int off)
{
float x10 = x1 - x0;
float y10 = y1 - y0;
float x10p = x1p - x0p;
float y10p = y1p - y0p;

+        // if this is 0, the lines are parallel. If they go in the
+        // same direction, there is no intersection so m[off] and
+        // m[off+1] will contain infinity, so no miter will be drawn.
+        // If they go in the same direction that means that the start of the
+        // current segment and the end of the previous segment have the same
+        // tangent, in which case this method won't even be involved in
+        // miter drawing because it won't be called by drawMiter (because
+        // (mx == omx && my == omy) will be true, and drawMiter will return
+        // immediately).
float den = x10*y10p - x10p*y10;
float t = x10p*(y0-y0p) - y10p*(x0-x0p);
t /= den;
m[off++] = x0 + t*x10;
m[off]   = y0 + t*y10;
}

+    // Return the intersection point of the lines (x0, y0) -> (x1, y1)
+    // and (x0p, y0p) -> (x1p, y1p) in m and m
+    private static void safecomputeMiter(final float x0, final float y0,
+                                         final float x1, final float y1,
+                                         final float x0p, final float y0p,
+                                         final float x1p, final float y1p,
+                                         final float[] m, int off)
+    {
+        float x10 = x1 - x0;
+        float y10 = y1 - y0;
+        float x10p = x1p - x0p;
+        float y10p = y1p - y0p;
+
+        // if this is 0, the lines are parallel. If they go in the
+        // same direction, there is no intersection so m[off] and
+        // m[off+1] will contain infinity, so no miter will be drawn.
+        // If they go in the same direction that means that the start of the
+        // current segment and the end of the previous segment have the same
+        // tangent, in which case this method won't even be involved in
+        // miter drawing because it won't be called by drawMiter (because
+        // (mx == omx && my == omy) will be true, and drawMiter will return
+        // immediately).
+        float den = x10*y10p - x10p*y10;
+        if (den == 0f) {
+            m[off++] = (x0 + x0p) / 2f;
+            m[off] = (y0 + y0p) / 2f;
+            return;
+        }
+        float t = x10p*(y0-y0p) - y10p*(x0-x0p);
+        t /= den;
+        m[off++] = x0 + t*x10;
+        m[off] = y0 + t*y10;
+    }
+
private void drawMiter(final float pdx, final float pdy,
final float x0, final float y0,
final float dx, final float dy,
float omx, float omy, float mx, float my,
boolean rev)
```

```@@ -387,13 +424,13 @@
omy = -omy;
mx  = -mx;
my  = -my;
}

-        computeIntersection((x0 - pdx) + omx, (y0 - pdy) + omy, x0 + omx, y0 + omy,
-                            (dx + x0) + mx, (dy + y0) + my, x0 + mx, y0 + my,
-                            miter, 0);
+        computeMiter((x0 - pdx) + omx, (y0 - pdy) + omy, x0 + omx, y0 + omy,
+                     (dx + x0) + mx, (dy + y0) + my, x0 + mx, y0 + my,
+                     miter, 0);

final float miterX = miter;
final float miterY = miter;
float lenSq = (miterX-x0)*(miterX-x0) + (miterY-y0)*(miterY-y0);

```

```@@ -655,12 +692,12 @@
float dx1 = x2 - x1;
float dy1 = y2 - y1;

// if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
// in which case ignore if p1 == p2
-        final boolean p1eqp2 = within(x1,y1,x2,y2, 6f * ulp(y2));
-        final boolean p3eqp4 = within(x3,y3,x4,y4, 6f * ulp(y4));
+        final boolean p1eqp2 = within(x1,y1,x2,y2, 6f * Math.ulp(y2));
+        final boolean p3eqp4 = within(x3,y3,x4,y4, 6f * Math.ulp(y4));
if (p1eqp2 && p3eqp4) {
getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
return 4;
} else if (p1eqp2) {
dx1 = x3 - x1;
```

```@@ -672,11 +709,11 @@

// if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
float dotsq = (dx1 * dx4 + dy1 * dy4);
dotsq *= dotsq;
float l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4;
-        if (Helpers.within(dotsq, l1sq * l4sq, 4f * ulp(dotsq))) {
+        if (Helpers.within(dotsq, l1sq * l4sq, 4f * Math.ulp(dotsq))) {
getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
return 4;
}

//      What we're trying to do in this function is to approximate an ideal
```

```@@ -782,10 +819,12 @@
rightOff = x3p; rightOff = y3p;
rightOff = x4p; rightOff = y4p;
return 8;
}

+    // compute offset curves using bezier spline through t=0.5 (i.e.
+    // ComputedCurve(0.5) == IdealParallelCurve(0.5))
// return the kind of curve in the right and left arrays.
private int computeOffsetQuad(float[] pts, final int off,
float[] leftOff, float[] rightOff)
{
final float x1 = pts[off + 0], y1 = pts[off + 1];
```

```@@ -795,68 +834,58 @@
final float dx3 = x3 - x2;
final float dy3 = y3 - y2;
final float dx1 = x2 - x1;
final float dy1 = y2 - y1;

-        // this computes the offsets at t = 0, 1
+        // if p1=p2 or p3=p4 it means that the derivative at the endpoint
+        // vanishes, which creates problems with computeOffset. Usually
+        // this happens when this stroker object is trying to winden
+        // a curve with a cusp. What happens is that curveTo splits
+        // the input curve at the cusp, and passes it to this function.
+        // because of inaccuracies in the splitting, we consider points
+        // equal if they're very close to each other.
+
+        // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
+        // in which case ignore.
+        final boolean p1eqp2 = within(x1,y1,x2,y2, 6f * Math.ulp(y2));
+        final boolean p2eqp3 = within(x2,y2,x3,y3, 6f * Math.ulp(y3));
+        if (p1eqp2 || p2eqp3) {
+            getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
+            return 4;
+        }
+
+        // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
+        float dotsq = (dx1 * dx3 + dy1 * dy3);
+        dotsq *= dotsq;
+        float l1sq = dx1 * dx1 + dy1 * dy1, l3sq = dx3 * dx3 + dy3 * dy3;
+        if (Helpers.within(dotsq, l1sq * l3sq, 4f * Math.ulp(dotsq))) {
+            getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
+            return 4;
+        }
+
+        // this computes the offsets at t=0, 0.5, 1, using the property that
+        // for any bezier curve the vectors p2-p1 and p4-p3 are parallel to
+        // the (dx/dt, dy/dt) vectors at the endpoints.
computeOffset(dx1, dy1, lineWidth2, offset0);
computeOffset(dx3, dy3, lineWidth2, offset1);

-        leftOff  = x1 + offset0; leftOff  = y1 + offset0;
-        leftOff  = x3 + offset1; leftOff  = y3 + offset1;
-        rightOff = x1 - offset0; rightOff = y1 - offset0;
-        rightOff = x3 - offset1; rightOff = y3 - offset1;
-
-        float x1p = leftOff; // start
-        float y1p = leftOff; // point
-        float x3p = leftOff; // end
-        float y3p = leftOff; // point
-
-        // Corner cases:
-        // 1. If the two control vectors are parallel, we'll end up with NaN's
-        //    in leftOff (and rightOff in the body of the if below), so we'll
-        //    do getLineOffsets, which is right.
-        // 2. If the first or second two points are equal, then (dx1,dy1)==(0,0)
-        //    or (dx3,dy3)==(0,0), so (x1p, y1p)==(x1p+dx1, y1p+dy1)
-        //    or (x3p, y3p)==(x3p-dx3, y3p-dy3), which means that
-        //    computeIntersection will put NaN's in leftOff and right off, and
-        //    we will do getLineOffsets, which is right.
-        computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2);
-        float cx = leftOff;
-        float cy = leftOff;
-
-        if (!(isFinite(cx) && isFinite(cy))) {
-            // maybe the right path is not degenerate.
-            x1p = rightOff;
-            y1p = rightOff;
-            x3p = rightOff;
-            y3p = rightOff;
-            computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2);
-            cx = rightOff;
-            cy = rightOff;
-            if (!(isFinite(cx) && isFinite(cy))) {
-                // both are degenerate. This curve is a line.
-                getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
-                return 4;
-            }
-            // {left,right}Off[0,1,4,5] are already set to the correct values.
-            leftOff = 2f * x2 - cx;
-            leftOff = 2f * y2 - cy;
-            return 6;
-        }
+        float x1p = x1 + offset0; // start
+        float y1p = y1 + offset0; // point
+        float x3p = x3 + offset1; // end
+        float y3p = y3 + offset1; // point
+        safecomputeMiter(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2);
+        leftOff = x1p; leftOff = y1p;
+        leftOff = x3p; leftOff = y3p;

-        // rightOff[2,3] = (x2,y2) - ((left_x2, left_y2) - (x2, y2))
-        // == 2*(x2, y2) - (left_x2, left_y2)
-        rightOff = 2f * x2 - cx;
-        rightOff = 2f * y2 - cy;
+        x1p = x1 - offset0; y1p = y1 - offset0;
+        x3p = x3 - offset1; y3p = y3 - offset1;
+        safecomputeMiter(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2);
+        rightOff = x1p; rightOff = y1p;
+        rightOff = x3p; rightOff = y3p;
return 6;
}

-    private static boolean isFinite(float x) {
-        return (Float.NEGATIVE_INFINITY < x && x < Float.POSITIVE_INFINITY);
-    }
-
// If this class is compiled with ecj, then Hotspot crashes when OSR
// compiling this function. See bugs 7004570 and 6675699
// TODO: until those are fixed, we should work around that by
// manually inlining this into curveTo and quadTo.
/******************************* WORKAROUND **********************************
```

```@@ -973,11 +1002,11 @@
// from rotating it.
if (y12 != 0f && x12 != 0f) {
// we rotate it so that the first vector in the control polygon is
// parallel to the x-axis. This will ensure that rotated quarter
// circles won't be subdivided.
-            final float hypot = (float) sqrt(x12 * x12 + y12 * y12);
+            final float hypot = (float) Math.sqrt(x12 * x12 + y12 * y12);
final float cos = x12 / hypot;
final float sin = y12 / hypot;
final float x1 = cos * pts + sin * pts;
final float y1 = cos * pts - sin * pts;
final float x2 = cos * pts + sin * pts;
```

```@@ -1066,34 +1095,40 @@
}

// if these vectors are too small, normalize them, to avoid future
// precision problems.
if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
-            float len = (float) sqrt(dxs*dxs + dys*dys);
+            float len = (float) Math.sqrt(dxs*dxs + dys*dys);
dxs /= len;
dys /= len;
}
if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
-            float len = (float) sqrt(dxf*dxf + dyf*dyf);
+            float len = (float) Math.sqrt(dxf*dxf + dyf*dyf);
dxf /= len;
dyf /= len;
}

computeOffset(dxs, dys, lineWidth2, offset0);
drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, offset0, offset0);

-        int nSplits = findSubdivPoints(curve, mid, subdivTs, 8, lineWidth2);
+        final int nSplits = findSubdivPoints(curve, mid, subdivTs, 8, lineWidth2);
+
+        float prevT = 0f;
+        for (int i = 0, off = 0; i < nSplits; i++, off += 6) {
+            final float t = subdivTs[i];
+            Helpers.subdivideCubicAt((t - prevT) / (1f - prevT),
+                                     mid, off, mid, off, mid, off + 6);
+            prevT = t;
+        }

final float[] l = lp;
final float[] r = rp;

int kind = 0;
-        BreakPtrIterator it = curve.breakPtsAtTs(mid, 8, subdivTs, nSplits);
-        while(it.hasNext()) {
-            int curCurveOff = it.next();
+        for (int i = 0, off = 0; i <= nSplits; i++, off += 6) {
+            kind = computeOffsetCubic(mid, off, l, r);

-            kind = computeOffsetCubic(mid, curCurveOff, l, r);
emitLineTo(l, l);

switch(kind) {
case 8:
emitCurveTo(l, l, l, l, l, l);
```

```@@ -1143,34 +1178,40 @@
return;
}
// if these vectors are too small, normalize them, to avoid future
// precision problems.
if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
-            float len = (float) sqrt(dxs*dxs + dys*dys);
+            float len = (float) Math.sqrt(dxs*dxs + dys*dys);
dxs /= len;
dys /= len;
}
if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
-            float len = (float) sqrt(dxf*dxf + dyf*dyf);
+            float len = (float) Math.sqrt(dxf*dxf + dyf*dyf);
dxf /= len;
dyf /= len;
}

computeOffset(dxs, dys, lineWidth2, offset0);
drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, offset0, offset0);

int nSplits = findSubdivPoints(curve, mid, subdivTs, 6, lineWidth2);

+        float prevt = 0f;
+        for (int i = 0, off = 0; i < nSplits; i++, off += 4) {
+            final float t = subdivTs[i];
+            Helpers.subdivideQuadAt((t - prevt) / (1f - prevt),
+                                    mid, off, mid, off, mid, off + 4);
+            prevt = t;
+        }
+
final float[] l = lp;
final float[] r = rp;

int kind = 0;
-        BreakPtrIterator it = curve.breakPtsAtTs(mid, 6, subdivTs, nSplits);
-        while(it.hasNext()) {
-            int curCurveOff = it.next();
+        for (int i = 0, off = 0; i <= nSplits; i++, off += 4) {
+            kind = computeOffsetQuad(mid, off, l, r);

-            kind = computeOffsetQuad(mid, curCurveOff, l, r);
emitLineTo(l, l);

switch(kind) {
case 6:
```

```@@ -1192,26 +1233,22 @@
this.cx0 = xf;
this.cy0 = yf;
this.prev = DRAWING_OP_TO;
}

-    @Override public long getNativeConsumer() {
-        throw new InternalError("Stroker doesn't use a native consumer");
-    }
-
// a stack of polynomial curves where each curve shares endpoints with
static final class PolyStack {
private static final byte TYPE_LINETO  = (byte) 0;
private static final byte TYPE_QUADTO  = (byte) 1;
private static final byte TYPE_CUBICTO = (byte) 2;

-        // curves capacity = edges count (4096) = half edges x 2 (coords)
-        private static final int INITIAL_CURVES_COUNT = INITIAL_EDGES_COUNT;
+        // curves capacity = edges count (8192) = edges x 2 (coords)
+        private static final int INITIAL_CURVES_COUNT = INITIAL_EDGES_COUNT << 1;

-        // types capacity = half edges count (2048)
-        private static final int INITIAL_TYPES_COUNT = INITIAL_EDGES_COUNT >> 1;
+        // types capacity = edges count (4096)
+        private static final int INITIAL_TYPES_COUNT = INITIAL_EDGES_COUNT;

float[] curves;
int end;
byte[] curveTypes;
int numCurves;
```

```@@ -1233,14 +1270,14 @@
* @param rdrCtx per-thread renderer context
*/
PolyStack(final RendererContext rdrCtx) {
this.rdrCtx = rdrCtx;

-            curves_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_CURVES_COUNT); // 16K
+            curves_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_CURVES_COUNT); // 32K
curves     = curves_ref.initial;

-            curveTypes_ref = rdrCtx.newDirtyByteArrayRef(INITIAL_TYPES_COUNT); // 2K
+            curveTypes_ref = rdrCtx.newDirtyByteArrayRef(INITIAL_TYPES_COUNT); // 4K
curveTypes     = curveTypes_ref.initial;
numCurves = 0;
end = 0;

if (DO_STATS) {
```

```@@ -1367,11 +1404,11 @@

@Override
public String toString() {
String ret = "";
int nc = numCurves;
-            int e  = end;
+            int last = end;
int len;
while (nc != 0) {
switch(curveTypes[--nc]) {
case TYPE_LINETO:
len = 2;
```

```@@ -1386,12 +1423,12 @@
ret += "cubic: ";
break;
default:
len = 0;
}
-                e -= len;
-                ret += Arrays.toString(Arrays.copyOfRange(curves, e, e+len))
+                last -= len;
+                ret += Arrays.toString(Arrays.copyOfRange(curves, last, last+len))
+ "\n";
}
return ret;
}
}
```
< prev index next >