Stroker.java 45.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27
 */

package sun.java2d.pisces;

28 29
import java.util.Arrays;
import java.util.Iterator;
D
dlila 已提交
30 31
import static java.lang.Math.ulp;
import static java.lang.Math.sqrt;
32 33 34 35 36 37

import sun.awt.geom.PathConsumer2D;

// TODO: some of the arithmetic here is too verbose and prone to hard to
// debug typos. We should consider making a small Point/Vector class that
// has methods like plus(Point), minus(Point), dot(Point), cross(Point)and such
38
final class Stroker implements PathConsumer2D {
D
duke 已提交
39 40

    private static final int MOVE_TO = 0;
41
    private static final int DRAWING_OP_TO = 1; // ie. curve, line, or quad
D
duke 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    private static final int CLOSE = 2;

    /**
     * Constant value for join style.
     */
    public static final int JOIN_MITER = 0;

    /**
     * Constant value for join style.
     */
    public static final int JOIN_ROUND = 1;

    /**
     * Constant value for join style.
     */
    public static final int JOIN_BEVEL = 2;

    /**
     * Constant value for end cap style.
     */
    public static final int CAP_BUTT = 0;

    /**
     * Constant value for end cap style.
     */
    public static final int CAP_ROUND = 1;

    /**
     * Constant value for end cap style.
     */
    public static final int CAP_SQUARE = 2;

74
    private final PathConsumer2D out;
D
duke 已提交
75

76 77
    private final int capStyle;
    private final int joinStyle;
D
duke 已提交
78

79
    private final float lineWidth2;
80 81

    private final float[][] offset = new float[3][2];
82 83 84 85 86
    private final float[] miter = new float[2];
    private final float miterLimitSq;

    private int prev;

87 88 89 90 91 92 93 94 95 96 97
    // The starting point of the path, and the slope there.
    private float sx0, sy0, sdx, sdy;
    // the current point and the slope there.
    private float cx0, cy0, cdx, cdy; // c stands for current
    // vectors that when added to (sx0,sy0) and (cx0,cy0) respectively yield the
    // first and last points on the left parallel path. Since this path is
    // parallel, it's slope at any point is parallel to the slope of the
    // original path (thought they may have different directions), so these
    // could be computed from sdx,sdy and cdx,cdy (and vice versa), but that
    // would be error prone and hard to read, so we keep these anyway.
    private float smx, smy, cmx, cmy;
98

99
    private final PolyStack reverse = new PolyStack();
D
duke 已提交
100 101 102 103

