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