< prev index next >

modules/javafx.graphics/src/main/java/com/sun/marlin/DDasher.java

Print this page




 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) {
 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.startPhase = this.phase = phase;

 161         this.startDashOn = dashOn;
 162         this.startIdx = sidx;
 163         this.starting = true;
 164         needsMoveTo = false;
 165         firstSegidx = 0;
 166 
 167         this.recycleDashes = recycleDashes;
 168 
 169         return this; // fluent API
 170     }
 171 
 172     /**
 173      * Disposes this dasher:
 174      * clean up before reusing this instance
 175      */
 176     void dispose() {
 177         if (DO_CLEAN_DIRTY) {
 178             // Force zero-fill dirty arrays:
 179             Arrays.fill(curCurvepts, 0.0d);
 180         }
 181         // Return arrays:
 182         if (recycleDashes) {
 183             dash = dashes_ref.putArray(dash);
 184         }
 185         firstSegmentsBuffer = firstSegmentsBuffer_ref.putArray(firstSegmentsBuffer);
 186     }
 187 
 188     public double[] copyDashArray(final float[] dashes) {
 189         final int len = dashes.length;
 190         final double[] newDashes;
 191         if (len <= MarlinConst.INITIAL_ARRAY) {
 192             newDashes = dashes_ref.initial;
 193         } else {
 194             if (DO_STATS) {
 195                 rdrCtx.stats.stat_array_dasher_dasher.add(len);
 196             }
 197             newDashes = dashes_ref.getArray(len);
 198         }
 199         for (int i = 0; i < len; i++) { newDashes[i] = dashes[i]; }
 200         return newDashes;
 201     }
 202 
 203     @Override
 204     public void moveTo(double x0, double y0) {
 205         if (firstSegidx > 0) {
 206             out.moveTo(sx, sy);
 207             emitFirstSegments();
 208         }
 209         needsMoveTo = true;
 210         this.idx = startIdx;
 211         this.dashOn = this.startDashOn;
 212         this.phase = this.startPhase;
 213         this.sx = this.x0 = x0;
 214         this.sy = this.y0 = y0;


 215         this.starting = true;
 216     }
 217 
 218     private void emitSeg(double[] buf, int off, int type) {
 219         switch (type) {
 220         case 8:
 221             out.curveTo(buf[off+0], buf[off+1],
 222                         buf[off+2], buf[off+3],
 223                         buf[off+4], buf[off+5]);
 224             return;
 225         case 6:
 226             out.quadTo(buf[off+0], buf[off+1],
 227                        buf[off+2], buf[off+3]);
 228             return;
 229         case 4:
 230             out.lineTo(buf[off], buf[off+1]);
 231             return;
 232         default:
 233         }
 234     }
 235 
 236     private void emitFirstSegments() {
 237         final double[] fSegBuf = firstSegmentsBuffer;
 238 
 239         for (int i = 0; i < firstSegidx; ) {
 240             int type = (int)fSegBuf[i];
 241             emitSeg(fSegBuf, i + 1, type);
 242             i += (type - 1);
 243         }
 244         firstSegidx = 0;
 245     }
 246     // We don't emit the first dash right away. If we did, caps would be
 247     // drawn on it, but we need joins to be drawn if there's a closePath()
 248     // So, we store the path elements that make up the first dash in the
 249     // buffer below.
 250     private double[] firstSegmentsBuffer; // dynamic array
 251     private int firstSegidx;
 252 
 253     // precondition: pts must be in relative coordinates (relative to x0,y0)
 254     private void goTo(double[] pts, int off, final int type) {
 255         double x = pts[off + type - 4];
 256         double y = pts[off + type - 3];
 257         if (dashOn) {


 258             if (starting) {
 259                 int len = type - 1; // - 2 + 1
 260                 int segIdx = firstSegidx;
 261                 double[] buf = firstSegmentsBuffer;
 262                 if (segIdx + len  > buf.length) {
 263                     if (DO_STATS) {
 264                         rdrCtx.stats.stat_array_dasher_firstSegmentsBuffer
 265                             .add(segIdx + len);
 266                     }
 267                     firstSegmentsBuffer = buf
 268                         = firstSegmentsBuffer_ref.widenArray(buf, segIdx,
 269                                                              segIdx + len);
 270                 }
 271                 buf[segIdx++] = type;
 272                 len--;
 273                 // small arraycopy (2, 4 or 6) but with offset:
 274                 System.arraycopy(pts, off, buf, segIdx, len);
 275                 segIdx += len;
 276                 firstSegidx = segIdx;
 277             } else {
 278                 if (needsMoveTo) {
 279                     out.moveTo(x0, y0);
 280                     needsMoveTo = false;

 281                 }
 282                 emitSeg(pts, off, type);
 283             }
 284         } else {
 285             starting = false;



 286             needsMoveTo = true;
 287         }
 288         this.x0 = x;
 289         this.y0 = y;
 290     }
 291 





















 292     @Override
 293     public void lineTo(double x1, double y1) {
 294         double dx = x1 - x0;
 295         double dy = y1 - y0;
 296 
 297         double len = dx*dx + dy*dy;
 298         if (len == 0.0d) {
 299             return;
 300         }
 301         len = Math.sqrt(len);
 302 
 303         // The scaling factors needed to get the dx and dy of the
 304         // transformed dash segments.
 305         final double cx = dx / len;
 306         final double cy = dy / len;
 307 
 308         final double[] _curCurvepts = curCurvepts;
 309         final double[] _dash = dash;





 310 
 311         double leftInThisDashSegment;
 312         double dashdx, dashdy, p;
 313 
 314         while (true) {
 315             leftInThisDashSegment = _dash[idx] - phase;

 316 
 317             if (len <= leftInThisDashSegment) {
 318                 _curCurvepts[0] = x1;
 319                 _curCurvepts[1] = y1;
 320                 goTo(_curCurvepts, 0, 4);

 321 
 322                 // Advance phase within current dash segment
 323                 phase += len;

 324                 // TODO: compare double values using epsilon:
 325                 if (len == leftInThisDashSegment) {
 326                     phase = 0.0d;
 327                     idx = (idx + 1) % dashLen;
 328                     dashOn = !dashOn;
 329                 }





 330                 return;
 331             }
 332 
 333             dashdx = _dash[idx] * cx;
 334             dashdy = _dash[idx] * cy;
 335 
 336             if (phase == 0.0d) {
 337                 _curCurvepts[0] = x0 + dashdx;
 338                 _curCurvepts[1] = y0 + dashdy;
 339             } else {
 340                 p = leftInThisDashSegment / _dash[idx];
 341                 _curCurvepts[0] = x0 + p * dashdx;
 342                 _curCurvepts[1] = y0 + p * dashdy;
 343             }
 344 
 345             goTo(_curCurvepts, 0, 4);
 346 
 347             len -= leftInThisDashSegment;
 348             // Advance to next dash segment
 349             idx = (idx + 1) % dashLen;
 350             dashOn = !dashOn;
 351             phase = 0.0d;
 352         }
 353     }
 354 
 355     // shared instance in DDasher
 356     private final LengthIterator li = new LengthIterator();
 357 
 358     // preconditions: curCurvepts must be an array of length at least 2 * type,
 359     // that contains the curve we want to dash in the first type elements
 360     private void somethingTo(int type) {
 361         if (pointCurve(curCurvepts, type)) {
 362             return;
 363         }
 364         li.initializeIterationOnCurve(curCurvepts, type);









 365 
 366         // initially the current curve is at curCurvepts[0...type]
 367         int curCurveoff = 0;
 368         double lastSplitT = 0.0d;
 369         double t;
 370         double leftInThisDashSegment = dash[idx] - phase;
 371 
 372         while ((t = li.next(leftInThisDashSegment)) < 1.0d) {
 373             if (t != 0.0d) {
 374                 DHelpers.subdivideAt((t - lastSplitT) / (1.0d - lastSplitT),
 375                                     curCurvepts, curCurveoff,
 376                                     curCurvepts, 0,
 377                                     curCurvepts, type, type);
 378                 lastSplitT = t;
 379                 goTo(curCurvepts, 2, type);
 380                 curCurveoff = type;
 381             }
 382             // Advance to next dash segment
 383             idx = (idx + 1) % dashLen;
 384             dashOn = !dashOn;
 385             phase = 0.0d;
 386             leftInThisDashSegment = dash[idx];
 387         }
 388         goTo(curCurvepts, curCurveoff+2, type);
 389         phase += li.lastSegLen();
 390         if (phase >= dash[idx]) {
 391             phase = 0.0d;
 392             idx = (idx + 1) % dashLen;
 393             dashOn = !dashOn;
 394         }




 395         // reset LengthIterator:
 396         li.reset();
 397     }
 398 
 399     private static boolean pointCurve(double[] curve, int type) {
 400         for (int i = 2; i < type; i++) {
 401             if (curve[i] != curve[i-2]) {
 402                 return false;
 403             }
 404         }
 405         return true;
 406     }
 407 
 408     // Objects of this class are used to iterate through curves. They return
 409     // t values where the left side of the curve has a specified length.
 410     // It does this by subdividing the input curve until a certain error
 411     // condition has been met. A recursive subdivision procedure would
 412     // return as many as 1<<limit curves, but this is an iterator and we
 413     // don't need all the curves all at once, so what we carry out a
 414     // lazy inorder traversal of the recursion tree (meaning we only move
 415     // through the tree when we need the next subdivided curve). This saves
 416     // us a lot of memory because at any one time we only need to store
 417     // limit+1 curves - one for each level of the tree + 1.
 418     // NOTE: the way we do things here is not enough to traverse a general
 419     // tree; however, the trees we are interested in have the property that
 420     // every non leaf node has exactly 2 children
 421     static final class LengthIterator {
 422         private enum Side {LEFT, RIGHT};
 423         // Holds the curves at various levels of the recursion. The root
 424         // (i.e. the original curve) is at recCurveStack[0] (but then it
 425         // gets subdivided, the left half is put at 1, so most of the time
 426         // only the right half of the original curve is at 0)
 427         private final double[][] recCurveStack; // dirty
 428         // sides[i] indicates whether the node at level i+1 in the path from
 429         // the root to the current leaf is a left or right child of its parent.
 430         private final Side[] sides; // dirty
 431         private int curveType;
 432         // lastT and nextT delimit the current leaf.
 433         private double nextT;
 434         private double lenAtNextT;
 435         private double lastT;
 436         private double lenAtLastT;
 437         private double lenAtLastSplit;
 438         private double lastSegLen;
 439         // the current level in the recursion tree. 0 is the root. limit
 440         // is the deepest possible leaf.
 441         private int recLevel;
 442         private boolean done;


 652                 lastT = nextT;
 653                 lenAtLastT = lenAtNextT;
 654                 nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC;
 655                 lenAtNextT += len;
 656                 // invalidate caches
 657                 flatLeafCoefCache[2] = -1.0d;
 658                 cachedHaveLowAcceleration = -1;
 659             } else {
 660                 DHelpers.subdivide(recCurveStack[recLevel], 0,
 661                                   recCurveStack[recLevel+1], 0,
 662                                   recCurveStack[recLevel], 0, curveType);
 663                 sides[recLevel] = Side.LEFT;
 664                 recLevel++;
 665                 goLeft();
 666             }
 667         }
 668 
 669         // this is a bit of a hack. It returns -1 if we're not on a leaf, and
 670         // the length of the leaf if we are on a leaf.
 671         private double onLeaf() {
 672             double[] curve = recCurveStack[recLevel];

 673             double polyLen = 0.0d;
 674 
 675             double x0 = curve[0], y0 = curve[1];
 676             for (int i = 2; i < curveType; i += 2) {
 677                 final double x1 = curve[i], y1 = curve[i+1];
 678                 final double len = DHelpers.linelen(x0, y0, x1, y1);
 679                 polyLen += len;
 680                 curLeafCtrlPolyLengths[i/2 - 1] = len;
 681                 x0 = x1;
 682                 y0 = y1;
 683             }
 684 
 685             final double lineLen = DHelpers.linelen(curve[0], curve[1],
 686                                                   curve[curveType-2],
 687                                                   curve[curveType-1]);
 688             if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) {
 689                 return (polyLen + lineLen) / 2.0d;
 690             }
 691             return -1.0d;
 692         }
 693     }
 694 
 695     @Override
 696     public void curveTo(double x1, double y1,
 697                         double x2, double y2,
 698                         double x3, double y3)
 699     {
 700         final double[] _curCurvepts = curCurvepts;
 701         _curCurvepts[0] = x0;        _curCurvepts[1] = y0;
 702         _curCurvepts[2] = x1;        _curCurvepts[3] = y1;
 703         _curCurvepts[4] = x2;        _curCurvepts[5] = y2;
 704         _curCurvepts[6] = x3;        _curCurvepts[7] = y3;
 705         somethingTo(8);
 706     }
 707 
 708     @Override
 709     public void quadTo(double x1, double y1, double x2, double y2) {
 710         final double[] _curCurvepts = curCurvepts;
 711         _curCurvepts[0] = x0;        _curCurvepts[1] = y0;
 712         _curCurvepts[2] = x1;        _curCurvepts[3] = y1;
 713         _curCurvepts[4] = x2;        _curCurvepts[5] = y2;
 714         somethingTo(6);
 715     }
 716 
 717     @Override
 718     public void closePath() {
 719         lineTo(sx, sy);
 720         if (firstSegidx > 0) {
 721             if (!dashOn || needsMoveTo) {
 722                 out.moveTo(sx, sy);
 723             }
 724             emitFirstSegments();
 725         }
 726         moveTo(sx, sy);
 727     }
 728 
 729     @Override
 730     public void pathDone() {
 731         if (firstSegidx > 0) {
 732             out.moveTo(sx, sy);
 733             emitFirstSegments();
 734         }
 735         out.pathDone();
 736 
 737         // Dispose this instance:
 738         dispose();
 739     }
 740 }
 741 


 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;


 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 
< prev index next >