    /**
     * Constructs a <code>Stroker</code>.
     *
104
     * @param pc2d an output <code>PathConsumer2D</code>.
105
     * @param lineWidth the desired line width in pixels
D
duke 已提交
106 107 108 109 110 111
     * @param capStyle the desired end cap style, one of
     * <code>CAP_BUTT</code>, <code>CAP_ROUND</code> or
     * <code>CAP_SQUARE</code>.
     * @param joinStyle the desired line join style, one of
     * <code>JOIN_MITER</code>, <code>JOIN_ROUND</code> or
     * <code>JOIN_BEVEL</code>.
112
     * @param miterLimit the desired miter limit
D
duke 已提交
113
     */
114
    public Stroker(PathConsumer2D pc2d,
115
                   float lineWidth,
D
duke 已提交
116 117
                   int capStyle,
                   int joinStyle,
118 119 120
                   float miterLimit)
    {
        this.out = pc2d;
D
duke 已提交
121

122
        this.lineWidth2 = lineWidth / 2;
D
duke 已提交
123 124 125
        this.capStyle = capStyle;
        this.joinStyle = joinStyle;

126
        float limit = miterLimit * lineWidth2;
127
        this.miterLimitSq = limit*limit;
D
duke 已提交
128

129
        this.prev = CLOSE;
D
duke 已提交
130 131
    }

132 133 134
    private static void computeOffset(final float lx, final float ly,
                                      final float w, final float[] m)
    {
D
dlila 已提交
135
        final float len = (float) sqrt(lx*lx + ly*ly);
136 137
        if (len == 0) {
            m[0] = m[1] = 0;
D
duke 已提交
138
        } else {
139 140
            m[0] = (ly * w)/len;
            m[1] = -(lx * w)/len;
D
duke 已提交
141 142 143
        }
    }

144 145 146 147 148 149 150 151 152 153 154 155
    // Returns true if the vectors (dx1, dy1) and (dx2, dy2) are
    // clockwise (if dx1,dy1 needs to be rotated clockwise to close
    // the smallest angle between it and dx2,dy2).
    // This is equivalent to detecting whether a point q is on the right side
    // of a line passing through points p1, p2 where p2 = p1+(dx1,dy1) and
    // q = p2+(dx2,dy2), which is the same as saying p1, p2, q are in a
    // clockwise order.
    // NOTE: "clockwise" here assumes coordinates with 0,0 at the bottom left.
    private static boolean isCW(final float dx1, final float dy1,
                                final float dx2, final float dy2)
    {
        return dx1 * dy2 <= dy1 * dx2;
D
duke 已提交
156 157
    }

158
    // pisces used to use fixed point arithmetic with 16 decimal digits. I
159
    // didn't want to change the values of the constant below when I converted
160 161
    // it to floating point, so that's why the divisions by 2^16 are there.
    private static final float ROUND_JOIN_THRESHOLD = 1000/65536f;
D
duke 已提交
162

163 164
    private void drawRoundJoin(float x, float y,
                               float omx, float omy, float mx, float my,
D
duke 已提交
165
                               boolean rev,
166 167
                               float threshold)
    {
D
duke 已提交
168 169 170 171
        if ((omx == 0 && omy == 0) || (mx == 0 && my == 0)) {
            return;
        }

172 173 174
        float domx = omx - mx;
        float domy = omy - my;
        float len = domx*domx + domy*domy;
D
duke 已提交
175 176 177 178 179 180 181 182 183 184
        if (len < threshold) {
            return;
        }

        if (rev) {
            omx = -omx;
            omy = -omy;
            mx = -mx;
            my = -my;
        }
185 186
        drawRoundJoin(x, y, omx, omy, mx, my, rev);
    }
D
duke 已提交
187

188 189 190 191 192 193 194 195
    private void drawRoundJoin(float cx, float cy,
                               float omx, float omy,
                               float mx, float my,
                               boolean rev)
    {
        // The sign of the dot product of mx,my and omx,omy is equal to the
        // the sign of the cosine of ext
        // (ext is the angle between omx,omy and mx,my).
196
        final float cosext = omx * mx + omy * my;
197 198 199
        // If it is >=0, we know that abs(ext) is <= 90 degrees, so we only
        // need 1 curve to approximate the circle section that joins omx,omy
        // and mx,my.
200
        final int numCurves = (cosext >= 0f) ? 1 : 2;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

        switch (numCurves) {
        case 1:
            drawBezApproxForArc(cx, cy, omx, omy, mx, my, rev);
            break;
        case 2:
            // we need to split the arc into 2 arcs spanning the same angle.
            // The point we want will be one of the 2 intersections of the
            // perpendicular bisector of the chord (omx,omy)->(mx,my) and the
            // circle. We could find this by scaling the vector
            // (omx+mx, omy+my)/2 so that it has length=lineWidth2 (and thus lies
            // on the circle), but that can have numerical problems when the angle
            // between omx,omy and mx,my is close to 180 degrees. So we compute a
            // normal of (omx,omy)-(mx,my). This will be the direction of the
            // perpendicular bisector. To get one of the intersections, we just scale
            // this vector that its length is lineWidth2 (this works because the
            // perpendicular bisector goes through the origin). This scaling doesn't
            // have numerical problems because we know that lineWidth2 divided by
            // this normal's length is at least 0.5 and at most sqrt(2)/2 (because
            // we know the angle of the arc is > 90 degrees).
            float nx = my - omy, ny = omx - mx;
D
dlila 已提交
222
            float nlen = (float) sqrt(nx*nx + ny*ny);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            float scale = lineWidth2/nlen;
            float mmx = nx * scale, mmy = ny * scale;

            // if (isCW(omx, omy, mx, my) != isCW(mmx, mmy, mx, my)) then we've
            // computed the wrong intersection so we get the other one.
            // The test above is equivalent to if (rev).
            if (rev) {
                mmx = -mmx;
                mmy = -mmy;
            }
            drawBezApproxForArc(cx, cy, omx, omy, mmx, mmy, rev);
            drawBezApproxForArc(cx, cy, mmx, mmy, mx, my, rev);
            break;
        }
    }
D
duke 已提交
238

239 240 241 242 243 244
    // the input arc defined by omx,omy and mx,my must span <= 90 degrees.
    private void drawBezApproxForArc(final float cx, final float cy,
                                     final float omx, final float omy,
                                     final float mx, final float my,
                                     boolean rev)
    {
245 246 247 248 249 250 251 252 253
        final float cosext2 = (omx * mx + omy * my) / (2f * lineWidth2 * lineWidth2);

        // check round off errors producing cos(ext) > 1 and a NaN below
        // cos(ext) == 1 implies colinear segments and an empty join anyway
        if (cosext2 >= 0.5f) {
            // just return to avoid generating a flat curve:
            return;
        }

254 255 256 257 258
        // cv is the length of P1-P0 and P2-P3 divided by the radius of the arc
        // (so, cv assumes the arc has radius 1). P0, P1, P2, P3 are the points that
        // define the bezier curve we're computing.
        // It is computed using the constraints that P1-P0 and P3-P2 are parallel
        // to the arc tangents at the endpoints, and that |P1-P0|=|P3-P2|.
259 260
        float cv = (float) ((4.0 / 3.0) * sqrt(0.5 - cosext2) /
                            (1.0 + sqrt(cosext2 + 0.5)));
261 262 263
        // if clockwise, we need to negate cv.
        if (rev) { // rev is equivalent to isCW(omx, omy, mx, my)
            cv = -cv;
D
duke 已提交
264
        }
265 266 267 268 269 270 271 272 273 274 275
        final float x1 = cx + omx;
        final float y1 = cy + omy;
        final float x2 = x1 - cv * omy;
        final float y2 = y1 + cv * omx;

        final float x4 = cx + mx;
        final float y4 = cy + my;
        final float x3 = x4 + cv * my;
        final float y3 = y4 - cv * mx;

        emitCurveTo(x1, y1, x2, y2, x3, y3, x4, y4, rev);
D
duke 已提交
276 277
    }

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    private void drawRoundCap(float cx, float cy, float mx, float my) {
        final float C = 0.5522847498307933f;
        // the first and second arguments of the following two calls
        // are really will be ignored by emitCurveTo (because of the false),
        // but we put them in anyway, as opposed to just giving it 4 zeroes,
        // because it's just 4 additions and it's not good to rely on this
        // sort of assumption (right now it's true, but that may change).
        emitCurveTo(cx+mx,      cy+my,
                    cx+mx-C*my, cy+my+C*mx,
                    cx-my+C*mx, cy+mx+C*my,
                    cx-my,      cy+mx,
                    false);
        emitCurveTo(cx-my,      cy+mx,
                    cx-my-C*mx, cy+mx-C*my,
                    cx-mx-C*my, cy-my+C*mx,
                    cx-mx,      cy-my,
                    false);
    }

D
dlila 已提交
297 298 299 300 301 302 303 304
    // Put the intersection point of the lines (x0, y0) -> (x1, y1)
    // and (x0p, y0p) -> (x1p, y1p) in m[off] and m[off+1].
    // If the lines are parallel, it will put a non finite number in m.
    private void computeIntersection(final float x0, final float y0,
                                     final float x1, final float y1,
                                     final float x0p, final float y0p,
                                     final float x1p, final float y1p,
                                     final float[] m, int off)
305
    {
306 307 308 309 310 311
        float x10 = x1 - x0;
        float y10 = y1 - y0;
        float x10p = x1p - x0p;
        float y10p = y1p - y0p;

        float den = x10*y10p - x10p*y10;
312 313 314 315
        float t = x10p*(y0-y0p) - y10p*(x0-x0p);
        t /= den;
        m[off++] = x0 + t*x10;
        m[off] = y0 + t*y10;
D
duke 已提交
316 317
    }

318 319 320
    private void drawMiter(final float pdx, final float pdy,
                           final float x0, final float y0,
                           final float dx, final float dy,
321
                           float omx, float omy, float mx, float my,
322 323 324 325
                           boolean rev)
    {
        if ((mx == omx && my == omy) ||
            (pdx == 0 && pdy == 0) ||
D
dlila 已提交
326 327
            (dx == 0 && dy == 0))
        {
D
duke 已提交
328 329 330 331 332 333 334 335 336 337
            return;
        }

        if (rev) {
            omx = -omx;
            omy = -omy;
            mx = -mx;
            my = -my;
        }

D
dlila 已提交
338 339 340
        computeIntersection((x0 - pdx) + omx, (y0 - pdy) + omy, x0 + omx, y0 + omy,
                            (dx + x0) + mx, (dy + y0) + my, x0 + mx, y0 + my,
                            miter, 0);
D
duke 已提交
341

342
        float lenSq = (miter[0]-x0)*(miter[0]-x0) + (miter[1]-y0)*(miter[1]-y0);
D
duke 已提交
343

D
dlila 已提交
344 345 346 347 348
        // If the lines are parallel, lenSq will be either NaN or +inf
        // (actually, I'm not sure if the latter is possible. The important
        // thing is that -inf is not possible, because lenSq is a square).
        // For both of those values, the comparison below will fail and
        // no miter will be drawn, which is correct.
D
duke 已提交
349 350 351 352 353
        if (lenSq < miterLimitSq) {
            emitLineTo(miter[0], miter[1], rev);
        }
    }

354
    public void moveTo(float x0, float y0) {
355
        if (prev == DRAWING_OP_TO) {
D
duke 已提交
356 357
            finish();
        }
358 359 360 361
        this.sx0 = this.cx0 = x0;
        this.sy0 = this.cy0 = y0;
        this.cdx = this.sdx = 1;
        this.cdy = this.sdy = 0;
D
duke 已提交
362 363 364
        this.prev = MOVE_TO;
    }

365 366 367 368 369 370 371 372 373 374 375
    public void lineTo(float x1, float y1) {
        float dx = x1 - cx0;
        float dy = y1 - cy0;
        if (dx == 0f && dy == 0f) {
            dx = 1;
        }
        computeOffset(dx, dy, lineWidth2, offset[0]);
        float mx = offset[0][0];
        float my = offset[0][1];

        drawJoin(cdx, cdy, cx0, cy0, dx, dy, cmx, cmy, mx, my);
D
duke 已提交
376

377 378
        emitLineTo(cx0 + mx, cy0 + my);
        emitLineTo(x1 + mx, y1 + my);
D
duke 已提交
379

380 381 382 383 384 385 386 387 388 389 390
        emitLineTo(cx0 - mx, cy0 - my, true);
        emitLineTo(x1 - mx, y1 - my, true);

        this.cmx = mx;
        this.cmy = my;
        this.cdx = dx;
        this.cdy = dy;
        this.cx0 = x1;
        this.cy0 = y1;
        this.prev = DRAWING_OP_TO;
    }
D
duke 已提交
391

392 393 394
    public void closePath() {
        if (prev != DRAWING_OP_TO) {
            if (prev == CLOSE) {
D
duke 已提交
395 396
                return;
            }
397 398 399 400 401 402
            emitMoveTo(cx0, cy0 - lineWidth2);
            this.cmx = this.smx = 0;
            this.cmy = this.smy = -lineWidth2;
            this.cdx = this.sdx = 1;
            this.cdy = this.sdy = 0;
            finish();
D
duke 已提交
403 404 405
            return;
        }

406 407 408 409 410 411 412 413 414 415 416 417 418
        if (cx0 != sx0 || cy0 != sy0) {
            lineTo(sx0, sy0);
        }

        drawJoin(cdx, cdy, cx0, cy0, sdx, sdy, cmx, cmy, smx, smy);

        emitLineTo(sx0 + smx, sy0 + smy);

        emitMoveTo(sx0 - smx, sy0 - smy);
        emitReverse();

        this.prev = CLOSE;
        emitClose();
D
duke 已提交
419 420
    }

421 422 423 424 425
    private void emitReverse() {
        while(!reverse.isEmpty()) {
            reverse.pop(out);
        }
    }
D
duke 已提交
426

427 428 429 430 431 432 433 434 435 436
    public void pathDone() {
        if (prev == DRAWING_OP_TO) {
            finish();
        }

        out.pathDone();
        // this shouldn't matter since this object won't be used
        // after the call to this method.
        this.prev = CLOSE;
    }
D
duke 已提交
437

438 439 440 441 442 443
    private void finish() {
        if (capStyle == CAP_ROUND) {
            drawRoundCap(cx0, cy0, cmx, cmy);
        } else if (capStyle == CAP_SQUARE) {
            emitLineTo(cx0 - cmy + cmx, cy0 + cmx + cmy);
            emitLineTo(cx0 - cmy - cmx, cy0 + cmx - cmy);
D
duke 已提交
444 445
        }

446
        emitReverse();
D
duke 已提交
447

448 449 450 451 452 453
        if (capStyle == CAP_ROUND) {
            drawRoundCap(sx0, sy0, -smx, -smy);
        } else if (capStyle == CAP_SQUARE) {
            emitLineTo(sx0 + smy - smx, sy0 - smx - smy);
            emitLineTo(sx0 + smy + smx, sy0 - smx + smy);
        }
D
duke 已提交
454

455
        emitClose();
D
duke 已提交
456 457
    }

458 459 460
    private void emitMoveTo(final float x0, final float y0) {
        out.moveTo(x0, y0);
    }
D
duke 已提交
461

462 463 464 465 466 467 468 469 470 471 472
    private void emitLineTo(final float x1, final float y1) {
        out.lineTo(x1, y1);
    }

