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 sun.java2d.marlin;
  27 
  28 import static java.lang.Math.PI;
  29 import static java.lang.Math.cos;
  30 import static java.lang.Math.sqrt;
  31 import static java.lang.Math.cbrt;
  32 import static java.lang.Math.acos;
  33 import java.util.Arrays;
  34 import static sun.java2d.marlin.MarlinConst.INITIAL_EDGES_COUNT;
  35 import sun.java2d.marlin.stats.Histogram;
  36 import sun.java2d.marlin.stats.StatLong;
  37 
  38 final class DHelpers implements MarlinConst {
  39 
  40     private DHelpers() {
  41         throw new Error("This is a non instantiable class");
  42     }
  43 
  44     static boolean within(final double x, final double y, final double err) {
  45         final double d = y - x;
  46         return (d <= err && d >= -err);
  47     }
  48 
  49     static int quadraticRoots(final double a, final double b,
  50                               final double c, double[] zeroes, final int off)
  51     {
  52         int ret = off;
  53         double t;
  54         if (a != 0.0d) {
  55             final double dis = b*b - 4*a*c;
  56             if (dis > 0.0d) {
  57                 final double sqrtDis = Math.sqrt(dis);
  58                 // depending on the sign of b we use a slightly different
  59                 // algorithm than the traditional one to find one of the roots
  60                 // so we can avoid adding numbers of different signs (which
  61                 // might result in loss of precision).
  62                 if (b >= 0.0d) {
  63                     zeroes[ret++] = (2.0d * c) / (-b - sqrtDis);
  64                     zeroes[ret++] = (-b - sqrtDis) / (2.0d * a);
  65                 } else {
  66                     zeroes[ret++] = (-b + sqrtDis) / (2.0d * a);
  67                     zeroes[ret++] = (2.0d * c) / (-b + sqrtDis);
  68                 }
  69             } else if (dis == 0.0d) {
  70                 t = (-b) / (2.0d * a);
  71                 zeroes[ret++] = t;
  72             }
  73         } else {
  74             if (b != 0.0d) {
  75                 t = (-c) / b;
  76                 zeroes[ret++] = t;
  77             }
  78         }
  79         return ret - off;
  80     }
  81 
  82     // find the roots of g(t) = d*t^3 + a*t^2 + b*t + c in [A,B)
  83     static int cubicRootsInAB(double d, double a, double b, double c,
  84                               double[] pts, final int off,
  85                               final double A, final double B)
  86     {
  87         if (d == 0.0d) {
  88             int num = quadraticRoots(a, b, c, pts, off);
  89             return filterOutNotInAB(pts, off, num, A, B) - off;
  90         }
  91         // From Graphics Gems:
  92         // http://tog.acm.org/resources/GraphicsGems/gems/Roots3And4.c
  93         // (also from awt.geom.CubicCurve2D. But here we don't need as
  94         // much accuracy and we don't want to create arrays so we use
  95         // our own customized version).
  96 
  97         // normal form: x^3 + ax^2 + bx + c = 0
  98         a /= d;
  99         b /= d;
 100         c /= d;
 101 
 102         //  substitute x = y - A/3 to eliminate quadratic term:
 103         //     x^3 +Px + Q = 0
 104         //
 105         // Since we actually need P/3 and Q/2 for all of the
 106         // calculations that follow, we will calculate
 107         // p = P/3
 108         // q = Q/2
 109         // instead and use those values for simplicity of the code.
 110         double sq_A = a * a;
 111         double p = (1.0d/3.0d) * ((-1.0d/3.0d) * sq_A + b);
 112         double q = (1.0d/2.0d) * ((2.0d/27.0d) * a * sq_A - (1.0d/3.0d) * a * b + c);
 113 
 114         // use Cardano's formula
 115 
 116         double cb_p = p * p * p;
 117         double D = q * q + cb_p;
 118 
 119         int num;
 120         if (D < 0.0d) {
 121             // see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method
 122             final double phi = (1.0d/3.0d) * acos(-q / sqrt(-cb_p));
 123             final double t = 2.0d * sqrt(-p);
 124 
 125             pts[ off+0 ] = ( t * cos(phi));
 126             pts[ off+1 ] = (-t * cos(phi + (PI / 3.0d)));
 127             pts[ off+2 ] = (-t * cos(phi - (PI / 3.0d)));
 128             num = 3;
 129         } else {
 130             final double sqrt_D = sqrt(D);
 131             final double u = cbrt(sqrt_D - q);
 132             final double v = - cbrt(sqrt_D + q);
 133 
 134             pts[ off ] = (u + v);
 135             num = 1;
 136 
 137             if (within(D, 0.0d, 1e-8d)) {
 138                 pts[off+1] = -(pts[off] / 2.0d);
 139                 num = 2;
 140             }
 141         }
 142 
 143         final double sub = (1.0d/3.0d) * a;
 144 
 145         for (int i = 0; i < num; ++i) {
 146             pts[ off+i ] -= sub;
 147         }
 148 
 149         return filterOutNotInAB(pts, off, num, A, B) - off;
 150     }
 151 
 152     static double evalCubic(final double a, final double b,
 153                            final double c, final double d,
 154                            final double t)
 155     {
 156         return t * (t * (t * a + b) + c) + d;
 157     }
 158 
 159     static double evalQuad(final double a, final double b,
 160                           final double c, final double t)
 161     {
 162         return t * (t * a + b) + c;
 163     }
 164 
 165     // returns the index 1 past the last valid element remaining after filtering
 166     static int filterOutNotInAB(double[] nums, final int off, final int len,
 167                                 final double a, final double b)
 168     {
 169         int ret = off;
 170         for (int i = off, end = off + len; i < end; i++) {
 171             if (nums[i] >= a && nums[i] < b) {
 172                 nums[ret++] = nums[i];
 173             }
 174         }
 175         return ret;
 176     }
 177 
 178     static double linelen(double x1, double y1, double x2, double y2) {
 179         final double dx = x2 - x1;
 180         final double dy = y2 - y1;
 181         return Math.sqrt(dx*dx + dy*dy);
 182     }
 183 
 184     static void subdivide(double[] src, int srcoff, double[] left, int leftoff,
 185                           double[] right, int rightoff, int type)
 186     {
 187         switch(type) {
 188         case 6:
 189             DHelpers.subdivideQuad(src, srcoff, left, leftoff, right, rightoff);
 190             return;
 191         case 8:
 192             DHelpers.subdivideCubic(src, srcoff, left, leftoff, right, rightoff);
 193             return;
 194         default:
 195             throw new InternalError("Unsupported curve type");
 196         }
 197     }
 198 
 199     static void isort(double[] a, int off, int len) {
 200         for (int i = off + 1, end = off + len; i < end; i++) {
 201             double ai = a[i];
 202             int j = i - 1;
 203             for (; j >= off && a[j] > ai; j--) {
 204                 a[j+1] = a[j];
 205             }
 206             a[j+1] = ai;
 207         }
 208     }
 209 
 210     // Most of these are copied from classes in java.awt.geom because we need
 211     // both single and double precision variants of these functions, and Line2D,
 212     // CubicCurve2D, QuadCurve2D don't provide them.
 213     /**
 214      * Subdivides the cubic curve specified by the coordinates
 215      * stored in the <code>src</code> array at indices <code>srcoff</code>
 216      * through (<code>srcoff</code>&nbsp;+&nbsp;7) and stores the
 217      * resulting two subdivided curves into the two result arrays at the
 218      * corresponding indices.
 219      * Either or both of the <code>left</code> and <code>right</code>
 220      * arrays may be <code>null</code> or a reference to the same array
 221      * as the <code>src</code> array.
 222      * Note that the last point in the first subdivided curve is the
 223      * same as the first point in the second subdivided curve. Thus,
 224      * it is possible to pass the same array for <code>left</code>
 225      * and <code>right</code> and to use offsets, such as <code>rightoff</code>
 226      * equals (<code>leftoff</code> + 6), in order
 227      * to avoid allocating extra storage for this common point.
 228      * @param src the array holding the coordinates for the source curve
 229      * @param srcoff the offset into the array of the beginning of the
 230      * the 6 source coordinates
 231      * @param left the array for storing the coordinates for the first
 232      * half of the subdivided curve
 233      * @param leftoff the offset into the array of the beginning of the
 234      * the 6 left coordinates
 235      * @param right the array for storing the coordinates for the second
 236      * half of the subdivided curve
 237      * @param rightoff the offset into the array of the beginning of the
 238      * the 6 right coordinates
 239      * @since 1.7
 240      */
 241     static void subdivideCubic(double[] src, int srcoff,
 242                                double[] left, int leftoff,
 243                                double[] right, int rightoff)
 244     {
 245         double x1 = src[srcoff + 0];
 246         double y1 = src[srcoff + 1];
 247         double ctrlx1 = src[srcoff + 2];
 248         double ctrly1 = src[srcoff + 3];
 249         double ctrlx2 = src[srcoff + 4];
 250         double ctrly2 = src[srcoff + 5];
 251         double x2 = src[srcoff + 6];
 252         double y2 = src[srcoff + 7];
 253         if (left != null) {
 254             left[leftoff + 0] = x1;
 255             left[leftoff + 1] = y1;
 256         }
 257         if (right != null) {
 258             right[rightoff + 6] = x2;
 259             right[rightoff + 7] = y2;
 260         }
 261         x1 = (x1 + ctrlx1) / 2.0d;
 262         y1 = (y1 + ctrly1) / 2.0d;
 263         x2 = (x2 + ctrlx2) / 2.0d;
 264         y2 = (y2 + ctrly2) / 2.0d;
 265         double centerx = (ctrlx1 + ctrlx2) / 2.0d;
 266         double centery = (ctrly1 + ctrly2) / 2.0d;
 267         ctrlx1 = (x1 + centerx) / 2.0d;
 268         ctrly1 = (y1 + centery) / 2.0d;
 269         ctrlx2 = (x2 + centerx) / 2.0d;
 270         ctrly2 = (y2 + centery) / 2.0d;
 271         centerx = (ctrlx1 + ctrlx2) / 2.0d;
 272         centery = (ctrly1 + ctrly2) / 2.0d;
 273         if (left != null) {
 274             left[leftoff + 2] = x1;
 275             left[leftoff + 3] = y1;
 276             left[leftoff + 4] = ctrlx1;
 277             left[leftoff + 5] = ctrly1;
 278             left[leftoff + 6] = centerx;
 279             left[leftoff + 7] = centery;
 280         }
 281         if (right != null) {
 282             right[rightoff + 0] = centerx;
 283             right[rightoff + 1] = centery;
 284             right[rightoff + 2] = ctrlx2;
 285             right[rightoff + 3] = ctrly2;
 286             right[rightoff + 4] = x2;
 287             right[rightoff + 5] = y2;
 288         }
 289     }
 290 
 291 
 292     static void subdivideCubicAt(double t, double[] src, int srcoff,
 293                                  double[] left, int leftoff,
 294                                  double[] right, int rightoff)
 295     {
 296         double x1 = src[srcoff + 0];
 297         double y1 = src[srcoff + 1];
 298         double ctrlx1 = src[srcoff + 2];
 299         double ctrly1 = src[srcoff + 3];
 300         double ctrlx2 = src[srcoff + 4];
 301         double ctrly2 = src[srcoff + 5];
 302         double x2 = src[srcoff + 6];
 303         double y2 = src[srcoff + 7];
 304         if (left != null) {
 305             left[leftoff + 0] = x1;
 306             left[leftoff + 1] = y1;
 307         }
 308         if (right != null) {
 309             right[rightoff + 6] = x2;
 310             right[rightoff + 7] = y2;
 311         }
 312         x1 = x1 + t * (ctrlx1 - x1);
 313         y1 = y1 + t * (ctrly1 - y1);
 314         x2 = ctrlx2 + t * (x2 - ctrlx2);
 315         y2 = ctrly2 + t * (y2 - ctrly2);
 316         double centerx = ctrlx1 + t * (ctrlx2 - ctrlx1);
 317         double centery = ctrly1 + t * (ctrly2 - ctrly1);
 318         ctrlx1 = x1 + t * (centerx - x1);
 319         ctrly1 = y1 + t * (centery - y1);
 320         ctrlx2 = centerx + t * (x2 - centerx);
 321         ctrly2 = centery + t * (y2 - centery);
 322         centerx = ctrlx1 + t * (ctrlx2 - ctrlx1);
 323         centery = ctrly1 + t * (ctrly2 - ctrly1);
 324         if (left != null) {
 325             left[leftoff + 2] = x1;
 326             left[leftoff + 3] = y1;
 327             left[leftoff + 4] = ctrlx1;
 328             left[leftoff + 5] = ctrly1;
 329             left[leftoff + 6] = centerx;
 330             left[leftoff + 7] = centery;
 331         }
 332         if (right != null) {
 333             right[rightoff + 0] = centerx;
 334             right[rightoff + 1] = centery;
 335             right[rightoff + 2] = ctrlx2;
 336             right[rightoff + 3] = ctrly2;
 337             right[rightoff + 4] = x2;
 338             right[rightoff + 5] = y2;
 339         }
 340     }
 341 
 342     static void subdivideQuad(double[] src, int srcoff,
 343                               double[] left, int leftoff,
 344                               double[] right, int rightoff)
 345     {
 346         double x1 = src[srcoff + 0];
 347         double y1 = src[srcoff + 1];
 348         double ctrlx = src[srcoff + 2];
 349         double ctrly = src[srcoff + 3];
 350         double x2 = src[srcoff + 4];
 351         double y2 = src[srcoff + 5];
 352         if (left != null) {
 353             left[leftoff + 0] = x1;
 354             left[leftoff + 1] = y1;
 355         }
 356         if (right != null) {
 357             right[rightoff + 4] = x2;
 358             right[rightoff + 5] = y2;
 359         }
 360         x1 = (x1 + ctrlx) / 2.0d;
 361         y1 = (y1 + ctrly) / 2.0d;
 362         x2 = (x2 + ctrlx) / 2.0d;
 363         y2 = (y2 + ctrly) / 2.0d;
 364         ctrlx = (x1 + x2) / 2.0d;
 365         ctrly = (y1 + y2) / 2.0d;
 366         if (left != null) {
 367             left[leftoff + 2] = x1;
 368             left[leftoff + 3] = y1;
 369             left[leftoff + 4] = ctrlx;
 370             left[leftoff + 5] = ctrly;
 371         }
 372         if (right != null) {
 373             right[rightoff + 0] = ctrlx;
 374             right[rightoff + 1] = ctrly;
 375             right[rightoff + 2] = x2;
 376             right[rightoff + 3] = y2;
 377         }
 378     }
 379 
 380     static void subdivideQuadAt(double t, double[] src, int srcoff,
 381                                 double[] left, int leftoff,
 382                                 double[] right, int rightoff)
 383     {
 384         double x1 = src[srcoff + 0];
 385         double y1 = src[srcoff + 1];
 386         double ctrlx = src[srcoff + 2];
 387         double ctrly = src[srcoff + 3];
 388         double x2 = src[srcoff + 4];
 389         double y2 = src[srcoff + 5];
 390         if (left != null) {
 391             left[leftoff + 0] = x1;
 392             left[leftoff + 1] = y1;
 393         }
 394         if (right != null) {
 395             right[rightoff + 4] = x2;
 396             right[rightoff + 5] = y2;
 397         }
 398         x1 = x1 + t * (ctrlx - x1);
 399         y1 = y1 + t * (ctrly - y1);
 400         x2 = ctrlx + t * (x2 - ctrlx);
 401         y2 = ctrly + t * (y2 - ctrly);
 402         ctrlx = x1 + t * (x2 - x1);
 403         ctrly = y1 + t * (y2 - y1);
 404         if (left != null) {
 405             left[leftoff + 2] = x1;
 406             left[leftoff + 3] = y1;
 407             left[leftoff + 4] = ctrlx;
 408             left[leftoff + 5] = ctrly;
 409         }
 410         if (right != null) {
 411             right[rightoff + 0] = ctrlx;
 412             right[rightoff + 1] = ctrly;
 413             right[rightoff + 2] = x2;
 414             right[rightoff + 3] = y2;
 415         }
 416     }
 417 
 418     static void subdivideAt(double t, double[] src, int srcoff,
 419                             double[] left, int leftoff,
 420                             double[] right, int rightoff, int size)
 421     {
 422         switch(size) {
 423         case 8:
 424             subdivideCubicAt(t, src, srcoff, left, leftoff, right, rightoff);
 425             return;
 426         case 6:
 427             subdivideQuadAt(t, src, srcoff, left, leftoff, right, rightoff);
 428             return;
 429         }
 430     }
 431 
 432     // From sun.java2d.loops.GeneralRenderer:
 433 
 434     static final int OUTCODE_TOP     = 1;
 435     static final int OUTCODE_BOTTOM  = 2;
 436     static final int OUTCODE_LEFT    = 4;
 437     static final int OUTCODE_RIGHT   = 8;
 438 
 439     static int outcode(final double x, final double y,
 440                        final double[] clipRect)
 441     {
 442         int code;
 443         if (y < clipRect[0]) {
 444             code = OUTCODE_TOP;
 445         } else if (y >= clipRect[1]) {
 446             code = OUTCODE_BOTTOM;
 447         } else {
 448             code = 0;
 449         }
 450         if (x < clipRect[2]) {
 451             code |= OUTCODE_LEFT;
 452         } else if (x >= clipRect[3]) {
 453             code |= OUTCODE_RIGHT;
 454         }
 455         return code;
 456     }
 457 
 458     // a stack of polynomial curves where each curve shares endpoints with
 459     // adjacent ones.
 460     static final class PolyStack {
 461         private static final byte TYPE_LINETO  = (byte) 0;
 462         private static final byte TYPE_QUADTO  = (byte) 1;
 463         private static final byte TYPE_CUBICTO = (byte) 2;
 464 
 465         // curves capacity = edges count (8192) = edges x 2 (coords)
 466         private static final int INITIAL_CURVES_COUNT = INITIAL_EDGES_COUNT << 1;
 467 
 468         // types capacity = edges count (4096)
 469         private static final int INITIAL_TYPES_COUNT = INITIAL_EDGES_COUNT;
 470 
 471         double[] curves;
 472         int end;
 473         byte[] curveTypes;
 474         int numCurves;
 475 
 476         // curves ref (dirty)
 477         final DoubleArrayCache.Reference curves_ref;
 478         // curveTypes ref (dirty)
 479         final ByteArrayCache.Reference curveTypes_ref;
 480 
 481         // used marks (stats only)
 482         int curveTypesUseMark;
 483         int curvesUseMark;
 484 
 485         private final StatLong stat_polystack_types;
 486         private final StatLong stat_polystack_curves;
 487         private final Histogram hist_polystack_curves;
 488         private final StatLong stat_array_polystack_curves;
 489         private final StatLong stat_array_polystack_curveTypes;
 490 
 491        PolyStack(final DRendererContext rdrCtx) {
 492            this(rdrCtx, null, null, null, null, null);
 493        }
 494 
 495        PolyStack(final DRendererContext rdrCtx,
 496                  final StatLong stat_polystack_types,
 497                  final StatLong stat_polystack_curves,
 498                  final Histogram hist_polystack_curves,
 499                  final StatLong stat_array_polystack_curves,
 500                  final StatLong stat_array_polystack_curveTypes)
 501        {
 502             curves_ref = rdrCtx.newDirtyDoubleArrayRef(INITIAL_CURVES_COUNT); // 32K
 503             curves     = curves_ref.initial;
 504 
 505             curveTypes_ref = rdrCtx.newDirtyByteArrayRef(INITIAL_TYPES_COUNT); // 4K
 506             curveTypes     = curveTypes_ref.initial;
 507             numCurves = 0;
 508             end = 0;
 509 
 510             if (DO_STATS) {
 511                 curveTypesUseMark = 0;
 512                 curvesUseMark = 0;
 513             }
 514             this.stat_polystack_types = stat_polystack_types;
 515             this.stat_polystack_curves = stat_polystack_curves;
 516             this.hist_polystack_curves = hist_polystack_curves;
 517             this.stat_array_polystack_curves = stat_array_polystack_curves;
 518             this.stat_array_polystack_curveTypes = stat_array_polystack_curveTypes;
 519         }
 520 
 521         /**
 522          * Disposes this PolyStack:
 523          * clean up before reusing this instance
 524          */
 525         void dispose() {
 526             end = 0;
 527             numCurves = 0;
 528 
 529             if (DO_STATS) {
 530                 stat_polystack_types.add(curveTypesUseMark);
 531                 stat_polystack_curves.add(curvesUseMark);
 532                 hist_polystack_curves.add(curvesUseMark);
 533 
 534                 // reset marks
 535                 curveTypesUseMark = 0;
 536                 curvesUseMark = 0;
 537             }
 538 
 539             // Return arrays:
 540             // curves and curveTypes are kept dirty
 541             curves     = curves_ref.putArray(curves);
 542             curveTypes = curveTypes_ref.putArray(curveTypes);
 543         }
 544 
 545         private void ensureSpace(final int n) {
 546             // use substraction to avoid integer overflow:
 547             if (curves.length - end < n) {
 548                 if (DO_STATS) {
 549                     stat_array_polystack_curves.add(end + n);
 550                 }
 551                 curves = curves_ref.widenArray(curves, end, end + n);
 552             }
 553             if (curveTypes.length <= numCurves) {
 554                 if (DO_STATS) {
 555                     stat_array_polystack_curveTypes.add(numCurves + 1);
 556                 }
 557                 curveTypes = curveTypes_ref.widenArray(curveTypes,
 558                                                        numCurves,
 559                                                        numCurves + 1);
 560             }
 561         }
 562 
 563         void pushCubic(double x0, double y0,
 564                        double x1, double y1,
 565                        double x2, double y2)
 566         {
 567             ensureSpace(6);
 568             curveTypes[numCurves++] = TYPE_CUBICTO;
 569             // we reverse the coordinate order to make popping easier
 570             final double[] _curves = curves;
 571             int e = end;
 572             _curves[e++] = x2;    _curves[e++] = y2;
 573             _curves[e++] = x1;    _curves[e++] = y1;
 574             _curves[e++] = x0;    _curves[e++] = y0;
 575             end = e;
 576         }
 577 
 578         void pushQuad(double x0, double y0,
 579                       double x1, double y1)
 580         {
 581             ensureSpace(4);
 582             curveTypes[numCurves++] = TYPE_QUADTO;
 583             final double[] _curves = curves;
 584             int e = end;
 585             _curves[e++] = x1;    _curves[e++] = y1;
 586             _curves[e++] = x0;    _curves[e++] = y0;
 587             end = e;
 588         }
 589 
 590         void pushLine(double x, double y) {
 591             ensureSpace(2);
 592             curveTypes[numCurves++] = TYPE_LINETO;
 593             curves[end++] = x;    curves[end++] = y;
 594         }
 595 
 596         void pullAll(final DPathConsumer2D io) {
 597             final int nc = numCurves;
 598             if (nc == 0) {
 599                 return;
 600             }
 601             if (DO_STATS) {
 602                 // update used marks:
 603                 if (numCurves > curveTypesUseMark) {
 604                     curveTypesUseMark = numCurves;
 605                 }
 606                 if (end > curvesUseMark) {
 607                     curvesUseMark = end;
 608                 }
 609             }
 610             final byte[]  _curveTypes = curveTypes;
 611             final double[] _curves = curves;
 612             int e = 0;
 613 
 614             for (int i = 0; i < nc; i++) {
 615                 switch(_curveTypes[i]) {
 616                 case TYPE_LINETO:
 617                     io.lineTo(_curves[e], _curves[e+1]);
 618                     e += 2;
 619                     continue;
 620                 case TYPE_QUADTO:
 621                     io.quadTo(_curves[e+0], _curves[e+1],
 622                               _curves[e+2], _curves[e+3]);
 623                     e += 4;
 624                     continue;
 625                 case TYPE_CUBICTO:
 626                     io.curveTo(_curves[e+0], _curves[e+1],
 627                                _curves[e+2], _curves[e+3],
 628                                _curves[e+4], _curves[e+5]);
 629                     e += 6;
 630                     continue;
 631                 default:
 632                 }
 633             }
 634             numCurves = 0;
 635             end = 0;
 636         }
 637 
 638         void popAll(final DPathConsumer2D io) {
 639             int nc = numCurves;
 640             if (nc == 0) {
 641                 return;
 642             }
 643             if (DO_STATS) {
 644                 // update used marks:
 645                 if (numCurves > curveTypesUseMark) {
 646                     curveTypesUseMark = numCurves;
 647                 }
 648                 if (end > curvesUseMark) {
 649                     curvesUseMark = end;
 650                 }
 651             }
 652             final byte[]  _curveTypes = curveTypes;
 653             final double[] _curves = curves;
 654             int e  = end;
 655 
 656             while (nc != 0) {
 657                 switch(_curveTypes[--nc]) {
 658                 case TYPE_LINETO:
 659                     e -= 2;
 660                     io.lineTo(_curves[e], _curves[e+1]);
 661                     continue;
 662                 case TYPE_QUADTO:
 663                     e -= 4;
 664                     io.quadTo(_curves[e+0], _curves[e+1],
 665                               _curves[e+2], _curves[e+3]);
 666                     continue;
 667                 case TYPE_CUBICTO:
 668                     e -= 6;
 669                     io.curveTo(_curves[e+0], _curves[e+1],
 670                                _curves[e+2], _curves[e+3],
 671                                _curves[e+4], _curves[e+5]);
 672                     continue;
 673                 default:
 674                 }
 675             }
 676             numCurves = 0;
 677             end = 0;
 678         }
 679 
 680         @Override
 681         public String toString() {
 682             String ret = "";
 683             int nc = numCurves;
 684             int last = end;
 685             int len;
 686             while (nc != 0) {
 687                 switch(curveTypes[--nc]) {
 688                 case TYPE_LINETO:
 689                     len = 2;
 690                     ret += "line: ";
 691                     break;
 692                 case TYPE_QUADTO:
 693                     len = 4;
 694                     ret += "quad: ";
 695                     break;
 696                 case TYPE_CUBICTO:
 697                     len = 6;
 698                     ret += "cubic: ";
 699                     break;
 700                 default:
 701                     len = 0;
 702                 }
 703                 last -= len;
 704                 ret += Arrays.toString(Arrays.copyOfRange(curves, last, last+len))
 705                                        + "\n";
 706             }
 707             return ret;
 708         }
 709     }
 710 }