1 /* 2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.java2d.marlin; 27 28 import java.util.Arrays; 29 import sun.awt.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 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 Dasher init(final PathConsumer2D out, float[] dash, int dashLen, 112 float phase, boolean recycleDashes) 113 { 114 this.out = out; 115 116 // Normalize so 0 <= phase < dash[0] 117 int sidx = 0; 118 dashOn = true; 119 float sum = 0.0f; 120 for (float d : dash) { 121 sum += d; 122 } 123 float cycles = phase / sum; 124 if (phase < 0.0f) { 125 if (-cycles >= MAX_CYCLES) { 126 phase = 0.0f; 127 } else { 128 int fullcycles = FloatMath.floor_int(-cycles); 129 if ((fullcycles & dash.length & 1) != 0) { 130 dashOn = !dashOn; 131 } 132 phase += fullcycles * sum; 133 while (phase < 0.0f) { 134 if (--sidx < 0) { 135 sidx = dash.length - 1; 136 } 137 phase += dash[sidx]; 138 dashOn = !dashOn; 139 } 140 } 141 } else if (phase > 0.0f) { 142 if (cycles >= MAX_CYCLES) { 151 while (phase >= (d = dash[sidx])) { 152 phase -= d; 153 sidx = (sidx + 1) % dash.length; 154 dashOn = !dashOn; 155 } 156 } 157 } 158 159 this.dash = dash; 160 this.dashLen = dashLen; 161 this.phase = phase; 162 this.startPhase = phase; 163 this.startDashOn = dashOn; 164 this.startIdx = sidx; 165 this.starting = true; 166 this.needsMoveTo = false; 167 this.firstSegidx = 0; 168 169 this.recycleDashes = recycleDashes; 170 171 return this; // fluent API 172 } 173 174 /** 175 * Disposes this dasher: 176 * clean up before reusing this instance 177 */ 178 void dispose() { 179 if (DO_CLEAN_DIRTY) { 180 // Force zero-fill dirty arrays: 181 Arrays.fill(curCurvepts, 0.0f); 182 } 183 // Return arrays: 184 if (recycleDashes) { 185 dash = dashes_ref.putArray(dash); 186 } 187 firstSegmentsBuffer = firstSegmentsBuffer_ref.putArray(firstSegmentsBuffer); 188 } 189 190 float[] copyDashArray(final float[] dashes) { 191 final int len = dashes.length; 192 final float[] newDashes; 193 if (len <= MarlinConst.INITIAL_ARRAY) { 194 newDashes = dashes_ref.initial; 195 } else { 196 if (DO_STATS) { 197 rdrCtx.stats.stat_array_dasher_dasher.add(len); 198 } 199 newDashes = dashes_ref.getArray(len); 200 } 201 System.arraycopy(dashes, 0, newDashes, 0, len); 202 return newDashes; 203 } 204 205 @Override 206 public void moveTo(final float x0, final float y0) { 207 if (firstSegidx != 0) { 208 out.moveTo(sx, sy); 209 emitFirstSegments(); 210 } 211 needsMoveTo = true; 212 this.idx = startIdx; 213 this.dashOn = this.startDashOn; 214 this.phase = this.startPhase; 215 this.sx = x0; 216 this.sy = y0; 217 this.x0 = x0; 218 this.y0 = y0; 219 this.starting = true; 220 } 221 222 private void emitSeg(float[] buf, int off, int type) { 223 switch (type) { 224 case 8: 225 out.curveTo(buf[off+0], buf[off+1], 226 buf[off+2], buf[off+3], 227 buf[off+4], buf[off+5]); 228 return; 229 case 6: 230 out.quadTo(buf[off+0], buf[off+1], 231 buf[off+2], buf[off+3]); 232 return; 233 case 4: 234 out.lineTo(buf[off], buf[off+1]); 235 return; 236 default: 237 } 238 } 239 240 private void emitFirstSegments() { 241 final float[] fSegBuf = firstSegmentsBuffer; 242 243 for (int i = 0, len = firstSegidx; i < len; ) { 244 int type = (int)fSegBuf[i]; 245 emitSeg(fSegBuf, i + 1, type); 246 i += (type - 1); 247 } 248 firstSegidx = 0; 249 } 250 // We don't emit the first dash right away. If we did, caps would be 251 // drawn on it, but we need joins to be drawn if there's a closePath() 252 // So, we store the path elements that make up the first dash in the 253 // buffer below. 254 private float[] firstSegmentsBuffer; // dynamic array 255 private int firstSegidx; 256 257 // precondition: pts must be in relative coordinates (relative to x0,y0) 258 private void goTo(final float[] pts, final int off, final int type, 259 final boolean on) 260 { 261 final int index = off + type; 262 final float x = pts[index - 4]; 263 final float y = pts[index - 3]; 264 265 if (on) { 266 if (starting) { 267 goTo_starting(pts, off, type); 268 } else { 269 if (needsMoveTo) { 270 needsMoveTo = false; 271 out.moveTo(x0, y0); 272 } 273 emitSeg(pts, off, type); 274 } 275 } else { 276 if (starting) { 277 // low probability test (hotspot) 278 starting = false; 279 } 280 needsMoveTo = true; 281 } 282 this.x0 = x; 283 this.y0 = y; 284 } 285 286 private void goTo_starting(final float[] pts, final int off, final int type) { 287 int len = type - 1; // - 2 + 1 288 int segIdx = firstSegidx; 289 float[] buf = firstSegmentsBuffer; 290 291 if (segIdx + len > buf.length) { 292 if (DO_STATS) { 293 rdrCtx.stats.stat_array_dasher_firstSegmentsBuffer 294 .add(segIdx + len); 295 } 296 firstSegmentsBuffer = buf 297 = firstSegmentsBuffer_ref.widenArray(buf, segIdx, 298 segIdx + len); 299 } 300 buf[segIdx++] = type; 301 len--; 302 // small arraycopy (2, 4 or 6) but with offset: 303 System.arraycopy(pts, off, buf, segIdx, len); 304 firstSegidx = segIdx + len; 305 } 306 307 @Override 308 public void lineTo(final float x1, final float y1) { 309 final float dx = x1 - x0; 310 final float dy = y1 - y0; 311 312 float len = dx*dx + dy*dy; 313 if (len == 0.0f) { 314 return; 315 } 316 len = (float) Math.sqrt(len); 317 318 // The scaling factors needed to get the dx and dy of the 319 // transformed dash segments. 320 final float cx = dx / len; 321 final float cy = dy / len; 322 323 final float[] _curCurvepts = curCurvepts; 324 final float[] _dash = dash; 325 final int _dashLen = this.dashLen; 326 327 int _idx = idx; 328 boolean _dashOn = dashOn; 329 float _phase = phase; 330 331 float leftInThisDashSegment; 332 float d, dashdx, dashdy, p; 333 334 while (true) { 335 d = _dash[_idx]; 336 leftInThisDashSegment = d - _phase; 337 338 if (len <= leftInThisDashSegment) { 339 _curCurvepts[0] = x1; 340 _curCurvepts[1] = y1; 341 342 goTo(_curCurvepts, 0, 4, _dashOn); 343 344 // Advance phase within current dash segment 345 _phase += len; 346 347 // TODO: compare float values using epsilon: 348 if (len == leftInThisDashSegment) { 349 _phase = 0.0f; 350 _idx = (_idx + 1) % _dashLen; 351 _dashOn = !_dashOn; 352 } 353 354 // Save local state: 355 idx = _idx; 356 dashOn = _dashOn; 357 phase = _phase; 358 return; 359 } 360 361 dashdx = d * cx; 362 dashdy = d * cy; 363 364 if (_phase == 0.0f) { 365 _curCurvepts[0] = x0 + dashdx; 366 _curCurvepts[1] = y0 + dashdy; 367 } else { 368 p = leftInThisDashSegment / d; 369 _curCurvepts[0] = x0 + p * dashdx; 370 _curCurvepts[1] = y0 + p * dashdy; 371 } 372 373 goTo(_curCurvepts, 0, 4, _dashOn); 374 375 len -= leftInThisDashSegment; 376 // Advance to next dash segment 377 _idx = (_idx + 1) % _dashLen; 378 _dashOn = !_dashOn; 379 _phase = 0.0f; 380 } 381 } 382 383 // shared instance in Dasher 384 private final LengthIterator li = new LengthIterator(); 385 386 // preconditions: curCurvepts must be an array of length at least 2 * type, 387 // that contains the curve we want to dash in the first type elements 388 private void somethingTo(final int type) { 389 if (pointCurve(curCurvepts, type)) { 390 return; 391 } 392 final LengthIterator _li = li; 393 final float[] _curCurvepts = curCurvepts; 394 final float[] _dash = dash; 395 final int _dashLen = this.dashLen; 396 397 _li.initializeIterationOnCurve(_curCurvepts, type); 398 399 int _idx = idx; 400 boolean _dashOn = dashOn; 401 float _phase = phase; 402 403 // initially the current curve is at curCurvepts[0...type] 404 int curCurveoff = 0; 405 float lastSplitT = 0.0f; 406 float t; 407 float leftInThisDashSegment = _dash[_idx] - _phase; 408 409 while ((t = _li.next(leftInThisDashSegment)) < 1.0f) { 410 if (t != 0.0f) { 411 Helpers.subdivideAt((t - lastSplitT) / (1.0f - lastSplitT), 412 _curCurvepts, curCurveoff, 413 _curCurvepts, 0, 414 _curCurvepts, type, type); 415 lastSplitT = t; 416 goTo(_curCurvepts, 2, type, _dashOn); 417 curCurveoff = type; 418 } 419 // Advance to next dash segment 420 _idx = (_idx + 1) % _dashLen; 421 _dashOn = !_dashOn; 422 _phase = 0.0f; 423 leftInThisDashSegment = _dash[_idx]; 424 } 425 426 goTo(_curCurvepts, curCurveoff + 2, type, _dashOn); 427 428 _phase += _li.lastSegLen(); 429 if (_phase >= _dash[_idx]) { 430 _phase = 0.0f; 431 _idx = (_idx + 1) % _dashLen; 432 _dashOn = !_dashOn; 433 } 434 // Save local state: 435 idx = _idx; 436 dashOn = _dashOn; 437 phase = _phase; 438 439 // reset LengthIterator: 440 _li.reset(); 441 } 442 443 private static boolean pointCurve(float[] curve, int type) { 444 for (int i = 2; i < type; i++) { 445 if (curve[i] != curve[i-2]) { 446 return false; 447 } 448 } 449 return true; 450 } 451 452 // Objects of this class are used to iterate through curves. They return 453 // t values where the left side of the curve has a specified length. 454 // It does this by subdividing the input curve until a certain error 455 // condition has been met. A recursive subdivision procedure would 456 // return as many as 1<<limit curves, but this is an iterator and we 457 // don't need all the curves all at once, so what we carry out a 458 // lazy inorder traversal of the recursion tree (meaning we only move 459 // through the tree when we need the next subdivided curve). This saves 460 // us a lot of memory because at any one time we only need to store 461 // limit+1 curves - one for each level of the tree + 1. 462 // NOTE: the way we do things here is not enough to traverse a general 463 // tree; however, the trees we are interested in have the property that 464 // every non leaf node has exactly 2 children 465 static final class LengthIterator { 466 private enum Side {LEFT, RIGHT} 467 // Holds the curves at various levels of the recursion. The root 468 // (i.e. the original curve) is at recCurveStack[0] (but then it 469 // gets subdivided, the left half is put at 1, so most of the time 470 // only the right half of the original curve is at 0) 471 private final float[][] recCurveStack; // dirty 472 // sides[i] indicates whether the node at level i+1 in the path from 473 // the root to the current leaf is a left or right child of its parent. 474 private final Side[] sides; // dirty 475 private int curveType; 476 // lastT and nextT delimit the current leaf. 477 private float nextT; 478 private float lenAtNextT; 479 private float lastT; 480 private float lenAtLastT; 481 private float lenAtLastSplit; 482 private float lastSegLen; 483 // the current level in the recursion tree. 0 is the root. limit 484 // is the deepest possible leaf. 485 private int recLevel; 486 private boolean done; 487 488 // the lengths of the lines of the control polygon. Only its first 489 // curveType/2 - 1 elements are valid. This is an optimization. See 490 // next() for more detail. 491 private final float[] curLeafCtrlPolyLengths = new float[3]; 492 493 LengthIterator() { 494 this.recCurveStack = new float[REC_LIMIT + 1][8]; 495 this.sides = new Side[REC_LIMIT]; 496 // if any methods are called without first initializing this object 497 // on a curve, we want it to fail ASAP. 498 this.nextT = Float.MAX_VALUE; 499 this.lenAtNextT = Float.MAX_VALUE; 500 this.lenAtLastSplit = Float.MIN_VALUE; 501 this.recLevel = Integer.MIN_VALUE; 502 this.lastSegLen = Float.MAX_VALUE; 503 this.done = true; 504 } 505 506 /** 507 * Reset this LengthIterator. 508 */ 509 void reset() { 510 // keep data dirty 511 // as it appears not useful to reset data: 512 if (DO_CLEAN_DIRTY) { 513 final int recLimit = recCurveStack.length - 1; 514 for (int i = recLimit; i >= 0; i--) { 515 Arrays.fill(recCurveStack[i], 0.0f); 516 } 517 Arrays.fill(sides, Side.LEFT); 518 Arrays.fill(curLeafCtrlPolyLengths, 0.0f); 519 Arrays.fill(nextRoots, 0.0f); 520 Arrays.fill(flatLeafCoefCache, 0.0f); 521 flatLeafCoefCache[2] = -1.0f; 522 } 523 } 524 525 void initializeIterationOnCurve(float[] pts, int type) { 526 // optimize arraycopy (8 values faster than 6 = type): 527 System.arraycopy(pts, 0, recCurveStack[0], 0, 8); 528 this.curveType = type; 529 this.recLevel = 0; 530 this.lastT = 0.0f; 531 this.lenAtLastT = 0.0f; 532 this.nextT = 0.0f; 533 this.lenAtNextT = 0.0f; 534 goLeft(); // initializes nextT and lenAtNextT properly 535 this.lenAtLastSplit = 0.0f; 536 if (recLevel > 0) { 537 this.sides[0] = Side.LEFT; 538 this.done = false; 539 } else { 540 // the root of the tree is a leaf so we're done. 541 this.sides[0] = Side.RIGHT; 542 this.done = true; 543 } 544 this.lastSegLen = 0.0f; 545 } 546 547 // 0 == false, 1 == true, -1 == invalid cached value. 548 private int cachedHaveLowAcceleration = -1; 549 550 private boolean haveLowAcceleration(float err) { 551 if (cachedHaveLowAcceleration == -1) { 552 final float len1 = curLeafCtrlPolyLengths[0]; 553 final float len2 = curLeafCtrlPolyLengths[1]; 554 // the test below is equivalent to !within(len1/len2, 1, err). 555 // It is using a multiplication instead of a division, so it 556 // should be a bit faster. 557 if (!Helpers.within(len1, len2, err * len2)) { 558 cachedHaveLowAcceleration = 0; 559 return false; 560 } 561 if (curveType == 8) { 562 final float len3 = curLeafCtrlPolyLengths[2]; 563 // if len1 is close to 2 and 2 is close to 3, that probably 564 // means 1 is close to 3 so the second part of this test might 565 // not be needed, but it doesn't hurt to include it. 566 final float errLen3 = err * len3; 567 if (!(Helpers.within(len2, len3, errLen3) && 568 Helpers.within(len1, len3, errLen3))) { 569 cachedHaveLowAcceleration = 0; 570 return false; 619 float z = y + curLeafCtrlPolyLengths[2]; 620 _flatLeafCoefCache[0] = 3.0f * (x - y) + z; 621 _flatLeafCoefCache[1] = 3.0f * (y - 2.0f * x); 622 _flatLeafCoefCache[2] = 3.0f * x; 623 _flatLeafCoefCache[3] = -z; 624 } else if (curveType == 6) { 625 _flatLeafCoefCache[0] = 0.0f; 626 _flatLeafCoefCache[1] = y - 2.0f * x; 627 _flatLeafCoefCache[2] = 2.0f * x; 628 _flatLeafCoefCache[3] = -y; 629 } 630 } 631 float a = _flatLeafCoefCache[0]; 632 float b = _flatLeafCoefCache[1]; 633 float c = _flatLeafCoefCache[2]; 634 float d = t * _flatLeafCoefCache[3]; 635 636 // we use cubicRootsInAB here, because we want only roots in 0, 1, 637 // and our quadratic root finder doesn't filter, so it's just a 638 // matter of convenience. 639 int n = Helpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0.0f, 1.0f); 640 if (n == 1 && !Float.isNaN(nextRoots[0])) { 641 t = nextRoots[0]; 642 } 643 } 644 // t is relative to the current leaf, so we must make it a valid parameter 645 // of the original curve. 646 t = t * (nextT - lastT) + lastT; 647 if (t >= 1.0f) { 648 t = 1.0f; 649 done = true; 650 } 651 // even if done = true, if we're here, that means targetLength 652 // is equal to, or very, very close to the total length of the 653 // curve, so lastSegLen won't be too high. In cases where len 654 // overshoots the curve, this method will exit in the while 655 // loop, and lastSegLen will still be set to the right value. 656 lastSegLen = len; 657 return t; 658 } 659 660 float lastSegLen() { 661 return lastSegLen; 662 } 663 664 // go to the next leaf (in an inorder traversal) in the recursion tree 665 // preconditions: must be on a leaf, and that leaf must not be the root. 666 private void goToNextLeaf() { 667 // We must go to the first ancestor node that has an unvisited 668 // right child. 669 int _recLevel = recLevel; 670 final Side[] _sides = sides; 671 672 _recLevel--; 673 while(_sides[_recLevel] == Side.RIGHT) { 674 if (_recLevel == 0) { 675 recLevel = 0; 676 done = true; 677 return; 678 } 679 _recLevel--; 680 } 681 682 _sides[_recLevel] = Side.RIGHT; 683 // optimize arraycopy (8 values faster than 6 = type): 684 System.arraycopy(recCurveStack[_recLevel], 0, 685 recCurveStack[_recLevel+1], 0, 8); 686 _recLevel++; 687 688 recLevel = _recLevel; 689 goLeft(); 690 } 691 692 // go to the leftmost node from the current node. Return its length. 693 private void goLeft() { 694 float len = onLeaf(); 695 if (len >= 0.0f) { 696 lastT = nextT; 697 lenAtLastT = lenAtNextT; 698 nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC; 699 lenAtNextT += len; 700 // invalidate caches 701 flatLeafCoefCache[2] = -1.0f; 702 cachedHaveLowAcceleration = -1; 703 } else { 704 Helpers.subdivide(recCurveStack[recLevel], 0, 705 recCurveStack[recLevel+1], 0, 706 recCurveStack[recLevel], 0, curveType); 707 sides[recLevel] = Side.LEFT; 708 recLevel++; 709 goLeft(); 710 } 711 } 712 713 // this is a bit of a hack. It returns -1 if we're not on a leaf, and 714 // the length of the leaf if we are on a leaf. 715 private float onLeaf() { 716 final float[] curve = recCurveStack[recLevel]; 717 final int _curveType = curveType; 718 float polyLen = 0.0f; 719 720 float x0 = curve[0], y0 = curve[1]; 721 for (int i = 2; i < _curveType; i += 2) { 722 final float x1 = curve[i], y1 = curve[i+1]; 723 final float len = Helpers.linelen(x0, y0, x1, y1); 724 polyLen += len; 725 curLeafCtrlPolyLengths[(i >> 1) - 1] = len; 726 x0 = x1; 727 y0 = y1; 728 } 729 730 final float lineLen = Helpers.linelen(curve[0], curve[1], 731 curve[_curveType-2], 732 curve[_curveType-1]); 733 if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) { 734 return (polyLen + lineLen) / 2.0f; 735 } 736 return -1.0f; 737 } 738 } 739 740 @Override 741 public void curveTo(final float x1, final float y1, 742 final float x2, final float y2, 743 final float x3, final float y3) 744 { 745 final float[] _curCurvepts = curCurvepts; 746 _curCurvepts[0] = x0; _curCurvepts[1] = y0; 747 _curCurvepts[2] = x1; _curCurvepts[3] = y1; 748 _curCurvepts[4] = x2; _curCurvepts[5] = y2; 749 _curCurvepts[6] = x3; _curCurvepts[7] = y3; 750 somethingTo(8); 751 } 752 753 @Override 754 public void quadTo(final float x1, final float y1, 755 final float x2, final float y2) 756 { 757 final float[] _curCurvepts = curCurvepts; 758 _curCurvepts[0] = x0; _curCurvepts[1] = y0; 759 _curCurvepts[2] = x1; _curCurvepts[3] = y1; 760 _curCurvepts[4] = x2; _curCurvepts[5] = y2; 761 somethingTo(6); 762 } 763 764 @Override 765 public void closePath() { 766 lineTo(sx, sy); 767 if (firstSegidx != 0) { 768 if (!dashOn || needsMoveTo) { 769 out.moveTo(sx, sy); 770 } 771 emitFirstSegments(); 772 } 773 moveTo(sx, sy); 774 } 775 776 @Override 777 public void pathDone() { 778 if (firstSegidx != 0) { 779 out.moveTo(sx, sy); 780 emitFirstSegments(); 781 } 782 out.pathDone(); 783 784 // Dispose this instance: 785 dispose(); 786 } 787 788 @Override 789 public long getNativeConsumer() { 790 throw new InternalError("Dasher does not use a native consumer"); 791 } 792 } 793 | 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 sun.java2d.marlin; 27 28 import java.util.Arrays; 29 import sun.awt.geom.PathConsumer2D; 30 import sun.java2d.marlin.TransformingPathConsumer2D.CurveBasicMonotonizer; 31 import sun.java2d.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 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 Dasher init(final PathConsumer2D out, float[] dash, int dashLen, 142 float phase, boolean recycleDashes) 143 { 144 this.out = out; 145 146 // Normalize so 0 <= phase < dash[0] 147 int sidx = 0; 148 dashOn = true; 149 150 float sum = 0.0f; 151 for (float d : dash) { 152 sum += d; 153 } 154 this.cycleLen = sum; 155 156 float cycles = phase / sum; 157 if (phase < 0.0f) { 158 if (-cycles >= MAX_CYCLES) { 159 phase = 0.0f; 160 } else { 161 int fullcycles = FloatMath.floor_int(-cycles); 162 if ((fullcycles & dash.length & 1) != 0) { 163 dashOn = !dashOn; 164 } 165 phase += fullcycles * sum; 166 while (phase < 0.0f) { 167 if (--sidx < 0) { 168 sidx = dash.length - 1; 169 } 170 phase += dash[sidx]; 171 dashOn = !dashOn; 172 } 173 } 174 } else if (phase > 0.0f) { 175 if (cycles >= MAX_CYCLES) { 184 while (phase >= (d = dash[sidx])) { 185 phase -= d; 186 sidx = (sidx + 1) % dash.length; 187 dashOn = !dashOn; 188 } 189 } 190 } 191 192 this.dash = dash; 193 this.dashLen = dashLen; 194 this.phase = phase; 195 this.startPhase = phase; 196 this.startDashOn = dashOn; 197 this.startIdx = sidx; 198 this.starting = true; 199 this.needsMoveTo = false; 200 this.firstSegidx = 0; 201 202 this.recycleDashes = recycleDashes; 203 204 if (rdrCtx.doClip) { 205 this.clipRect = rdrCtx.clipRect; 206 } else { 207 this.clipRect = null; 208 this.cOutCode = 0; 209 } 210 return this; // fluent API 211 } 212 213 /** 214 * Disposes this dasher: 215 * clean up before reusing this instance 216 */ 217 void dispose() { 218 if (DO_CLEAN_DIRTY) { 219 // Force zero-fill dirty arrays: 220 Arrays.fill(curCurvepts, 0.0f); 221 } 222 // Return arrays: 223 if (recycleDashes) { 224 dash = dashes_ref.putArray(dash); 225 } 226 firstSegmentsBuffer = firstSegmentsBuffer_ref.putArray(firstSegmentsBuffer); 227 } 228 229 float[] copyDashArray(final float[] dashes) { 230 final int len = dashes.length; 231 final float[] newDashes; 232 if (len <= MarlinConst.INITIAL_ARRAY) { 233 newDashes = dashes_ref.initial; 234 } else { 235 if (DO_STATS) { 236 rdrCtx.stats.stat_array_dasher_dasher.add(len); 237 } 238 newDashes = dashes_ref.getArray(len); 239 } 240 System.arraycopy(dashes, 0, newDashes, 0, len); 241 return newDashes; 242 } 243 244 @Override 245 public void moveTo(final float x0, final float y0) { 246 if (firstSegidx != 0) { 247 out.moveTo(sx0, sy0); 248 emitFirstSegments(); 249 } 250 this.needsMoveTo = true; 251 this.idx = startIdx; 252 this.dashOn = this.startDashOn; 253 this.phase = this.startPhase; 254 this.cx0 = x0; 255 this.cy0 = y0; 256 257 // update starting point: 258 this.sx0 = x0; 259 this.sy0 = y0; 260 this.starting = true; 261 262 if (clipRect != null) { 263 final int outcode = Helpers.outcode(x0, y0, clipRect); 264 this.cOutCode = outcode; 265 this.outside = false; 266 this.totalSkipLen = 0.0f; 267 } 268 } 269 270 private void emitSeg(float[] buf, int off, int type) { 271 switch (type) { 272 case 8: 273 out.curveTo(buf[off ], buf[off + 1], 274 buf[off + 2], buf[off + 3], 275 buf[off + 4], buf[off + 5]); 276 return; 277 case 6: 278 out.quadTo(buf[off ], buf[off + 1], 279 buf[off + 2], buf[off + 3]); 280 return; 281 case 4: 282 out.lineTo(buf[off], buf[off + 1]); 283 return; 284 default: 285 } 286 } 287 288 private void emitFirstSegments() { 289 final float[] fSegBuf = firstSegmentsBuffer; 290 291 for (int i = 0, len = firstSegidx; i < len; ) { 292 int type = (int)fSegBuf[i]; 293 emitSeg(fSegBuf, i + 1, type); 294 i += (type - 1); 295 } 296 firstSegidx = 0; 297 } 298 299 // precondition: pts must be in relative coordinates (relative to x0,y0) 300 private void goTo(final float[] pts, final int off, final int type, 301 final boolean on) 302 { 303 final int index = off + type; 304 final float x = pts[index - 4]; 305 final float y = pts[index - 3]; 306 307 if (on) { 308 if (starting) { 309 goTo_starting(pts, off, type); 310 } else { 311 if (needsMoveTo) { 312 needsMoveTo = false; 313 out.moveTo(cx0, cy0); 314 } 315 emitSeg(pts, off, type); 316 } 317 } else { 318 if (starting) { 319 // low probability test (hotspot) 320 starting = false; 321 } 322 needsMoveTo = true; 323 } 324 this.cx0 = x; 325 this.cy0 = y; 326 } 327 328 private void goTo_starting(final float[] pts, final int off, final int type) { 329 int len = type - 1; // - 2 + 1 330 int segIdx = firstSegidx; 331 float[] buf = firstSegmentsBuffer; 332 333 if (segIdx + len > buf.length) { 334 if (DO_STATS) { 335 rdrCtx.stats.stat_array_dasher_firstSegmentsBuffer 336 .add(segIdx + len); 337 } 338 firstSegmentsBuffer = buf 339 = firstSegmentsBuffer_ref.widenArray(buf, segIdx, 340 segIdx + len); 341 } 342 buf[segIdx++] = type; 343 len--; 344 // small arraycopy (2, 4 or 6) but with offset: 345 System.arraycopy(pts, off, buf, segIdx, len); 346 firstSegidx = segIdx + len; 347 } 348 349 @Override 350 public void lineTo(final float x1, final float y1) { 351 final int outcode0 = this.cOutCode; 352 353 if (clipRect != null) { 354 final int outcode1 = Helpers.outcode(x1, y1, clipRect); 355 356 // Should clip 357 final int orCode = (outcode0 | outcode1); 358 359 if (orCode != 0) { 360 final int sideCode = outcode0 & outcode1; 361 362 // basic rejection criteria: 363 if (sideCode == 0) { 364 // ovelap clip: 365 if (subdivide) { 366 // avoid reentrance 367 subdivide = false; 368 // subdivide curve => callback with subdivided parts: 369 boolean ret = curveSplitter.splitLine(cx0, cy0, x1, y1, 370 orCode, this); 371 // reentrance is done: 372 subdivide = true; 373 if (ret) { 374 return; 375 } 376 } 377 // already subdivided so render it 378 } else { 379 this.cOutCode = outcode1; 380 skipLineTo(x1, y1); 381 return; 382 } 383 } 384 385 this.cOutCode = outcode1; 386 387 if (this.outside) { 388 this.outside = false; 389 // Adjust current index, phase & dash: 390 skipLen(); 391 } 392 } 393 _lineTo(x1, y1); 394 } 395 396 private void _lineTo(final float x1, final float y1) { 397 final float dx = x1 - cx0; 398 final float dy = y1 - cy0; 399 400 float len = dx * dx + dy * dy; 401 if (len == 0.0f) { 402 return; 403 } 404 len = (float) Math.sqrt(len); 405 406 // The scaling factors needed to get the dx and dy of the 407 // transformed dash segments. 408 final float cx = dx / len; 409 final float cy = dy / len; 410 411 final float[] _curCurvepts = curCurvepts; 412 final float[] _dash = dash; 413 final int _dashLen = this.dashLen; 414 415 int _idx = idx; 416 boolean _dashOn = dashOn; 417 float _phase = phase; 418 419 float leftInThisDashSegment, d; 420 421 while (true) { 422 d = _dash[_idx]; 423 leftInThisDashSegment = d - _phase; 424 425 if (len <= leftInThisDashSegment) { 426 _curCurvepts[0] = x1; 427 _curCurvepts[1] = y1; 428 429 goTo(_curCurvepts, 0, 4, _dashOn); 430 431 // Advance phase within current dash segment 432 _phase += len; 433 434 // TODO: compare float values using epsilon: 435 if (len == leftInThisDashSegment) { 436 _phase = 0.0f; 437 _idx = (_idx + 1) % _dashLen; 438 _dashOn = !_dashOn; 439 } 440 break; 441 } 442 443 if (_phase == 0.0f) { 444 _curCurvepts[0] = cx0 + d * cx; 445 _curCurvepts[1] = cy0 + d * cy; 446 } else { 447 _curCurvepts[0] = cx0 + leftInThisDashSegment * cx; 448 _curCurvepts[1] = cy0 + leftInThisDashSegment * cy; 449 } 450 451 goTo(_curCurvepts, 0, 4, _dashOn); 452 453 len -= leftInThisDashSegment; 454 // Advance to next dash segment 455 _idx = (_idx + 1) % _dashLen; 456 _dashOn = !_dashOn; 457 _phase = 0.0f; 458 } 459 // Save local state: 460 idx = _idx; 461 dashOn = _dashOn; 462 phase = _phase; 463 } 464 465 private void skipLineTo(final float x1, final float y1) { 466 final float dx = x1 - cx0; 467 final float dy = y1 - cy0; 468 469 float len = dx * dx + dy * dy; 470 if (len != 0.0f) { 471 len = (float)Math.sqrt(len); 472 } 473 474 // Accumulate skipped length: 475 this.outside = true; 476 this.totalSkipLen += len; 477 478 // Fix initial move: 479 this.needsMoveTo = true; 480 this.starting = false; 481 482 this.cx0 = x1; 483 this.cy0 = y1; 484 } 485 486 public void skipLen() { 487 float len = this.totalSkipLen; 488 this.totalSkipLen = 0.0f; 489 490 final float[] _dash = dash; 491 final int _dashLen = this.dashLen; 492 493 int _idx = idx; 494 boolean _dashOn = dashOn; 495 float _phase = phase; 496 497 // -2 to ensure having 2 iterations of the post-loop 498 // to compensate the remaining phase 499 final long fullcycles = (long)Math.floor(len / cycleLen) - 2L; 500 501 if (fullcycles > 0L) { 502 len -= cycleLen * fullcycles; 503 504 final long iterations = fullcycles * _dashLen; 505 _idx = (int) (iterations + _idx) % _dashLen; 506 _dashOn = (iterations + (_dashOn ? 1L : 0L) & 1L) == 1L; 507 } 508 509 float leftInThisDashSegment, d; 510 511 while (true) { 512 d = _dash[_idx]; 513 leftInThisDashSegment = d - _phase; 514 515 if (len <= leftInThisDashSegment) { 516 // Advance phase within current dash segment 517 _phase += len; 518 519 // TODO: compare float values using epsilon: 520 if (len == leftInThisDashSegment) { 521 _phase = 0.0f; 522 _idx = (_idx + 1) % _dashLen; 523 _dashOn = !_dashOn; 524 } 525 break; 526 } 527 528 len -= leftInThisDashSegment; 529 // Advance to next dash segment 530 _idx = (_idx + 1) % _dashLen; 531 _dashOn = !_dashOn; 532 _phase = 0.0f; 533 } 534 // Save local state: 535 idx = _idx; 536 dashOn = _dashOn; 537 phase = _phase; 538 } 539 540 // preconditions: curCurvepts must be an array of length at least 2 * type, 541 // that contains the curve we want to dash in the first type elements 542 private void somethingTo(final int type) { 543 final float[] _curCurvepts = curCurvepts; 544 if (pointCurve(_curCurvepts, type)) { 545 return; 546 } 547 final LengthIterator _li = li; 548 final float[] _dash = dash; 549 final int _dashLen = this.dashLen; 550 551 _li.initializeIterationOnCurve(_curCurvepts, type); 552 553 int _idx = idx; 554 boolean _dashOn = dashOn; 555 float _phase = phase; 556 557 // initially the current curve is at curCurvepts[0...type] 558 int curCurveoff = 0; 559 float prevT = 0.0f; 560 float t; 561 float leftInThisDashSegment = _dash[_idx] - _phase; 562 563 while ((t = _li.next(leftInThisDashSegment)) < 1.0f) { 564 if (t != 0.0f) { 565 Helpers.subdivideAt((t - prevT) / (1.0f - prevT), 566 _curCurvepts, curCurveoff, 567 _curCurvepts, 0, type); 568 prevT = t; 569 goTo(_curCurvepts, 2, type, _dashOn); 570 curCurveoff = type; 571 } 572 // Advance to next dash segment 573 _idx = (_idx + 1) % _dashLen; 574 _dashOn = !_dashOn; 575 _phase = 0.0f; 576 leftInThisDashSegment = _dash[_idx]; 577 } 578 579 goTo(_curCurvepts, curCurveoff + 2, type, _dashOn); 580 581 _phase += _li.lastSegLen(); 582 if (_phase >= _dash[_idx]) { 583 _phase = 0.0f; 584 _idx = (_idx + 1) % _dashLen; 585 _dashOn = !_dashOn; 586 } 587 // Save local state: 588 idx = _idx; 589 dashOn = _dashOn; 590 phase = _phase; 591 592 // reset LengthIterator: 593 _li.reset(); 594 } 595 596 private void skipSomethingTo(final int type) { 597 final float[] _curCurvepts = curCurvepts; 598 if (pointCurve(_curCurvepts, type)) { 599 return; 600 } 601 final LengthIterator _li = li; 602 603 _li.initializeIterationOnCurve(_curCurvepts, type); 604 605 // In contrary to somethingTo(), 606 // just estimate properly the curve length: 607 final float len = _li.totalLength(); 608 609 // Accumulate skipped length: 610 this.outside = true; 611 this.totalSkipLen += len; 612 613 // Fix initial move: 614 this.needsMoveTo = true; 615 this.starting = false; 616 } 617 618 private static boolean pointCurve(final float[] curve, final int type) { 619 for (int i = 2; i < type; i++) { 620 if (curve[i] != curve[i-2]) { 621 return false; 622 } 623 } 624 return true; 625 } 626 627 // Objects of this class are used to iterate through curves. They return 628 // t values where the left side of the curve has a specified length. 629 // It does this by subdividing the input curve until a certain error 630 // condition has been met. A recursive subdivision procedure would 631 // return as many as 1<<limit curves, but this is an iterator and we 632 // don't need all the curves all at once, so what we carry out a 633 // lazy inorder traversal of the recursion tree (meaning we only move 634 // through the tree when we need the next subdivided curve). This saves 635 // us a lot of memory because at any one time we only need to store 636 // limit+1 curves - one for each level of the tree + 1. 637 // NOTE: the way we do things here is not enough to traverse a general 638 // tree; however, the trees we are interested in have the property that 639 // every non leaf node has exactly 2 children 640 static final class LengthIterator { 641 // Holds the curves at various levels of the recursion. The root 642 // (i.e. the original curve) is at recCurveStack[0] (but then it 643 // gets subdivided, the left half is put at 1, so most of the time 644 // only the right half of the original curve is at 0) 645 private final float[][] recCurveStack; // dirty 646 // sidesRight[i] indicates whether the node at level i+1 in the path from 647 // the root to the current leaf is a left or right child of its parent. 648 private final boolean[] sidesRight; // dirty 649 private int curveType; 650 // lastT and nextT delimit the current leaf. 651 private float nextT; 652 private float lenAtNextT; 653 private float lastT; 654 private float lenAtLastT; 655 private float lenAtLastSplit; 656 private float lastSegLen; 657 // the current level in the recursion tree. 0 is the root. limit 658 // is the deepest possible leaf. 659 private int recLevel; 660 private boolean done; 661 662 // the lengths of the lines of the control polygon. Only its first 663 // curveType/2 - 1 elements are valid. This is an optimization. See 664 // next() for more detail. 665 private final float[] curLeafCtrlPolyLengths = new float[3]; 666 667 LengthIterator() { 668 this.recCurveStack = new float[REC_LIMIT + 1][8]; 669 this.sidesRight = new boolean[REC_LIMIT]; 670 // if any methods are called without first initializing this object 671 // on a curve, we want it to fail ASAP. 672 this.nextT = Float.MAX_VALUE; 673 this.lenAtNextT = Float.MAX_VALUE; 674 this.lenAtLastSplit = Float.MIN_VALUE; 675 this.recLevel = Integer.MIN_VALUE; 676 this.lastSegLen = Float.MAX_VALUE; 677 this.done = true; 678 } 679 680 /** 681 * Reset this LengthIterator. 682 */ 683 void reset() { 684 // keep data dirty 685 // as it appears not useful to reset data: 686 if (DO_CLEAN_DIRTY) { 687 final int recLimit = recCurveStack.length - 1; 688 for (int i = recLimit; i >= 0; i--) { 689 Arrays.fill(recCurveStack[i], 0.0f); 690 } 691 Arrays.fill(sidesRight, false); 692 Arrays.fill(curLeafCtrlPolyLengths, 0.0f); 693 Arrays.fill(nextRoots, 0.0f); 694 Arrays.fill(flatLeafCoefCache, 0.0f); 695 flatLeafCoefCache[2] = -1.0f; 696 } 697 } 698 699 void initializeIterationOnCurve(final float[] pts, final int type) { 700 // optimize arraycopy (8 values faster than 6 = type): 701 System.arraycopy(pts, 0, recCurveStack[0], 0, 8); 702 this.curveType = type; 703 this.recLevel = 0; 704 this.lastT = 0.0f; 705 this.lenAtLastT = 0.0f; 706 this.nextT = 0.0f; 707 this.lenAtNextT = 0.0f; 708 goLeft(); // initializes nextT and lenAtNextT properly 709 this.lenAtLastSplit = 0.0f; 710 if (recLevel > 0) { 711 this.sidesRight[0] = false; 712 this.done = false; 713 } else { 714 // the root of the tree is a leaf so we're done. 715 this.sidesRight[0] = true; 716 this.done = true; 717 } 718 this.lastSegLen = 0.0f; 719 } 720 721 // 0 == false, 1 == true, -1 == invalid cached value. 722 private int cachedHaveLowAcceleration = -1; 723 724 private boolean haveLowAcceleration(final float err) { 725 if (cachedHaveLowAcceleration == -1) { 726 final float len1 = curLeafCtrlPolyLengths[0]; 727 final float len2 = curLeafCtrlPolyLengths[1]; 728 // the test below is equivalent to !within(len1/len2, 1, err). 729 // It is using a multiplication instead of a division, so it 730 // should be a bit faster. 731 if (!Helpers.within(len1, len2, err * len2)) { 732 cachedHaveLowAcceleration = 0; 733 return false; 734 } 735 if (curveType == 8) { 736 final float len3 = curLeafCtrlPolyLengths[2]; 737 // if len1 is close to 2 and 2 is close to 3, that probably 738 // means 1 is close to 3 so the second part of this test might 739 // not be needed, but it doesn't hurt to include it. 740 final float errLen3 = err * len3; 741 if (!(Helpers.within(len2, len3, errLen3) && 742 Helpers.within(len1, len3, errLen3))) { 743 cachedHaveLowAcceleration = 0; 744 return false; 793 float z = y + curLeafCtrlPolyLengths[2]; 794 _flatLeafCoefCache[0] = 3.0f * (x - y) + z; 795 _flatLeafCoefCache[1] = 3.0f * (y - 2.0f * x); 796 _flatLeafCoefCache[2] = 3.0f * x; 797 _flatLeafCoefCache[3] = -z; 798 } else if (curveType == 6) { 799 _flatLeafCoefCache[0] = 0.0f; 800 _flatLeafCoefCache[1] = y - 2.0f * x; 801 _flatLeafCoefCache[2] = 2.0f * x; 802 _flatLeafCoefCache[3] = -y; 803 } 804 } 805 float a = _flatLeafCoefCache[0]; 806 float b = _flatLeafCoefCache[1]; 807 float c = _flatLeafCoefCache[2]; 808 float d = t * _flatLeafCoefCache[3]; 809 810 // we use cubicRootsInAB here, because we want only roots in 0, 1, 811 // and our quadratic root finder doesn't filter, so it's just a 812 // matter of convenience. 813 final int n = Helpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0.0f, 1.0f); 814 // TODO: check NaN is impossible 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 @Override 1121 public long getNativeConsumer() { 1122 throw new InternalError("Dasher does not use a native consumer"); 1123 } 1124 } 1125 |