    private void emitLineTo(final float x1, final float y1,
                            final boolean rev)
    {
        if (rev) {
            reverse.pushLine(x1, y1);
        } else {
            emitLineTo(x1, y1);
D
duke 已提交
473
        }
474
    }
D
duke 已提交
475

476 477 478 479 480 481 482 483
    private void emitQuadTo(final float x0, final float y0,
                            final float x1, final float y1,
                            final float x2, final float y2, final boolean rev)
    {
        if (rev) {
            reverse.pushQuad(x0, y0, x1, y1);
        } else {
            out.quadTo(x1, y1, x2, y2);
D
duke 已提交
484
        }
485 486 487 488 489 490 491 492 493 494 495 496 497
    }

    private void emitCurveTo(final float x0, final float y0,
                             final float x1, final float y1,
                             final float x2, final float y2,
                             final float x3, final float y3, final boolean rev)
    {
        if (rev) {
            reverse.pushCubic(x0, y0, x1, y1, x2, y2);
        } else {
            out.curveTo(x1, y1, x2, y2, x3, y3);
        }
    }
D
duke 已提交
498

499 500 501
    private void emitClose() {
        out.closePath();
    }
D
duke 已提交
502

503 504 505 506 507 508 509 510 511 512 513 514 515 516
    private void drawJoin(float pdx, float pdy,
                          float x0, float y0,
                          float dx, float dy,
                          float omx, float omy,
                          float mx, float my)
    {
        if (prev != DRAWING_OP_TO) {
            emitMoveTo(x0 + mx, y0 + my);
            this.sdx = dx;
            this.sdy = dy;
            this.smx = mx;
            this.smy = my;
        } else {
            boolean cw = isCW(pdx, pdy, dx, dy);
D
duke 已提交
517
            if (joinStyle == JOIN_MITER) {
518
                drawMiter(pdx, pdy, x0, y0, dx, dy, omx, omy, mx, my, cw);
D
duke 已提交
519
            } else if (joinStyle == JOIN_ROUND) {
520 521 522
                drawRoundJoin(x0, y0,
                              omx, omy,
                              mx, my, cw,
D
duke 已提交
523 524
                              ROUND_JOIN_THRESHOLD);
            }
525
            emitLineTo(x0, y0, !cw);
D
duke 已提交
526
        }
527 528
        prev = DRAWING_OP_TO;
    }
D
duke 已提交
529

530 531 532 533 534 535 536 537 538 539
    private static boolean within(final float x1, final float y1,
                                  final float x2, final float y2,
                                  final float ERR)
    {
        assert ERR > 0 : "";
        // compare taxicab distance. ERR will always be small, so using
        // true distance won't give much benefit
        return (Helpers.within(x1, x2, ERR) &&  // we want to avoid calling Math.abs
                Helpers.within(y1, y2, ERR)); // this is just as good.
    }
D
duke 已提交
540

541 542 543 544 545 546 547 548 549 550 551 552 553
    private void getLineOffsets(float x1, float y1,
                                float x2, float y2,
                                float[] left, float[] right) {
        computeOffset(x2 - x1, y2 - y1, lineWidth2, offset[0]);
        left[0] = x1 + offset[0][0];
        left[1] = y1 + offset[0][1];
        left[2] = x2 + offset[0][0];
        left[3] = y2 + offset[0][1];
        right[0] = x1 - offset[0][0];
        right[1] = y1 - offset[0][1];
        right[2] = x2 - offset[0][0];
        right[3] = y2 - offset[0][1];
    }
D
duke 已提交
554

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    private int computeOffsetCubic(float[] pts, final int off,
                                   float[] leftOff, float[] rightOff)
    {
        // if p1=p2 or p3=p4 it means that the derivative at the endpoint
        // vanishes, which creates problems with computeOffset. Usually
        // this happens when this stroker object is trying to winden
        // a curve with a cusp. What happens is that curveTo splits
        // the input curve at the cusp, and passes it to this function.
        // because of inaccuracies in the splitting, we consider points
        // equal if they're very close to each other.
        final float x1 = pts[off + 0], y1 = pts[off + 1];
        final float x2 = pts[off + 2], y2 = pts[off + 3];
        final float x3 = pts[off + 4], y3 = pts[off + 5];
        final float x4 = pts[off + 6], y4 = pts[off + 7];

        float dx4 = x4 - x3;
        float dy4 = y4 - y3;
        float dx1 = x2 - x1;
        float dy1 = y2 - y1;

        // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
        // in which case ignore if p1 == p2
D
dlila 已提交
577 578
        final boolean p1eqp2 = within(x1,y1,x2,y2, 6 * ulp(y2));
        final boolean p3eqp4 = within(x3,y3,x4,y4, 6 * ulp(y4));
579 580 581 582 583 584 585 586 587
        if (p1eqp2 && p3eqp4) {
            getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
            return 4;
        } else if (p1eqp2) {
            dx1 = x3 - x1;
            dy1 = y3 - y1;
        } else if (p3eqp4) {
            dx4 = x4 - x2;
            dy4 = y4 - y2;
D
duke 已提交
588 589
        }

590 591 592 593
        // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
        float dotsq = (dx1 * dx4 + dy1 * dy4);
        dotsq = dotsq * dotsq;
        float l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4;
D
dlila 已提交
594
        if (Helpers.within(dotsq, l1sq * l4sq, 4 * ulp(dotsq))) {
595 596 597
            getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
            return 4;
        }
D
duke 已提交
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
//      What we're trying to do in this function is to approximate an ideal
//      offset curve (call it I) of the input curve B using a bezier curve Bp.
//      The constraints I use to get the equations are:
//
//      1. The computed curve Bp should go through I(0) and I(1). These are
//      x1p, y1p, x4p, y4p, which are p1p and p4p. We still need to find
//      4 variables: the x and y components of p2p and p3p (i.e. x2p, y2p, x3p, y3p).
//
//      2. Bp should have slope equal in absolute value to I at the endpoints. So,
//      (by the way, the operator || in the comments below means "aligned with".
//      It is defined on vectors, so when we say I'(0) || Bp'(0) we mean that
//      vectors I'(0) and Bp'(0) are aligned, which is the same as saying
//      that the tangent lines of I and Bp at 0 are parallel. Mathematically
//      this means (I'(t) || Bp'(t)) <==> (I'(t) = c * Bp'(t)) where c is some
//      nonzero constant.)
//      I'(0) || Bp'(0) and I'(1) || Bp'(1). Obviously, I'(0) || B'(0) and
//      I'(1) || B'(1); therefore, Bp'(0) || B'(0) and Bp'(1) || B'(1).
//      We know that Bp'(0) || (p2p-p1p) and Bp'(1) || (p4p-p3p) and the same
//      is true for any bezier curve; therefore, we get the equations
//          (1) p2p = c1 * (p2-p1) + p1p
//          (2) p3p = c2 * (p4-p3) + p4p
//      We know p1p, p4p, p2, p1, p3, and p4; therefore, this reduces the number
//      of unknowns from 4 to 2 (i.e. just c1 and c2).
//      To eliminate these 2 unknowns we use the following constraint:
//
//      3. Bp(0.5) == I(0.5). Bp(0.5)=(x,y) and I(0.5)=(xi,yi), and I should note
//      that I(0.5) is *the only* reason for computing dxm,dym. This gives us
//          (3) Bp(0.5) = (p1p + 3 * (p2p + p3p) + p4p)/8, which is equivalent to
//          (4) p2p + p3p = (Bp(0.5)*8 - p1p - p4p) / 3
//      We can substitute (1) and (2) from above into (4) and we get:
//          (5) c1*(p2-p1) + c2*(p4-p3) = (Bp(0.5)*8 - p1p - p4p)/3 - p1p - p4p
//      which is equivalent to
//          (6) c1*(p2-p1) + c2*(p4-p3) = (4/3) * (Bp(0.5) * 2 - p1p - p4p)
//
//      The right side of this is a 2D vector, and we know I(0.5), which gives us
//      Bp(0.5), which gives us the value of the right side.
//      The left side is just a matrix vector multiplication in disguise. It is
//
//      [x2-x1, x4-x3][c1]
//      [y2-y1, y4-y3][c2]
//      which, is equal to
//      [dx1, dx4][c1]
//      [dy1, dy4][c2]
//      At this point we are left with a simple linear system and we solve it by
//      getting the inverse of the matrix above. Then we use [c1,c2] to compute
//      p2p and p3p.

        float x = 0.125f * (x1 + 3 * (x2 + x3) + x4);
        float y = 0.125f * (y1 + 3 * (y2 + y3) + y4);
        // (dxm,dym) is some tangent of B at t=0.5. This means it's equal to
        // c*B'(0.5) for some constant c.
        float dxm = x3 + x4 - x1 - x2, dym = y3 + y4 - y1 - y2;

        // this computes the offsets at t=0, 0.5, 1, using the property that
        // for any bezier curve the vectors p2-p1 and p4-p3 are parallel to
        // the (dx/dt, dy/dt) vectors at the endpoints.
        computeOffset(dx1, dy1, lineWidth2, offset[0]);
        computeOffset(dxm, dym, lineWidth2, offset[1]);
        computeOffset(dx4, dy4, lineWidth2, offset[2]);
        float x1p = x1 + offset[0][0]; // start
        float y1p = y1 + offset[0][1]; // point
        float xi  = x + offset[1][0]; // interpolation
        float yi  = y + offset[1][1]; // point
        float x4p = x4 + offset[2][0]; // end
        float y4p = y4 + offset[2][1]; // point

        float invdet43 = 4f / (3f * (dx1 * dy4 - dy1 * dx4));

        float two_pi_m_p1_m_p4x = 2*xi - x1p - x4p;
        float two_pi_m_p1_m_p4y = 2*yi - y1p - y4p;
        float c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);
        float c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);

        float x2p, y2p, x3p, y3p;
        x2p = x1p + c1*dx1;
        y2p = y1p + c1*dy1;
        x3p = x4p + c2*dx4;
        y3p = y4p + c2*dy4;

        leftOff[0] = x1p; leftOff[1] = y1p;
        leftOff[2] = x2p; leftOff[3] = y2p;
        leftOff[4] = x3p; leftOff[5] = y3p;
        leftOff[6] = x4p; leftOff[7] = y4p;

        x1p = x1 - offset[0][0]; y1p = y1 - offset[0][1];
        xi = xi - 2 * offset[1][0]; yi = yi - 2 * offset[1][1];
        x4p = x4 - offset[2][0]; y4p = y4 - offset[2][1];

        two_pi_m_p1_m_p4x = 2*xi - x1p - x4p;
        two_pi_m_p1_m_p4y = 2*yi - y1p - y4p;
        c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);
        c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);

        x2p = x1p + c1*dx1;
        y2p = y1p + c1*dy1;
        x3p = x4p + c2*dx4;
        y3p = y4p + c2*dy4;

        rightOff[0] = x1p; rightOff[1] = y1p;
        rightOff[2] = x2p; rightOff[3] = y2p;
        rightOff[4] = x3p; rightOff[5] = y3p;
        rightOff[6] = x4p; rightOff[7] = y4p;
        return 8;
    }

    // return the kind of curve in the right and left arrays.
    private int computeOffsetQuad(float[] pts, final int off,
                                  float[] leftOff, float[] rightOff)
    {
        final float x1 = pts[off + 0], y1 = pts[off + 1];
        final float x2 = pts[off + 2], y2 = pts[off + 3];
        final float x3 = pts[off + 4], y3 = pts[off + 5];

D
dlila 已提交
712 713 714 715
        final float dx3 = x3 - x2;
        final float dy3 = y3 - y2;
        final float dx1 = x2 - x1;
        final float dy1 = y2 - y1;
D
duke 已提交
716

D
dlila 已提交
717
        // this computes the offsets at t = 0, 1
718 719 720
        computeOffset(dx1, dy1, lineWidth2, offset[0]);
        computeOffset(dx3, dy3, lineWidth2, offset[1]);

D
dlila 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
        leftOff[0]  = x1 + offset[0][0];  leftOff[1] = y1 + offset[0][1];
        leftOff[4]  = x3 + offset[1][0];  leftOff[5] = y3 + offset[1][1];
        rightOff[0] = x1 - offset[0][0]; rightOff[1] = y1 - offset[0][1];
        rightOff[4] = x3 - offset[1][0]; rightOff[5] = y3 - offset[1][1];

        float x1p = leftOff[0]; // start
        float y1p = leftOff[1]; // point
        float x3p = leftOff[4]; // end
        float y3p = leftOff[5]; // point

        // Corner cases:
        // 1. If the two control vectors are parallel, we'll end up with NaN's
        //    in leftOff (and rightOff in the body of the if below), so we'll
        //    do getLineOffsets, which is right.
        // 2. If the first or second two points are equal, then (dx1,dy1)==(0,0)
        //    or (dx3,dy3)==(0,0), so (x1p, y1p)==(x1p+dx1, y1p+dy1)
        //    or (x3p, y3p)==(x3p-dx3, y3p-dy3), which means that
        //    computeIntersection will put NaN's in leftOff and right off, and
        //    we will do getLineOffsets, which is right.
        computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2);
        float cx = leftOff[2];
        float cy = leftOff[3];

        if (!(isFinite(cx) && isFinite(cy))) {
            // maybe the right path is not degenerate.
            x1p = rightOff[0];
            y1p = rightOff[1];
            x3p = rightOff[4];
            y3p = rightOff[5];
            computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2);
            cx = rightOff[2];
            cy = rightOff[3];
            if (!(isFinite(cx) && isFinite(cy))) {
                // both are degenerate. This curve is a line.
                getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
                return 4;
            }
            // {left,right}Off[0,1,4,5] are already set to the correct values.
            leftOff[2] = 2*x2 - cx;
            leftOff[3] = 2*y2 - cy;
            return 6;
        }

        // rightOff[2,3] = (x2,y2) - ((left_x2, left_y2) - (x2, y2))
        // == 2*(x2, y2) - (left_x2, left_y2)
        rightOff[2] = 2*x2 - cx;
        rightOff[3] = 2*y2 - cy;
