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