< prev index next >

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

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.marlin;
  27 
  28 import java.util.Arrays;
  29 import static java.lang.Math.ulp;
  30 import static java.lang.Math.sqrt;
  31 
  32 import sun.awt.geom.PathConsumer2D;
  33 import sun.java2d.marlin.Curve.BreakPtrIterator;
  34 

  35 
  36 // TODO: some of the arithmetic here is too verbose and prone to hard to
  37 // debug typos. We should consider making a small Point/Vector class that
  38 // has methods like plus(Point), minus(Point), dot(Point), cross(Point)and such
  39 final class Stroker implements PathConsumer2D, MarlinConst {
  40 
  41     private static final int MOVE_TO = 0;
  42     private static final int DRAWING_OP_TO = 1; // ie. curve, line, or quad
  43     private static final int CLOSE = 2;
  44 
  45     /**
  46      * Constant value for join style.
  47      */
  48     public static final int JOIN_MITER = 0;
  49 
  50     /**
  51      * Constant value for join style.
  52      */
  53     public static final int JOIN_ROUND = 1;
  54 
  55     /**
  56      * Constant value for join style.
  57      */
  58     public static final int JOIN_BEVEL = 2;
  59 


  95     private final float[] miter = new float[2];
  96     private float miterLimitSq;
  97 
  98     private int prev;
  99 
 100     // The starting point of the path, and the slope there.
 101     private float sx0, sy0, sdx, sdy;
 102     // the current point and the slope there.
 103     private float cx0, cy0, cdx, cdy; // c stands for current
 104     // vectors that when added to (sx0,sy0) and (cx0,cy0) respectively yield the
 105     // first and last points on the left parallel path. Since this path is
 106     // parallel, it's slope at any point is parallel to the slope of the
 107     // original path (thought they may have different directions), so these
 108     // could be computed from sdx,sdy and cdx,cdy (and vice versa), but that
 109     // would be error prone and hard to read, so we keep these anyway.
 110     private float smx, smy, cmx, cmy;
 111 
 112     private final PolyStack reverse;
 113 
 114     // This is where the curve to be processed is put. We give it
 115     // enough room to store 2 curves: one for the current subdivision, the
 116     // other for the rest of the curve.
 117     private final float[] middle = new float[2 * 8];
 118     private final float[] lp = new float[8];
 119     private final float[] rp = new float[8];
 120     private final float[] subdivTs = new float[MAX_N_CURVES - 1];
 121 
 122     // per-thread renderer context
 123     final RendererContext rdrCtx;
 124 
 125     // dirty curve
 126     final Curve curve;
 127 
 128     /**
 129      * Constructs a <code>Stroker</code>.
 130      * @param rdrCtx per-thread renderer context
 131      */
 132     Stroker(final RendererContext rdrCtx) {
 133         this.rdrCtx = rdrCtx;
 134 
 135         this.reverse = new PolyStack(rdrCtx);
 136         this.curve = rdrCtx.curve;
 137     }
 138 
 139     /**
 140      * Inits the <code>Stroker</code>.
 141      *
 142      * @param pc2d an output <code>PathConsumer2D</code>.
 143      * @param lineWidth the desired line width in pixels
 144      * @param capStyle the desired end cap style, one of
 145      * <code>CAP_BUTT</code>, <code>CAP_ROUND</code> or
 146      * <code>CAP_SQUARE</code>.
 147      * @param joinStyle the desired line join style, one of
 148      * <code>JOIN_MITER</code>, <code>JOIN_ROUND</code> or
 149      * <code>JOIN_BEVEL</code>.
 150      * @param miterLimit the desired miter limit
 151      * @return this instance
 152      */
 153     Stroker init(PathConsumer2D pc2d,
 154               float lineWidth,
 155               int capStyle,
 156               int joinStyle,
 157               float miterLimit)
 158     {
 159         this.out = pc2d;
 160 
 161         this.lineWidth2 = lineWidth / 2f;
 162         this.invHalfLineWidth2Sq = 1f / (2f * lineWidth2 * lineWidth2);
 163         this.capStyle = capStyle;
 164         this.joinStyle = joinStyle;
 165 
 166         float limit = miterLimit * lineWidth2;
 167         this.miterLimitSq = limit * limit;
 168 
 169         this.prev = CLOSE;
 170 
 171         rdrCtx.stroking = 1;
 172 
 173         return this; // fluent API


 184             // Force zero-fill dirty arrays:
 185             Arrays.fill(offset0, 0f);
 186             Arrays.fill(offset1, 0f);
 187             Arrays.fill(offset2, 0f);
 188             Arrays.fill(miter, 0f);
 189             Arrays.fill(middle, 0f);
 190             Arrays.fill(lp, 0f);
 191             Arrays.fill(rp, 0f);
 192             Arrays.fill(subdivTs, 0f);
 193         }
 194     }
 195 
 196     private static void computeOffset(final float lx, final float ly,
 197                                       final float w, final float[] m)
 198     {
 199         float len = lx*lx + ly*ly;
 200         if (len == 0f) {
 201             m[0] = 0f;
 202             m[1] = 0f;
 203         } else {
 204             len = (float) sqrt(len);
 205             m[0] =  (ly * w) / len;
 206             m[1] = -(lx * w) / len;
 207         }
 208     }
 209 
 210     // Returns true if the vectors (dx1, dy1) and (dx2, dy2) are
 211     // clockwise (if dx1,dy1 needs to be rotated clockwise to close
 212     // the smallest angle between it and dx2,dy2).
 213     // This is equivalent to detecting whether a point q is on the right side
 214     // of a line passing through points p1, p2 where p2 = p1+(dx1,dy1) and
 215     // q = p2+(dx2,dy2), which is the same as saying p1, p2, q are in a
 216     // clockwise order.
 217     // NOTE: "clockwise" here assumes coordinates with 0,0 at the bottom left.
 218     private static boolean isCW(final float dx1, final float dy1,
 219                                 final float dx2, final float dy2)
 220     {
 221         return dx1 * dy2 <= dy1 * dx2;
 222     }
 223 
 224     private void drawRoundJoin(float x, float y,


 263         switch (numCurves) {
 264         case 1:
 265             drawBezApproxForArc(cx, cy, omx, omy, mx, my, rev);
 266             break;
 267         case 2:
 268             // we need to split the arc into 2 arcs spanning the same angle.
 269             // The point we want will be one of the 2 intersections of the
 270             // perpendicular bisector of the chord (omx,omy)->(mx,my) and the
 271             // circle. We could find this by scaling the vector
 272             // (omx+mx, omy+my)/2 so that it has length=lineWidth2 (and thus lies
 273             // on the circle), but that can have numerical problems when the angle
 274             // between omx,omy and mx,my is close to 180 degrees. So we compute a
 275             // normal of (omx,omy)-(mx,my). This will be the direction of the
 276             // perpendicular bisector. To get one of the intersections, we just scale
 277             // this vector that its length is lineWidth2 (this works because the
 278             // perpendicular bisector goes through the origin). This scaling doesn't
 279             // have numerical problems because we know that lineWidth2 divided by
 280             // this normal's length is at least 0.5 and at most sqrt(2)/2 (because
 281             // we know the angle of the arc is > 90 degrees).
 282             float nx = my - omy, ny = omx - mx;
 283             float nlen = (float) sqrt(nx*nx + ny*ny);
 284             float scale = lineWidth2/nlen;
 285             float mmx = nx * scale, mmy = ny * scale;
 286 
 287             // if (isCW(omx, omy, mx, my) != isCW(mmx, mmy, mx, my)) then we've
 288             // computed the wrong intersection so we get the other one.
 289             // The test above is equivalent to if (rev).
 290             if (rev) {
 291                 mmx = -mmx;
 292                 mmy = -mmy;
 293             }
 294             drawBezApproxForArc(cx, cy, omx, omy, mmx, mmy, rev);
 295             drawBezApproxForArc(cx, cy, mmx, mmy, mx, my, rev);
 296             break;
 297         default:
 298         }
 299     }
 300 
 301     // the input arc defined by omx,omy and mx,my must span <= 90 degrees.
 302     private void drawBezApproxForArc(final float cx, final float cy,
 303                                      final float omx, final float omy,
 304                                      final float mx, final float my,
 305                                      boolean rev)
 306     {
 307         final float cosext2 = (omx * mx + omy * my) * invHalfLineWidth2Sq;
 308 
 309         // check round off errors producing cos(ext) > 1 and a NaN below
 310         // cos(ext) == 1 implies colinear segments and an empty join anyway
 311         if (cosext2 >= 0.5f) {
 312             // just return to avoid generating a flat curve:
 313             return;
 314         }
 315 
 316         // cv is the length of P1-P0 and P2-P3 divided by the radius of the arc
 317         // (so, cv assumes the arc has radius 1). P0, P1, P2, P3 are the points that
 318         // define the bezier curve we're computing.
 319         // It is computed using the constraints that P1-P0 and P3-P2 are parallel
 320         // to the arc tangents at the endpoints, and that |P1-P0|=|P3-P2|.
 321         float cv = (float) ((4.0 / 3.0) * sqrt(0.5 - cosext2) /
 322                             (1.0 + sqrt(cosext2 + 0.5)));
 323         // if clockwise, we need to negate cv.
 324         if (rev) { // rev is equivalent to isCW(omx, omy, mx, my)
 325             cv = -cv;
 326         }
 327         final float x1 = cx + omx;
 328         final float y1 = cy + omy;
 329         final float x2 = x1 - cv * omy;
 330         final float y2 = y1 + cv * omx;
 331 
 332         final float x4 = cx + mx;
 333         final float y4 = cy + my;
 334         final float x3 = x4 + cv * my;
 335         final float y3 = y4 - cv * mx;
 336 
 337         emitCurveTo(x1, y1, x2, y2, x3, y3, x4, y4, rev);
 338     }
 339 
 340     private void drawRoundCap(float cx, float cy, float mx, float my) {
 341         final float Cmx = C * mx;
 342         final float Cmy = C * my;
 343         emitCurveTo(cx + mx - Cmy, cy + my + Cmx,
 344                     cx - my + Cmx, cy + mx + Cmy,
 345                     cx - my,       cy + mx);
 346         emitCurveTo(cx - my - Cmx, cy + mx - Cmy,
 347                     cx - mx - Cmy, cy - my + Cmx,
 348                     cx - mx,       cy - my);
 349     }
 350 
 351     // Put the intersection point of the lines (x0, y0) -> (x1, y1)
 352     // and (x0p, y0p) -> (x1p, y1p) in m[off] and m[off+1].
 353     // If the lines are parallel, it will put a non finite number in m.
 354     private static void computeIntersection(final float x0, final float y0,
 355                                             final float x1, final float y1,
 356                                             final float x0p, final float y0p,
 357                                             final float x1p, final float y1p,
 358                                             final float[] m, int off)
 359     {
 360         float x10 = x1 - x0;
 361         float y10 = y1 - y0;
 362         float x10p = x1p - x0p;
 363         float y10p = y1p - y0p;
 364 









 365         float den = x10*y10p - x10p*y10;
 366         float t = x10p*(y0-y0p) - y10p*(x0-x0p);
 367         t /= den;
 368         m[off++] = x0 + t*x10;
 369         m[off]   = y0 + t*y10;
 370     }
 371 


































 372     private void drawMiter(final float pdx, final float pdy,
 373                            final float x0, final float y0,
 374                            final float dx, final float dy,
 375                            float omx, float omy, float mx, float my,
 376                            boolean rev)
 377     {
 378         if ((mx == omx && my == omy) ||
 379             (pdx == 0f && pdy == 0f) ||
 380             (dx == 0f && dy == 0f))
 381         {
 382             return;
 383         }
 384 
 385         if (rev) {
 386             omx = -omx;
 387             omy = -omy;
 388             mx  = -mx;
 389             my  = -my;
 390         }
 391 
 392         computeIntersection((x0 - pdx) + omx, (y0 - pdy) + omy, x0 + omx, y0 + omy,
 393                             (dx + x0) + mx, (dy + y0) + my, x0 + mx, y0 + my,
 394                             miter, 0);
 395 
 396         final float miterX = miter[0];
 397         final float miterY = miter[1];
 398         float lenSq = (miterX-x0)*(miterX-x0) + (miterY-y0)*(miterY-y0);
 399 
 400         // If the lines are parallel, lenSq will be either NaN or +inf
 401         // (actually, I'm not sure if the latter is possible. The important
 402         // thing is that -inf is not possible, because lenSq is a square).
 403         // For both of those values, the comparison below will fail and
 404         // no miter will be drawn, which is correct.
 405         if (lenSq < miterLimitSq) {
 406             emitLineTo(miterX, miterY, rev);
 407         }
 408     }
 409 
 410     @Override
 411     public void moveTo(float x0, float y0) {
 412         if (prev == DRAWING_OP_TO) {
 413             finish();
 414         }


 640     {
 641         // if p1=p2 or p3=p4 it means that the derivative at the endpoint
 642         // vanishes, which creates problems with computeOffset. Usually
 643         // this happens when this stroker object is trying to winden
 644         // a curve with a cusp. What happens is that curveTo splits
 645         // the input curve at the cusp, and passes it to this function.
 646         // because of inaccuracies in the splitting, we consider points
 647         // equal if they're very close to each other.
 648         final float x1 = pts[off + 0], y1 = pts[off + 1];
 649         final float x2 = pts[off + 2], y2 = pts[off + 3];
 650         final float x3 = pts[off + 4], y3 = pts[off + 5];
 651         final float x4 = pts[off + 6], y4 = pts[off + 7];
 652 
 653         float dx4 = x4 - x3;
 654         float dy4 = y4 - y3;
 655         float dx1 = x2 - x1;
 656         float dy1 = y2 - y1;
 657 
 658         // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
 659         // in which case ignore if p1 == p2
 660         final boolean p1eqp2 = within(x1,y1,x2,y2, 6f * ulp(y2));
 661         final boolean p3eqp4 = within(x3,y3,x4,y4, 6f * ulp(y4));
 662         if (p1eqp2 && p3eqp4) {
 663             getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
 664             return 4;
 665         } else if (p1eqp2) {
 666             dx1 = x3 - x1;
 667             dy1 = y3 - y1;
 668         } else if (p3eqp4) {
 669             dx4 = x4 - x2;
 670             dy4 = y4 - y2;
 671         }
 672 
 673         // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
 674         float dotsq = (dx1 * dx4 + dy1 * dy4);
 675         dotsq *= dotsq;
 676         float l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4;
 677         if (Helpers.within(dotsq, l1sq * l4sq, 4f * ulp(dotsq))) {
 678             getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
 679             return 4;
 680         }
 681 
 682 //      What we're trying to do in this function is to approximate an ideal
 683 //      offset curve (call it I) of the input curve B using a bezier curve Bp.
 684 //      The constraints I use to get the equations are:
 685 //
 686 //      1. The computed curve Bp should go through I(0) and I(1). These are
 687 //      x1p, y1p, x4p, y4p, which are p1p and p4p. We still need to find
 688 //      4 variables: the x and y components of p2p and p3p (i.e. x2p, y2p, x3p, y3p).
 689 //
 690 //      2. Bp should have slope equal in absolute value to I at the endpoints. So,
 691 //      (by the way, the operator || in the comments below means "aligned with".
 692 //      It is defined on vectors, so when we say I'(0) || Bp'(0) we mean that
 693 //      vectors I'(0) and Bp'(0) are aligned, which is the same as saying
 694 //      that the tangent lines of I and Bp at 0 are parallel. Mathematically
 695 //      this means (I'(t) || Bp'(t)) <==> (I'(t) = c * Bp'(t)) where c is some
 696 //      nonzero constant.)
 697 //      I'(0) || Bp'(0) and I'(1) || Bp'(1). Obviously, I'(0) || B'(0) and


 767         xi = xi - 2f * offset1[0]; yi = yi - 2f * offset1[1];
 768         x4p = x4 - offset2[0]; y4p = y4 - offset2[1];
 769 
 770         two_pi_m_p1_m_p4x = 2f * xi - x1p - x4p;
 771         two_pi_m_p1_m_p4y = 2f * yi - y1p - y4p;
 772         c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);
 773         c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);
 774 
 775         x2p = x1p + c1*dx1;
 776         y2p = y1p + c1*dy1;
 777         x3p = x4p + c2*dx4;
 778         y3p = y4p + c2*dy4;
 779 
 780         rightOff[0] = x1p; rightOff[1] = y1p;
 781         rightOff[2] = x2p; rightOff[3] = y2p;
 782         rightOff[4] = x3p; rightOff[5] = y3p;
 783         rightOff[6] = x4p; rightOff[7] = y4p;
 784         return 8;
 785     }
 786 


 787     // return the kind of curve in the right and left arrays.
 788     private int computeOffsetQuad(float[] pts, final int off,
 789                                   float[] leftOff, float[] rightOff)
 790     {
 791         final float x1 = pts[off + 0], y1 = pts[off + 1];
 792         final float x2 = pts[off + 2], y2 = pts[off + 3];
 793         final float x3 = pts[off + 4], y3 = pts[off + 5];
 794 
 795         final float dx3 = x3 - x2;
 796         final float dy3 = y3 - y2;
 797         final float dx1 = x2 - x1;
 798         final float dy1 = y2 - y1;
 799 
 800         // this computes the offsets at t = 0, 1




























 801         computeOffset(dx1, dy1, lineWidth2, offset0);
 802         computeOffset(dx3, dy3, lineWidth2, offset1);
 803 
 804         leftOff[0]  = x1 + offset0[0]; leftOff[1]  = y1 + offset0[1];
 805         leftOff[4]  = x3 + offset1[0]; leftOff[5]  = y3 + offset1[1];
 806         rightOff[0] = x1 - offset0[0]; rightOff[1] = y1 - offset0[1];
 807         rightOff[4] = x3 - offset1[0]; rightOff[5] = y3 - offset1[1];
 808 
 809         float x1p = leftOff[0]; // start
 810         float y1p = leftOff[1]; // point
 811         float x3p = leftOff[4]; // end
 812         float y3p = leftOff[5]; // point
 813 
 814         // Corner cases:
 815         // 1. If the two control vectors are parallel, we'll end up with NaN's
 816         //    in leftOff (and rightOff in the body of the if below), so we'll
 817         //    do getLineOffsets, which is right.
 818         // 2. If the first or second two points are equal, then (dx1,dy1)==(0,0)
 819         //    or (dx3,dy3)==(0,0), so (x1p, y1p)==(x1p+dx1, y1p+dy1)
 820         //    or (x3p, y3p)==(x3p-dx3, y3p-dy3), which means that
 821         //    computeIntersection will put NaN's in leftOff and right off, and
 822         //    we will do getLineOffsets, which is right.
 823         computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2);
 824         float cx = leftOff[2];
 825         float cy = leftOff[3];
 826 
 827         if (!(isFinite(cx) && isFinite(cy))) {
 828             // maybe the right path is not degenerate.
 829             x1p = rightOff[0];
 830             y1p = rightOff[1];
 831             x3p = rightOff[4];
 832             y3p = rightOff[5];
 833             computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2);
 834             cx = rightOff[2];
 835             cy = rightOff[3];
 836             if (!(isFinite(cx) && isFinite(cy))) {
 837                 // both are degenerate. This curve is a line.
 838                 getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
 839                 return 4;
 840             }
 841             // {left,right}Off[0,1,4,5] are already set to the correct values.
 842             leftOff[2] = 2f * x2 - cx;
 843             leftOff[3] = 2f * y2 - cy;
 844             return 6;
 845         }
 846 
 847         // rightOff[2,3] = (x2,y2) - ((left_x2, left_y2) - (x2, y2))
 848         // == 2*(x2, y2) - (left_x2, left_y2)
 849         rightOff[2] = 2f * x2 - cx;
 850         rightOff[3] = 2f * y2 - cy;

 851         return 6;
 852     }
 853 
 854     private static boolean isFinite(float x) {
 855         return (Float.NEGATIVE_INFINITY < x && x < Float.POSITIVE_INFINITY);
 856     }
 857 
 858     // If this class is compiled with ecj, then Hotspot crashes when OSR
 859     // compiling this function. See bugs 7004570 and 6675699
 860     // TODO: until those are fixed, we should work around that by
 861     // manually inlining this into curveTo and quadTo.
 862 /******************************* WORKAROUND **********************************
 863     private void somethingTo(final int type) {
 864         // need these so we can update the state at the end of this method
 865         final float xf = middle[type-2], yf = middle[type-1];
 866         float dxs = middle[2] - middle[0];
 867         float dys = middle[3] - middle[1];
 868         float dxf = middle[type - 2] - middle[type - 4];
 869         float dyf = middle[type - 1] - middle[type - 3];
 870         switch(type) {
 871         case 6:
 872             if ((dxs == 0f && dys == 0f) ||
 873                 (dxf == 0f && dyf == 0f)) {
 874                dxs = dxf = middle[4] - middle[0];
 875                dys = dyf = middle[5] - middle[1];
 876             }
 877             break;


 958         this.cx0 = xf;
 959         this.cy0 = yf;
 960         this.prev = DRAWING_OP_TO;
 961     }
 962 ****************************** END WORKAROUND *******************************/
 963 
 964     // finds values of t where the curve in pts should be subdivided in order
 965     // to get good offset curves a distance of w away from the middle curve.
 966     // Stores the points in ts, and returns how many of them there were.
 967     private static int findSubdivPoints(final Curve c, float[] pts, float[] ts,
 968                                         final int type, final float w)
 969     {
 970         final float x12 = pts[2] - pts[0];
 971         final float y12 = pts[3] - pts[1];
 972         // if the curve is already parallel to either axis we gain nothing
 973         // from rotating it.
 974         if (y12 != 0f && x12 != 0f) {
 975             // we rotate it so that the first vector in the control polygon is
 976             // parallel to the x-axis. This will ensure that rotated quarter
 977             // circles won't be subdivided.
 978             final float hypot = (float) sqrt(x12 * x12 + y12 * y12);
 979             final float cos = x12 / hypot;
 980             final float sin = y12 / hypot;
 981             final float x1 = cos * pts[0] + sin * pts[1];
 982             final float y1 = cos * pts[1] - sin * pts[0];
 983             final float x2 = cos * pts[2] + sin * pts[3];
 984             final float y2 = cos * pts[3] - sin * pts[2];
 985             final float x3 = cos * pts[4] + sin * pts[5];
 986             final float y3 = cos * pts[5] - sin * pts[4];
 987 
 988             switch(type) {
 989             case 8:
 990                 final float x4 = cos * pts[6] + sin * pts[7];
 991                 final float y4 = cos * pts[7] - sin * pts[6];
 992                 c.set(x1, y1, x2, y2, x3, y3, x4, y4);
 993                 break;
 994             case 6:
 995                 c.set(x1, y1, x2, y2, x3, y3);
 996                 break;
 997             default:
 998             }


1051                 dys = mid[7] - mid[1];
1052             }
1053         }
1054         if (p3eqp4) {
1055             dxf = mid[6] - mid[2];
1056             dyf = mid[7] - mid[3];
1057             if (dxf == 0f && dyf == 0f) {
1058                 dxf = mid[6] - mid[0];
1059                 dyf = mid[7] - mid[1];
1060             }
1061         }
1062         if (dxs == 0f && dys == 0f) {
1063             // this happens if the "curve" is just a point
1064             lineTo(mid[0], mid[1]);
1065             return;
1066         }
1067 
1068         // if these vectors are too small, normalize them, to avoid future
1069         // precision problems.
1070         if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
1071             float len = (float) sqrt(dxs*dxs + dys*dys);
1072             dxs /= len;
1073             dys /= len;
1074         }
1075         if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
1076             float len = (float) sqrt(dxf*dxf + dyf*dyf);
1077             dxf /= len;
1078             dyf /= len;
1079         }
1080 
1081         computeOffset(dxs, dys, lineWidth2, offset0);
1082         drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, offset0[0], offset0[1]);
1083 
1084         int nSplits = findSubdivPoints(curve, mid, subdivTs, 8, lineWidth2);