768
        return 6;
D
duke 已提交
769 770
    }

D
dlila 已提交
771 772 773 774
    private static boolean isFinite(float x) {
        return (Float.NEGATIVE_INFINITY < x && x < Float.POSITIVE_INFINITY);
    }

775 776 777
    // This is where the curve to be processed is put. We give it
    // enough room to store 2 curves: one for the current subdivision, the
    // other for the rest of the curve.
778
    private float[] middle = new float[2*8];
779 780 781 782 783
    private float[] lp = new float[8];
    private float[] rp = new float[8];
    private static final int MAX_N_CURVES = 11;
    private float[] subdivTs = new float[MAX_N_CURVES - 1];

784 785 786 787 788
    // If this class is compiled with ecj, then Hotspot crashes when OSR
    // compiling this function. See bugs 7004570 and 6675699
    // TODO: until those are fixed, we should work around that by
    // manually inlining this into curveTo and quadTo.
/******************************* WORKAROUND **********************************
789 790
    private void somethingTo(final int type) {
        // need these so we can update the state at the end of this method
791 792 793 794 795
        final float xf = middle[type-2], yf = middle[type-1];
        float dxs = middle[2] - middle[0];
        float dys = middle[3] - middle[1];
        float dxf = middle[type - 2] - middle[type - 4];
        float dyf = middle[type - 1] - middle[type - 3];
796 797 798 799
        switch(type) {
        case 6:
            if ((dxs == 0f && dys == 0f) ||
                (dxf == 0f && dyf == 0f)) {
800 801
               dxs = dxf = middle[4] - middle[0];
               dys = dyf = middle[5] - middle[1];
802 803 804 805 806 807
            }
            break;
        case 8:
            boolean p1eqp2 = (dxs == 0f && dys == 0f);
            boolean p3eqp4 = (dxf == 0f && dyf == 0f);
            if (p1eqp2) {
808 809
                dxs = middle[4] - middle[0];
                dys = middle[5] - middle[1];
810
                if (dxs == 0f && dys == 0f) {
811 812
                    dxs = middle[6] - middle[0];
                    dys = middle[7] - middle[1];
813 814 815
                }
            }
            if (p3eqp4) {
816 817
                dxf = middle[6] - middle[2];
                dyf = middle[7] - middle[3];
818
                if (dxf == 0f && dyf == 0f) {
819 820
                    dxf = middle[6] - middle[0];
                    dyf = middle[7] - middle[1];
821 822 823 824 825
                }
            }
        }
        if (dxs == 0f && dys == 0f) {
            // this happens iff the "curve" is just a point
826
            lineTo(middle[0], middle[1]);
827 828 829 830 831
            return;
        }
        // if these vectors are too small, normalize them, to avoid future
        // precision problems.
        if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
D
dlila 已提交
832
            float len = (float) sqrt(dxs*dxs + dys*dys);
833 834
            dxs /= len;
            dys /= len;
835 836
        }
        if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
D
dlila 已提交
837
            float len = (float) sqrt(dxf*dxf + dyf*dyf);
838 839
            dxf /= len;
            dyf /= len;
840
        }
D
duke 已提交
841

842 843 844 845 846
        computeOffset(dxs, dys, lineWidth2, offset[0]);
        final float mx = offset[0][0];
        final float my = offset[0][1];
        drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my);

847
        int nSplits = findSubdivPoints(middle, subdivTs, type, lineWidth2);
848 849

        int kind = 0;
850
        Iterator<Integer> it = Curve.breakPtsAtTs(middle, type, subdivTs, nSplits);
851
        while(it.hasNext()) {
852
            int curCurveOff = it.next();
853 854 855

            switch (type) {
            case 8:
856
                kind = computeOffsetCubic(middle, curCurveOff, lp, rp);
857 858
                break;
            case 6:
859
                kind = computeOffsetQuad(middle, curCurveOff, lp, rp);
860 861
                break;
            }
D
dlila 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875
            emitLineTo(lp[0], lp[1]);
            switch(kind) {
            case 8:
                emitCurveTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], lp[6], lp[7], false);
                emitCurveTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], rp[6], rp[7], true);
                break;
            case 6:
                emitQuadTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], false);
                emitQuadTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], true);
                break;
            case 4:
                emitLineTo(lp[2], lp[3]);
                emitLineTo(rp[0], rp[1], true);
                break;
876
            }
D
dlila 已提交
877
            emitLineTo(rp[kind - 2], rp[kind - 1], true);
D
duke 已提交
878 879
        }

880 881 882 883 884 885 886 887
        this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;
        this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;
        this.cdx = dxf;
        this.cdy = dyf;
        this.cx0 = xf;
        this.cy0 = yf;
        this.prev = DRAWING_OP_TO;
    }
888
****************************** END WORKAROUND *******************************/
889 890 891 892 893

    // finds values of t where the curve in pts should be subdivided in order
    // to get good offset curves a distance of w away from the middle curve.
    // Stores the points in ts, and returns how many of them there were.
    private static Curve c = new Curve();
