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