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