894
    private static int findSubdivPoints(float[] pts, float[] ts, final int type, final float w)
895 896 897 898 899 900 901 902 903
    {
        final float x12 = pts[2] - pts[0];
        final float y12 = pts[3] - pts[1];
        // if the curve is already parallel to either axis we gain nothing
        // from rotating it.
        if (y12 != 0f && x12 != 0f) {
            // we rotate it so that the first vector in the control polygon is
            // parallel to the x-axis. This will ensure that rotated quarter
            // circles won't be subdivided.
D
dlila 已提交
904
            final float hypot = (float) sqrt(x12 * x12 + y12 * y12);
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
            final float cos = x12 / hypot;
            final float sin = y12 / hypot;
            final float x1 = cos * pts[0] + sin * pts[1];
            final float y1 = cos * pts[1] - sin * pts[0];
            final float x2 = cos * pts[2] + sin * pts[3];
            final float y2 = cos * pts[3] - sin * pts[2];
            final float x3 = cos * pts[4] + sin * pts[5];
            final float y3 = cos * pts[5] - sin * pts[4];
            switch(type) {
            case 8:
                final float x4 = cos * pts[6] + sin * pts[7];
                final float y4 = cos * pts[7] - sin * pts[6];
                c.set(x1, y1, x2, y2, x3, y3, x4, y4);
                break;
            case 6:
                c.set(x1, y1, x2, y2, x3, y3);
                break;
            }
        } else {
            c.set(pts, type);
D
duke 已提交
925 926
        }

927 928 929 930 931 932 933 934 935 936 937 938 939 940
        int ret = 0;
        // we subdivide at values of t such that the remaining rotated
        // curves are monotonic in x and y.
        ret += c.dxRoots(ts, ret);
        ret += c.dyRoots(ts, ret);
        // subdivide at inflection points.
        if (type == 8) {
            // quadratic curves can't have inflection points
            ret += c.infPoints(ts, ret);
        }

        // now we must subdivide at points where one of the offset curves will have
        // a cusp. This happens at ts where the radius of curvature is equal to w.
        ret += c.rootsOfROCMinusW(ts, ret, w, 0.0001f);
941

942 943 944
        ret = Helpers.filterOutNotInAB(ts, 0, ret, 0.0001f, 0.9999f);
        Helpers.isort(ts, 0, ret);
        return ret;
D
duke 已提交
945 946
    }

