SkPaint.h revision a2c71cbd51da8b7767d43fe8954e7a4c674b2005
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 17#ifndef SkPaint_DEFINED 18#define SkPaint_DEFINED 19 20#include "SkColor.h" 21#include "SkMath.h" 22#include "SkXfermode.h" 23 24class SkAutoGlyphCache; 25class SkColorFilter; 26class SkDescriptor; 27class SkFlattenableReadBuffer; 28class SkFlattenableWriteBuffer; 29struct SkGlyph; 30struct SkRect; 31class SkGlyphCache; 32class SkMaskFilter; 33class SkMatrix; 34class SkPath; 35class SkPathEffect; 36class SkRasterizer; 37class SkShader; 38class SkDrawLooper; 39class SkTypeface; 40 41typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**, 42 SkFixed x, SkFixed y); 43 44typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**); 45 46/** \class SkPaint 47 48 The SkPaint class holds the style and color information about how to draw 49 geometries, text and bitmaps. 50*/ 51class SkPaint { 52public: 53 SkPaint(); 54 SkPaint(const SkPaint& paint); 55 ~SkPaint(); 56 57 SkPaint& operator=(const SkPaint&); 58 59 friend int operator==(const SkPaint& a, const SkPaint& b); 60 friend int operator!=(const SkPaint& a, const SkPaint& b) 61 { 62 return !(a == b); 63 } 64 65 void flatten(SkFlattenableWriteBuffer&) const; 66 void unflatten(SkFlattenableReadBuffer&); 67 68 /** Restores the paint to its initial settings. 69 */ 70 void reset(); 71 72 /** Specifies the level of hinting to be performed. These names are taken 73 from the Gnome/Cairo names for the same. They are translated into 74 Freetype concepts the same as in cairo-ft-font.c: 75 kNo_Hinting -> FT_LOAD_NO_HINTING 76 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT 77 kNormal_Hinting -> <default, no option> 78 kFull_Hinting -> <same as kNormalHinting, unless we are rendering 79 subpixel glyphs, in which case TARGET_LCD or 80 TARGET_LCD_V is used> 81 */ 82 enum Hinting { 83 kNo_Hinting = 0, 84 kSlight_Hinting = 1, 85 kNormal_Hinting = 2, //!< this is the default 86 kFull_Hinting = 3, 87 }; 88 89 Hinting getHinting() const 90 { 91 return static_cast<Hinting>(fHinting); 92 } 93 94 void setHinting(Hinting hintingLevel) 95 { 96 fHinting = hintingLevel; 97 } 98 99 /** Specifies the bit values that are stored in the paint's flags. 100 */ 101 enum Flags { 102 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing 103 kFilterBitmap_Flag = 0x02, //!< mask to enable bitmap filtering 104 kDither_Flag = 0x04, //!< mask to enable dithering 105 kUnderlineText_Flag = 0x08, //!< mask to enable underline text 106 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text 107 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text 108 kLinearText_Flag = 0x40, //!< mask to enable linear-text 109 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning 110 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text 111 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering 112 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes 113 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter 114 // when adding extra flags, note that the fFlags member is specified 115 // with a bit-width and you'll have to expand it. 116 117 kAllFlags = 0xFFF 118 }; 119 120 /** Return the paint's flags. Use the Flag enum to test flag values. 121 @return the paint's flags (see enums ending in _Flag for bit masks) 122 */ 123 uint32_t getFlags() const { return fFlags; } 124 125 /** Set the paint's flags. Use the Flag enum to specific flag values. 126 @param flags The new flag bits for the paint (see Flags enum) 127 */ 128 void setFlags(uint32_t flags); 129 130 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set 131 @return true if the antialias bit is set in the paint's flags. 132 */ 133 bool isAntiAlias() const 134 { 135 return SkToBool(this->getFlags() & kAntiAlias_Flag); 136 } 137 138 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit 139 @param aa true to enable antialiasing, false to disable it 140 */ 141 void setAntiAlias(bool aa); 142 143 /** Helper for getFlags(), returning true if kDither_Flag bit is set 144 @return true if the dithering bit is set in the paint's flags. 145 */ 146 bool isDither() const 147 { 148 return SkToBool(this->getFlags() & kDither_Flag); 149 } 150 151 /** Helper for setFlags(), setting or clearing the kDither_Flag bit 152 @param dither true to enable dithering, false to disable it 153 */ 154 void setDither(bool dither); 155 156 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set 157 @return true if the lineartext bit is set in the paint's flags 158 */ 159 bool isLinearText() const 160 { 161 return SkToBool(this->getFlags() & kLinearText_Flag); 162 } 163 164 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit 165 @param linearText true to set the linearText bit in the paint's flags, 166 false to clear it. 167 */ 168 void setLinearText(bool linearText); 169 170 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set 171 @return true if the lineartext bit is set in the paint's flags 172 */ 173 bool isSubpixelText() const 174 { 175 return SkToBool(this->getFlags() & kSubpixelText_Flag); 176 } 177 178 /** Helper for setFlags(), setting or clearing the kSubpixelText_Flag 179 bit @param subpixelText true to set the subpixelText bit in the paint's flags, 180 false to clear it. 181 */ 182 void setSubpixelText(bool subpixelText); 183 184 bool isLCDRenderText() const 185 { 186 return SkToBool(this->getFlags() & kLCDRenderText_Flag); 187 } 188 189 /** Helper for setFlags(), setting or clearing the kLCDRenderText_Flag bit 190 @param subpixelRender true to set the subpixelRenderText bit in the paint's flags, 191 false to clear it. 192 */ 193 void setLCDRenderText(bool subpixelRender); 194 195 bool isEmbeddedBitmapText() const 196 { 197 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag); 198 } 199 200 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit 201 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags, 202 false to clear it. 203 */ 204 void setEmbeddedBitmapText(bool useEmbeddedBitmapText); 205 206 bool isAutohinted() const 207 { 208 return SkToBool(this->getFlags() & kAutoHinting_Flag); 209 } 210 211 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit 212 @param useAutohinter true to set the kEmbeddedBitmapText bit in the 213 paint's flags, 214 false to clear it. 215 */ 216 void setAutohinted(bool useAutohinter); 217 218 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set 219 @return true if the underlineText bit is set in the paint's flags. 220 */ 221 bool isUnderlineText() const 222 { 223 return SkToBool(this->getFlags() & kUnderlineText_Flag); 224 } 225 226 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit 227 @param underlineText true to set the underlineText bit in the paint's 228 flags, false to clear it. 229 */ 230 void setUnderlineText(bool underlineText); 231 232 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set 233 @return true if the strikeThruText bit is set in the paint's flags. 234 */ 235 bool isStrikeThruText() const 236 { 237 return SkToBool(this->getFlags() & kStrikeThruText_Flag); 238 } 239 240 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit 241 @param strikeThruText true to set the strikeThruText bit in the 242 paint's flags, false to clear it. 243 */ 244 void setStrikeThruText(bool strikeThruText); 245 246 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set 247 @return true if the kFakeBoldText_Flag bit is set in the paint's flags. 248 */ 249 bool isFakeBoldText() const 250 { 251 return SkToBool(this->getFlags() & kFakeBoldText_Flag); 252 } 253 254 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit 255 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's 256 flags, false to clear it. 257 */ 258 void setFakeBoldText(bool fakeBoldText); 259 260 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set 261 @return true if the kernText bit is set in the paint's flags. 262 */ 263 bool isDevKernText() const 264 { 265 return SkToBool(this->getFlags() & kDevKernText_Flag); 266 } 267 268 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit 269 @param kernText true to set the kKernText_Flag bit in the paint's 270 flags, false to clear it. 271 */ 272 void setDevKernText(bool devKernText); 273 274 bool isFilterBitmap() const 275 { 276 return SkToBool(this->getFlags() & kFilterBitmap_Flag); 277 } 278 279 void setFilterBitmap(bool filterBitmap); 280 281 /** Styles apply to rect, oval, path, and text. 282 Bitmaps are always drawn in "fill", and lines are always drawn in 283 "stroke". 284 285 Note: strokeandfill implicitly draws the result with 286 SkPath::kWinding_FillType, so if the original path is even-odd, the 287 results may not appear the same as if it was drawn twice, filled and 288 then stroked. 289 */ 290 enum Style { 291 kFill_Style, //!< fill the geometry 292 kStroke_Style, //!< stroke the geometry 293 kStrokeAndFill_Style, //!< fill and stroke the geometry 294 295 kStyleCount, 296 }; 297 298 /** Return the paint's style, used for controlling how primitives' 299 geometries are interpreted (except for drawBitmap, which always assumes 300 kFill_Style). 301 @return the paint's Style 302 */ 303 Style getStyle() const { return (Style)fStyle; } 304 305 /** Set the paint's style, used for controlling how primitives' 306 geometries are interpreted (except for drawBitmap, which always assumes 307 Fill). 308 @param style The new style to set in the paint 309 */ 310 void setStyle(Style style); 311 312 /** Return the paint's color. Note that the color is a 32bit value 313 containing alpha as well as r,g,b. This 32bit value is not 314 premultiplied, meaning that its alpha can be any value, regardless of 315 the values of r,g,b. 316 @return the paint's color (and alpha). 317 */ 318 SkColor getColor() const { return fColor; } 319 320 /** Set the paint's color. Note that the color is a 32bit value containing 321 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning 322 that its alpha can be any value, regardless of the values of r,g,b. 323 @param color The new color (including alpha) to set in the paint. 324 */ 325 void setColor(SkColor color); 326 327 /** Helper to getColor() that just returns the color's alpha value. 328 @return the alpha component of the paint's color. 329 */ 330 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); } 331 332 /** Helper to setColor(), that only assigns the color's alpha value, 333 leaving its r,g,b values unchanged. 334 @param a set the alpha component (0..255) of the paint's color. 335 */ 336 void setAlpha(U8CPU a); 337 338 /** Helper to setColor(), that takes a,r,g,b and constructs the color value 339 using SkColorSetARGB() 340 @param a The new alpha component (0..255) of the paint's color. 341 @param r The new red component (0..255) of the paint's color. 342 @param g The new green component (0..255) of the paint's color. 343 @param b The new blue component (0..255) of the paint's color. 344 */ 345 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b); 346 347 /** Return the width for stroking. 348 <p /> 349 A value of 0 strokes in hairline mode. 350 Hairlines always draw 1-pixel wide, regardless of the matrix. 351 @return the paint's stroke width, used whenever the paint's style is 352 Stroke or StrokeAndFill. 353 */ 354 SkScalar getStrokeWidth() const { return fWidth; } 355 356 /** Set the width for stroking. 357 Pass 0 to stroke in hairline mode. 358 Hairlines always draw 1-pixel wide, regardless of the matrix. 359 @param width set the paint's stroke width, used whenever the paint's 360 style is Stroke or StrokeAndFill. 361 */ 362 void setStrokeWidth(SkScalar width); 363 364 /** Return the paint's stroke miter value. This is used to control the 365 behavior of miter joins when the joins angle is sharp. 366 @return the paint's miter limit, used whenever the paint's style is 367 Stroke or StrokeAndFill. 368 */ 369 SkScalar getStrokeMiter() const { return fMiterLimit; } 370 371 /** Set the paint's stroke miter value. This is used to control the 372 behavior of miter joins when the joins angle is sharp. This value must 373 be >= 0. 374 @param miter set the miter limit on the paint, used whenever the 375 paint's style is Stroke or StrokeAndFill. 376 */ 377 void setStrokeMiter(SkScalar miter); 378 379 /** Cap enum specifies the settings for the paint's strokecap. This is the 380 treatment that is applied to the beginning and end of each non-closed 381 contour (e.g. lines). 382 */ 383 enum Cap { 384 kButt_Cap, //!< begin/end contours with no extension 385 kRound_Cap, //!< begin/end contours with a semi-circle extension 386 kSquare_Cap, //!< begin/end contours with a half square extension 387 388 kCapCount, 389 kDefault_Cap = kButt_Cap 390 }; 391 392 /** Join enum specifies the settings for the paint's strokejoin. This is 393 the treatment that is applied to corners in paths and rectangles. 394 */ 395 enum Join { 396 kMiter_Join, //!< connect path segments with a sharp join 397 kRound_Join, //!< connect path segments with a round join 398 kBevel_Join, //!< connect path segments with a flat bevel join 399 400 kJoinCount, 401 kDefault_Join = kMiter_Join 402 }; 403 404 /** Return the paint's stroke cap type, controlling how the start and end 405 of stroked lines and paths are treated. 406 @return the line cap style for the paint, used whenever the paint's 407 style is Stroke or StrokeAndFill. 408 */ 409 Cap getStrokeCap() const { return (Cap)fCapType; } 410 411 /** Set the paint's stroke cap type. 412 @param cap set the paint's line cap style, used whenever the paint's 413 style is Stroke or StrokeAndFill. 414 */ 415 void setStrokeCap(Cap cap); 416 417 /** Return the paint's stroke join type. 418 @return the paint's line join style, used whenever the paint's style is 419 Stroke or StrokeAndFill. 420 */ 421 Join getStrokeJoin() const { return (Join)fJoinType; } 422 423 /** Set the paint's stroke join type. 424 @param join set the paint's line join style, used whenever the paint's 425 style is Stroke or StrokeAndFill. 426 */ 427 void setStrokeJoin(Join join); 428 429 /** Applies any/all effects (patheffect, stroking) to src, returning the 430 result in dst. The result is that drawing src with this paint will be 431 the same as drawing dst with a default paint (at least from the 432 geometric perspective). 433 @param src input path 434 @param dst output path (may be the same as src) 435 @return true if the path should be filled, or false if it should be 436 drawn with a hairline (width == 0) 437 */ 438 bool getFillPath(const SkPath& src, SkPath* dst) const; 439 440 /** Returns true if the current paint settings allow for fast computation of 441 bounds (i.e. there is nothing complex like a patheffect that would make 442 the bounds computation expensive. 443 */ 444 bool canComputeFastBounds() const { 445 // use bit-or since no need for early exit 446 return (reinterpret_cast<uintptr_t>(this->getMaskFilter()) | 447 reinterpret_cast<uintptr_t>(this->getLooper()) | 448 reinterpret_cast<uintptr_t>(this->getRasterizer()) | 449 reinterpret_cast<uintptr_t>(this->getPathEffect())) == 0; 450 } 451 452 /** Only call this if canComputeFastBounds() returned true. This takes a 453 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic 454 effects in the paint (e.g. stroking). If needed, it uses the storage 455 rect parameter. It returns the adjusted bounds that can then be used 456 for quickReject tests. 457 458 The returned rect will either be orig or storage, thus the caller 459 should not rely on storage being set to the result, but should always 460 use the retured value. It is legal for orig and storage to be the same 461 rect. 462 463 e.g. 464 if (paint.canComputeFastBounds()) { 465 SkRect r, storage; 466 path.computeBounds(&r, SkPath::kFast_BoundsType); 467 const SkRect& fastR = paint.computeFastBounds(r, &storage); 468 if (canvas->quickReject(fastR, ...)) { 469 // don't draw the path 470 } 471 } 472 */ 473 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const { 474 return this->getStyle() == kFill_Style ? orig : 475 this->computeStrokeFastBounds(orig, storage); 476 } 477 478 /** Get the paint's shader object. 479 <p /> 480 The shader's reference count is not affected. 481 @return the paint's shader (or NULL) 482 */ 483 SkShader* getShader() const { return fShader; } 484 485 /** Set or clear the shader object. 486 <p /> 487 Pass NULL to clear any previous shader. 488 As a convenience, the parameter passed is also returned. 489 If a previous shader exists, its reference count is decremented. 490 If shader is not NULL, its reference count is incremented. 491 @param shader May be NULL. The shader to be installed in the paint 492 @return shader 493 */ 494 SkShader* setShader(SkShader* shader); 495 496 /** Get the paint's colorfilter. If there is a colorfilter, its reference 497 count is not changed. 498 @return the paint's colorfilter (or NULL) 499 */ 500 SkColorFilter* getColorFilter() const { return fColorFilter; } 501 502 /** Set or clear the paint's colorfilter, returning the parameter. 503 <p /> 504 If the paint already has a filter, its reference count is decremented. 505 If filter is not NULL, its reference count is incremented. 506 @param filter May be NULL. The filter to be installed in the paint 507 @return filter 508 */ 509 SkColorFilter* setColorFilter(SkColorFilter* filter); 510 511 /** Get the paint's xfermode object. 512 <p /> 513 The xfermode's reference count is not affected. 514 @return the paint's xfermode (or NULL) 515 */ 516 SkXfermode* getXfermode() const { return fXfermode; } 517 518 /** Set or clear the xfermode object. 519 <p /> 520 Pass NULL to clear any previous xfermode. 521 As a convenience, the parameter passed is also returned. 522 If a previous xfermode exists, its reference count is decremented. 523 If xfermode is not NULL, its reference count is incremented. 524 @param xfermode May be NULL. The new xfermode to be installed in the 525 paint 526 @return xfermode 527 */ 528 SkXfermode* setXfermode(SkXfermode* xfermode); 529 530 /** Create an xfermode based on the specified Mode, and assign it into the 531 paint, returning the mode that was set. If the Mode is SrcOver, then 532 the paint's xfermode is set to null. 533 */ 534 SkXfermode* setXfermodeMode(SkXfermode::Mode); 535 536 /** Get the paint's patheffect object. 537 <p /> 538 The patheffect reference count is not affected. 539 @return the paint's patheffect (or NULL) 540 */ 541 SkPathEffect* getPathEffect() const { return fPathEffect; } 542 543 /** Set or clear the patheffect object. 544 <p /> 545 Pass NULL to clear any previous patheffect. 546 As a convenience, the parameter passed is also returned. 547 If a previous patheffect exists, its reference count is decremented. 548 If patheffect is not NULL, its reference count is incremented. 549 @param effect May be NULL. The new patheffect to be installed in the 550 paint 551 @return effect 552 */ 553 SkPathEffect* setPathEffect(SkPathEffect* effect); 554 555 /** Get the paint's maskfilter object. 556 <p /> 557 The maskfilter reference count is not affected. 558 @return the paint's maskfilter (or NULL) 559 */ 560 SkMaskFilter* getMaskFilter() const { return fMaskFilter; } 561 562 /** Set or clear the maskfilter object. 563 <p /> 564 Pass NULL to clear any previous maskfilter. 565 As a convenience, the parameter passed is also returned. 566 If a previous maskfilter exists, its reference count is decremented. 567 If maskfilter is not NULL, its reference count is incremented. 568 @param maskfilter May be NULL. The new maskfilter to be installed in 569 the paint 570 @return maskfilter 571 */ 572 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter); 573 574 // These attributes are for text/fonts 575 576 /** Get the paint's typeface object. 577 <p /> 578 The typeface object identifies which font to use when drawing or 579 measuring text. The typeface reference count is not affected. 580 @return the paint's typeface (or NULL) 581 */ 582 SkTypeface* getTypeface() const { return fTypeface; } 583 584 /** Set or clear the typeface object. 585 <p /> 586 Pass NULL to clear any previous typeface. 587 As a convenience, the parameter passed is also returned. 588 If a previous typeface exists, its reference count is decremented. 589 If typeface is not NULL, its reference count is incremented. 590 @param typeface May be NULL. The new typeface to be installed in the 591 paint 592 @return typeface 593 */ 594 SkTypeface* setTypeface(SkTypeface* typeface); 595 596 /** Get the paint's rasterizer (or NULL). 597 <p /> 598 The raster controls how paths/text are turned into alpha masks. 599 @return the paint's rasterizer (or NULL) 600 */ 601 SkRasterizer* getRasterizer() const { return fRasterizer; } 602 603 /** Set or clear the rasterizer object. 604 <p /> 605 Pass NULL to clear any previous rasterizer. 606 As a convenience, the parameter passed is also returned. 607 If a previous rasterizer exists in the paint, its reference count is 608 decremented. If rasterizer is not NULL, its reference count is 609 incremented. 610 @param rasterizer May be NULL. The new rasterizer to be installed in 611 the paint. 612 @return rasterizer 613 */ 614 SkRasterizer* setRasterizer(SkRasterizer* rasterizer); 615 616 SkDrawLooper* getLooper() const { return fLooper; } 617 SkDrawLooper* setLooper(SkDrawLooper*); 618 619 enum Align { 620 kLeft_Align, 621 kCenter_Align, 622 kRight_Align, 623 624 kAlignCount 625 }; 626 /** Return the paint's Align value for drawing text. 627 @return the paint's Align value for drawing text. 628 */ 629 Align getTextAlign() const { return (Align)fTextAlign; } 630 /** Set the paint's text alignment. 631 @param align set the paint's Align value for drawing text. 632 */ 633 void setTextAlign(Align align); 634 635 /** Return the paint's text size. 636 @return the paint's text size. 637 */ 638 SkScalar getTextSize() const { return fTextSize; } 639 640 /** Set the paint's text size. This value must be > 0 641 @param textSize set the paint's text size. 642 */ 643 void setTextSize(SkScalar textSize); 644 645 /** Return the paint's horizontal scale factor for text. The default value 646 is 1.0. 647 @return the paint's scale factor in X for drawing/measuring text 648 */ 649 SkScalar getTextScaleX() const { return fTextScaleX; } 650 651 /** Set the paint's horizontal scale factor for text. The default value 652 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will 653 stretch the text narrower. 654 @param scaleX set the paint's scale factor in X for drawing/measuring 655 text. 656 */ 657 void setTextScaleX(SkScalar scaleX); 658 659 /** Return the paint's horizontal skew factor for text. The default value 660 is 0. 661 @return the paint's skew factor in X for drawing text. 662 */ 663 SkScalar getTextSkewX() const { return fTextSkewX; } 664 665 /** Set the paint's horizontal skew factor for text. The default value 666 is 0. For approximating oblique text, use values around -0.25. 667 @param skewX set the paint's skew factor in X for drawing text. 668 */ 669 void setTextSkewX(SkScalar skewX); 670 671 /** Describes how to interpret the text parameters that are passed to paint 672 methods like measureText() and getTextWidths(). 673 */ 674 enum TextEncoding { 675 kUTF8_TextEncoding, //!< the text parameters are UTF8 676 kUTF16_TextEncoding, //!< the text parameters are UTF16 677 kGlyphID_TextEncoding //!< the text parameters are glyph indices 678 }; 679 680 TextEncoding getTextEncoding() const 681 { 682 return (TextEncoding)fTextEncoding; 683 } 684 685 void setTextEncoding(TextEncoding encoding); 686 687 struct FontMetrics { 688 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0) 689 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0) 690 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0) 691 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0) 692 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0) 693 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0) 694 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs 695 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs 696 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face 697 }; 698 699 /** Return the recommend spacing between lines (which will be 700 fDescent - fAscent + fLeading). 701 If metrics is not null, return in it the font metrics for the 702 typeface/pointsize/etc. currently set in the paint. 703 @param metrics If not null, returns the font metrics for the 704 current typeface/pointsize/etc setting in this 705 paint. 706 @param scale If not 0, return width as if the canvas were scaled 707 by this value 708 @param return the recommended spacing between lines 709 */ 710 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const; 711 712 /** Return the recommend line spacing. This will be 713 fDescent - fAscent + fLeading 714 */ 715 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); } 716 717 /** Convert the specified text into glyph IDs, returning the number of 718 glyphs ID written. If glyphs is NULL, it is ignore and only the count 719 is returned. 720 */ 721 int textToGlyphs(const void* text, size_t byteLength, 722 uint16_t glyphs[]) const; 723 724 /** Return true if all of the specified text has a corresponding non-zero 725 glyph ID. If any of the code-points in the text are not supported in 726 the typeface (i.e. the glyph ID would be zero), then return false. 727 728 If the text encoding for the paint is kGlyph_TextEncoding, then this 729 returns true if all of the specified glyph IDs are non-zero. 730 */ 731 bool containsText(const void* text, size_t byteLength) const; 732 733 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped 734 to zero. Note: this does not look at the text-encoding setting in the 735 paint, only at the typeface. 736 */ 737 void glyphsToUnichars(const uint16_t glyphs[], int count, 738 SkUnichar text[]) const; 739 740 /** Return the number of drawable units in the specified text buffer. 741 This looks at the current TextEncoding field of the paint. If you also 742 want to have the text converted into glyph IDs, call textToGlyphs 743 instead. 744 */ 745 int countText(const void* text, size_t byteLength) const 746 { 747 return this->textToGlyphs(text, byteLength, NULL); 748 } 749 750 /** Return the width of the text. 751 @param text The text to be measured 752 @param length Number of bytes of text to measure 753 @param bounds If not NULL, returns the bounds of the text, 754 relative to (0, 0). 755 @param scale If not 0, return width as if the canvas were scaled 756 by this value 757 @return The advance width of the text 758 */ 759 SkScalar measureText(const void* text, size_t length, 760 SkRect* bounds, SkScalar scale = 0) const; 761 762 /** Return the width of the text. 763 @param text Address of the text 764 @param length Number of bytes of text to measure 765 @return The width of the text 766 */ 767 SkScalar measureText(const void* text, size_t length) const 768 { 769 return this->measureText(text, length, NULL, 0); 770 } 771 772 /** Specify the direction the text buffer should be processed in breakText() 773 */ 774 enum TextBufferDirection { 775 /** When measuring text for breakText(), begin at the start of the text 776 buffer and proceed forward through the data. This is the default. 777 */ 778 kForward_TextBufferDirection, 779 /** When measuring text for breakText(), begin at the end of the text 780 buffer and proceed backwards through the data. 781 */ 782 kBackward_TextBufferDirection 783 }; 784 785 /** Return the width of the text. 786 @param text The text to be measured 787 @param length Number of bytes of text to measure 788 @param maxWidth Maximum width. Only the subset of text whose accumulated 789 widths are <= maxWidth are measured. 790 @param measuredWidth Optional. If non-null, this returns the actual 791 width of the measured text. 792 @param tbd Optional. The direction the text buffer should be 793 traversed during measuring. 794 @return The number of bytes of text that were measured. Will be 795 <= length. 796 */ 797 size_t breakText(const void* text, size_t length, SkScalar maxWidth, 798 SkScalar* measuredWidth = NULL, 799 TextBufferDirection tbd = kForward_TextBufferDirection) 800 const; 801 802 /** Return the advance widths for the characters in the string. 803 @param text the text 804 @param byteLength number of bytes to of text 805 @param widths If not null, returns the array of advance widths of 806 the glyphs. If not NULL, must be at least a large 807 as the number of unichars in the specified text. 808 @param bounds If not null, returns the bounds for each of 809 character, relative to (0, 0) 810 @return the number of unichars in the specified text. 811 */ 812 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[], 813 SkRect bounds[] = NULL) const; 814 815 /** Return the path (outline) for the specified text. 816 Note: just like SkCanvas::drawText, this will respect the Align setting 817 in the paint. 818 */ 819 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y, 820 SkPath* path) const; 821 822private: 823 SkTypeface* fTypeface; 824 SkScalar fTextSize; 825 SkScalar fTextScaleX; 826 SkScalar fTextSkewX; 827 828 SkPathEffect* fPathEffect; 829 SkShader* fShader; 830 SkXfermode* fXfermode; 831 SkMaskFilter* fMaskFilter; 832 SkColorFilter* fColorFilter; 833 SkRasterizer* fRasterizer; 834 SkDrawLooper* fLooper; 835 836 SkColor fColor; 837 SkScalar fWidth; 838 SkScalar fMiterLimit; 839 unsigned fFlags : 12; 840 unsigned fTextAlign : 2; 841 unsigned fCapType : 2; 842 unsigned fJoinType : 2; 843 unsigned fStyle : 2; 844 unsigned fTextEncoding : 2; // 3 values 845 unsigned fHinting : 2; 846 847 SkDrawCacheProc getDrawCacheProc() const; 848 SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir, 849 bool needFullMetrics) const; 850 851 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length, 852 int* count, SkRect* bounds) const; 853 854 SkGlyphCache* detachCache(const SkMatrix*) const; 855 856 void descriptorProc(const SkMatrix* deviceMatrix, 857 void (*proc)(const SkDescriptor*, void*), 858 void* context) const; 859 860 const SkRect& computeStrokeFastBounds(const SkRect& orig, 861 SkRect* storage) const; 862 863 enum { 864 kCanonicalTextSizeForPaths = 64 865 }; 866 friend class SkCanvas; 867 friend class SkDraw; 868 friend class SkAutoGlyphCache; 869 friend class SkTextToPathIter; 870}; 871 872////////////////////////////////////////////////////////////////////////// 873 874#include "SkPathEffect.h" 875 876/** \class SkStrokePathEffect 877 878 SkStrokePathEffect simulates stroking inside a patheffect, allowing the 879 caller to have explicit control of when to stroke a path. Typically this is 880 used if the caller wants to stroke before another patheffect is applied 881 (using SkComposePathEffect or SkSumPathEffect). 882*/ 883class SkStrokePathEffect : public SkPathEffect { 884public: 885 SkStrokePathEffect(const SkPaint&); 886 SkStrokePathEffect(SkScalar width, SkPaint::Style, SkPaint::Join, 887 SkPaint::Cap, SkScalar miterLimit = -1); 888 889 // overrides 890 // This method is not exported to java. 891 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width); 892 893 // overrides for SkFlattenable 894 // This method is not exported to java. 895 virtual void flatten(SkFlattenableWriteBuffer&); 896 // This method is not exported to java. 897 virtual Factory getFactory(); 898 899private: 900 SkScalar fWidth, fMiter; 901 uint8_t fStyle, fJoin, fCap; 902 903 static SkFlattenable* CreateProc(SkFlattenableReadBuffer&); 904 SkStrokePathEffect(SkFlattenableReadBuffer&); 905 906 typedef SkPathEffect INHERITED; 907 908 // illegal 909 SkStrokePathEffect(const SkStrokePathEffect&); 910 SkStrokePathEffect& operator=(const SkStrokePathEffect&); 911}; 912 913#endif 914 915