Path_Delegate.java revision 34b16b854ae8c78554a75b136a1df403c385f2e9
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19import com.android.ide.common.rendering.api.LayoutLog;
20import com.android.layoutlib.bridge.Bridge;
21import com.android.layoutlib.bridge.impl.DelegateManager;
22import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
23
24import android.graphics.Path.Direction;
25import android.graphics.Path.FillType;
26
27import java.awt.Shape;
28import java.awt.geom.AffineTransform;
29import java.awt.geom.Arc2D;
30import java.awt.geom.Area;
31import java.awt.geom.Ellipse2D;
32import java.awt.geom.GeneralPath;
33import java.awt.geom.PathIterator;
34import java.awt.geom.Point2D;
35import java.awt.geom.Rectangle2D;
36import java.awt.geom.RoundRectangle2D;
37
38/**
39 * Delegate implementing the native methods of android.graphics.Path
40 *
41 * Through the layoutlib_create tool, the original native methods of Path have been replaced
42 * by calls to methods of the same name in this delegate class.
43 *
44 * This class behaves like the original native implementation, but in Java, keeping previously
45 * native data into its own objects and mapping them to int that are sent back and forth between
46 * it and the original Path class.
47 *
48 * @see DelegateManager
49 *
50 */
51public final class Path_Delegate {
52
53    // ---- delegate manager ----
54    private static final DelegateManager<Path_Delegate> sManager =
55            new DelegateManager<Path_Delegate>(Path_Delegate.class);
56
57    // ---- delegate data ----
58    private FillType mFillType = FillType.WINDING;
59    private GeneralPath mPath = new GeneralPath();
60
61    private float mLastX = 0;
62    private float mLastY = 0;
63
64    // ---- Public Helper methods ----
65
66    public static Path_Delegate getDelegate(long nPath) {
67        return sManager.getDelegate(nPath);
68    }
69
70    public Shape getJavaShape() {
71        return mPath;
72    }
73
74    public void setJavaShape(Shape shape) {
75        mPath.reset();
76        mPath.append(shape, false /*connect*/);
77    }
78
79    public void reset() {
80        mPath.reset();
81    }
82
83    public void setPathIterator(PathIterator iterator) {
84        mPath.reset();
85        mPath.append(iterator, false /*connect*/);
86    }
87
88    // ---- native methods ----
89
90    @LayoutlibDelegate
91    /*package*/ static long init1() {
92        // create the delegate
93        Path_Delegate newDelegate = new Path_Delegate();
94
95        return sManager.addNewDelegate(newDelegate);
96    }
97
98    @LayoutlibDelegate
99    /*package*/ static long init2(long nPath) {
100        // create the delegate
101        Path_Delegate newDelegate = new Path_Delegate();
102
103        // get the delegate to copy, which could be null if nPath is 0
104        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
105        if (pathDelegate != null) {
106            newDelegate.set(pathDelegate);
107        }
108
109        return sManager.addNewDelegate(newDelegate);
110    }
111
112    @LayoutlibDelegate
113    /*package*/ static void native_reset(long nPath) {
114        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
115        if (pathDelegate == null) {
116            return;
117        }
118
119        pathDelegate.mPath.reset();
120    }
121
122    @LayoutlibDelegate
123    /*package*/ static void native_rewind(long nPath) {
124        // call out to reset since there's nothing to optimize in
125        // terms of data structs.
126        native_reset(nPath);
127    }
128
129    @LayoutlibDelegate
130    /*package*/ static void native_set(long native_dst, long native_src) {
131        Path_Delegate pathDstDelegate = sManager.getDelegate(native_dst);
132        if (pathDstDelegate == null) {
133            return;
134        }
135
136        Path_Delegate pathSrcDelegate = sManager.getDelegate(native_src);
137        if (pathSrcDelegate == null) {
138            return;
139        }
140
141        pathDstDelegate.set(pathSrcDelegate);
142    }
143
144    @LayoutlibDelegate
145    /*package*/ static long native_getFillType(long nPath) {
146        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
147        if (pathDelegate == null) {
148            return 0;
149        }
150
151        return pathDelegate.mFillType.nativeInt;
152    }
153
154    @LayoutlibDelegate
155    /*package*/ static void native_setFillType(long nPath, int ft) {
156        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
157        if (pathDelegate == null) {
158            return;
159        }
160
161        pathDelegate.mFillType = Path.sFillTypeArray[ft];
162    }
163
164    @LayoutlibDelegate
165    /*package*/ static boolean native_isEmpty(long nPath) {
166        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
167        if (pathDelegate == null) {
168            return true;
169        }
170
171        return pathDelegate.isEmpty();
172    }
173
174    @LayoutlibDelegate
175    /*package*/ static boolean native_isRect(long nPath, RectF rect) {
176        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
177        if (pathDelegate == null) {
178            return false;
179        }
180
181        // create an Area that can test if the path is a rect
182        Area area = new Area(pathDelegate.mPath);
183        if (area.isRectangular()) {
184            if (rect != null) {
185                pathDelegate.fillBounds(rect);
186            }
187
188            return true;
189        }
190
191        return false;
192    }
193
194    @LayoutlibDelegate
195    /*package*/ static void native_computeBounds(long nPath, RectF bounds) {
196        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
197        if (pathDelegate == null) {
198            return;
199        }
200
201        pathDelegate.fillBounds(bounds);
202    }
203
204    @LayoutlibDelegate
205    /*package*/ static void native_incReserve(long nPath, int extraPtCount) {
206        // since we use a java2D path, there's no way to pre-allocate new points,
207        // so we do nothing.
208    }
209
210    @LayoutlibDelegate
211    /*package*/ static void native_moveTo(long nPath, float x, float y) {
212        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
213        if (pathDelegate == null) {
214            return;
215        }
216
217        pathDelegate.moveTo(x, y);
218    }
219
220    @LayoutlibDelegate
221    /*package*/ static void native_rMoveTo(long nPath, float dx, float dy) {
222        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
223        if (pathDelegate == null) {
224            return;
225        }
226
227        pathDelegate.rMoveTo(dx, dy);
228    }
229
230    @LayoutlibDelegate
231    /*package*/ static void native_lineTo(long nPath, float x, float y) {
232        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
233        if (pathDelegate == null) {
234            return;
235        }
236
237        pathDelegate.lineTo(x, y);
238    }
239
240    @LayoutlibDelegate
241    /*package*/ static void native_rLineTo(long nPath, float dx, float dy) {
242        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
243        if (pathDelegate == null) {
244            return;
245        }
246
247        pathDelegate.rLineTo(dx, dy);
248    }
249
250    @LayoutlibDelegate
251    /*package*/ static void native_quadTo(long nPath, float x1, float y1, float x2, float y2) {
252        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
253        if (pathDelegate == null) {
254            return;
255        }
256
257        pathDelegate.quadTo(x1, y1, x2, y2);
258    }
259
260    @LayoutlibDelegate
261    /*package*/ static void native_rQuadTo(long nPath, float dx1, float dy1, float dx2, float dy2) {
262        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
263        if (pathDelegate == null) {
264            return;
265        }
266
267        pathDelegate.rQuadTo(dx1, dy1, dx2, dy2);
268    }
269
270    @LayoutlibDelegate
271    /*package*/ static void native_cubicTo(long nPath, float x1, float y1,
272            float x2, float y2, float x3, float y3) {
273        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
274        if (pathDelegate == null) {
275            return;
276        }
277
278        pathDelegate.cubicTo(x1, y1, x2, y2, x3, y3);
279    }
280
281    @LayoutlibDelegate
282    /*package*/ static void native_rCubicTo(long nPath, float x1, float y1,
283            float x2, float y2, float x3, float y3) {
284        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
285        if (pathDelegate == null) {
286            return;
287        }
288
289        pathDelegate.rCubicTo(x1, y1, x2, y2, x3, y3);
290    }
291
292    @LayoutlibDelegate
293    /*package*/ static void native_arcTo(long nPath, RectF oval,
294                    float startAngle, float sweepAngle, boolean forceMoveTo) {
295        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
296        if (pathDelegate == null) {
297            return;
298        }
299
300        pathDelegate.arcTo(oval, startAngle, sweepAngle, forceMoveTo);
301    }
302
303    @LayoutlibDelegate
304    /*package*/ static void native_close(long nPath) {
305        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
306        if (pathDelegate == null) {
307            return;
308        }
309
310        pathDelegate.close();
311    }
312
313    @LayoutlibDelegate
314    /*package*/ static void native_addRect(long nPath, RectF rect, int dir) {
315        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
316        if (pathDelegate == null) {
317            return;
318        }
319
320        pathDelegate.addRect(rect.left, rect.top, rect.right, rect.bottom, dir);
321    }
322
323    @LayoutlibDelegate
324    /*package*/ static void native_addRect(long nPath,
325            float left, float top, float right, float bottom, int dir) {
326        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
327        if (pathDelegate == null) {
328            return;
329        }
330
331        pathDelegate.addRect(left, top, right, bottom, dir);
332    }
333
334    @LayoutlibDelegate
335    /*package*/ static void native_addOval(long nPath, RectF oval, int dir) {
336        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
337        if (pathDelegate == null) {
338            return;
339        }
340
341        pathDelegate.mPath.append(new Ellipse2D.Float(
342                oval.left, oval.top, oval.width(), oval.height()), false);
343    }
344
345    @LayoutlibDelegate
346    /*package*/ static void native_addCircle(long nPath, float x, float y, float radius, int dir) {
347        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
348        if (pathDelegate == null) {
349            return;
350        }
351
352        // because x/y is the center of the circle, need to offset this by the radius
353        pathDelegate.mPath.append(new Ellipse2D.Float(
354                x - radius, y - radius, radius * 2, radius * 2), false);
355    }
356
357    @LayoutlibDelegate
358    /*package*/ static void native_addArc(long nPath, RectF oval,
359            float startAngle, float sweepAngle) {
360        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
361        if (pathDelegate == null) {
362            return;
363        }
364
365        // because x/y is the center of the circle, need to offset this by the radius
366        pathDelegate.mPath.append(new Arc2D.Float(
367                oval.left, oval.top, oval.width(), oval.height(),
368                -startAngle, -sweepAngle, Arc2D.OPEN), false);
369    }
370
371    @LayoutlibDelegate
372    /*package*/ static void native_addRoundRect(
373            long nPath, RectF rect, float rx, float ry, int dir) {
374
375        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
376        if (pathDelegate == null) {
377            return;
378        }
379
380        pathDelegate.mPath.append(new RoundRectangle2D.Float(
381                rect.left, rect.top, rect.width(), rect.height(), rx * 2, ry * 2), false);
382    }
383
384    @LayoutlibDelegate
385    /*package*/ static void native_addRoundRect(long nPath, RectF rect, float[] radii, int dir) {
386        // Java2D doesn't support different rounded corners in each corner, so just use the
387        // first value.
388        native_addRoundRect(nPath, rect, radii[0], radii[1], dir);
389
390        // there can be a case where this API is used but with similar values for all corners, so
391        // in that case we don't warn.
392        // we only care if 2 corners are different so just compare to the next one.
393        for (int i = 0 ; i < 3 ; i++) {
394            if (radii[i * 2] != radii[(i + 1) * 2] || radii[i * 2 + 1] != radii[(i + 1) * 2 + 1]) {
395                Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
396                        "Different corner sizes are not supported in Path.addRoundRect.",
397                        null, null /*data*/);
398                break;
399            }
400        }
401    }
402
403    @LayoutlibDelegate
404    /*package*/ static void native_addPath(long nPath, long src, float dx, float dy) {
405        addPath(nPath, src, AffineTransform.getTranslateInstance(dx, dy));
406    }
407
408    @LayoutlibDelegate
409    /*package*/ static void native_addPath(long nPath, long src) {
410        addPath(nPath, src, null /*transform*/);
411    }
412
413    @LayoutlibDelegate
414    /*package*/ static void native_addPath(long nPath, long src, long matrix) {
415        Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(matrix);
416        if (matrixDelegate == null) {
417            return;
418        }
419
420        addPath(nPath, src, matrixDelegate.getAffineTransform());
421    }
422
423    @LayoutlibDelegate
424    /*package*/ static void native_offset(long nPath, float dx, float dy, long dst_path) {
425        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
426        if (pathDelegate == null) {
427            return;
428        }
429
430        // could be null if the int is 0;
431        Path_Delegate dstDelegate = sManager.getDelegate(dst_path);
432
433        pathDelegate.offset(dx, dy, dstDelegate);
434    }
435
436    @LayoutlibDelegate
437    /*package*/ static void native_offset(long nPath, float dx, float dy) {
438        native_offset(nPath, dx, dy, 0);
439    }
440
441    @LayoutlibDelegate
442    /*package*/ static void native_setLastPoint(long nPath, float dx, float dy) {
443        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
444        if (pathDelegate == null) {
445            return;
446        }
447
448        pathDelegate.mLastX = dx;
449        pathDelegate.mLastY = dy;
450    }
451
452    @LayoutlibDelegate
453    /*package*/ static void native_transform(long nPath, long matrix,
454                                                long dst_path) {
455        Path_Delegate pathDelegate = sManager.getDelegate(nPath);
456        if (pathDelegate == null) {
457            return;
458        }
459
460        Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(matrix);
461        if (matrixDelegate == null) {
462            return;
463        }
464
465        // this can be null if dst_path is 0
466        Path_Delegate dstDelegate = sManager.getDelegate(dst_path);
467
468        pathDelegate.transform(matrixDelegate, dstDelegate);
469    }
470
471    @LayoutlibDelegate
472    /*package*/ static void native_transform(long nPath, long matrix) {
473        native_transform(nPath, matrix, 0);
474    }
475
476    @LayoutlibDelegate
477    /*package*/ static boolean native_op(long nPath1, long nPath2, int op, long result) {
478        Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED, "Path.op() not supported", null);
479        return false;
480    }
481
482    @LayoutlibDelegate
483    /*package*/ static void finalizer(long nPath) {
484        sManager.removeJavaReferenceFor(nPath);
485    }
486
487    @LayoutlibDelegate
488    /*package*/ static float[] native_approximate(long nPath, float error) {
489        Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED, "Path.approximate() not supported", null);
490        return new float[0];
491    }
492
493    @LayoutlibDelegate
494    /*package*/ static long native_trim(long nPath, long nTargetPath, long nPathMeasure,
495            float trimStart, float trimEnd, float trimOffset) {
496        // TODO: add trim.
497        Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED, "Path.trim() not supported", null);
498        return nPathMeasure;
499
500    }
501
502    @LayoutlibDelegate
503    private static void native_destroyMeasure(long nPathMeasure) {
504        // Do nothing.
505    }
506
507    // ---- Private helper methods ----
508
509    private void set(Path_Delegate delegate) {
510        mPath.reset();
511        setFillType(delegate.mFillType);
512        mPath.append(delegate.mPath, false /*connect*/);
513    }
514
515    private void setFillType(FillType fillType) {
516        mFillType = fillType;
517        mPath.setWindingRule(getWindingRule(fillType));
518    }
519
520    /**
521     * Returns the Java2D winding rules matching a given Android {@link FillType}.
522     * @param type the android fill type
523     * @return the matching java2d winding rule.
524     */
525    private static int getWindingRule(FillType type) {
526        switch (type) {
527            case WINDING:
528            case INVERSE_WINDING:
529                return GeneralPath.WIND_NON_ZERO;
530            case EVEN_ODD:
531            case INVERSE_EVEN_ODD:
532                return GeneralPath.WIND_EVEN_ODD;
533        }
534
535        assert false;
536        throw new IllegalArgumentException();
537    }
538
539    private static Direction getDirection(int direction) {
540        for (Direction d : Direction.values()) {
541            if (direction == d.nativeInt) {
542                return d;
543            }
544        }
545
546        assert false;
547        return null;
548    }
549
550    private static void addPath(long destPath, long srcPath, AffineTransform transform) {
551        Path_Delegate destPathDelegate = sManager.getDelegate(destPath);
552        if (destPathDelegate == null) {
553            return;
554        }
555
556        Path_Delegate srcPathDelegate = sManager.getDelegate(srcPath);
557        if (srcPathDelegate == null) {
558            return;
559        }
560
561        if (transform != null) {
562            destPathDelegate.mPath.append(
563                    srcPathDelegate.mPath.getPathIterator(transform), false);
564        } else {
565            destPathDelegate.mPath.append(srcPathDelegate.mPath, false);
566        }
567    }
568
569
570    /**
571     * Returns whether the path is empty.
572     * @return true if the path is empty.
573     */
574    private boolean isEmpty() {
575        return mPath.getCurrentPoint() == null;
576    }
577
578    /**
579     * Fills the given {@link RectF} with the path bounds.
580     * @param bounds the RectF to be filled.
581     */
582    private void fillBounds(RectF bounds) {
583        Rectangle2D rect = mPath.getBounds2D();
584        bounds.left = (float)rect.getMinX();
585        bounds.right = (float)rect.getMaxX();
586        bounds.top = (float)rect.getMinY();
587        bounds.bottom = (float)rect.getMaxY();
588    }
589
590    /**
591     * Set the beginning of the next contour to the point (x,y).
592     *
593     * @param x The x-coordinate of the start of a new contour
594     * @param y The y-coordinate of the start of a new contour
595     */
596    private void moveTo(float x, float y) {
597        mPath.moveTo(mLastX = x, mLastY = y);
598    }
599
600    /**
601     * Set the beginning of the next contour relative to the last point on the
602     * previous contour. If there is no previous contour, this is treated the
603     * same as moveTo().
604     *
605     * @param dx The amount to add to the x-coordinate of the end of the
606     *           previous contour, to specify the start of a new contour
607     * @param dy The amount to add to the y-coordinate of the end of the
608     *           previous contour, to specify the start of a new contour
609     */
610    private void rMoveTo(float dx, float dy) {
611        dx += mLastX;
612        dy += mLastY;
613        mPath.moveTo(mLastX = dx, mLastY = dy);
614    }
615
616    /**
617     * Add a line from the last point to the specified point (x,y).
618     * If no moveTo() call has been made for this contour, the first point is
619     * automatically set to (0,0).
620     *
621     * @param x The x-coordinate of the end of a line
622     * @param y The y-coordinate of the end of a line
623     */
624    private void lineTo(float x, float y) {
625        mPath.lineTo(mLastX = x, mLastY = y);
626    }
627
628    /**
629     * Same as lineTo, but the coordinates are considered relative to the last
630     * point on this contour. If there is no previous point, then a moveTo(0,0)
631     * is inserted automatically.
632     *
633     * @param dx The amount to add to the x-coordinate of the previous point on
634     *           this contour, to specify a line
635     * @param dy The amount to add to the y-coordinate of the previous point on
636     *           this contour, to specify a line
637     */
638    private void rLineTo(float dx, float dy) {
639        if (isEmpty()) {
640            mPath.moveTo(mLastX = 0, mLastY = 0);
641        }
642        dx += mLastX;
643        dy += mLastY;
644        mPath.lineTo(mLastX = dx, mLastY = dy);
645    }
646
647    /**
648     * Add a quadratic bezier from the last point, approaching control point
649     * (x1,y1), and ending at (x2,y2). If no moveTo() call has been made for
650     * this contour, the first point is automatically set to (0,0).
651     *
652     * @param x1 The x-coordinate of the control point on a quadratic curve
653     * @param y1 The y-coordinate of the control point on a quadratic curve
654     * @param x2 The x-coordinate of the end point on a quadratic curve
655     * @param y2 The y-coordinate of the end point on a quadratic curve
656     */
657    private void quadTo(float x1, float y1, float x2, float y2) {
658        mPath.quadTo(x1, y1, mLastX = x2, mLastY = y2);
659    }
660
661    /**
662     * Same as quadTo, but the coordinates are considered relative to the last
663     * point on this contour. If there is no previous point, then a moveTo(0,0)
664     * is inserted automatically.
665     *
666     * @param dx1 The amount to add to the x-coordinate of the last point on
667     *            this contour, for the control point of a quadratic curve
668     * @param dy1 The amount to add to the y-coordinate of the last point on
669     *            this contour, for the control point of a quadratic curve
670     * @param dx2 The amount to add to the x-coordinate of the last point on
671     *            this contour, for the end point of a quadratic curve
672     * @param dy2 The amount to add to the y-coordinate of the last point on
673     *            this contour, for the end point of a quadratic curve
674     */
675    private void rQuadTo(float dx1, float dy1, float dx2, float dy2) {
676        if (isEmpty()) {
677            mPath.moveTo(mLastX = 0, mLastY = 0);
678        }
679        dx1 += mLastX;
680        dy1 += mLastY;
681        dx2 += mLastX;
682        dy2 += mLastY;
683        mPath.quadTo(dx1, dy1, mLastX = dx2, mLastY = dy2);
684    }
685
686    /**
687     * Add a cubic bezier from the last point, approaching control points
688     * (x1,y1) and (x2,y2), and ending at (x3,y3). If no moveTo() call has been
689     * made for this contour, the first point is automatically set to (0,0).
690     *
691     * @param x1 The x-coordinate of the 1st control point on a cubic curve
692     * @param y1 The y-coordinate of the 1st control point on a cubic curve
693     * @param x2 The x-coordinate of the 2nd control point on a cubic curve
694     * @param y2 The y-coordinate of the 2nd control point on a cubic curve
695     * @param x3 The x-coordinate of the end point on a cubic curve
696     * @param y3 The y-coordinate of the end point on a cubic curve
697     */
698    private void cubicTo(float x1, float y1, float x2, float y2,
699                        float x3, float y3) {
700        mPath.curveTo(x1, y1, x2, y2, mLastX = x3, mLastY = y3);
701    }
702
703    /**
704     * Same as cubicTo, but the coordinates are considered relative to the
705     * current point on this contour. If there is no previous point, then a
706     * moveTo(0,0) is inserted automatically.
707     */
708    private void rCubicTo(float dx1, float dy1, float dx2, float dy2,
709                         float dx3, float dy3) {
710        if (isEmpty()) {
711            mPath.moveTo(mLastX = 0, mLastY = 0);
712        }
713        dx1 += mLastX;
714        dy1 += mLastY;
715        dx2 += mLastX;
716        dy2 += mLastY;
717        dx3 += mLastX;
718        dy3 += mLastY;
719        mPath.curveTo(dx1, dy1, dx2, dy2, mLastX = dx3, mLastY = dy3);
720    }
721
722    /**
723     * Append the specified arc to the path as a new contour. If the start of
724     * the path is different from the path's current last point, then an
725     * automatic lineTo() is added to connect the current contour to the
726     * start of the arc. However, if the path is empty, then we call moveTo()
727     * with the first point of the arc. The sweep angle is tread mod 360.
728     *
729     * @param oval        The bounds of oval defining shape and size of the arc
730     * @param startAngle  Starting angle (in degrees) where the arc begins
731     * @param sweepAngle  Sweep angle (in degrees) measured clockwise, treated
732     *                    mod 360.
733     * @param forceMoveTo If true, always begin a new contour with the arc
734     */
735    private void arcTo(RectF oval, float startAngle, float sweepAngle, boolean forceMoveTo) {
736        Arc2D arc = new Arc2D.Float(oval.left, oval.top, oval.width(), oval.height(), -startAngle,
737                -sweepAngle, Arc2D.OPEN);
738        mPath.append(arc, true /*connect*/);
739
740        resetLastPointFromPath();
741    }
742
743    /**
744     * Close the current contour. If the current point is not equal to the
745     * first point of the contour, a line segment is automatically added.
746     */
747    private void close() {
748        mPath.closePath();
749    }
750
751    private void resetLastPointFromPath() {
752        Point2D last = mPath.getCurrentPoint();
753        mLastX = (float) last.getX();
754        mLastY = (float) last.getY();
755    }
756
757    /**
758     * Add a closed rectangle contour to the path
759     *
760     * @param left   The left side of a rectangle to add to the path
761     * @param top    The top of a rectangle to add to the path
762     * @param right  The right side of a rectangle to add to the path
763     * @param bottom The bottom of a rectangle to add to the path
764     * @param dir    The direction to wind the rectangle's contour
765     */
766    private void addRect(float left, float top, float right, float bottom,
767                        int dir) {
768        moveTo(left, top);
769
770        Direction direction = getDirection(dir);
771
772        switch (direction) {
773            case CW:
774                lineTo(right, top);
775                lineTo(right, bottom);
776                lineTo(left, bottom);
777                break;
778            case CCW:
779                lineTo(left, bottom);
780                lineTo(right, bottom);
781                lineTo(right, top);
782                break;
783        }
784
785        close();
786
787        resetLastPointFromPath();
788    }
789
790    /**
791     * Offset the path by (dx,dy), returning true on success
792     *
793     * @param dx  The amount in the X direction to offset the entire path
794     * @param dy  The amount in the Y direction to offset the entire path
795     * @param dst The translated path is written here. If this is null, then
796     *            the original path is modified.
797     */
798    public void offset(float dx, float dy, Path_Delegate dst) {
799        GeneralPath newPath = new GeneralPath();
800
801        PathIterator iterator = mPath.getPathIterator(new AffineTransform(0, 0, dx, 0, 0, dy));
802
803        newPath.append(iterator, false /*connect*/);
804
805        if (dst != null) {
806            dst.mPath = newPath;
807        } else {
808            mPath = newPath;
809        }
810    }
811
812    /**
813     * Transform the points in this path by matrix, and write the answer
814     * into dst. If dst is null, then the the original path is modified.
815     *
816     * @param matrix The matrix to apply to the path
817     * @param dst    The transformed path is written here. If dst is null,
818     *               then the the original path is modified
819     */
820    public void transform(Matrix_Delegate matrix, Path_Delegate dst) {
821        if (matrix.hasPerspective()) {
822            assert false;
823            Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_AFFINE,
824                    "android.graphics.Path#transform() only " +
825                    "supports affine transformations.", null, null /*data*/);
826        }
827
828        GeneralPath newPath = new GeneralPath();
829
830        PathIterator iterator = mPath.getPathIterator(matrix.getAffineTransform());
831
832        newPath.append(iterator, false /*connect*/);
833
834        if (dst != null) {
835            dst.mPath = newPath;
836        } else {
837            mPath = newPath;
838        }
839    }
840}
841