947 948 949 950
    @Override public void curveTo(float x1, float y1,
                                  float x2, float y2,
                                  float x3, float y3)
    {
951 952 953 954
        middle[0] = cx0; middle[1] = cy0;
        middle[2] = x1;  middle[3] = y1;
        middle[4] = x2;  middle[5] = y2;
        middle[6] = x3;  middle[7] = y3;
D
duke 已提交
955

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
        // inlined version of somethingTo(8);
        // See the TODO on somethingTo

        // need these so we can update the state at the end of this method
        final float xf = middle[6], yf = middle[7];
        float dxs = middle[2] - middle[0];
        float dys = middle[3] - middle[1];
        float dxf = middle[6] - middle[4];
        float dyf = middle[7] - middle[5];

        boolean p1eqp2 = (dxs == 0f && dys == 0f);
        boolean p3eqp4 = (dxf == 0f && dyf == 0f);
        if (p1eqp2) {
            dxs = middle[4] - middle[0];
            dys = middle[5] - middle[1];
            if (dxs == 0f && dys == 0f) {
                dxs = middle[6] - middle[0];
                dys = middle[7] - middle[1];
            }
        }
        if (p3eqp4) {
            dxf = middle[6] - middle[2];
            dyf = middle[7] - middle[3];
            if (dxf == 0f && dyf == 0f) {
                dxf = middle[6] - middle[0];
                dyf = middle[7] - middle[1];
            }
        }
        if (dxs == 0f && dys == 0f) {
            // this happens iff the "curve" is just a point
            lineTo(middle[0], middle[1]);
            return;
        }

        // if these vectors are too small, normalize them, to avoid future
        // precision problems.
        if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
D
dlila 已提交
993
            float len = (float) sqrt(dxs*dxs + dys*dys);
994 995 996 997
            dxs /= len;
            dys /= len;
        }
        if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
