Paint.java revision dcc3630100b406e4e6cb18f00d39059690153292
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.annotation.ColorInt;
20import android.annotation.NonNull;
21import android.annotation.Size;
22import android.os.LocaleList;
23import android.text.GraphicsOperations;
24import android.text.SpannableString;
25import android.text.SpannedString;
26import android.text.TextUtils;
27
28import com.android.internal.annotations.GuardedBy;
29
30import dalvik.annotation.optimization.CriticalNative;
31import dalvik.annotation.optimization.FastNative;
32
33import java.util.HashMap;
34import java.util.Locale;
35
36import libcore.util.NativeAllocationRegistry;
37
38/**
39 * The Paint class holds the style and color information about how to draw
40 * geometries, text and bitmaps.
41 */
42public class Paint {
43
44    private long mNativePaint;
45    private long mNativeShader = 0;
46
47    // The approximate size of a native paint object.
48    private static final long NATIVE_PAINT_SIZE = 98;
49
50    // Use a Holder to allow static initialization of Paint in the boot image.
51    private static class NoImagePreloadHolder {
52        public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
53                Paint.class.getClassLoader(), nGetNativeFinalizer(), NATIVE_PAINT_SIZE);
54    }
55
56    /**
57     * @hide
58     */
59    public long mNativeTypeface;
60
61    private ColorFilter mColorFilter;
62    private MaskFilter  mMaskFilter;
63    private PathEffect  mPathEffect;
64    private Shader      mShader;
65    private Typeface    mTypeface;
66    private Xfermode    mXfermode;
67
68    private boolean     mHasCompatScaling;
69    private float       mCompatScaling;
70    private float       mInvCompatScaling;
71
72    private LocaleList  mLocales;
73    private String      mFontFeatureSettings;
74
75    private static final Object sCacheLock = new Object();
76
77    /**
78     * Cache for the Minikin language list ID.
79     *
80     * A map from a string representation of the LocaleList to Minikin's language list ID.
81     */
82    @GuardedBy("sCacheLock")
83    private static final HashMap<String, Integer> sMinikinLangListIdCache = new HashMap<>();
84
85    /**
86     * @hide
87     */
88    public  int         mBidiFlags = BIDI_DEFAULT_LTR;
89
90    static final Style[] sStyleArray = {
91        Style.FILL, Style.STROKE, Style.FILL_AND_STROKE
92    };
93    static final Cap[] sCapArray = {
94        Cap.BUTT, Cap.ROUND, Cap.SQUARE
95    };
96    static final Join[] sJoinArray = {
97        Join.MITER, Join.ROUND, Join.BEVEL
98    };
99    static final Align[] sAlignArray = {
100        Align.LEFT, Align.CENTER, Align.RIGHT
101    };
102
103    /**
104     * Paint flag that enables antialiasing when drawing.
105     *
106     * <p>Enabling this flag will cause all draw operations that support
107     * antialiasing to use it.</p>
108     *
109     * @see #Paint(int)
110     * @see #setFlags(int)
111     */
112    public static final int ANTI_ALIAS_FLAG     = 0x01;
113    /**
114     * Paint flag that enables bilinear sampling on scaled bitmaps.
115     *
116     * <p>If cleared, scaled bitmaps will be drawn with nearest neighbor
117     * sampling, likely resulting in artifacts. This should generally be on
118     * when drawing bitmaps, unless performance-bound (rendering to software
119     * canvas) or preferring pixelation artifacts to blurriness when scaling
120     * significantly.</p>
121     *
122     * <p>If bitmaps are scaled for device density at creation time (as
123     * resource bitmaps often are) the filtering will already have been
124     * done.</p>
125     *
126     * @see #Paint(int)
127     * @see #setFlags(int)
128     */
129    public static final int FILTER_BITMAP_FLAG  = 0x02;
130    /**
131     * Paint flag that enables dithering when blitting.
132     *
133     * <p>Enabling this flag applies a dither to any blit operation where the
134     * target's colour space is more constrained than the source.
135     *
136     * @see #Paint(int)
137     * @see #setFlags(int)
138     */
139    public static final int DITHER_FLAG         = 0x04;
140    /**
141     * Paint flag that applies an underline decoration to drawn text.
142     *
143     * @see #Paint(int)
144     * @see #setFlags(int)
145     */
146    public static final int UNDERLINE_TEXT_FLAG = 0x08;
147    /**
148     * Paint flag that applies a strike-through decoration to drawn text.
149     *
150     * @see #Paint(int)
151     * @see #setFlags(int)
152     */
153    public static final int STRIKE_THRU_TEXT_FLAG = 0x10;
154    /**
155     * Paint flag that applies a synthetic bolding effect to drawn text.
156     *
157     * <p>Enabling this flag will cause text draw operations to apply a
158     * simulated bold effect when drawing a {@link Typeface} that is not
159     * already bold.</p>
160     *
161     * @see #Paint(int)
162     * @see #setFlags(int)
163     */
164    public static final int FAKE_BOLD_TEXT_FLAG = 0x20;
165    /**
166     * Paint flag that enables smooth linear scaling of text.
167     *
168     * <p>Enabling this flag does not actually scale text, but rather adjusts
169     * text draw operations to deal gracefully with smooth adjustment of scale.
170     * When this flag is enabled, font hinting is disabled to prevent shape
171     * deformation between scale factors, and glyph caching is disabled due to
172     * the large number of glyph images that will be generated.</p>
173     *
174     * <p>{@link #SUBPIXEL_TEXT_FLAG} should be used in conjunction with this
175     * flag to prevent glyph positions from snapping to whole pixel values as
176     * scale factor is adjusted.</p>
177     *
178     * @see #Paint(int)
179     * @see #setFlags(int)
180     */
181    public static final int LINEAR_TEXT_FLAG    = 0x40;
182    /**
183     * Paint flag that enables subpixel positioning of text.
184     *
185     * <p>Enabling this flag causes glyph advances to be computed with subpixel
186     * accuracy.</p>
187     *
188     * <p>This can be used with {@link #LINEAR_TEXT_FLAG} to prevent text from
189     * jittering during smooth scale transitions.</p>
190     *
191     * @see #Paint(int)
192     * @see #setFlags(int)
193     */
194    public static final int SUBPIXEL_TEXT_FLAG  = 0x80;
195    /** Legacy Paint flag, no longer used. */
196    public static final int DEV_KERN_TEXT_FLAG  = 0x100;
197    /** @hide bit mask for the flag enabling subpixel glyph rendering for text */
198    public static final int LCD_RENDER_TEXT_FLAG = 0x200;
199    /**
200     * Paint flag that enables the use of bitmap fonts when drawing text.
201     *
202     * <p>Disabling this flag will prevent text draw operations from using
203     * embedded bitmap strikes in fonts, causing fonts with both scalable
204     * outlines and bitmap strikes to draw only the scalable outlines, and
205     * fonts with only bitmap strikes to not draw at all.</p>
206     *
207     * @see #Paint(int)
208     * @see #setFlags(int)
209     */
210    public static final int EMBEDDED_BITMAP_TEXT_FLAG = 0x400;
211    /** @hide bit mask for the flag forcing freetype's autohinter on for text */
212    public static final int AUTO_HINTING_TEXT_FLAG = 0x800;
213    /** @hide bit mask for the flag enabling vertical rendering for text */
214    public static final int VERTICAL_TEXT_FLAG = 0x1000;
215
216    // These flags are always set on a new/reset paint, even if flags 0 is passed.
217    static final int HIDDEN_DEFAULT_PAINT_FLAGS = DEV_KERN_TEXT_FLAG | EMBEDDED_BITMAP_TEXT_FLAG;
218
219    /**
220     * Font hinter option that disables font hinting.
221     *
222     * @see #setHinting(int)
223     */
224    public static final int HINTING_OFF = 0x0;
225
226    /**
227     * Font hinter option that enables font hinting.
228     *
229     * @see #setHinting(int)
230     */
231    public static final int HINTING_ON = 0x1;
232
233    /**
234     * Bidi flag to set LTR paragraph direction.
235     *
236     * @hide
237     */
238    public static final int BIDI_LTR = 0x0;
239
240    /**
241     * Bidi flag to set RTL paragraph direction.
242     *
243     * @hide
244     */
245    public static final int BIDI_RTL = 0x1;
246
247    /**
248     * Bidi flag to detect paragraph direction via heuristics, defaulting to
249     * LTR.
250     *
251     * @hide
252     */
253    public static final int BIDI_DEFAULT_LTR = 0x2;
254
255    /**
256     * Bidi flag to detect paragraph direction via heuristics, defaulting to
257     * RTL.
258     *
259     * @hide
260     */
261    public static final int BIDI_DEFAULT_RTL = 0x3;
262
263    /**
264     * Bidi flag to override direction to all LTR (ignore bidi).
265     *
266     * @hide
267     */
268    public static final int BIDI_FORCE_LTR = 0x4;
269
270    /**
271     * Bidi flag to override direction to all RTL (ignore bidi).
272     *
273     * @hide
274     */
275    public static final int BIDI_FORCE_RTL = 0x5;
276
277    /**
278     * Maximum Bidi flag value.
279     * @hide
280     */
281    private static final int BIDI_MAX_FLAG_VALUE = BIDI_FORCE_RTL;
282
283    /**
284     * Mask for bidi flags.
285     * @hide
286     */
287    private static final int BIDI_FLAG_MASK = 0x7;
288
289    /**
290     * Flag for getTextRunAdvances indicating left-to-right run direction.
291     * @hide
292     */
293    public static final int DIRECTION_LTR = 0;
294
295    /**
296     * Flag for getTextRunAdvances indicating right-to-left run direction.
297     * @hide
298     */
299    public static final int DIRECTION_RTL = 1;
300
301    /**
302     * Option for getTextRunCursor to compute the valid cursor after
303     * offset or the limit of the context, whichever is less.
304     * @hide
305     */
306    public static final int CURSOR_AFTER = 0;
307
308    /**
309     * Option for getTextRunCursor to compute the valid cursor at or after
310     * the offset or the limit of the context, whichever is less.
311     * @hide
312     */
313    public static final int CURSOR_AT_OR_AFTER = 1;
314
315     /**
316     * Option for getTextRunCursor to compute the valid cursor before
317     * offset or the start of the context, whichever is greater.
318     * @hide
319     */
320    public static final int CURSOR_BEFORE = 2;
321
322   /**
323     * Option for getTextRunCursor to compute the valid cursor at or before
324     * offset or the start of the context, whichever is greater.
325     * @hide
326     */
327    public static final int CURSOR_AT_OR_BEFORE = 3;
328
329    /**
330     * Option for getTextRunCursor to return offset if the cursor at offset
331     * is valid, or -1 if it isn't.
332     * @hide
333     */
334    public static final int CURSOR_AT = 4;
335
336    /**
337     * Maximum cursor option value.
338     */
339    private static final int CURSOR_OPT_MAX_VALUE = CURSOR_AT;
340
341    /**
342     * The Style specifies if the primitive being drawn is filled, stroked, or
343     * both (in the same color). The default is FILL.
344     */
345    public enum Style {
346        /**
347         * Geometry and text drawn with this style will be filled, ignoring all
348         * stroke-related settings in the paint.
349         */
350        FILL            (0),
351        /**
352         * Geometry and text drawn with this style will be stroked, respecting
353         * the stroke-related fields on the paint.
354         */
355        STROKE          (1),
356        /**
357         * Geometry and text drawn with this style will be both filled and
358         * stroked at the same time, respecting the stroke-related fields on
359         * the paint. This mode can give unexpected results if the geometry
360         * is oriented counter-clockwise. This restriction does not apply to
361         * either FILL or STROKE.
362         */
363        FILL_AND_STROKE (2);
364
365        Style(int nativeInt) {
366            this.nativeInt = nativeInt;
367        }
368        final int nativeInt;
369    }
370
371    /**
372     * The Cap specifies the treatment for the beginning and ending of
373     * stroked lines and paths. The default is BUTT.
374     */
375    public enum Cap {
376        /**
377         * The stroke ends with the path, and does not project beyond it.
378         */
379        BUTT    (0),
380        /**
381         * The stroke projects out as a semicircle, with the center at the
382         * end of the path.
383         */
384        ROUND   (1),
385        /**
386         * The stroke projects out as a square, with the center at the end
387         * of the path.
388         */
389        SQUARE  (2);
390
391        private Cap(int nativeInt) {
392            this.nativeInt = nativeInt;
393        }
394        final int nativeInt;
395    }
396
397    /**
398     * The Join specifies the treatment where lines and curve segments
399     * join on a stroked path. The default is MITER.
400     */
401    public enum Join {
402        /**
403         * The outer edges of a join meet at a sharp angle
404         */
405        MITER   (0),
406        /**
407         * The outer edges of a join meet in a circular arc.
408         */
409        ROUND   (1),
410        /**
411         * The outer edges of a join meet with a straight line
412         */
413        BEVEL   (2);
414
415        private Join(int nativeInt) {
416            this.nativeInt = nativeInt;
417        }
418        final int nativeInt;
419    }
420
421    /**
422     * Align specifies how drawText aligns its text relative to the
423     * [x,y] coordinates. The default is LEFT.
424     */
425    public enum Align {
426        /**
427         * The text is drawn to the right of the x,y origin
428         */
429        LEFT    (0),
430        /**
431         * The text is drawn centered horizontally on the x,y origin
432         */
433        CENTER  (1),
434        /**
435         * The text is drawn to the left of the x,y origin
436         */
437        RIGHT   (2);
438
439        private Align(int nativeInt) {
440            this.nativeInt = nativeInt;
441        }
442        final int nativeInt;
443    }
444
445    /**
446     * Create a new paint with default settings.
447     */
448    public Paint() {
449        this(0);
450    }
451
452    /**
453     * Create a new paint with the specified flags. Use setFlags() to change
454     * these after the paint is created.
455     *
456     * @param flags initial flag bits, as if they were passed via setFlags().
457     */
458    public Paint(int flags) {
459        mNativePaint = nInit();
460        NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mNativePaint);
461        setFlags(flags | HIDDEN_DEFAULT_PAINT_FLAGS);
462        // TODO: Turning off hinting has undesirable side effects, we need to
463        //       revisit hinting once we add support for subpixel positioning
464        // setHinting(DisplayMetrics.DENSITY_DEVICE >= DisplayMetrics.DENSITY_TV
465        //        ? HINTING_OFF : HINTING_ON);
466        mCompatScaling = mInvCompatScaling = 1;
467        setTextLocales(LocaleList.getAdjustedDefault());
468    }
469
470    /**
471     * Create a new paint, initialized with the attributes in the specified
472     * paint parameter.
473     *
474     * @param paint Existing paint used to initialized the attributes of the
475     *              new paint.
476     */
477    public Paint(Paint paint) {
478        mNativePaint = nInitWithPaint(paint.getNativeInstance());
479        NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mNativePaint);
480        setClassVariablesFrom(paint);
481    }
482
483    /** Restores the paint to its default settings. */
484    public void reset() {
485        nReset(mNativePaint);
486        setFlags(HIDDEN_DEFAULT_PAINT_FLAGS);
487
488        // TODO: Turning off hinting has undesirable side effects, we need to
489        //       revisit hinting once we add support for subpixel positioning
490        // setHinting(DisplayMetrics.DENSITY_DEVICE >= DisplayMetrics.DENSITY_TV
491        //        ? HINTING_OFF : HINTING_ON);
492
493        mColorFilter = null;
494        mMaskFilter = null;
495        mPathEffect = null;
496        mShader = null;
497        mNativeShader = 0;
498        mTypeface = null;
499        mNativeTypeface = 0;
500        mXfermode = null;
501
502        mHasCompatScaling = false;
503        mCompatScaling = 1;
504        mInvCompatScaling = 1;
505
506        mBidiFlags = BIDI_DEFAULT_LTR;
507        setTextLocales(LocaleList.getAdjustedDefault());
508        setElegantTextHeight(false);
509        mFontFeatureSettings = null;
510    }
511
512    /**
513     * Copy the fields from src into this paint. This is equivalent to calling
514     * get() on all of the src fields, and calling the corresponding set()
515     * methods on this.
516     */
517    public void set(Paint src) {
518        if (this != src) {
519            // copy over the native settings
520            nSet(mNativePaint, src.mNativePaint);
521            setClassVariablesFrom(src);
522        }
523    }
524
525    /**
526     * Set all class variables using current values from the given
527     * {@link Paint}.
528     */
529    private void setClassVariablesFrom(Paint paint) {
530        mColorFilter = paint.mColorFilter;
531        mMaskFilter = paint.mMaskFilter;
532        mPathEffect = paint.mPathEffect;
533        mShader = paint.mShader;
534        mNativeShader = paint.mNativeShader;
535        mTypeface = paint.mTypeface;
536        mNativeTypeface = paint.mNativeTypeface;
537        mXfermode = paint.mXfermode;
538
539        mHasCompatScaling = paint.mHasCompatScaling;
540        mCompatScaling = paint.mCompatScaling;
541        mInvCompatScaling = paint.mInvCompatScaling;
542
543        mBidiFlags = paint.mBidiFlags;
544        mLocales = paint.mLocales;
545        mFontFeatureSettings = paint.mFontFeatureSettings;
546    }
547
548    /** @hide */
549    public void setCompatibilityScaling(float factor) {
550        if (factor == 1.0) {
551            mHasCompatScaling = false;
552            mCompatScaling = mInvCompatScaling = 1.0f;
553        } else {
554            mHasCompatScaling = true;
555            mCompatScaling = factor;
556            mInvCompatScaling = 1.0f/factor;
557        }
558    }
559
560    /**
561     * Return the pointer to the native object while ensuring that any
562     * mutable objects that are attached to the paint are also up-to-date.
563     *
564     * @hide
565     */
566    public long getNativeInstance() {
567        long newNativeShader = mShader == null ? 0 : mShader.getNativeInstance();
568        if (newNativeShader != mNativeShader) {
569            mNativeShader = newNativeShader;
570            nSetShader(mNativePaint, mNativeShader);
571        }
572        return mNativePaint;
573    }
574
575    /**
576     * Return the bidi flags on the paint.
577     *
578     * @return the bidi flags on the paint
579     * @hide
580     */
581    public int getBidiFlags() {
582        return mBidiFlags;
583    }
584
585    /**
586     * Set the bidi flags on the paint.
587     * @hide
588     */
589    public void setBidiFlags(int flags) {
590        // only flag value is the 3-bit BIDI control setting
591        flags &= BIDI_FLAG_MASK;
592        if (flags > BIDI_MAX_FLAG_VALUE) {
593            throw new IllegalArgumentException("unknown bidi flag: " + flags);
594        }
595        mBidiFlags = flags;
596    }
597
598    /**
599     * Return the paint's flags. Use the Flag enum to test flag values.
600     *
601     * @return the paint's flags (see enums ending in _Flag for bit masks)
602     */
603    public int getFlags() {
604        return nGetFlags(mNativePaint);
605    }
606
607    /**
608     * Set the paint's flags. Use the Flag enum to specific flag values.
609     *
610     * @param flags The new flag bits for the paint
611     */
612    public void setFlags(int flags) {
613        nSetFlags(mNativePaint, flags);
614    }
615
616    /**
617     * Return the paint's hinting mode.  Returns either
618     * {@link #HINTING_OFF} or {@link #HINTING_ON}.
619     */
620    public int getHinting() {
621        return nGetHinting(mNativePaint);
622    }
623
624    /**
625     * Set the paint's hinting mode.  May be either
626     * {@link #HINTING_OFF} or {@link #HINTING_ON}.
627     */
628    public void setHinting(int mode) {
629        nSetHinting(mNativePaint, mode);
630    }
631
632    /**
633     * Helper for getFlags(), returning true if ANTI_ALIAS_FLAG bit is set
634     * AntiAliasing smooths out the edges of what is being drawn, but is has
635     * no impact on the interior of the shape. See setDither() and
636     * setFilterBitmap() to affect how colors are treated.
637     *
638     * @return true if the antialias bit is set in the paint's flags.
639     */
640    public final boolean isAntiAlias() {
641        return (getFlags() & ANTI_ALIAS_FLAG) != 0;
642    }
643
644    /**
645     * Helper for setFlags(), setting or clearing the ANTI_ALIAS_FLAG bit
646     * AntiAliasing smooths out the edges of what is being drawn, but is has
647     * no impact on the interior of the shape. See setDither() and
648     * setFilterBitmap() to affect how colors are treated.
649     *
650     * @param aa true to set the antialias bit in the flags, false to clear it
651     */
652    public void setAntiAlias(boolean aa) {
653        nSetAntiAlias(mNativePaint, aa);
654    }
655
656    /**
657     * Helper for getFlags(), returning true if DITHER_FLAG bit is set
658     * Dithering affects how colors that are higher precision than the device
659     * are down-sampled. No dithering is generally faster, but higher precision
660     * colors are just truncated down (e.g. 8888 -> 565). Dithering tries to
661     * distribute the error inherent in this process, to reduce the visual
662     * artifacts.
663     *
664     * @return true if the dithering bit is set in the paint's flags.
665     */
666    public final boolean isDither() {
667        return (getFlags() & DITHER_FLAG) != 0;
668    }
669
670    /**
671     * Helper for setFlags(), setting or clearing the DITHER_FLAG bit
672     * Dithering affects how colors that are higher precision than the device
673     * are down-sampled. No dithering is generally faster, but higher precision
674     * colors are just truncated down (e.g. 8888 -> 565). Dithering tries to
675     * distribute the error inherent in this process, to reduce the visual
676     * artifacts.
677     *
678     * @param dither true to set the dithering bit in flags, false to clear it
679     */
680    public void setDither(boolean dither) {
681        nSetDither(mNativePaint, dither);
682    }
683
684    /**
685     * Helper for getFlags(), returning true if LINEAR_TEXT_FLAG bit is set
686     *
687     * @return true if the lineartext bit is set in the paint's flags
688     */
689    public final boolean isLinearText() {
690        return (getFlags() & LINEAR_TEXT_FLAG) != 0;
691    }
692
693    /**
694     * Helper for setFlags(), setting or clearing the LINEAR_TEXT_FLAG bit
695     *
696     * @param linearText true to set the linearText bit in the paint's flags,
697     *                   false to clear it.
698     */
699    public void setLinearText(boolean linearText) {
700        nSetLinearText(mNativePaint, linearText);
701    }
702
703    /**
704     * Helper for getFlags(), returning true if SUBPIXEL_TEXT_FLAG bit is set
705     *
706     * @return true if the subpixel bit is set in the paint's flags
707     */
708    public final boolean isSubpixelText() {
709        return (getFlags() & SUBPIXEL_TEXT_FLAG) != 0;
710    }
711
712    /**
713     * Helper for setFlags(), setting or clearing the SUBPIXEL_TEXT_FLAG bit
714     *
715     * @param subpixelText true to set the subpixelText bit in the paint's
716     *                     flags, false to clear it.
717     */
718    public void setSubpixelText(boolean subpixelText) {
719        nSetSubpixelText(mNativePaint, subpixelText);
720    }
721
722    /**
723     * Helper for getFlags(), returning true if UNDERLINE_TEXT_FLAG bit is set
724     *
725     * @return true if the underlineText bit is set in the paint's flags.
726     */
727    public final boolean isUnderlineText() {
728        return (getFlags() & UNDERLINE_TEXT_FLAG) != 0;
729    }
730
731    /**
732     * Helper for setFlags(), setting or clearing the UNDERLINE_TEXT_FLAG bit
733     *
734     * @param underlineText true to set the underlineText bit in the paint's
735     *                      flags, false to clear it.
736     */
737    public void setUnderlineText(boolean underlineText) {
738        nSetUnderlineText(mNativePaint, underlineText);
739    }
740
741    /**
742     * Helper for getFlags(), returning true if STRIKE_THRU_TEXT_FLAG bit is set
743     *
744     * @return true if the strikeThruText bit is set in the paint's flags.
745     */
746    public final boolean isStrikeThruText() {
747        return (getFlags() & STRIKE_THRU_TEXT_FLAG) != 0;
748    }
749
750    /**
751     * Helper for setFlags(), setting or clearing the STRIKE_THRU_TEXT_FLAG bit
752     *
753     * @param strikeThruText true to set the strikeThruText bit in the paint's
754     *                       flags, false to clear it.
755     */
756    public void setStrikeThruText(boolean strikeThruText) {
757        nSetStrikeThruText(mNativePaint, strikeThruText);
758    }
759
760    /**
761     * Helper for getFlags(), returning true if FAKE_BOLD_TEXT_FLAG bit is set
762     *
763     * @return true if the fakeBoldText bit is set in the paint's flags.
764     */
765    public final boolean isFakeBoldText() {
766        return (getFlags() & FAKE_BOLD_TEXT_FLAG) != 0;
767    }
768
769    /**
770     * Helper for setFlags(), setting or clearing the FAKE_BOLD_TEXT_FLAG bit
771     *
772     * @param fakeBoldText true to set the fakeBoldText bit in the paint's
773     *                     flags, false to clear it.
774     */
775    public void setFakeBoldText(boolean fakeBoldText) {
776        nSetFakeBoldText(mNativePaint, fakeBoldText);
777    }
778
779    /**
780     * Whether or not the bitmap filter is activated.
781     * Filtering affects the sampling of bitmaps when they are transformed.
782     * Filtering does not affect how the colors in the bitmap are converted into
783     * device pixels. That is dependent on dithering and xfermodes.
784     *
785     * @see #setFilterBitmap(boolean) setFilterBitmap()
786     */
787    public final boolean isFilterBitmap() {
788        return (getFlags() & FILTER_BITMAP_FLAG) != 0;
789    }
790
791    /**
792     * Helper for setFlags(), setting or clearing the FILTER_BITMAP_FLAG bit.
793     * Filtering affects the sampling of bitmaps when they are transformed.
794     * Filtering does not affect how the colors in the bitmap are converted into
795     * device pixels. That is dependent on dithering and xfermodes.
796     *
797     * @param filter true to set the FILTER_BITMAP_FLAG bit in the paint's
798     *               flags, false to clear it.
799     */
800    public void setFilterBitmap(boolean filter) {
801        nSetFilterBitmap(mNativePaint, filter);
802    }
803
804    /**
805     * Return the paint's style, used for controlling how primitives'
806     * geometries are interpreted (except for drawBitmap, which always assumes
807     * FILL_STYLE).
808     *
809     * @return the paint's style setting (Fill, Stroke, StrokeAndFill)
810     */
811    public Style getStyle() {
812        return sStyleArray[nGetStyle(mNativePaint)];
813    }
814
815    /**
816     * Set the paint's style, used for controlling how primitives'
817     * geometries are interpreted (except for drawBitmap, which always assumes
818     * Fill).
819     *
820     * @param style The new style to set in the paint
821     */
822    public void setStyle(Style style) {
823        nSetStyle(mNativePaint, style.nativeInt);
824    }
825
826    /**
827     * Return the paint's color. Note that the color is a 32bit value
828     * containing alpha as well as r,g,b. This 32bit value is not premultiplied,
829     * meaning that its alpha can be any value, regardless of the values of
830     * r,g,b. See the Color class for more details.
831     *
832     * @return the paint's color (and alpha).
833     */
834    @ColorInt
835    public int getColor() {
836        return nGetColor(mNativePaint);
837    }
838
839    /**
840     * Set the paint's color. Note that the color is an int containing alpha
841     * as well as r,g,b. This 32bit value is not premultiplied, meaning that
842     * its alpha can be any value, regardless of the values of r,g,b.
843     * See the Color class for more details.
844     *
845     * @param color The new color (including alpha) to set in the paint.
846     */
847    public void setColor(@ColorInt int color) {
848        nSetColor(mNativePaint, color);
849    }
850
851    /**
852     * Helper to getColor() that just returns the color's alpha value. This is
853     * the same as calling getColor() >>> 24. It always returns a value between
854     * 0 (completely transparent) and 255 (completely opaque).
855     *
856     * @return the alpha component of the paint's color.
857     */
858    public int getAlpha() {
859        return nGetAlpha(mNativePaint);
860    }
861
862    /**
863     * Helper to setColor(), that only assigns the color's alpha value,
864     * leaving its r,g,b values unchanged. Results are undefined if the alpha
865     * value is outside of the range [0..255]
866     *
867     * @param a set the alpha component [0..255] of the paint's color.
868     */
869    public void setAlpha(int a) {
870        nSetAlpha(mNativePaint, a);
871    }
872
873    /**
874     * Helper to setColor(), that takes a,r,g,b and constructs the color int
875     *
876     * @param a The new alpha component (0..255) of the paint's color.
877     * @param r The new red component (0..255) of the paint's color.
878     * @param g The new green component (0..255) of the paint's color.
879     * @param b The new blue component (0..255) of the paint's color.
880     */
881    public void setARGB(int a, int r, int g, int b) {
882        setColor((a << 24) | (r << 16) | (g << 8) | b);
883    }
884
885    /**
886     * Return the width for stroking.
887     * <p />
888     * A value of 0 strokes in hairline mode.
889     * Hairlines always draws a single pixel independent of the canva's matrix.
890     *
891     * @return the paint's stroke width, used whenever the paint's style is
892     *         Stroke or StrokeAndFill.
893     */
894    public float getStrokeWidth() {
895        return nGetStrokeWidth(mNativePaint);
896    }
897
898    /**
899     * Set the width for stroking.
900     * Pass 0 to stroke in hairline mode.
901     * Hairlines always draws a single pixel independent of the canva's matrix.
902     *
903     * @param width set the paint's stroke width, used whenever the paint's
904     *              style is Stroke or StrokeAndFill.
905     */
906    public void setStrokeWidth(float width) {
907        nSetStrokeWidth(mNativePaint, width);
908    }
909
910    /**
911     * Return the paint's stroke miter value. Used to control the behavior
912     * of miter joins when the joins angle is sharp.
913     *
914     * @return the paint's miter limit, used whenever the paint's style is
915     *         Stroke or StrokeAndFill.
916     */
917    public float getStrokeMiter() {
918        return nGetStrokeMiter(mNativePaint);
919    }
920
921    /**
922     * Set the paint's stroke miter value. This is used to control the behavior
923     * of miter joins when the joins angle is sharp. This value must be >= 0.
924     *
925     * @param miter set the miter limit on the paint, used whenever the paint's
926     *              style is Stroke or StrokeAndFill.
927     */
928    public void setStrokeMiter(float miter) {
929        nSetStrokeMiter(mNativePaint, miter);
930    }
931
932    /**
933     * Return the paint's Cap, controlling how the start and end of stroked
934     * lines and paths are treated.
935     *
936     * @return the line cap style for the paint, used whenever the paint's
937     *         style is Stroke or StrokeAndFill.
938     */
939    public Cap getStrokeCap() {
940        return sCapArray[nGetStrokeCap(mNativePaint)];
941    }
942
943    /**
944     * Set the paint's Cap.
945     *
946     * @param cap set the paint's line cap style, used whenever the paint's
947     *            style is Stroke or StrokeAndFill.
948     */
949    public void setStrokeCap(Cap cap) {
950        nSetStrokeCap(mNativePaint, cap.nativeInt);
951    }
952
953    /**
954     * Return the paint's stroke join type.
955     *
956     * @return the paint's Join.
957     */
958    public Join getStrokeJoin() {
959        return sJoinArray[nGetStrokeJoin(mNativePaint)];
960    }
961
962    /**
963     * Set the paint's Join.
964     *
965     * @param join set the paint's Join, used whenever the paint's style is
966     *             Stroke or StrokeAndFill.
967     */
968    public void setStrokeJoin(Join join) {
969        nSetStrokeJoin(mNativePaint, join.nativeInt);
970    }
971
972    /**
973     * Applies any/all effects (patheffect, stroking) to src, returning the
974     * result in dst. The result is that drawing src with this paint will be
975     * the same as drawing dst with a default paint (at least from the
976     * geometric perspective).
977     *
978     * @param src input path
979     * @param dst output path (may be the same as src)
980     * @return    true if the path should be filled, or false if it should be
981     *                 drawn with a hairline (width == 0)
982     */
983    public boolean getFillPath(Path src, Path dst) {
984        return nGetFillPath(mNativePaint, src.readOnlyNI(), dst.mutateNI());
985    }
986
987    /**
988     * Get the paint's shader object.
989     *
990     * @return the paint's shader (or null)
991     */
992    public Shader getShader() {
993        return mShader;
994    }
995
996    /**
997     * Set or clear the shader object.
998     * <p />
999     * Pass null to clear any previous shader.
1000     * As a convenience, the parameter passed is also returned.
1001     *
1002     * @param shader May be null. the new shader to be installed in the paint
1003     * @return       shader
1004     */
1005    public Shader setShader(Shader shader) {
1006        // If mShader changes, cached value of native shader aren't valid, since
1007        // old shader's pointer may be reused by another shader allocation later
1008        if (mShader != shader) {
1009            mNativeShader = -1;
1010        }
1011        // Defer setting the shader natively until getNativeInstance() is called
1012        mShader = shader;
1013        return shader;
1014    }
1015
1016    /**
1017     * Get the paint's colorfilter (maybe be null).
1018     *
1019     * @return the paint's colorfilter (maybe be null)
1020     */
1021    public ColorFilter getColorFilter() {
1022        return mColorFilter;
1023    }
1024
1025    /**
1026     * Set or clear the paint's colorfilter, returning the parameter.
1027     *
1028     * @param filter May be null. The new filter to be installed in the paint
1029     * @return       filter
1030     */
1031    public ColorFilter setColorFilter(ColorFilter filter) {
1032        long filterNative = 0;
1033        if (filter != null)
1034            filterNative = filter.native_instance;
1035        nSetColorFilter(mNativePaint, filterNative);
1036        mColorFilter = filter;
1037        return filter;
1038    }
1039
1040    /**
1041     * Get the paint's xfermode object.
1042     *
1043     * @return the paint's xfermode (or null)
1044     */
1045    public Xfermode getXfermode() {
1046        return mXfermode;
1047    }
1048
1049    /**
1050     * Set or clear the xfermode object.
1051     * <p />
1052     * Pass null to clear any previous xfermode.
1053     * As a convenience, the parameter passed is also returned.
1054     *
1055     * @param xfermode May be null. The xfermode to be installed in the paint
1056     * @return         xfermode
1057     */
1058    public Xfermode setXfermode(Xfermode xfermode) {
1059        int newMode = xfermode != null ? xfermode.porterDuffMode : Xfermode.DEFAULT;
1060        int curMode = mXfermode != null ? mXfermode.porterDuffMode : Xfermode.DEFAULT;
1061        if (newMode != curMode) {
1062            nSetXfermode(mNativePaint, newMode);
1063        }
1064        mXfermode = xfermode;
1065        return xfermode;
1066    }
1067
1068    /**
1069     * Get the paint's patheffect object.
1070     *
1071     * @return the paint's patheffect (or null)
1072     */
1073    public PathEffect getPathEffect() {
1074        return mPathEffect;
1075    }
1076
1077    /**
1078     * Set or clear the patheffect object.
1079     * <p />
1080     * Pass null to clear any previous patheffect.
1081     * As a convenience, the parameter passed is also returned.
1082     *
1083     * @param effect May be null. The patheffect to be installed in the paint
1084     * @return       effect
1085     */
1086    public PathEffect setPathEffect(PathEffect effect) {
1087        long effectNative = 0;
1088        if (effect != null) {
1089            effectNative = effect.native_instance;
1090        }
1091        nSetPathEffect(mNativePaint, effectNative);
1092        mPathEffect = effect;
1093        return effect;
1094    }
1095
1096    /**
1097     * Get the paint's maskfilter object.
1098     *
1099     * @return the paint's maskfilter (or null)
1100     */
1101    public MaskFilter getMaskFilter() {
1102        return mMaskFilter;
1103    }
1104
1105    /**
1106     * Set or clear the maskfilter object.
1107     * <p />
1108     * Pass null to clear any previous maskfilter.
1109     * As a convenience, the parameter passed is also returned.
1110     *
1111     * @param maskfilter May be null. The maskfilter to be installed in the
1112     *                   paint
1113     * @return           maskfilter
1114     */
1115    public MaskFilter setMaskFilter(MaskFilter maskfilter) {
1116        long maskfilterNative = 0;
1117        if (maskfilter != null) {
1118            maskfilterNative = maskfilter.native_instance;
1119        }
1120        nSetMaskFilter(mNativePaint, maskfilterNative);
1121        mMaskFilter = maskfilter;
1122        return maskfilter;
1123    }
1124
1125    /**
1126     * Get the paint's typeface object.
1127     * <p />
1128     * The typeface object identifies which font to use when drawing or
1129     * measuring text.
1130     *
1131     * @return the paint's typeface (or null)
1132     */
1133    public Typeface getTypeface() {
1134        return mTypeface;
1135    }
1136
1137    /**
1138     * Set or clear the typeface object.
1139     * <p />
1140     * Pass null to clear any previous typeface.
1141     * As a convenience, the parameter passed is also returned.
1142     *
1143     * @param typeface May be null. The typeface to be installed in the paint
1144     * @return         typeface
1145     */
1146    public Typeface setTypeface(Typeface typeface) {
1147        long typefaceNative = 0;
1148        if (typeface != null) {
1149            typefaceNative = typeface.native_instance;
1150        }
1151        nSetTypeface(mNativePaint, typefaceNative);
1152        mTypeface = typeface;
1153        mNativeTypeface = typefaceNative;
1154        return typeface;
1155    }
1156
1157    /**
1158     * Get the paint's rasterizer (or null).
1159     * <p />
1160     * The raster controls/modifies how paths/text are turned into alpha masks.
1161     *
1162     * @return         the paint's rasterizer (or null)
1163     *
1164     * @deprecated Rasterizer is not supported by either the HW or PDF backends.
1165     * @removed
1166     */
1167    @Deprecated
1168    public Rasterizer getRasterizer() {
1169        return null;
1170    }
1171
1172    /**
1173     * Set or clear the rasterizer object.
1174     * <p />
1175     * Pass null to clear any previous rasterizer.
1176     * As a convenience, the parameter passed is also returned.
1177     *
1178     * @param rasterizer May be null. The new rasterizer to be installed in
1179     *                   the paint.
1180     * @return           rasterizer
1181     *
1182     * @deprecated Rasterizer is not supported by either the HW or PDF backends.
1183     * @removed
1184     */
1185    @Deprecated
1186    public Rasterizer setRasterizer(Rasterizer rasterizer) {
1187        return rasterizer;
1188    }
1189
1190    /**
1191     * This draws a shadow layer below the main layer, with the specified
1192     * offset and color, and blur radius. If radius is 0, then the shadow
1193     * layer is removed.
1194     * <p>
1195     * Can be used to create a blurred shadow underneath text. Support for use
1196     * with other drawing operations is constrained to the software rendering
1197     * pipeline.
1198     * <p>
1199     * The alpha of the shadow will be the paint's alpha if the shadow color is
1200     * opaque, or the alpha from the shadow color if not.
1201     */
1202    public void setShadowLayer(float radius, float dx, float dy, int shadowColor) {
1203      nSetShadowLayer(mNativePaint, radius, dx, dy, shadowColor);
1204    }
1205
1206    /**
1207     * Clear the shadow layer.
1208     */
1209    public void clearShadowLayer() {
1210        setShadowLayer(0, 0, 0, 0);
1211    }
1212
1213    /**
1214     * Checks if the paint has a shadow layer attached
1215     *
1216     * @return true if the paint has a shadow layer attached and false otherwise
1217     * @hide
1218     */
1219    public boolean hasShadowLayer() {
1220        return nHasShadowLayer(mNativePaint);
1221    }
1222
1223    /**
1224     * Return the paint's Align value for drawing text. This controls how the
1225     * text is positioned relative to its origin. LEFT align means that all of
1226     * the text will be drawn to the right of its origin (i.e. the origin
1227     * specifieds the LEFT edge of the text) and so on.
1228     *
1229     * @return the paint's Align value for drawing text.
1230     */
1231    public Align getTextAlign() {
1232        return sAlignArray[nGetTextAlign(mNativePaint)];
1233    }
1234
1235    /**
1236     * Set the paint's text alignment. This controls how the
1237     * text is positioned relative to its origin. LEFT align means that all of
1238     * the text will be drawn to the right of its origin (i.e. the origin
1239     * specifieds the LEFT edge of the text) and so on.
1240     *
1241     * @param align set the paint's Align value for drawing text.
1242     */
1243    public void setTextAlign(Align align) {
1244        nSetTextAlign(mNativePaint, align.nativeInt);
1245    }
1246
1247    /**
1248     * Get the text's primary Locale. Note that this is not all of the locale-related information
1249     * Paint has. Use {@link #getTextLocales()} to get the complete list.
1250     *
1251     * @return the paint's primary Locale used for drawing text, never null.
1252     */
1253    @NonNull
1254    public Locale getTextLocale() {
1255        return mLocales.get(0);
1256    }
1257
1258    /**
1259     * Get the text locale list.
1260     *
1261     * @return the paint's LocaleList used for drawing text, never null or empty.
1262     */
1263    @NonNull @Size(min=1)
1264    public LocaleList getTextLocales() {
1265        return mLocales;
1266    }
1267
1268    /**
1269     * Set the text locale list to a one-member list consisting of just the locale.
1270     *
1271     * See {@link #setTextLocales(LocaleList)} for how the locale list affects
1272     * the way the text is drawn for some languages.
1273     *
1274     * @param locale the paint's locale value for drawing text, must not be null.
1275     */
1276    public void setTextLocale(@NonNull Locale locale) {
1277        if (locale == null) {
1278            throw new IllegalArgumentException("locale cannot be null");
1279        }
1280        if (mLocales != null && mLocales.size() == 1 && locale.equals(mLocales.get(0))) {
1281            return;
1282        }
1283        mLocales = new LocaleList(locale);
1284        syncTextLocalesWithMinikin();
1285    }
1286
1287    /**
1288     * Set the text locale list.
1289     *
1290     * The text locale list affects how the text is drawn for some languages.
1291     *
1292     * For example, if the locale list contains {@link Locale#CHINESE} or {@link Locale#CHINA},
1293     * then the text renderer will prefer to draw text using a Chinese font. Likewise,
1294     * if the locale list contains {@link Locale#JAPANESE} or {@link Locale#JAPAN}, then the text
1295     * renderer will prefer to draw text using a Japanese font. If the locale list contains both,
1296     * the order those locales appear in the list is considered for deciding the font.
1297     *
1298     * This distinction is important because Chinese and Japanese text both use many
1299     * of the same Unicode code points but their appearance is subtly different for
1300     * each language.
1301     *
1302     * By default, the text locale list is initialized to a one-member list just containing the
1303     * system locales. This assumes that the text to be rendered will most likely be in the user's
1304     * preferred language.
1305     *
1306     * If the actual language or languages of the text is/are known, then they can be provided to
1307     * the text renderer using this method. The text renderer may attempt to guess the
1308     * language script based on the contents of the text to be drawn independent of
1309     * the text locale here. Specifying the text locales just helps it do a better
1310     * job in certain ambiguous cases.
1311     *
1312     * @param locales the paint's locale list for drawing text, must not be null or empty.
1313     */
1314    public void setTextLocales(@NonNull @Size(min=1) LocaleList locales) {
1315        if (locales == null || locales.isEmpty()) {
1316            throw new IllegalArgumentException("locales cannot be null or empty");
1317        }
1318        if (locales.equals(mLocales)) return;
1319        mLocales = locales;
1320        syncTextLocalesWithMinikin();
1321    }
1322
1323    private void syncTextLocalesWithMinikin() {
1324        final String languageTags = mLocales.toLanguageTags();
1325        final Integer minikinLangListId;
1326        synchronized (sCacheLock) {
1327            minikinLangListId = sMinikinLangListIdCache.get(languageTags);
1328            if (minikinLangListId == null) {
1329                final int newID = nSetTextLocales(mNativePaint, languageTags);
1330                sMinikinLangListIdCache.put(languageTags, newID);
1331                return;
1332            }
1333        }
1334        nSetTextLocalesByMinikinLangListId(mNativePaint, minikinLangListId.intValue());
1335    }
1336
1337    /**
1338     * Get the elegant metrics flag.
1339     *
1340     * @return true if elegant metrics are enabled for text drawing.
1341     */
1342    public boolean isElegantTextHeight() {
1343        return nIsElegantTextHeight(mNativePaint);
1344    }
1345
1346    /**
1347     * Set the paint's elegant height metrics flag. This setting selects font
1348     * variants that have not been compacted to fit Latin-based vertical
1349     * metrics, and also increases top and bottom bounds to provide more space.
1350     *
1351     * @param elegant set the paint's elegant metrics flag for drawing text.
1352     */
1353    public void setElegantTextHeight(boolean elegant) {
1354        nSetElegantTextHeight(mNativePaint, elegant);
1355    }
1356
1357    /**
1358     * Return the paint's text size.
1359     *
1360     * @return the paint's text size.
1361     */
1362    public float getTextSize() {
1363        return nGetTextSize(mNativePaint);
1364    }
1365
1366    /**
1367     * Set the paint's text size. This value must be > 0
1368     *
1369     * @param textSize set the paint's text size.
1370     */
1371    public void setTextSize(float textSize) {
1372        nSetTextSize(mNativePaint, textSize);
1373    }
1374
1375    /**
1376     * Return the paint's horizontal scale factor for text. The default value
1377     * is 1.0.
1378     *
1379     * @return the paint's scale factor in X for drawing/measuring text
1380     */
1381    public float getTextScaleX() {
1382        return nGetTextScaleX(mNativePaint);
1383    }
1384
1385    /**
1386     * Set the paint's horizontal scale factor for text. The default value
1387     * is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
1388     * stretch the text narrower.
1389     *
1390     * @param scaleX set the paint's scale in X for drawing/measuring text.
1391     */
1392    public void setTextScaleX(float scaleX) {
1393        nSetTextScaleX(mNativePaint, scaleX);
1394    }
1395
1396    /**
1397     * Return the paint's horizontal skew factor for text. The default value
1398     * is 0.
1399     *
1400     * @return         the paint's skew factor in X for drawing text.
1401     */
1402    public float getTextSkewX() {
1403        return nGetTextSkewX(mNativePaint);
1404    }
1405
1406    /**
1407     * Set the paint's horizontal skew factor for text. The default value
1408     * is 0. For approximating oblique text, use values around -0.25.
1409     *
1410     * @param skewX set the paint's skew factor in X for drawing text.
1411     */
1412    public void setTextSkewX(float skewX) {
1413        nSetTextSkewX(mNativePaint, skewX);
1414    }
1415
1416    /**
1417     * Return the paint's letter-spacing for text. The default value
1418     * is 0.
1419     *
1420     * @return         the paint's letter-spacing for drawing text.
1421     */
1422    public float getLetterSpacing() {
1423        return nGetLetterSpacing(mNativePaint);
1424    }
1425
1426    /**
1427     * Set the paint's letter-spacing for text. The default value
1428     * is 0.  The value is in 'EM' units.  Typical values for slight
1429     * expansion will be around 0.05.  Negative values tighten text.
1430     *
1431     * @param letterSpacing set the paint's letter-spacing for drawing text.
1432     */
1433    public void setLetterSpacing(float letterSpacing) {
1434        nSetLetterSpacing(mNativePaint, letterSpacing);
1435    }
1436
1437    /**
1438     * Return the paint's word-spacing for text. The default value is 0.
1439     *
1440     * @return the paint's word-spacing for drawing text.
1441     * @hide
1442     */
1443    public float getWordSpacing() {
1444        return nGetWordSpacing(mNativePaint);
1445    }
1446
1447    /**
1448     * Set the paint's word-spacing for text. The default value is 0.
1449     * The value is in pixels (note the units are not the same as for
1450     * letter-spacing).
1451     *
1452     * @param wordSpacing set the paint's word-spacing for drawing text.
1453     * @hide
1454     */
1455    public void setWordSpacing(float wordSpacing) {
1456        nSetWordSpacing(mNativePaint, wordSpacing);
1457    }
1458
1459    /**
1460     * Returns the font feature settings. The format is the same as the CSS
1461     * font-feature-settings attribute:
1462     * <a href="https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop">
1463     *     https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop</a>
1464     *
1465     * @return the paint's currently set font feature settings. Default is null.
1466     *
1467     * @see #setFontFeatureSettings(String)
1468     */
1469    public String getFontFeatureSettings() {
1470        return mFontFeatureSettings;
1471    }
1472
1473    /**
1474     * Set font feature settings.
1475     *
1476     * The format is the same as the CSS font-feature-settings attribute:
1477     * <a href="https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop">
1478     *     https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop</a>
1479     *
1480     * @see #getFontFeatureSettings()
1481     *
1482     * @param settings the font feature settings string to use, may be null.
1483     */
1484    public void setFontFeatureSettings(String settings) {
1485        if (settings != null && settings.equals("")) {
1486            settings = null;
1487        }
1488        if ((settings == null && mFontFeatureSettings == null)
1489                || (settings != null && settings.equals(mFontFeatureSettings))) {
1490            return;
1491        }
1492        mFontFeatureSettings = settings;
1493        nSetFontFeatureSettings(mNativePaint, settings);
1494    }
1495
1496    /**
1497     * Get the current value of hyphen edit.
1498     *
1499     * @return the current hyphen edit value
1500     *
1501     * @hide
1502     */
1503    public int getHyphenEdit() {
1504        return nGetHyphenEdit(mNativePaint);
1505    }
1506
1507    /**
1508     * Set a hyphen edit on the paint (causes a hyphen to be added to text when
1509     * measured or drawn).
1510     *
1511     * @param hyphen 0 for no edit, 1 for adding a hyphen (other values in future)
1512     *
1513     * @hide
1514     */
1515    public void setHyphenEdit(int hyphen) {
1516        nSetHyphenEdit(mNativePaint, hyphen);
1517    }
1518
1519    /**
1520     * Return the distance above (negative) the baseline (ascent) based on the
1521     * current typeface and text size.
1522     *
1523     * @return the distance above (negative) the baseline (ascent) based on the
1524     *         current typeface and text size.
1525     */
1526    public float ascent() {
1527        return nAscent(mNativePaint, mNativeTypeface);
1528    }
1529
1530    /**
1531     * Return the distance below (positive) the baseline (descent) based on the
1532     * current typeface and text size.
1533     *
1534     * @return the distance below (positive) the baseline (descent) based on
1535     *         the current typeface and text size.
1536     */
1537    public float descent() {
1538        return nDescent(mNativePaint, mNativeTypeface);
1539    }
1540
1541    /**
1542     * Class that describes the various metrics for a font at a given text size.
1543     * Remember, Y values increase going down, so those values will be positive,
1544     * and values that measure distances going up will be negative. This class
1545     * is returned by getFontMetrics().
1546     */
1547    public static class FontMetrics {
1548        /**
1549         * The maximum distance above the baseline for the tallest glyph in
1550         * the font at a given text size.
1551         */
1552        public float   top;
1553        /**
1554         * The recommended distance above the baseline for singled spaced text.
1555         */
1556        public float   ascent;
1557        /**
1558         * The recommended distance below the baseline for singled spaced text.
1559         */
1560        public float   descent;
1561        /**
1562         * The maximum distance below the baseline for the lowest glyph in
1563         * the font at a given text size.
1564         */
1565        public float   bottom;
1566        /**
1567         * The recommended additional space to add between lines of text.
1568         */
1569        public float   leading;
1570    }
1571
1572    /**
1573     * Return the font's recommended interline spacing, given the Paint's
1574     * settings for typeface, textSize, etc. If metrics is not null, return the
1575     * fontmetric values in it.
1576     *
1577     * @param metrics If this object is not null, its fields are filled with
1578     *                the appropriate values given the paint's text attributes.
1579     * @return the font's recommended interline spacing.
1580     */
1581    public float getFontMetrics(FontMetrics metrics) {
1582        return nGetFontMetrics(mNativePaint, mNativeTypeface, metrics);
1583    }
1584
1585    /**
1586     * Allocates a new FontMetrics object, and then calls getFontMetrics(fm)
1587     * with it, returning the object.
1588     */
1589    public FontMetrics getFontMetrics() {
1590        FontMetrics fm = new FontMetrics();
1591        getFontMetrics(fm);
1592        return fm;
1593    }
1594
1595    /**
1596     * Convenience method for callers that want to have FontMetrics values as
1597     * integers.
1598     */
1599    public static class FontMetricsInt {
1600        /**
1601         * The maximum distance above the baseline for the tallest glyph in
1602         * the font at a given text size.
1603         */
1604        public int   top;
1605        /**
1606         * The recommended distance above the baseline for singled spaced text.
1607         */
1608        public int   ascent;
1609        /**
1610         * The recommended distance below the baseline for singled spaced text.
1611         */
1612        public int   descent;
1613        /**
1614         * The maximum distance below the baseline for the lowest glyph in
1615         * the font at a given text size.
1616         */
1617        public int   bottom;
1618        /**
1619         * The recommended additional space to add between lines of text.
1620         */
1621        public int   leading;
1622
1623        @Override public String toString() {
1624            return "FontMetricsInt: top=" + top + " ascent=" + ascent +
1625                    " descent=" + descent + " bottom=" + bottom +
1626                    " leading=" + leading;
1627        }
1628    }
1629
1630    /**
1631     * Return the font's interline spacing, given the Paint's settings for
1632     * typeface, textSize, etc. If metrics is not null, return the fontmetric
1633     * values in it. Note: all values have been converted to integers from
1634     * floats, in such a way has to make the answers useful for both spacing
1635     * and clipping. If you want more control over the rounding, call
1636     * getFontMetrics().
1637     *
1638     * @return the font's interline spacing.
1639     */
1640    public int getFontMetricsInt(FontMetricsInt fmi) {
1641        return nGetFontMetricsInt(mNativePaint, mNativeTypeface, fmi);
1642    }
1643
1644    public FontMetricsInt getFontMetricsInt() {
1645        FontMetricsInt fm = new FontMetricsInt();
1646        getFontMetricsInt(fm);
1647        return fm;
1648    }
1649
1650    /**
1651     * Return the recommend line spacing based on the current typeface and
1652     * text size.
1653     *
1654     * @return  recommend line spacing based on the current typeface and
1655     *          text size.
1656     */
1657    public float getFontSpacing() {
1658        return getFontMetrics(null);
1659    }
1660
1661    /**
1662     * Return the width of the text.
1663     *
1664     * @param text  The text to measure. Cannot be null.
1665     * @param index The index of the first character to start measuring
1666     * @param count THe number of characters to measure, beginning with start
1667     * @return      The width of the text
1668     */
1669    public float measureText(char[] text, int index, int count) {
1670        if (text == null) {
1671            throw new IllegalArgumentException("text cannot be null");
1672        }
1673        if ((index | count) < 0 || index + count > text.length) {
1674            throw new ArrayIndexOutOfBoundsException();
1675        }
1676
1677        if (text.length == 0 || count == 0) {
1678            return 0f;
1679        }
1680        if (!mHasCompatScaling) {
1681            return (float) Math.ceil(nGetTextAdvances(mNativePaint, mNativeTypeface, text,
1682                    index, count, index, count, mBidiFlags, null, 0));
1683        }
1684
1685        final float oldSize = getTextSize();
1686        setTextSize(oldSize * mCompatScaling);
1687        float w = nGetTextAdvances(mNativePaint, mNativeTypeface, text, index, count, index,
1688                count, mBidiFlags, null, 0);
1689        setTextSize(oldSize);
1690        return (float) Math.ceil(w*mInvCompatScaling);
1691    }
1692
1693    /**
1694     * Return the width of the text.
1695     *
1696     * @param text  The text to measure. Cannot be null.
1697     * @param start The index of the first character to start measuring
1698     * @param end   1 beyond the index of the last character to measure
1699     * @return      The width of the text
1700     */
1701    public float measureText(String text, int start, int end) {
1702        if (text == null) {
1703            throw new IllegalArgumentException("text cannot be null");
1704        }
1705        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1706            throw new IndexOutOfBoundsException();
1707        }
1708
1709        if (text.length() == 0 || start == end) {
1710            return 0f;
1711        }
1712        if (!mHasCompatScaling) {
1713            return (float) Math.ceil(nGetTextAdvances(mNativePaint, mNativeTypeface, text,
1714                    start, end, start, end, mBidiFlags, null, 0));
1715        }
1716        final float oldSize = getTextSize();
1717        setTextSize(oldSize * mCompatScaling);
1718        float w = nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, end, start,
1719                end, mBidiFlags, null, 0);
1720        setTextSize(oldSize);
1721        return (float) Math.ceil(w * mInvCompatScaling);
1722    }
1723
1724    /**
1725     * Return the width of the text.
1726     *
1727     * @param text  The text to measure. Cannot be null.
1728     * @return      The width of the text
1729     */
1730    public float measureText(String text) {
1731        if (text == null) {
1732            throw new IllegalArgumentException("text cannot be null");
1733        }
1734        return measureText(text, 0, text.length());
1735    }
1736
1737    /**
1738     * Return the width of the text.
1739     *
1740     * @param text  The text to measure
1741     * @param start The index of the first character to start measuring
1742     * @param end   1 beyond the index of the last character to measure
1743     * @return      The width of the text
1744     */
1745    public float measureText(CharSequence text, int start, int end) {
1746        if (text == null) {
1747            throw new IllegalArgumentException("text cannot be null");
1748        }
1749        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1750            throw new IndexOutOfBoundsException();
1751        }
1752
1753        if (text.length() == 0 || start == end) {
1754            return 0f;
1755        }
1756        if (text instanceof String) {
1757            return measureText((String)text, start, end);
1758        }
1759        if (text instanceof SpannedString ||
1760            text instanceof SpannableString) {
1761            return measureText(text.toString(), start, end);
1762        }
1763        if (text instanceof GraphicsOperations) {
1764            return ((GraphicsOperations)text).measureText(start, end, this);
1765        }
1766
1767        char[] buf = TemporaryBuffer.obtain(end - start);
1768        TextUtils.getChars(text, start, end, buf, 0);
1769        float result = measureText(buf, 0, end - start);
1770        TemporaryBuffer.recycle(buf);
1771        return result;
1772    }
1773
1774    /**
1775     * Measure the text, stopping early if the measured width exceeds maxWidth.
1776     * Return the number of chars that were measured, and if measuredWidth is
1777     * not null, return in it the actual width measured.
1778     *
1779     * @param text  The text to measure. Cannot be null.
1780     * @param index The offset into text to begin measuring at
1781     * @param count The number of maximum number of entries to measure. If count
1782     *              is negative, then the characters are measured in reverse order.
1783     * @param maxWidth The maximum width to accumulate.
1784     * @param measuredWidth Optional. If not null, returns the actual width
1785     *                     measured.
1786     * @return The number of chars that were measured. Will always be <=
1787     *         abs(count).
1788     */
1789    public int breakText(char[] text, int index, int count,
1790                                float maxWidth, float[] measuredWidth) {
1791        if (text == null) {
1792            throw new IllegalArgumentException("text cannot be null");
1793        }
1794        if (index < 0 || text.length - index < Math.abs(count)) {
1795            throw new ArrayIndexOutOfBoundsException();
1796        }
1797
1798        if (text.length == 0 || count == 0) {
1799            return 0;
1800        }
1801        if (!mHasCompatScaling) {
1802            return nBreakText(mNativePaint, mNativeTypeface, text, index, count, maxWidth,
1803                    mBidiFlags, measuredWidth);
1804        }
1805
1806        final float oldSize = getTextSize();
1807        setTextSize(oldSize * mCompatScaling);
1808        int res = nBreakText(mNativePaint, mNativeTypeface, text, index, count,
1809                maxWidth * mCompatScaling, mBidiFlags, measuredWidth);
1810        setTextSize(oldSize);
1811        if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling;
1812        return res;
1813    }
1814
1815    /**
1816     * Measure the text, stopping early if the measured width exceeds maxWidth.
1817     * Return the number of chars that were measured, and if measuredWidth is
1818     * not null, return in it the actual width measured.
1819     *
1820     * @param text  The text to measure. Cannot be null.
1821     * @param start The offset into text to begin measuring at
1822     * @param end   The end of the text slice to measure.
1823     * @param measureForwards If true, measure forwards, starting at start.
1824     *                        Otherwise, measure backwards, starting with end.
1825     * @param maxWidth The maximum width to accumulate.
1826     * @param measuredWidth Optional. If not null, returns the actual width
1827     *                     measured.
1828     * @return The number of chars that were measured. Will always be <=
1829     *         abs(end - start).
1830     */
1831    public int breakText(CharSequence text, int start, int end,
1832                         boolean measureForwards,
1833                         float maxWidth, float[] measuredWidth) {
1834        if (text == null) {
1835            throw new IllegalArgumentException("text cannot be null");
1836        }
1837        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1838            throw new IndexOutOfBoundsException();
1839        }
1840
1841        if (text.length() == 0 || start == end) {
1842            return 0;
1843        }
1844        if (start == 0 && text instanceof String && end == text.length()) {
1845            return breakText((String) text, measureForwards, maxWidth,
1846                             measuredWidth);
1847        }
1848
1849        char[] buf = TemporaryBuffer.obtain(end - start);
1850        int result;
1851
1852        TextUtils.getChars(text, start, end, buf, 0);
1853
1854        if (measureForwards) {
1855            result = breakText(buf, 0, end - start, maxWidth, measuredWidth);
1856        } else {
1857            result = breakText(buf, 0, -(end - start), maxWidth, measuredWidth);
1858        }
1859
1860        TemporaryBuffer.recycle(buf);
1861        return result;
1862    }
1863
1864    /**
1865     * Measure the text, stopping early if the measured width exceeds maxWidth.
1866     * Return the number of chars that were measured, and if measuredWidth is
1867     * not null, return in it the actual width measured.
1868     *
1869     * @param text  The text to measure. Cannot be null.
1870     * @param measureForwards If true, measure forwards, starting with the
1871     *                        first character in the string. Otherwise,
1872     *                        measure backwards, starting with the
1873     *                        last character in the string.
1874     * @param maxWidth The maximum width to accumulate.
1875     * @param measuredWidth Optional. If not null, returns the actual width
1876     *                     measured.
1877     * @return The number of chars that were measured. Will always be <=
1878     *         abs(count).
1879     */
1880    public int breakText(String text, boolean measureForwards,
1881                                float maxWidth, float[] measuredWidth) {
1882        if (text == null) {
1883            throw new IllegalArgumentException("text cannot be null");
1884        }
1885
1886        if (text.length() == 0) {
1887            return 0;
1888        }
1889        if (!mHasCompatScaling) {
1890            return nBreakText(mNativePaint, mNativeTypeface, text, measureForwards,
1891                    maxWidth, mBidiFlags, measuredWidth);
1892        }
1893
1894        final float oldSize = getTextSize();
1895        setTextSize(oldSize*mCompatScaling);
1896        int res = nBreakText(mNativePaint, mNativeTypeface, text, measureForwards,
1897                maxWidth*mCompatScaling, mBidiFlags, measuredWidth);
1898        setTextSize(oldSize);
1899        if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling;
1900        return res;
1901    }
1902
1903    /**
1904     * Return the advance widths for the characters in the string.
1905     *
1906     * @param text     The text to measure. Cannot be null.
1907     * @param index    The index of the first char to to measure
1908     * @param count    The number of chars starting with index to measure
1909     * @param widths   array to receive the advance widths of the characters.
1910     *                 Must be at least a large as count.
1911     * @return         the actual number of widths returned.
1912     */
1913    public int getTextWidths(char[] text, int index, int count,
1914                             float[] widths) {
1915        if (text == null) {
1916            throw new IllegalArgumentException("text cannot be null");
1917        }
1918        if ((index | count) < 0 || index + count > text.length
1919                || count > widths.length) {
1920            throw new ArrayIndexOutOfBoundsException();
1921        }
1922
1923        if (text.length == 0 || count == 0) {
1924            return 0;
1925        }
1926        if (!mHasCompatScaling) {
1927            nGetTextAdvances(mNativePaint, mNativeTypeface, text, index, count, index, count,
1928                    mBidiFlags, widths, 0);
1929            return count;
1930        }
1931
1932        final float oldSize = getTextSize();
1933        setTextSize(oldSize * mCompatScaling);
1934        nGetTextAdvances(mNativePaint, mNativeTypeface, text, index, count, index, count,
1935                mBidiFlags, widths, 0);
1936        setTextSize(oldSize);
1937        for (int i = 0; i < count; i++) {
1938            widths[i] *= mInvCompatScaling;
1939        }
1940        return count;
1941    }
1942
1943    /**
1944     * Return the advance widths for the characters in the string.
1945     *
1946     * @param text     The text to measure. Cannot be null.
1947     * @param start    The index of the first char to to measure
1948     * @param end      The end of the text slice to measure
1949     * @param widths   array to receive the advance widths of the characters.
1950     *                 Must be at least a large as (end - start).
1951     * @return         the actual number of widths returned.
1952     */
1953    public int getTextWidths(CharSequence text, int start, int end,
1954                             float[] widths) {
1955        if (text == null) {
1956            throw new IllegalArgumentException("text cannot be null");
1957        }
1958        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1959            throw new IndexOutOfBoundsException();
1960        }
1961        if (end - start > widths.length) {
1962            throw new ArrayIndexOutOfBoundsException();
1963        }
1964
1965        if (text.length() == 0 || start == end) {
1966            return 0;
1967        }
1968        if (text instanceof String) {
1969            return getTextWidths((String) text, start, end, widths);
1970        }
1971        if (text instanceof SpannedString ||
1972            text instanceof SpannableString) {
1973            return getTextWidths(text.toString(), start, end, widths);
1974        }
1975        if (text instanceof GraphicsOperations) {
1976            return ((GraphicsOperations) text).getTextWidths(start, end,
1977                                                                 widths, this);
1978        }
1979
1980        char[] buf = TemporaryBuffer.obtain(end - start);
1981        TextUtils.getChars(text, start, end, buf, 0);
1982        int result = getTextWidths(buf, 0, end - start, widths);
1983        TemporaryBuffer.recycle(buf);
1984        return result;
1985    }
1986
1987    /**
1988     * Return the advance widths for the characters in the string.
1989     *
1990     * @param text   The text to measure. Cannot be null.
1991     * @param start  The index of the first char to to measure
1992     * @param end    The end of the text slice to measure
1993     * @param widths array to receive the advance widths of the characters.
1994     *               Must be at least a large as the text.
1995     * @return       the number of code units in the specified text.
1996     */
1997    public int getTextWidths(String text, int start, int end, float[] widths) {
1998        if (text == null) {
1999            throw new IllegalArgumentException("text cannot be null");
2000        }
2001        if ((start | end | (end - start) | (text.length() - end)) < 0) {
2002            throw new IndexOutOfBoundsException();
2003        }
2004        if (end - start > widths.length) {
2005            throw new ArrayIndexOutOfBoundsException();
2006        }
2007
2008        if (text.length() == 0 || start == end) {
2009            return 0;
2010        }
2011        if (!mHasCompatScaling) {
2012            nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, end, start, end,
2013                    mBidiFlags, widths, 0);
2014            return end - start;
2015        }
2016
2017        final float oldSize = getTextSize();
2018        setTextSize(oldSize * mCompatScaling);
2019        nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, end, start, end,
2020                mBidiFlags, widths, 0);
2021        setTextSize(oldSize);
2022        for (int i = 0; i < end - start; i++) {
2023            widths[i] *= mInvCompatScaling;
2024        }
2025        return end - start;
2026    }
2027
2028    /**
2029     * Return the advance widths for the characters in the string.
2030     *
2031     * @param text   The text to measure
2032     * @param widths array to receive the advance widths of the characters.
2033     *               Must be at least a large as the text.
2034     * @return       the number of code units in the specified text.
2035     */
2036    public int getTextWidths(String text, float[] widths) {
2037        return getTextWidths(text, 0, text.length(), widths);
2038    }
2039
2040    /**
2041     * Convenience overload that takes a char array instead of a
2042     * String.
2043     *
2044     * @see #getTextRunAdvances(String, int, int, int, int, boolean, float[], int)
2045     * @hide
2046     */
2047    public float getTextRunAdvances(char[] chars, int index, int count,
2048            int contextIndex, int contextCount, boolean isRtl, float[] advances,
2049            int advancesIndex) {
2050
2051        if (chars == null) {
2052            throw new IllegalArgumentException("text cannot be null");
2053        }
2054        if ((index | count | contextIndex | contextCount | advancesIndex
2055                | (index - contextIndex) | (contextCount - count)
2056                | ((contextIndex + contextCount) - (index + count))
2057                | (chars.length - (contextIndex + contextCount))
2058                | (advances == null ? 0 :
2059                    (advances.length - (advancesIndex + count)))) < 0) {
2060            throw new IndexOutOfBoundsException();
2061        }
2062
2063        if (chars.length == 0 || count == 0){
2064            return 0f;
2065        }
2066        if (!mHasCompatScaling) {
2067            return nGetTextAdvances(mNativePaint, mNativeTypeface, chars, index, count,
2068                    contextIndex, contextCount, isRtl ? BIDI_FORCE_RTL : BIDI_FORCE_LTR, advances,
2069                    advancesIndex);
2070        }
2071
2072        final float oldSize = getTextSize();
2073        setTextSize(oldSize * mCompatScaling);
2074        float res = nGetTextAdvances(mNativePaint, mNativeTypeface, chars, index, count,
2075                contextIndex, contextCount, isRtl ? BIDI_FORCE_RTL : BIDI_FORCE_LTR, advances,
2076                advancesIndex);
2077        setTextSize(oldSize);
2078
2079        if (advances != null) {
2080            for (int i = advancesIndex, e = i + count; i < e; i++) {
2081                advances[i] *= mInvCompatScaling;
2082            }
2083        }
2084        return res * mInvCompatScaling; // assume errors are not significant
2085    }
2086
2087    /**
2088     * Convenience overload that takes a CharSequence instead of a
2089     * String.
2090     *
2091     * @see #getTextRunAdvances(String, int, int, int, int, boolean, float[], int)
2092     * @hide
2093     */
2094    public float getTextRunAdvances(CharSequence text, int start, int end,
2095            int contextStart, int contextEnd, boolean isRtl, float[] advances,
2096            int advancesIndex) {
2097        if (text == null) {
2098            throw new IllegalArgumentException("text cannot be null");
2099        }
2100        if ((start | end | contextStart | contextEnd | advancesIndex | (end - start)
2101                | (start - contextStart) | (contextEnd - end)
2102                | (text.length() - contextEnd)
2103                | (advances == null ? 0 :
2104                    (advances.length - advancesIndex - (end - start)))) < 0) {
2105            throw new IndexOutOfBoundsException();
2106        }
2107
2108        if (text instanceof String) {
2109            return getTextRunAdvances((String) text, start, end,
2110                    contextStart, contextEnd, isRtl, advances, advancesIndex);
2111        }
2112        if (text instanceof SpannedString ||
2113            text instanceof SpannableString) {
2114            return getTextRunAdvances(text.toString(), start, end,
2115                    contextStart, contextEnd, isRtl, advances, advancesIndex);
2116        }
2117        if (text instanceof GraphicsOperations) {
2118            return ((GraphicsOperations) text).getTextRunAdvances(start, end,
2119                    contextStart, contextEnd, isRtl, advances, advancesIndex, this);
2120        }
2121        if (text.length() == 0 || end == start) {
2122            return 0f;
2123        }
2124
2125        int contextLen = contextEnd - contextStart;
2126        int len = end - start;
2127        char[] buf = TemporaryBuffer.obtain(contextLen);
2128        TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
2129        float result = getTextRunAdvances(buf, start - contextStart, len,
2130                0, contextLen, isRtl, advances, advancesIndex);
2131        TemporaryBuffer.recycle(buf);
2132        return result;
2133    }
2134
2135    /**
2136     * Returns the total advance width for the characters in the run
2137     * between start and end, and if advances is not null, the advance
2138     * assigned to each of these characters (java chars).
2139     *
2140     * <p>The trailing surrogate in a valid surrogate pair is assigned
2141     * an advance of 0.  Thus the number of returned advances is
2142     * always equal to count, not to the number of unicode codepoints
2143     * represented by the run.
2144     *
2145     * <p>In the case of conjuncts or combining marks, the total
2146     * advance is assigned to the first logical character, and the
2147     * following characters are assigned an advance of 0.
2148     *
2149     * <p>This generates the sum of the advances of glyphs for
2150     * characters in a reordered cluster as the width of the first
2151     * logical character in the cluster, and 0 for the widths of all
2152     * other characters in the cluster.  In effect, such clusters are
2153     * treated like conjuncts.
2154     *
2155     * <p>The shaping bounds limit the amount of context available
2156     * outside start and end that can be used for shaping analysis.
2157     * These bounds typically reflect changes in bidi level or font
2158     * metrics across which shaping does not occur.
2159     *
2160     * @param text the text to measure. Cannot be null.
2161     * @param start the index of the first character to measure
2162     * @param end the index past the last character to measure
2163     * @param contextStart the index of the first character to use for shaping context,
2164     * must be <= start
2165     * @param contextEnd the index past the last character to use for shaping context,
2166     * must be >= end
2167     * @param isRtl whether the run is in RTL direction
2168     * @param advances array to receive the advances, must have room for all advances,
2169     * can be null if only total advance is needed
2170     * @param advancesIndex the position in advances at which to put the
2171     * advance corresponding to the character at start
2172     * @return the total advance
2173     *
2174     * @hide
2175     */
2176    public float getTextRunAdvances(String text, int start, int end, int contextStart,
2177            int contextEnd, boolean isRtl, float[] advances, int advancesIndex) {
2178        if (text == null) {
2179            throw new IllegalArgumentException("text cannot be null");
2180        }
2181        if ((start | end | contextStart | contextEnd | advancesIndex | (end - start)
2182                | (start - contextStart) | (contextEnd - end)
2183                | (text.length() - contextEnd)
2184                | (advances == null ? 0 :
2185                    (advances.length - advancesIndex - (end - start)))) < 0) {
2186            throw new IndexOutOfBoundsException();
2187        }
2188
2189        if (text.length() == 0 || start == end) {
2190            return 0f;
2191        }
2192
2193        if (!mHasCompatScaling) {
2194            return nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, end,
2195                    contextStart, contextEnd, isRtl ? BIDI_FORCE_RTL : BIDI_FORCE_LTR, advances,
2196                    advancesIndex);
2197        }
2198
2199        final float oldSize = getTextSize();
2200        setTextSize(oldSize * mCompatScaling);
2201        float totalAdvance = nGetTextAdvances(mNativePaint, mNativeTypeface, text, start,
2202                end, contextStart, contextEnd, isRtl ? BIDI_FORCE_RTL : BIDI_FORCE_LTR, advances,
2203                advancesIndex);
2204        setTextSize(oldSize);
2205
2206        if (advances != null) {
2207            for (int i = advancesIndex, e = i + (end - start); i < e; i++) {
2208                advances[i] *= mInvCompatScaling;
2209            }
2210        }
2211        return totalAdvance * mInvCompatScaling; // assume errors are insignificant
2212    }
2213
2214    /**
2215     * Returns the next cursor position in the run.  This avoids placing the
2216     * cursor between surrogates, between characters that form conjuncts,
2217     * between base characters and combining marks, or within a reordering
2218     * cluster.
2219     *
2220     * <p>ContextStart and offset are relative to the start of text.
2221     * The context is the shaping context for cursor movement, generally
2222     * the bounds of the metric span enclosing the cursor in the direction of
2223     * movement.
2224     *
2225     * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid
2226     * cursor position, this returns -1.  Otherwise this will never return a
2227     * value before contextStart or after contextStart + contextLength.
2228     *
2229     * @param text the text
2230     * @param contextStart the start of the context
2231     * @param contextLength the length of the context
2232     * @param dir either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR}
2233     * @param offset the cursor position to move from
2234     * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER},
2235     * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE},
2236     * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT}
2237     * @return the offset of the next position, or -1
2238     * @hide
2239     */
2240    public int getTextRunCursor(char[] text, int contextStart, int contextLength,
2241            int dir, int offset, int cursorOpt) {
2242        int contextEnd = contextStart + contextLength;
2243        if (((contextStart | contextEnd | offset | (contextEnd - contextStart)
2244                | (offset - contextStart) | (contextEnd - offset)
2245                | (text.length - contextEnd) | cursorOpt) < 0)
2246                || cursorOpt > CURSOR_OPT_MAX_VALUE) {
2247            throw new IndexOutOfBoundsException();
2248        }
2249
2250        return nGetTextRunCursor(mNativePaint, text,
2251                contextStart, contextLength, dir, offset, cursorOpt);
2252    }
2253
2254    /**
2255     * Returns the next cursor position in the run.  This avoids placing the
2256     * cursor between surrogates, between characters that form conjuncts,
2257     * between base characters and combining marks, or within a reordering
2258     * cluster.
2259     *
2260     * <p>ContextStart, contextEnd, and offset are relative to the start of
2261     * text.  The context is the shaping context for cursor movement, generally
2262     * the bounds of the metric span enclosing the cursor in the direction of
2263     * movement.
2264     *
2265     * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid
2266     * cursor position, this returns -1.  Otherwise this will never return a
2267     * value before contextStart or after contextEnd.
2268     *
2269     * @param text the text
2270     * @param contextStart the start of the context
2271     * @param contextEnd the end of the context
2272     * @param dir either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR}
2273     * @param offset the cursor position to move from
2274     * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER},
2275     * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE},
2276     * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT}
2277     * @return the offset of the next position, or -1
2278     * @hide
2279     */
2280    public int getTextRunCursor(CharSequence text, int contextStart,
2281           int contextEnd, int dir, int offset, int cursorOpt) {
2282
2283        if (text instanceof String || text instanceof SpannedString ||
2284                text instanceof SpannableString) {
2285            return getTextRunCursor(text.toString(), contextStart, contextEnd,
2286                    dir, offset, cursorOpt);
2287        }
2288        if (text instanceof GraphicsOperations) {
2289            return ((GraphicsOperations) text).getTextRunCursor(
2290                    contextStart, contextEnd, dir, offset, cursorOpt, this);
2291        }
2292
2293        int contextLen = contextEnd - contextStart;
2294        char[] buf = TemporaryBuffer.obtain(contextLen);
2295        TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
2296        int relPos = getTextRunCursor(buf, 0, contextLen, dir, offset - contextStart, cursorOpt);
2297        TemporaryBuffer.recycle(buf);
2298        return (relPos == -1) ? -1 : relPos + contextStart;
2299    }
2300
2301    /**
2302     * Returns the next cursor position in the run.  This avoids placing the
2303     * cursor between surrogates, between characters that form conjuncts,
2304     * between base characters and combining marks, or within a reordering
2305     * cluster.
2306     *
2307     * <p>ContextStart, contextEnd, and offset are relative to the start of
2308     * text.  The context is the shaping context for cursor movement, generally
2309     * the bounds of the metric span enclosing the cursor in the direction of
2310     * movement.
2311     *
2312     * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid
2313     * cursor position, this returns -1.  Otherwise this will never return a
2314     * value before contextStart or after contextEnd.
2315     *
2316     * @param text the text
2317     * @param contextStart the start of the context
2318     * @param contextEnd the end of the context
2319     * @param dir either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR}
2320     * @param offset the cursor position to move from
2321     * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER},
2322     * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE},
2323     * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT}
2324     * @return the offset of the next position, or -1
2325     * @hide
2326     */
2327    public int getTextRunCursor(String text, int contextStart, int contextEnd,
2328            int dir, int offset, int cursorOpt) {
2329        if (((contextStart | contextEnd | offset | (contextEnd - contextStart)
2330                | (offset - contextStart) | (contextEnd - offset)
2331                | (text.length() - contextEnd) | cursorOpt) < 0)
2332                || cursorOpt > CURSOR_OPT_MAX_VALUE) {
2333            throw new IndexOutOfBoundsException();
2334        }
2335
2336        return nGetTextRunCursor(mNativePaint, text,
2337                contextStart, contextEnd, dir, offset, cursorOpt);
2338    }
2339
2340    /**
2341     * Return the path (outline) for the specified text.
2342     * Note: just like Canvas.drawText, this will respect the Align setting in
2343     * the paint.
2344     *
2345     * @param text the text to retrieve the path from
2346     * @param index the index of the first character in text
2347     * @param count the number of characters starting with index
2348     * @param x the x coordinate of the text's origin
2349     * @param y the y coordinate of the text's origin
2350     * @param path the path to receive the data describing the text. Must be allocated by the caller
2351     */
2352    public void getTextPath(char[] text, int index, int count,
2353                            float x, float y, Path path) {
2354        if ((index | count) < 0 || index + count > text.length) {
2355            throw new ArrayIndexOutOfBoundsException();
2356        }
2357        nGetTextPath(mNativePaint, mNativeTypeface, mBidiFlags, text, index, count, x, y,
2358                path.mutateNI());
2359    }
2360
2361    /**
2362     * Return the path (outline) for the specified text.
2363     * Note: just like Canvas.drawText, this will respect the Align setting
2364     * in the paint.
2365     *
2366     * @param text the text to retrieve the path from
2367     * @param start the first character in the text
2368     * @param end 1 past the last character in the text
2369     * @param x the x coordinate of the text's origin
2370     * @param y the y coordinate of the text's origin
2371     * @param path the path to receive the data describing the text. Must be allocated by the caller
2372     */
2373    public void getTextPath(String text, int start, int end,
2374                            float x, float y, Path path) {
2375        if ((start | end | (end - start) | (text.length() - end)) < 0) {
2376            throw new IndexOutOfBoundsException();
2377        }
2378        nGetTextPath(mNativePaint, mNativeTypeface, mBidiFlags, text, start, end, x, y,
2379                path.mutateNI());
2380    }
2381
2382    /**
2383     * Return in bounds (allocated by the caller) the smallest rectangle that
2384     * encloses all of the characters, with an implied origin at (0,0).
2385     *
2386     * @param text string to measure and return its bounds
2387     * @param start index of the first char in the string to measure
2388     * @param end 1 past the last char in the string to measure
2389     * @param bounds returns the unioned bounds of all the text. Must be allocated by the caller
2390     */
2391    public void getTextBounds(String text, int start, int end, Rect bounds) {
2392        if ((start | end | (end - start) | (text.length() - end)) < 0) {
2393            throw new IndexOutOfBoundsException();
2394        }
2395        if (bounds == null) {
2396            throw new NullPointerException("need bounds Rect");
2397        }
2398        nGetStringBounds(mNativePaint, mNativeTypeface, text, start, end, mBidiFlags, bounds);
2399    }
2400
2401    /**
2402     * Return in bounds (allocated by the caller) the smallest rectangle that
2403     * encloses all of the characters, with an implied origin at (0,0).
2404     *
2405     * @param text text to measure and return its bounds
2406     * @param start index of the first char in the text to measure
2407     * @param end 1 past the last char in the text to measure
2408     * @param bounds returns the unioned bounds of all the text. Must be allocated by the caller
2409     * @hide
2410     */
2411    public void getTextBounds(CharSequence text, int start, int end, Rect bounds) {
2412        if ((start | end | (end - start) | (text.length() - end)) < 0) {
2413            throw new IndexOutOfBoundsException();
2414        }
2415        if (bounds == null) {
2416            throw new NullPointerException("need bounds Rect");
2417        }
2418        char[] buf = TemporaryBuffer.obtain(end - start);
2419        TextUtils.getChars(text, start, end, buf, 0);
2420        getTextBounds(buf, 0, end - start, bounds);
2421        TemporaryBuffer.recycle(buf);
2422    }
2423
2424    /**
2425     * Return in bounds (allocated by the caller) the smallest rectangle that
2426     * encloses all of the characters, with an implied origin at (0,0).
2427     *
2428     * @param text  array of chars to measure and return their unioned bounds
2429     * @param index index of the first char in the array to measure
2430     * @param count the number of chars, beginning at index, to measure
2431     * @param bounds returns the unioned bounds of all the text. Must be allocated by the caller
2432     */
2433    public void getTextBounds(char[] text, int index, int count, Rect bounds) {
2434        if ((index | count) < 0 || index + count > text.length) {
2435            throw new ArrayIndexOutOfBoundsException();
2436        }
2437        if (bounds == null) {
2438            throw new NullPointerException("need bounds Rect");
2439        }
2440        nGetCharArrayBounds(mNativePaint, mNativeTypeface, text, index, count, mBidiFlags,
2441            bounds);
2442    }
2443
2444    /**
2445     * Determine whether the typeface set on the paint has a glyph supporting the string. The
2446     * simplest case is when the string contains a single character, in which this method
2447     * determines whether the font has the character. In the case of multiple characters, the
2448     * method returns true if there is a single glyph representing the ligature. For example, if
2449     * the input is a pair of regional indicator symbols, determine whether there is an emoji flag
2450     * for the pair.
2451     *
2452     * <p>Finally, if the string contains a variation selector, the method only returns true if
2453     * the fonts contains a glyph specific to that variation.
2454     *
2455     * <p>Checking is done on the entire fallback chain, not just the immediate font referenced.
2456     *
2457     * @param string the string to test whether there is glyph support
2458     * @return true if the typeface has a glyph for the string
2459     */
2460    public boolean hasGlyph(String string) {
2461        return nHasGlyph(mNativePaint, mNativeTypeface, mBidiFlags, string);
2462    }
2463
2464    /**
2465     * Measure cursor position within a run of text.
2466     *
2467     * <p>The run of text includes the characters from {@code start} to {@code end} in the text. In
2468     * addition, the range {@code contextStart} to {@code contextEnd} is used as context for the
2469     * purpose of complex text shaping, such as Arabic text potentially shaped differently based on
2470     * the text next to it.
2471     *
2472     * <p>All text outside the range {@code contextStart..contextEnd} is ignored. The text between
2473     * {@code start} and {@code end} will be laid out to be measured.
2474     *
2475     * <p>The returned width measurement is the advance from {@code start} to {@code offset}. It is
2476     * generally a positive value, no matter the direction of the run. If {@code offset == end},
2477     * the return value is simply the width of the whole run from {@code start} to {@code end}.
2478     *
2479     * <p>Ligatures are formed for characters in the range {@code start..end} (but not for
2480     * {@code start..contextStart} or {@code end..contextEnd}). If {@code offset} points to a
2481     * character in the middle of such a formed ligature, but at a grapheme cluster boundary, the
2482     * return value will also reflect an advance in the middle of the ligature. See
2483     * {@link #getOffsetForAdvance} for more discussion of grapheme cluster boundaries.
2484     *
2485     * <p>The direction of the run is explicitly specified by {@code isRtl}. Thus, this method is
2486     * suitable only for runs of a single direction.
2487     *
2488     * <p>All indices are relative to the start of {@code text}. Further, {@code 0 <= contextStart
2489     * <= start <= offset <= end <= contextEnd <= text.length} must hold on entry.
2490     *
2491     * @param text the text to measure. Cannot be null.
2492     * @param start the index of the start of the range to measure
2493     * @param end the index + 1 of the end of the range to measure
2494     * @param contextStart the index of the start of the shaping context
2495     * @param contextEnd the index + 1 of the end of the shaping context
2496     * @param isRtl whether the run is in RTL direction
2497     * @param offset index of caret position
2498     * @return width measurement between start and offset
2499     */
2500    public float getRunAdvance(char[] text, int start, int end, int contextStart, int contextEnd,
2501            boolean isRtl, int offset) {
2502        if (text == null) {
2503            throw new IllegalArgumentException("text cannot be null");
2504        }
2505        if ((contextStart | start | offset | end | contextEnd
2506                | start - contextStart | offset - start | end - offset
2507                | contextEnd - end | text.length - contextEnd) < 0) {
2508            throw new IndexOutOfBoundsException();
2509        }
2510        if (end == start) {
2511            return 0.0f;
2512        }
2513        // TODO: take mCompatScaling into account (or eliminate compat scaling)?
2514        return nGetRunAdvance(mNativePaint, mNativeTypeface, text, start, end,
2515                contextStart, contextEnd, isRtl, offset);
2516    }
2517
2518    /**
2519     * @see #getRunAdvance(char[], int, int, int, int, boolean, int)
2520     *
2521     * @param text the text to measure. Cannot be null.
2522     * @param start the index of the start of the range to measure
2523     * @param end the index + 1 of the end of the range to measure
2524     * @param contextStart the index of the start of the shaping context
2525     * @param contextEnd the index + 1 of the end of the shaping context
2526     * @param isRtl whether the run is in RTL direction
2527     * @param offset index of caret position
2528     * @return width measurement between start and offset
2529     */
2530    public float getRunAdvance(CharSequence text, int start, int end, int contextStart,
2531            int contextEnd, boolean isRtl, int offset) {
2532        if (text == null) {
2533            throw new IllegalArgumentException("text cannot be null");
2534        }
2535        if ((contextStart | start | offset | end | contextEnd
2536                | start - contextStart | offset - start | end - offset
2537                | contextEnd - end | text.length() - contextEnd) < 0) {
2538            throw new IndexOutOfBoundsException();
2539        }
2540        if (end == start) {
2541            return 0.0f;
2542        }
2543        // TODO performance: specialized alternatives to avoid buffer copy, if win is significant
2544        char[] buf = TemporaryBuffer.obtain(contextEnd - contextStart);
2545        TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
2546        float result = getRunAdvance(buf, start - contextStart, end - contextStart, 0,
2547                contextEnd - contextStart, isRtl, offset - contextStart);
2548        TemporaryBuffer.recycle(buf);
2549        return result;
2550    }
2551
2552    /**
2553     * Get the character offset within the string whose position is closest to the specified
2554     * horizontal position.
2555     *
2556     * <p>The returned value is generally the value of {@code offset} for which
2557     * {@link #getRunAdvance} yields a result most closely approximating {@code advance},
2558     * and which is also on a grapheme cluster boundary. As such, it is the preferred method
2559     * for positioning a cursor in response to a touch or pointer event. The grapheme cluster
2560     * boundaries are based on
2561     * <a href="http://unicode.org/reports/tr29/">Unicode Standard Annex #29</a> but with some
2562     * tailoring for better user experience.
2563     *
2564     * <p>Note that {@code advance} is a (generally positive) width measurement relative to the start
2565     * of the run. Thus, for RTL runs it the distance from the point to the right edge.
2566     *
2567     * <p>All indices are relative to the start of {@code text}. Further, {@code 0 <= contextStart
2568     * <= start <= end <= contextEnd <= text.length} must hold on entry, and {@code start <= result
2569     * <= end} will hold on return.
2570     *
2571     * @param text the text to measure. Cannot be null.
2572     * @param start the index of the start of the range to measure
2573     * @param end the index + 1 of the end of the range to measure
2574     * @param contextStart the index of the start of the shaping context
2575     * @param contextEnd the index + 1 of the end of the range to measure
2576     * @param isRtl whether the run is in RTL direction
2577     * @param advance width relative to start of run
2578     * @return index of offset
2579     */
2580    public int getOffsetForAdvance(char[] text, int start, int end, int contextStart,
2581            int contextEnd, boolean isRtl, float advance) {
2582        if (text == null) {
2583            throw new IllegalArgumentException("text cannot be null");
2584        }
2585        if ((contextStart | start | end | contextEnd
2586                | start - contextStart | end - start | contextEnd - end
2587                | text.length - contextEnd) < 0) {
2588            throw new IndexOutOfBoundsException();
2589        }
2590        // TODO: take mCompatScaling into account (or eliminate compat scaling)?
2591        return nGetOffsetForAdvance(mNativePaint, mNativeTypeface, text, start, end,
2592                contextStart, contextEnd, isRtl, advance);
2593    }
2594
2595    /**
2596     * @see #getOffsetForAdvance(char[], int, int, int, int, boolean, float)
2597     *
2598     * @param text the text to measure. Cannot be null.
2599     * @param start the index of the start of the range to measure
2600     * @param end the index + 1 of the end of the range to measure
2601     * @param contextStart the index of the start of the shaping context
2602     * @param contextEnd the index + 1 of the end of the range to measure
2603     * @param isRtl whether the run is in RTL direction
2604     * @param advance width relative to start of run
2605     * @return index of offset
2606     */
2607    public int getOffsetForAdvance(CharSequence text, int start, int end, int contextStart,
2608            int contextEnd, boolean isRtl, float advance) {
2609        if (text == null) {
2610            throw new IllegalArgumentException("text cannot be null");
2611        }
2612        if ((contextStart | start | end | contextEnd
2613                | start - contextStart | end - start | contextEnd - end
2614                | text.length() - contextEnd) < 0) {
2615            throw new IndexOutOfBoundsException();
2616        }
2617        // TODO performance: specialized alternatives to avoid buffer copy, if win is significant
2618        char[] buf = TemporaryBuffer.obtain(contextEnd - contextStart);
2619        TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
2620        int result = getOffsetForAdvance(buf, start - contextStart, end - contextStart, 0,
2621                contextEnd - contextStart, isRtl, advance) + contextStart;
2622        TemporaryBuffer.recycle(buf);
2623        return result;
2624    }
2625
2626    // regular JNI
2627    private static native long nGetNativeFinalizer();
2628    private static native long nInit();
2629    private static native long nInitWithPaint(long paint);
2630    private static native int nBreakText(long nObject, long nTypeface,
2631            char[] text, int index, int count,
2632            float maxWidth, int bidiFlags, float[] measuredWidth);
2633    private static native int nBreakText(long nObject, long nTypeface,
2634            String text, boolean measureForwards,
2635            float maxWidth, int bidiFlags, float[] measuredWidth);
2636    private static native float nGetTextAdvances(long paintPtr, long typefacePtr,
2637            char[] text, int index, int count, int contextIndex, int contextCount,
2638            int bidiFlags, float[] advances, int advancesIndex);
2639    private static native float nGetTextAdvances(long paintPtr, long typefacePtr,
2640            String text, int start, int end, int contextStart, int contextEnd,
2641            int bidiFlags, float[] advances, int advancesIndex);
2642    private native int nGetTextRunCursor(long paintPtr, char[] text,
2643            int contextStart, int contextLength, int dir, int offset, int cursorOpt);
2644    private native int nGetTextRunCursor(long paintPtr, String text,
2645            int contextStart, int contextEnd, int dir, int offset, int cursorOpt);
2646    private static native void nGetTextPath(long paintPtr, long typefacePtr,
2647            int bidiFlags, char[] text, int index, int count, float x, float y, long path);
2648    private static native void nGetTextPath(long paintPtr, long typefacePtr,
2649            int bidiFlags, String text, int start, int end, float x, float y, long path);
2650    private static native void nGetStringBounds(long nativePaint, long typefacePtr,
2651            String text, int start, int end, int bidiFlags, Rect bounds);
2652    private static native void nGetCharArrayBounds(long nativePaint, long typefacePtr,
2653            char[] text, int index, int count, int bidiFlags, Rect bounds);
2654    private static native boolean nHasGlyph(long paintPtr, long typefacePtr,
2655            int bidiFlags, String string);
2656    private static native float nGetRunAdvance(long paintPtr, long typefacePtr,
2657            char[] text, int start, int end, int contextStart, int contextEnd, boolean isRtl,
2658            int offset);
2659    private static native int nGetOffsetForAdvance(long paintPtr,
2660            long typefacePtr, char[] text, int start, int end, int contextStart, int contextEnd,
2661            boolean isRtl, float advance);
2662
2663
2664    // ---------------- @FastNative ------------------------
2665
2666    @FastNative
2667    private static native int nSetTextLocales(long paintPtr, String locales);
2668    @FastNative
2669    private static native void nSetFontFeatureSettings(long paintPtr, String settings);
2670    @FastNative
2671    private static native float nGetFontMetrics(long paintPtr,
2672            long typefacePtr, FontMetrics metrics);
2673    @FastNative
2674    private static native int nGetFontMetricsInt(long paintPtr,
2675            long typefacePtr, FontMetricsInt fmi);
2676
2677
2678    // ---------------- @CriticalNative ------------------------
2679
2680    @CriticalNative
2681    private static native void nReset(long paintPtr);
2682    @CriticalNative
2683    private static native void nSet(long paintPtrDest, long paintPtrSrc);
2684    @CriticalNative
2685    private static native int nGetStyle(long paintPtr);
2686    @CriticalNative
2687    private static native void nSetStyle(long paintPtr, int style);
2688    @CriticalNative
2689    private static native int nGetStrokeCap(long paintPtr);
2690    @CriticalNative
2691    private static native void nSetStrokeCap(long paintPtr, int cap);
2692    @CriticalNative
2693    private static native int nGetStrokeJoin(long paintPtr);
2694    @CriticalNative
2695    private static native void nSetStrokeJoin(long paintPtr, int join);
2696    @CriticalNative
2697    private static native boolean nGetFillPath(long paintPtr, long src, long dst);
2698    @CriticalNative
2699    private static native long nSetShader(long paintPtr, long shader);
2700    @CriticalNative
2701    private static native long nSetColorFilter(long paintPtr, long filter);
2702    @CriticalNative
2703    private static native void nSetXfermode(long paintPtr, int xfermode);
2704    @CriticalNative
2705    private static native long nSetPathEffect(long paintPtr, long effect);
2706    @CriticalNative
2707    private static native long nSetMaskFilter(long paintPtr, long maskfilter);
2708    @CriticalNative
2709    private static native long nSetTypeface(long paintPtr, long typeface);
2710    @CriticalNative
2711    private static native int nGetTextAlign(long paintPtr);
2712    @CriticalNative
2713    private static native void nSetTextAlign(long paintPtr, int align);
2714    @CriticalNative
2715    private static native void nSetTextLocalesByMinikinLangListId(long paintPtr,
2716            int mMinikinLangListId);
2717    @CriticalNative
2718    private static native void nSetShadowLayer(long paintPtr,
2719            float radius, float dx, float dy, int color);
2720    @CriticalNative
2721    private static native boolean nHasShadowLayer(long paintPtr);
2722    @CriticalNative
2723    private static native float nGetLetterSpacing(long paintPtr);
2724    @CriticalNative
2725    private static native void nSetLetterSpacing(long paintPtr, float letterSpacing);
2726    @CriticalNative
2727    private static native float nGetWordSpacing(long paintPtr);
2728    @CriticalNative
2729    private static native void nSetWordSpacing(long paintPtr, float wordSpacing);
2730    @CriticalNative
2731    private static native int nGetHyphenEdit(long paintPtr);
2732    @CriticalNative
2733    private static native void nSetHyphenEdit(long paintPtr, int hyphen);
2734    @CriticalNative
2735    private static native void nSetStrokeMiter(long paintPtr, float miter);
2736    @CriticalNative
2737    private static native float nGetStrokeMiter(long paintPtr);
2738    @CriticalNative
2739    private static native void nSetStrokeWidth(long paintPtr, float width);
2740    @CriticalNative
2741    private static native float nGetStrokeWidth(long paintPtr);
2742    @CriticalNative
2743    private static native void nSetAlpha(long paintPtr, int a);
2744    @CriticalNative
2745    private static native void nSetDither(long paintPtr, boolean dither);
2746    @CriticalNative
2747    private static native int nGetFlags(long paintPtr);
2748    @CriticalNative
2749    private static native void nSetFlags(long paintPtr, int flags);
2750    @CriticalNative
2751    private static native int nGetHinting(long paintPtr);
2752    @CriticalNative
2753    private static native void nSetHinting(long paintPtr, int mode);
2754    @CriticalNative
2755    private static native void nSetAntiAlias(long paintPtr, boolean aa);
2756    @CriticalNative
2757    private static native void nSetLinearText(long paintPtr, boolean linearText);
2758    @CriticalNative
2759    private static native void nSetSubpixelText(long paintPtr, boolean subpixelText);
2760    @CriticalNative
2761    private static native void nSetUnderlineText(long paintPtr, boolean underlineText);
2762    @CriticalNative
2763    private static native void nSetFakeBoldText(long paintPtr, boolean fakeBoldText);
2764    @CriticalNative
2765    private static native void nSetFilterBitmap(long paintPtr, boolean filter);
2766    @CriticalNative
2767    private static native int nGetColor(long paintPtr);
2768    @CriticalNative
2769    private static native void nSetColor(long paintPtr, @ColorInt int color);
2770    @CriticalNative
2771    private static native int nGetAlpha(long paintPtr);
2772    @CriticalNative
2773    private static native void nSetStrikeThruText(long paintPtr, boolean strikeThruText);
2774    @CriticalNative
2775    private static native boolean nIsElegantTextHeight(long paintPtr);
2776    @CriticalNative
2777    private static native void nSetElegantTextHeight(long paintPtr, boolean elegant);
2778    @CriticalNative
2779    private static native float nGetTextSize(long paintPtr);
2780    @CriticalNative
2781    private static native float nGetTextScaleX(long paintPtr);
2782    @CriticalNative
2783    private static native void nSetTextScaleX(long paintPtr, float scaleX);
2784    @CriticalNative
2785    private static native float nGetTextSkewX(long paintPtr);
2786    @CriticalNative
2787    private static native void nSetTextSkewX(long paintPtr, float skewX);
2788    @CriticalNative
2789    private static native float nAscent(long paintPtr, long typefacePtr);
2790    @CriticalNative
2791    private static native float nDescent(long paintPtr, long typefacePtr);
2792    @CriticalNative
2793    private static native void nSetTextSize(long paintPtr, float textSize);
2794}
2795