Paint.java revision f6765f3965ce669458e0bb5d2e226d4233a1acb3
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.text.GraphicsOperations; 23import android.text.SpannableString; 24import android.text.SpannedString; 25import android.text.TextUtils; 26import android.util.LocaleList; 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 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.ni(), dst.ni()); 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 long xfermodeNative = 0; 1100 if (xfermode != null) 1101 xfermodeNative = xfermode.native_instance; 1102 nSetXfermode(mNativePaint, xfermodeNative); 1103 mXfermode = xfermode; 1104 return xfermode; 1105 } 1106 1107 /** 1108 * Get the paint's patheffect object. 1109 * 1110 * @return the paint's patheffect (or null) 1111 */ 1112 public PathEffect getPathEffect() { 1113 return mPathEffect; 1114 } 1115 1116 /** 1117 * Set or clear the patheffect object. 1118 * <p /> 1119 * Pass null to clear any previous patheffect. 1120 * As a convenience, the parameter passed is also returned. 1121 * 1122 * @param effect May be null. The patheffect to be installed in the paint 1123 * @return effect 1124 */ 1125 public PathEffect setPathEffect(PathEffect effect) { 1126 long effectNative = 0; 1127 if (effect != null) { 1128 effectNative = effect.native_instance; 1129 } 1130 nSetPathEffect(mNativePaint, effectNative); 1131 mPathEffect = effect; 1132 return effect; 1133 } 1134 1135 /** 1136 * Get the paint's maskfilter object. 1137 * 1138 * @return the paint's maskfilter (or null) 1139 */ 1140 public MaskFilter getMaskFilter() { 1141 return mMaskFilter; 1142 } 1143 1144 /** 1145 * Set or clear the maskfilter object. 1146 * <p /> 1147 * Pass null to clear any previous maskfilter. 1148 * As a convenience, the parameter passed is also returned. 1149 * 1150 * @param maskfilter May be null. The maskfilter to be installed in the 1151 * paint 1152 * @return maskfilter 1153 */ 1154 public MaskFilter setMaskFilter(MaskFilter maskfilter) { 1155 long maskfilterNative = 0; 1156 if (maskfilter != null) { 1157 maskfilterNative = maskfilter.native_instance; 1158 } 1159 nSetMaskFilter(mNativePaint, maskfilterNative); 1160 mMaskFilter = maskfilter; 1161 return maskfilter; 1162 } 1163 1164 /** 1165 * Get the paint's typeface object. 1166 * <p /> 1167 * The typeface object identifies which font to use when drawing or 1168 * measuring text. 1169 * 1170 * @return the paint's typeface (or null) 1171 */ 1172 public Typeface getTypeface() { 1173 return mTypeface; 1174 } 1175 1176 /** 1177 * Set or clear the typeface object. 1178 * <p /> 1179 * Pass null to clear any previous typeface. 1180 * As a convenience, the parameter passed is also returned. 1181 * 1182 * @param typeface May be null. The typeface to be installed in the paint 1183 * @return typeface 1184 */ 1185 public Typeface setTypeface(Typeface typeface) { 1186 long typefaceNative = 0; 1187 if (typeface != null) { 1188 typefaceNative = typeface.native_instance; 1189 } 1190 nSetTypeface(mNativePaint, typefaceNative); 1191 mTypeface = typeface; 1192 mNativeTypeface = typefaceNative; 1193 return typeface; 1194 } 1195 1196 /** 1197 * Get the paint's rasterizer (or null). 1198 * <p /> 1199 * The raster controls/modifies how paths/text are turned into alpha masks. 1200 * 1201 * @return the paint's rasterizer (or null) 1202 * 1203 * @deprecated Rasterizer is not supported by either the HW or PDF backends. 1204 */ 1205 @Deprecated 1206 public Rasterizer getRasterizer() { 1207 return mRasterizer; 1208 } 1209 1210 /** 1211 * Set or clear the rasterizer object. 1212 * <p /> 1213 * Pass null to clear any previous rasterizer. 1214 * As a convenience, the parameter passed is also returned. 1215 * 1216 * @param rasterizer May be null. The new rasterizer to be installed in 1217 * the paint. 1218 * @return rasterizer 1219 * 1220 * @deprecated Rasterizer is not supported by either the HW or PDF backends. 1221 */ 1222 @Deprecated 1223 public Rasterizer setRasterizer(Rasterizer rasterizer) { 1224 long rasterizerNative = 0; 1225 if (rasterizer != null) { 1226 rasterizerNative = rasterizer.native_instance; 1227 } 1228 nSetRasterizer(mNativePaint, rasterizerNative); 1229 mRasterizer = rasterizer; 1230 return rasterizer; 1231 } 1232 1233 /** 1234 * This draws a shadow layer below the main layer, with the specified 1235 * offset and color, and blur radius. If radius is 0, then the shadow 1236 * layer is removed. 1237 * <p> 1238 * Can be used to create a blurred shadow underneath text. Support for use 1239 * with other drawing operations is constrained to the software rendering 1240 * pipeline. 1241 * <p> 1242 * The alpha of the shadow will be the paint's alpha if the shadow color is 1243 * opaque, or the alpha from the shadow color if not. 1244 */ 1245 public void setShadowLayer(float radius, float dx, float dy, int shadowColor) { 1246 nSetShadowLayer(mNativePaint, radius, dx, dy, shadowColor); 1247 } 1248 1249 /** 1250 * Clear the shadow layer. 1251 */ 1252 public void clearShadowLayer() { 1253 setShadowLayer(0, 0, 0, 0); 1254 } 1255 1256 /** 1257 * Checks if the paint has a shadow layer attached 1258 * 1259 * @return true if the paint has a shadow layer attached and false otherwise 1260 * @hide 1261 */ 1262 public boolean hasShadowLayer() { 1263 return nHasShadowLayer(mNativePaint); 1264 } 1265 1266 /** 1267 * Return the paint's Align value for drawing text. This controls how the 1268 * text is positioned relative to its origin. LEFT align means that all of 1269 * the text will be drawn to the right of its origin (i.e. the origin 1270 * specifieds the LEFT edge of the text) and so on. 1271 * 1272 * @return the paint's Align value for drawing text. 1273 */ 1274 public Align getTextAlign() { 1275 return sAlignArray[nGetTextAlign(mNativePaint)]; 1276 } 1277 1278 /** 1279 * Set the paint's text alignment. This controls how the 1280 * text is positioned relative to its origin. LEFT align means that all of 1281 * the text will be drawn to the right of its origin (i.e. the origin 1282 * specifieds the LEFT edge of the text) and so on. 1283 * 1284 * @param align set the paint's Align value for drawing text. 1285 */ 1286 public void setTextAlign(Align align) { 1287 nSetTextAlign(mNativePaint, align.nativeInt); 1288 } 1289 1290 /** 1291 * Get the text's primary Locale. Note that this is not all of the locale-related information 1292 * Paint has. Use {@link #getTextLocales()} to get the complete list. 1293 * 1294 * @return the paint's primary Locale used for drawing text, never null. 1295 */ 1296 @NonNull 1297 public Locale getTextLocale() { 1298 return mLocales.get(0); 1299 } 1300 1301 /** 1302 * Get the text locale list. 1303 * 1304 * @return the paint's LocaleList used for drawing text, never null or empty. 1305 */ 1306 @NonNull @Size(min=1) 1307 public LocaleList getTextLocales() { 1308 return mLocales; 1309 } 1310 1311 /** 1312 * Set the text locale list to a one-member list consisting of just the locale. 1313 * 1314 * See {@link #setTextLocales(LocaleList)} for how the locale list affects 1315 * the way the text is drawn for some languages. 1316 * 1317 * @param locale the paint's locale value for drawing text, must not be null. 1318 */ 1319 public void setTextLocale(@NonNull Locale locale) { 1320 if (locale == null) { 1321 throw new IllegalArgumentException("locale cannot be null"); 1322 } 1323 if (mLocales != null && mLocales.size() == 1 && locale.equals(mLocales.get(0))) { 1324 return; 1325 } 1326 mLocales = new LocaleList(locale); 1327 syncTextLocalesWithMinikin(); 1328 } 1329 1330 /** 1331 * Set the text locale list. 1332 * 1333 * The text locale list affects how the text is drawn for some languages. 1334 * 1335 * For example, if the locale list contains {@link Locale#CHINESE} or {@link Locale#CHINA}, 1336 * then the text renderer will prefer to draw text using a Chinese font. Likewise, 1337 * if the locale list contains {@link Locale#JAPANESE} or {@link Locale#JAPAN}, then the text 1338 * renderer will prefer to draw text using a Japanese font. If the locale list contains both, 1339 * the order those locales appear in the list is considered for deciding the font. 1340 * 1341 * This distinction is important because Chinese and Japanese text both use many 1342 * of the same Unicode code points but their appearance is subtly different for 1343 * each language. 1344 * 1345 * By default, the text locale list is initialized to a one-member list just containing the 1346 * system locales. This assumes that the text to be rendered will most likely be in the user's 1347 * preferred language. 1348 * 1349 * If the actual language or languages of the text is/are known, then they can be provided to 1350 * the text renderer using this method. The text renderer may attempt to guess the 1351 * language script based on the contents of the text to be drawn independent of 1352 * the text locale here. Specifying the text locales just helps it do a better 1353 * job in certain ambiguous cases. 1354 * 1355 * @param locales the paint's locale list for drawing text, must not be null or empty. 1356 */ 1357 public void setTextLocales(@NonNull @Size(min=1) LocaleList locales) { 1358 if (locales == null || locales.isEmpty()) { 1359 throw new IllegalArgumentException("locales cannot be null or empty"); 1360 } 1361 if (locales.equals(mLocales)) return; 1362 mLocales = locales; 1363 syncTextLocalesWithMinikin(); 1364 } 1365 1366 private void syncTextLocalesWithMinikin() { 1367 final String languageTags = mLocales.toLanguageTags(); 1368 final Integer minikinLangListId; 1369 synchronized (sCacheLock) { 1370 minikinLangListId = sMinikinLangListIdCache.get(languageTags); 1371 if (minikinLangListId == null) { 1372 final int newID = nSetTextLocales(mNativePaint, languageTags); 1373 sMinikinLangListIdCache.put(languageTags, newID); 1374 return; 1375 } 1376 } 1377 nSetTextLocalesByMinikinLangListId(mNativePaint, minikinLangListId.intValue()); 1378 } 1379 1380 /** 1381 * Get the elegant metrics flag. 1382 * 1383 * @return true if elegant metrics are enabled for text drawing. 1384 */ 1385 public boolean isElegantTextHeight() { 1386 return nIsElegantTextHeight(mNativePaint); 1387 } 1388 1389 private native boolean nIsElegantTextHeight(long paintPtr); 1390 1391 /** 1392 * Set the paint's elegant height metrics flag. This setting selects font 1393 * variants that have not been compacted to fit Latin-based vertical 1394 * metrics, and also increases top and bottom bounds to provide more space. 1395 * 1396 * @param elegant set the paint's elegant metrics flag for drawing text. 1397 */ 1398 public void setElegantTextHeight(boolean elegant) { 1399 nSetElegantTextHeight(mNativePaint, elegant); 1400 } 1401 1402 private native void nSetElegantTextHeight(long paintPtr, boolean elegant); 1403 1404 /** 1405 * Return the paint's text size. 1406 * 1407 * @return the paint's text size. 1408 */ 1409 public float getTextSize() { 1410 return nGetTextSize(mNativePaint); 1411 } 1412 1413 private native float nGetTextSize(long paintPtr); 1414 1415 /** 1416 * Set the paint's text size. This value must be > 0 1417 * 1418 * @param textSize set the paint's text size. 1419 */ 1420 public void setTextSize(float textSize) { 1421 nSetTextSize(mNativePaint, textSize); 1422 } 1423 1424 private native void nSetTextSize(long paintPtr, float textSize); 1425 1426 /** 1427 * Return the paint's horizontal scale factor for text. The default value 1428 * is 1.0. 1429 * 1430 * @return the paint's scale factor in X for drawing/measuring text 1431 */ 1432 public float getTextScaleX() { 1433 return nGetTextScaleX(mNativePaint); 1434 } 1435 1436 private native float nGetTextScaleX(long paintPtr); 1437 1438 /** 1439 * Set the paint's horizontal scale factor for text. The default value 1440 * is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will 1441 * stretch the text narrower. 1442 * 1443 * @param scaleX set the paint's scale in X for drawing/measuring text. 1444 */ 1445 public void setTextScaleX(float scaleX) { 1446 nSetTextScaleX(mNativePaint, scaleX); 1447 } 1448 1449 private native void nSetTextScaleX(long paintPtr, float scaleX); 1450 1451 /** 1452 * Return the paint's horizontal skew factor for text. The default value 1453 * is 0. 1454 * 1455 * @return the paint's skew factor in X for drawing text. 1456 */ 1457 public float getTextSkewX() { 1458 return nGetTextSkewX(mNativePaint); 1459 } 1460 1461 private native float nGetTextSkewX(long paintPtr); 1462 1463 /** 1464 * Set the paint's horizontal skew factor for text. The default value 1465 * is 0. For approximating oblique text, use values around -0.25. 1466 * 1467 * @param skewX set the paint's skew factor in X for drawing text. 1468 */ 1469 public void setTextSkewX(float skewX) { 1470 nSetTextSkewX(mNativePaint, skewX); 1471 } 1472 1473 private native void nSetTextSkewX(long paintPtr, float skewX); 1474 1475 /** 1476 * Return the paint's letter-spacing for text. The default value 1477 * is 0. 1478 * 1479 * @return the paint's letter-spacing for drawing text. 1480 */ 1481 public float getLetterSpacing() { 1482 return nGetLetterSpacing(mNativePaint); 1483 } 1484 1485 /** 1486 * Set the paint's letter-spacing for text. The default value 1487 * is 0. The value is in 'EM' units. Typical values for slight 1488 * expansion will be around 0.05. Negative values tighten text. 1489 * 1490 * @param letterSpacing set the paint's letter-spacing for drawing text. 1491 */ 1492 public void setLetterSpacing(float letterSpacing) { 1493 nSetLetterSpacing(mNativePaint, letterSpacing); 1494 } 1495 1496 /** 1497 * Get font feature settings. Default is null. 1498 * 1499 * @return the paint's currently set font feature settings. 1500 */ 1501 public String getFontFeatureSettings() { 1502 return mFontFeatureSettings; 1503 } 1504 1505 /** 1506 * Set font feature settings. 1507 * 1508 * The format is the same as the CSS font-feature-settings attribute: 1509 * http://dev.w3.org/csswg/css-fonts/#propdef-font-feature-settings 1510 * 1511 * @param settings the font feature settings string to use, may be null. 1512 */ 1513 public void setFontFeatureSettings(String settings) { 1514 if (settings != null && settings.equals("")) { 1515 settings = null; 1516 } 1517 if ((settings == null && mFontFeatureSettings == null) 1518 || (settings != null && settings.equals(mFontFeatureSettings))) { 1519 return; 1520 } 1521 mFontFeatureSettings = settings; 1522 nSetFontFeatureSettings(mNativePaint, settings); 1523 } 1524 1525 /** 1526 * Get the current value of hyphen edit. 1527 * 1528 * @return the current hyphen edit value 1529 * 1530 * @hide 1531 */ 1532 public int getHyphenEdit() { 1533 return nGetHyphenEdit(mNativePaint); 1534 } 1535 1536 /** 1537 * Set a hyphen edit on the paint (causes a hyphen to be added to text when 1538 * measured or drawn). 1539 * 1540 * @param hyphen 0 for no edit, 1 for adding a hyphen (other values in future) 1541 * 1542 * @hide 1543 */ 1544 public void setHyphenEdit(int hyphen) { 1545 nSetHyphenEdit(mNativePaint, hyphen); 1546 } 1547 1548 /** 1549 * Return the distance above (negative) the baseline (ascent) based on the 1550 * current typeface and text size. 1551 * 1552 * @return the distance above (negative) the baseline (ascent) based on the 1553 * current typeface and text size. 1554 */ 1555 public float ascent() { 1556 return nAscent(mNativePaint, mNativeTypeface); 1557 } 1558 1559 private native float nAscent(long paintPtr, long typefacePtr); 1560 1561 /** 1562 * Return the distance below (positive) the baseline (descent) based on the 1563 * current typeface and text size. 1564 * 1565 * @return the distance below (positive) the baseline (descent) based on 1566 * the current typeface and text size. 1567 */ 1568 public float descent() { 1569 return nDescent(mNativePaint, mNativeTypeface); 1570 } 1571 1572 private native float nDescent(long paintPtr, long typefacePtr); 1573 1574 /** 1575 * Class that describes the various metrics for a font at a given text size. 1576 * Remember, Y values increase going down, so those values will be positive, 1577 * and values that measure distances going up will be negative. This class 1578 * is returned by getFontMetrics(). 1579 */ 1580 public static class FontMetrics { 1581 /** 1582 * The maximum distance above the baseline for the tallest glyph in 1583 * the font at a given text size. 1584 */ 1585 public float top; 1586 /** 1587 * The recommended distance above the baseline for singled spaced text. 1588 */ 1589 public float ascent; 1590 /** 1591 * The recommended distance below the baseline for singled spaced text. 1592 */ 1593 public float descent; 1594 /** 1595 * The maximum distance below the baseline for the lowest glyph in 1596 * the font at a given text size. 1597 */ 1598 public float bottom; 1599 /** 1600 * The recommended additional space to add between lines of text. 1601 */ 1602 public float leading; 1603 } 1604 1605 /** 1606 * Return the font's recommended interline spacing, given the Paint's 1607 * settings for typeface, textSize, etc. If metrics is not null, return the 1608 * fontmetric values in it. 1609 * 1610 * @param metrics If this object is not null, its fields are filled with 1611 * the appropriate values given the paint's text attributes. 1612 * @return the font's recommended interline spacing. 1613 */ 1614 public float getFontMetrics(FontMetrics metrics) { 1615 return nGetFontMetrics(mNativePaint, mNativeTypeface, metrics); 1616 } 1617 1618 private native float nGetFontMetrics(long paintPtr, 1619 long typefacePtr, FontMetrics metrics); 1620 1621 /** 1622 * Allocates a new FontMetrics object, and then calls getFontMetrics(fm) 1623 * with it, returning the object. 1624 */ 1625 public FontMetrics getFontMetrics() { 1626 FontMetrics fm = new FontMetrics(); 1627 getFontMetrics(fm); 1628 return fm; 1629 } 1630 1631 /** 1632 * Convenience method for callers that want to have FontMetrics values as 1633 * integers. 1634 */ 1635 public static class FontMetricsInt { 1636 public int top; 1637 public int ascent; 1638 public int descent; 1639 public int bottom; 1640 public int leading; 1641 1642 @Override public String toString() { 1643 return "FontMetricsInt: top=" + top + " ascent=" + ascent + 1644 " descent=" + descent + " bottom=" + bottom + 1645 " leading=" + leading; 1646 } 1647 } 1648 1649 /** 1650 * Return the font's interline spacing, given the Paint's settings for 1651 * typeface, textSize, etc. If metrics is not null, return the fontmetric 1652 * values in it. Note: all values have been converted to integers from 1653 * floats, in such a way has to make the answers useful for both spacing 1654 * and clipping. If you want more control over the rounding, call 1655 * getFontMetrics(). 1656 * 1657 * @return the font's interline spacing. 1658 */ 1659 public int getFontMetricsInt(FontMetricsInt fmi) { 1660 return nGetFontMetricsInt(mNativePaint, mNativeTypeface, fmi); 1661 } 1662 1663 private native int nGetFontMetricsInt(long paintPtr, 1664 long typefacePtr, FontMetricsInt fmi); 1665 1666 public FontMetricsInt getFontMetricsInt() { 1667 FontMetricsInt fm = new FontMetricsInt(); 1668 getFontMetricsInt(fm); 1669 return fm; 1670 } 1671 1672 /** 1673 * Return the recommend line spacing based on the current typeface and 1674 * text size. 1675 * 1676 * @return recommend line spacing based on the current typeface and 1677 * text size. 1678 */ 1679 public float getFontSpacing() { 1680 return getFontMetrics(null); 1681 } 1682 1683 /** 1684 * Return the width of the text. 1685 * 1686 * @param text The text to measure. Cannot be null. 1687 * @param index The index of the first character to start measuring 1688 * @param count THe number of characters to measure, beginning with start 1689 * @return The width of the text 1690 */ 1691 public float measureText(char[] text, int index, int count) { 1692 if (text == null) { 1693 throw new IllegalArgumentException("text cannot be null"); 1694 } 1695 if ((index | count) < 0 || index + count > text.length) { 1696 throw new ArrayIndexOutOfBoundsException(); 1697 } 1698 1699 if (text.length == 0 || count == 0) { 1700 return 0f; 1701 } 1702 if (!mHasCompatScaling) { 1703 return (float) Math.ceil(nGetTextAdvances(mNativePaint, mNativeTypeface, text, 1704 index, count, index, count, mBidiFlags, null, 0)); 1705 } 1706 1707 final float oldSize = getTextSize(); 1708 setTextSize(oldSize * mCompatScaling); 1709 float w = nGetTextAdvances(mNativePaint, mNativeTypeface, text, index, count, index, 1710 count, mBidiFlags, null, 0); 1711 setTextSize(oldSize); 1712 return (float) Math.ceil(w*mInvCompatScaling); 1713 } 1714 1715 /** 1716 * Return the width of the text. 1717 * 1718 * @param text The text to measure. Cannot be null. 1719 * @param start The index of the first character to start measuring 1720 * @param end 1 beyond the index of the last character to measure 1721 * @return The width of the text 1722 */ 1723 public float measureText(String text, int start, int end) { 1724 if (text == null) { 1725 throw new IllegalArgumentException("text cannot be null"); 1726 } 1727 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1728 throw new IndexOutOfBoundsException(); 1729 } 1730 1731 if (text.length() == 0 || start == end) { 1732 return 0f; 1733 } 1734 if (!mHasCompatScaling) { 1735 return (float) Math.ceil(nGetTextAdvances(mNativePaint, mNativeTypeface, text, 1736 start, end, start, end, mBidiFlags, null, 0)); 1737 } 1738 final float oldSize = getTextSize(); 1739 setTextSize(oldSize * mCompatScaling); 1740 float w = nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, end, start, 1741 end, mBidiFlags, null, 0); 1742 setTextSize(oldSize); 1743 return (float) Math.ceil(w * mInvCompatScaling); 1744 } 1745 1746 /** 1747 * Return the width of the text. 1748 * 1749 * @param text The text to measure. Cannot be null. 1750 * @return The width of the text 1751 */ 1752 public float measureText(String text) { 1753 if (text == null) { 1754 throw new IllegalArgumentException("text cannot be null"); 1755 } 1756 return measureText(text, 0, text.length()); 1757 } 1758 1759 /** 1760 * Return the width of the text. 1761 * 1762 * @param text The text to measure 1763 * @param start The index of the first character to start measuring 1764 * @param end 1 beyond the index of the last character to measure 1765 * @return The width of the text 1766 */ 1767 public float measureText(CharSequence text, int start, int end) { 1768 if (text == null) { 1769 throw new IllegalArgumentException("text cannot be null"); 1770 } 1771 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1772 throw new IndexOutOfBoundsException(); 1773 } 1774 1775 if (text.length() == 0 || start == end) { 1776 return 0f; 1777 } 1778 if (text instanceof String) { 1779 return measureText((String)text, start, end); 1780 } 1781 if (text instanceof SpannedString || 1782 text instanceof SpannableString) { 1783 return measureText(text.toString(), start, end); 1784 } 1785 if (text instanceof GraphicsOperations) { 1786 return ((GraphicsOperations)text).measureText(start, end, this); 1787 } 1788 1789 char[] buf = TemporaryBuffer.obtain(end - start); 1790 TextUtils.getChars(text, start, end, buf, 0); 1791 float result = measureText(buf, 0, end - start); 1792 TemporaryBuffer.recycle(buf); 1793 return result; 1794 } 1795 1796 /** 1797 * Measure the text, stopping early if the measured width exceeds maxWidth. 1798 * Return the number of chars that were measured, and if measuredWidth is 1799 * not null, return in it the actual width measured. 1800 * 1801 * @param text The text to measure. Cannot be null. 1802 * @param index The offset into text to begin measuring at 1803 * @param count The number of maximum number of entries to measure. If count 1804 * is negative, then the characters are measured in reverse order. 1805 * @param maxWidth The maximum width to accumulate. 1806 * @param measuredWidth Optional. If not null, returns the actual width 1807 * measured. 1808 * @return The number of chars that were measured. Will always be <= 1809 * abs(count). 1810 */ 1811 public int breakText(char[] text, int index, int count, 1812 float maxWidth, float[] measuredWidth) { 1813 if (text == null) { 1814 throw new IllegalArgumentException("text cannot be null"); 1815 } 1816 if (index < 0 || text.length - index < Math.abs(count)) { 1817 throw new ArrayIndexOutOfBoundsException(); 1818 } 1819 1820 if (text.length == 0 || count == 0) { 1821 return 0; 1822 } 1823 if (!mHasCompatScaling) { 1824 return nBreakText(mNativePaint, mNativeTypeface, text, index, count, maxWidth, 1825 mBidiFlags, measuredWidth); 1826 } 1827 1828 final float oldSize = getTextSize(); 1829 setTextSize(oldSize * mCompatScaling); 1830 int res = nBreakText(mNativePaint, mNativeTypeface, text, index, count, 1831 maxWidth * mCompatScaling, mBidiFlags, measuredWidth); 1832 setTextSize(oldSize); 1833 if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling; 1834 return res; 1835 } 1836 1837 private static native int nBreakText(long nObject, long nTypeface, 1838 char[] text, int index, int count, 1839 float maxWidth, int bidiFlags, float[] measuredWidth); 1840 1841 /** 1842 * Measure the text, stopping early if the measured width exceeds maxWidth. 1843 * Return the number of chars that were measured, and if measuredWidth is 1844 * not null, return in it the actual width measured. 1845 * 1846 * @param text The text to measure. Cannot be null. 1847 * @param start The offset into text to begin measuring at 1848 * @param end The end of the text slice to measure. 1849 * @param measureForwards If true, measure forwards, starting at start. 1850 * Otherwise, measure backwards, starting with end. 1851 * @param maxWidth The maximum width to accumulate. 1852 * @param measuredWidth Optional. If not null, returns the actual width 1853 * measured. 1854 * @return The number of chars that were measured. Will always be <= 1855 * abs(end - start). 1856 */ 1857 public int breakText(CharSequence text, int start, int end, 1858 boolean measureForwards, 1859 float maxWidth, float[] measuredWidth) { 1860 if (text == null) { 1861 throw new IllegalArgumentException("text cannot be null"); 1862 } 1863 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1864 throw new IndexOutOfBoundsException(); 1865 } 1866 1867 if (text.length() == 0 || start == end) { 1868 return 0; 1869 } 1870 if (start == 0 && text instanceof String && end == text.length()) { 1871 return breakText((String) text, measureForwards, maxWidth, 1872 measuredWidth); 1873 } 1874 1875 char[] buf = TemporaryBuffer.obtain(end - start); 1876 int result; 1877 1878 TextUtils.getChars(text, start, end, buf, 0); 1879 1880 if (measureForwards) { 1881 result = breakText(buf, 0, end - start, maxWidth, measuredWidth); 1882 } else { 1883 result = breakText(buf, 0, -(end - start), maxWidth, measuredWidth); 1884 } 1885 1886 TemporaryBuffer.recycle(buf); 1887 return result; 1888 } 1889 1890 /** 1891 * Measure the text, stopping early if the measured width exceeds maxWidth. 1892 * Return the number of chars that were measured, and if measuredWidth is 1893 * not null, return in it the actual width measured. 1894 * 1895 * @param text The text to measure. Cannot be null. 1896 * @param measureForwards If true, measure forwards, starting with the 1897 * first character in the string. Otherwise, 1898 * measure backwards, starting with the 1899 * last character in the string. 1900 * @param maxWidth The maximum width to accumulate. 1901 * @param measuredWidth Optional. If not null, returns the actual width 1902 * measured. 1903 * @return The number of chars that were measured. Will always be <= 1904 * abs(count). 1905 */ 1906 public int breakText(String text, boolean measureForwards, 1907 float maxWidth, float[] measuredWidth) { 1908 if (text == null) { 1909 throw new IllegalArgumentException("text cannot be null"); 1910 } 1911 1912 if (text.length() == 0) { 1913 return 0; 1914 } 1915 if (!mHasCompatScaling) { 1916 return nBreakText(mNativePaint, mNativeTypeface, text, measureForwards, 1917 maxWidth, mBidiFlags, measuredWidth); 1918 } 1919 1920 final float oldSize = getTextSize(); 1921 setTextSize(oldSize*mCompatScaling); 1922 int res = nBreakText(mNativePaint, mNativeTypeface, text, measureForwards, 1923 maxWidth*mCompatScaling, mBidiFlags, measuredWidth); 1924 setTextSize(oldSize); 1925 if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling; 1926 return res; 1927 } 1928 1929 private static native int nBreakText(long nObject, long nTypeface, 1930 String text, boolean measureForwards, 1931 float maxWidth, int bidiFlags, float[] measuredWidth); 1932 1933 /** 1934 * Return the advance widths for the characters in the string. 1935 * 1936 * @param text The text to measure. Cannot be null. 1937 * @param index The index of the first char to to measure 1938 * @param count The number of chars starting with index to measure 1939 * @param widths array to receive the advance widths of the characters. 1940 * Must be at least a large as count. 1941 * @return the actual number of widths returned. 1942 */ 1943 public int getTextWidths(char[] text, int index, int count, 1944 float[] widths) { 1945 if (text == null) { 1946 throw new IllegalArgumentException("text cannot be null"); 1947 } 1948 if ((index | count) < 0 || index + count > text.length 1949 || count > widths.length) { 1950 throw new ArrayIndexOutOfBoundsException(); 1951 } 1952 1953 if (text.length == 0 || count == 0) { 1954 return 0; 1955 } 1956 if (!mHasCompatScaling) { 1957 nGetTextAdvances(mNativePaint, mNativeTypeface, text, index, count, index, count, 1958 mBidiFlags, widths, 0); 1959 return count; 1960 } 1961 1962 final float oldSize = getTextSize(); 1963 setTextSize(oldSize * mCompatScaling); 1964 nGetTextAdvances(mNativePaint, mNativeTypeface, text, index, count, index, count, 1965 mBidiFlags, widths, 0); 1966 setTextSize(oldSize); 1967 for (int i = 0; i < count; i++) { 1968 widths[i] *= mInvCompatScaling; 1969 } 1970 return count; 1971 } 1972 1973 /** 1974 * Return the advance widths for the characters in the string. 1975 * 1976 * @param text The text to measure. Cannot be null. 1977 * @param start The index of the first char to to measure 1978 * @param end The end of the text slice to measure 1979 * @param widths array to receive the advance widths of the characters. 1980 * Must be at least a large as (end - start). 1981 * @return the actual number of widths returned. 1982 */ 1983 public int getTextWidths(CharSequence text, int start, int end, 1984 float[] widths) { 1985 if (text == null) { 1986 throw new IllegalArgumentException("text cannot be null"); 1987 } 1988 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1989 throw new IndexOutOfBoundsException(); 1990 } 1991 if (end - start > widths.length) { 1992 throw new ArrayIndexOutOfBoundsException(); 1993 } 1994 1995 if (text.length() == 0 || start == end) { 1996 return 0; 1997 } 1998 if (text instanceof String) { 1999 return getTextWidths((String) text, start, end, widths); 2000 } 2001 if (text instanceof SpannedString || 2002 text instanceof SpannableString) { 2003 return getTextWidths(text.toString(), start, end, widths); 2004 } 2005 if (text instanceof GraphicsOperations) { 2006 return ((GraphicsOperations) text).getTextWidths(start, end, 2007 widths, this); 2008 } 2009 2010 char[] buf = TemporaryBuffer.obtain(end - start); 2011 TextUtils.getChars(text, start, end, buf, 0); 2012 int result = getTextWidths(buf, 0, end - start, widths); 2013 TemporaryBuffer.recycle(buf); 2014 return result; 2015 } 2016 2017 /** 2018 * Return the advance widths for the characters in the string. 2019 * 2020 * @param text The text to measure. Cannot be null. 2021 * @param start The index of the first char to to measure 2022 * @param end The end of the text slice to measure 2023 * @param widths array to receive the advance widths of the characters. 2024 * Must be at least a large as the text. 2025 * @return the number of code units in the specified text. 2026 */ 2027 public int getTextWidths(String text, int start, int end, float[] widths) { 2028 if (text == null) { 2029 throw new IllegalArgumentException("text cannot be null"); 2030 } 2031 if ((start | end | (end - start) | (text.length() - end)) < 0) { 2032 throw new IndexOutOfBoundsException(); 2033 } 2034 if (end - start > widths.length) { 2035 throw new ArrayIndexOutOfBoundsException(); 2036 } 2037 2038 if (text.length() == 0 || start == end) { 2039 return 0; 2040 } 2041 if (!mHasCompatScaling) { 2042 nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, end, start, end, 2043 mBidiFlags, widths, 0); 2044 return end - start; 2045 } 2046 2047 final float oldSize = getTextSize(); 2048 setTextSize(oldSize * mCompatScaling); 2049 nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, end, start, end, 2050 mBidiFlags, widths, 0); 2051 setTextSize(oldSize); 2052 for (int i = 0; i < end - start; i++) { 2053 widths[i] *= mInvCompatScaling; 2054 } 2055 return end - start; 2056 } 2057 2058 /** 2059 * Return the advance widths for the characters in the string. 2060 * 2061 * @param text The text to measure 2062 * @param widths array to receive the advance widths of the characters. 2063 * Must be at least a large as the text. 2064 * @return the number of code units in the specified text. 2065 */ 2066 public int getTextWidths(String text, float[] widths) { 2067 return getTextWidths(text, 0, text.length(), widths); 2068 } 2069 2070 /** 2071 * Convenience overload that takes a char array instead of a 2072 * String. 2073 * 2074 * @see #getTextRunAdvances(String, int, int, int, int, boolean, float[], int) 2075 * @hide 2076 */ 2077 public float getTextRunAdvances(char[] chars, int index, int count, 2078 int contextIndex, int contextCount, boolean isRtl, float[] advances, 2079 int advancesIndex) { 2080 2081 if (chars == null) { 2082 throw new IllegalArgumentException("text cannot be null"); 2083 } 2084 if ((index | count | contextIndex | contextCount | advancesIndex 2085 | (index - contextIndex) | (contextCount - count) 2086 | ((contextIndex + contextCount) - (index + count)) 2087 | (chars.length - (contextIndex + contextCount)) 2088 | (advances == null ? 0 : 2089 (advances.length - (advancesIndex + count)))) < 0) { 2090 throw new IndexOutOfBoundsException(); 2091 } 2092 2093 if (chars.length == 0 || count == 0){ 2094 return 0f; 2095 } 2096 if (!mHasCompatScaling) { 2097 return nGetTextAdvances(mNativePaint, mNativeTypeface, chars, index, count, 2098 contextIndex, contextCount, isRtl ? BIDI_FORCE_RTL : BIDI_FORCE_LTR, advances, 2099 advancesIndex); 2100 } 2101 2102 final float oldSize = getTextSize(); 2103 setTextSize(oldSize * mCompatScaling); 2104 float res = nGetTextAdvances(mNativePaint, mNativeTypeface, chars, index, count, 2105 contextIndex, contextCount, isRtl ? BIDI_FORCE_RTL : BIDI_FORCE_LTR, advances, 2106 advancesIndex); 2107 setTextSize(oldSize); 2108 2109 if (advances != null) { 2110 for (int i = advancesIndex, e = i + count; i < e; i++) { 2111 advances[i] *= mInvCompatScaling; 2112 } 2113 } 2114 return res * mInvCompatScaling; // assume errors are not significant 2115 } 2116 2117 /** 2118 * Convenience overload that takes a CharSequence instead of a 2119 * String. 2120 * 2121 * @see #getTextRunAdvances(String, int, int, int, int, boolean, float[], int) 2122 * @hide 2123 */ 2124 public float getTextRunAdvances(CharSequence text, int start, int end, 2125 int contextStart, int contextEnd, boolean isRtl, float[] advances, 2126 int advancesIndex) { 2127 if (text == null) { 2128 throw new IllegalArgumentException("text cannot be null"); 2129 } 2130 if ((start | end | contextStart | contextEnd | advancesIndex | (end - start) 2131 | (start - contextStart) | (contextEnd - end) 2132 | (text.length() - contextEnd) 2133 | (advances == null ? 0 : 2134 (advances.length - advancesIndex - (end - start)))) < 0) { 2135 throw new IndexOutOfBoundsException(); 2136 } 2137 2138 if (text instanceof String) { 2139 return getTextRunAdvances((String) text, start, end, 2140 contextStart, contextEnd, isRtl, advances, advancesIndex); 2141 } 2142 if (text instanceof SpannedString || 2143 text instanceof SpannableString) { 2144 return getTextRunAdvances(text.toString(), start, end, 2145 contextStart, contextEnd, isRtl, advances, advancesIndex); 2146 } 2147 if (text instanceof GraphicsOperations) { 2148 return ((GraphicsOperations) text).getTextRunAdvances(start, end, 2149 contextStart, contextEnd, isRtl, advances, advancesIndex, this); 2150 } 2151 if (text.length() == 0 || end == start) { 2152 return 0f; 2153 } 2154 2155 int contextLen = contextEnd - contextStart; 2156 int len = end - start; 2157 char[] buf = TemporaryBuffer.obtain(contextLen); 2158 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 2159 float result = getTextRunAdvances(buf, start - contextStart, len, 2160 0, contextLen, isRtl, advances, advancesIndex); 2161 TemporaryBuffer.recycle(buf); 2162 return result; 2163 } 2164 2165 /** 2166 * Returns the total advance width for the characters in the run 2167 * between start and end, and if advances is not null, the advance 2168 * assigned to each of these characters (java chars). 2169 * 2170 * <p>The trailing surrogate in a valid surrogate pair is assigned 2171 * an advance of 0. Thus the number of returned advances is 2172 * always equal to count, not to the number of unicode codepoints 2173 * represented by the run. 2174 * 2175 * <p>In the case of conjuncts or combining marks, the total 2176 * advance is assigned to the first logical character, and the 2177 * following characters are assigned an advance of 0. 2178 * 2179 * <p>This generates the sum of the advances of glyphs for 2180 * characters in a reordered cluster as the width of the first 2181 * logical character in the cluster, and 0 for the widths of all 2182 * other characters in the cluster. In effect, such clusters are 2183 * treated like conjuncts. 2184 * 2185 * <p>The shaping bounds limit the amount of context available 2186 * outside start and end that can be used for shaping analysis. 2187 * These bounds typically reflect changes in bidi level or font 2188 * metrics across which shaping does not occur. 2189 * 2190 * @param text the text to measure. Cannot be null. 2191 * @param start the index of the first character to measure 2192 * @param end the index past the last character to measure 2193 * @param contextStart the index of the first character to use for shaping context, 2194 * must be <= start 2195 * @param contextEnd the index past the last character to use for shaping context, 2196 * must be >= end 2197 * @param isRtl whether the run is in RTL direction 2198 * @param advances array to receive the advances, must have room for all advances, 2199 * can be null if only total advance is needed 2200 * @param advancesIndex the position in advances at which to put the 2201 * advance corresponding to the character at start 2202 * @return the total advance 2203 * 2204 * @hide 2205 */ 2206 public float getTextRunAdvances(String text, int start, int end, int contextStart, 2207 int contextEnd, boolean isRtl, float[] advances, int advancesIndex) { 2208 if (text == null) { 2209 throw new IllegalArgumentException("text cannot be null"); 2210 } 2211 if ((start | end | contextStart | contextEnd | advancesIndex | (end - start) 2212 | (start - contextStart) | (contextEnd - end) 2213 | (text.length() - contextEnd) 2214 | (advances == null ? 0 : 2215 (advances.length - advancesIndex - (end - start)))) < 0) { 2216 throw new IndexOutOfBoundsException(); 2217 } 2218 2219 if (text.length() == 0 || start == end) { 2220 return 0f; 2221 } 2222 2223 if (!mHasCompatScaling) { 2224 return nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, end, 2225 contextStart, contextEnd, isRtl ? BIDI_FORCE_RTL : BIDI_FORCE_LTR, advances, 2226 advancesIndex); 2227 } 2228 2229 final float oldSize = getTextSize(); 2230 setTextSize(oldSize * mCompatScaling); 2231 float totalAdvance = nGetTextAdvances(mNativePaint, mNativeTypeface, text, start, 2232 end, contextStart, contextEnd, isRtl ? BIDI_FORCE_RTL : BIDI_FORCE_LTR, advances, 2233 advancesIndex); 2234 setTextSize(oldSize); 2235 2236 if (advances != null) { 2237 for (int i = advancesIndex, e = i + (end - start); i < e; i++) { 2238 advances[i] *= mInvCompatScaling; 2239 } 2240 } 2241 return totalAdvance * mInvCompatScaling; // assume errors are insignificant 2242 } 2243 2244 /** 2245 * Returns the next cursor position in the run. This avoids placing the 2246 * cursor between surrogates, between characters that form conjuncts, 2247 * between base characters and combining marks, or within a reordering 2248 * cluster. 2249 * 2250 * <p>ContextStart and offset are relative to the start of text. 2251 * The context is the shaping context for cursor movement, generally 2252 * the bounds of the metric span enclosing the cursor in the direction of 2253 * movement. 2254 * 2255 * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid 2256 * cursor position, this returns -1. Otherwise this will never return a 2257 * value before contextStart or after contextStart + contextLength. 2258 * 2259 * @param text the text 2260 * @param contextStart the start of the context 2261 * @param contextLength the length of the context 2262 * @param dir either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR} 2263 * @param offset the cursor position to move from 2264 * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER}, 2265 * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE}, 2266 * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT} 2267 * @return the offset of the next position, or -1 2268 * @hide 2269 */ 2270 public int getTextRunCursor(char[] text, int contextStart, int contextLength, 2271 int dir, int offset, int cursorOpt) { 2272 int contextEnd = contextStart + contextLength; 2273 if (((contextStart | contextEnd | offset | (contextEnd - contextStart) 2274 | (offset - contextStart) | (contextEnd - offset) 2275 | (text.length - contextEnd) | cursorOpt) < 0) 2276 || cursorOpt > CURSOR_OPT_MAX_VALUE) { 2277 throw new IndexOutOfBoundsException(); 2278 } 2279 2280 return nGetTextRunCursor(mNativePaint, text, 2281 contextStart, contextLength, dir, offset, cursorOpt); 2282 } 2283 2284 /** 2285 * Returns the next cursor position in the run. This avoids placing the 2286 * cursor between surrogates, between characters that form conjuncts, 2287 * between base characters and combining marks, or within a reordering 2288 * cluster. 2289 * 2290 * <p>ContextStart, contextEnd, and offset are relative to the start of 2291 * text. The context is the shaping context for cursor movement, generally 2292 * the bounds of the metric span enclosing the cursor in the direction of 2293 * movement. 2294 * 2295 * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid 2296 * cursor position, this returns -1. Otherwise this will never return a 2297 * value before contextStart or after contextEnd. 2298 * 2299 * @param text the text 2300 * @param contextStart the start of the context 2301 * @param contextEnd the end of the context 2302 * @param dir either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR} 2303 * @param offset the cursor position to move from 2304 * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER}, 2305 * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE}, 2306 * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT} 2307 * @return the offset of the next position, or -1 2308 * @hide 2309 */ 2310 public int getTextRunCursor(CharSequence text, int contextStart, 2311 int contextEnd, int dir, int offset, int cursorOpt) { 2312 2313 if (text instanceof String || text instanceof SpannedString || 2314 text instanceof SpannableString) { 2315 return getTextRunCursor(text.toString(), contextStart, contextEnd, 2316 dir, offset, cursorOpt); 2317 } 2318 if (text instanceof GraphicsOperations) { 2319 return ((GraphicsOperations) text).getTextRunCursor( 2320 contextStart, contextEnd, dir, offset, cursorOpt, this); 2321 } 2322 2323 int contextLen = contextEnd - contextStart; 2324 char[] buf = TemporaryBuffer.obtain(contextLen); 2325 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 2326 int relPos = getTextRunCursor(buf, 0, contextLen, dir, offset - contextStart, cursorOpt); 2327 TemporaryBuffer.recycle(buf); 2328 return (relPos == -1) ? -1 : relPos + contextStart; 2329 } 2330 2331 /** 2332 * Returns the next cursor position in the run. This avoids placing the 2333 * cursor between surrogates, between characters that form conjuncts, 2334 * between base characters and combining marks, or within a reordering 2335 * cluster. 2336 * 2337 * <p>ContextStart, contextEnd, and offset are relative to the start of 2338 * text. The context is the shaping context for cursor movement, generally 2339 * the bounds of the metric span enclosing the cursor in the direction of 2340 * movement. 2341 * 2342 * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid 2343 * cursor position, this returns -1. Otherwise this will never return a 2344 * value before contextStart or after contextEnd. 2345 * 2346 * @param text the text 2347 * @param contextStart the start of the context 2348 * @param contextEnd the end of the context 2349 * @param dir either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR} 2350 * @param offset the cursor position to move from 2351 * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER}, 2352 * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE}, 2353 * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT} 2354 * @return the offset of the next position, or -1 2355 * @hide 2356 */ 2357 public int getTextRunCursor(String text, int contextStart, int contextEnd, 2358 int dir, int offset, int cursorOpt) { 2359 if (((contextStart | contextEnd | offset | (contextEnd - contextStart) 2360 | (offset - contextStart) | (contextEnd - offset) 2361 | (text.length() - contextEnd) | cursorOpt) < 0) 2362 || cursorOpt > CURSOR_OPT_MAX_VALUE) { 2363 throw new IndexOutOfBoundsException(); 2364 } 2365 2366 return nGetTextRunCursor(mNativePaint, text, 2367 contextStart, contextEnd, dir, offset, cursorOpt); 2368 } 2369 2370 /** 2371 * Return the path (outline) for the specified text. 2372 * Note: just like Canvas.drawText, this will respect the Align setting in 2373 * the paint. 2374 * 2375 * @param text The text to retrieve the path from 2376 * @param index The index of the first character in text 2377 * @param count The number of characterss starting with index 2378 * @param x The x coordinate of the text's origin 2379 * @param y The y coordinate of the text's origin 2380 * @param path The path to receive the data describing the text. Must 2381 * be allocated by the caller. 2382 */ 2383 public void getTextPath(char[] text, int index, int count, 2384 float x, float y, Path path) { 2385 if ((index | count) < 0 || index + count > text.length) { 2386 throw new ArrayIndexOutOfBoundsException(); 2387 } 2388 nGetTextPath(mNativePaint, mNativeTypeface, mBidiFlags, text, index, count, x, y, 2389 path.ni()); 2390 } 2391 2392 /** 2393 * Return the path (outline) for the specified text. 2394 * Note: just like Canvas.drawText, this will respect the Align setting 2395 * in the paint. 2396 * 2397 * @param text The text to retrieve the path from 2398 * @param start The first character in the text 2399 * @param end 1 past the last charcter in the text 2400 * @param x The x coordinate of the text's origin 2401 * @param y The y coordinate of the text's origin 2402 * @param path The path to receive the data describing the text. Must 2403 * be allocated by the caller. 2404 */ 2405 public void getTextPath(String text, int start, int end, 2406 float x, float y, Path path) { 2407 if ((start | end | (end - start) | (text.length() - end)) < 0) { 2408 throw new IndexOutOfBoundsException(); 2409 } 2410 nGetTextPath(mNativePaint, mNativeTypeface, mBidiFlags, text, start, end, x, y, 2411 path.ni()); 2412 } 2413 2414 /** 2415 * Return in bounds (allocated by the caller) the smallest rectangle that 2416 * encloses all of the characters, with an implied origin at (0,0). 2417 * 2418 * @param text String to measure and return its bounds 2419 * @param start Index of the first char in the string to measure 2420 * @param end 1 past the last char in the string measure 2421 * @param bounds Returns the unioned bounds of all the text. Must be 2422 * allocated by the caller. 2423 */ 2424 public void getTextBounds(String text, int start, int end, Rect bounds) { 2425 if ((start | end | (end - start) | (text.length() - end)) < 0) { 2426 throw new IndexOutOfBoundsException(); 2427 } 2428 if (bounds == null) { 2429 throw new NullPointerException("need bounds Rect"); 2430 } 2431 nGetStringBounds(mNativePaint, mNativeTypeface, text, start, end, mBidiFlags, bounds); 2432 } 2433 2434 /** 2435 * Return in bounds (allocated by the caller) the smallest rectangle that 2436 * encloses all of the characters, with an implied origin at (0,0). 2437 * 2438 * @param text Array of chars to measure and return their unioned bounds 2439 * @param index Index of the first char in the array to measure 2440 * @param count The number of chars, beginning at index, to measure 2441 * @param bounds Returns the unioned bounds of all the text. Must be 2442 * allocated by the caller. 2443 */ 2444 public void getTextBounds(char[] text, int index, int count, Rect bounds) { 2445 if ((index | count) < 0 || index + count > text.length) { 2446 throw new ArrayIndexOutOfBoundsException(); 2447 } 2448 if (bounds == null) { 2449 throw new NullPointerException("need bounds Rect"); 2450 } 2451 nGetCharArrayBounds(mNativePaint, mNativeTypeface, text, index, count, mBidiFlags, 2452 bounds); 2453 } 2454 2455 /** 2456 * Determine whether the typeface set on the paint has a glyph supporting the string. The 2457 * simplest case is when the string contains a single character, in which this method 2458 * determines whether the font has the character. In the case of multiple characters, the 2459 * method returns true if there is a single glyph representing the ligature. For example, if 2460 * the input is a pair of regional indicator symbols, determine whether there is an emoji flag 2461 * for the pair. 2462 * 2463 * <p>Finally, if the string contains a variation selector, the method only returns true if 2464 * the fonts contains a glyph specific to that variation. 2465 * 2466 * <p>Checking is done on the entire fallback chain, not just the immediate font referenced. 2467 * 2468 * @param string the string to test whether there is glyph support 2469 * @return true if the typeface has a glyph for the string 2470 */ 2471 public boolean hasGlyph(String string) { 2472 return nHasGlyph(mNativePaint, mNativeTypeface, mBidiFlags, string); 2473 } 2474 2475 /** 2476 * Measure cursor position within a run of text. 2477 * 2478 * <p>The run of text includes the characters from {@code start} to {@code end} in the text. In 2479 * addition, the range {@code contextStart} to {@code contextEnd} is used as context for the 2480 * purpose of complex text shaping, such as Arabic text potentially shaped differently based on 2481 * the text next to it. 2482 * 2483 * <p>All text outside the range {@code contextStart..contextEnd} is ignored. The text between 2484 * {@code start} and {@code end} will be laid out to be measured. 2485 * 2486 * <p>The returned width measurement is the advance from {@code start} to {@code offset}. It is 2487 * generally a positive value, no matter the direction of the run. If {@code offset == end}, 2488 * the return value is simply the width of the whole run from {@code start} to {@code end}. 2489 * 2490 * <p>Ligatures are formed for characters in the range {@code start..end} (but not for 2491 * {@code start..contextStart} or {@code end..contextEnd}). If {@code offset} points to a 2492 * character in the middle of such a formed ligature, but at a grapheme cluster boundary, the 2493 * return value will also reflect an advance in the middle of the ligature. See 2494 * {@link #getOffsetForAdvance} for more discussion of grapheme cluster boundaries. 2495 * 2496 * <p>The direction of the run is explicitly specified by {@code isRtl}. Thus, this method is 2497 * suitable only for runs of a single direction. 2498 * 2499 * <p>All indices are relative to the start of {@code text}. Further, {@code 0 <= contextStart 2500 * <= start <= offset <= end <= contextEnd <= text.length} must hold on entry. 2501 * 2502 * @param text the text to measure. Cannot be null. 2503 * @param start the index of the start of the range to measure 2504 * @param end the index + 1 of the end of the range to measure 2505 * @param contextStart the index of the start of the shaping context 2506 * @param contextEnd the index + 1 of the end of the shaping context 2507 * @param isRtl whether the run is in RTL direction 2508 * @param offset index of caret position 2509 * @return width measurement between start and offset 2510 */ 2511 public float getRunAdvance(char[] text, int start, int end, int contextStart, int contextEnd, 2512 boolean isRtl, int offset) { 2513 if (text == null) { 2514 throw new IllegalArgumentException("text cannot be null"); 2515 } 2516 if ((contextStart | start | offset | end | contextEnd 2517 | start - contextStart | offset - start | end - offset 2518 | contextEnd - end | text.length - contextEnd) < 0) { 2519 throw new IndexOutOfBoundsException(); 2520 } 2521 if (end == start) { 2522 return 0.0f; 2523 } 2524 // TODO: take mCompatScaling into account (or eliminate compat scaling)? 2525 return nGetRunAdvance(mNativePaint, mNativeTypeface, text, start, end, 2526 contextStart, contextEnd, isRtl, offset); 2527 } 2528 2529 /** 2530 * @see #getRunAdvance(char[], int, int, int, int, boolean, int) 2531 * 2532 * @param text the text to measure. Cannot be null. 2533 * @param start the index of the start of the range to measure 2534 * @param end the index + 1 of the end of the range to measure 2535 * @param contextStart the index of the start of the shaping context 2536 * @param contextEnd the index + 1 of the end of the shaping context 2537 * @param isRtl whether the run is in RTL direction 2538 * @param offset index of caret position 2539 * @return width measurement between start and offset 2540 */ 2541 public float getRunAdvance(CharSequence text, int start, int end, int contextStart, 2542 int contextEnd, boolean isRtl, int offset) { 2543 if (text == null) { 2544 throw new IllegalArgumentException("text cannot be null"); 2545 } 2546 if ((contextStart | start | offset | end | contextEnd 2547 | start - contextStart | offset - start | end - offset 2548 | contextEnd - end | text.length() - contextEnd) < 0) { 2549 throw new IndexOutOfBoundsException(); 2550 } 2551 if (end == start) { 2552 return 0.0f; 2553 } 2554 // TODO performance: specialized alternatives to avoid buffer copy, if win is significant 2555 char[] buf = TemporaryBuffer.obtain(contextEnd - contextStart); 2556 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 2557 float result = getRunAdvance(buf, start - contextStart, end - contextStart, 0, 2558 contextEnd - contextStart, isRtl, offset - contextStart); 2559 TemporaryBuffer.recycle(buf); 2560 return result; 2561 } 2562 2563 /** 2564 * Get the character offset within the string whose position is closest to the specified 2565 * horizontal position. 2566 * 2567 * <p>The returned value is generally the value of {@code offset} for which 2568 * {@link #getRunAdvance} yields a result most closely approximating {@code advance}, 2569 * and which is also on a grapheme cluster boundary. As such, it is the preferred method 2570 * for positioning a cursor in response to a touch or pointer event. The grapheme cluster 2571 * boundaries are based on 2572 * <a href="http://unicode.org/reports/tr29/">Unicode Standard Annex #29</a> but with some 2573 * tailoring for better user experience. 2574 * 2575 * <p>Note that {@code advance} is a (generally positive) width measurement relative to the start 2576 * of the run. Thus, for RTL runs it the distance from the point to the right edge. 2577 * 2578 * <p>All indices are relative to the start of {@code text}. Further, {@code 0 <= contextStart 2579 * <= start <= end <= contextEnd <= text.length} must hold on entry, and {@code start <= result 2580 * <= end} will hold on return. 2581 * 2582 * @param text the text to measure. Cannot be null. 2583 * @param start the index of the start of the range to measure 2584 * @param end the index + 1 of the end of the range to measure 2585 * @param contextStart the index of the start of the shaping context 2586 * @param contextEnd the index + 1 of the end of the range to measure 2587 * @param isRtl whether the run is in RTL direction 2588 * @param advance width relative to start of run 2589 * @return index of offset 2590 */ 2591 public int getOffsetForAdvance(char[] text, int start, int end, int contextStart, 2592 int contextEnd, boolean isRtl, float advance) { 2593 if (text == null) { 2594 throw new IllegalArgumentException("text cannot be null"); 2595 } 2596 if ((contextStart | start | end | contextEnd 2597 | start - contextStart | end - start | contextEnd - end 2598 | text.length - contextEnd) < 0) { 2599 throw new IndexOutOfBoundsException(); 2600 } 2601 // TODO: take mCompatScaling into account (or eliminate compat scaling)? 2602 return nGetOffsetForAdvance(mNativePaint, mNativeTypeface, text, start, end, 2603 contextStart, contextEnd, isRtl, advance); 2604 } 2605 2606 /** 2607 * @see #getOffsetForAdvance(char[], int, int, int, int, boolean, float) 2608 * 2609 * @param text the text to measure. Cannot be null. 2610 * @param start the index of the start of the range to measure 2611 * @param end the index + 1 of the end of the range to measure 2612 * @param contextStart the index of the start of the shaping context 2613 * @param contextEnd the index + 1 of the end of the range to measure 2614 * @param isRtl whether the run is in RTL direction 2615 * @param advance width relative to start of run 2616 * @return index of offset 2617 */ 2618 public int getOffsetForAdvance(CharSequence text, int start, int end, int contextStart, 2619 int contextEnd, boolean isRtl, float advance) { 2620 if (text == null) { 2621 throw new IllegalArgumentException("text cannot be null"); 2622 } 2623 if ((contextStart | start | end | contextEnd 2624 | start - contextStart | end - start | contextEnd - end 2625 | text.length() - contextEnd) < 0) { 2626 throw new IndexOutOfBoundsException(); 2627 } 2628 // TODO performance: specialized alternatives to avoid buffer copy, if win is significant 2629 char[] buf = TemporaryBuffer.obtain(contextEnd - contextStart); 2630 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 2631 int result = getOffsetForAdvance(buf, start - contextStart, end - contextStart, 0, 2632 contextEnd - contextStart, isRtl, advance) + contextStart; 2633 TemporaryBuffer.recycle(buf); 2634 return result; 2635 } 2636 2637 private static native long nInit(); 2638 private static native long nInitWithPaint(long paint); 2639 private static native void nReset(long paintPtr); 2640 private static native void nSet(long paintPtrDest, long paintPtrSrc); 2641 private static native int nGetStyle(long paintPtr); 2642 private static native void nSetStyle(long paintPtr, int style); 2643 private static native int nGetStrokeCap(long paintPtr); 2644 private static native void nSetStrokeCap(long paintPtr, int cap); 2645 private static native int nGetStrokeJoin(long paintPtr); 2646 private static native void nSetStrokeJoin(long paintPtr, 2647 int join); 2648 private static native boolean nGetFillPath(long paintPtr, 2649 long src, long dst); 2650 private static native long nSetShader(long paintPtr, long shader); 2651 private static native long nSetColorFilter(long paintPtr, 2652 long filter); 2653 private static native long nSetXfermode(long paintPtr, 2654 long xfermode); 2655 private static native long nSetPathEffect(long paintPtr, 2656 long effect); 2657 private static native long nSetMaskFilter(long paintPtr, 2658 long maskfilter); 2659 private static native long nSetTypeface(long paintPtr, 2660 long typeface); 2661 private static native long nSetRasterizer(long paintPtr, 2662 long rasterizer); 2663 2664 private static native int nGetTextAlign(long paintPtr); 2665 private static native void nSetTextAlign(long paintPtr, 2666 int align); 2667 2668 private static native int nSetTextLocales(long paintPtr, String locales); 2669 private static native void nSetTextLocalesByMinikinLangListId(long paintPtr, 2670 int mMinikinLangListId); 2671 2672 private static native float nGetTextAdvances(long paintPtr, long typefacePtr, 2673 char[] text, int index, int count, int contextIndex, int contextCount, 2674 int bidiFlags, float[] advances, int advancesIndex); 2675 private static native float nGetTextAdvances(long paintPtr, long typefacePtr, 2676 String text, int start, int end, int contextStart, int contextEnd, 2677 int bidiFlags, float[] advances, int advancesIndex); 2678 2679 private native int nGetTextRunCursor(long paintPtr, char[] text, 2680 int contextStart, int contextLength, int dir, int offset, int cursorOpt); 2681 private native int nGetTextRunCursor(long paintPtr, String text, 2682 int contextStart, int contextEnd, int dir, int offset, int cursorOpt); 2683 2684 private static native void nGetTextPath(long paintPtr, long typefacePtr, 2685 int bidiFlags, char[] text, int index, int count, float x, float y, long path); 2686 private static native void nGetTextPath(long paintPtr, long typefacePtr, 2687 int bidiFlags, String text, int start, int end, float x, float y, long path); 2688 private static native void nGetStringBounds(long nativePaint, long typefacePtr, 2689 String text, int start, int end, int bidiFlags, Rect bounds); 2690 private static native void nGetCharArrayBounds(long nativePaint, long typefacePtr, 2691 char[] text, int index, int count, int bidiFlags, Rect bounds); 2692 private static native long nGetNativeFinalizer(); 2693 2694 private static native void nSetShadowLayer(long paintPtr, 2695 float radius, float dx, float dy, int color); 2696 private static native boolean nHasShadowLayer(long paintPtr); 2697 2698 private static native float nGetLetterSpacing(long paintPtr); 2699 private static native void nSetLetterSpacing(long paintPtr, 2700 float letterSpacing); 2701 private static native void nSetFontFeatureSettings(long paintPtr, 2702 String settings); 2703 private static native int nGetHyphenEdit(long paintPtr); 2704 private static native void nSetHyphenEdit(long paintPtr, int hyphen); 2705 private static native boolean nHasGlyph(long paintPtr, long typefacePtr, 2706 int bidiFlags, String string); 2707 private static native float nGetRunAdvance(long paintPtr, long typefacePtr, 2708 char[] text, int start, int end, int contextStart, int contextEnd, boolean isRtl, 2709 int offset); 2710 private static native int nGetOffsetForAdvance(long paintPtr, 2711 long typefacePtr, char[] text, int start, int end, int contextStart, int contextEnd, 2712 boolean isRtl, float advance); 2713} 2714