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