1085 
1086         final float[] l = lp;
1087         final float[] r = rp;
1088 
1089         int kind = 0;
1090         BreakPtrIterator it = curve.breakPtsAtTs(mid, 8, subdivTs, nSplits);
1091         while(it.hasNext()) {
1092             int curCurveOff = it.next();
1093 
1094             kind = computeOffsetCubic(mid, curCurveOff, l, r);
1095             emitLineTo(l[0], l[1]);
1096 
1097             switch(kind) {
1098             case 8:
1099                 emitCurveTo(l[2], l[3], l[4], l[5], l[6], l[7]);
1100                 emitCurveToRev(r[0], r[1], r[2], r[3], r[4], r[5]);
1101                 break;
1102             case 4:
1103                 emitLineTo(l[2], l[3]);
1104                 emitLineToRev(r[0], r[1]);
1105                 break;
1106             default:
1107             }
1108             emitLineToRev(r[kind - 2], r[kind - 1]);
1109         }
1110 
1111         this.cmx = (l[kind - 2] - r[kind - 2]) / 2f;
1112         this.cmy = (l[kind - 1] - r[kind - 1]) / 2f;
1113         this.cdx = dxf;
1114         this.cdy = dyf;


1128         // See the TODO on somethingTo
1129 
1130         // need these so we can update the state at the end of this method
1131         final float xf = mid[4], yf = mid[5];
1132         float dxs = mid[2] - mid[0];
1133         float dys = mid[3] - mid[1];
1134         float dxf = mid[4] - mid[2];
1135         float dyf = mid[5] - mid[3];
1136         if ((dxs == 0f && dys == 0f) || (dxf == 0f && dyf == 0f)) {
1137             dxs = dxf = mid[4] - mid[0];
1138             dys = dyf = mid[5] - mid[1];
1139         }
1140         if (dxs == 0f && dys == 0f) {
1141             // this happens if the "curve" is just a point
1142             lineTo(mid[0], mid[1]);
1143             return;
1144         }
1145         // if these vectors are too small, normalize them, to avoid future
1146         // precision problems.
1147         if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
1148             float len = (float) sqrt(dxs*dxs + dys*dys);
1149             dxs /= len;
1150             dys /= len;
1151         }
1152         if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
1153             float len = (float) sqrt(dxf*dxf + dyf*dyf);
1154             dxf /= len;
1155             dyf /= len;
1156         }
1157 
1158         computeOffset(dxs, dys, lineWidth2, offset0);
1159         drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, offset0[0], offset0[1]);
1160 
1161         int nSplits = findSubdivPoints(curve, mid, subdivTs, 6, lineWidth2);
1162 








