1 /*
   2  * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 import com.sun.marlin.DHelpers.PolyStack;
  30 
  31 // TODO: some of the arithmetic here is too verbose and prone to hard to
  32 // debug typos. We should consider making a small Point/Vector class that
  33 // has methods like plus(Point), minus(Point), dot(Point), cross(Point)and such
  34 public final class DStroker implements DPathConsumer2D, MarlinConst {
  35 
  36     private static final int MOVE_TO = 0;
  37     private static final int DRAWING_OP_TO = 1; // ie. curve, line, or quad
  38     private static final int CLOSE = 2;
  39 
  40     // pisces used to use fixed point arithmetic with 16 decimal digits. I
  41     // didn't want to change the values of the constant below when I converted
  42     // it to floating point, so that's why the divisions by 2^16 are there.
  43     private static final double ROUND_JOIN_THRESHOLD = 1000.0d/65536.0d;
  44 
  45     // kappa = (4/3) * (SQRT(2) - 1)
  46     private static final double C = (4.0d * (Math.sqrt(2.0d) - 1.0d) / 3.0d);
  47 
  48     // SQRT(2)
  49     private static final double SQRT_2 = Math.sqrt(2.0d);
  50 
  51     private static final int MAX_N_CURVES = 11;
  52 
  53     private DPathConsumer2D out;
  54 
  55     private int capStyle;
  56     private int joinStyle;
  57 
  58     private double lineWidth2;
  59     private double invHalfLineWidth2Sq;
  60 
  61     private final double[] offset0 = new double[2];
  62     private final double[] offset1 = new double[2];
  63     private final double[] offset2 = new double[2];
  64     private final double[] miter = new double[2];
  65     private double miterLimitSq;
  66 
  67     private int prev;
  68 
  69     // The starting point of the path, and the slope there.
  70     private double sx0, sy0, sdx, sdy;
  71     // the current point and the slope there.
  72     private double cx0, cy0, cdx, cdy; // c stands for current
  73     // vectors that when added to (sx0,sy0) and (cx0,cy0) respectively yield the
  74     // first and last points on the left parallel path. Since this path is
  75     // parallel, it's slope at any point is parallel to the slope of the
  76     // original path (thought they may have different directions), so these
  77     // could be computed from sdx,sdy and cdx,cdy (and vice versa), but that
  78     // would be error prone and hard to read, so we keep these anyway.
  79     private double smx, smy, cmx, cmy;
  80 
  81     private final PolyStack reverse;
  82 
  83     // This is where the curve to be processed is put. We give it
  84     // enough room to store all curves.
  85     private final double[] middle = new double[MAX_N_CURVES * 6 + 2];
  86     private final double[] lp = new double[8];
  87     private final double[] rp = new double[8];
  88     private final double[] subdivTs = new double[MAX_N_CURVES - 1];
  89 
  90     // per-thread renderer context
  91     final DRendererContext rdrCtx;
  92 
  93     // dirty curve
  94     final DCurve curve;
  95 
  96     // Bounds of the drawing region, at pixel precision.
  97     private double[] clipRect;
  98 
  99     // the outcode of the current point
 100     private int cOutCode = 0;
 101 
 102     // the outcode of the starting point
 103     private int sOutCode = 0;
 104 
 105     // flag indicating if the path is opened (clipped)
 106     private boolean opened = false;
 107     // flag indicating if the starting point's cap is done
 108     private boolean capStart = false;
 109 
 110     /**
 111      * Constructs a <code>DStroker</code>.
 112      * @param rdrCtx per-thread renderer context
 113      */
 114     DStroker(final DRendererContext rdrCtx) {
 115         this.rdrCtx = rdrCtx;
 116 
 117         this.reverse = (rdrCtx.stats != null) ?
 118             new PolyStack(rdrCtx,
 119                     rdrCtx.stats.stat_str_polystack_types,
 120                     rdrCtx.stats.stat_str_polystack_curves,
 121                     rdrCtx.stats.hist_str_polystack_curves,
 122                     rdrCtx.stats.stat_array_str_polystack_curves,
 123                     rdrCtx.stats.stat_array_str_polystack_types)
 124             : new PolyStack(rdrCtx);
 125 
 126         this.curve = rdrCtx.curve;
 127     }
 128 
 129     /**
 130      * Inits the <code>DStroker</code>.
 131      *
 132      * @param pc2d an output <code>DPathConsumer2D</code>.
 133      * @param lineWidth the desired line width in pixels
 134      * @param capStyle the desired end cap style, one of
 135      * <code>CAP_BUTT</code>, <code>CAP_ROUND</code> or
 136      * <code>CAP_SQUARE</code>.
 137      * @param joinStyle the desired line join style, one of
 138      * <code>JOIN_MITER</code>, <code>JOIN_ROUND</code> or
 139      * <code>JOIN_BEVEL</code>.
 140      * @param miterLimit the desired miter limit
 141      * @param scale scaling factor applied to clip boundaries
 142      * @param rdrOffX renderer's coordinate offset on X axis
 143      * @param rdrOffY renderer's coordinate offset on Y axis
 144      * @return this instance
 145      */
 146     public DStroker init(final DPathConsumer2D pc2d,
 147                          final double lineWidth,
 148                          final int capStyle,
 149                          final int joinStyle,
 150                          final double miterLimit,
 151                          final double scale,
 152                          double rdrOffX,
 153                          double rdrOffY)
 154     {
 155         this.out = pc2d;
 156 
 157         this.lineWidth2 = lineWidth / 2.0d;
 158         this.invHalfLineWidth2Sq = 1.0d / (2.0d * lineWidth2 * lineWidth2);
 159         this.capStyle = capStyle;
 160         this.joinStyle = joinStyle;
 161 
 162         final double limit = miterLimit * lineWidth2;
 163         this.miterLimitSq = limit * limit;
 164 
 165         this.prev = CLOSE;
 166 
 167         rdrCtx.stroking = 1;
 168 
 169         if (rdrCtx.doClip) {
 170             // Adjust the clipping rectangle with the stroker margin (miter limit, width)
 171             double margin = lineWidth2;
 172 
 173             if (capStyle == CAP_SQUARE) {
 174                 margin *= SQRT_2;
 175             }
 176             if ((joinStyle == JOIN_MITER) && (margin < limit)) {
 177                 margin = limit;
 178             }
 179             if (scale != 1.0d) {
 180                 margin  *= scale;
 181                 rdrOffX *= scale;
 182                 rdrOffY *= scale;
 183             }
 184             // add a small rounding error:
 185             margin += 1e-3d;
 186 
 187             // bounds as half-open intervals: minX <= x < maxX and minY <= y < maxY
 188             // adjust clip rectangle (ymin, ymax, xmin, xmax):
 189             final double[] _clipRect = rdrCtx.clipRect;
 190             _clipRect[0] -= margin - rdrOffY;
 191             _clipRect[1] += margin + rdrOffY;
 192             _clipRect[2] -= margin - rdrOffX;
 193             _clipRect[3] += margin + rdrOffX;
 194             this.clipRect = _clipRect;
 195         } else {
 196             this.clipRect = null;
 197             this.cOutCode = 0;
 198             this.sOutCode = 0;
 199         }
 200         return this; // fluent API
 201     }
 202 
 203     /**
 204      * Disposes this stroker:
 205      * clean up before reusing this instance
 206      */
 207     void dispose() {
 208         reverse.dispose();
 209 
 210         opened   = false;
 211         capStart = false;
 212 
 213         if (DO_CLEAN_DIRTY) {
 214             // Force zero-fill dirty arrays:
 215             Arrays.fill(offset0, 0.0d);
 216             Arrays.fill(offset1, 0.0d);
 217             Arrays.fill(offset2, 0.0d);
 218             Arrays.fill(miter, 0.0d);
 219             Arrays.fill(middle, 0.0d);
 220             Arrays.fill(lp, 0.0d);
 221             Arrays.fill(rp, 0.0d);
 222             Arrays.fill(subdivTs, 0.0d);
 223         }
 224     }
 225 
 226     private static void computeOffset(final double lx, final double ly,
 227                                       final double w, final double[] m)
 228     {
 229         double len = lx*lx + ly*ly;
 230         if (len == 0.0d) {
 231             m[0] = 0.0d;
 232             m[1] = 0.0d;
 233         } else {
 234             len = Math.sqrt(len);
 235             m[0] =  (ly * w) / len;
 236             m[1] = -(lx * w) / len;
 237         }
 238     }
 239 
 240     // Returns true if the vectors (dx1, dy1) and (dx2, dy2) are
 241     // clockwise (if dx1,dy1 needs to be rotated clockwise to close
 242     // the smallest angle between it and dx2,dy2).
 243     // This is equivalent to detecting whether a point q is on the right side
 244     // of a line passing through points p1, p2 where p2 = p1+(dx1,dy1) and
 245     // q = p2+(dx2,dy2), which is the same as saying p1, p2, q are in a
 246     // clockwise order.
 247     // NOTE: "clockwise" here assumes coordinates with 0,0 at the bottom left.
 248     private static boolean isCW(final double dx1, final double dy1,
 249                                 final double dx2, final double dy2)
 250     {
 251         return dx1 * dy2 <= dy1 * dx2;
 252     }
 253 
 254     private void drawRoundJoin(double x, double y,
 255                                double omx, double omy, double mx, double my,
 256                                boolean rev,
 257                                double threshold)
 258     {
 259         if ((omx == 0.0d && omy == 0.0d) || (mx == 0.0d && my == 0.0d)) {
 260             return;
 261         }
 262 
 263         double domx = omx - mx;
 264         double domy = omy - my;
 265         double len = domx*domx + domy*domy;
 266         if (len < threshold) {
 267             return;
 268         }
 269 
 270         if (rev) {
 271             omx = -omx;
 272             omy = -omy;
 273             mx  = -mx;
 274             my  = -my;
 275         }
 276         drawRoundJoin(x, y, omx, omy, mx, my, rev);
 277     }
 278 
 279     private void drawRoundJoin(double cx, double cy,
 280                                double omx, double omy,
 281                                double mx, double my,
 282                                boolean rev)
 283     {
 284         // The sign of the dot product of mx,my and omx,omy is equal to the
 285         // the sign of the cosine of ext
 286         // (ext is the angle between omx,omy and mx,my).
 287         final double cosext = omx * mx + omy * my;
 288         // If it is >=0, we know that abs(ext) is <= 90 degrees, so we only
 289         // need 1 curve to approximate the circle section that joins omx,omy
 290         // and mx,my.
 291         final int numCurves = (cosext >= 0.0d) ? 1 : 2;
 292 
 293         switch (numCurves) {
 294         case 1:
 295             drawBezApproxForArc(cx, cy, omx, omy, mx, my, rev);
 296             break;
 297         case 2:
 298             // we need to split the arc into 2 arcs spanning the same angle.
 299             // The point we want will be one of the 2 intersections of the
 300             // perpendicular bisector of the chord (omx,omy)->(mx,my) and the
 301             // circle. We could find this by scaling the vector
 302             // (omx+mx, omy+my)/2 so that it has length=lineWidth2 (and thus lies
 303             // on the circle), but that can have numerical problems when the angle
 304             // between omx,omy and mx,my is close to 180 degrees. So we compute a
 305             // normal of (omx,omy)-(mx,my). This will be the direction of the
 306             // perpendicular bisector. To get one of the intersections, we just scale
 307             // this vector that its length is lineWidth2 (this works because the
 308             // perpendicular bisector goes through the origin). This scaling doesn't
 309             // have numerical problems because we know that lineWidth2 divided by
 310             // this normal's length is at least 0.5 and at most sqrt(2)/2 (because
 311             // we know the angle of the arc is > 90 degrees).
 312             double nx = my - omy, ny = omx - mx;
 313             double nlen = Math.sqrt(nx*nx + ny*ny);
 314             double scale = lineWidth2/nlen;
 315             double mmx = nx * scale, mmy = ny * scale;
 316 
 317             // if (isCW(omx, omy, mx, my) != isCW(mmx, mmy, mx, my)) then we've
 318             // computed the wrong intersection so we get the other one.
 319             // The test above is equivalent to if (rev).
 320             if (rev) {
 321                 mmx = -mmx;
 322                 mmy = -mmy;
 323             }
 324             drawBezApproxForArc(cx, cy, omx, omy, mmx, mmy, rev);
 325             drawBezApproxForArc(cx, cy, mmx, mmy, mx, my, rev);
 326             break;
 327         default:
 328         }
 329     }
 330 
 331     // the input arc defined by omx,omy and mx,my must span <= 90 degrees.
 332     private void drawBezApproxForArc(final double cx, final double cy,
 333                                      final double omx, final double omy,
 334                                      final double mx, final double my,
 335                                      boolean rev)
 336     {
 337         final double cosext2 = (omx * mx + omy * my) * invHalfLineWidth2Sq;
 338 
 339         // check round off errors producing cos(ext) > 1 and a NaN below
 340         // cos(ext) == 1 implies colinear segments and an empty join anyway
 341         if (cosext2 >= 0.5d) {
 342             // just return to avoid generating a flat curve:
 343             return;
 344         }
 345 
 346         // cv is the length of P1-P0 and P2-P3 divided by the radius of the arc
 347         // (so, cv assumes the arc has radius 1). P0, P1, P2, P3 are the points that
 348         // define the bezier curve we're computing.
 349         // It is computed using the constraints that P1-P0 and P3-P2 are parallel
 350         // to the arc tangents at the endpoints, and that |P1-P0|=|P3-P2|.
 351         double cv = ((4.0d / 3.0d) * Math.sqrt(0.5d - cosext2) /
 352                             (1.0d + Math.sqrt(cosext2 + 0.5d)));
 353         // if clockwise, we need to negate cv.
 354         if (rev) { // rev is equivalent to isCW(omx, omy, mx, my)
 355             cv = -cv;
 356         }
 357         final double x1 = cx + omx;
 358         final double y1 = cy + omy;
 359         final double x2 = x1 - cv * omy;
 360         final double y2 = y1 + cv * omx;
 361 
 362         final double x4 = cx + mx;
 363         final double y4 = cy + my;
 364         final double x3 = x4 + cv * my;
 365         final double y3 = y4 - cv * mx;
 366 
 367         emitCurveTo(x1, y1, x2, y2, x3, y3, x4, y4, rev);
 368     }
 369 
 370     private void drawRoundCap(double cx, double cy, double mx, double my) {
 371         final double Cmx = C * mx;
 372         final double Cmy = C * my;
 373         emitCurveTo(cx + mx - Cmy, cy + my + Cmx,
 374                     cx - my + Cmx, cy + mx + Cmy,
 375                     cx - my,       cy + mx);
 376         emitCurveTo(cx - my - Cmx, cy + mx - Cmy,
 377                     cx - mx - Cmy, cy - my + Cmx,
 378                     cx - mx,       cy - my);
 379     }
 380 
 381     // Return the intersection point of the lines (x0, y0) -> (x1, y1)
 382     // and (x0p, y0p) -> (x1p, y1p) in m[off] and m[off+1]
 383     private static void computeMiter(final double x0, final double y0,
 384                                      final double x1, final double y1,
 385                                      final double x0p, final double y0p,
 386                                      final double x1p, final double y1p,
 387                                      final double[] m, int off)
 388     {
 389         double x10 = x1 - x0;
 390         double y10 = y1 - y0;
 391         double x10p = x1p - x0p;
 392         double y10p = y1p - y0p;
 393 
 394         // if this is 0, the lines are parallel. If they go in the
 395         // same direction, there is no intersection so m[off] and
 396         // m[off+1] will contain infinity, so no miter will be drawn.
 397         // If they go in the same direction that means that the start of the
 398         // current segment and the end of the previous segment have the same
 399         // tangent, in which case this method won't even be involved in
 400         // miter drawing because it won't be called by drawMiter (because
 401         // (mx == omx && my == omy) will be true, and drawMiter will return
 402         // immediately).
 403         double den = x10*y10p - x10p*y10;
 404         double t = x10p*(y0-y0p) - y10p*(x0-x0p);
 405         t /= den;
 406         m[off++] = x0 + t*x10;
 407         m[off]   = y0 + t*y10;
 408     }
 409 
 410     // Return the intersection point of the lines (x0, y0) -> (x1, y1)
 411     // and (x0p, y0p) -> (x1p, y1p) in m[off] and m[off+1]
 412     private static void safeComputeMiter(final double x0, final double y0,
 413                                          final double x1, final double y1,
 414                                          final double x0p, final double y0p,
 415                                          final double x1p, final double y1p,
 416                                          final double[] m, int off)
 417     {
 418         double x10 = x1 - x0;
 419         double y10 = y1 - y0;
 420         double x10p = x1p - x0p;
 421         double y10p = y1p - y0p;
 422 
 423         // if this is 0, the lines are parallel. If they go in the
 424         // same direction, there is no intersection so m[off] and
 425         // m[off+1] will contain infinity, so no miter will be drawn.
 426         // If they go in the same direction that means that the start of the
 427         // current segment and the end of the previous segment have the same
 428         // tangent, in which case this method won't even be involved in
 429         // miter drawing because it won't be called by drawMiter (because
 430         // (mx == omx && my == omy) will be true, and drawMiter will return
 431         // immediately).
 432         double den = x10*y10p - x10p*y10;
 433         if (den == 0.0d) {
 434             m[off++] = (x0 + x0p) / 2.0d;
 435             m[off]   = (y0 + y0p) / 2.0d;
 436             return;
 437         }
 438         double t = x10p*(y0-y0p) - y10p*(x0-x0p);
 439         t /= den;
 440         m[off++] = x0 + t*x10;
 441         m[off] = y0 + t*y10;
 442     }
 443 
 444     private void drawMiter(final double pdx, final double pdy,
 445                            final double x0, final double y0,
 446                            final double dx, final double dy,
 447                            double omx, double omy, double mx, double my,
 448                            boolean rev)
 449     {
 450         if ((mx == omx && my == omy) ||
 451             (pdx == 0.0d && pdy == 0.0d) ||
 452             (dx == 0.0d && dy == 0.0d))
 453         {
 454             return;
 455         }
 456 
 457         if (rev) {
 458             omx = -omx;
 459             omy = -omy;
 460             mx  = -mx;
 461             my  = -my;
 462         }
 463 
 464         computeMiter((x0 - pdx) + omx, (y0 - pdy) + omy, x0 + omx, y0 + omy,
 465                      (dx + x0) + mx, (dy + y0) + my, x0 + mx, y0 + my,
 466                      miter, 0);
 467 
 468         final double miterX = miter[0];
 469         final double miterY = miter[1];
 470         double lenSq = (miterX-x0)*(miterX-x0) + (miterY-y0)*(miterY-y0);
 471 
 472         // If the lines are parallel, lenSq will be either NaN or +inf
 473         // (actually, I'm not sure if the latter is possible. The important
 474         // thing is that -inf is not possible, because lenSq is a square).
 475         // For both of those values, the comparison below will fail and
 476         // no miter will be drawn, which is correct.
 477         if (lenSq < miterLimitSq) {
 478             emitLineTo(miterX, miterY, rev);
 479         }
 480     }
 481 
 482     @Override
 483     public void moveTo(final double x0, final double y0) {
 484         moveTo(x0, y0, cOutCode);
 485         // update starting point:
 486         this.sx0 = x0;
 487         this.sy0 = y0;
 488         this.sdx = 1.0d;
 489         this.sdy = 0.0d;
 490         this.opened   = false;
 491         this.capStart = false;
 492 
 493         if (clipRect != null) {
 494             final int outcode = DHelpers.outcode(x0, y0, clipRect);
 495             this.cOutCode = outcode;
 496             this.sOutCode = outcode;
 497         }
 498     }
 499 
 500     private void moveTo(final double x0, final double y0,
 501                         final int outcode)
 502     {
 503         if (prev == MOVE_TO) {
 504             this.cx0 = x0;
 505             this.cy0 = y0;
 506         } else {
 507             if (prev == DRAWING_OP_TO) {
 508                 finish(outcode);
 509             }
 510             this.prev = MOVE_TO;
 511             this.cx0 = x0;
 512             this.cy0 = y0;
 513             this.cdx = 1.0d;
 514             this.cdy = 0.0d;
 515         }
 516     }
 517 
 518     @Override
 519     public void lineTo(final double x1, final double y1) {
 520         lineTo(x1, y1, false);
 521     }
 522 
 523     private void lineTo(final double x1, final double y1,
 524                         final boolean force)
 525     {
 526         final int outcode0 = this.cOutCode;
 527         if (!force && clipRect != null) {
 528             final int outcode1 = DHelpers.outcode(x1, y1, clipRect);
 529             this.cOutCode = outcode1;
 530 
 531             // basic rejection criteria
 532             if ((outcode0 & outcode1) != 0) {
 533                 moveTo(x1, y1, outcode0);
 534                 opened = true;
 535                 return;
 536             }
 537         }
 538 
 539         double dx = x1 - cx0;
 540         double dy = y1 - cy0;
 541         if (dx == 0.0d && dy == 0.0d) {
 542             dx = 1.0d;
 543         }
 544         computeOffset(dx, dy, lineWidth2, offset0);
 545         final double mx = offset0[0];
 546         final double my = offset0[1];
 547 
 548         drawJoin(cdx, cdy, cx0, cy0, dx, dy, cmx, cmy, mx, my, outcode0);
 549 
 550         emitLineTo(cx0 + mx, cy0 + my);
 551         emitLineTo( x1 + mx,  y1 + my);
 552 
 553         emitLineToRev(cx0 - mx, cy0 - my);
 554         emitLineToRev( x1 - mx,  y1 - my);
 555 
 556         this.prev = DRAWING_OP_TO;
 557         this.cx0 = x1;
 558         this.cy0 = y1;
 559         this.cdx = dx;
 560         this.cdy = dy;
 561         this.cmx = mx;
 562         this.cmy = my;
 563     }
 564 
 565     @Override
 566     public void closePath() {
 567         // distinguish empty path at all vs opened path ?
 568         if (prev != DRAWING_OP_TO && !opened) {
 569             if (prev == CLOSE) {
 570                 return;
 571             }
 572             emitMoveTo(cx0, cy0 - lineWidth2);
 573 
 574             this.sdx = 1.0d;
 575             this.sdy = 0.0d;
 576             this.cdx = 1.0d;
 577             this.cdy = 0.0d;
 578 
 579             this.smx = 0.0d;
 580             this.smy = -lineWidth2;
 581             this.cmx = 0.0d;
 582             this.cmy = -lineWidth2;
 583 
 584             finish(cOutCode);
 585             return;
 586         }
 587 
 588         // basic acceptance criteria
 589         if ((sOutCode & cOutCode) == 0) {
 590             if (cx0 != sx0 || cy0 != sy0) {
 591                 lineTo(sx0, sy0, true);
 592             }
 593 
 594             drawJoin(cdx, cdy, cx0, cy0, sdx, sdy, cmx, cmy, smx, smy, sOutCode);
 595 
 596             emitLineTo(sx0 + smx, sy0 + smy);
 597 
 598             if (opened) {
 599                 emitLineTo(sx0 - smx, sy0 - smy);
 600             } else {
 601                 emitMoveTo(sx0 - smx, sy0 - smy);
 602             }
 603         }
 604         // Ignore caps like finish(false)
 605         emitReverse();
 606 
 607         this.prev = CLOSE;
 608 
 609         if (opened) {
 610             // do not emit close
 611             opened = false;
 612         } else {
 613             emitClose();
 614         }
 615     }
 616 
 617     private void emitReverse() {
 618         reverse.popAll(out);
 619     }
 620 
 621     @Override
 622     public void pathDone() {
 623         if (prev == DRAWING_OP_TO) {
 624             finish(cOutCode);
 625         }
 626 
 627         out.pathDone();
 628 
 629         // this shouldn't matter since this object won't be used
 630         // after the call to this method.
 631         this.prev = CLOSE;
 632 
 633         // Dispose this instance:
 634         dispose();
 635     }
 636 
 637     private void finish(final int outcode) {
 638         // Problem: impossible to guess if the path will be closed in advance
 639         //          i.e. if caps must be drawn or not ?
 640         // Solution: use the ClosedPathDetector before Stroker to determine
 641         // if the path is a closed path or not
 642         if (!rdrCtx.closedPath) {
 643             if (outcode == 0) {
 644                 // current point = end's cap:
 645                 if (capStyle == CAP_ROUND) {
 646                     drawRoundCap(cx0, cy0, cmx, cmy);
 647                 } else if (capStyle == CAP_SQUARE) {
 648                     emitLineTo(cx0 - cmy + cmx, cy0 + cmx + cmy);
 649                     emitLineTo(cx0 - cmy - cmx, cy0 + cmx - cmy);
 650                 }
 651             }
 652             emitReverse();
 653 
 654             if (!capStart) {
 655                 capStart = true;
 656 
 657                 if (sOutCode == 0) {
 658                     // starting point = initial cap:
 659                     if (capStyle == CAP_ROUND) {
 660                         drawRoundCap(sx0, sy0, -smx, -smy);
 661                     } else if (capStyle == CAP_SQUARE) {
 662                         emitLineTo(sx0 + smy - smx, sy0 - smx - smy);
 663                         emitLineTo(sx0 + smy + smx, sy0 - smx + smy);
 664                     }
 665                 }
 666             }
 667         } else {
 668             emitReverse();
 669         }
 670         emitClose();
 671     }
 672 
 673     private void emitMoveTo(final double x0, final double y0) {
 674         out.moveTo(x0, y0);
 675     }
 676 
 677     private void emitLineTo(final double x1, final double y1) {
 678         out.lineTo(x1, y1);
 679     }
 680 
 681     private void emitLineToRev(final double x1, final double y1) {
 682         reverse.pushLine(x1, y1);
 683     }
 684 
 685     private void emitLineTo(final double x1, final double y1,
 686                             final boolean rev)
 687     {
 688         if (rev) {
 689             emitLineToRev(x1, y1);
 690         } else {
 691             emitLineTo(x1, y1);
 692         }
 693     }
 694 
 695     private void emitQuadTo(final double x1, final double y1,
 696                             final double x2, final double y2)
 697     {
 698         out.quadTo(x1, y1, x2, y2);
 699     }
 700 
 701     private void emitQuadToRev(final double x0, final double y0,
 702                                final double x1, final double y1)
 703     {
 704         reverse.pushQuad(x0, y0, x1, y1);
 705     }
 706 
 707     private void emitCurveTo(final double x1, final double y1,
 708                              final double x2, final double y2,
 709                              final double x3, final double y3)
 710     {
 711         out.curveTo(x1, y1, x2, y2, x3, y3);
 712     }
 713 
 714     private void emitCurveToRev(final double x0, final double y0,
 715                                 final double x1, final double y1,
 716                                 final double x2, final double y2)
 717     {
 718         reverse.pushCubic(x0, y0, x1, y1, x2, y2);
 719     }
 720 
 721     private void emitCurveTo(final double x0, final double y0,
 722                              final double x1, final double y1,
 723                              final double x2, final double y2,
 724                              final double x3, final double y3, final boolean rev)
 725     {
 726         if (rev) {
 727             reverse.pushCubic(x0, y0, x1, y1, x2, y2);
 728         } else {
 729             out.curveTo(x1, y1, x2, y2, x3, y3);
 730         }
 731     }
 732 
 733     private void emitClose() {
 734         out.closePath();
 735     }
 736 
 737     private void drawJoin(double pdx, double pdy,
 738                           double x0, double y0,
 739                           double dx, double dy,
 740                           double omx, double omy,
 741                           double mx, double my,
 742                           final int outcode)
 743     {
 744         if (prev != DRAWING_OP_TO) {
 745             emitMoveTo(x0 + mx, y0 + my);
 746             if (!opened) {
 747                 this.sdx = dx;
 748                 this.sdy = dy;
 749                 this.smx = mx;
 750                 this.smy = my;
 751             }
 752         } else {
 753             final boolean cw = isCW(pdx, pdy, dx, dy);
 754             if (outcode == 0) {
 755                 if (joinStyle == JOIN_MITER) {
 756                     drawMiter(pdx, pdy, x0, y0, dx, dy, omx, omy, mx, my, cw);
 757                 } else if (joinStyle == JOIN_ROUND) {
 758                     drawRoundJoin(x0, y0,
 759                                   omx, omy,
 760                                   mx, my, cw,
 761                                   ROUND_JOIN_THRESHOLD);
 762                 }
 763             }
 764             emitLineTo(x0, y0, !cw);
 765         }
 766         prev = DRAWING_OP_TO;
 767     }
 768 
 769     private static boolean within(final double x1, final double y1,
 770                                   final double x2, final double y2,
 771                                   final double ERR)
 772     {
 773         assert ERR > 0 : "";
 774         // compare taxicab distance. ERR will always be small, so using
 775         // true distance won't give much benefit
 776         return (DHelpers.within(x1, x2, ERR) &&  // we want to avoid calling Math.abs
 777                 DHelpers.within(y1, y2, ERR)); // this is just as good.
 778     }
 779 
 780     private void getLineOffsets(double x1, double y1,
 781                                 double x2, double y2,
 782                                 double[] left, double[] right) {
 783         computeOffset(x2 - x1, y2 - y1, lineWidth2, offset0);
 784         final double mx = offset0[0];
 785         final double my = offset0[1];
 786         left[0] = x1 + mx;
 787         left[1] = y1 + my;
 788         left[2] = x2 + mx;
 789         left[3] = y2 + my;
 790         right[0] = x1 - mx;
 791         right[1] = y1 - my;
 792         right[2] = x2 - mx;
 793         right[3] = y2 - my;
 794     }
 795 
 796     private int computeOffsetCubic(double[] pts, final int off,
 797                                    double[] leftOff, double[] rightOff)
 798     {
 799         // if p1=p2 or p3=p4 it means that the derivative at the endpoint
 800         // vanishes, which creates problems with computeOffset. Usually
 801         // this happens when this stroker object is trying to widen
 802         // a curve with a cusp. What happens is that curveTo splits
 803         // the input curve at the cusp, and passes it to this function.
 804         // because of inaccuracies in the splitting, we consider points
 805         // equal if they're very close to each other.
 806         final double x1 = pts[off + 0], y1 = pts[off + 1];
 807         final double x2 = pts[off + 2], y2 = pts[off + 3];
 808         final double x3 = pts[off + 4], y3 = pts[off + 5];
 809         final double x4 = pts[off + 6], y4 = pts[off + 7];
 810 
 811         double dx4 = x4 - x3;
 812         double dy4 = y4 - y3;
 813         double dx1 = x2 - x1;
 814         double dy1 = y2 - y1;
 815 
 816         // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
 817         // in which case ignore if p1 == p2
 818         final boolean p1eqp2 = within(x1, y1, x2, y2, 6.0d * Math.ulp(y2));
 819         final boolean p3eqp4 = within(x3, y3, x4, y4, 6.0d * Math.ulp(y4));
 820         if (p1eqp2 && p3eqp4) {
 821             getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
 822             return 4;
 823         } else if (p1eqp2) {
 824             dx1 = x3 - x1;
 825             dy1 = y3 - y1;
 826         } else if (p3eqp4) {
 827             dx4 = x4 - x2;
 828             dy4 = y4 - y2;
 829         }
 830 
 831         // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
 832         double dotsq = (dx1 * dx4 + dy1 * dy4);
 833         dotsq *= dotsq;
 834         double l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4;
 835         if (DHelpers.within(dotsq, l1sq * l4sq, 4.0d * Math.ulp(dotsq))) {
 836             getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
 837             return 4;
 838         }
 839 
 840 //      What we're trying to do in this function is to approximate an ideal
 841 //      offset curve (call it I) of the input curve B using a bezier curve Bp.
 842 //      The constraints I use to get the equations are:
 843 //
 844 //      1. The computed curve Bp should go through I(0) and I(1). These are
 845 //      x1p, y1p, x4p, y4p, which are p1p and p4p. We still need to find
 846 //      4 variables: the x and y components of p2p and p3p (i.e. x2p, y2p, x3p, y3p).
 847 //
 848 //      2. Bp should have slope equal in absolute value to I at the endpoints. So,
 849 //      (by the way, the operator || in the comments below means "aligned with".
 850 //      It is defined on vectors, so when we say I'(0) || Bp'(0) we mean that
 851 //      vectors I'(0) and Bp'(0) are aligned, which is the same as saying
 852 //      that the tangent lines of I and Bp at 0 are parallel. Mathematically
 853 //      this means (I'(t) || Bp'(t)) <==> (I'(t) = c * Bp'(t)) where c is some
 854 //      nonzero constant.)
 855 //      I'(0) || Bp'(0) and I'(1) || Bp'(1). Obviously, I'(0) || B'(0) and
 856 //      I'(1) || B'(1); therefore, Bp'(0) || B'(0) and Bp'(1) || B'(1).
 857 //      We know that Bp'(0) || (p2p-p1p) and Bp'(1) || (p4p-p3p) and the same
 858 //      is true for any bezier curve; therefore, we get the equations
 859 //          (1) p2p = c1 * (p2-p1) + p1p
 860 //          (2) p3p = c2 * (p4-p3) + p4p
 861 //      We know p1p, p4p, p2, p1, p3, and p4; therefore, this reduces the number
 862 //      of unknowns from 4 to 2 (i.e. just c1 and c2).
 863 //      To eliminate these 2 unknowns we use the following constraint:
 864 //
 865 //      3. Bp(0.5) == I(0.5). Bp(0.5)=(x,y) and I(0.5)=(xi,yi), and I should note
 866 //      that I(0.5) is *the only* reason for computing dxm,dym. This gives us
 867 //          (3) Bp(0.5) = (p1p + 3 * (p2p + p3p) + p4p)/8, which is equivalent to
 868 //          (4) p2p + p3p = (Bp(0.5)*8 - p1p - p4p) / 3
 869 //      We can substitute (1) and (2) from above into (4) and we get:
 870 //          (5) c1*(p2-p1) + c2*(p4-p3) = (Bp(0.5)*8 - p1p - p4p)/3 - p1p - p4p
 871 //      which is equivalent to
 872 //          (6) c1*(p2-p1) + c2*(p4-p3) = (4/3) * (Bp(0.5) * 2 - p1p - p4p)
 873 //
 874 //      The right side of this is a 2D vector, and we know I(0.5), which gives us
 875 //      Bp(0.5), which gives us the value of the right side.
 876 //      The left side is just a matrix vector multiplication in disguise. It is
 877 //
 878 //      [x2-x1, x4-x3][c1]
 879 //      [y2-y1, y4-y3][c2]
 880 //      which, is equal to
 881 //      [dx1, dx4][c1]
 882 //      [dy1, dy4][c2]
 883 //      At this point we are left with a simple linear system and we solve it by
 884 //      getting the inverse of the matrix above. Then we use [c1,c2] to compute
 885 //      p2p and p3p.
 886 
 887         double x = (x1 + 3.0d * (x2 + x3) + x4) / 8.0d;
 888         double y = (y1 + 3.0d * (y2 + y3) + y4) / 8.0d;
 889         // (dxm,dym) is some tangent of B at t=0.5. This means it's equal to
 890         // c*B'(0.5) for some constant c.
 891         double dxm = x3 + x4 - x1 - x2, dym = y3 + y4 - y1 - y2;
 892 
 893         // this computes the offsets at t=0, 0.5, 1, using the property that
 894         // for any bezier curve the vectors p2-p1 and p4-p3 are parallel to
 895         // the (dx/dt, dy/dt) vectors at the endpoints.
 896         computeOffset(dx1, dy1, lineWidth2, offset0);
 897         computeOffset(dxm, dym, lineWidth2, offset1);
 898         computeOffset(dx4, dy4, lineWidth2, offset2);
 899         double x1p = x1 + offset0[0]; // start
 900         double y1p = y1 + offset0[1]; // point
 901         double xi  = x  + offset1[0]; // interpolation
 902         double yi  = y  + offset1[1]; // point
 903         double x4p = x4 + offset2[0]; // end
 904         double y4p = y4 + offset2[1]; // point
 905 
 906         double invdet43 = 4.0d / (3.0d * (dx1 * dy4 - dy1 * dx4));
 907 
 908         double two_pi_m_p1_m_p4x = 2.0d * xi - x1p - x4p;
 909         double two_pi_m_p1_m_p4y = 2.0d * yi - y1p - y4p;
 910         double c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);
 911         double c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);
 912 
 913         double x2p, y2p, x3p, y3p;
 914         x2p = x1p + c1*dx1;
 915         y2p = y1p + c1*dy1;
 916         x3p = x4p + c2*dx4;
 917         y3p = y4p + c2*dy4;
 918 
 919         leftOff[0] = x1p; leftOff[1] = y1p;
 920         leftOff[2] = x2p; leftOff[3] = y2p;
 921         leftOff[4] = x3p; leftOff[5] = y3p;
 922         leftOff[6] = x4p; leftOff[7] = y4p;
 923 
 924         x1p = x1 - offset0[0]; y1p = y1 - offset0[1];
 925         xi = xi - 2.0d * offset1[0]; yi = yi - 2.0d * offset1[1];
 926         x4p = x4 - offset2[0]; y4p = y4 - offset2[1];
 927 
 928         two_pi_m_p1_m_p4x = 2.0d * xi - x1p - x4p;
 929         two_pi_m_p1_m_p4y = 2.0d * yi - y1p - y4p;
 930         c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);
 931         c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);
 932 
 933         x2p = x1p + c1*dx1;
 934         y2p = y1p + c1*dy1;
 935         x3p = x4p + c2*dx4;
 936         y3p = y4p + c2*dy4;
 937 
 938         rightOff[0] = x1p; rightOff[1] = y1p;
 939         rightOff[2] = x2p; rightOff[3] = y2p;
 940         rightOff[4] = x3p; rightOff[5] = y3p;
 941         rightOff[6] = x4p; rightOff[7] = y4p;
 942         return 8;
 943     }
 944 
 945     // compute offset curves using bezier spline through t=0.5 (i.e.
 946     // ComputedCurve(0.5) == IdealParallelCurve(0.5))
 947     // return the kind of curve in the right and left arrays.
 948     private int computeOffsetQuad(double[] pts, final int off,
 949                                   double[] leftOff, double[] rightOff)
 950     {
 951         final double x1 = pts[off + 0], y1 = pts[off + 1];
 952         final double x2 = pts[off + 2], y2 = pts[off + 3];
 953         final double x3 = pts[off + 4], y3 = pts[off + 5];
 954 
 955         final double dx3 = x3 - x2;
 956         final double dy3 = y3 - y2;
 957         final double dx1 = x2 - x1;
 958         final double dy1 = y2 - y1;
 959 
 960         // if p1=p2 or p3=p4 it means that the derivative at the endpoint
 961         // vanishes, which creates problems with computeOffset. Usually
 962         // this happens when this stroker object is trying to widen
 963         // a curve with a cusp. What happens is that curveTo splits
 964         // the input curve at the cusp, and passes it to this function.
 965         // because of inaccuracies in the splitting, we consider points
 966         // equal if they're very close to each other.
 967 
 968         // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
 969         // in which case ignore.
 970         final boolean p1eqp2 = within(x1, y1, x2, y2, 6.0d * Math.ulp(y2));
 971         final boolean p2eqp3 = within(x2, y2, x3, y3, 6.0d * Math.ulp(y3));
 972         if (p1eqp2 || p2eqp3) {
 973             getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
 974             return 4;
 975         }
 976 
 977         // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
 978         double dotsq = (dx1 * dx3 + dy1 * dy3);
 979         dotsq *= dotsq;
 980         double l1sq = dx1 * dx1 + dy1 * dy1, l3sq = dx3 * dx3 + dy3 * dy3;
 981         if (DHelpers.within(dotsq, l1sq * l3sq, 4.0d * Math.ulp(dotsq))) {
 982             getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
 983             return 4;
 984         }
 985 
 986         // this computes the offsets at t=0, 0.5, 1, using the property that
 987         // for any bezier curve the vectors p2-p1 and p4-p3 are parallel to
 988         // the (dx/dt, dy/dt) vectors at the endpoints.
 989         computeOffset(dx1, dy1, lineWidth2, offset0);
 990         computeOffset(dx3, dy3, lineWidth2, offset1);
 991 
 992         double x1p = x1 + offset0[0]; // start
 993         double y1p = y1 + offset0[1]; // point
 994         double x3p = x3 + offset1[0]; // end
 995         double y3p = y3 + offset1[1]; // point
 996         safeComputeMiter(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2);
 997         leftOff[0] = x1p; leftOff[1] = y1p;
 998         leftOff[4] = x3p; leftOff[5] = y3p;
 999 
1000         x1p = x1 - offset0[0]; y1p = y1 - offset0[1];
1001         x3p = x3 - offset1[0]; y3p = y3 - offset1[1];
1002         safeComputeMiter(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2);
1003         rightOff[0] = x1p; rightOff[1] = y1p;
1004         rightOff[4] = x3p; rightOff[5] = y3p;
1005         return 6;
1006     }
1007 
1008     // finds values of t where the curve in pts should be subdivided in order
1009     // to get good offset curves a distance of w away from the middle curve.
1010     // Stores the points in ts, and returns how many of them there were.
1011     private static int findSubdivPoints(final DCurve c, double[] pts, double[] ts,
1012                                         final int type, final double w)
1013     {
1014         final double x12 = pts[2] - pts[0];
1015         final double y12 = pts[3] - pts[1];
1016         // if the curve is already parallel to either axis we gain nothing
1017         // from rotating it.
1018         if (y12 != 0.0d && x12 != 0.0d) {
1019             // we rotate it so that the first vector in the control polygon is
1020             // parallel to the x-axis. This will ensure that rotated quarter
1021             // circles won't be subdivided.
1022             final double hypot = Math.sqrt(x12 * x12 + y12 * y12);
1023             final double cos = x12 / hypot;
1024             final double sin = y12 / hypot;
1025             final double x1 = cos * pts[0] + sin * pts[1];
1026             final double y1 = cos * pts[1] - sin * pts[0];
1027             final double x2 = cos * pts[2] + sin * pts[3];
1028             final double y2 = cos * pts[3] - sin * pts[2];
1029             final double x3 = cos * pts[4] + sin * pts[5];
1030             final double y3 = cos * pts[5] - sin * pts[4];
1031 
1032             switch(type) {
1033             case 8:
1034                 final double x4 = cos * pts[6] + sin * pts[7];
1035                 final double y4 = cos * pts[7] - sin * pts[6];
1036                 c.set(x1, y1, x2, y2, x3, y3, x4, y4);
1037                 break;
1038             case 6:
1039                 c.set(x1, y1, x2, y2, x3, y3);
1040                 break;
1041             default:
1042             }
1043         } else {
1044             c.set(pts, type);
1045         }
1046 
1047         int ret = 0;
1048         // we subdivide at values of t such that the remaining rotated
1049         // curves are monotonic in x and y.
1050         ret += c.dxRoots(ts, ret);
1051         ret += c.dyRoots(ts, ret);
1052         // subdivide at inflection points.
1053         if (type == 8) {
1054             // quadratic curves can't have inflection points
1055             ret += c.infPoints(ts, ret);
1056         }
1057 
1058         // now we must subdivide at points where one of the offset curves will have
1059         // a cusp. This happens at ts where the radius of curvature is equal to w.
1060         ret += c.rootsOfROCMinusW(ts, ret, w, 0.0001d);
1061 
1062         ret = DHelpers.filterOutNotInAB(ts, 0, ret, 0.0001d, 0.9999d);
1063         DHelpers.isort(ts, 0, ret);
1064         return ret;
1065     }
1066 
1067     @Override
1068     public void curveTo(final double x1, final double y1,
1069                         final double x2, final double y2,
1070                         final double x3, final double y3)
1071     {
1072         final int outcode0 = this.cOutCode;
1073         if (clipRect != null) {
1074             final int outcode3 = DHelpers.outcode(x3, y3, clipRect);
1075             this.cOutCode = outcode3;
1076 
1077             if ((outcode0 & outcode3) != 0) {
1078                 final int outcode1 = DHelpers.outcode(x1, y1, clipRect);
1079                 final int outcode2 = DHelpers.outcode(x2, y2, clipRect);
1080 
1081                 // basic rejection criteria
1082                 if ((outcode0 & outcode1 & outcode2 & outcode3) != 0) {
1083                     moveTo(x3, y3, outcode0);
1084                     opened = true;
1085                     return;
1086                 }
1087             }
1088         }
1089 
1090         final double[] mid = middle;
1091 
1092         mid[0] = cx0; mid[1] = cy0;
1093         mid[2] = x1;  mid[3] = y1;
1094         mid[4] = x2;  mid[5] = y2;
1095         mid[6] = x3;  mid[7] = y3;
1096 
1097         // need these so we can update the state at the end of this method
1098         final double xf = x3, yf = y3;
1099         double dxs = mid[2] - mid[0];
1100         double dys = mid[3] - mid[1];
1101         double dxf = mid[6] - mid[4];
1102         double dyf = mid[7] - mid[5];
1103 
1104         boolean p1eqp2 = (dxs == 0.0d && dys == 0.0d);
1105         boolean p3eqp4 = (dxf == 0.0d && dyf == 0.0d);
1106         if (p1eqp2) {
1107             dxs = mid[4] - mid[0];
1108             dys = mid[5] - mid[1];
1109             if (dxs == 0.0d && dys == 0.0d) {
1110                 dxs = mid[6] - mid[0];
1111                 dys = mid[7] - mid[1];
1112             }
1113         }
1114         if (p3eqp4) {
1115             dxf = mid[6] - mid[2];
1116             dyf = mid[7] - mid[3];
1117             if (dxf == 0.0d && dyf == 0.0d) {
1118                 dxf = mid[6] - mid[0];
1119                 dyf = mid[7] - mid[1];
1120             }
1121         }
1122         if (dxs == 0.0d && dys == 0.0d) {
1123             // this happens if the "curve" is just a point
1124             // fix outcode0 for lineTo() call:
1125             if (clipRect != null) {
1126                 this.cOutCode = outcode0;
1127             }
1128             lineTo(mid[0], mid[1]);
1129             return;
1130         }
1131 
1132         // if these vectors are too small, normalize them, to avoid future
1133         // precision problems.
1134         if (Math.abs(dxs) < 0.1d && Math.abs(dys) < 0.1d) {
1135             double len = Math.sqrt(dxs*dxs + dys*dys);
1136             dxs /= len;
1137             dys /= len;
1138         }
1139         if (Math.abs(dxf) < 0.1d && Math.abs(dyf) < 0.1d) {
1140             double len = Math.sqrt(dxf*dxf + dyf*dyf);
1141             dxf /= len;
1142             dyf /= len;
1143         }
1144 
1145         computeOffset(dxs, dys, lineWidth2, offset0);
1146         drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, offset0[0], offset0[1], outcode0);
1147 
1148         final int nSplits = findSubdivPoints(curve, mid, subdivTs, 8, lineWidth2);
1149 
1150         double prevT = 0.0d;
1151         for (int i = 0, off = 0; i < nSplits; i++, off += 6) {
1152             final double t = subdivTs[i];
1153             DHelpers.subdivideCubicAt((t - prevT) / (1.0d - prevT),
1154                                      mid, off, mid, off, mid, off + 6);
1155             prevT = t;
1156         }
1157 
1158         final double[] l = lp;
1159         final double[] r = rp;
1160 
1161         int kind = 0;
1162         for (int i = 0, off = 0; i <= nSplits; i++, off += 6) {
1163             kind = computeOffsetCubic(mid, off, l, r);
1164 
1165             emitLineTo(l[0], l[1]);
1166 
1167             switch(kind) {
1168             case 8:
1169                 emitCurveTo(l[2], l[3], l[4], l[5], l[6], l[7]);
1170                 emitCurveToRev(r[0], r[1], r[2], r[3], r[4], r[5]);
1171                 break;
1172             case 4:
1173                 emitLineTo(l[2], l[3]);
1174                 emitLineToRev(r[0], r[1]);
1175                 break;
1176             default:
1177             }
1178             emitLineToRev(r[kind - 2], r[kind - 1]);
1179         }
1180 
1181         this.prev = DRAWING_OP_TO;
1182         this.cx0 = xf;
1183         this.cy0 = yf;
1184         this.cdx = dxf;
1185         this.cdy = dyf;
1186         this.cmx = (l[kind - 2] - r[kind - 2]) / 2.0d;
1187         this.cmy = (l[kind - 1] - r[kind - 1]) / 2.0d;
1188     }
1189 
1190     @Override
1191     public void quadTo(final double x1, final double y1,
1192                        final double x2, final double y2)
1193     {
1194         final int outcode0 = this.cOutCode;
1195         if (clipRect != null) {
1196             final int outcode2 = DHelpers.outcode(x2, y2, clipRect);
1197             this.cOutCode = outcode2;
1198 
1199             if ((outcode0 & outcode2) != 0) {
1200                 final int outcode1 = DHelpers.outcode(x1, y1, clipRect);
1201 
1202                 // basic rejection criteria
1203                 if ((outcode0 & outcode1 & outcode2) != 0) {
1204                     moveTo(x2, y2, outcode0);
1205                     opened = true;
1206                     return;
1207                 }
1208             }
1209         }
1210 
1211         final double[] mid = middle;
1212 
1213         mid[0] = cx0; mid[1] = cy0;
1214         mid[2] = x1;  mid[3] = y1;
1215         mid[4] = x2;  mid[5] = y2;
1216 
1217         // need these so we can update the state at the end of this method
1218         final double xf = x2, yf = y2;
1219         double dxs = mid[2] - mid[0];
1220         double dys = mid[3] - mid[1];
1221         double dxf = mid[4] - mid[2];
1222         double dyf = mid[5] - mid[3];
1223         if ((dxs == 0.0d && dys == 0.0d) || (dxf == 0.0d && dyf == 0.0d)) {
1224             dxs = dxf = mid[4] - mid[0];
1225             dys = dyf = mid[5] - mid[1];
1226         }
1227         if (dxs == 0.0d && dys == 0.0d) {
1228             // this happens if the "curve" is just a point
1229             // fix outcode0 for lineTo() call:
1230             if (clipRect != null) {
1231                 this.cOutCode = outcode0;
1232             }
1233             lineTo(mid[0], mid[1]);
1234             return;
1235         }
1236         // if these vectors are too small, normalize them, to avoid future
1237         // precision problems.
1238         if (Math.abs(dxs) < 0.1d && Math.abs(dys) < 0.1d) {
1239             double len = Math.sqrt(dxs*dxs + dys*dys);
1240             dxs /= len;
1241             dys /= len;
1242         }
1243         if (Math.abs(dxf) < 0.1d && Math.abs(dyf) < 0.1d) {
1244             double len = Math.sqrt(dxf*dxf + dyf*dyf);
1245             dxf /= len;
1246             dyf /= len;
1247         }
1248 
1249         computeOffset(dxs, dys, lineWidth2, offset0);
1250         drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, offset0[0], offset0[1], outcode0);
1251 
1252         int nSplits = findSubdivPoints(curve, mid, subdivTs, 6, lineWidth2);
1253 
1254         double prevt = 0.0d;
1255         for (int i = 0, off = 0; i < nSplits; i++, off += 4) {
1256             final double t = subdivTs[i];
1257             DHelpers.subdivideQuadAt((t - prevt) / (1.0d - prevt),
1258                                     mid, off, mid, off, mid, off + 4);
1259             prevt = t;
1260         }
1261 
1262         final double[] l = lp;
1263         final double[] r = rp;
1264 
1265         int kind = 0;
1266         for (int i = 0, off = 0; i <= nSplits; i++, off += 4) {
1267             kind = computeOffsetQuad(mid, off, l, r);
1268 
1269             emitLineTo(l[0], l[1]);
1270 
1271             switch(kind) {
1272             case 6:
1273                 emitQuadTo(l[2], l[3], l[4], l[5]);
1274                 emitQuadToRev(r[0], r[1], r[2], r[3]);
1275                 break;
1276             case 4:
1277                 emitLineTo(l[2], l[3]);
1278                 emitLineToRev(r[0], r[1]);
1279                 break;
1280             default:
1281             }
1282             emitLineToRev(r[kind - 2], r[kind - 1]);
1283         }
1284 
1285         this.prev = DRAWING_OP_TO;
1286         this.cx0 = xf;
1287         this.cy0 = yf;
1288         this.cdx = dxf;
1289         this.cdy = dyf;
1290         this.cmx = (l[kind - 2] - r[kind - 2]) / 2.0d;
1291         this.cmy = (l[kind - 1] - r[kind - 1]) / 2.0d;
1292     }
1293 
1294 }