Paint.java revision 41207bce2a13c77bf03a05524ba3b3c59f486cc0
1/*
2 * Copyright (C) 2006 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 android.text.GraphicsOperations;
20import android.text.SpannableString;
21import android.text.SpannedString;
22import android.text.TextUtils;
23
24import java.util.Locale;
25
26/**
27 * The Paint class holds the style and color information about how to draw
28 * geometries, text and bitmaps.
29 */
30public class Paint {
31
32    /**
33     * @hide
34     */
35    public int mNativePaint;
36
37    private ColorFilter mColorFilter;
38    private MaskFilter  mMaskFilter;
39    private PathEffect  mPathEffect;
40    private Rasterizer  mRasterizer;
41    private Shader      mShader;
42    private Typeface    mTypeface;
43    private Xfermode    mXfermode;
44
45    private boolean     mHasCompatScaling;
46    private float       mCompatScaling;
47    private float       mInvCompatScaling;
48
49    private Locale      mLocale;
50
51    /**
52     * @hide
53     */
54    public boolean hasShadow;
55    /**
56     * @hide
57     */
58    public float shadowDx;
59    /**
60     * @hide
61     */
62    public float shadowDy;
63    /**
64     * @hide
65     */
66    public float shadowRadius;
67    /**
68     * @hide
69     */
70    public int shadowColor;
71
72    /**
73     * @hide
74     */
75    public  int         mBidiFlags = BIDI_DEFAULT_LTR;
76
77    static final Style[] sStyleArray = {
78        Style.FILL, Style.STROKE, Style.FILL_AND_STROKE
79    };
80    static final Cap[] sCapArray = {
81        Cap.BUTT, Cap.ROUND, Cap.SQUARE
82    };
83    static final Join[] sJoinArray = {
84        Join.MITER, Join.ROUND, Join.BEVEL
85    };
86    static final Align[] sAlignArray = {
87        Align.LEFT, Align.CENTER, Align.RIGHT
88    };
89
90    /** bit mask for the flag enabling antialiasing */
91    public static final int ANTI_ALIAS_FLAG     = 0x01;
92    /** bit mask for the flag enabling bitmap filtering */
93    public static final int FILTER_BITMAP_FLAG  = 0x02;
94    /** bit mask for the flag enabling dithering */
95    public static final int DITHER_FLAG         = 0x04;
96    /** bit mask for the flag enabling underline text */
97    public static final int UNDERLINE_TEXT_FLAG = 0x08;
98    /** bit mask for the flag enabling strike-thru text */
99    public static final int STRIKE_THRU_TEXT_FLAG = 0x10;
100    /** bit mask for the flag enabling fake-bold text */
101    public static final int FAKE_BOLD_TEXT_FLAG = 0x20;
102    /** bit mask for the flag enabling linear-text (no caching) */
103    public static final int LINEAR_TEXT_FLAG    = 0x40;
104    /** bit mask for the flag enabling subpixel-text */
105    public static final int SUBPIXEL_TEXT_FLAG  = 0x80;
106    /** bit mask for the flag enabling device kerning for text */
107    public static final int DEV_KERN_TEXT_FLAG  = 0x100;
108    /** @hide bit mask for the flag enabling subpixel glyph rendering for text */
109    public static final int LCD_RENDER_TEXT_FLAG = 0x200;
110    /** bit mask for the flag enabling embedded bitmap strikes for text */
111    public static final int EMBEDDED_BITMAP_TEXT_FLAG = 0x400;
112    /** @hide bit mask for the flag forcing freetype's autohinter on for text */
113    public static final int AUTO_HINTING_TEXT_FLAG = 0x800;
114    /** @hide bit mask for the flag enabling vertical rendering for text */
115    public static final int VERTICAL_TEXT_FLAG = 0x1000;
116
117    // we use this when we first create a paint
118    static final int DEFAULT_PAINT_FLAGS = DEV_KERN_TEXT_FLAG | EMBEDDED_BITMAP_TEXT_FLAG;
119
120    /**
121     * Option for {@link #setHinting}: disable hinting.
122     */
123    public static final int HINTING_OFF = 0x0;
124
125    /**
126     * Option for {@link #setHinting}: enable hinting.
127     */
128    public static final int HINTING_ON = 0x1;
129
130    /**
131     * Bidi flag to set LTR paragraph direction.
132     *
133     * @hide
134     */
135    public static final int BIDI_LTR = 0x0;
136
137    /**
138     * Bidi flag to set RTL paragraph direction.
139     *
140     * @hide
141     */
142    public static final int BIDI_RTL = 0x1;
143
144    /**
145     * Bidi flag to detect paragraph direction via heuristics, defaulting to
146     * LTR.
147     *
148     * @hide
149     */
150    public static final int BIDI_DEFAULT_LTR = 0x2;
151
152    /**
153     * Bidi flag to detect paragraph direction via heuristics, defaulting to
154     * RTL.
155     *
156     * @hide
157     */
158    public static final int BIDI_DEFAULT_RTL = 0x3;
159
160    /**
161     * Bidi flag to override direction to all LTR (ignore bidi).
162     *
163     * @hide
164     */
165    public static final int BIDI_FORCE_LTR = 0x4;
166
167    /**
168     * Bidi flag to override direction to all RTL (ignore bidi).
169     *
170     * @hide
171     */
172    public static final int BIDI_FORCE_RTL = 0x5;
173
174    /**
175     * Maximum Bidi flag value.
176     * @hide
177     */
178    private static final int BIDI_MAX_FLAG_VALUE = BIDI_FORCE_RTL;
179
180    /**
181     * Mask for bidi flags.
182     * @hide
183     */
184    private static final int BIDI_FLAG_MASK = 0x7;
185
186    /**
187     * Flag for getTextRunAdvances indicating left-to-right run direction.
188     * @hide
189     */
190    public static final int DIRECTION_LTR = 0;
191
192    /**
193     * Flag for getTextRunAdvances indicating right-to-left run direction.
194     * @hide
195     */
196    public static final int DIRECTION_RTL = 1;
197
198    /**
199     * Option for getTextRunCursor to compute the valid cursor after
200     * offset or the limit of the context, whichever is less.
201     * @hide
202     */
203    public static final int CURSOR_AFTER = 0;
204
205    /**
206     * Option for getTextRunCursor to compute the valid cursor at or after
207     * the offset or the limit of the context, whichever is less.
208     * @hide
209     */
210    public static final int CURSOR_AT_OR_AFTER = 1;
211
212     /**
213     * Option for getTextRunCursor to compute the valid cursor before
214     * offset or the start of the context, whichever is greater.
215     * @hide
216     */
217    public static final int CURSOR_BEFORE = 2;
218
219   /**
220     * Option for getTextRunCursor to compute the valid cursor at or before
221     * offset or the start of the context, whichever is greater.
222     * @hide
223     */
224    public static final int CURSOR_AT_OR_BEFORE = 3;
225
226    /**
227     * Option for getTextRunCursor to return offset if the cursor at offset
228     * is valid, or -1 if it isn't.
229     * @hide
230     */
231    public static final int CURSOR_AT = 4;
232
233    /**
234     * Maximum cursor option value.
235     */
236    private static final int CURSOR_OPT_MAX_VALUE = CURSOR_AT;
237
238    /**
239     * The Style specifies if the primitive being drawn is filled, stroked, or
240     * both (in the same color). The default is FILL.
241     */
242    public enum Style {
243        /**
244         * Geometry and text drawn with this style will be filled, ignoring all
245         * stroke-related settings in the paint.
246         */
247        FILL            (0),
248        /**
249         * Geometry and text drawn with this style will be stroked, respecting
250         * the stroke-related fields on the paint.
251         */
252        STROKE          (1),
253        /**
254         * Geometry and text drawn with this style will be both filled and
255         * stroked at the same time, respecting the stroke-related fields on
256         * the paint. This mode can give unexpected results if the geometry
257         * is oriented counter-clockwise. This restriction does not apply to
258         * either FILL or STROKE.
259         */
260        FILL_AND_STROKE (2);
261
262        Style(int nativeInt) {
263            this.nativeInt = nativeInt;
264        }
265        final int nativeInt;
266    }
267
268    /**
269     * The Cap specifies the treatment for the beginning and ending of
270     * stroked lines and paths. The default is BUTT.
271     */
272    public enum Cap {
273        /**
274         * The stroke ends with the path, and does not project beyond it.
275         */
276        BUTT    (0),
277        /**
278         * The stroke projects out as a semicircle, with the center at the
279         * end of the path.
280         */
281        ROUND   (1),
282        /**
283         * The stroke projects out as a square, with the center at the end
284         * of the path.
285         */
286        SQUARE  (2);
287
288        private Cap(int nativeInt) {
289            this.nativeInt = nativeInt;
290        }
291        final int nativeInt;
292    }
293
294    /**
295     * The Join specifies the treatment where lines and curve segments
296     * join on a stroked path. The default is MITER.
297     */
298    public enum Join {
299        /**
300         * The outer edges of a join meet at a sharp angle
301         */
302        MITER   (0),
303        /**
304         * The outer edges of a join meet in a circular arc.
305         */
306        ROUND   (1),
307        /**
308         * The outer edges of a join meet with a straight line
309         */
310        BEVEL   (2);
311
312        private Join(int nativeInt) {
313            this.nativeInt = nativeInt;
314        }
315        final int nativeInt;
316    }
317
318    /**
319     * Align specifies how drawText aligns its text relative to the
320     * [x,y] coordinates. The default is LEFT.
321     */
322    public enum Align {
323        /**
324         * The text is drawn to the right of the x,y origin
325         */
326        LEFT    (0),
327        /**
328         * The text is drawn centered horizontally on the x,y origin
329         */
330        CENTER  (1),
331        /**
332         * The text is drawn to the left of the x,y origin
333         */
334        RIGHT   (2);
335
336        private Align(int nativeInt) {
337            this.nativeInt = nativeInt;
338        }
339        final int nativeInt;
340    }
341
342    /**
343     * Create a new paint with default settings.
344     */
345    public Paint() {
346        this(0);
347    }
348
349    /**
350     * Create a new paint with the specified flags. Use setFlags() to change
351     * these after the paint is created.
352     *
353     * @param flags initial flag bits, as if they were passed via setFlags().
354     */
355    public Paint(int flags) {
356        mNativePaint = native_init();
357        setFlags(flags | DEFAULT_PAINT_FLAGS);
358        // TODO: Turning off hinting has undesirable side effects, we need to
359        //       revisit hinting once we add support for subpixel positioning
360        // setHinting(DisplayMetrics.DENSITY_DEVICE >= DisplayMetrics.DENSITY_TV
361        //        ? HINTING_OFF : HINTING_ON);
362        mCompatScaling = mInvCompatScaling = 1;
363        setTextLocale(Locale.getDefault());
364    }
365
366    /**
367     * Create a new paint, initialized with the attributes in the specified
368     * paint parameter.
369     *
370     * @param paint Existing paint used to initialized the attributes of the
371     *              new paint.
372     */
373    public Paint(Paint paint) {
374        mNativePaint = native_initWithPaint(paint.mNativePaint);
375        setClassVariablesFrom(paint);
376    }
377
378    /** Restores the paint to its default settings. */
379    public void reset() {
380        native_reset(mNativePaint);
381        setFlags(DEFAULT_PAINT_FLAGS);
382
383        // TODO: Turning off hinting has undesirable side effects, we need to
384        //       revisit hinting once we add support for subpixel positioning
385        // setHinting(DisplayMetrics.DENSITY_DEVICE >= DisplayMetrics.DENSITY_TV
386        //        ? HINTING_OFF : HINTING_ON);
387
388        mColorFilter = null;
389        mMaskFilter = null;
390        mPathEffect = null;
391        mRasterizer = null;
392        mShader = null;
393        mTypeface = null;
394        mXfermode = null;
395
396        mHasCompatScaling = false;
397        mCompatScaling = 1;
398        mInvCompatScaling = 1;
399
400        hasShadow = false;
401        shadowDx = 0;
402        shadowDy = 0;
403        shadowRadius = 0;
404        shadowColor = 0;
405
406        mBidiFlags = BIDI_DEFAULT_LTR;
407        setTextLocale(Locale.getDefault());
408    }
409
410    /**
411     * Copy the fields from src into this paint. This is equivalent to calling
412     * get() on all of the src fields, and calling the corresponding set()
413     * methods on this.
414     */
415    public void set(Paint src) {
416        if (this != src) {
417            // copy over the native settings
418            native_set(mNativePaint, src.mNativePaint);
419            setClassVariablesFrom(src);
420        }
421    }
422
423    /**
424     * Set all class variables using current values from the given
425     * {@link Paint}.
426     */
427    private void setClassVariablesFrom(Paint paint) {
428        mColorFilter = paint.mColorFilter;
429        mMaskFilter = paint.mMaskFilter;
430        mPathEffect = paint.mPathEffect;
431        mRasterizer = paint.mRasterizer;
432        if (paint.mShader != null) {
433            mShader = paint.mShader.copy();
434        } else {
435            mShader = null;
436        }
437        mTypeface = paint.mTypeface;
438        mXfermode = paint.mXfermode;
439
440        mHasCompatScaling = paint.mHasCompatScaling;
441        mCompatScaling = paint.mCompatScaling;
442        mInvCompatScaling = paint.mInvCompatScaling;
443
444        hasShadow = paint.hasShadow;
445        shadowDx = paint.shadowDx;
446        shadowDy = paint.shadowDy;
447        shadowRadius = paint.shadowRadius;
448        shadowColor = paint.shadowColor;
449
450        mBidiFlags = paint.mBidiFlags;
451        mLocale = paint.mLocale;
452    }
453
454    /** @hide */
455    public void setCompatibilityScaling(float factor) {
456        if (factor == 1.0) {
457            mHasCompatScaling = false;
458            mCompatScaling = mInvCompatScaling = 1.0f;
459        } else {
460            mHasCompatScaling = true;
461            mCompatScaling = factor;
462            mInvCompatScaling = 1.0f/factor;
463        }
464    }
465
466    /**
467     * Return the bidi flags on the paint.
468     *
469     * @return the bidi flags on the paint
470     * @hide
471     */
472    public int getBidiFlags() {
473        return mBidiFlags;
474    }
475
476    /**
477     * Set the bidi flags on the paint.
478     * @hide
479     */
480    public void setBidiFlags(int flags) {
481        // only flag value is the 3-bit BIDI control setting
482        flags &= BIDI_FLAG_MASK;
483        if (flags > BIDI_MAX_FLAG_VALUE) {
484            throw new IllegalArgumentException("unknown bidi flag: " + flags);
485        }
486        mBidiFlags = flags;
487    }
488
489    /**
490     * Return the paint's flags. Use the Flag enum to test flag values.
491     *
492     * @return the paint's flags (see enums ending in _Flag for bit masks)
493     */
494    public native int getFlags();
495
496    /**
497     * Set the paint's flags. Use the Flag enum to specific flag values.
498     *
499     * @param flags The new flag bits for the paint
500     */
501    public native void setFlags(int flags);
502
503    /**
504     * Return the paint's hinting mode.  Returns either
505     * {@link #HINTING_OFF} or {@link #HINTING_ON}.
506     */
507    public native int getHinting();
508
509    /**
510     * Set the paint's hinting mode.  May be either
511     * {@link #HINTING_OFF} or {@link #HINTING_ON}.
512     */
513    public native void setHinting(int mode);
514
515    /**
516     * Helper for getFlags(), returning true if ANTI_ALIAS_FLAG bit is set
517     * AntiAliasing smooths out the edges of what is being drawn, but is has
518     * no impact on the interior of the shape. See setDither() and
519     * setFilterBitmap() to affect how colors are treated.
520     *
521     * @return true if the antialias bit is set in the paint's flags.
522     */
523    public final boolean isAntiAlias() {
524        return (getFlags() & ANTI_ALIAS_FLAG) != 0;
525    }
526
527    /**
528     * Helper for setFlags(), setting or clearing the ANTI_ALIAS_FLAG bit
529     * AntiAliasing smooths out the edges of what is being drawn, but is has
530     * no impact on the interior of the shape. See setDither() and
531     * setFilterBitmap() to affect how colors are treated.
532     *
533     * @param aa true to set the antialias bit in the flags, false to clear it
534     */
535    public native void setAntiAlias(boolean aa);
536
537    /**
538     * Helper for getFlags(), returning true if DITHER_FLAG bit is set
539     * Dithering affects how colors that are higher precision than the device
540     * are down-sampled. No dithering is generally faster, but higher precision
541     * colors are just truncated down (e.g. 8888 -> 565). Dithering tries to
542     * distribute the error inherent in this process, to reduce the visual
543     * artifacts.
544     *
545     * @return true if the dithering bit is set in the paint's flags.
546     */
547    public final boolean isDither() {
548        return (getFlags() & DITHER_FLAG) != 0;
549    }
550
551    /**
552     * Helper for setFlags(), setting or clearing the DITHER_FLAG bit
553     * Dithering affects how colors that are higher precision than the device
554     * are down-sampled. No dithering is generally faster, but higher precision
555     * colors are just truncated down (e.g. 8888 -> 565). Dithering tries to
556     * distribute the error inherent in this process, to reduce the visual
557     * artifacts.
558     *
559     * @param dither true to set the dithering bit in flags, false to clear it
560     */
561    public native void setDither(boolean dither);
562
563    /**
564     * Helper for getFlags(), returning true if LINEAR_TEXT_FLAG bit is set
565     *
566     * @return true if the lineartext bit is set in the paint's flags
567     */
568    public final boolean isLinearText() {
569        return (getFlags() & LINEAR_TEXT_FLAG) != 0;
570    }
571
572    /**
573     * Helper for setFlags(), setting or clearing the LINEAR_TEXT_FLAG bit
574     *
575     * @param linearText true to set the linearText bit in the paint's flags,
576     *                   false to clear it.
577     */
578    public native void setLinearText(boolean linearText);
579
580    /**
581     * Helper for getFlags(), returning true if SUBPIXEL_TEXT_FLAG bit is set
582     *
583     * @return true if the subpixel bit is set in the paint's flags
584     */
585    public final boolean isSubpixelText() {
586        return (getFlags() & SUBPIXEL_TEXT_FLAG) != 0;
587    }
588
589    /**
590     * Helper for setFlags(), setting or clearing the SUBPIXEL_TEXT_FLAG bit
591     *
592     * @param subpixelText true to set the subpixelText bit in the paint's
593     *                     flags, false to clear it.
594     */
595    public native void setSubpixelText(boolean subpixelText);
596
597    /**
598     * Helper for getFlags(), returning true if UNDERLINE_TEXT_FLAG bit is set
599     *
600     * @return true if the underlineText bit is set in the paint's flags.
601     */
602    public final boolean isUnderlineText() {
603        return (getFlags() & UNDERLINE_TEXT_FLAG) != 0;
604    }
605
606    /**
607     * Helper for setFlags(), setting or clearing the UNDERLINE_TEXT_FLAG bit
608     *
609     * @param underlineText true to set the underlineText bit in the paint's
610     *                      flags, false to clear it.
611     */
612    public native void setUnderlineText(boolean underlineText);
613
614    /**
615     * Helper for getFlags(), returning true if STRIKE_THRU_TEXT_FLAG bit is set
616     *
617     * @return true if the strikeThruText bit is set in the paint's flags.
618     */
619    public final boolean isStrikeThruText() {
620        return (getFlags() & STRIKE_THRU_TEXT_FLAG) != 0;
621    }
622
623    /**
624     * Helper for setFlags(), setting or clearing the STRIKE_THRU_TEXT_FLAG bit
625     *
626     * @param strikeThruText true to set the strikeThruText bit in the paint's
627     *                       flags, false to clear it.
628     */
629    public native void setStrikeThruText(boolean strikeThruText);
630
631    /**
632     * Helper for getFlags(), returning true if FAKE_BOLD_TEXT_FLAG bit is set
633     *
634     * @return true if the fakeBoldText bit is set in the paint's flags.
635     */
636    public final boolean isFakeBoldText() {
637        return (getFlags() & FAKE_BOLD_TEXT_FLAG) != 0;
638    }
639
640    /**
641     * Helper for setFlags(), setting or clearing the FAKE_BOLD_TEXT_FLAG bit
642     *
643     * @param fakeBoldText true to set the fakeBoldText bit in the paint's
644     *                     flags, false to clear it.
645     */
646    public native void setFakeBoldText(boolean fakeBoldText);
647
648    /**
649     * Whether or not the bitmap filter is activated.
650     * Filtering affects the sampling of bitmaps when they are transformed.
651     * Filtering does not affect how the colors in the bitmap are converted into
652     * device pixels. That is dependent on dithering and xfermodes.
653     *
654     * @see #setFilterBitmap(boolean) setFilterBitmap()
655     */
656    public final boolean isFilterBitmap() {
657        return (getFlags() & FILTER_BITMAP_FLAG) != 0;
658    }
659
660    /**
661     * Helper for setFlags(), setting or clearing the FILTER_BITMAP_FLAG bit.
662     * Filtering affects the sampling of bitmaps when they are transformed.
663     * Filtering does not affect how the colors in the bitmap are converted into
664     * device pixels. That is dependent on dithering and xfermodes.
665     *
666     * @param filter true to set the FILTER_BITMAP_FLAG bit in the paint's
667     *               flags, false to clear it.
668     */
669    public native void setFilterBitmap(boolean filter);
670
671    /**
672     * Return the paint's style, used for controlling how primitives'
673     * geometries are interpreted (except for drawBitmap, which always assumes
674     * FILL_STYLE).
675     *
676     * @return the paint's style setting (Fill, Stroke, StrokeAndFill)
677     */
678    public Style getStyle() {
679        return sStyleArray[native_getStyle(mNativePaint)];
680    }
681
682    /**
683     * Set the paint's style, used for controlling how primitives'
684     * geometries are interpreted (except for drawBitmap, which always assumes
685     * Fill).
686     *
687     * @param style The new style to set in the paint
688     */
689    public void setStyle(Style style) {
690        native_setStyle(mNativePaint, style.nativeInt);
691    }
692
693    /**
694     * Return the paint's color. Note that the color is a 32bit value
695     * containing alpha as well as r,g,b. This 32bit value is not premultiplied,
696     * meaning that its alpha can be any value, regardless of the values of
697     * r,g,b. See the Color class for more details.
698     *
699     * @return the paint's color (and alpha).
700     */
701    public native int getColor();
702
703    /**
704     * Set the paint's color. Note that the color is an int containing alpha
705     * as well as r,g,b. This 32bit value is not premultiplied, meaning that
706     * its alpha can be any value, regardless of the values of r,g,b.
707     * See the Color class for more details.
708     *
709     * @param color The new color (including alpha) to set in the paint.
710     */
711    public native void setColor(int color);
712
713    /**
714     * Helper to getColor() that just returns the color's alpha value. This is
715     * the same as calling getColor() >>> 24. It always returns a value between
716     * 0 (completely transparent) and 255 (completely opaque).
717     *
718     * @return the alpha component of the paint's color.
719     */
720    public native int getAlpha();
721
722    /**
723     * Helper to setColor(), that only assigns the color's alpha value,
724     * leaving its r,g,b values unchanged. Results are undefined if the alpha
725     * value is outside of the range [0..255]
726     *
727     * @param a set the alpha component [0..255] of the paint's color.
728     */
729    public native void setAlpha(int a);
730
731    /**
732     * Helper to setColor(), that takes a,r,g,b and constructs the color int
733     *
734     * @param a The new alpha component (0..255) of the paint's color.
735     * @param r The new red component (0..255) of the paint's color.
736     * @param g The new green component (0..255) of the paint's color.
737     * @param b The new blue component (0..255) of the paint's color.
738     */
739    public void setARGB(int a, int r, int g, int b) {
740        setColor((a << 24) | (r << 16) | (g << 8) | b);
741    }
742
743    /**
744     * Return the width for stroking.
745     * <p />
746     * A value of 0 strokes in hairline mode.
747     * Hairlines always draws a single pixel independent of the canva's matrix.
748     *
749     * @return the paint's stroke width, used whenever the paint's style is
750     *         Stroke or StrokeAndFill.
751     */
752    public native float getStrokeWidth();
753
754    /**
755     * Set the width for stroking.
756     * Pass 0 to stroke in hairline mode.
757     * Hairlines always draws a single pixel independent of the canva's matrix.
758     *
759     * @param width set the paint's stroke width, used whenever the paint's
760     *              style is Stroke or StrokeAndFill.
761     */
762    public native void setStrokeWidth(float width);
763
764    /**
765     * Return the paint's stroke miter value. Used to control the behavior
766     * of miter joins when the joins angle is sharp.
767     *
768     * @return the paint's miter limit, used whenever the paint's style is
769     *         Stroke or StrokeAndFill.
770     */
771    public native float getStrokeMiter();
772
773    /**
774     * Set the paint's stroke miter value. This is used to control the behavior
775     * of miter joins when the joins angle is sharp. This value must be >= 0.
776     *
777     * @param miter set the miter limit on the paint, used whenever the paint's
778     *              style is Stroke or StrokeAndFill.
779     */
780    public native void setStrokeMiter(float miter);
781
782    /**
783     * Return the paint's Cap, controlling how the start and end of stroked
784     * lines and paths are treated.
785     *
786     * @return the line cap style for the paint, used whenever the paint's
787     *         style is Stroke or StrokeAndFill.
788     */
789    public Cap getStrokeCap() {
790        return sCapArray[native_getStrokeCap(mNativePaint)];
791    }
792
793    /**
794     * Set the paint's Cap.
795     *
796     * @param cap set the paint's line cap style, used whenever the paint's
797     *            style is Stroke or StrokeAndFill.
798     */
799    public void setStrokeCap(Cap cap) {
800        native_setStrokeCap(mNativePaint, cap.nativeInt);
801    }
802
803    /**
804     * Return the paint's stroke join type.
805     *
806     * @return the paint's Join.
807     */
808    public Join getStrokeJoin() {
809        return sJoinArray[native_getStrokeJoin(mNativePaint)];
810    }
811
812    /**
813     * Set the paint's Join.
814     *
815     * @param join set the paint's Join, used whenever the paint's style is
816     *             Stroke or StrokeAndFill.
817     */
818    public void setStrokeJoin(Join join) {
819        native_setStrokeJoin(mNativePaint, join.nativeInt);
820    }
821
822    /**
823     * Applies any/all effects (patheffect, stroking) to src, returning the
824     * result in dst. The result is that drawing src with this paint will be
825     * the same as drawing dst with a default paint (at least from the
826     * geometric perspective).
827     *
828     * @param src input path
829     * @param dst output path (may be the same as src)
830     * @return    true if the path should be filled, or false if it should be
831     *                 drawn with a hairline (width == 0)
832     */
833    public boolean getFillPath(Path src, Path dst) {
834        return native_getFillPath(mNativePaint, src.ni(), dst.ni());
835    }
836
837    /**
838     * Get the paint's shader object.
839     *
840     * @return the paint's shader (or null)
841     */
842    public Shader getShader() {
843        return mShader;
844    }
845
846    /**
847     * Set or clear the shader object.
848     * <p />
849     * Pass null to clear any previous shader.
850     * As a convenience, the parameter passed is also returned.
851     *
852     * @param shader May be null. the new shader to be installed in the paint
853     * @return       shader
854     */
855    public Shader setShader(Shader shader) {
856        int shaderNative = 0;
857        if (shader != null)
858            shaderNative = shader.native_instance;
859        native_setShader(mNativePaint, shaderNative);
860        mShader = shader;
861        return shader;
862    }
863
864    /**
865     * Get the paint's colorfilter (maybe be null).
866     *
867     * @return the paint's colorfilter (maybe be null)
868     */
869    public ColorFilter getColorFilter() {
870        return mColorFilter;
871    }
872
873    /**
874     * Set or clear the paint's colorfilter, returning the parameter.
875     *
876     * @param filter May be null. The new filter to be installed in the paint
877     * @return       filter
878     */
879    public ColorFilter setColorFilter(ColorFilter filter) {
880        int filterNative = 0;
881        if (filter != null)
882            filterNative = filter.native_instance;
883        native_setColorFilter(mNativePaint, filterNative);
884        mColorFilter = filter;
885        return filter;
886    }
887
888    /**
889     * Get the paint's xfermode object.
890     *
891     * @return the paint's xfermode (or null)
892     */
893    public Xfermode getXfermode() {
894        return mXfermode;
895    }
896
897    /**
898     * Set or clear the xfermode object.
899     * <p />
900     * Pass null to clear any previous xfermode.
901     * As a convenience, the parameter passed is also returned.
902     *
903     * @param xfermode May be null. The xfermode to be installed in the paint
904     * @return         xfermode
905     */
906    public Xfermode setXfermode(Xfermode xfermode) {
907        int xfermodeNative = 0;
908        if (xfermode != null)
909            xfermodeNative = xfermode.native_instance;
910        native_setXfermode(mNativePaint, xfermodeNative);
911        mXfermode = xfermode;
912        return xfermode;
913    }
914
915    /**
916     * Get the paint's patheffect object.
917     *
918     * @return the paint's patheffect (or null)
919     */
920    public PathEffect getPathEffect() {
921        return mPathEffect;
922    }
923
924    /**
925     * Set or clear the patheffect object.
926     * <p />
927     * Pass null to clear any previous patheffect.
928     * As a convenience, the parameter passed is also returned.
929     *
930     * @param effect May be null. The patheffect to be installed in the paint
931     * @return       effect
932     */
933    public PathEffect setPathEffect(PathEffect effect) {
934        int effectNative = 0;
935        if (effect != null) {
936            effectNative = effect.native_instance;
937        }
938        native_setPathEffect(mNativePaint, effectNative);
939        mPathEffect = effect;
940        return effect;
941    }
942
943    /**
944     * Get the paint's maskfilter object.
945     *
946     * @return the paint's maskfilter (or null)
947     */
948    public MaskFilter getMaskFilter() {
949        return mMaskFilter;
950    }
951
952    /**
953     * Set or clear the maskfilter object.
954     * <p />
955     * Pass null to clear any previous maskfilter.
956     * As a convenience, the parameter passed is also returned.
957     *
958     * @param maskfilter May be null. The maskfilter to be installed in the
959     *                   paint
960     * @return           maskfilter
961     */
962    public MaskFilter setMaskFilter(MaskFilter maskfilter) {
963        int maskfilterNative = 0;
964        if (maskfilter != null) {
965            maskfilterNative = maskfilter.native_instance;
966        }
967        native_setMaskFilter(mNativePaint, maskfilterNative);
968        mMaskFilter = maskfilter;
969        return maskfilter;
970    }
971
972    /**
973     * Get the paint's typeface object.
974     * <p />
975     * The typeface object identifies which font to use when drawing or
976     * measuring text.
977     *
978     * @return the paint's typeface (or null)
979     */
980    public Typeface getTypeface() {
981        return mTypeface;
982    }
983
984    /**
985     * Set or clear the typeface object.
986     * <p />
987     * Pass null to clear any previous typeface.
988     * As a convenience, the parameter passed is also returned.
989     *
990     * @param typeface May be null. The typeface to be installed in the paint
991     * @return         typeface
992     */
993    public Typeface setTypeface(Typeface typeface) {
994        int typefaceNative = 0;
995        if (typeface != null) {
996            typefaceNative = typeface.native_instance;
997        }
998        native_setTypeface(mNativePaint, typefaceNative);
999        mTypeface = typeface;
1000        return typeface;
1001    }
1002
1003    /**
1004     * Get the paint's rasterizer (or null).
1005     * <p />
1006     * The raster controls/modifies how paths/text are turned into alpha masks.
1007     *
1008     * @return         the paint's rasterizer (or null)
1009     */
1010    public Rasterizer getRasterizer() {
1011        return mRasterizer;
1012    }
1013
1014    /**
1015     * Set or clear the rasterizer object.
1016     * <p />
1017     * Pass null to clear any previous rasterizer.
1018     * As a convenience, the parameter passed is also returned.
1019     *
1020     * @param rasterizer May be null. The new rasterizer to be installed in
1021     *                   the paint.
1022     * @return           rasterizer
1023     */
1024    public Rasterizer setRasterizer(Rasterizer rasterizer) {
1025        int rasterizerNative = 0;
1026        if (rasterizer != null) {
1027            rasterizerNative = rasterizer.native_instance;
1028        }
1029        native_setRasterizer(mNativePaint, rasterizerNative);
1030        mRasterizer = rasterizer;
1031        return rasterizer;
1032    }
1033
1034    /**
1035     * This draws a shadow layer below the main layer, with the specified
1036     * offset and color, and blur radius. If radius is 0, then the shadow
1037     * layer is removed.
1038     */
1039    public void setShadowLayer(float radius, float dx, float dy, int color) {
1040        hasShadow = radius > 0.0f;
1041        shadowRadius = radius;
1042        shadowDx = dx;
1043        shadowDy = dy;
1044        shadowColor = color;
1045        nSetShadowLayer(radius, dx, dy, color);
1046    }
1047
1048    private native void nSetShadowLayer(float radius, float dx, float dy, int color);
1049
1050    /**
1051     * Clear the shadow layer.
1052     */
1053    public void clearShadowLayer() {
1054        hasShadow = false;
1055        nSetShadowLayer(0, 0, 0, 0);
1056    }
1057
1058    /**
1059     * Return the paint's Align value for drawing text. This controls how the
1060     * text is positioned relative to its origin. LEFT align means that all of
1061     * the text will be drawn to the right of its origin (i.e. the origin
1062     * specifieds the LEFT edge of the text) and so on.
1063     *
1064     * @return the paint's Align value for drawing text.
1065     */
1066    public Align getTextAlign() {
1067        return sAlignArray[native_getTextAlign(mNativePaint)];
1068    }
1069
1070    /**
1071     * Set the paint's text alignment. This controls how the
1072     * text is positioned relative to its origin. LEFT align means that all of
1073     * the text will be drawn to the right of its origin (i.e. the origin
1074     * specifieds the LEFT edge of the text) and so on.
1075     *
1076     * @param align set the paint's Align value for drawing text.
1077     */
1078    public void setTextAlign(Align align) {
1079        native_setTextAlign(mNativePaint, align.nativeInt);
1080    }
1081
1082    /**
1083     * Get the text Locale.
1084     *
1085     * @return the paint's Locale used for drawing text, never null.
1086     */
1087    public Locale getTextLocale() {
1088        return mLocale;
1089    }
1090
1091    /**
1092     * Set the text locale.
1093     *
1094     * The text locale affects how the text is drawn for some languages.
1095     *
1096     * For example, if the locale is {@link Locale#CHINESE} or {@link Locale#CHINA},
1097     * then the text renderer will prefer to draw text using a Chinese font. Likewise,
1098     * if the locale is {@link Locale#JAPANESE} or {@link Locale#JAPAN}, then the text
1099     * renderer will prefer to draw text using a Japanese font.
1100     *
1101     * This distinction is important because Chinese and Japanese text both use many
1102     * of the same Unicode code points but their appearance is subtly different for
1103     * each language.
1104     *
1105     * By default, the text locale is initialized to the system locale (as returned
1106     * by {@link Locale#getDefault}). This assumes that the text to be rendered will
1107     * most likely be in the user's preferred language.
1108     *
1109     * If the actual language of the text is known, then it can be provided to the
1110     * text renderer using this method. The text renderer may attempt to guess the
1111     * language script based on the contents of the text to be drawn independent of
1112     * the text locale here. Specifying the text locale just helps it do a better
1113     * job in certain ambiguous cases
1114     *
1115     * @param locale the paint's locale value for drawing text, must not be null.
1116     */
1117    public void setTextLocale(Locale locale) {
1118        if (locale == null) {
1119            throw new IllegalArgumentException("locale cannot be null");
1120        }
1121        if (locale.equals(mLocale)) return;
1122        mLocale = locale;
1123        native_setTextLocale(mNativePaint, locale.toString());
1124    }
1125
1126    /**
1127     * Return the paint's text size.
1128     *
1129     * @return the paint's text size.
1130     */
1131    public native float getTextSize();
1132
1133    /**
1134     * Set the paint's text size. This value must be > 0
1135     *
1136     * @param textSize set the paint's text size.
1137     */
1138    public native void setTextSize(float textSize);
1139
1140    /**
1141     * Return the paint's horizontal scale factor for text. The default value
1142     * is 1.0.
1143     *
1144     * @return the paint's scale factor in X for drawing/measuring text
1145     */
1146    public native float getTextScaleX();
1147
1148    /**
1149     * Set the paint's horizontal scale factor for text. The default value
1150     * is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
1151     * stretch the text narrower.
1152     *
1153     * @param scaleX set the paint's scale in X for drawing/measuring text.
1154     */
1155    public native void setTextScaleX(float scaleX);
1156
1157    /**
1158     * Return the paint's horizontal skew factor for text. The default value
1159     * is 0.
1160     *
1161     * @return         the paint's skew factor in X for drawing text.
1162     */
1163    public native float getTextSkewX();
1164
1165    /**
1166     * Set the paint's horizontal skew factor for text. The default value
1167     * is 0. For approximating oblique text, use values around -0.25.
1168     *
1169     * @param skewX set the paint's skew factor in X for drawing text.
1170     */
1171    public native void setTextSkewX(float skewX);
1172
1173    /**
1174     * Return the distance above (negative) the baseline (ascent) based on the
1175     * current typeface and text size.
1176     *
1177     * @return the distance above (negative) the baseline (ascent) based on the
1178     *         current typeface and text size.
1179     */
1180    public native float ascent();
1181
1182    /**
1183     * Return the distance below (positive) the baseline (descent) based on the
1184     * current typeface and text size.
1185     *
1186     * @return the distance below (positive) the baseline (descent) based on
1187     *         the current typeface and text size.
1188     */
1189    public native float descent();
1190
1191    /**
1192     * Class that describes the various metrics for a font at a given text size.
1193     * Remember, Y values increase going down, so those values will be positive,
1194     * and values that measure distances going up will be negative. This class
1195     * is returned by getFontMetrics().
1196     */
1197    public static class FontMetrics {
1198        /**
1199         * The maximum distance above the baseline for the tallest glyph in
1200         * the font at a given text size.
1201         */
1202        public float   top;
1203        /**
1204         * The recommended distance above the baseline for singled spaced text.
1205         */
1206        public float   ascent;
1207        /**
1208         * The recommended distance below the baseline for singled spaced text.
1209         */
1210        public float   descent;
1211        /**
1212         * The maximum distance below the baseline for the lowest glyph in
1213         * the font at a given text size.
1214         */
1215        public float   bottom;
1216        /**
1217         * The recommended additional space to add between lines of text.
1218         */
1219        public float   leading;
1220    }
1221
1222    /**
1223     * Return the font's recommended interline spacing, given the Paint's
1224     * settings for typeface, textSize, etc. If metrics is not null, return the
1225     * fontmetric values in it.
1226     *
1227     * @param metrics If this object is not null, its fields are filled with
1228     *                the appropriate values given the paint's text attributes.
1229     * @return the font's recommended interline spacing.
1230     */
1231    public native float getFontMetrics(FontMetrics metrics);
1232
1233    /**
1234     * Allocates a new FontMetrics object, and then calls getFontMetrics(fm)
1235     * with it, returning the object.
1236     */
1237    public FontMetrics getFontMetrics() {
1238        FontMetrics fm = new FontMetrics();
1239        getFontMetrics(fm);
1240        return fm;
1241    }
1242
1243    /**
1244     * Convenience method for callers that want to have FontMetrics values as
1245     * integers.
1246     */
1247    public static class FontMetricsInt {
1248        public int   top;
1249        public int   ascent;
1250        public int   descent;
1251        public int   bottom;
1252        public int   leading;
1253
1254        @Override public String toString() {
1255            return "FontMetricsInt: top=" + top + " ascent=" + ascent +
1256                    " descent=" + descent + " bottom=" + bottom +
1257                    " leading=" + leading;
1258        }
1259    }
1260
1261    /**
1262     * Return the font's interline spacing, given the Paint's settings for
1263     * typeface, textSize, etc. If metrics is not null, return the fontmetric
1264     * values in it. Note: all values have been converted to integers from
1265     * floats, in such a way has to make the answers useful for both spacing
1266     * and clipping. If you want more control over the rounding, call
1267     * getFontMetrics().
1268     *
1269     * @return the font's interline spacing.
1270     */
1271    public native int getFontMetricsInt(FontMetricsInt fmi);
1272
1273    public FontMetricsInt getFontMetricsInt() {
1274        FontMetricsInt fm = new FontMetricsInt();
1275        getFontMetricsInt(fm);
1276        return fm;
1277    }
1278
1279    /**
1280     * Return the recommend line spacing based on the current typeface and
1281     * text size.
1282     *
1283     * @return  recommend line spacing based on the current typeface and
1284     *          text size.
1285     */
1286    public float getFontSpacing() {
1287        return getFontMetrics(null);
1288    }
1289
1290    /**
1291     * Return the width of the text.
1292     *
1293     * @param text  The text to measure. Cannot be null.
1294     * @param index The index of the first character to start measuring
1295     * @param count THe number of characters to measure, beginning with start
1296     * @return      The width of the text
1297     */
1298    public float measureText(char[] text, int index, int count) {
1299        if (text == null) {
1300            throw new IllegalArgumentException("text cannot be null");
1301        }
1302        if ((index | count) < 0 || index + count > text.length) {
1303            throw new ArrayIndexOutOfBoundsException();
1304        }
1305
1306        if (text.length == 0 || count == 0) {
1307            return 0f;
1308        }
1309        if (!mHasCompatScaling) {
1310            return (float) Math.ceil(native_measureText(text, index, count, mBidiFlags));
1311        }
1312
1313        final float oldSize = getTextSize();
1314        setTextSize(oldSize*mCompatScaling);
1315        float w = native_measureText(text, index, count, mBidiFlags);
1316        setTextSize(oldSize);
1317        return (float) Math.ceil(w*mInvCompatScaling);
1318    }
1319
1320    private native float native_measureText(char[] text, int index, int count, int bidiFlags);
1321
1322    /**
1323     * Return the width of the text.
1324     *
1325     * @param text  The text to measure. Cannot be null.
1326     * @param start The index of the first character to start measuring
1327     * @param end   1 beyond the index of the last character to measure
1328     * @return      The width of the text
1329     */
1330    public float measureText(String text, int start, int end) {
1331        if (text == null) {
1332            throw new IllegalArgumentException("text cannot be null");
1333        }
1334        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1335            throw new IndexOutOfBoundsException();
1336        }
1337
1338        if (text.length() == 0 || start == end) {
1339            return 0f;
1340        }
1341        if (!mHasCompatScaling) {
1342            return (float) Math.ceil(native_measureText(text, start, end, mBidiFlags));
1343        }
1344
1345        final float oldSize = getTextSize();
1346        setTextSize(oldSize*mCompatScaling);
1347        float w = native_measureText(text, start, end, mBidiFlags);
1348        setTextSize(oldSize);
1349        return (float) Math.ceil(w*mInvCompatScaling);
1350    }
1351
1352    private native float native_measureText(String text, int start, int end, int bidiFlags);
1353
1354    /**
1355     * Return the width of the text.
1356     *
1357     * @param text  The text to measure. Cannot be null.
1358     * @return      The width of the text
1359     */
1360    public float measureText(String text) {
1361        if (text == null) {
1362            throw new IllegalArgumentException("text cannot be null");
1363        }
1364
1365        if (text.length() == 0) {
1366            return 0f;
1367        }
1368
1369        if (!mHasCompatScaling) {
1370            return (float) Math.ceil(native_measureText(text, mBidiFlags));
1371        }
1372        final float oldSize = getTextSize();
1373        setTextSize(oldSize*mCompatScaling);
1374        float w = native_measureText(text, mBidiFlags);
1375        setTextSize(oldSize);
1376        return (float) Math.ceil(w*mInvCompatScaling);
1377    }
1378
1379    private native float native_measureText(String text, int bidiFlags);
1380
1381    /**
1382     * Return the width of the text.
1383     *
1384     * @param text  The text to measure
1385     * @param start The index of the first character to start measuring
1386     * @param end   1 beyond the index of the last character to measure
1387     * @return      The width of the text
1388     */
1389    public float measureText(CharSequence text, int start, int end) {
1390        if (text == null) {
1391            throw new IllegalArgumentException("text cannot be null");
1392        }
1393        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1394            throw new IndexOutOfBoundsException();
1395        }
1396
1397        if (text.length() == 0 || start == end) {
1398            return 0f;
1399        }
1400        if (text instanceof String) {
1401            return measureText((String)text, start, end);
1402        }
1403        if (text instanceof SpannedString ||
1404            text instanceof SpannableString) {
1405            return measureText(text.toString(), start, end);
1406        }
1407        if (text instanceof GraphicsOperations) {
1408            return ((GraphicsOperations)text).measureText(start, end, this);
1409        }
1410
1411        char[] buf = TemporaryBuffer.obtain(end - start);
1412        TextUtils.getChars(text, start, end, buf, 0);
1413        float result = measureText(buf, 0, end - start);
1414        TemporaryBuffer.recycle(buf);
1415        return result;
1416    }
1417
1418    /**
1419     * Measure the text, stopping early if the measured width exceeds maxWidth.
1420     * Return the number of chars that were measured, and if measuredWidth is
1421     * not null, return in it the actual width measured.
1422     *
1423     * @param text  The text to measure. Cannot be null.
1424     * @param index The offset into text to begin measuring at
1425     * @param count The number of maximum number of entries to measure. If count
1426     *              is negative, then the characters are measured in reverse order.
1427     * @param maxWidth The maximum width to accumulate.
1428     * @param measuredWidth Optional. If not null, returns the actual width
1429     *                     measured.
1430     * @return The number of chars that were measured. Will always be <=
1431     *         abs(count).
1432     */
1433    public int breakText(char[] text, int index, int count,
1434                                float maxWidth, float[] measuredWidth) {
1435        if (text == null) {
1436            throw new IllegalArgumentException("text cannot be null");
1437        }
1438        if (index < 0 || text.length - index < Math.abs(count)) {
1439            throw new ArrayIndexOutOfBoundsException();
1440        }
1441
1442        if (text.length == 0 || count == 0) {
1443            return 0;
1444        }
1445        if (!mHasCompatScaling) {
1446            return native_breakText(text, index, count, maxWidth, mBidiFlags, measuredWidth);
1447        }
1448
1449        final float oldSize = getTextSize();
1450        setTextSize(oldSize*mCompatScaling);
1451        int res = native_breakText(text, index, count, maxWidth*mCompatScaling, mBidiFlags,
1452                measuredWidth);
1453        setTextSize(oldSize);
1454        if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling;
1455        return res;
1456    }
1457
1458    private native int native_breakText(char[] text, int index, int count,
1459                                        float maxWidth, int bidiFlags, float[] measuredWidth);
1460
1461    /**
1462     * Measure the text, stopping early if the measured width exceeds maxWidth.
1463     * Return the number of chars that were measured, and if measuredWidth is
1464     * not null, return in it the actual width measured.
1465     *
1466     * @param text  The text to measure. Cannot be null.
1467     * @param start The offset into text to begin measuring at
1468     * @param end   The end of the text slice to measure.
1469     * @param measureForwards If true, measure forwards, starting at start.
1470     *                        Otherwise, measure backwards, starting with end.
1471     * @param maxWidth The maximum width to accumulate.
1472     * @param measuredWidth Optional. If not null, returns the actual width
1473     *                     measured.
1474     * @return The number of chars that were measured. Will always be <=
1475     *         abs(end - start).
1476     */
1477    public int breakText(CharSequence text, int start, int end,
1478                         boolean measureForwards,
1479                         float maxWidth, float[] measuredWidth) {
1480        if (text == null) {
1481            throw new IllegalArgumentException("text cannot be null");
1482        }
1483        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1484            throw new IndexOutOfBoundsException();
1485        }
1486
1487        if (text.length() == 0 || start == end) {
1488            return 0;
1489        }
1490        if (start == 0 && text instanceof String && end == text.length()) {
1491            return breakText((String) text, measureForwards, maxWidth,
1492                             measuredWidth);
1493        }
1494
1495        char[] buf = TemporaryBuffer.obtain(end - start);
1496        int result;
1497
1498        TextUtils.getChars(text, start, end, buf, 0);
1499
1500        if (measureForwards) {
1501            result = breakText(buf, 0, end - start, maxWidth, measuredWidth);
1502        } else {
1503            result = breakText(buf, 0, -(end - start), maxWidth, measuredWidth);
1504        }
1505
1506        TemporaryBuffer.recycle(buf);
1507        return result;
1508    }
1509
1510    /**
1511     * Measure the text, stopping early if the measured width exceeds maxWidth.
1512     * Return the number of chars that were measured, and if measuredWidth is
1513     * not null, return in it the actual width measured.
1514     *
1515     * @param text  The text to measure. Cannot be null.
1516     * @param measureForwards If true, measure forwards, starting with the
1517     *                        first character in the string. Otherwise,
1518     *                        measure backwards, starting with the
1519     *                        last character in the string.
1520     * @param maxWidth The maximum width to accumulate.
1521     * @param measuredWidth Optional. If not null, returns the actual width
1522     *                     measured.
1523     * @return The number of chars that were measured. Will always be <=
1524     *         abs(count).
1525     */
1526    public int breakText(String text, boolean measureForwards,
1527                                float maxWidth, float[] measuredWidth) {
1528        if (text == null) {
1529            throw new IllegalArgumentException("text cannot be null");
1530        }
1531
1532        if (text.length() == 0) {
1533            return 0;
1534        }
1535        if (!mHasCompatScaling) {
1536            return native_breakText(text, measureForwards, maxWidth, mBidiFlags, measuredWidth);
1537        }
1538
1539        final float oldSize = getTextSize();
1540        setTextSize(oldSize*mCompatScaling);
1541        int res = native_breakText(text, measureForwards, maxWidth*mCompatScaling, mBidiFlags,
1542                measuredWidth);
1543        setTextSize(oldSize);
1544        if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling;
1545        return res;
1546    }
1547
1548    private native int native_breakText(String text, boolean measureForwards,
1549                                        float maxWidth, int bidiFlags, float[] measuredWidth);
1550
1551    /**
1552     * Return the advance widths for the characters in the string.
1553     *
1554     * @param text     The text to measure. Cannot be null.
1555     * @param index    The index of the first char to to measure
1556     * @param count    The number of chars starting with index to measure
1557     * @param widths   array to receive the advance widths of the characters.
1558     *                 Must be at least a large as count.
1559     * @return         the actual number of widths returned.
1560     */
1561    public int getTextWidths(char[] text, int index, int count,
1562                             float[] widths) {
1563        if (text == null) {
1564            throw new IllegalArgumentException("text cannot be null");
1565        }
1566        if ((index | count) < 0 || index + count > text.length
1567                || count > widths.length) {
1568            throw new ArrayIndexOutOfBoundsException();
1569        }
1570
1571        if (text.length == 0 || count == 0) {
1572            return 0;
1573        }
1574        if (!mHasCompatScaling) {
1575            return native_getTextWidths(mNativePaint, text, index, count, mBidiFlags, widths);
1576        }
1577
1578        final float oldSize = getTextSize();
1579        setTextSize(oldSize*mCompatScaling);
1580        int res = native_getTextWidths(mNativePaint, text, index, count, mBidiFlags, widths);
1581        setTextSize(oldSize);
1582        for (int i=0; i<res; i++) {
1583            widths[i] *= mInvCompatScaling;
1584        }
1585        return res;
1586    }
1587
1588    /**
1589     * Return the advance widths for the characters in the string.
1590     *
1591     * @param text     The text to measure. Cannot be null.
1592     * @param start    The index of the first char to to measure
1593     * @param end      The end of the text slice to measure
1594     * @param widths   array to receive the advance widths of the characters.
1595     *                 Must be at least a large as (end - start).
1596     * @return         the actual number of widths returned.
1597     */
1598    public int getTextWidths(CharSequence text, int start, int end,
1599                             float[] widths) {
1600        if (text == null) {
1601            throw new IllegalArgumentException("text cannot be null");
1602        }
1603        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1604            throw new IndexOutOfBoundsException();
1605        }
1606        if (end - start > widths.length) {
1607            throw new ArrayIndexOutOfBoundsException();
1608        }
1609
1610        if (text.length() == 0 || start == end) {
1611            return 0;
1612        }
1613        if (text instanceof String) {
1614            return getTextWidths((String) text, start, end, widths);
1615        }
1616        if (text instanceof SpannedString ||
1617            text instanceof SpannableString) {
1618            return getTextWidths(text.toString(), start, end, widths);
1619        }
1620        if (text instanceof GraphicsOperations) {
1621            return ((GraphicsOperations) text).getTextWidths(start, end,
1622                                                                 widths, this);
1623        }
1624
1625        char[] buf = TemporaryBuffer.obtain(end - start);
1626        TextUtils.getChars(text, start, end, buf, 0);
1627        int result = getTextWidths(buf, 0, end - start, widths);
1628        TemporaryBuffer.recycle(buf);
1629        return result;
1630    }
1631
1632    /**
1633     * Return the advance widths for the characters in the string.
1634     *
1635     * @param text   The text to measure. Cannot be null.
1636     * @param start  The index of the first char to to measure
1637     * @param end    The end of the text slice to measure
1638     * @param widths array to receive the advance widths of the characters.
1639     *               Must be at least a large as the text.
1640     * @return       the number of unichars in the specified text.
1641     */
1642    public int getTextWidths(String text, int start, int end, float[] widths) {
1643        if (text == null) {
1644            throw new IllegalArgumentException("text cannot be null");
1645        }
1646        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1647            throw new IndexOutOfBoundsException();
1648        }
1649        if (end - start > widths.length) {
1650            throw new ArrayIndexOutOfBoundsException();
1651        }
1652
1653        if (text.length() == 0 || start == end) {
1654            return 0;
1655        }
1656        if (!mHasCompatScaling) {
1657            return native_getTextWidths(mNativePaint, text, start, end, mBidiFlags, widths);
1658        }
1659
1660        final float oldSize = getTextSize();
1661        setTextSize(oldSize*mCompatScaling);
1662        int res = native_getTextWidths(mNativePaint, text, start, end, mBidiFlags, widths);
1663        setTextSize(oldSize);
1664        for (int i=0; i<res; i++) {
1665            widths[i] *= mInvCompatScaling;
1666        }
1667        return res;
1668    }
1669
1670    /**
1671     * Return the advance widths for the characters in the string.
1672     *
1673     * @param text   The text to measure
1674     * @param widths array to receive the advance widths of the characters.
1675     *               Must be at least a large as the text.
1676     * @return       the number of unichars in the specified text.
1677     */
1678    public int getTextWidths(String text, float[] widths) {
1679        return getTextWidths(text, 0, text.length(), widths);
1680    }
1681
1682    /**
1683     * Return the glyph Ids for the characters in the string.
1684     *
1685     * @param text   The text to measure
1686     * @param start  The index of the first char to to measure
1687     * @param end    The end of the text slice to measure
1688     * @param contextStart the index of the first character to use for shaping context,
1689     * must be <= start
1690     * @param contextEnd the index past the last character to use for shaping context,
1691     * must be >= end
1692     * @param flags the flags to control the advances, either {@link #DIRECTION_LTR}
1693     * or {@link #DIRECTION_RTL}
1694     * @param glyphs array to receive the glyph Ids of the characters.
1695     *               Must be at least a large as the text.
1696     * @return       the number of glyphs in the returned array
1697     *
1698     * @hide
1699     *
1700     * Used only for BiDi / RTL Tests
1701     */
1702    public int getTextGlyphs(String text, int start, int end, int contextStart, int contextEnd,
1703            int flags, char[] glyphs) {
1704        if (text == null) {
1705            throw new IllegalArgumentException("text cannot be null");
1706        }
1707        if (flags != DIRECTION_LTR && flags != DIRECTION_RTL) {
1708            throw new IllegalArgumentException("unknown flags value: " + flags);
1709        }
1710        if ((start | end | contextStart | contextEnd | (end - start)
1711                | (start - contextStart) | (contextEnd - end) | (text.length() - end)
1712                | (text.length() - contextEnd)) < 0) {
1713            throw new IndexOutOfBoundsException();
1714        }
1715        if (end - start > glyphs.length) {
1716            throw new ArrayIndexOutOfBoundsException();
1717        }
1718        return native_getTextGlyphs(mNativePaint, text, start, end, contextStart, contextEnd,
1719                flags, glyphs);
1720    }
1721
1722    /**
1723     * Convenience overload that takes a char array instead of a
1724     * String.
1725     *
1726     * @see #getTextRunAdvances(String, int, int, int, int, int, float[], int)
1727     * @hide
1728     */
1729    public float getTextRunAdvances(char[] chars, int index, int count,
1730            int contextIndex, int contextCount, int flags, float[] advances,
1731            int advancesIndex) {
1732
1733        if (chars == null) {
1734            throw new IllegalArgumentException("text cannot be null");
1735        }
1736        if (flags != DIRECTION_LTR && flags != DIRECTION_RTL) {
1737            throw new IllegalArgumentException("unknown flags value: " + flags);
1738        }
1739        if ((index | count | contextIndex | contextCount | advancesIndex
1740                | (index - contextIndex) | (contextCount - count)
1741                | ((contextIndex + contextCount) - (index + count))
1742                | (chars.length - (contextIndex + contextCount))
1743                | (advances == null ? 0 :
1744                    (advances.length - (advancesIndex + count)))) < 0) {
1745            throw new IndexOutOfBoundsException();
1746        }
1747
1748        if (chars.length == 0 || count == 0){
1749            return 0f;
1750        }
1751        if (!mHasCompatScaling) {
1752            return native_getTextRunAdvances(mNativePaint, chars, index, count,
1753                    contextIndex, contextCount, flags, advances, advancesIndex);
1754        }
1755
1756        final float oldSize = getTextSize();
1757        setTextSize(oldSize * mCompatScaling);
1758        float res = native_getTextRunAdvances(mNativePaint, chars, index, count,
1759                contextIndex, contextCount, flags, advances, advancesIndex);
1760        setTextSize(oldSize);
1761
1762        if (advances != null) {
1763            for (int i = advancesIndex, e = i + count; i < e; i++) {
1764                advances[i] *= mInvCompatScaling;
1765            }
1766        }
1767        return res * mInvCompatScaling; // assume errors are not significant
1768    }
1769
1770    /**
1771     * Convenience overload that takes a CharSequence instead of a
1772     * String.
1773     *
1774     * @see #getTextRunAdvances(String, int, int, int, int, int, float[], int)
1775     * @hide
1776     */
1777    public float getTextRunAdvances(CharSequence text, int start, int end,
1778            int contextStart, int contextEnd, int flags, float[] advances,
1779            int advancesIndex) {
1780
1781        if (text == null) {
1782            throw new IllegalArgumentException("text cannot be null");
1783        }
1784        if ((start | end | contextStart | contextEnd | advancesIndex | (end - start)
1785                | (start - contextStart) | (contextEnd - end)
1786                | (text.length() - contextEnd)
1787                | (advances == null ? 0 :
1788                    (advances.length - advancesIndex - (end - start)))) < 0) {
1789            throw new IndexOutOfBoundsException();
1790        }
1791
1792        if (text instanceof String) {
1793            return getTextRunAdvances((String) text, start, end,
1794                    contextStart, contextEnd, flags, advances, advancesIndex);
1795        }
1796        if (text instanceof SpannedString ||
1797            text instanceof SpannableString) {
1798            return getTextRunAdvances(text.toString(), start, end,
1799                    contextStart, contextEnd, flags, advances, advancesIndex);
1800        }
1801        if (text instanceof GraphicsOperations) {
1802            return ((GraphicsOperations) text).getTextRunAdvances(start, end,
1803                    contextStart, contextEnd, flags, advances, advancesIndex, this);
1804        }
1805        if (text.length() == 0 || end == start) {
1806            return 0f;
1807        }
1808
1809        int contextLen = contextEnd - contextStart;
1810        int len = end - start;
1811        char[] buf = TemporaryBuffer.obtain(contextLen);
1812        TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
1813        float result = getTextRunAdvances(buf, start - contextStart, len,
1814                0, contextLen, flags, advances, advancesIndex);
1815        TemporaryBuffer.recycle(buf);
1816        return result;
1817    }
1818
1819    /**
1820     * Returns the total advance width for the characters in the run
1821     * between start and end, and if advances is not null, the advance
1822     * assigned to each of these characters (java chars).
1823     *
1824     * <p>The trailing surrogate in a valid surrogate pair is assigned
1825     * an advance of 0.  Thus the number of returned advances is
1826     * always equal to count, not to the number of unicode codepoints
1827     * represented by the run.
1828     *
1829     * <p>In the case of conjuncts or combining marks, the total
1830     * advance is assigned to the first logical character, and the
1831     * following characters are assigned an advance of 0.
1832     *
1833     * <p>This generates the sum of the advances of glyphs for
1834     * characters in a reordered cluster as the width of the first
1835     * logical character in the cluster, and 0 for the widths of all
1836     * other characters in the cluster.  In effect, such clusters are
1837     * treated like conjuncts.
1838     *
1839     * <p>The shaping bounds limit the amount of context available
1840     * outside start and end that can be used for shaping analysis.
1841     * These bounds typically reflect changes in bidi level or font
1842     * metrics across which shaping does not occur.
1843     *
1844     * @param text the text to measure. Cannot be null.
1845     * @param start the index of the first character to measure
1846     * @param end the index past the last character to measure
1847     * @param contextStart the index of the first character to use for shaping context,
1848     * must be <= start
1849     * @param contextEnd the index past the last character to use for shaping context,
1850     * must be >= end
1851     * @param flags the flags to control the advances, either {@link #DIRECTION_LTR}
1852     * or {@link #DIRECTION_RTL}
1853     * @param advances array to receive the advances, must have room for all advances,
1854     * can be null if only total advance is needed
1855     * @param advancesIndex the position in advances at which to put the
1856     * advance corresponding to the character at start
1857     * @return the total advance
1858     *
1859     * @hide
1860     */
1861    public float getTextRunAdvances(String text, int start, int end, int contextStart,
1862            int contextEnd, int flags, float[] advances, int advancesIndex) {
1863
1864        if (text == null) {
1865            throw new IllegalArgumentException("text cannot be null");
1866        }
1867        if (flags != DIRECTION_LTR && flags != DIRECTION_RTL) {
1868            throw new IllegalArgumentException("unknown flags value: " + flags);
1869        }
1870        if ((start | end | contextStart | contextEnd | advancesIndex | (end - start)
1871                | (start - contextStart) | (contextEnd - end)
1872                | (text.length() - contextEnd)
1873                | (advances == null ? 0 :
1874                    (advances.length - advancesIndex - (end - start)))) < 0) {
1875            throw new IndexOutOfBoundsException();
1876        }
1877
1878        if (text.length() == 0 || start == end) {
1879            return 0f;
1880        }
1881
1882        if (!mHasCompatScaling) {
1883            return native_getTextRunAdvances(mNativePaint, text, start, end,
1884                    contextStart, contextEnd, flags, advances, advancesIndex);
1885        }
1886
1887        final float oldSize = getTextSize();
1888        setTextSize(oldSize * mCompatScaling);
1889        float totalAdvance = native_getTextRunAdvances(mNativePaint, text, start, end,
1890                contextStart, contextEnd, flags, advances, advancesIndex);
1891        setTextSize(oldSize);
1892
1893        if (advances != null) {
1894            for (int i = advancesIndex, e = i + (end - start); i < e; i++) {
1895                advances[i] *= mInvCompatScaling;
1896            }
1897        }
1898        return totalAdvance * mInvCompatScaling; // assume errors are insignificant
1899    }
1900
1901    /**
1902     * Returns the next cursor position in the run.  This avoids placing the
1903     * cursor between surrogates, between characters that form conjuncts,
1904     * between base characters and combining marks, or within a reordering
1905     * cluster.
1906     *
1907     * <p>ContextStart and offset are relative to the start of text.
1908     * The context is the shaping context for cursor movement, generally
1909     * the bounds of the metric span enclosing the cursor in the direction of
1910     * movement.
1911     *
1912     * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid
1913     * cursor position, this returns -1.  Otherwise this will never return a
1914     * value before contextStart or after contextStart + contextLength.
1915     *
1916     * @param text the text
1917     * @param contextStart the start of the context
1918     * @param contextLength the length of the context
1919     * @param flags either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR}
1920     * @param offset the cursor position to move from
1921     * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER},
1922     * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE},
1923     * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT}
1924     * @return the offset of the next position, or -1
1925     * @hide
1926     */
1927    public int getTextRunCursor(char[] text, int contextStart, int contextLength,
1928            int flags, int offset, int cursorOpt) {
1929        int contextEnd = contextStart + contextLength;
1930        if (((contextStart | contextEnd | offset | (contextEnd - contextStart)
1931                | (offset - contextStart) | (contextEnd - offset)
1932                | (text.length - contextEnd) | cursorOpt) < 0)
1933                || cursorOpt > CURSOR_OPT_MAX_VALUE) {
1934            throw new IndexOutOfBoundsException();
1935        }
1936
1937        return native_getTextRunCursor(mNativePaint, text,
1938                contextStart, contextLength, flags, offset, cursorOpt);
1939    }
1940
1941    /**
1942     * Returns the next cursor position in the run.  This avoids placing the
1943     * cursor between surrogates, between characters that form conjuncts,
1944     * between base characters and combining marks, or within a reordering
1945     * cluster.
1946     *
1947     * <p>ContextStart, contextEnd, and offset are relative to the start of
1948     * text.  The context is the shaping context for cursor movement, generally
1949     * the bounds of the metric span enclosing the cursor in the direction of
1950     * movement.
1951     *
1952     * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid
1953     * cursor position, this returns -1.  Otherwise this will never return a
1954     * value before contextStart or after contextEnd.
1955     *
1956     * @param text the text
1957     * @param contextStart the start of the context
1958     * @param contextEnd the end of the context
1959     * @param flags either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR}
1960     * @param offset the cursor position to move from
1961     * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER},
1962     * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE},
1963     * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT}
1964     * @return the offset of the next position, or -1
1965     * @hide
1966     */
1967    public int getTextRunCursor(CharSequence text, int contextStart,
1968           int contextEnd, int flags, int offset, int cursorOpt) {
1969
1970        if (text instanceof String || text instanceof SpannedString ||
1971                text instanceof SpannableString) {
1972            return getTextRunCursor(text.toString(), contextStart, contextEnd,
1973                    flags, offset, cursorOpt);
1974        }
1975        if (text instanceof GraphicsOperations) {
1976            return ((GraphicsOperations) text).getTextRunCursor(
1977                    contextStart, contextEnd, flags, offset, cursorOpt, this);
1978        }
1979
1980        int contextLen = contextEnd - contextStart;
1981        char[] buf = TemporaryBuffer.obtain(contextLen);
1982        TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
1983        int result = getTextRunCursor(buf, 0, contextLen, flags, offset - contextStart, cursorOpt);
1984        TemporaryBuffer.recycle(buf);
1985        return result;
1986    }
1987
1988    /**
1989     * Returns the next cursor position in the run.  This avoids placing the
1990     * cursor between surrogates, between characters that form conjuncts,
1991     * between base characters and combining marks, or within a reordering
1992     * cluster.
1993     *
1994     * <p>ContextStart, contextEnd, and offset are relative to the start of
1995     * text.  The context is the shaping context for cursor movement, generally
1996     * the bounds of the metric span enclosing the cursor in the direction of
1997     * movement.
1998     *
1999     * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid
2000     * cursor position, this returns -1.  Otherwise this will never return a
2001     * value before contextStart or after contextEnd.
2002     *
2003     * @param text the text
2004     * @param contextStart the start of the context
2005     * @param contextEnd the end of the context
2006     * @param flags either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR}
2007     * @param offset the cursor position to move from
2008     * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER},
2009     * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE},
2010     * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT}
2011     * @return the offset of the next position, or -1
2012     * @hide
2013     */
2014    public int getTextRunCursor(String text, int contextStart, int contextEnd,
2015            int flags, int offset, int cursorOpt) {
2016        if (((contextStart | contextEnd | offset | (contextEnd - contextStart)
2017                | (offset - contextStart) | (contextEnd - offset)
2018                | (text.length() - contextEnd) | cursorOpt) < 0)
2019                || cursorOpt > CURSOR_OPT_MAX_VALUE) {
2020            throw new IndexOutOfBoundsException();
2021        }
2022
2023        return native_getTextRunCursor(mNativePaint, text,
2024                contextStart, contextEnd, flags, offset, cursorOpt);
2025    }
2026
2027    /**
2028     * Return the path (outline) for the specified text.
2029     * Note: just like Canvas.drawText, this will respect the Align setting in
2030     * the paint.
2031     *
2032     * @param text     The text to retrieve the path from
2033     * @param index    The index of the first character in text
2034     * @param count    The number of characterss starting with index
2035     * @param x        The x coordinate of the text's origin
2036     * @param y        The y coordinate of the text's origin
2037     * @param path     The path to receive the data describing the text. Must
2038     *                 be allocated by the caller.
2039     */
2040    public void getTextPath(char[] text, int index, int count,
2041                            float x, float y, Path path) {
2042        if ((index | count) < 0 || index + count > text.length) {
2043            throw new ArrayIndexOutOfBoundsException();
2044        }
2045        native_getTextPath(mNativePaint, mBidiFlags, text, index, count, x, y,
2046                path.ni());
2047    }
2048
2049    /**
2050     * Return the path (outline) for the specified text.
2051     * Note: just like Canvas.drawText, this will respect the Align setting
2052     * in the paint.
2053     *
2054     * @param text  The text to retrieve the path from
2055     * @param start The first character in the text
2056     * @param end   1 past the last charcter in the text
2057     * @param x     The x coordinate of the text's origin
2058     * @param y     The y coordinate of the text's origin
2059     * @param path  The path to receive the data describing the text. Must
2060     *              be allocated by the caller.
2061     */
2062    public void getTextPath(String text, int start, int end,
2063                            float x, float y, Path path) {
2064        if ((start | end | (end - start) | (text.length() - end)) < 0) {
2065            throw new IndexOutOfBoundsException();
2066        }
2067        native_getTextPath(mNativePaint, mBidiFlags, text, start, end, x, y,
2068                path.ni());
2069    }
2070
2071    /**
2072     * Return in bounds (allocated by the caller) the smallest rectangle that
2073     * encloses all of the characters, with an implied origin at (0,0).
2074     *
2075     * @param text  String to measure and return its bounds
2076     * @param start Index of the first char in the string to measure
2077     * @param end   1 past the last char in the string measure
2078     * @param bounds Returns the unioned bounds of all the text. Must be
2079     *               allocated by the caller.
2080     */
2081    public void getTextBounds(String text, int start, int end, Rect bounds) {
2082        if ((start | end | (end - start) | (text.length() - end)) < 0) {
2083            throw new IndexOutOfBoundsException();
2084        }
2085        if (bounds == null) {
2086            throw new NullPointerException("need bounds Rect");
2087        }
2088        nativeGetStringBounds(mNativePaint, text, start, end, mBidiFlags, bounds);
2089    }
2090
2091    /**
2092     * Return in bounds (allocated by the caller) the smallest rectangle that
2093     * encloses all of the characters, with an implied origin at (0,0).
2094     *
2095     * @param text  Array of chars to measure and return their unioned bounds
2096     * @param index Index of the first char in the array to measure
2097     * @param count The number of chars, beginning at index, to measure
2098     * @param bounds Returns the unioned bounds of all the text. Must be
2099     *               allocated by the caller.
2100     */
2101    public void getTextBounds(char[] text, int index, int count, Rect bounds) {
2102        if ((index | count) < 0 || index + count > text.length) {
2103            throw new ArrayIndexOutOfBoundsException();
2104        }
2105        if (bounds == null) {
2106            throw new NullPointerException("need bounds Rect");
2107        }
2108        nativeGetCharArrayBounds(mNativePaint, text, index, count, mBidiFlags, bounds);
2109    }
2110
2111    @Override
2112    protected void finalize() throws Throwable {
2113        try {
2114            finalizer(mNativePaint);
2115        } finally {
2116            super.finalize();
2117        }
2118    }
2119
2120    private static native int native_init();
2121    private static native int native_initWithPaint(int paint);
2122    private static native void native_reset(int native_object);
2123    private static native void native_set(int native_dst, int native_src);
2124    private static native int native_getStyle(int native_object);
2125    private static native void native_setStyle(int native_object, int style);
2126    private static native int native_getStrokeCap(int native_object);
2127    private static native void native_setStrokeCap(int native_object, int cap);
2128    private static native int native_getStrokeJoin(int native_object);
2129    private static native void native_setStrokeJoin(int native_object,
2130                                                    int join);
2131    private static native boolean native_getFillPath(int native_object,
2132                                                     int src, int dst);
2133    private static native int native_setShader(int native_object, int shader);
2134    private static native int native_setColorFilter(int native_object,
2135                                                    int filter);
2136    private static native int native_setXfermode(int native_object,
2137                                                 int xfermode);
2138    private static native int native_setPathEffect(int native_object,
2139                                                   int effect);
2140    private static native int native_setMaskFilter(int native_object,
2141                                                   int maskfilter);
2142    private static native int native_setTypeface(int native_object,
2143                                                 int typeface);
2144    private static native int native_setRasterizer(int native_object,
2145                                                   int rasterizer);
2146
2147    private static native int native_getTextAlign(int native_object);
2148    private static native void native_setTextAlign(int native_object,
2149                                                   int align);
2150
2151    private static native void native_setTextLocale(int native_object,
2152                                                    String locale);
2153
2154    private static native int native_getTextWidths(int native_object,
2155                            char[] text, int index, int count, int bidiFlags, float[] widths);
2156    private static native int native_getTextWidths(int native_object,
2157                            String text, int start, int end, int bidiFlags, float[] widths);
2158
2159    private static native int native_getTextGlyphs(int native_object,
2160            String text, int start, int end, int contextStart, int contextEnd,
2161            int flags, char[] glyphs);
2162
2163    private static native float native_getTextRunAdvances(int native_object,
2164            char[] text, int index, int count, int contextIndex, int contextCount,
2165            int flags, float[] advances, int advancesIndex);
2166    private static native float native_getTextRunAdvances(int native_object,
2167            String text, int start, int end, int contextStart, int contextEnd,
2168            int flags, float[] advances, int advancesIndex);
2169
2170    private native int native_getTextRunCursor(int native_object, char[] text,
2171            int contextStart, int contextLength, int flags, int offset, int cursorOpt);
2172    private native int native_getTextRunCursor(int native_object, String text,
2173            int contextStart, int contextEnd, int flags, int offset, int cursorOpt);
2174
2175    private static native void native_getTextPath(int native_object, int bidiFlags,
2176                char[] text, int index, int count, float x, float y, int path);
2177    private static native void native_getTextPath(int native_object, int bidiFlags,
2178                String text, int start, int end, float x, float y, int path);
2179    private static native void nativeGetStringBounds(int nativePaint,
2180                                String text, int start, int end, int bidiFlags, Rect bounds);
2181    private static native void nativeGetCharArrayBounds(int nativePaint,
2182                                char[] text, int index, int count, int bidiFlags, Rect bounds);
2183    private static native void finalizer(int nativePaint);
2184}
2185