1163         final float[] l = lp;
1164         final float[] r = rp;
1165 
1166         int kind = 0;
1167         BreakPtrIterator it = curve.breakPtsAtTs(mid, 6, subdivTs, nSplits);
1168         while(it.hasNext()) {
1169             int curCurveOff = it.next();
1170 
1171             kind = computeOffsetQuad(mid, curCurveOff, l, r);
1172             emitLineTo(l[0], l[1]);
1173 
1174             switch(kind) {
1175             case 6:
1176                 emitQuadTo(l[2], l[3], l[4], l[5]);
1177                 emitQuadToRev(r[0], r[1], r[2], r[3]);
1178                 break;
1179             case 4:
1180                 emitLineTo(l[2], l[3]);
1181                 emitLineToRev(r[0], r[1]);
1182                 break;
1183             default:
1184             }
1185             emitLineToRev(r[kind - 2], r[kind - 1]);
1186         }
1187 
1188         this.cmx = (l[kind - 2] - r[kind - 2]) / 2f;
1189         this.cmy = (l[kind - 1] - r[kind - 1]) / 2f;
1190         this.cdx = dxf;
1191         this.cdy = dyf;
1192         this.cx0 = xf;
1193         this.cy0 = yf;
1194         this.prev = DRAWING_OP_TO;
1195     }
1196 
1197     @Override public long getNativeConsumer() {
1198         throw new InternalError("Stroker doesn't use a native consumer");
1199     }
1200 
1201     // a stack of polynomial curves where each curve shares endpoints with
1202     // adjacent ones.
1203     static final class PolyStack {
1204         private static final byte TYPE_LINETO  = (byte) 0;
1205         private static final byte TYPE_QUADTO  = (byte) 1;
1206         private static final byte TYPE_CUBICTO = (byte) 2;
1207 
1208         // curves capacity = edges count (4096) = half edges x 2 (coords)
1209         private static final int INITIAL_CURVES_COUNT = INITIAL_EDGES_COUNT;
1210 
1211         // types capacity = half edges count (2048)
1212         private static final int INITIAL_TYPES_COUNT = INITIAL_EDGES_COUNT >> 1;
1213 
1214         float[] curves;
1215         int end;
1216         byte[] curveTypes;
1217         int numCurves;
1218 
1219         // per-thread renderer context
1220         final RendererContext rdrCtx;
1221 
1222         // curves ref (dirty)
1223         final FloatArrayCache.Reference curves_ref;
1224         // curveTypes ref (dirty)
1225         final ByteArrayCache.Reference curveTypes_ref;
1226 
1227         // used marks (stats only)
1228         int curveTypesUseMark;
1229         int curvesUseMark;
1230 
1231         /**
1232          * Constructor
1233          * @param rdrCtx per-thread renderer context
1234          */
1235         PolyStack(final RendererContext rdrCtx) {
1236             this.rdrCtx = rdrCtx;
1237 
1238             curves_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_CURVES_COUNT); // 16K
1239             curves     = curves_ref.initial;
1240 
1241             curveTypes_ref = rdrCtx.newDirtyByteArrayRef(INITIAL_TYPES_COUNT); // 2K
1242             curveTypes     = curveTypes_ref.initial;
1243             numCurves = 0;
1244             end = 0;
1245 
1246             if (DO_STATS) {
1247                 curveTypesUseMark = 0;
1248                 curvesUseMark = 0;
1249             }
1250         }
1251 
1252         /**
1253          * Disposes this PolyStack:
1254          * clean up before reusing this instance
1255          */
1256         void dispose() {
1257             end = 0;
1258             numCurves = 0;
1259 
1260             if (DO_STATS) {
1261                 rdrCtx.stats.stat_rdr_poly_stack_types.add(curveTypesUseMark);


1352                     io.quadTo(_curves[e+0], _curves[e+1],
1353                               _curves[e+2], _curves[e+3]);
1354                     continue;
1355                 case TYPE_CUBICTO:
1356                     e -= 6;
1357                     io.curveTo(_curves[e+0], _curves[e+1],
1358                                _curves[e+2], _curves[e+3],
1359                                _curves[e+4], _curves[e+5]);
1360                     continue;
1361                 default:
1362                 }
1363             }
1364             numCurves = 0;
1365             end = 0;
1366         }
1367 
1368         @Override
1369         public String toString() {
1370             String ret = "";
1371             int nc = numCurves;
1372             int e  = end;
1373             int len;
1374             while (nc != 0) {
1375                 switch(curveTypes[--nc]) {
1376                 case TYPE_LINETO:
1377                     len = 2;
1378                     ret += "line: ";
1379                     break;
1380                 case TYPE_QUADTO:
1381                     len = 4;
1382                     ret += "quad: ";
1383                     break;
1384                 case TYPE_CUBICTO:
1385                     len = 6;
1386                     ret += "cubic: ";
1387                     break;
1388                 default:
1389                     len = 0;
1390                 }
1391                 e -= len;
1392                 ret += Arrays.toString(Arrays.copyOfRange(curves, e, e+len))
1393                                        + "\n";
1394             }
1395             return ret;
1396         }
1397     }
1398 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.marlin;
  27 
  28 import java.util.Arrays;





  29 
  30 import com.sun.javafx.geom.PathConsumer2D;
  31 
  32 // TODO: some of the arithmetic here is too verbose and prone to hard to
  33 // debug typos. We should consider making a small Point/Vector class that
  34 // has methods like plus(Point), minus(Point), dot(Point), cross(Point)and such
  35 public final class Stroker implements PathConsumer2D, MarlinConst {
  36 
  37     private static final int MOVE_TO = 0;
  38     private static final int DRAWING_OP_TO = 1; // ie. curve, line, or quad
  39     private static final int CLOSE = 2;
  40 
  41     /**
  42      * Constant value for join style.
  43      */
  44     public static final int JOIN_MITER = 0;
  45 
  46     /**
  47      * Constant value for join style.
  48      */
  49     public static final int JOIN_ROUND = 1;
  50 
  51     /**
  52      * Constant value for join style.
  53      */
  54     public static final int JOIN_BEVEL = 2;
  55 


  91     private final float[] miter = new float[2];
  92     private float miterLimitSq;
  93 
  94     private int prev;
  95 
  96     // The starting point of the path, and the slope there.
  97     private float sx0, sy0, sdx, sdy;
  98     // the current point and the slope there.
  99     private float cx0, cy0, cdx, cdy; // c stands for current
 100     // vectors that when added to (sx0,sy0) and (cx0,cy0) respectively yield the
 101     // first and last points on the left parallel path. Since this path is
 102     // parallel, it's slope at any point is parallel to the slope of the
 103     // original path (thought they may have different directions), so these
 104     // could be computed from sdx,sdy and cdx,cdy (and vice versa), but that
 105     // would be error prone and hard to read, so we keep these anyway.
 106     private float smx, smy, cmx, cmy;
 107 
 108     private final PolyStack reverse;
 109 
 110     // This is where the curve to be processed is put. We give it
 111     // enough room to store all curves.
 112     private final float[] middle = new float[MAX_N_CURVES * 8];

 113     private final float[] lp = new float[8];
 114     private final float[] rp = new float[8];
 115     private final float[] subdivTs = new float[MAX_N_CURVES - 1];
 116 
 117     // per-thread renderer context
 118     final RendererContext rdrCtx;
 119 
 120     // dirty curve
 121     final Curve curve;
 122 
 123     /**
 124      * Constructs a <code>Stroker</code>.
 125      * @param rdrCtx per-thread renderer context
 126      */
 127     Stroker(final RendererContext rdrCtx) {
 128         this.rdrCtx = rdrCtx;
 129 
 130         this.reverse = new PolyStack(rdrCtx);
 131         this.curve = rdrCtx.curve;
 132     }
 133 
 134     /**
 135      * Inits the <code>Stroker</code>.
 136      *
 137      * @param pc2d an output <code>PathConsumer2D</code>.
 138      * @param lineWidth the desired line width in pixels
 139      * @param capStyle the desired end cap style, one of
 140      * <code>CAP_BUTT</code>, <code>CAP_ROUND</code> or
 141      * <code>CAP_SQUARE</code>.
 142      * @param joinStyle the desired line join style, one of
 143      * <code>JOIN_MITER</code>, <code>JOIN_ROUND</code> or
 144      * <code>JOIN_BEVEL</code>.
 145      * @param miterLimit the desired miter limit
 146      * @return this instance
 147      */
 148     public Stroker init(PathConsumer2D pc2d,
 149               float lineWidth,
 150               int capStyle,
 151               int joinStyle,
 152               float miterLimit)
 153     {
 154         this.out = pc2d;
 155 
 156         this.lineWidth2 = lineWidth / 2f;
 157         this.invHalfLineWidth2Sq = 1f / (2f * lineWidth2 * lineWidth2);
 158         this.capStyle = capStyle;
 159         this.joinStyle = joinStyle;
 160 
 161         float limit = miterLimit * lineWidth2;
 162         this.miterLimitSq = limit * limit;
 163 
 164         this.prev = CLOSE;
 165 
 166         rdrCtx.stroking = 1;
 167 
 168         return this; // fluent API


 179             // Force zero-fill dirty arrays:
 180             Arrays.fill(offset0, 0f);
 181             Arrays.fill(offset1, 0f);
 182             Arrays.fill(offset2, 0f);
 183             Arrays.fill(miter, 0f);
 184             Arrays.fill(middle, 0f);
 185             Arrays.fill(lp, 0f);
 186             Arrays.fill(rp, 0f);
 187             Arrays.fill(subdivTs, 0f);
 188         }
 189     }
 190 
 191     private static void computeOffset(final float lx, final float ly,
 192                                       final float w, final float[] m)
 193     {
 194         float len = lx*lx + ly*ly;
 195         if (len == 0f) {
 196             m[0] = 0f;
 197             m[1] = 0f;
 198         } else {
 199             len = (float) Math.sqrt(len);
 200             m[0] =  (ly * w) / len;
 201             m[1] = -(lx * w) / len;
 202         }
 203     }
 204 
 205     // Returns true if the vectors (dx1, dy1) and (dx2, dy2) are
 206     // clockwise (if dx1,dy1 needs to be rotated clockwise to close
 207     // the smallest angle between it and dx2,dy2).
 208     // This is equivalent to detecting whether a point q is on the right side
 209     // of a line passing through points p1, p2 where p2 = p1+(dx1,dy1) and
 210     // q = p2+(dx2,dy2), which is the same as saying p1, p2, q are in a
 211     // clockwise order.
 212     // NOTE: "clockwise" here assumes coordinates with 0,0 at the bottom left.
 213     private static boolean isCW(final float dx1, final float dy1,
 214                                 final float dx2, final float dy2)
 215     {
 216         return dx1 * dy2 <= dy1 * dx2;
 217     }
 218 
 219     private void drawRoundJoin(float x, float y,


 258         switch (numCurves) {
 259         case 1:
 260             drawBezApproxForArc(cx, cy, omx, omy, mx, my, rev);
 261             break;
 262         case 2:
 263             // we need to split the arc into 2 arcs spanning the same angle.
 264             // The point we want will be one of the 2 intersections of the
 265             // perpendicular bisector of the chord (omx,omy)->(mx,my) and the
 266             // circle. We could find this by scaling the vector
 267             // (omx+mx, omy+my)/2 so that it has length=lineWidth2 (and thus lies
 268             // on the circle), but that can have numerical problems when the angle
 269             // between omx,omy and mx,my is close to 180 degrees. So we compute a
 270             // normal of (omx,omy)-(mx,my). This will be the direction of the
 271             // perpendicular bisector. To get one of the intersections, we just scale
 272             // this vector that its length is lineWidth2 (this works because the
 273             // perpendicular bisector goes through the origin). This scaling doesn't
 274             // have numerical problems because we know that lineWidth2 divided by
 275             // this normal's length is at least 0.5 and at most sqrt(2)/2 (because
 276             // we know the angle of the arc is > 90 degrees).
 277             float nx = my - omy, ny = omx - mx;
 278             float nlen = (float) Math.sqrt(nx*nx + ny*ny);
 279             float scale = lineWidth2/nlen;
 280             float mmx = nx * scale, mmy = ny * scale;
 281 
 282             // if (isCW(omx, omy, mx, my) != isCW(mmx, mmy, mx, my)) then we've
 283             // computed the wrong intersection so we get the other one.
 284             // The test above is equivalent to if (rev).
 285             if (rev) {
 286                 mmx = -mmx;
 287                 mmy = -mmy;
 288             }
 289             drawBezApproxForArc(cx, cy, omx, omy, mmx, mmy, rev);
 290             drawBezApproxForArc(cx, cy, mmx, mmy, mx, my, rev);
 291             break;
 292         default:
 293         }
 294     }
 295 
 296     // the input arc defined by omx,omy and mx,my must span <= 90 degrees.
 297     private void drawBezApproxForArc(final float cx, final float cy,
 298                                      final float omx, final float omy,
 299                                      final float mx, final float my,
 300                                      boolean rev)
 301     {
 302         final float cosext2 = (omx * mx + omy * my) * invHalfLineWidth2Sq;
 303 
 304         // check round off errors producing cos(ext) > 1 and a NaN below
 305         // cos(ext) == 1 implies colinear segments and an empty join anyway
 306         if (cosext2 >= 0.5f) {
 307             // just return to avoid generating a flat curve:
 308             return;
 309         }
 310 
 311         // cv is the length of P1-P0 and P2-P3 divided by the radius of the arc
 312         // (so, cv assumes the arc has radius 1). P0, P1, P2, P3 are the points that
 313         // define the bezier curve we're computing.
 314         // It is computed using the constraints that P1-P0 and P3-P2 are parallel
 315         // to the arc tangents at the endpoints, and that |P1-P0|=|P3-P2|.
 316         float cv = (float) ((4.0 / 3.0) * Math.sqrt(0.5 - cosext2) /
 317                             (1.0 + Math.sqrt(cosext2 + 0.5)));
 318         // if clockwise, we need to negate cv.
 319         if (rev) { // rev is equivalent to isCW(omx, omy, mx, my)
 320             cv = -cv;
 321         }
 322         final float x1 = cx + omx;
 323         final float y1 = cy + omy;
 324         final float x2 = x1 - cv * omy;
 325         final float y2 = y1 + cv * omx;
 326 
 327         final float x4 = cx + mx;
 328         final float y4 = cy + my;
 329         final float x3 = x4 + cv * my;
 330         final float y3 = y4 - cv * mx;
 331 
 332         emitCurveTo(x1, y1, x2, y2, x3, y3, x4, y4, rev);
 333     }
 334 
 335     private void drawRoundCap(float cx, float cy, float mx, float my) {
 336         final float Cmx = C * mx;
 337         final float Cmy = C * my;
 338         emitCurveTo(cx + mx - Cmy, cy + my + Cmx,
 339                     cx - my + Cmx, cy + mx + Cmy,
 340                     cx - my,       cy + mx);
 341         emitCurveTo(cx - my - Cmx, cy + mx - Cmy,
 342                     cx - mx - Cmy, cy - my + Cmx,
 343                     cx - mx,       cy - my);
 344     }
 345 
 346     // Return the intersection point of the lines (x0, y0) -> (x1, y1)
 347     // and (x0p, y0p) -> (x1p, y1p) in m[0] and m[1]
 348     private static void computeMiter(final float x0, final float y0,
 349                                      final float x1, final float y1,
 350                                      final float x0p, final float y0p,
 351                                      final float x1p, final float y1p,
 352                                      final float[] m, int off)

 353     {
 354         float x10 = x1 - x0;
 355         float y10 = y1 - y0;
 356         float x10p = x1p - x0p;
 357         float y10p = y1p - y0p;
 358 
 359         // if this is 0, the lines are parallel. If they go in the
 360         // same direction, there is no intersection so m[off] and
 361         // m[off+1] will contain infinity, so no miter will be drawn.
 362         // If they go in the same direction that means that the start of the
 363         // current segment and the end of the previous segment have the same
 364         // tangent, in which case this method won't even be involved in
 365         // miter drawing because it won't be called by drawMiter (because
 366         // (mx == omx && my == omy) will be true, and drawMiter will return
 367         // immediately).
 368         float den = x10*y10p - x10p*y10;
 369         float t = x10p*(y0-y0p) - y10p*(x0-x0p);
 370         t /= den;
 371         m[off++] = x0 + t*x10;
 372         m[off]   = y0 + t*y10;
 373     }
 374 
 375     // Return the intersection point of the lines (x0, y0) -> (x1, y1)
 376     // and (x0p, y0p) -> (x1p, y1p) in m[0] and m[1]
 377     private static void safecomputeMiter(final float x0, final float y0,
 378                                          final float x1, final float y1,
 379                                          final float x0p, final float y0p,
 380                                          final float x1p, final float y1p,
 381                                          final float[] m, int off)
 382     {
 383         float x10 = x1 - x0;
 384         float y10 = y1 - y0;
 385         float x10p = x1p - x0p;
 386         float y10p = y1p - y0p;
 387 
 388         // if this is 0, the lines are parallel. If they go in the
 389         // same direction, there is no intersection so m[off] and
 390         // m[off+1] will contain infinity, so no miter will be drawn.
 391         // If they go in the same direction that means that the start of the
 392         // current segment and the end of the previous segment have the same
 393         // tangent, in which case this method won't even be involved in
 394         // miter drawing because it won't be called by drawMiter (because
 395         // (mx == omx && my == omy) will be true, and drawMiter will return
 396         // immediately).
 397         float den = x10*y10p - x10p*y10;
 398         if (den == 0f) {
 399             m[off++] = (x0 + x0p) / 2f;
 400             m[off] = (y0 + y0p) / 2f;
 401             return;
 402         }
 403         float t = x10p*(y0-y0p) - y10p*(x0-x0p);
 404         t /= den;
 405         m[off++] = x0 + t*x10;
 406         m[off] = y0 + t*y10;
 407     }
 408 
 409     private void drawMiter(final float pdx, final float pdy,
 410                            final float x0, final float y0,
 411                            final float dx, final float dy,
 412                            float omx, float omy, float mx, float my,
 413                            boolean rev)
 414     {
 415         if ((mx == omx && my == omy) ||
 416             (pdx == 0f && pdy == 0f) ||
 417             (dx == 0f && dy == 0f))
 418         {
 419             return;
 420         }
 421 
 422         if (rev) {
 423             omx = -omx;
 424             omy = -omy;
 425             mx  = -mx;
 426             my  = -my;
 427         }
 428 
 429         computeMiter((x0 - pdx) + omx, (y0 - pdy) + omy, x0 + omx, y0 + omy,
 430                      (dx + x0) + mx, (dy + y0) + my, x0 + mx, y0 + my,
 431                      miter, 0);
 432 
 433         final float miterX = miter[0];
 434         final float miterY = miter[1];
 435         float lenSq = (miterX-x0)*(miterX-x0) + (miterY-y0)*(miterY-y0);
 436 
 437         // If the lines are parallel, lenSq will be either NaN or +inf
 438         // (actually, I'm not sure if the latter is possible. The important
 439         // thing is that -inf is not possible, because lenSq is a square).
 440         // For both of those values, the comparison below will fail and
 441         // no miter will be drawn, which is correct.
 442         if (lenSq < miterLimitSq) {
 443             emitLineTo(miterX, miterY, rev);
 444         }
 445     }
 446 
 447     @Override
 448     public void moveTo(float x0, float y0) {
 449         if (prev == DRAWING_OP_TO) {
 450             finish();
 451         }


 677     {
 678         // if p1=p2 or p3=p4 it means that the derivative at the endpoint
 679         // vanishes, which creates problems with computeOffset. Usually
 680         // this happens when this stroker object is trying to winden
 681         // a curve with a cusp. What happens is that curveTo splits
 682         // the input curve at the cusp, and passes it to this function.
 683         // because of inaccuracies in the splitting, we consider points
 684         // equal if they're very close to each other.
 685         final float x1 = pts[off + 0], y1 = pts[off + 1];
 686         final float x2 = pts[off + 2], y2 = pts[off + 3];
 687         final float x3 = pts[off + 4], y3 = pts[off + 5];
 688         final float x4 = pts[off + 6], y4 = pts[off + 7];
 689 
 690         float dx4 = x4 - x3;
 691         float dy4 = y4 - y3;
 692         float dx1 = x2 - x1;
 693         float dy1 = y2 - y1;
 694 
 695         // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
 696         // in which case ignore if p1 == p2
 697         final boolean p1eqp2 = within(x1,y1,x2,y2, 6f * Math.ulp(y2));
 698         final boolean p3eqp4 = within(x3,y3,x4,y4, 6f * Math.ulp(y4));
 699         if (p1eqp2 && p3eqp4) {
 700             getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
 701             return 4;
 702         } else if (p1eqp2) {
 703             dx1 = x3 - x1;
 704             dy1 = y3 - y1;
 705         } else if (p3eqp4) {
 706             dx4 = x4 - x2;
 707             dy4 = y4 - y2;
 708         }
 709 
 710         // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
 711         float dotsq = (dx1 * dx4 + dy1 * dy4);
 712         dotsq *= dotsq;
 713         float l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4;
 714         if (Helpers.within(dotsq, l1sq * l4sq, 4f * Math.ulp(dotsq))) {
 715             getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
 716             return 4;
 717         }
 718 
 719 //      What we're trying to do in this function is to approximate an ideal
 720 //      offset curve (call it I) of the input curve B using a bezier curve Bp.
 721 //      The constraints I use to get the equations are:
 722 //
 723 //      1. The computed curve Bp should go through I(0) and I(1). These are
 724 //      x1p, y1p, x4p, y4p, which are p1p and p4p. We still need to find
 725 //      4 variables: the x and y components of p2p and p3p (i.e. x2p, y2p, x3p, y3p).
 726 //
 727 //      2. Bp should have slope equal in absolute value to I at the endpoints. So,
 728 //      (by the way, the operator || in the comments below means "aligned with".
 729 //      It is defined on vectors, so when we say I'(0) || Bp'(0) we mean that
 730 //      vectors I'(0) and Bp'(0) are aligned, which is the same as saying
 731 //      that the tangent lines of I and Bp at 0 are parallel. Mathematically
 732 //      this means (I'(t) || Bp'(t)) <==> (I'(t) = c * Bp'(t)) where c is some
 733 //      nonzero constant.)
 734 //      I'(0) || Bp'(0) and I'(1) || Bp'(1). Obviously, I'(0) || B'(0) and


 804         xi = xi - 2f * offset1[0]; yi = yi - 2f * offset1[1];
 805         x4p = x4 - offset2[0]; y4p = y4 - offset2[1];
 806 
 807         two_pi_m_p1_m_p4x = 2f * xi - x1p - x4p;
 808         two_pi_m_p1_m_p4y = 2f * yi - y1p - y4p;
 809         c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);
 810         c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);
 811 
 812         x2p = x1p + c1*dx1;
 813         y2p = y1p + c1*dy1;
 814         x3p = x4p + c2*dx4;
 815         y3p = y4p + c2*dy4;
 816 
 817         rightOff[0] = x1p; rightOff[1] = y1p;
 818         rightOff[2] = x2p; rightOff[3] = y2p;
 819         rightOff[4] = x3p; rightOff[5] = y3p;
 820         rightOff[6] = x4p; rightOff[7] = y4p;
 821         return 8;
 822     }
 823 
 824     // compute offset curves using bezier spline through t=0.5 (i.e.
 825     // ComputedCurve(0.5) == IdealParallelCurve(0.5))
 826     // return the kind of curve in the right and left arrays.
 827     private int computeOffsetQuad(float[] pts, final int off,
 828                                   float[] leftOff, float[] rightOff)
 829     {
 830         final float x1 = pts[off + 0], y1 = pts[off + 1];
 831         final float x2 = pts[off + 2], y2 = pts[off + 3];
 832         final float x3 = pts[off + 4], y3 = pts[off + 5];
 833 
 834         final float dx3 = x3 - x2;
 835         final float dy3 = y3 - y2;
 836         final float dx1 = x2 - x1;
 837         final float dy1 = y2 - y1;
 838 
 839         // if p1=p2 or p3=p4 it means that the derivative at the endpoint
 840         // vanishes, which creates problems with computeOffset. Usually
 841         // this happens when this stroker object is trying to winden
 842         // a curve with a cusp. What happens is that curveTo splits
 843         // the input curve at the cusp, and passes it to this function.
 844         // because of inaccuracies in the splitting, we consider points
 845         // equal if they're very close to each other.
 846 
 847         // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
 848         // in which case ignore.
 849         final boolean p1eqp2 = within(x1,y1,x2,y2, 6f * Math.ulp(y2));
 850         final boolean p2eqp3 = within(x2,y2,x3,y3, 6f * Math.ulp(y3));
 851         if (p1eqp2 || p2eqp3) {
 852             getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
 853             return 4;
 854         }
 855 
 856         // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
 857         float dotsq = (dx1 * dx3 + dy1 * dy3);
 858         dotsq *= dotsq;
 859         float l1sq = dx1 * dx1 + dy1 * dy1, l3sq = dx3 * dx3 + dy3 * dy3;
 860         if (Helpers.within(dotsq, l1sq * l3sq, 4f * Math.ulp(dotsq))) {
 861             getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
 862             return 4;
 863         }
 864 
 865         // this computes the offsets at t=0, 0.5, 1, using the property that
 866         // for any bezier curve the vectors p2-p1 and p4-p3 are parallel to
 867         // the (dx/dt, dy/dt) vectors at the endpoints.
 868         computeOffset(dx1, dy1, lineWidth2, offset0);
 869         computeOffset(dx3, dy3, lineWidth2, offset1);
 870 
 871         float x1p = x1 + offset0[0]; // start
 872         float y1p = y1 + offset0[1]; // point
 873         float x3p = x3 + offset1[0]; // end
 874         float y3p = y3 + offset1[1]; // point
 875         safecomputeMiter(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2);
 876         leftOff[0] = x1p; leftOff[1] = y1p;
 877         leftOff[4] = x3p; leftOff[5] = y3p;



































 878 
 879         x1p = x1 - offset0[0]; y1p = y1 - offset0[1];
 880         x3p = x3 - offset1[0]; y3p = y3 - offset1[1];
 881         safecomputeMiter(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2);
 882         rightOff[0] = x1p; rightOff[1] = y1p;
 883         rightOff[4] = x3p; rightOff[5] = y3p;
 884         return 6;
 885     }
 886 




 887     // If this class is compiled with ecj, then Hotspot crashes when OSR
 888     // compiling this function. See bugs 7004570 and 6675699
 889     // TODO: until those are fixed, we should work around that by
 890     // manually inlining this into curveTo and quadTo.
 891 /******************************* WORKAROUND **********************************
 892     private void somethingTo(final int type) {
 893         // need these so we can update the state at the end of this method
 894         final float xf = middle[type-2], yf = middle[type-1];
 895         float dxs = middle[2] - middle[0];
 896         float dys = middle[3] - middle[1];
 897         float dxf = middle[type - 2] - middle[type - 4];
 898         float dyf = middle[type - 1] - middle[type - 3];
 899         switch(type) {
 900         case 6:
 901             if ((dxs == 0f && dys == 0f) ||
 902                 (dxf == 0f && dyf == 0f)) {
 903                dxs = dxf = middle[4] - middle[0];
 904                dys = dyf = middle[5] - middle[1];
 905             }
 906             break;


 987         this.cx0 = xf;
 988         this.cy0 = yf;
 989         this.prev = DRAWING_OP_TO;
 990     }
 991 ****************************** END WORKAROUND *******************************/
 992 
 993     // finds values of t where the curve in pts should be subdivided in order
 994     // to get good offset curves a distance of w away from the middle curve.
 995     // Stores the points in ts, and returns how many of them there were.
 996     private static int findSubdivPoints(final Curve c, float[] pts, float[] ts,
 997                                         final int type, final float w)
 998     {
 999         final float x12 = pts[2] - pts[0];
1000         final float y12 = pts[3] - pts[1];
1001         // if the curve is already parallel to either axis we gain nothing
1002         // from rotating it.
1003         if (y12 != 0f && x12 != 0f) {
1004             // we rotate it so that the first vector in the control polygon is
1005             // parallel to the x-axis. This will ensure that rotated quarter
1006             // circles won't be subdivided.
1007             final float hypot = (float) Math.sqrt(x12 * x12 + y12 * y12);
1008             final float cos = x12 / hypot;
1009             final float sin = y12 / hypot;
1010             final float x1 = cos * pts[0] + sin * pts[1];
1011             final float y1 = cos * pts[1] - sin * pts[0];
1012             final float x2 = cos * pts[2] + sin * pts[3];
1013             final float y2 = cos * pts[3] - sin * pts[2];
1014             final float x3 = cos * pts[4] + sin * pts[5];
1015             final float y3 = cos * pts[5] - sin * pts[4];
1016 
1017             switch(type) {
1018             case 8:
1019                 final float x4 = cos * pts[6] + sin * pts[7];
1020                 final float y4 = cos * pts[7] - sin * pts[6];
1021                 c.set(x1, y1, x2, y2, x3, y3, x4, y4);
1022                 break;
1023             case 6:
1024                 c.set(x1, y1, x2, y2, x3, y3);
1025                 break;
1026             default:
1027             }


1080                 dys = mid[7] - mid[1];
1081             }
1082         }
1083         if (p3eqp4) {
1084             dxf = mid[6] - mid[2];
1085             dyf = mid[7] - mid[3];
1086             if (dxf == 0f && dyf == 0f) {
1087                 dxf = mid[6] - mid[0];
1088                 dyf = mid[7] - mid[1];
1089             }
1090         }
1091         if (dxs == 0f && dys == 0f) {
1092             // this happens if the "curve" is just a point
1093             lineTo(mid[0], mid[1]);
1094             return;
1095         }
1096 
1097         // if these vectors are too small, normalize them, to avoid future
1098         // precision problems.
1099         if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
1100             float len = (float) Math.sqrt(dxs*dxs + dys*dys);
1101             dxs /= len;
1102             dys /= len;
1103         }
1104         if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
1105             float len = (float) Math.sqrt(dxf*dxf + dyf*dyf);
1106             dxf /= len;
1107             dyf /= len;
1108         }
1109 
1110         computeOffset(dxs, dys, lineWidth2, offset0);
1111         drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, offset0[0], offset0[1]);
1112 
1113         final int nSplits = findSubdivPoints(curve, mid, subdivTs, 8, lineWidth2);
1114 
1115         float prevT = 0f;
1116         for (int i = 0, off = 0; i < nSplits; i++, off += 6) {
1117             final float t = subdivTs[i];
1118             Helpers.subdivideCubicAt((t - prevT) / (1f - prevT),
1119                                      mid, off, mid, off, mid, off + 6);
1120             prevT = t;
1121         }
1122 
1123         final float[] l = lp;
1124         final float[] r = rp;
1125 
1126         int kind = 0;
1127         for (int i = 0, off = 0; i <= nSplits; i++, off += 6) {
1128             kind = computeOffsetCubic(mid, off, l, r);

1129 

1130             emitLineTo(l[0], l[1]);
1131 
1132             switch(kind) {
1133             case 8:
1134                 emitCurveTo(l[2], l[3], l[4], l[5], l[6], l[7]);
1135                 emitCurveToRev(r[0], r[1], r[2], r[3], r[4], r[5]);
1136                 break;
1137             case 4:
1138                 emitLineTo(l[2], l[3]);
1139                 emitLineToRev(r[0], r[1]);
1140                 break;
1141             default:
1142             }
1143             emitLineToRev(r[kind - 2], r[kind - 1]);
1144         }
1145 
1146         this.cmx = (l[kind - 2] - r[kind - 2]) / 2f;
1147         this.cmy = (l[kind - 1] - r[kind - 1]) / 2f;
1148         this.cdx = dxf;
1149         this.cdy = dyf;


