10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.marlin; 27 28 import java.util.Arrays; 29 import com.sun.javafx.geom.PathConsumer2D; 30 31 /** 32 * The <code>Dasher</code> class takes a series of linear commands 33 * (<code>moveTo</code>, <code>lineTo</code>, <code>close</code> and 34 * <code>end</code>) and breaks them into smaller segments according to a 35 * dash pattern array and a starting dash phase. 36 * 37 * <p> Issues: in J2Se, a zero length dash segment as drawn as a very 38 * short dash, whereas Pisces does not draw anything. The PostScript 39 * semantics are unclear. 40 * 41 */ 42 public final class Dasher implements PathConsumer2D, MarlinConst { 43 44 static final int REC_LIMIT = 4; 45 static final float ERR = 0.01f; 46 static final float MIN_T_INC = 1.0f / (1 << REC_LIMIT); 47 48 // More than 24 bits of mantissa means we can no longer accurately 49 // measure the number of times cycled through the dash array so we 50 // punt and override the phase to just be 0 past that point. 51 static final float MAX_CYCLES = 16000000.0f; 52 53 private PathConsumer2D out; 54 private float[] dash; 55 private int dashLen; 56 private float startPhase; 57 private boolean startDashOn; 58 private int startIdx; 59 60 private boolean starting; 61 private boolean needsMoveTo; 62 63 private int idx; 64 private boolean dashOn; 65 private float phase; 66 67 private float sx, sy; 68 private float x0, y0; 69 70 // temporary storage for the current curve 71 private final float[] curCurvepts; 72 73 // per-thread renderer context 74 final RendererContext rdrCtx; 75 76 // flag to recycle dash array copy 77 boolean recycleDashes; 78 79 // dashes ref (dirty) 80 final FloatArrayCache.Reference dashes_ref; 81 // firstSegmentsBuffer ref (dirty) 82 final FloatArrayCache.Reference firstSegmentsBuffer_ref; 83 84 /** 85 * Constructs a <code>Dasher</code>. 86 * @param rdrCtx per-thread renderer context 87 */ 88 Dasher(final RendererContext rdrCtx) { 89 this.rdrCtx = rdrCtx; 90 91 dashes_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_ARRAY); // 1K 92 93 firstSegmentsBuffer_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_ARRAY); // 1K 94 firstSegmentsBuffer = firstSegmentsBuffer_ref.initial; 95 96 // we need curCurvepts to be able to contain 2 curves because when 97 // dashing curves, we need to subdivide it 98 curCurvepts = new float[8 * 2]; 99 } 100 101 /** 102 * Initialize the <code>Dasher</code>. 103 * 104 * @param out an output <code>PathConsumer2D</code>. 105 * @param dash an array of <code>float</code>s containing the dash pattern 106 * @param dashLen length of the given dash array 107 * @param phase a <code>float</code> containing the dash phase 108 * @param recycleDashes true to indicate to recycle the given dash array 109 * @return this instance 110 */ 111 public Dasher init(final PathConsumer2D out, final float[] dash, final int dashLen, 112 float phase, final boolean recycleDashes) 113 { 114 this.out = out; 115 116 // Normalize so 0 <= phase < dash[0] 117 int sidx = 0; 118 dashOn = true; 119 120 // note: BasicStroke constructor checks dash elements and sum > 0 121 float sum = 0.0f; 122 for (int i = 0; i < dashLen; i++) { 123 sum += dash[i]; 124 } 125 float cycles = phase / sum; 126 if (phase < 0.0f) { 127 if (-cycles >= MAX_CYCLES) { 128 phase = 0.0f; 129 } else { 130 int fullcycles = FloatMath.floor_int(-cycles); 131 if ((fullcycles & dashLen & 1) != 0) { 132 dashOn = !dashOn; 133 } 134 phase += fullcycles * sum; 135 while (phase < 0.0f) { 136 if (--sidx < 0) { 137 sidx = dashLen - 1; 138 } 139 phase += dash[sidx]; 140 dashOn = !dashOn; 141 } 142 } 143 } else if (phase > 0.0f) { 144 if (cycles >= MAX_CYCLES) { 153 while (phase >= (d = dash[sidx])) { 154 phase -= d; 155 sidx = (sidx + 1) % dashLen; 156 dashOn = !dashOn; 157 } 158 } 159 } 160 161 this.dash = dash; 162 this.dashLen = dashLen; 163 this.phase = phase; 164 this.startPhase = phase; 165 this.startDashOn = dashOn; 166 this.startIdx = sidx; 167 this.starting = true; 168 this.needsMoveTo = false; 169 this.firstSegidx = 0; 170 171 this.recycleDashes = recycleDashes; 172 173 return this; // fluent API 174 } 175 176 /** 177 * Disposes this dasher: 178 * clean up before reusing this instance 179 */ 180 void dispose() { 181 if (DO_CLEAN_DIRTY) { 182 // Force zero-fill dirty arrays: 183 Arrays.fill(curCurvepts, 0.0f); 184 } 185 // Return arrays: 186 if (recycleDashes) { 187 dash = dashes_ref.putArray(dash); 188 } 189 firstSegmentsBuffer = firstSegmentsBuffer_ref.putArray(firstSegmentsBuffer); 190 } 191 192 public float[] copyDashArray(final float[] dashes) { 193 final int len = dashes.length; 194 final float[] newDashes; 195 if (len <= MarlinConst.INITIAL_ARRAY) { 196 newDashes = dashes_ref.initial; 197 } else { 198 if (DO_STATS) { 199 rdrCtx.stats.stat_array_dasher_dasher.add(len); 200 } 201 newDashes = dashes_ref.getArray(len); 202 } 203 System.arraycopy(dashes, 0, newDashes, 0, len); 204 return newDashes; 205 } 206 207 @Override 208 public void moveTo(final float x0, final float y0) { 209 if (firstSegidx != 0) { 210 out.moveTo(sx, sy); 211 emitFirstSegments(); 212 } 213 needsMoveTo = true; 214 this.idx = startIdx; 215 this.dashOn = this.startDashOn; 216 this.phase = this.startPhase; 217 this.sx = x0; 218 this.sy = y0; 219 this.x0 = x0; 220 this.y0 = y0; 221 this.starting = true; 222 } 223 224 private void emitSeg(float[] buf, int off, int type) { 225 switch (type) { 226 case 8: 227 out.curveTo(buf[off+0], buf[off+1], 228 buf[off+2], buf[off+3], 229 buf[off+4], buf[off+5]); 230 return; 231 case 6: 232 out.quadTo(buf[off+0], buf[off+1], 233 buf[off+2], buf[off+3]); 234 return; 235 case 4: 236 out.lineTo(buf[off], buf[off+1]); 237 return; 238 default: 239 } 240 } 241 242 private void emitFirstSegments() { 243 final float[] fSegBuf = firstSegmentsBuffer; 244 245 for (int i = 0, len = firstSegidx; i < len; ) { 246 int type = (int)fSegBuf[i]; 247 emitSeg(fSegBuf, i + 1, type); 248 i += (type - 1); 249 } 250 firstSegidx = 0; 251 } 252 // We don't emit the first dash right away. If we did, caps would be 253 // drawn on it, but we need joins to be drawn if there's a closePath() 254 // So, we store the path elements that make up the first dash in the 255 // buffer below. 256 private float[] firstSegmentsBuffer; // dynamic array 257 private int firstSegidx; 258 259 // precondition: pts must be in relative coordinates (relative to x0,y0) 260 private void goTo(final float[] pts, final int off, final int type, 261 final boolean on) 262 { 263 final int index = off + type; 264 final float x = pts[index - 4]; 265 final float y = pts[index - 3]; 266 267 if (on) { 268 if (starting) { 269 goTo_starting(pts, off, type); 270 } else { 271 if (needsMoveTo) { 272 needsMoveTo = false; 273 out.moveTo(x0, y0); 274 } 275 emitSeg(pts, off, type); 276 } 277 } else { 278 if (starting) { 279 // low probability test (hotspot) 280 starting = false; 281 } 282 needsMoveTo = true; 283 } 284 this.x0 = x; 285 this.y0 = y; 286 } 287 288 private void goTo_starting(final float[] pts, final int off, final int type) { 289 int len = type - 1; // - 2 + 1 290 int segIdx = firstSegidx; 291 float[] buf = firstSegmentsBuffer; 292 293 if (segIdx + len > buf.length) { 294 if (DO_STATS) { 295 rdrCtx.stats.stat_array_dasher_firstSegmentsBuffer 296 .add(segIdx + len); 297 } 298 firstSegmentsBuffer = buf 299 = firstSegmentsBuffer_ref.widenArray(buf, segIdx, 300 segIdx + len); 301 } 302 buf[segIdx++] = type; 303 len--; 304 // small arraycopy (2, 4 or 6) but with offset: 305 System.arraycopy(pts, off, buf, segIdx, len); 306 firstSegidx = segIdx + len; 307 } 308 309 @Override 310 public void lineTo(final float x1, final float y1) { 311 final float dx = x1 - x0; 312 final float dy = y1 - y0; 313 314 float len = dx*dx + dy*dy; 315 if (len == 0.0f) { 316 return; 317 } 318 len = (float) Math.sqrt(len); 319 320 // The scaling factors needed to get the dx and dy of the 321 // transformed dash segments. 322 final float cx = dx / len; 323 final float cy = dy / len; 324 325 final float[] _curCurvepts = curCurvepts; 326 final float[] _dash = dash; 327 final int _dashLen = this.dashLen; 328 329 int _idx = idx; 330 boolean _dashOn = dashOn; 331 float _phase = phase; 332 333 float leftInThisDashSegment; 334 float d, dashdx, dashdy, p; 335 336 while (true) { 337 d = _dash[_idx]; 338 leftInThisDashSegment = d - _phase; 339 340 if (len <= leftInThisDashSegment) { 341 _curCurvepts[0] = x1; 342 _curCurvepts[1] = y1; 343 344 goTo(_curCurvepts, 0, 4, _dashOn); 345 346 // Advance phase within current dash segment 347 _phase += len; 348 349 // TODO: compare float values using epsilon: 350 if (len == leftInThisDashSegment) { 351 _phase = 0.0f; 352 _idx = (_idx + 1) % _dashLen; 353 _dashOn = !_dashOn; 354 } 355 356 // Save local state: 357 idx = _idx; 358 dashOn = _dashOn; 359 phase = _phase; 360 return; 361 } 362 363 dashdx = d * cx; 364 dashdy = d * cy; 365 366 if (_phase == 0.0f) { 367 _curCurvepts[0] = x0 + dashdx; 368 _curCurvepts[1] = y0 + dashdy; 369 } else { 370 p = leftInThisDashSegment / d; 371 _curCurvepts[0] = x0 + p * dashdx; 372 _curCurvepts[1] = y0 + p * dashdy; 373 } 374 375 goTo(_curCurvepts, 0, 4, _dashOn); 376 377 len -= leftInThisDashSegment; 378 // Advance to next dash segment 379 _idx = (_idx + 1) % _dashLen; 380 _dashOn = !_dashOn; 381 _phase = 0.0f; 382 } 383 } 384 385 // shared instance in Dasher 386 private final LengthIterator li = new LengthIterator(); 387 388 // preconditions: curCurvepts must be an array of length at least 2 * type, 389 // that contains the curve we want to dash in the first type elements 390 private void somethingTo(final int type) { 391 if (pointCurve(curCurvepts, type)) { 392 return; 393 } 394 final LengthIterator _li = li; 395 final float[] _curCurvepts = curCurvepts; 396 final float[] _dash = dash; 397 final int _dashLen = this.dashLen; 398 399 _li.initializeIterationOnCurve(_curCurvepts, type); 400 401 int _idx = idx; 402 boolean _dashOn = dashOn; 403 float _phase = phase; 404 405 // initially the current curve is at curCurvepts[0...type] 406 int curCurveoff = 0; 407 float lastSplitT = 0.0f; 408 float t; 409 float leftInThisDashSegment = _dash[_idx] - _phase; 410 411 while ((t = _li.next(leftInThisDashSegment)) < 1.0f) { 412 if (t != 0.0f) { 413 Helpers.subdivideAt((t - lastSplitT) / (1.0f - lastSplitT), 414 _curCurvepts, curCurveoff, 415 _curCurvepts, 0, 416 _curCurvepts, type, type); 417 lastSplitT = t; 418 goTo(_curCurvepts, 2, type, _dashOn); 419 curCurveoff = type; 420 } 421 // Advance to next dash segment 422 _idx = (_idx + 1) % _dashLen; 423 _dashOn = !_dashOn; 424 _phase = 0.0f; 425 leftInThisDashSegment = _dash[_idx]; 426 } 427 428 goTo(_curCurvepts, curCurveoff + 2, type, _dashOn); 429 430 _phase += _li.lastSegLen(); 431 if (_phase >= _dash[_idx]) { 432 _phase = 0.0f; 433 _idx = (_idx + 1) % _dashLen; 434 _dashOn = !_dashOn; 435 } 436 // Save local state: 437 idx = _idx; 438 dashOn = _dashOn; 439 phase = _phase; 440 441 // reset LengthIterator: 442 _li.reset(); 443 } 444 445 private static boolean pointCurve(float[] curve, int type) { 446 for (int i = 2; i < type; i++) { 447 if (curve[i] != curve[i-2]) { 448 return false; 449 } 450 } 451 return true; 452 } 453 454 // Objects of this class are used to iterate through curves. They return 455 // t values where the left side of the curve has a specified length. 456 // It does this by subdividing the input curve until a certain error 457 // condition has been met. A recursive subdivision procedure would 458 // return as many as 1<<limit curves, but this is an iterator and we 459 // don't need all the curves all at once, so what we carry out a 460 // lazy inorder traversal of the recursion tree (meaning we only move 461 // through the tree when we need the next subdivided curve). This saves 462 // us a lot of memory because at any one time we only need to store 463 // limit+1 curves - one for each level of the tree + 1. 464 // NOTE: the way we do things here is not enough to traverse a general 465 // tree; however, the trees we are interested in have the property that 466 // every non leaf node has exactly 2 children 467 static final class LengthIterator { 468 private enum Side {LEFT, RIGHT} 469 // Holds the curves at various levels of the recursion. The root 470 // (i.e. the original curve) is at recCurveStack[0] (but then it 471 // gets subdivided, the left half is put at 1, so most of the time 472 // only the right half of the original curve is at 0) 473 private final float[][] recCurveStack; // dirty 474 // sides[i] indicates whether the node at level i+1 in the path from 475 // the root to the current leaf is a left or right child of its parent. 476 private final Side[] sides; // dirty 477 private int curveType; 478 // lastT and nextT delimit the current leaf. 479 private float nextT; 480 private float lenAtNextT; 481 private float lastT; 482 private float lenAtLastT; 483 private float lenAtLastSplit; 484 private float lastSegLen; 485 // the current level in the recursion tree. 0 is the root. limit 486 // is the deepest possible leaf. 487 private int recLevel; 488 private boolean done; 489 490 // the lengths of the lines of the control polygon. Only its first 491 // curveType/2 - 1 elements are valid. This is an optimization. See 492 // next() for more detail. 493 private final float[] curLeafCtrlPolyLengths = new float[3]; 494 495 LengthIterator() { 496 this.recCurveStack = new float[REC_LIMIT + 1][8]; 497 this.sides = new Side[REC_LIMIT]; 498 // if any methods are called without first initializing this object 499 // on a curve, we want it to fail ASAP. 500 this.nextT = Float.MAX_VALUE; 501 this.lenAtNextT = Float.MAX_VALUE; 502 this.lenAtLastSplit = Float.MIN_VALUE; 503 this.recLevel = Integer.MIN_VALUE; 504 this.lastSegLen = Float.MAX_VALUE; 505 this.done = true; 506 } 507 508 /** 509 * Reset this LengthIterator. 510 */ 511 void reset() { 512 // keep data dirty 513 // as it appears not useful to reset data: 514 if (DO_CLEAN_DIRTY) { 515 final int recLimit = recCurveStack.length - 1; 516 for (int i = recLimit; i >= 0; i--) { 517 Arrays.fill(recCurveStack[i], 0.0f); 518 } 519 Arrays.fill(sides, Side.LEFT); 520 Arrays.fill(curLeafCtrlPolyLengths, 0.0f); 521 Arrays.fill(nextRoots, 0.0f); 522 Arrays.fill(flatLeafCoefCache, 0.0f); 523 flatLeafCoefCache[2] = -1.0f; 524 } 525 } 526 527 void initializeIterationOnCurve(float[] pts, int type) { 528 // optimize arraycopy (8 values faster than 6 = type): 529 System.arraycopy(pts, 0, recCurveStack[0], 0, 8); 530 this.curveType = type; 531 this.recLevel = 0; 532 this.lastT = 0.0f; 533 this.lenAtLastT = 0.0f; 534 this.nextT = 0.0f; 535 this.lenAtNextT = 0.0f; 536 goLeft(); // initializes nextT and lenAtNextT properly 537 this.lenAtLastSplit = 0.0f; 538 if (recLevel > 0) { 539 this.sides[0] = Side.LEFT; 540 this.done = false; 541 } else { 542 // the root of the tree is a leaf so we're done. 543 this.sides[0] = Side.RIGHT; 544 this.done = true; 545 } 546 this.lastSegLen = 0.0f; 547 } 548 549 // 0 == false, 1 == true, -1 == invalid cached value. 550 private int cachedHaveLowAcceleration = -1; 551 552 private boolean haveLowAcceleration(float err) { 553 if (cachedHaveLowAcceleration == -1) { 554 final float len1 = curLeafCtrlPolyLengths[0]; 555 final float len2 = curLeafCtrlPolyLengths[1]; 556 // the test below is equivalent to !within(len1/len2, 1, err). 557 // It is using a multiplication instead of a division, so it 558 // should be a bit faster. 559 if (!Helpers.within(len1, len2, err * len2)) { 560 cachedHaveLowAcceleration = 0; 561 return false; 562 } 563 if (curveType == 8) { 564 final float len3 = curLeafCtrlPolyLengths[2]; 565 // if len1 is close to 2 and 2 is close to 3, that probably 566 // means 1 is close to 3 so the second part of this test might 567 // not be needed, but it doesn't hurt to include it. 568 final float errLen3 = err * len3; 569 if (!(Helpers.within(len2, len3, errLen3) && 570 Helpers.within(len1, len3, errLen3))) { 571 cachedHaveLowAcceleration = 0; 572 return false; 621 float z = y + curLeafCtrlPolyLengths[2]; 622 _flatLeafCoefCache[0] = 3.0f * (x - y) + z; 623 _flatLeafCoefCache[1] = 3.0f * (y - 2.0f * x); 624 _flatLeafCoefCache[2] = 3.0f * x; 625 _flatLeafCoefCache[3] = -z; 626 } else if (curveType == 6) { 627 _flatLeafCoefCache[0] = 0.0f; 628 _flatLeafCoefCache[1] = y - 2.0f * x; 629 _flatLeafCoefCache[2] = 2.0f * x; 630 _flatLeafCoefCache[3] = -y; 631 } 632 } 633 float a = _flatLeafCoefCache[0]; 634 float b = _flatLeafCoefCache[1]; 635 float c = _flatLeafCoefCache[2]; 636 float d = t * _flatLeafCoefCache[3]; 637 638 // we use cubicRootsInAB here, because we want only roots in 0, 1, 639 // and our quadratic root finder doesn't filter, so it's just a 640 // matter of convenience. 641 int n = Helpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0.0f, 1.0f); 642 if (n == 1 && !Float.isNaN(nextRoots[0])) { 643 t = nextRoots[0]; 644 } 645 } 646 // t is relative to the current leaf, so we must make it a valid parameter 647 // of the original curve. 648 t = t * (nextT - lastT) + lastT; 649 if (t >= 1.0f) { 650 t = 1.0f; 651 done = true; 652 } 653 // even if done = true, if we're here, that means targetLength 654 // is equal to, or very, very close to the total length of the 655 // curve, so lastSegLen won't be too high. In cases where len 656 // overshoots the curve, this method will exit in the while 657 // loop, and lastSegLen will still be set to the right value. 658 lastSegLen = len; 659 return t; 660 } 661 662 float lastSegLen() { 663 return lastSegLen; 664 } 665 666 // go to the next leaf (in an inorder traversal) in the recursion tree 667 // preconditions: must be on a leaf, and that leaf must not be the root. 668 private void goToNextLeaf() { 669 // We must go to the first ancestor node that has an unvisited 670 // right child. 671 int _recLevel = recLevel; 672 final Side[] _sides = sides; 673 674 _recLevel--; 675 while(_sides[_recLevel] == Side.RIGHT) { 676 if (_recLevel == 0) { 677 recLevel = 0; 678 done = true; 679 return; 680 } 681 _recLevel--; 682 } 683 684 _sides[_recLevel] = Side.RIGHT; 685 // optimize arraycopy (8 values faster than 6 = type): 686 System.arraycopy(recCurveStack[_recLevel], 0, 687 recCurveStack[_recLevel+1], 0, 8); 688 _recLevel++; 689 690 recLevel = _recLevel; 691 goLeft(); 692 } 693 694 // go to the leftmost node from the current node. Return its length. 695 private void goLeft() { 696 float len = onLeaf(); 697 if (len >= 0.0f) { 698 lastT = nextT; 699 lenAtLastT = lenAtNextT; 700 nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC; 701 lenAtNextT += len; 702 // invalidate caches 703 flatLeafCoefCache[2] = -1.0f; 704 cachedHaveLowAcceleration = -1; 705 } else { 706 Helpers.subdivide(recCurveStack[recLevel], 0, 707 recCurveStack[recLevel+1], 0, 708 recCurveStack[recLevel], 0, curveType); 709 sides[recLevel] = Side.LEFT; 710 recLevel++; 711 goLeft(); 712 } 713 } 714 715 // this is a bit of a hack. It returns -1 if we're not on a leaf, and 716 // the length of the leaf if we are on a leaf. 717 private float onLeaf() { 718 final float[] curve = recCurveStack[recLevel]; 719 final int _curveType = curveType; 720 float polyLen = 0.0f; 721 722 float x0 = curve[0], y0 = curve[1]; 723 for (int i = 2; i < _curveType; i += 2) { 724 final float x1 = curve[i], y1 = curve[i+1]; 725 final float len = Helpers.linelen(x0, y0, x1, y1); 726 polyLen += len; 727 curLeafCtrlPolyLengths[(i >> 1) - 1] = len; 728 x0 = x1; 729 y0 = y1; 730 } 731 732 final float lineLen = Helpers.linelen(curve[0], curve[1], 733 curve[_curveType-2], 734 curve[_curveType-1]); 735 if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) { 736 return (polyLen + lineLen) / 2.0f; 737 } 738 return -1.0f; 739 } 740 } 741 742 @Override 743 public void curveTo(final float x1, final float y1, 744 final float x2, final float y2, 745 final float x3, final float y3) 746 { 747 final float[] _curCurvepts = curCurvepts; 748 _curCurvepts[0] = x0; _curCurvepts[1] = y0; 749 _curCurvepts[2] = x1; _curCurvepts[3] = y1; 750 _curCurvepts[4] = x2; _curCurvepts[5] = y2; 751 _curCurvepts[6] = x3; _curCurvepts[7] = y3; 752 somethingTo(8); 753 } 754 755 @Override 756 public void quadTo(final float x1, final float y1, 757 final float x2, final float y2) 758 { 759 final float[] _curCurvepts = curCurvepts; 760 _curCurvepts[0] = x0; _curCurvepts[1] = y0; 761 _curCurvepts[2] = x1; _curCurvepts[3] = y1; 762 _curCurvepts[4] = x2; _curCurvepts[5] = y2; 763 somethingTo(6); 764 } 765 766 @Override 767 public void closePath() { 768 lineTo(sx, sy); 769 if (firstSegidx != 0) { 770 if (!dashOn || needsMoveTo) { 771 out.moveTo(sx, sy); 772 } 773 emitFirstSegments(); 774 } 775 moveTo(sx, sy); 776 } 777 778 @Override 779 public void pathDone() { 780 if (firstSegidx != 0) { 781 out.moveTo(sx, sy); 782 emitFirstSegments(); 783 } 784 out.pathDone(); 785 786 // Dispose this instance: 787 dispose(); 788 } 789 } 790 | 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.marlin; 27 28 import java.util.Arrays; 29 import com.sun.javafx.geom.PathConsumer2D; 30 import com.sun.marlin.TransformingPathConsumer2D.CurveBasicMonotonizer; 31 import com.sun.marlin.TransformingPathConsumer2D.CurveClipSplitter; 32 33 /** 34 * The <code>Dasher</code> class takes a series of linear commands 35 * (<code>moveTo</code>, <code>lineTo</code>, <code>close</code> and 36 * <code>end</code>) and breaks them into smaller segments according to a 37 * dash pattern array and a starting dash phase. 38 * 39 * <p> Issues: in J2Se, a zero length dash segment as drawn as a very 40 * short dash, whereas Pisces does not draw anything. The PostScript 41 * semantics are unclear. 42 * 43 */ 44 public final class Dasher implements PathConsumer2D, MarlinConst { 45 46 /* huge circle with radius ~ 2E9 only needs 12 subdivision levels */ 47 static final int REC_LIMIT = 16; 48 static final float CURVE_LEN_ERR = MarlinProperties.getCurveLengthError(); // 0.01 49 static final float MIN_T_INC = 1.0f / (1 << REC_LIMIT); 50 51 // More than 24 bits of mantissa means we can no longer accurately 52 // measure the number of times cycled through the dash array so we 53 // punt and override the phase to just be 0 past that point. 54 static final float MAX_CYCLES = 16000000.0f; 55 56 private PathConsumer2D out; 57 private float[] dash; 58 private int dashLen; 59 private float startPhase; 60 private boolean startDashOn; 61 private int startIdx; 62 63 private boolean starting; 64 private boolean needsMoveTo; 65 66 private int idx; 67 private boolean dashOn; 68 private float phase; 69 70 // The starting point of the path 71 private float sx0, sy0; 72 // the current point 73 private float cx0, cy0; 74 75 // temporary storage for the current curve 76 private final float[] curCurvepts; 77 78 // per-thread renderer context 79 final RendererContext rdrCtx; 80 81 // flag to recycle dash array copy 82 boolean recycleDashes; 83 84 // We don't emit the first dash right away. If we did, caps would be 85 // drawn on it, but we need joins to be drawn if there's a closePath() 86 // So, we store the path elements that make up the first dash in the 87 // buffer below. 88 private float[] firstSegmentsBuffer; // dynamic array 89 private int firstSegidx; 90 91 // dashes ref (dirty) 92 final FloatArrayCache.Reference dashes_ref; 93 // firstSegmentsBuffer ref (dirty) 94 final FloatArrayCache.Reference firstSegmentsBuffer_ref; 95 96 // Bounds of the drawing region, at pixel precision. 97 private float[] clipRect; 98 99 // the outcode of the current point 100 private int cOutCode = 0; 101 102 private boolean subdivide = DO_CLIP_SUBDIVIDER; 103 104 private final LengthIterator li = new LengthIterator(); 105 106 private final CurveClipSplitter curveSplitter; 107 108 private float cycleLen; 109 private boolean outside; 110 private float totalSkipLen; 111 112 /** 113 * Constructs a <code>Dasher</code>. 114 * @param rdrCtx per-thread renderer context 115 */ 116 Dasher(final RendererContext rdrCtx) { 117 this.rdrCtx = rdrCtx; 118 119 dashes_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_ARRAY); // 1K 120 121 firstSegmentsBuffer_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_ARRAY); // 1K 122 firstSegmentsBuffer = firstSegmentsBuffer_ref.initial; 123 124 // we need curCurvepts to be able to contain 2 curves because when 125 // dashing curves, we need to subdivide it 126 curCurvepts = new float[8 * 2]; 127 128 this.curveSplitter = rdrCtx.curveClipSplitter; 129 } 130 131 /** 132 * Initialize the <code>Dasher</code>. 133 * 134 * @param out an output <code>PathConsumer2D</code>. 135 * @param dash an array of <code>float</code>s containing the dash pattern 136 * @param dashLen length of the given dash array 137 * @param phase a <code>float</code> containing the dash phase 138 * @param recycleDashes true to indicate to recycle the given dash array 139 * @return this instance 140 */ 141 public Dasher init(final PathConsumer2D out, final float[] dash, final int dashLen, 142 float phase, final boolean recycleDashes) 143 { 144 this.out = out; 145 146 // Normalize so 0 <= phase < dash[0] 147 int sidx = 0; 148 dashOn = true; 149 150 // note: BasicStroke constructor checks dash elements and sum > 0 151 float sum = 0.0f; 152 for (int i = 0; i < dashLen; i++) { 153 sum += dash[i]; 154 } 155 this.cycleLen = sum; 156 157 float cycles = phase / sum; 158 if (phase < 0.0f) { 159 if (-cycles >= MAX_CYCLES) { 160 phase = 0.0f; 161 } else { 162 int fullcycles = FloatMath.floor_int(-cycles); 163 if ((fullcycles & dashLen & 1) != 0) { 164 dashOn = !dashOn; 165 } 166 phase += fullcycles * sum; 167 while (phase < 0.0f) { 168 if (--sidx < 0) { 169 sidx = dashLen - 1; 170 } 171 phase += dash[sidx]; 172 dashOn = !dashOn; 173 } 174 } 175 } else if (phase > 0.0f) { 176 if (cycles >= MAX_CYCLES) { 185 while (phase >= (d = dash[sidx])) { 186 phase -= d; 187 sidx = (sidx + 1) % dashLen; 188 dashOn = !dashOn; 189 } 190 } 191 } 192 193 this.dash = dash; 194 this.dashLen = dashLen; 195 this.phase = phase; 196 this.startPhase = phase; 197 this.startDashOn = dashOn; 198 this.startIdx = sidx; 199 this.starting = true; 200 this.needsMoveTo = false; 201 this.firstSegidx = 0; 202 203 this.recycleDashes = recycleDashes; 204 205 if (rdrCtx.doClip) { 206 this.clipRect = rdrCtx.clipRect; 207 } else { 208 this.clipRect = null; 209 this.cOutCode = 0; 210 } 211 return this; // fluent API 212 } 213 214 /** 215 * Disposes this dasher: 216 * clean up before reusing this instance 217 */ 218 void dispose() { 219 if (DO_CLEAN_DIRTY) { 220 // Force zero-fill dirty arrays: 221 Arrays.fill(curCurvepts, 0.0f); 222 } 223 // Return arrays: 224 if (recycleDashes) { 225 dash = dashes_ref.putArray(dash); 226 } 227 firstSegmentsBuffer = firstSegmentsBuffer_ref.putArray(firstSegmentsBuffer); 228 } 229 230 public float[] copyDashArray(final float[] dashes) { 231 final int len = dashes.length; 232 final float[] newDashes; 233 if (len <= MarlinConst.INITIAL_ARRAY) { 234 newDashes = dashes_ref.initial; 235 } else { 236 if (DO_STATS) { 237 rdrCtx.stats.stat_array_dasher_dasher.add(len); 238 } 239 newDashes = dashes_ref.getArray(len); 240 } 241 System.arraycopy(dashes, 0, newDashes, 0, len); 242 return newDashes; 243 } 244 245 @Override 246 public void moveTo(final float x0, final float y0) { 247 if (firstSegidx != 0) { 248 out.moveTo(sx0, sy0); 249 emitFirstSegments(); 250 } 251 this.needsMoveTo = true; 252 this.idx = startIdx; 253 this.dashOn = this.startDashOn; 254 this.phase = this.startPhase; 255 this.cx0 = x0; 256 this.cy0 = y0; 257 258 // update starting point: 259 this.sx0 = x0; 260 this.sy0 = y0; 261 this.starting = true; 262 263 if (clipRect != null) { 264 final int outcode = Helpers.outcode(x0, y0, clipRect); 265 this.cOutCode = outcode; 266 this.outside = false; 267 this.totalSkipLen = 0.0f; 268 } 269 } 270 271 private void emitSeg(float[] buf, int off, int type) { 272 switch (type) { 273 case 4: 274 out.lineTo(buf[off], buf[off + 1]); 275 return; 276 case 8: 277 out.curveTo(buf[off ], buf[off + 1], 278 buf[off + 2], buf[off + 3], 279 buf[off + 4], buf[off + 5]); 280 return; 281 case 6: 282 out.quadTo(buf[off ], buf[off + 1], 283 buf[off + 2], buf[off + 3]); 284 return; 285 default: 286 } 287 } 288 289 private void emitFirstSegments() { 290 final float[] fSegBuf = firstSegmentsBuffer; 291 292 for (int i = 0, len = firstSegidx; i < len; ) { 293 int type = (int)fSegBuf[i]; 294 emitSeg(fSegBuf, i + 1, type); 295 i += (type - 1); 296 } 297 firstSegidx = 0; 298 } 299 300 // precondition: pts must be in relative coordinates (relative to x0,y0) 301 private void goTo(final float[] pts, final int off, final int type, 302 final boolean on) 303 { 304 final int index = off + type; 305 final float x = pts[index - 4]; 306 final float y = pts[index - 3]; 307 308 if (on) { 309 if (starting) { 310 goTo_starting(pts, off, type); 311 } else { 312 if (needsMoveTo) { 313 needsMoveTo = false; 314 out.moveTo(cx0, cy0); 315 } 316 emitSeg(pts, off, type); 317 } 318 } else { 319 if (starting) { 320 // low probability test (hotspot) 321 starting = false; 322 } 323 needsMoveTo = true; 324 } 325 this.cx0 = x; 326 this.cy0 = y; 327 } 328 329 private void goTo_starting(final float[] pts, final int off, final int type) { 330 int len = type - 1; // - 2 + 1 331 int segIdx = firstSegidx; 332 float[] buf = firstSegmentsBuffer; 333 334 if (segIdx + len > buf.length) { 335 if (DO_STATS) { 336 rdrCtx.stats.stat_array_dasher_firstSegmentsBuffer 337 .add(segIdx + len); 338 } 339 firstSegmentsBuffer = buf 340 = firstSegmentsBuffer_ref.widenArray(buf, segIdx, 341 segIdx + len); 342 } 343 buf[segIdx++] = type; 344 len--; 345 // small arraycopy (2, 4 or 6) but with offset: 346 System.arraycopy(pts, off, buf, segIdx, len); 347 firstSegidx = segIdx + len; 348 } 349 350 @Override 351 public void lineTo(final float x1, final float y1) { 352 final int outcode0 = this.cOutCode; 353 354 if (clipRect != null) { 355 final int outcode1 = Helpers.outcode(x1, y1, clipRect); 356 357 // Should clip 358 final int orCode = (outcode0 | outcode1); 359 360 if (orCode != 0) { 361 final int sideCode = outcode0 & outcode1; 362 363 // basic rejection criteria: 364 if (sideCode == 0) { 365 // ovelap clip: 366 if (subdivide) { 367 // avoid reentrance 368 subdivide = false; 369 // subdivide curve => callback with subdivided parts: 370 boolean ret = curveSplitter.splitLine(cx0, cy0, x1, y1, 371 orCode, this); 372 // reentrance is done: 373 subdivide = true; 374 if (ret) { 375 return; 376 } 377 } 378 // already subdivided so render it 379 } else { 380 this.cOutCode = outcode1; 381 skipLineTo(x1, y1); 382 return; 383 } 384 } 385 386 this.cOutCode = outcode1; 387 388 if (this.outside) { 389 this.outside = false; 390 // Adjust current index, phase & dash: 391 skipLen(); 392 } 393 } 394 _lineTo(x1, y1); 395 } 396 397 private void _lineTo(final float x1, final float y1) { 398 final float dx = x1 - cx0; 399 final float dy = y1 - cy0; 400 401 float len = dx * dx + dy * dy; 402 if (len == 0.0f) { 403 return; 404 } 405 len = (float) Math.sqrt(len); 406 407 // The scaling factors needed to get the dx and dy of the 408 // transformed dash segments. 409 final float cx = dx / len; 410 final float cy = dy / len; 411 412 final float[] _curCurvepts = curCurvepts; 413 final float[] _dash = dash; 414 final int _dashLen = this.dashLen; 415 416 int _idx = idx; 417 boolean _dashOn = dashOn; 418 float _phase = phase; 419 420 float leftInThisDashSegment, d; 421 422 while (true) { 423 d = _dash[_idx]; 424 leftInThisDashSegment = d - _phase; 425 426 if (len <= leftInThisDashSegment) { 427 _curCurvepts[0] = x1; 428 _curCurvepts[1] = y1; 429 430 goTo(_curCurvepts, 0, 4, _dashOn); 431 432 // Advance phase within current dash segment 433 _phase += len; 434 435 // TODO: compare float values using epsilon: 436 if (len == leftInThisDashSegment) { 437 _phase = 0.0f; 438 _idx = (_idx + 1) % _dashLen; 439 _dashOn = !_dashOn; 440 } 441 break; 442 } 443 444 if (_phase == 0.0f) { 445 _curCurvepts[0] = cx0 + d * cx; 446 _curCurvepts[1] = cy0 + d * cy; 447 } else { 448 _curCurvepts[0] = cx0 + leftInThisDashSegment * cx; 449 _curCurvepts[1] = cy0 + leftInThisDashSegment * cy; 450 } 451 452 goTo(_curCurvepts, 0, 4, _dashOn); 453 454 len -= leftInThisDashSegment; 455 // Advance to next dash segment 456 _idx = (_idx + 1) % _dashLen; 457 _dashOn = !_dashOn; 458 _phase = 0.0f; 459 } 460 // Save local state: 461 idx = _idx; 462 dashOn = _dashOn; 463 phase = _phase; 464 } 465 466 private void skipLineTo(final float x1, final float y1) { 467 final float dx = x1 - cx0; 468 final float dy = y1 - cy0; 469 470 float len = dx * dx + dy * dy; 471 if (len != 0.0f) { 472 len = (float)Math.sqrt(len); 473 } 474 475 // Accumulate skipped length: 476 this.outside = true; 477 this.totalSkipLen += len; 478 479 // Fix initial move: 480 this.needsMoveTo = true; 481 this.starting = false; 482 483 this.cx0 = x1; 484 this.cy0 = y1; 485 } 486 487 public void skipLen() { 488 float len = this.totalSkipLen; 489 this.totalSkipLen = 0.0f; 490 491 final float[] _dash = dash; 492 final int _dashLen = this.dashLen; 493 494 int _idx = idx; 495 boolean _dashOn = dashOn; 496 float _phase = phase; 497 498 // -2 to ensure having 2 iterations of the post-loop 499 // to compensate the remaining phase 500 final long fullcycles = (long)Math.floor(len / cycleLen) - 2L; 501 502 if (fullcycles > 0L) { 503 len -= cycleLen * fullcycles; 504 505 final long iterations = fullcycles * _dashLen; 506 _idx = (int) (iterations + _idx) % _dashLen; 507 _dashOn = (iterations + (_dashOn ? 1L : 0L) & 1L) == 1L; 508 } 509 510 float leftInThisDashSegment, d; 511 512 while (true) { 513 d = _dash[_idx]; 514 leftInThisDashSegment = d - _phase; 515 516 if (len <= leftInThisDashSegment) { 517 // Advance phase within current dash segment 518 _phase += len; 519 520 // TODO: compare float values using epsilon: 521 if (len == leftInThisDashSegment) { 522 _phase = 0.0f; 523 _idx = (_idx + 1) % _dashLen; 524 _dashOn = !_dashOn; 525 } 526 break; 527 } 528 529 len -= leftInThisDashSegment; 530 // Advance to next dash segment 531 _idx = (_idx + 1) % _dashLen; 532 _dashOn = !_dashOn; 533 _phase = 0.0f; 534 } 535 // Save local state: 536 idx = _idx; 537 dashOn = _dashOn; 538 phase = _phase; 539 } 540 541 // preconditions: curCurvepts must be an array of length at least 2 * type, 542 // that contains the curve we want to dash in the first type elements 543 private void somethingTo(final int type) { 544 final float[] _curCurvepts = curCurvepts; 545 if (pointCurve(_curCurvepts, type)) { 546 return; 547 } 548 final LengthIterator _li = li; 549 final float[] _dash = dash; 550 final int _dashLen = this.dashLen; 551 552 _li.initializeIterationOnCurve(_curCurvepts, type); 553 554 int _idx = idx; 555 boolean _dashOn = dashOn; 556 float _phase = phase; 557 558 // initially the current curve is at curCurvepts[0...type] 559 int curCurveoff = 0; 560 float prevT = 0.0f; 561 float t; 562 float leftInThisDashSegment = _dash[_idx] - _phase; 563 564 while ((t = _li.next(leftInThisDashSegment)) < 1.0f) { 565 if (t != 0.0f) { 566 Helpers.subdivideAt((t - prevT) / (1.0f - prevT), 567 _curCurvepts, curCurveoff, 568 _curCurvepts, 0, type); 569 prevT = t; 570 goTo(_curCurvepts, 2, type, _dashOn); 571 curCurveoff = type; 572 } 573 // Advance to next dash segment 574 _idx = (_idx + 1) % _dashLen; 575 _dashOn = !_dashOn; 576 _phase = 0.0f; 577 leftInThisDashSegment = _dash[_idx]; 578 } 579 580 goTo(_curCurvepts, curCurveoff + 2, type, _dashOn); 581 582 _phase += _li.lastSegLen(); 583 if (_phase >= _dash[_idx]) { 584 _phase = 0.0f; 585 _idx = (_idx + 1) % _dashLen; 586 _dashOn = !_dashOn; 587 } 588 // Save local state: 589 idx = _idx; 590 dashOn = _dashOn; 591 phase = _phase; 592 593 // reset LengthIterator: 594 _li.reset(); 595 } 596 597 private void skipSomethingTo(final int type) { 598 final float[] _curCurvepts = curCurvepts; 599 if (pointCurve(_curCurvepts, type)) { 600 return; 601 } 602 final LengthIterator _li = li; 603 604 _li.initializeIterationOnCurve(_curCurvepts, type); 605 606 // In contrary to somethingTo(), 607 // just estimate properly the curve length: 608 final float len = _li.totalLength(); 609 610 // Accumulate skipped length: 611 this.outside = true; 612 this.totalSkipLen += len; 613 614 // Fix initial move: 615 this.needsMoveTo = true; 616 this.starting = false; 617 } 618 619 private static boolean pointCurve(final float[] curve, final int type) { 620 for (int i = 2; i < type; i++) { 621 if (curve[i] != curve[i-2]) { 622 return false; 623 } 624 } 625 return true; 626 } 627 628 // Objects of this class are used to iterate through curves. They return 629 // t values where the left side of the curve has a specified length. 630 // It does this by subdividing the input curve until a certain error 631 // condition has been met. A recursive subdivision procedure would 632 // return as many as 1<<limit curves, but this is an iterator and we 633 // don't need all the curves all at once, so what we carry out a 634 // lazy inorder traversal of the recursion tree (meaning we only move 635 // through the tree when we need the next subdivided curve). This saves 636 // us a lot of memory because at any one time we only need to store 637 // limit+1 curves - one for each level of the tree + 1. 638 // NOTE: the way we do things here is not enough to traverse a general 639 // tree; however, the trees we are interested in have the property that 640 // every non leaf node has exactly 2 children 641 static final class LengthIterator { 642 // Holds the curves at various levels of the recursion. The root 643 // (i.e. the original curve) is at recCurveStack[0] (but then it 644 // gets subdivided, the left half is put at 1, so most of the time 645 // only the right half of the original curve is at 0) 646 private final float[][] recCurveStack; // dirty 647 // sidesRight[i] indicates whether the node at level i+1 in the path from 648 // the root to the current leaf is a left or right child of its parent. 649 private final boolean[] sidesRight; // dirty 650 private int curveType; 651 // lastT and nextT delimit the current leaf. 652 private float nextT; 653 private float lenAtNextT; 654 private float lastT; 655 private float lenAtLastT; 656 private float lenAtLastSplit; 657 private float lastSegLen; 658 // the current level in the recursion tree. 0 is the root. limit 659 // is the deepest possible leaf. 660 private int recLevel; 661 private boolean done; 662 663 // the lengths of the lines of the control polygon. Only its first 664 // curveType/2 - 1 elements are valid. This is an optimization. See 665 // next() for more detail. 666 private final float[] curLeafCtrlPolyLengths = new float[3]; 667 668 LengthIterator() { 669 this.recCurveStack = new float[REC_LIMIT + 1][8]; 670 this.sidesRight = new boolean[REC_LIMIT]; 671 // if any methods are called without first initializing this object 672 // on a curve, we want it to fail ASAP. 673 this.nextT = Float.MAX_VALUE; 674 this.lenAtNextT = Float.MAX_VALUE; 675 this.lenAtLastSplit = Float.MIN_VALUE; 676 this.recLevel = Integer.MIN_VALUE; 677 this.lastSegLen = Float.MAX_VALUE; 678 this.done = true; 679 } 680 681 /** 682 * Reset this LengthIterator. 683 */ 684 void reset() { 685 // keep data dirty 686 // as it appears not useful to reset data: 687 if (DO_CLEAN_DIRTY) { 688 final int recLimit = recCurveStack.length - 1; 689 for (int i = recLimit; i >= 0; i--) { 690 Arrays.fill(recCurveStack[i], 0.0f); 691 } 692 Arrays.fill(sidesRight, false); 693 Arrays.fill(curLeafCtrlPolyLengths, 0.0f); 694 Arrays.fill(nextRoots, 0.0f); 695 Arrays.fill(flatLeafCoefCache, 0.0f); 696 flatLeafCoefCache[2] = -1.0f; 697 } 698 } 699 700 void initializeIterationOnCurve(final float[] pts, final int type) { 701 // optimize arraycopy (8 values faster than 6 = type): 702 System.arraycopy(pts, 0, recCurveStack[0], 0, 8); 703 this.curveType = type; 704 this.recLevel = 0; 705 this.lastT = 0.0f; 706 this.lenAtLastT = 0.0f; 707 this.nextT = 0.0f; 708 this.lenAtNextT = 0.0f; 709 goLeft(); // initializes nextT and lenAtNextT properly 710 this.lenAtLastSplit = 0.0f; 711 if (recLevel > 0) { 712 this.sidesRight[0] = false; 713 this.done = false; 714 } else { 715 // the root of the tree is a leaf so we're done. 716 this.sidesRight[0] = true; 717 this.done = true; 718 } 719 this.lastSegLen = 0.0f; 720 } 721 722 // 0 == false, 1 == true, -1 == invalid cached value. 723 private int cachedHaveLowAcceleration = -1; 724 725 private boolean haveLowAcceleration(final float err) { 726 if (cachedHaveLowAcceleration == -1) { 727 final float len1 = curLeafCtrlPolyLengths[0]; 728 final float len2 = curLeafCtrlPolyLengths[1]; 729 // the test below is equivalent to !within(len1/len2, 1, err). 730 // It is using a multiplication instead of a division, so it 731 // should be a bit faster. 732 if (!Helpers.within(len1, len2, err * len2)) { 733 cachedHaveLowAcceleration = 0; 734 return false; 735 } 736 if (curveType == 8) { 737 final float len3 = curLeafCtrlPolyLengths[2]; 738 // if len1 is close to 2 and 2 is close to 3, that probably 739 // means 1 is close to 3 so the second part of this test might 740 // not be needed, but it doesn't hurt to include it. 741 final float errLen3 = err * len3; 742 if (!(Helpers.within(len2, len3, errLen3) && 743 Helpers.within(len1, len3, errLen3))) { 744 cachedHaveLowAcceleration = 0; 745 return false; 794 float z = y + curLeafCtrlPolyLengths[2]; 795 _flatLeafCoefCache[0] = 3.0f * (x - y) + z; 796 _flatLeafCoefCache[1] = 3.0f * (y - 2.0f * x); 797 _flatLeafCoefCache[2] = 3.0f * x; 798 _flatLeafCoefCache[3] = -z; 799 } else if (curveType == 6) { 800 _flatLeafCoefCache[0] = 0.0f; 801 _flatLeafCoefCache[1] = y - 2.0f * x; 802 _flatLeafCoefCache[2] = 2.0f * x; 803 _flatLeafCoefCache[3] = -y; 804 } 805 } 806 float a = _flatLeafCoefCache[0]; 807 float b = _flatLeafCoefCache[1]; 808 float c = _flatLeafCoefCache[2]; 809 float d = t * _flatLeafCoefCache[3]; 810 811 // we use cubicRootsInAB here, because we want only roots in 0, 1, 812 // and our quadratic root finder doesn't filter, so it's just a 813 // matter of convenience. 814 final int n = Helpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0.0f, 1.0f); 815 if (n == 1 && !Float.isNaN(nextRoots[0])) { 816 t = nextRoots[0]; 817 } 818 } 819 // t is relative to the current leaf, so we must make it a valid parameter 820 // of the original curve. 821 t = t * (nextT - lastT) + lastT; 822 if (t >= 1.0f) { 823 t = 1.0f; 824 done = true; 825 } 826 // even if done = true, if we're here, that means targetLength 827 // is equal to, or very, very close to the total length of the 828 // curve, so lastSegLen won't be too high. In cases where len 829 // overshoots the curve, this method will exit in the while 830 // loop, and lastSegLen will still be set to the right value. 831 lastSegLen = len; 832 return t; 833 } 834 835 float totalLength() { 836 while (!done) { 837 goToNextLeaf(); 838 } 839 // reset LengthIterator: 840 reset(); 841 842 return lenAtNextT; 843 } 844 845 float lastSegLen() { 846 return lastSegLen; 847 } 848 849 // go to the next leaf (in an inorder traversal) in the recursion tree 850 // preconditions: must be on a leaf, and that leaf must not be the root. 851 private void goToNextLeaf() { 852 // We must go to the first ancestor node that has an unvisited 853 // right child. 854 final boolean[] _sides = sidesRight; 855 int _recLevel = recLevel; 856 _recLevel--; 857 858 while(_sides[_recLevel]) { 859 if (_recLevel == 0) { 860 recLevel = 0; 861 done = true; 862 return; 863 } 864 _recLevel--; 865 } 866 867 _sides[_recLevel] = true; 868 // optimize arraycopy (8 values faster than 6 = type): 869 System.arraycopy(recCurveStack[_recLevel++], 0, 870 recCurveStack[_recLevel], 0, 8); 871 recLevel = _recLevel; 872 goLeft(); 873 } 874 875 // go to the leftmost node from the current node. Return its length. 876 private void goLeft() { 877 final float len = onLeaf(); 878 if (len >= 0.0f) { 879 lastT = nextT; 880 lenAtLastT = lenAtNextT; 881 nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC; 882 lenAtNextT += len; 883 // invalidate caches 884 flatLeafCoefCache[2] = -1.0f; 885 cachedHaveLowAcceleration = -1; 886 } else { 887 Helpers.subdivide(recCurveStack[recLevel], 888 recCurveStack[recLevel + 1], 889 recCurveStack[recLevel], curveType); 890 891 sidesRight[recLevel] = false; 892 recLevel++; 893 goLeft(); 894 } 895 } 896 897 // this is a bit of a hack. It returns -1 if we're not on a leaf, and 898 // the length of the leaf if we are on a leaf. 899 private float onLeaf() { 900 final float[] curve = recCurveStack[recLevel]; 901 final int _curveType = curveType; 902 float polyLen = 0.0f; 903 904 float x0 = curve[0], y0 = curve[1]; 905 for (int i = 2; i < _curveType; i += 2) { 906 final float x1 = curve[i], y1 = curve[i + 1]; 907 final float len = Helpers.linelen(x0, y0, x1, y1); 908 polyLen += len; 909 curLeafCtrlPolyLengths[(i >> 1) - 1] = len; 910 x0 = x1; 911 y0 = y1; 912 } 913 914 final float lineLen = Helpers.linelen(curve[0], curve[1], x0, y0); 915 916 if ((polyLen - lineLen) < CURVE_LEN_ERR || recLevel == REC_LIMIT) { 917 return (polyLen + lineLen) / 2.0f; 918 } 919 return -1.0f; 920 } 921 } 922 923 @Override 924 public void curveTo(final float x1, final float y1, 925 final float x2, final float y2, 926 final float x3, final float y3) 927 { 928 final int outcode0 = this.cOutCode; 929 930 if (clipRect != null) { 931 final int outcode1 = Helpers.outcode(x1, y1, clipRect); 932 final int outcode2 = Helpers.outcode(x2, y2, clipRect); 933 final int outcode3 = Helpers.outcode(x3, y3, clipRect); 934 935 // Should clip 936 final int orCode = (outcode0 | outcode1 | outcode2 | outcode3); 937 if (orCode != 0) { 938 final int sideCode = outcode0 & outcode1 & outcode2 & outcode3; 939 940 // basic rejection criteria: 941 if (sideCode == 0) { 942 // ovelap clip: 943 if (subdivide) { 944 // avoid reentrance 945 subdivide = false; 946 // subdivide curve => callback with subdivided parts: 947 boolean ret = curveSplitter.splitCurve(cx0, cy0, x1, y1, x2, y2, x3, y3, 948 orCode, this); 949 // reentrance is done: 950 subdivide = true; 951 if (ret) { 952 return; 953 } 954 } 955 // already subdivided so render it 956 } else { 957 this.cOutCode = outcode3; 958 skipCurveTo(x1, y1, x2, y2, x3, y3); 959 return; 960 } 961 } 962 963 this.cOutCode = outcode3; 964 965 if (this.outside) { 966 this.outside = false; 967 // Adjust current index, phase & dash: 968 skipLen(); 969 } 970 } 971 _curveTo(x1, y1, x2, y2, x3, y3); 972 } 973 974 private void _curveTo(final float x1, final float y1, 975 final float x2, final float y2, 976 final float x3, final float y3) 977 { 978 final float[] _curCurvepts = curCurvepts; 979 980 // monotonize curve: 981 final CurveBasicMonotonizer monotonizer 982 = rdrCtx.monotonizer.curve(cx0, cy0, x1, y1, x2, y2, x3, y3); 983 984 final int nSplits = monotonizer.nbSplits; 985 final float[] mid = monotonizer.middle; 986 987 for (int i = 0, off = 0; i <= nSplits; i++, off += 6) { 988 // optimize arraycopy (8 values faster than 6 = type): 989 System.arraycopy(mid, off, _curCurvepts, 0, 8); 990 991 somethingTo(8); 992 } 993 } 994 995 private void skipCurveTo(final float x1, final float y1, 996 final float x2, final float y2, 997 final float x3, final float y3) 998 { 999 final float[] _curCurvepts = curCurvepts; 1000 _curCurvepts[0] = cx0; _curCurvepts[1] = cy0; 1001 _curCurvepts[2] = x1; _curCurvepts[3] = y1; 1002 _curCurvepts[4] = x2; _curCurvepts[5] = y2; 1003 _curCurvepts[6] = x3; _curCurvepts[7] = y3; 1004 1005 skipSomethingTo(8); 1006 1007 this.cx0 = x3; 1008 this.cy0 = y3; 1009 } 1010 1011 @Override 1012 public void quadTo(final float x1, final float y1, 1013 final float x2, final float y2) 1014 { 1015 final int outcode0 = this.cOutCode; 1016 1017 if (clipRect != null) { 1018 final int outcode1 = Helpers.outcode(x1, y1, clipRect); 1019 final int outcode2 = Helpers.outcode(x2, y2, clipRect); 1020 1021 // Should clip 1022 final int orCode = (outcode0 | outcode1 | outcode2); 1023 if (orCode != 0) { 1024 final int sideCode = outcode0 & outcode1 & outcode2; 1025 1026 // basic rejection criteria: 1027 if (sideCode == 0) { 1028 // ovelap clip: 1029 if (subdivide) { 1030 // avoid reentrance 1031 subdivide = false; 1032 // subdivide curve => call lineTo() with subdivided curves: 1033 boolean ret = curveSplitter.splitQuad(cx0, cy0, x1, y1, 1034 x2, y2, orCode, this); 1035 // reentrance is done: 1036 subdivide = true; 1037 if (ret) { 1038 return; 1039 } 1040 } 1041 // already subdivided so render it 1042 } else { 1043 this.cOutCode = outcode2; 1044 skipQuadTo(x1, y1, x2, y2); 1045 return; 1046 } 1047 } 1048 1049 this.cOutCode = outcode2; 1050 1051 if (this.outside) { 1052 this.outside = false; 1053 // Adjust current index, phase & dash: 1054 skipLen(); 1055 } 1056 } 1057 _quadTo(x1, y1, x2, y2); 1058 } 1059 1060 private void _quadTo(final float x1, final float y1, 1061 final float x2, final float y2) 1062 { 1063 final float[] _curCurvepts = curCurvepts; 1064 1065 // monotonize quad: 1066 final CurveBasicMonotonizer monotonizer 1067 = rdrCtx.monotonizer.quad(cx0, cy0, x1, y1, x2, y2); 1068 1069 final int nSplits = monotonizer.nbSplits; 1070 final float[] mid = monotonizer.middle; 1071 1072 for (int i = 0, off = 0; i <= nSplits; i++, off += 4) { 1073 // optimize arraycopy (8 values faster than 6 = type): 1074 System.arraycopy(mid, off, _curCurvepts, 0, 8); 1075 1076 somethingTo(6); 1077 } 1078 } 1079 1080 private void skipQuadTo(final float x1, final float y1, 1081 final float x2, final float y2) 1082 { 1083 final float[] _curCurvepts = curCurvepts; 1084 _curCurvepts[0] = cx0; _curCurvepts[1] = cy0; 1085 _curCurvepts[2] = x1; _curCurvepts[3] = y1; 1086 _curCurvepts[4] = x2; _curCurvepts[5] = y2; 1087 1088 skipSomethingTo(6); 1089 1090 this.cx0 = x2; 1091 this.cy0 = y2; 1092 } 1093 1094 @Override 1095 public void closePath() { 1096 if (cx0 != sx0 || cy0 != sy0) { 1097 lineTo(sx0, sy0); 1098 } 1099 if (firstSegidx != 0) { 1100 if (!dashOn || needsMoveTo) { 1101 out.moveTo(sx0, sy0); 1102 } 1103 emitFirstSegments(); 1104 } 1105 moveTo(sx0, sy0); 1106 } 1107 1108 @Override 1109 public void pathDone() { 1110 if (firstSegidx != 0) { 1111 out.moveTo(sx0, sy0); 1112 emitFirstSegments(); 1113 } 1114 out.pathDone(); 1115 1116 // Dispose this instance: 1117 dispose(); 1118 } 1119 } 1120 |