1 /* 2 * Copyright (c) 2007, 2018, 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 30 /** 31 * The <code>DDasher</code> class takes a series of linear commands 32 * (<code>moveTo</code>, <code>lineTo</code>, <code>close</code> and 33 * <code>end</code>) and breaks them into smaller segments according to a 34 * dash pattern array and a starting dash phase. 35 * 36 * <p> Issues: in J2Se, a zero length dash segment as drawn as a very 37 * short dash, whereas Pisces does not draw anything. The PostScript 38 * semantics are unclear. 39 * 40 */ 41 public final class DDasher implements DPathConsumer2D, MarlinConst { 42 43 static final int REC_LIMIT = 4; 44 static final double ERR = 0.01d; 45 static final double MIN_T_INC = 1.0d / (1 << REC_LIMIT); 46 47 // More than 24 bits of mantissa means we can no longer accurately 48 // measure the number of times cycled through the dash array so we 49 // punt and override the phase to just be 0 past that point. 50 static final double MAX_CYCLES = 16000000.0d; 51 52 private DPathConsumer2D out; 53 private double[] dash; 54 private int dashLen; 55 private double startPhase; 56 private boolean startDashOn; 57 private int startIdx; 58 59 private boolean starting; 60 private boolean needsMoveTo; 61 62 private int idx; 63 private boolean dashOn; 64 private double phase; 65 66 private double sx, sy; 67 private double x0, y0; 68 69 // temporary storage for the current curve 70 private final double[] curCurvepts; 71 72 // per-thread renderer context 73 final DRendererContext rdrCtx; 74 75 // flag to recycle dash array copy 76 boolean recycleDashes; 77 78 // dashes ref (dirty) 79 final DoubleArrayCache.Reference dashes_ref; 80 // firstSegmentsBuffer ref (dirty) 81 final DoubleArrayCache.Reference firstSegmentsBuffer_ref; 82 83 /** 84 * Constructs a <code>DDasher</code>. 85 * @param rdrCtx per-thread renderer context 86 */ 87 DDasher(final DRendererContext rdrCtx) { 88 this.rdrCtx = rdrCtx; 89 90 dashes_ref = rdrCtx.newDirtyDoubleArrayRef(INITIAL_ARRAY); // 1K 91 92 firstSegmentsBuffer_ref = rdrCtx.newDirtyDoubleArrayRef(INITIAL_ARRAY); // 1K 93 firstSegmentsBuffer = firstSegmentsBuffer_ref.initial; 94 95 // we need curCurvepts to be able to contain 2 curves because when 96 // dashing curves, we need to subdivide it 97 curCurvepts = new double[8 * 2]; 98 } 99 100 /** 101 * Initialize the <code>DDasher</code>. 102 * 103 * @param out an output <code>DPathConsumer2D</code>. 104 * @param dash an array of <code>double</code>s containing the dash pattern 105 * @param dashLen length of the given dash array 106 * @param phase a <code>double</code> containing the dash phase 107 * @param recycleDashes true to indicate to recycle the given dash array 108 * @return this instance 109 */ 110 public DDasher init(final DPathConsumer2D out, final double[] dash, final int dashLen, 111 double phase, final boolean recycleDashes) 112 { 113 this.out = out; 114 115 // Normalize so 0 <= phase < dash[0] 116 int sidx = 0; 117 dashOn = true; 118 119 // note: BasicStroke constructor checks dash elements and sum > 0 120 double sum = 0.0d; 121 for (int i = 0; i < dashLen; i++) { 122 sum += dash[i]; 123 } 124 double cycles = phase / sum; 125 if (phase < 0.0d) { 126 if (-cycles >= MAX_CYCLES) { 127 phase = 0.0d; 128 } else { 129 int fullcycles = FloatMath.floor_int(-cycles); 130 if ((fullcycles & dashLen & 1) != 0) { 131 dashOn = !dashOn; 132 } 133 phase += fullcycles * sum; 134 while (phase < 0.0d) { 135 if (--sidx < 0) { 136 sidx = dashLen - 1; 137 } 138 phase += dash[sidx]; 139 dashOn = !dashOn; 140 } 141 } 142 } else if (phase > 0.0d) { 143 if (cycles >= MAX_CYCLES) { 144 phase = 0.0d; 145 } else { 146 int fullcycles = FloatMath.floor_int(cycles); 147 if ((fullcycles & dashLen & 1) != 0) { 148 dashOn = !dashOn; 149 } 150 phase -= fullcycles * sum; 151 double d; 152 while (phase >= (d = dash[sidx])) { 153 phase -= d; 154 sidx = (sidx + 1) % dashLen; 155 dashOn = !dashOn; 156 } 157 } 158 } 159 160 this.dash = dash; 161 this.dashLen = dashLen; 162 this.phase = phase; 163 this.startPhase = phase; 164 this.startDashOn = dashOn; 165 this.startIdx = sidx; 166 this.starting = true; 167 this.needsMoveTo = false; 168 this.firstSegidx = 0; 169 170 this.recycleDashes = recycleDashes; 171 172 return this; // fluent API 173 } 174 175 /** 176 * Disposes this dasher: 177 * clean up before reusing this instance 178 */ 179 void dispose() { 180 if (DO_CLEAN_DIRTY) { 181 // Force zero-fill dirty arrays: 182 Arrays.fill(curCurvepts, 0.0d); 183 } 184 // Return arrays: 185 if (recycleDashes) { 186 dash = dashes_ref.putArray(dash); 187 } 188 firstSegmentsBuffer = firstSegmentsBuffer_ref.putArray(firstSegmentsBuffer); 189 } 190 191 public double[] copyDashArray(final float[] dashes) { 192 final int len = dashes.length; 193 final double[] newDashes; 194 if (len <= MarlinConst.INITIAL_ARRAY) { 195 newDashes = dashes_ref.initial; 196 } else { 197 if (DO_STATS) { 198 rdrCtx.stats.stat_array_dasher_dasher.add(len); 199 } 200 newDashes = dashes_ref.getArray(len); 201 } 202 for (int i = 0; i < len; i++) { newDashes[i] = dashes[i]; } 203 return newDashes; 204 } 205 206 @Override 207 public void moveTo(final double x0, final double y0) { 208 if (firstSegidx != 0) { 209 out.moveTo(sx, sy); 210 emitFirstSegments(); 211 } 212 needsMoveTo = true; 213 this.idx = startIdx; 214 this.dashOn = this.startDashOn; 215 this.phase = this.startPhase; 216 this.sx = x0; 217 this.sy = y0; 218 this.x0 = x0; 219 this.y0 = y0; 220 this.starting = true; 221 } 222 223 private void emitSeg(double[] buf, int off, int type) { 224 switch (type) { 225 case 8: 226 out.curveTo(buf[off+0], buf[off+1], 227 buf[off+2], buf[off+3], 228 buf[off+4], buf[off+5]); 229 return; 230 case 6: 231 out.quadTo(buf[off+0], buf[off+1], 232 buf[off+2], buf[off+3]); 233 return; 234 case 4: 235 out.lineTo(buf[off], buf[off+1]); 236 return; 237 default: 238 } 239 } 240 241 private void emitFirstSegments() { 242 final double[] fSegBuf = firstSegmentsBuffer; 243 244 for (int i = 0, len = firstSegidx; i < len; ) { 245 int type = (int)fSegBuf[i]; 246 emitSeg(fSegBuf, i + 1, type); 247 i += (type - 1); 248 } 249 firstSegidx = 0; 250 } 251 // We don't emit the first dash right away. If we did, caps would be 252 // drawn on it, but we need joins to be drawn if there's a closePath() 253 // So, we store the path elements that make up the first dash in the 254 // buffer below. 255 private double[] firstSegmentsBuffer; // dynamic array 256 private int firstSegidx; 257 258 // precondition: pts must be in relative coordinates (relative to x0,y0) 259 private void goTo(final double[] pts, final int off, final int type, 260 final boolean on) 261 { 262 final int index = off + type; 263 final double x = pts[index - 4]; 264 final double y = pts[index - 3]; 265 266 if (on) { 267 if (starting) { 268 goTo_starting(pts, off, type); 269 } else { 270 if (needsMoveTo) { 271 needsMoveTo = false; 272 out.moveTo(x0, y0); 273 } 274 emitSeg(pts, off, type); 275 } 276 } else { 277 if (starting) { 278 // low probability test (hotspot) 279 starting = false; 280 } 281 needsMoveTo = true; 282 } 283 this.x0 = x; 284 this.y0 = y; 285 } 286 287 private void goTo_starting(final double[] pts, final int off, final int type) { 288 int len = type - 1; // - 2 + 1 289 int segIdx = firstSegidx; 290 double[] buf = firstSegmentsBuffer; 291 292 if (segIdx + len > buf.length) { 293 if (DO_STATS) { 294 rdrCtx.stats.stat_array_dasher_firstSegmentsBuffer 295 .add(segIdx + len); 296 } 297 firstSegmentsBuffer = buf 298 = firstSegmentsBuffer_ref.widenArray(buf, segIdx, 299 segIdx + len); 300 } 301 buf[segIdx++] = type; 302 len--; 303 // small arraycopy (2, 4 or 6) but with offset: 304 System.arraycopy(pts, off, buf, segIdx, len); 305 firstSegidx = segIdx + len; 306 } 307 308 @Override 309 public void lineTo(final double x1, final double y1) { 310 final double dx = x1 - x0; 311 final double dy = y1 - y0; 312 313 double len = dx*dx + dy*dy; 314 if (len == 0.0d) { 315 return; 316 } 317 len = Math.sqrt(len); 318 319 // The scaling factors needed to get the dx and dy of the 320 // transformed dash segments. 321 final double cx = dx / len; 322 final double cy = dy / len; 323 324 final double[] _curCurvepts = curCurvepts; 325 final double[] _dash = dash; 326 final int _dashLen = this.dashLen; 327 328 int _idx = idx; 329 boolean _dashOn = dashOn; 330 double _phase = phase; 331 332 double leftInThisDashSegment; 333 double d, dashdx, dashdy, p; 334 335 while (true) { 336 d = _dash[_idx]; 337 leftInThisDashSegment = d - _phase; 338 339 if (len <= leftInThisDashSegment) { 340 _curCurvepts[0] = x1; 341 _curCurvepts[1] = y1; 342 343 goTo(_curCurvepts, 0, 4, _dashOn); 344 345 // Advance phase within current dash segment 346 _phase += len; 347 348 // TODO: compare double values using epsilon: 349 if (len == leftInThisDashSegment) { 350 _phase = 0.0d; 351 _idx = (_idx + 1) % _dashLen; 352 _dashOn = !_dashOn; 353 } 354 355 // Save local state: 356 idx = _idx; 357 dashOn = _dashOn; 358 phase = _phase; 359 return; 360 } 361 362 dashdx = d * cx; 363 dashdy = d * cy; 364 365 if (_phase == 0.0d) { 366 _curCurvepts[0] = x0 + dashdx; 367 _curCurvepts[1] = y0 + dashdy; 368 } else { 369 p = leftInThisDashSegment / d; 370 _curCurvepts[0] = x0 + p * dashdx; 371 _curCurvepts[1] = y0 + p * dashdy; 372 } 373 374 goTo(_curCurvepts, 0, 4, _dashOn); 375 376 len -= leftInThisDashSegment; 377 // Advance to next dash segment 378 _idx = (_idx + 1) % _dashLen; 379 _dashOn = !_dashOn; 380 _phase = 0.0d; 381 } 382 } 383 384 // shared instance in DDasher 385 private final LengthIterator li = new LengthIterator(); 386 387 // preconditions: curCurvepts must be an array of length at least 2 * type, 388 // that contains the curve we want to dash in the first type elements 389 private void somethingTo(final int type) { 390 if (pointCurve(curCurvepts, type)) { 391 return; 392 } 393 final LengthIterator _li = li; 394 final double[] _curCurvepts = curCurvepts; 395 final double[] _dash = dash; 396 final int _dashLen = this.dashLen; 397 398 _li.initializeIterationOnCurve(_curCurvepts, type); 399 400 int _idx = idx; 401 boolean _dashOn = dashOn; 402 double _phase = phase; 403 404 // initially the current curve is at curCurvepts[0...type] 405 int curCurveoff = 0; 406 double lastSplitT = 0.0d; 407 double t; 408 double leftInThisDashSegment = _dash[_idx] - _phase; 409 410 while ((t = _li.next(leftInThisDashSegment)) < 1.0d) { 411 if (t != 0.0d) { 412 DHelpers.subdivideAt((t - lastSplitT) / (1.0d - lastSplitT), 413 _curCurvepts, curCurveoff, 414 _curCurvepts, 0, 415 _curCurvepts, type, type); 416 lastSplitT = t; 417 goTo(_curCurvepts, 2, type, _dashOn); 418 curCurveoff = type; 419 } 420 // Advance to next dash segment 421 _idx = (_idx + 1) % _dashLen; 422 _dashOn = !_dashOn; 423 _phase = 0.0d; 424 leftInThisDashSegment = _dash[_idx]; 425 } 426 427 goTo(_curCurvepts, curCurveoff + 2, type, _dashOn); 428 429 _phase += _li.lastSegLen(); 430 if (_phase >= _dash[_idx]) { 431 _phase = 0.0d; 432 _idx = (_idx + 1) % _dashLen; 433 _dashOn = !_dashOn; 434 } 435 // Save local state: 436 idx = _idx; 437 dashOn = _dashOn; 438 phase = _phase; 439 440 // reset LengthIterator: 441 _li.reset(); 442 } 443 444 private static boolean pointCurve(double[] curve, int type) { 445 for (int i = 2; i < type; i++) { 446 if (curve[i] != curve[i-2]) { 447 return false; 448 } 449 } 450 return true; 451 } 452 453 // Objects of this class are used to iterate through curves. They return 454 // t values where the left side of the curve has a specified length. 455 // It does this by subdividing the input curve until a certain error 456 // condition has been met. A recursive subdivision procedure would 457 // return as many as 1<<limit curves, but this is an iterator and we 458 // don't need all the curves all at once, so what we carry out a 459 // lazy inorder traversal of the recursion tree (meaning we only move 460 // through the tree when we need the next subdivided curve). This saves 461 // us a lot of memory because at any one time we only need to store 462 // limit+1 curves - one for each level of the tree + 1. 463 // NOTE: the way we do things here is not enough to traverse a general 464 // tree; however, the trees we are interested in have the property that 465 // every non leaf node has exactly 2 children 466 static final class LengthIterator { 467 private enum Side {LEFT, RIGHT} 468 // Holds the curves at various levels of the recursion. The root 469 // (i.e. the original curve) is at recCurveStack[0] (but then it 470 // gets subdivided, the left half is put at 1, so most of the time 471 // only the right half of the original curve is at 0) 472 private final double[][] recCurveStack; // dirty 473 // sides[i] indicates whether the node at level i+1 in the path from 474 // the root to the current leaf is a left or right child of its parent. 475 private final Side[] sides; // dirty 476 private int curveType; 477 // lastT and nextT delimit the current leaf. 478 private double nextT; 479 private double lenAtNextT; 480 private double lastT; 481 private double lenAtLastT; 482 private double lenAtLastSplit; 483 private double lastSegLen; 484 // the current level in the recursion tree. 0 is the root. limit 485 // is the deepest possible leaf. 486 private int recLevel; 487 private boolean done; 488 489 // the lengths of the lines of the control polygon. Only its first 490 // curveType/2 - 1 elements are valid. This is an optimization. See 491 // next() for more detail. 492 private final double[] curLeafCtrlPolyLengths = new double[3]; 493 494 LengthIterator() { 495 this.recCurveStack = new double[REC_LIMIT + 1][8]; 496 this.sides = new Side[REC_LIMIT]; 497 // if any methods are called without first initializing this object 498 // on a curve, we want it to fail ASAP. 499 this.nextT = Double.MAX_VALUE; 500 this.lenAtNextT = Double.MAX_VALUE; 501 this.lenAtLastSplit = Double.MIN_VALUE; 502 this.recLevel = Integer.MIN_VALUE; 503 this.lastSegLen = Double.MAX_VALUE; 504 this.done = true; 505 } 506 507 /** 508 * Reset this LengthIterator. 509 */ 510 void reset() { 511 // keep data dirty 512 // as it appears not useful to reset data: 513 if (DO_CLEAN_DIRTY) { 514 final int recLimit = recCurveStack.length - 1; 515 for (int i = recLimit; i >= 0; i--) { 516 Arrays.fill(recCurveStack[i], 0.0d); 517 } 518 Arrays.fill(sides, Side.LEFT); 519 Arrays.fill(curLeafCtrlPolyLengths, 0.0d); 520 Arrays.fill(nextRoots, 0.0d); 521 Arrays.fill(flatLeafCoefCache, 0.0d); 522 flatLeafCoefCache[2] = -1.0d; 523 } 524 } 525 526 void initializeIterationOnCurve(double[] pts, int type) { 527 // optimize arraycopy (8 values faster than 6 = type): 528 System.arraycopy(pts, 0, recCurveStack[0], 0, 8); 529 this.curveType = type; 530 this.recLevel = 0; 531 this.lastT = 0.0d; 532 this.lenAtLastT = 0.0d; 533 this.nextT = 0.0d; 534 this.lenAtNextT = 0.0d; 535 goLeft(); // initializes nextT and lenAtNextT properly 536 this.lenAtLastSplit = 0.0d; 537 if (recLevel > 0) { 538 this.sides[0] = Side.LEFT; 539 this.done = false; 540 } else { 541 // the root of the tree is a leaf so we're done. 542 this.sides[0] = Side.RIGHT; 543 this.done = true; 544 } 545 this.lastSegLen = 0.0d; 546 } 547 548 // 0 == false, 1 == true, -1 == invalid cached value. 549 private int cachedHaveLowAcceleration = -1; 550 551 private boolean haveLowAcceleration(double err) { 552 if (cachedHaveLowAcceleration == -1) { 553 final double len1 = curLeafCtrlPolyLengths[0]; 554 final double len2 = curLeafCtrlPolyLengths[1]; 555 // the test below is equivalent to !within(len1/len2, 1, err). 556 // It is using a multiplication instead of a division, so it 557 // should be a bit faster. 558 if (!DHelpers.within(len1, len2, err * len2)) { 559 cachedHaveLowAcceleration = 0; 560 return false; 561 } 562 if (curveType == 8) { 563 final double len3 = curLeafCtrlPolyLengths[2]; 564 // if len1 is close to 2 and 2 is close to 3, that probably 565 // means 1 is close to 3 so the second part of this test might 566 // not be needed, but it doesn't hurt to include it. 567 final double errLen3 = err * len3; 568 if (!(DHelpers.within(len2, len3, errLen3) && 569 DHelpers.within(len1, len3, errLen3))) { 570 cachedHaveLowAcceleration = 0; 571 return false; 572 } 573 } 574 cachedHaveLowAcceleration = 1; 575 return true; 576 } 577 578 return (cachedHaveLowAcceleration == 1); 579 } 580 581 // we want to avoid allocations/gc so we keep this array so we 582 // can put roots in it, 583 private final double[] nextRoots = new double[4]; 584 585 // caches the coefficients of the current leaf in its flattened 586 // form (see inside next() for what that means). The cache is 587 // invalid when it's third element is negative, since in any 588 // valid flattened curve, this would be >= 0. 589 private final double[] flatLeafCoefCache = new double[]{0.0d, 0.0d, -1.0d, 0.0d}; 590 591 // returns the t value where the remaining curve should be split in 592 // order for the left subdivided curve to have length len. If len 593 // is >= than the length of the uniterated curve, it returns 1. 594 double next(final double len) { 595 final double targetLength = lenAtLastSplit + len; 596 while (lenAtNextT < targetLength) { 597 if (done) { 598 lastSegLen = lenAtNextT - lenAtLastSplit; 599 return 1.0d; 600 } 601 goToNextLeaf(); 602 } 603 lenAtLastSplit = targetLength; 604 final double leaflen = lenAtNextT - lenAtLastT; 605 double t = (targetLength - lenAtLastT) / leaflen; 606 607 // cubicRootsInAB is a fairly expensive call, so we just don't do it 608 // if the acceleration in this section of the curve is small enough. 609 if (!haveLowAcceleration(0.05d)) { 610 // We flatten the current leaf along the x axis, so that we're 611 // left with a, b, c which define a 1D Bezier curve. We then 612 // solve this to get the parameter of the original leaf that 613 // gives us the desired length. 614 final double[] _flatLeafCoefCache = flatLeafCoefCache; 615 616 if (_flatLeafCoefCache[2] < 0.0d) { 617 double x = curLeafCtrlPolyLengths[0], 618 y = x + curLeafCtrlPolyLengths[1]; 619 if (curveType == 8) { 620 double z = y + curLeafCtrlPolyLengths[2]; 621 _flatLeafCoefCache[0] = 3.0d * (x - y) + z; 622 _flatLeafCoefCache[1] = 3.0d * (y - 2.0d * x); 623 _flatLeafCoefCache[2] = 3.0d * x; 624 _flatLeafCoefCache[3] = -z; 625 } else if (curveType == 6) { 626 _flatLeafCoefCache[0] = 0.0d; 627 _flatLeafCoefCache[1] = y - 2.0d * x; 628 _flatLeafCoefCache[2] = 2.0d * x; 629 _flatLeafCoefCache[3] = -y; 630 } 631 } 632 double a = _flatLeafCoefCache[0]; 633 double b = _flatLeafCoefCache[1]; 634 double c = _flatLeafCoefCache[2]; 635 double d = t * _flatLeafCoefCache[3]; 636 637 // we use cubicRootsInAB here, because we want only roots in 0, 1, 638 // and our quadratic root finder doesn't filter, so it's just a 639 // matter of convenience. 640 int n = DHelpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0.0d, 1.0d); 641 if (n == 1 && !Double.isNaN(nextRoots[0])) { 642 t = nextRoots[0]; 643 } 644 } 645 // t is relative to the current leaf, so we must make it a valid parameter 646 // of the original curve. 647 t = t * (nextT - lastT) + lastT; 648 if (t >= 1.0d) { 649 t = 1.0d; 650 done = true; 651 } 652 // even if done = true, if we're here, that means targetLength 653 // is equal to, or very, very close to the total length of the 654 // curve, so lastSegLen won't be too high. In cases where len 655 // overshoots the curve, this method will exit in the while 656 // loop, and lastSegLen will still be set to the right value. 657 lastSegLen = len; 658 return t; 659 } 660 661 double lastSegLen() { 662 return lastSegLen; 663 } 664 665 // go to the next leaf (in an inorder traversal) in the recursion tree 666 // preconditions: must be on a leaf, and that leaf must not be the root. 667 private void goToNextLeaf() { 668 // We must go to the first ancestor node that has an unvisited 669 // right child. 670 int _recLevel = recLevel; 671 final Side[] _sides = sides; 672 673 _recLevel--; 674 while(_sides[_recLevel] == Side.RIGHT) { 675 if (_recLevel == 0) { 676 recLevel = 0; 677 done = true; 678 return; 679 } 680 _recLevel--; 681 } 682 683 _sides[_recLevel] = Side.RIGHT; 684 // optimize arraycopy (8 values faster than 6 = type): 685 System.arraycopy(recCurveStack[_recLevel], 0, 686 recCurveStack[_recLevel+1], 0, 8); 687 _recLevel++; 688 689 recLevel = _recLevel; 690 goLeft(); 691 } 692 693 // go to the leftmost node from the current node. Return its length. 694 private void goLeft() { 695 double len = onLeaf(); 696 if (len >= 0.0d) { 697 lastT = nextT; 698 lenAtLastT = lenAtNextT; 699 nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC; 700 lenAtNextT += len; 701 // invalidate caches 702 flatLeafCoefCache[2] = -1.0d; 703 cachedHaveLowAcceleration = -1; 704 } else { 705 DHelpers.subdivide(recCurveStack[recLevel], 0, 706 recCurveStack[recLevel+1], 0, 707 recCurveStack[recLevel], 0, curveType); 708 sides[recLevel] = Side.LEFT; 709 recLevel++; 710 goLeft(); 711 } 712 } 713 714 // this is a bit of a hack. It returns -1 if we're not on a leaf, and 715 // the length of the leaf if we are on a leaf. 716 private double onLeaf() { 717 final double[] curve = recCurveStack[recLevel]; 718 final int _curveType = curveType; 719 double polyLen = 0.0d; 720 721 double x0 = curve[0], y0 = curve[1]; 722 for (int i = 2; i < _curveType; i += 2) { 723 final double x1 = curve[i], y1 = curve[i+1]; 724 final double len = DHelpers.linelen(x0, y0, x1, y1); 725 polyLen += len; 726 curLeafCtrlPolyLengths[(i >> 1) - 1] = len; 727 x0 = x1; 728 y0 = y1; 729 } 730 731 final double lineLen = DHelpers.linelen(curve[0], curve[1], 732 curve[_curveType-2], 733 curve[_curveType-1]); 734 if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) { 735 return (polyLen + lineLen) / 2.0d; 736 } 737 return -1.0d; 738 } 739 } 740 741 @Override 742 public void curveTo(final double x1, final double y1, 743 final double x2, final double y2, 744 final double x3, final double y3) 745 { 746 final double[] _curCurvepts = curCurvepts; 747 _curCurvepts[0] = x0; _curCurvepts[1] = y0; 748 _curCurvepts[2] = x1; _curCurvepts[3] = y1; 749 _curCurvepts[4] = x2; _curCurvepts[5] = y2; 750 _curCurvepts[6] = x3; _curCurvepts[7] = y3; 751 somethingTo(8); 752 } 753 754 @Override 755 public void quadTo(final double x1, final double y1, 756 final double x2, final double y2) 757 { 758 final double[] _curCurvepts = curCurvepts; 759 _curCurvepts[0] = x0; _curCurvepts[1] = y0; 760 _curCurvepts[2] = x1; _curCurvepts[3] = y1; 761 _curCurvepts[4] = x2; _curCurvepts[5] = y2; 762 somethingTo(6); 763 } 764 765 @Override 766 public void closePath() { 767 lineTo(sx, sy); 768 if (firstSegidx != 0) { 769 if (!dashOn || needsMoveTo) { 770 out.moveTo(sx, sy); 771 } 772 emitFirstSegments(); 773 } 774 moveTo(sx, sy); 775 } 776 777 @Override 778 public void pathDone() { 779 if (firstSegidx != 0) { 780 out.moveTo(sx, sy); 781 emitFirstSegments(); 782 } 783 out.pathDone(); 784 785 // Dispose this instance: 786 dispose(); 787 } 788 } 789