1163         // See the TODO on somethingTo
1164 
1165         // need these so we can update the state at the end of this method
1166         final float xf = mid[4], yf = mid[5];
1167         float dxs = mid[2] - mid[0];
1168         float dys = mid[3] - mid[1];
1169         float dxf = mid[4] - mid[2];
1170         float dyf = mid[5] - mid[3];
1171         if ((dxs == 0f && dys == 0f) || (dxf == 0f && dyf == 0f)) {
1172             dxs = dxf = mid[4] - mid[0];
1173             dys = dyf = mid[5] - mid[1];
1174         }
1175         if (dxs == 0f && dys == 0f) {
1176             // this happens if the "curve" is just a point
1177             lineTo(mid[0], mid[1]);
1178             return;
1179         }
1180         // if these vectors are too small, normalize them, to avoid future
1181         // precision problems.
1182         if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
1183             float len = (float) Math.sqrt(dxs*dxs + dys*dys);
1184             dxs /= len;
1185             dys /= len;
1186         }
1187         if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
1188             float len = (float) Math.sqrt(dxf*dxf + dyf*dyf);
1189             dxf /= len;
1190             dyf /= len;
1191         }
1192 
1193         computeOffset(dxs, dys, lineWidth2, offset0);
1194         drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, offset0[0], offset0[1]);
1195 
1196         int nSplits = findSubdivPoints(curve, mid, subdivTs, 6, lineWidth2);
1197 
1198         float prevt = 0f;
1199         for (int i = 0, off = 0; i < nSplits; i++, off += 4) {
1200             final float t = subdivTs[i];
1201             Helpers.subdivideQuadAt((t - prevt) / (1f - prevt),
1202                                     mid, off, mid, off, mid, off + 4);
1203             prevt = t;
1204         }
1205 
1206         final float[] l = lp;
1207         final float[] r = rp;
1208 
1209         int kind = 0;
1210         for (int i = 0, off = 0; i <= nSplits; i++, off += 4) {
1211             kind = computeOffsetQuad(mid, off, l, r);

1212 

1213             emitLineTo(l[0], l[1]);
1214 
1215             switch(kind) {
1216             case 6:
1217                 emitQuadTo(l[2], l[3], l[4], l[5]);
1218                 emitQuadToRev(r[0], r[1], r[2], r[3]);
1219                 break;
1220             case 4:
1221                 emitLineTo(l[2], l[3]);
1222                 emitLineToRev(r[0], r[1]);
1223                 break;
1224             default:
1225             }
1226             emitLineToRev(r[kind - 2], r[kind - 1]);
1227         }
1228 
1229         this.cmx = (l[kind - 2] - r[kind - 2]) / 2f;
1230         this.cmy = (l[kind - 1] - r[kind - 1]) / 2f;
1231         this.cdx = dxf;
1232         this.cdy = dyf;
1233         this.cx0 = xf;
1234         this.cy0 = yf;
1235         this.prev = DRAWING_OP_TO;
1236     }
1237 




