Paint.java revision 8a9f9d880da83877206d0a52d03c784e936d3814
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 are measured in reverse order. 1258 * @param maxWidth The maximum width to accumulate. 1259 * @param measuredWidth Optional. If not null, returns the actual width 1260 * measured. 1261 * @return The number of chars that were measured. Will always be <= 1262 * abs(count). 1263 */ 1264 public int breakText(char[] text, int index, int count, 1265 float maxWidth, float[] measuredWidth) { 1266 if (!mHasCompatScaling) { 1267 return native_breakText(text, index, count, maxWidth, measuredWidth); 1268 } 1269 final float oldSize = getTextSize(); 1270 setTextSize(oldSize*mCompatScaling); 1271 int res = native_breakText(text, index, count, maxWidth*mCompatScaling, 1272 measuredWidth); 1273 setTextSize(oldSize); 1274 if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling; 1275 return res; 1276 } 1277 1278 private native int native_breakText(char[] text, int index, int count, 1279 float maxWidth, float[] measuredWidth); 1280 1281 /** 1282 * Measure the text, stopping early if the measured width exceeds maxWidth. 1283 * Return the number of chars that were measured, and if measuredWidth is 1284 * not null, return in it the actual width measured. 1285 * 1286 * @param text The text to measure 1287 * @param start The offset into text to begin measuring at 1288 * @param end The end of the text slice to measure. 1289 * @param measureForwards If true, measure forwards, starting at start. 1290 * Otherwise, measure backwards, starting with end. 1291 * @param maxWidth The maximum width to accumulate. 1292 * @param measuredWidth Optional. If not null, returns the actual width 1293 * measured. 1294 * @return The number of chars that were measured. Will always be <= 1295 * abs(end - start). 1296 */ 1297 public int breakText(CharSequence text, int start, int end, 1298 boolean measureForwards, 1299 float maxWidth, float[] measuredWidth) { 1300 if (start == 0 && text instanceof String && end == text.length()) { 1301 return breakText((String) text, measureForwards, maxWidth, 1302 measuredWidth); 1303 } 1304 1305 char[] buf = TemporaryBuffer.obtain(end - start); 1306 int result; 1307 1308 TextUtils.getChars(text, start, end, buf, 0); 1309 1310 if (measureForwards) { 1311 result = breakText(buf, 0, end - start, maxWidth, measuredWidth); 1312 } else { 1313 result = breakText(buf, 0, -(end - start), maxWidth, measuredWidth); 1314 } 1315 1316 TemporaryBuffer.recycle(buf); 1317 return result; 1318 } 1319 1320 /** 1321 * Measure the text, stopping early if the measured width exceeds maxWidth. 1322 * Return the number of chars that were measured, and if measuredWidth is 1323 * not null, return in it the actual width measured. 1324 * 1325 * @param text The text to measure 1326 * @param measureForwards If true, measure forwards, starting with the 1327 * first character in the string. Otherwise, 1328 * measure backwards, starting with the 1329 * last character in the string. 1330 * @param maxWidth The maximum width to accumulate. 1331 * @param measuredWidth Optional. If not null, returns the actual width 1332 * measured. 1333 * @return The number of chars that were measured. Will always be <= 1334 * abs(count). 1335 */ 1336 public int breakText(String text, boolean measureForwards, 1337 float maxWidth, float[] measuredWidth) { 1338 if (!mHasCompatScaling) { 1339 return native_breakText(text, measureForwards, maxWidth, measuredWidth); 1340 } 1341 final float oldSize = getTextSize(); 1342 setTextSize(oldSize*mCompatScaling); 1343 int res = native_breakText(text, measureForwards, maxWidth*mCompatScaling, 1344 measuredWidth); 1345 setTextSize(oldSize); 1346 if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling; 1347 return res; 1348 } 1349 1350 private native int native_breakText(String text, boolean measureForwards, 1351 float maxWidth, float[] measuredWidth); 1352 1353 /** 1354 * Return the advance widths for the characters in the string. 1355 * 1356 * @param text The text to measure 1357 * @param index The index of the first char to to measure 1358 * @param count The number of chars starting with index to measure 1359 * @param widths array to receive the advance widths of the characters. 1360 * Must be at least a large as count. 1361 * @return the actual number of widths returned. 1362 */ 1363 public int getTextWidths(char[] text, int index, int count, 1364 float[] widths) { 1365 if ((index | count) < 0 || index + count > text.length 1366 || count > widths.length) { 1367 throw new ArrayIndexOutOfBoundsException(); 1368 } 1369 1370 if (!mHasCompatScaling) { 1371 return native_getTextWidths(mNativePaint, text, index, count, widths); 1372 } 1373 final float oldSize = getTextSize(); 1374 setTextSize(oldSize*mCompatScaling); 1375 int res = native_getTextWidths(mNativePaint, text, index, count, widths); 1376 setTextSize(oldSize); 1377 for (int i=0; i<res; i++) { 1378 widths[i] *= mInvCompatScaling; 1379 } 1380 return res; 1381 } 1382 1383 /** 1384 * Return the advance widths for the characters in the string. 1385 * 1386 * @param text The text to measure 1387 * @param start The index of the first char to to measure 1388 * @param end The end of the text slice to measure 1389 * @param widths array to receive the advance widths of the characters. 1390 * Must be at least a large as (end - start). 1391 * @return the actual number of widths returned. 1392 */ 1393 public int getTextWidths(CharSequence text, int start, int end, 1394 float[] widths) { 1395 if (text instanceof String) { 1396 return getTextWidths((String) text, start, end, widths); 1397 } 1398 if (text instanceof SpannedString || 1399 text instanceof SpannableString) { 1400 return getTextWidths(text.toString(), start, end, widths); 1401 } 1402 if (text instanceof GraphicsOperations) { 1403 return ((GraphicsOperations) text).getTextWidths(start, end, 1404 widths, this); 1405 } 1406 1407 char[] buf = TemporaryBuffer.obtain(end - start); 1408 TextUtils.getChars(text, start, end, buf, 0); 1409 int result = getTextWidths(buf, 0, end - start, widths); 1410 TemporaryBuffer.recycle(buf); 1411 return result; 1412 } 1413 1414 /** 1415 * Return the advance widths for the characters in the string. 1416 * 1417 * @param text The text to measure 1418 * @param start The index of the first char to to measure 1419 * @param end The end of the text slice to measure 1420 * @param widths array to receive the advance widths of the characters. 1421 * Must be at least a large as the text. 1422 * @return the number of unichars in the specified text. 1423 */ 1424 public int getTextWidths(String text, int start, int end, float[] widths) { 1425 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1426 throw new IndexOutOfBoundsException(); 1427 } 1428 if (end - start > widths.length) { 1429 throw new ArrayIndexOutOfBoundsException(); 1430 } 1431 1432 if (!mHasCompatScaling) { 1433 return native_getTextWidths(mNativePaint, text, start, end, widths); 1434 } 1435 final float oldSize = getTextSize(); 1436 setTextSize(oldSize*mCompatScaling); 1437 int res = native_getTextWidths(mNativePaint, text, start, end, widths); 1438 setTextSize(oldSize); 1439 for (int i=0; i<res; i++) { 1440 widths[i] *= mInvCompatScaling; 1441 } 1442 return res; 1443 } 1444 1445 /** 1446 * Return the advance widths for the characters in the string. 1447 * 1448 * @param text The text to measure 1449 * @param widths array to receive the advance widths of the characters. 1450 * Must be at least a large as the text. 1451 * @return the number of unichars in the specified text. 1452 */ 1453 public int getTextWidths(String text, float[] widths) { 1454 return getTextWidths(text, 0, text.length(), widths); 1455 } 1456 1457 /** 1458 * Convenience overload that takes a char array instead of a 1459 * String. 1460 * 1461 * @see #getTextRunAdvances(String, int, int, int, int, int, float[], int) 1462 * @hide 1463 */ 1464 public float getTextRunAdvances(char[] chars, int index, int count, 1465 int contextIndex, int contextCount, int flags, float[] advances, 1466 int advancesIndex) { 1467 1468 if ((index | count | contextIndex | contextCount | advancesIndex 1469 | (index - contextIndex) 1470 | ((contextIndex + contextCount) - (index + count)) 1471 | (chars.length - (contextIndex + contextCount)) 1472 | (advances == null ? 0 : 1473 (advances.length - (advancesIndex + count)))) < 0) { 1474 throw new IndexOutOfBoundsException(); 1475 } 1476 if (flags != DIRECTION_LTR && flags != DIRECTION_RTL) { 1477 throw new IllegalArgumentException("unknown flags value: " + flags); 1478 } 1479 1480 if (!mHasCompatScaling) { 1481 return native_getTextRunAdvances(mNativePaint, chars, index, count, 1482 contextIndex, contextCount, flags, advances, advancesIndex); 1483 } 1484 1485 final float oldSize = getTextSize(); 1486 setTextSize(oldSize * mCompatScaling); 1487 float res = native_getTextRunAdvances(mNativePaint, chars, index, count, 1488 contextIndex, contextCount, flags, advances, advancesIndex); 1489 setTextSize(oldSize); 1490 1491 if (advances != null) { 1492 for (int i = advancesIndex, e = i + count; i < e; i++) { 1493 advances[i] *= mInvCompatScaling; 1494 } 1495 } 1496 return res * mInvCompatScaling; // assume errors are not significant 1497 } 1498 1499 /** 1500 * Convenience overload that takes a CharSequence instead of a 1501 * String. 1502 * 1503 * @see #getTextRunAdvances(String, int, int, int, int, int, float[], int) 1504 * @hide 1505 */ 1506 public float getTextRunAdvances(CharSequence text, int start, int end, 1507 int contextStart, int contextEnd, int flags, float[] advances, 1508 int advancesIndex) { 1509 1510 if (text instanceof String) { 1511 return getTextRunAdvances((String) text, start, end, 1512 contextStart, contextEnd, flags, advances, advancesIndex); 1513 } 1514 if (text instanceof SpannedString || 1515 text instanceof SpannableString) { 1516 return getTextRunAdvances(text.toString(), start, end, 1517 contextStart, contextEnd, flags, advances, advancesIndex); 1518 } 1519 if (text instanceof GraphicsOperations) { 1520 return ((GraphicsOperations) text).getTextRunAdvances(start, end, 1521 contextStart, contextEnd, flags, advances, advancesIndex, this); 1522 } 1523 1524 int contextLen = contextEnd - contextStart; 1525 int len = end - start; 1526 char[] buf = TemporaryBuffer.obtain(contextLen); 1527 TextUtils.getChars(text, start, end, buf, 0); 1528 float result = getTextRunAdvances(buf, start - contextStart, len, 1529 0, contextLen, flags, advances, advancesIndex); 1530 TemporaryBuffer.recycle(buf); 1531 return result; 1532 } 1533 1534 /** 1535 * Returns the total advance width for the characters in the run 1536 * between start and end, and if advances is not null, the advance 1537 * assigned to each of these characters (java chars). 1538 * 1539 * <p>The trailing surrogate in a valid surrogate pair is assigned 1540 * an advance of 0. Thus the number of returned advances is 1541 * always equal to count, not to the number of unicode codepoints 1542 * represented by the run. 1543 * 1544 * <p>In the case of conjuncts or combining marks, the total 1545 * advance is assigned to the first logical character, and the 1546 * following characters are assigned an advance of 0. 1547 * 1548 * <p>This generates the sum of the advances of glyphs for 1549 * characters in a reordered cluster as the width of the first 1550 * logical character in the cluster, and 0 for the widths of all 1551 * other characters in the cluster. In effect, such clusters are 1552 * treated like conjuncts. 1553 * 1554 * <p>The shaping bounds limit the amount of context available 1555 * outside start and end that can be used for shaping analysis. 1556 * These bounds typically reflect changes in bidi level or font 1557 * metrics across which shaping does not occur. 1558 * 1559 * @param text the text to measure 1560 * @param start the index of the first character to measure 1561 * @param end the index past the last character to measure 1562 * @param contextStart the index of the first character to use for shaping context, 1563 * must be <= start 1564 * @param contextEnd the index past the last character to use for shaping context, 1565 * must be >= end 1566 * @param flags the flags to control the advances, either {@link #DIRECTION_LTR} 1567 * or {@link #DIRECTION_RTL} 1568 * @param advances array to receive the advances, must have room for all advances, 1569 * can be null if only total advance is needed 1570 * @param advancesIndex the position in advances at which to put the 1571 * advance corresponding to the character at start 1572 * @return the total advance 1573 * 1574 * @hide 1575 */ 1576 public float getTextRunAdvances(String text, int start, int end, int contextStart, 1577 int contextEnd, int flags, float[] advances, int advancesIndex) { 1578 1579 if ((start | end | contextStart | contextEnd | advancesIndex | (end - start) 1580 | (start - contextStart) | (contextEnd - end) 1581 | (text.length() - contextEnd) 1582 | (advances == null ? 0 : 1583 (advances.length - advancesIndex - (end - start)))) < 0) { 1584 throw new IndexOutOfBoundsException(); 1585 } 1586 if (flags != DIRECTION_LTR && flags != DIRECTION_RTL) { 1587 throw new IllegalArgumentException("unknown flags value: " + flags); 1588 } 1589 1590 if (!mHasCompatScaling) { 1591 return native_getTextRunAdvances(mNativePaint, text, start, end, 1592 contextStart, contextEnd, flags, advances, advancesIndex); 1593 } 1594 1595 final float oldSize = getTextSize(); 1596 setTextSize(oldSize * mCompatScaling); 1597 float totalAdvance = native_getTextRunAdvances(mNativePaint, text, start, end, 1598 contextStart, contextEnd, flags, advances, advancesIndex); 1599 setTextSize(oldSize); 1600 1601 if (advances != null) { 1602 for (int i = advancesIndex, e = i + (end - start); i < e; i++) { 1603 advances[i] *= mInvCompatScaling; 1604 } 1605 } 1606 return totalAdvance * mInvCompatScaling; // assume errors are insignificant 1607 } 1608 1609 /** 1610 * Returns the next cursor position in the run. This avoids placing the 1611 * cursor between surrogates, between characters that form conjuncts, 1612 * between base characters and combining marks, or within a reordering 1613 * cluster. 1614 * 1615 * <p>ContextStart and offset are relative to the start of text. 1616 * The context is the shaping context for cursor movement, generally 1617 * the bounds of the metric span enclosing the cursor in the direction of 1618 * movement. 1619 * 1620 * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid 1621 * cursor position, this returns -1. Otherwise this will never return a 1622 * value before contextStart or after contextStart + contextLength. 1623 * 1624 * @param text the text 1625 * @param contextStart the start of the context 1626 * @param contextLength the length of the context 1627 * @param flags either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR} 1628 * @param offset the cursor position to move from 1629 * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER}, 1630 * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE}, 1631 * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT} 1632 * @return the offset of the next position, or -1 1633 * @hide 1634 */ 1635 public int getTextRunCursor(char[] text, int contextStart, int contextLength, 1636 int flags, int offset, int cursorOpt) { 1637 int contextEnd = contextStart + contextLength; 1638 if (((contextStart | contextEnd | offset | (contextEnd - contextStart) 1639 | (offset - contextStart) | (contextEnd - offset) 1640 | (text.length - contextEnd) | cursorOpt) < 0) 1641 || cursorOpt > CURSOR_OPT_MAX_VALUE) { 1642 throw new IndexOutOfBoundsException(); 1643 } 1644 1645 return native_getTextRunCursor(mNativePaint, text, 1646 contextStart, contextLength, flags, offset, cursorOpt); 1647 } 1648 1649 /** 1650 * Returns the next cursor position in the run. This avoids placing the 1651 * cursor between surrogates, between characters that form conjuncts, 1652 * between base characters and combining marks, or within a reordering 1653 * cluster. 1654 * 1655 * <p>ContextStart, contextEnd, and offset are relative to the start of 1656 * text. The context is the shaping context for cursor movement, generally 1657 * the bounds of the metric span enclosing the cursor in the direction of 1658 * movement. 1659 * 1660 * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid 1661 * cursor position, this returns -1. Otherwise this will never return a 1662 * value before contextStart or after contextEnd. 1663 * 1664 * @param text the text 1665 * @param contextStart the start of the context 1666 * @param contextEnd the end of the context 1667 * @param flags either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR} 1668 * @param offset the cursor position to move from 1669 * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER}, 1670 * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE}, 1671 * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT} 1672 * @return the offset of the next position, or -1 1673 * @hide 1674 */ 1675 public int getTextRunCursor(CharSequence text, int contextStart, 1676 int contextEnd, int flags, int offset, int cursorOpt) { 1677 1678 if (text instanceof String || text instanceof SpannedString || 1679 text instanceof SpannableString) { 1680 return getTextRunCursor(text.toString(), contextStart, contextEnd, 1681 flags, offset, cursorOpt); 1682 } 1683 if (text instanceof GraphicsOperations) { 1684 return ((GraphicsOperations) text).getTextRunCursor( 1685 contextStart, contextEnd, flags, offset, cursorOpt, this); 1686 } 1687 1688 int contextLen = contextEnd - contextStart; 1689 char[] buf = TemporaryBuffer.obtain(contextLen); 1690 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 1691 int result = getTextRunCursor(buf, 0, contextLen, flags, offset - contextStart, cursorOpt); 1692 TemporaryBuffer.recycle(buf); 1693 return result; 1694 } 1695 1696 /** 1697 * Returns the next cursor position in the run. This avoids placing the 1698 * cursor between surrogates, between characters that form conjuncts, 1699 * between base characters and combining marks, or within a reordering 1700 * cluster. 1701 * 1702 * <p>ContextStart, contextEnd, and offset are relative to the start of 1703 * text. The context is the shaping context for cursor movement, generally 1704 * the bounds of the metric span enclosing the cursor in the direction of 1705 * movement. 1706 * 1707 * <p>If cursorOpt is {@link #CURSOR_AT} and the offset is not a valid 1708 * cursor position, this returns -1. Otherwise this will never return a 1709 * value before contextStart or after contextEnd. 1710 * 1711 * @param text the text 1712 * @param contextStart the start of the context 1713 * @param contextEnd the end of the context 1714 * @param flags either {@link #DIRECTION_RTL} or {@link #DIRECTION_LTR} 1715 * @param offset the cursor position to move from 1716 * @param cursorOpt how to move the cursor, one of {@link #CURSOR_AFTER}, 1717 * {@link #CURSOR_AT_OR_AFTER}, {@link #CURSOR_BEFORE}, 1718 * {@link #CURSOR_AT_OR_BEFORE}, or {@link #CURSOR_AT} 1719 * @return the offset of the next position, or -1 1720 * @hide 1721 */ 1722 public int getTextRunCursor(String text, int contextStart, int contextEnd, 1723 int flags, int offset, int cursorOpt) { 1724 if (((contextStart | contextEnd | offset | (contextEnd - contextStart) 1725 | (offset - contextStart) | (contextEnd - offset) 1726 | (text.length() - contextEnd) | cursorOpt) < 0) 1727 || cursorOpt > CURSOR_OPT_MAX_VALUE) { 1728 throw new IndexOutOfBoundsException(); 1729 } 1730 1731 return native_getTextRunCursor(mNativePaint, text, 1732 contextStart, contextEnd, flags, offset, cursorOpt); 1733 } 1734 1735 /** 1736 * Return the path (outline) for the specified text. 1737 * Note: just like Canvas.drawText, this will respect the Align setting in 1738 * the paint. 1739 * 1740 * @param text The text to retrieve the path from 1741 * @param index The index of the first character in text 1742 * @param count The number of characterss starting with index 1743 * @param x The x coordinate of the text's origin 1744 * @param y The y coordinate of the text's origin 1745 * @param path The path to receive the data describing the text. Must 1746 * be allocated by the caller. 1747 */ 1748 public void getTextPath(char[] text, int index, int count, 1749 float x, float y, Path path) { 1750 if ((index | count) < 0 || index + count > text.length) { 1751 throw new ArrayIndexOutOfBoundsException(); 1752 } 1753 native_getTextPath(mNativePaint, mBidiFlags, text, index, count, x, y, 1754 path.ni()); 1755 } 1756 1757 /** 1758 * Return the path (outline) for the specified text. 1759 * Note: just like Canvas.drawText, this will respect the Align setting 1760 * in the paint. 1761 * 1762 * @param text The text to retrieve the path from 1763 * @param start The first character in the text 1764 * @param end 1 past the last charcter in the text 1765 * @param x The x coordinate of the text's origin 1766 * @param y The y coordinate of the text's origin 1767 * @param path The path to receive the data describing the text. Must 1768 * be allocated by the caller. 1769 */ 1770 public void getTextPath(String text, int start, int end, 1771 float x, float y, Path path) { 1772 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1773 throw new IndexOutOfBoundsException(); 1774 } 1775 native_getTextPath(mNativePaint, mBidiFlags, text, start, end, x, y, 1776 path.ni()); 1777 } 1778 1779 /** 1780 * Return in bounds (allocated by the caller) the smallest rectangle that 1781 * encloses all of the characters, with an implied origin at (0,0). 1782 * 1783 * @param text String to measure and return its bounds 1784 * @param start Index of the first char in the string to measure 1785 * @param end 1 past the last char in the string measure 1786 * @param bounds Returns the unioned bounds of all the text. Must be 1787 * allocated by the caller. 1788 */ 1789 public void getTextBounds(String text, int start, int end, Rect bounds) { 1790 if ((start | end | (end - start) | (text.length() - end)) < 0) { 1791 throw new IndexOutOfBoundsException(); 1792 } 1793 if (bounds == null) { 1794 throw new NullPointerException("need bounds Rect"); 1795 } 1796 nativeGetStringBounds(mNativePaint, text, start, end, bounds); 1797 } 1798 1799 /** 1800 * Return in bounds (allocated by the caller) the smallest rectangle that 1801 * encloses all of the characters, with an implied origin at (0,0). 1802 * 1803 * @param text Array of chars to measure and return their unioned bounds 1804 * @param index Index of the first char in the array to measure 1805 * @param count The number of chars, beginning at index, to measure 1806 * @param bounds Returns the unioned bounds of all the text. Must be 1807 * allocated by the caller. 1808 */ 1809 public void getTextBounds(char[] text, int index, int count, Rect bounds) { 1810 if ((index | count) < 0 || index + count > text.length) { 1811 throw new ArrayIndexOutOfBoundsException(); 1812 } 1813 if (bounds == null) { 1814 throw new NullPointerException("need bounds Rect"); 1815 } 1816 nativeGetCharArrayBounds(mNativePaint, text, index, count, bounds); 1817 } 1818 1819 @Override 1820 protected void finalize() throws Throwable { 1821 finalizer(mNativePaint); 1822 } 1823 1824 private static native int native_init(); 1825 private static native int native_initWithPaint(int paint); 1826 private static native void native_reset(int native_object); 1827 private static native void native_set(int native_dst, int native_src); 1828 private static native int native_getStyle(int native_object); 1829 private static native void native_setStyle(int native_object, int style); 1830 private static native int native_getStrokeCap(int native_object); 1831 private static native void native_setStrokeCap(int native_object, int cap); 1832 private static native int native_getStrokeJoin(int native_object); 1833 private static native void native_setStrokeJoin(int native_object, 1834 int join); 1835 private static native boolean native_getFillPath(int native_object, 1836 int src, int dst); 1837 private static native int native_setShader(int native_object, int shader); 1838 private static native int native_setColorFilter(int native_object, 1839 int filter); 1840 private static native int native_setXfermode(int native_object, 1841 int xfermode); 1842 private static native int native_setPathEffect(int native_object, 1843 int effect); 1844 private static native int native_setMaskFilter(int native_object, 1845 int maskfilter); 1846 private static native int native_setTypeface(int native_object, 1847 int typeface); 1848 private static native int native_setRasterizer(int native_object, 1849 int rasterizer); 1850 1851 private static native int native_getTextAlign(int native_object); 1852 private static native void native_setTextAlign(int native_object, 1853 int align); 1854 1855 private static native float native_getFontMetrics(int native_paint, 1856 FontMetrics metrics); 1857 private static native int native_getTextWidths(int native_object, 1858 char[] text, int index, int count, float[] widths); 1859 private static native int native_getTextWidths(int native_object, 1860 String text, int start, int end, float[] widths); 1861 1862 private static native float native_getTextRunAdvances(int native_object, 1863 char[] text, int index, int count, int contextIndex, int contextCount, 1864 int flags, float[] advances, int advancesIndex); 1865 private static native float native_getTextRunAdvances(int native_object, 1866 String text, int start, int end, int contextStart, int contextEnd, 1867 int flags, float[] advances, int advancesIndex); 1868 1869 private native int native_getTextRunCursor(int native_object, char[] text, 1870 int contextStart, int contextLength, int flags, int offset, int cursorOpt); 1871 private native int native_getTextRunCursor(int native_object, String text, 1872 int contextStart, int contextEnd, int flags, int offset, int cursorOpt); 1873 1874 private static native void native_getTextPath(int native_object, int bidiFlags, 1875 char[] text, int index, int count, float x, float y, int path); 1876 private static native void native_getTextPath(int native_object, int bidiFlags, 1877 String text, int start, int end, float x, float y, int path); 1878 private static native void nativeGetStringBounds(int nativePaint, 1879 String text, int start, int end, Rect bounds); 1880 private static native void nativeGetCharArrayBounds(int nativePaint, 1881 char[] text, int index, int count, Rect bounds); 1882 private static native void finalizer(int nativePaint); 1883} 1884