D
dlila 已提交
998
            float len = (float) sqrt(dxf*dxf + dyf*dyf);
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
            dxf /= len;
            dyf /= len;
        }

        computeOffset(dxs, dys, lineWidth2, offset[0]);
        final float mx = offset[0][0];
        final float my = offset[0][1];
        drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my);

        int nSplits = findSubdivPoints(middle, subdivTs, 8, lineWidth2);

        int kind = 0;
        Iterator<Integer> it = Curve.breakPtsAtTs(middle, 8, subdivTs, nSplits);
        while(it.hasNext()) {
            int curCurveOff = it.next();

            kind = computeOffsetCubic(middle, curCurveOff, lp, rp);
D
dlila 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
            emitLineTo(lp[0], lp[1]);
            switch(kind) {
            case 8:
                emitCurveTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], lp[6], lp[7], false);
                emitCurveTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], rp[6], rp[7], true);
                break;
            case 4:
                emitLineTo(lp[2], lp[3]);
                emitLineTo(rp[0], rp[1], true);
                break;
1026
            }
D
dlila 已提交
1027
            emitLineTo(rp[kind - 2], rp[kind - 1], true);
1028 1029 1030 1031 1032 1033 1034 1035 1036
        }

        this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;
        this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;
        this.cdx = dxf;
        this.cdy = dyf;
        this.cx0 = xf;
        this.cy0 = yf;
        this.prev = DRAWING_OP_TO;
1037
    }
D
duke 已提交
1038