1238     // a stack of polynomial curves where each curve shares endpoints with
1239     // adjacent ones.
1240     static final class PolyStack {
1241         private static final byte TYPE_LINETO  = (byte) 0;
1242         private static final byte TYPE_QUADTO  = (byte) 1;
1243         private static final byte TYPE_CUBICTO = (byte) 2;
1244 
1245         // curves capacity = edges count (8192) = edges x 2 (coords)
1246         private static final int INITIAL_CURVES_COUNT = INITIAL_EDGES_COUNT << 1;
1247 
1248         // types capacity = edges count (4096)
1249         private static final int INITIAL_TYPES_COUNT = INITIAL_EDGES_COUNT;
1250 
1251         float[] curves;
1252         int end;
1253         byte[] curveTypes;
1254         int numCurves;
1255 
1256         // per-thread renderer context
1257         final RendererContext rdrCtx;
1258 
1259         // curves ref (dirty)
1260         final FloatArrayCache.Reference curves_ref;
1261         // curveTypes ref (dirty)
1262         final ByteArrayCache.Reference curveTypes_ref;
1263 
1264         // used marks (stats only)
1265         int curveTypesUseMark;
1266         int curvesUseMark;
1267 
1268         /**
1269          * Constructor
1270          * @param rdrCtx per-thread renderer context
1271          */
1272         PolyStack(final RendererContext rdrCtx) {
1273             this.rdrCtx = rdrCtx;
1274 
1275             curves_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_CURVES_COUNT); // 32K
1276             curves     = curves_ref.initial;
1277 
1278             curveTypes_ref = rdrCtx.newDirtyByteArrayRef(INITIAL_TYPES_COUNT); // 4K
1279             curveTypes     = curveTypes_ref.initial;
1280             numCurves = 0;
1281             end = 0;
1282 
1283             if (DO_STATS) {
1284                 curveTypesUseMark = 0;
1285                 curvesUseMark = 0;
1286             }
1287         }
1288 
1289         /**
1290          * Disposes this PolyStack:
1291          * clean up before reusing this instance
1292          */
1293         void dispose() {
1294             end = 0;
1295             numCurves = 0;
1296 
1297             if (DO_STATS) {
1298                 rdrCtx.stats.stat_rdr_poly_stack_types.add(curveTypesUseMark);


1389                     io.quadTo(_curves[e+0], _curves[e+1],
1390                               _curves[e+2], _curves[e+3]);
1391                     continue;
1392                 case TYPE_CUBICTO:
1393                     e -= 6;
1394                     io.curveTo(_curves[e+0], _curves[e+1],
1395                                _curves[e+2], _curves[e+3],
1396                                _curves[e+4], _curves[e+5]);
1397                     continue;
1398                 default:
1399                 }
1400             }
1401             numCurves = 0;
1402             end = 0;
1403         }
1404 
1405         @Override
1406         public String toString() {
1407             String ret = "";
1408             int nc = numCurves;
1409             int last = end;
1410             int len;
1411             while (nc != 0) {
1412                 switch(curveTypes[--nc]) {
1413                 case TYPE_LINETO:
1414                     len = 2;
1415                     ret += "line: ";
1416                     break;
1417                 case TYPE_QUADTO:
1418                     len = 4;
1419                     ret += "quad: ";
1420                     break;
1421                 case TYPE_CUBICTO:
1422                     len = 6;
1423                     ret += "cubic: ";
1424                     break;
1425                 default:
1426                     len = 0;
1427                 }
1428                 last -= len;
1429                 ret += Arrays.toString(Arrays.copyOfRange(curves, last, last+len))
1430                                        + "\n";
1431             }
1432             return ret;
1433         }
1434     }
1435 }
< prev index next >