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