1039
    @Override public void quadTo(float x1, float y1, float x2, float y2) {
1040 1041 1042
        middle[0] = cx0; middle[1] = cy0;
        middle[2] = x1;  middle[3] = y1;
        middle[4] = x2;  middle[5] = y2;
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

        // inlined version of somethingTo(8);
        // See the TODO on somethingTo

        // need these so we can update the state at the end of this method
        final float xf = middle[4], yf = middle[5];
        float dxs = middle[2] - middle[0];
        float dys = middle[3] - middle[1];
        float dxf = middle[4] - middle[2];
        float dyf = middle[5] - middle[3];
        if ((dxs == 0f && dys == 0f) || (dxf == 0f && dyf == 0f)) {
            dxs = dxf = middle[4] - middle[0];
            dys = dyf = middle[5] - middle[1];
        }
        if (dxs == 0f && dys == 0f) {
            // this happens iff the "curve" is just a point
            lineTo(middle[0], middle[1]);
            return;
        }
        // if these vectors are too small, normalize them, to avoid future
        // precision problems.
        if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
D
dlila 已提交
1065
            float len = (float) sqrt(dxs*dxs + dys*dys);
1066 1067 1068 1069
            dxs /= len;
            dys /= len;
        }
        if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
D
dlila 已提交
1070
            float len = (float) sqrt(dxf*dxf + dyf*dyf);
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
            dxf /= len;
            dyf /= len;
        }

        computeOffset(dxs, dys, lineWidth2, offset[0]);
        final float mx = offset[0][0];
        final float my = offset[0][1];
        drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my);

        int nSplits = findSubdivPoints(middle, subdivTs, 6, lineWidth2);

        int kind = 0;
        Iterator<Integer> it = Curve.breakPtsAtTs(middle, 6, subdivTs, nSplits);
        while(it.hasNext()) {
            int curCurveOff = it.next();

            kind = computeOffsetQuad(middle, curCurveOff, lp, rp);
D
dlila 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
            emitLineTo(lp[0], lp[1]);
            switch(kind) {
            case 6:
                emitQuadTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], false);
                emitQuadTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], true);
                break;
            case 4:
                emitLineTo(lp[2], lp[3]);
                emitLineTo(rp[0], rp[1], true);
                break;
1098
            }
D
dlila 已提交
1099
            emitLineTo(rp[kind - 2], rp[kind - 1], true);
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
        }

        this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;
        this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;
        this.cdx = dxf;
        this.cdy = dyf;
        this.cx0 = xf;
        this.cy0 = yf;
        this.prev = DRAWING_OP_TO;
    }

    @Override public long getNativeConsumer() {
        throw new InternalError("Stroker doesn't use a native consumer");
1113
    }
D
duke 已提交
1114

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    // a stack of polynomial curves where each curve shares endpoints with
    // adjacent ones.
    private static final class PolyStack {
        float[] curves;
        int end;
        int[] curveTypes;
        int numCurves;

        private static final int INIT_SIZE = 50;

        PolyStack() {
            curves = new float[8 * INIT_SIZE];
            curveTypes = new int[INIT_SIZE];
            end = 0;
            numCurves = 0;
D
duke 已提交
1130 1131
        }

1132 1133
        public boolean isEmpty() {
            return numCurves == 0;
D
duke 已提交
1134 1135
        }

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
        private void ensureSpace(int n) {
            if (end + n >= curves.length) {
                int newSize = (end + n) * 2;
                curves = Arrays.copyOf(curves, newSize);
            }
            if (numCurves >= curveTypes.length) {
                int newSize = numCurves * 2;
                curveTypes = Arrays.copyOf(curveTypes, newSize);
            }
        }
D
duke 已提交
1146

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
        public void pushCubic(float x0, float y0,
                              float x1, float y1,
                              float x2, float y2)
        {
            ensureSpace(6);
            curveTypes[numCurves++] = 8;
            // assert(x0 == lastX && y0 == lastY)

            // we reverse the coordinate order to make popping easier
            curves[end++] = x2;    curves[end++] = y2;
            curves[end++] = x1;    curves[end++] = y1;
            curves[end++] = x0;    curves[end++] = y0;
D
duke 已提交
1159 1160
        }

1161 1162 1163 1164 1165 1166 1167 1168 1169
        public void pushQuad(float x0, float y0,
                             float x1, float y1)
        {
            ensureSpace(4);
            curveTypes[numCurves++] = 6;
            // assert(x0 == lastX && y0 == lastY)
            curves[end++] = x1;    curves[end++] = y1;
            curves[end++] = x0;    curves[end++] = y0;
        }
D
duke 已提交
1170

1171 1172 1173 1174 1175 1176
        public void pushLine(float x, float y) {
            ensureSpace(2);
            curveTypes[numCurves++] = 4;
            // assert(x0 == lastX && y0 == lastY)
            curves[end++] = x;    curves[end++] = y;
        }
D
duke 已提交
1177

1178 1179 1180 1181 1182 1183 1184 1185
        @SuppressWarnings("unused")
        public int pop(float[] pts) {
            int ret = curveTypes[numCurves - 1];
            numCurves--;
            end -= (ret - 2);
            System.arraycopy(curves, end, pts, 0, ret - 2);
            return ret;
        }
D
duke 已提交
1186

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
        public void pop(PathConsumer2D io) {
            numCurves--;
            int type = curveTypes[numCurves];
            end -= (type - 2);
            switch(type) {
            case 8:
                io.curveTo(curves[end+0], curves[end+1],
                           curves[end+2], curves[end+3],
                           curves[end+4], curves[end+5]);
                break;
            case 6:
                io.quadTo(curves[end+0], curves[end+1],
                           curves[end+2], curves[end+3]);
                 break;
            case 4:
                io.lineTo(curves[end], curves[end+1]);
            }
D
duke 已提交
1204 1205
        }

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
        @Override
        public String toString() {
            String ret = "";
            int nc = numCurves;
            int end = this.end;
            while (nc > 0) {
                nc--;
                int type = curveTypes[numCurves];
                end -= (type - 2);
                switch(type) {
                case 8:
                    ret += "cubic: ";
                    break;
                case 6:
                    ret += "quad: ";
                    break;
                case 4:
                    ret += "line: ";
                    break;
                }
                ret += Arrays.toString(Arrays.copyOfRange(curves, end, end+type-2)) + "\n";
            }
            return ret;
        }
D
duke 已提交
1230 1231
    }
}