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