1 /* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkPaint_DEFINED 9 #define SkPaint_DEFINED 10 11 #include "SkColor.h" 12 #include "SkFilterQuality.h" 13 #include "SkMatrix.h" 14 #include "SkXfermode.h" 15 16 class SkAnnotation; 17 class SkAutoDescriptor; 18 class SkAutoGlyphCache; 19 class SkColorFilter; 20 class SkData; 21 class SkDescriptor; 22 class SkDrawLooper; 23 class SkReadBuffer; 24 class SkWriteBuffer; 25 class SkGlyph; 26 struct SkRect; 27 class SkGlyphCache; 28 class SkImageFilter; 29 class SkMaskFilter; 30 class SkPath; 31 class SkPathEffect; 32 struct SkPoint; 33 class SkRasterizer; 34 class SkShader; 35 class SkSurfaceProps; 36 class SkTypeface; 37 38 #define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag 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 46 class SK_API SkPaint { 47 public: 48 SkPaint(); 49 SkPaint(const SkPaint& paint); 50 SkPaint(SkPaint&& paint); 51 ~SkPaint(); 52 53 SkPaint& operator=(const SkPaint&); 54 SkPaint& operator=(SkPaint&&); 55 56 /** operator== may give false negatives: two paints that draw equivalently 57 may return false. It will never give false positives: two paints that 58 are not equivalent always return false. 59 */ 60 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b); 61 friend bool operator!=(const SkPaint& a, const SkPaint& b) { 62 return !(a == b); 63 } 64 65 /** getHash() is a shallow hash, with the same limitations as operator==. 66 * If operator== returns true for two paints, getHash() returns the same value for each. 67 */ 68 uint32_t getHash() const; 69 70 void flatten(SkWriteBuffer&) const; 71 void unflatten(SkReadBuffer&); 72 73 /** Restores the paint to its initial settings. 74 */ 75 void reset(); 76 77 /** Specifies the level of hinting to be performed. These names are taken 78 from the Gnome/Cairo names for the same. They are translated into 79 Freetype concepts the same as in cairo-ft-font.c: 80 kNo_Hinting -> FT_LOAD_NO_HINTING 81 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT 82 kNormal_Hinting -> <default, no option> 83 kFull_Hinting -> <same as kNormalHinting, unless we are rendering 84 subpixel glyphs, in which case TARGET_LCD or 85 TARGET_LCD_V is used> 86 */ 87 enum Hinting { 88 kNo_Hinting = 0, 89 kSlight_Hinting = 1, 90 kNormal_Hinting = 2, //!< this is the default 91 kFull_Hinting = 3 92 }; 93 94 Hinting getHinting() const { 95 return static_cast<Hinting>(fBitfields.fHinting); 96 } 97 98 void setHinting(Hinting hintingLevel); 99 100 /** Specifies the bit values that are stored in the paint's flags. 101 */ 102 enum Flags { 103 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing 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 kVerticalText_Flag = 0x1000, 115 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it 116 // when adding extra flags, note that the fFlags member is specified 117 // with a bit-width and you'll have to expand it. 118 119 kAllFlags = 0xFFFF 120 }; 121 122 /** Return the paint's flags. Use the Flag enum to test flag values. 123 @return the paint's flags (see enums ending in _Flag for bit masks) 124 */ 125 uint32_t getFlags() const { return fBitfields.fFlags; } 126 127 /** Set the paint's flags. Use the Flag enum to specific flag values. 128 @param flags The new flag bits for the paint (see Flags enum) 129 */ 130 void setFlags(uint32_t flags); 131 132 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set 133 @return true if the antialias bit is set in the paint's flags. 134 */ 135 bool isAntiAlias() const { 136 return SkToBool(this->getFlags() & kAntiAlias_Flag); 137 } 138 139 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit 140 @param aa true to enable antialiasing, false to disable it 141 */ 142 void setAntiAlias(bool aa); 143 144 /** Helper for getFlags(), returning true if kDither_Flag bit is set 145 @return true if the dithering bit is set in the paint's flags. 146 */ 147 bool isDither() const { 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 return SkToBool(this->getFlags() & kLinearText_Flag); 161 } 162 163 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit 164 @param linearText true to set the linearText bit in the paint's flags, 165 false to clear it. 166 */ 167 void setLinearText(bool linearText); 168 169 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set 170 @return true if the lineartext bit is set in the paint's flags 171 */ 172 bool isSubpixelText() const { 173 return SkToBool(this->getFlags() & kSubpixelText_Flag); 174 } 175 176 /** 177 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag. 178 * @param subpixelText true to set the subpixelText bit in the paint's 179 * flags, false to clear it. 180 */ 181 void setSubpixelText(bool subpixelText); 182 183 bool isLCDRenderText() const { 184 return SkToBool(this->getFlags() & kLCDRenderText_Flag); 185 } 186 187 /** 188 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag. 189 * Note: antialiasing must also be on for lcd rendering 190 * @param lcdText true to set the LCDRenderText bit in the paint's flags, 191 * false to clear it. 192 */ 193 void setLCDRenderText(bool lcdText); 194 195 bool isEmbeddedBitmapText() const { 196 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag); 197 } 198 199 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit 200 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags, 201 false to clear it. 202 */ 203 void setEmbeddedBitmapText(bool useEmbeddedBitmapText); 204 205 bool isAutohinted() const { 206 return SkToBool(this->getFlags() & kAutoHinting_Flag); 207 } 208 209 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit 210 @param useAutohinter true to set the kEmbeddedBitmapText bit in the 211 paint's flags, 212 false to clear it. 213 */ 214 void setAutohinted(bool useAutohinter); 215 216 bool isVerticalText() const { 217 return SkToBool(this->getFlags() & kVerticalText_Flag); 218 } 219 220 /** 221 * Helper for setting or clearing the kVerticalText_Flag bit in 222 * setFlags(...). 223 * 224 * If this bit is set, then advances are treated as Y values rather than 225 * X values, and drawText will places its glyphs vertically rather than 226 * horizontally. 227 */ 228 void setVerticalText(bool); 229 230 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set 231 @return true if the underlineText bit is set in the paint's flags. 232 */ 233 bool isUnderlineText() const { 234 return SkToBool(this->getFlags() & kUnderlineText_Flag); 235 } 236 237 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit 238 @param underlineText true to set the underlineText bit in the paint's 239 flags, false to clear it. 240 */ 241 void setUnderlineText(bool underlineText); 242 243 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set 244 @return true if the strikeThruText bit is set in the paint's flags. 245 */ 246 bool isStrikeThruText() const { 247 return SkToBool(this->getFlags() & kStrikeThruText_Flag); 248 } 249 250 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit 251 @param strikeThruText true to set the strikeThruText bit in the 252 paint's flags, false to clear it. 253 */ 254 void setStrikeThruText(bool strikeThruText); 255 256 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set 257 @return true if the kFakeBoldText_Flag bit is set in the paint's flags. 258 */ 259 bool isFakeBoldText() const { 260 return SkToBool(this->getFlags() & kFakeBoldText_Flag); 261 } 262 263 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit 264 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's 265 flags, false to clear it. 266 */ 267 void setFakeBoldText(bool fakeBoldText); 268 269 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set 270 @return true if the kernText bit is set in the paint's flags. 271 */ 272 bool isDevKernText() const { 273 return SkToBool(this->getFlags() & kDevKernText_Flag); 274 } 275 276 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit 277 @param kernText true to set the kKernText_Flag bit in the paint's 278 flags, false to clear it. 279 */ 280 void setDevKernText(bool devKernText); 281 282 /** 283 * Return the filter level. This affects the quality (and performance) of 284 * drawing scaled images. 285 */ 286 SkFilterQuality getFilterQuality() const { 287 return (SkFilterQuality)fBitfields.fFilterQuality; 288 } 289 290 /** 291 * Set the filter quality. This affects the quality (and performance) of 292 * drawing scaled images. 293 */ 294 void setFilterQuality(SkFilterQuality quality); 295 296 /** Styles apply to rect, oval, path, and text. 297 Bitmaps are always drawn in "fill", and lines are always drawn in 298 "stroke". 299 300 Note: strokeandfill implicitly draws the result with 301 SkPath::kWinding_FillType, so if the original path is even-odd, the 302 results may not appear the same as if it was drawn twice, filled and 303 then stroked. 304 */ 305 enum Style { 306 kFill_Style, //!< fill the geometry 307 kStroke_Style, //!< stroke the geometry 308 kStrokeAndFill_Style, //!< fill and stroke the geometry 309 }; 310 enum { 311 kStyleCount = kStrokeAndFill_Style + 1 312 }; 313 314 /** Return the paint's style, used for controlling how primitives' 315 geometries are interpreted (except for drawBitmap, which always assumes 316 kFill_Style). 317 @return the paint's Style 318 */ 319 Style getStyle() const { return (Style)fBitfields.fStyle; } 320 321 /** Set the paint's style, used for controlling how primitives' 322 geometries are interpreted (except for drawBitmap, which always assumes 323 Fill). 324 @param style The new style to set in the paint 325 */ 326 void setStyle(Style style); 327 328 /** Return the paint's color. Note that the color is a 32bit value 329 containing alpha as well as r,g,b. This 32bit value is not 330 premultiplied, meaning that its alpha can be any value, regardless of 331 the values of r,g,b. 332 @return the paint's color (and alpha). 333 */ 334 SkColor getColor() const { return fColor; } 335 336 /** Set the paint's color. Note that the color is a 32bit value containing 337 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning 338 that its alpha can be any value, regardless of the values of r,g,b. 339 @param color The new color (including alpha) to set in the paint. 340 */ 341 void setColor(SkColor color); 342 343 /** Helper to getColor() that just returns the color's alpha value. 344 @return the alpha component of the paint's color. 345 */ 346 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); } 347 348 /** Helper to setColor(), that only assigns the color's alpha value, 349 leaving its r,g,b values unchanged. 350 @param a set the alpha component (0..255) of the paint's color. 351 */ 352 void setAlpha(U8CPU a); 353 354 /** Helper to setColor(), that takes a,r,g,b and constructs the color value 355 using SkColorSetARGB() 356 @param a The new alpha component (0..255) of the paint's color. 357 @param r The new red component (0..255) of the paint's color. 358 @param g The new green component (0..255) of the paint's color. 359 @param b The new blue component (0..255) of the paint's color. 360 */ 361 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b); 362 363 /** Return the width for stroking. 364 <p /> 365 A value of 0 strokes in hairline mode. 366 Hairlines always draw 1-pixel wide, regardless of the matrix. 367 @return the paint's stroke width, used whenever the paint's style is 368 Stroke or StrokeAndFill. 369 */ 370 SkScalar getStrokeWidth() const { return fWidth; } 371 372 /** Set the width for stroking. 373 Pass 0 to stroke in hairline mode. 374 Hairlines always draw 1-pixel wide, regardless of the matrix. 375 @param width set the paint's stroke width, used whenever the paint's 376 style is Stroke or StrokeAndFill. 377 */ 378 void setStrokeWidth(SkScalar width); 379 380 /** Return the paint's stroke miter value. This is used to control the 381 behavior of miter joins when the joins angle is sharp. 382 @return the paint's miter limit, used whenever the paint's style is 383 Stroke or StrokeAndFill. 384 */ 385 SkScalar getStrokeMiter() const { return fMiterLimit; } 386 387 /** Set the paint's stroke miter value. This is used to control the 388 behavior of miter joins when the joins angle is sharp. This value must 389 be >= 0. 390 @param miter set the miter limit on the paint, used whenever the 391 paint's style is Stroke or StrokeAndFill. 392 */ 393 void setStrokeMiter(SkScalar miter); 394 395 /** Cap enum specifies the settings for the paint's strokecap. This is the 396 treatment that is applied to the beginning and end of each non-closed 397 contour (e.g. lines). 398 399 If the cap is round or square, the caps are drawn when the contour has 400 a zero length. Zero length contours can be created by following moveTo 401 with a lineTo at the same point, or a moveTo followed by a close. 402 403 A dash with an on interval of zero also creates a zero length contour. 404 405 The zero length contour draws the square cap without rotation, since 406 the no direction can be inferred. 407 */ 408 enum Cap { 409 kButt_Cap, //!< begin/end contours with no extension 410 kRound_Cap, //!< begin/end contours with a semi-circle extension 411 kSquare_Cap, //!< begin/end contours with a half square extension 412 413 kCapCount, 414 kDefault_Cap = kButt_Cap 415 }; 416 417 /** Join enum specifies the settings for the paint's strokejoin. This is 418 the treatment that is applied to corners in paths and rectangles. 419 */ 420 enum Join { 421 kMiter_Join, //!< connect path segments with a sharp join 422 kRound_Join, //!< connect path segments with a round join 423 kBevel_Join, //!< connect path segments with a flat bevel join 424 425 kJoinCount, 426 kDefault_Join = kMiter_Join 427 }; 428 429 /** Return the paint's stroke cap type, controlling how the start and end 430 of stroked lines and paths are treated. 431 @return the line cap style for the paint, used whenever the paint's 432 style is Stroke or StrokeAndFill. 433 */ 434 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; } 435 436 /** Set the paint's stroke cap type. 437 @param cap set the paint's line cap style, used whenever the paint's 438 style is Stroke or StrokeAndFill. 439 */ 440 void setStrokeCap(Cap cap); 441 442 /** Return the paint's stroke join type. 443 @return the paint's line join style, used whenever the paint's style is 444 Stroke or StrokeAndFill. 445 */ 446 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; } 447 448 /** Set the paint's stroke join type. 449 @param join set the paint's line join style, used whenever the paint's 450 style is Stroke or StrokeAndFill. 451 */ 452 void setStrokeJoin(Join join); 453 454 /** 455 * Applies any/all effects (patheffect, stroking) to src, returning the 456 * result in dst. The result is that drawing src with this paint will be 457 * the same as drawing dst with a default paint (at least from the 458 * geometric perspective). 459 * 460 * @param src input path 461 * @param dst output path (may be the same as src) 462 * @param cullRect If not null, the dst path may be culled to this rect. 463 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision 464 * in favor of speed/size. 465 * @return true if the path should be filled, or false if it should be 466 * drawn with a hairline (width == 0) 467 */ 468 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect, 469 SkScalar resScale = 1) const; 470 471 bool getFillPath(const SkPath& src, SkPath* dst) const { 472 return this->getFillPath(src, dst, NULL, 1); 473 } 474 475 /** Get the paint's shader object. 476 <p /> 477 The shader's reference count is not affected. 478 @return the paint's shader (or NULL) 479 */ 480 SkShader* getShader() const { return fShader; } 481 482 /** Set or clear the shader object. 483 * Shaders specify the source color(s) for what is being drawn. If a paint 484 * has no shader, then the paint's color is used. If the paint has a 485 * shader, then the shader's color(s) are use instead, but they are 486 * modulated by the paint's alpha. This makes it easy to create a shader 487 * once (e.g. bitmap tiling or gradient) and then change its transparency 488 * w/o having to modify the original shader... only the paint's alpha needs 489 * to be modified. 490 * 491 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates 492 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8) 493 * then the shader will use the paint's entire color to "colorize" its output (modulating the 494 * bitmap's alpha with the paint's color+alpha). 495 * 496 * Pass NULL to clear any previous shader. 497 * As a convenience, the parameter passed is also returned. 498 * If a previous shader exists, its reference count is decremented. 499 * If shader is not NULL, its reference count is incremented. 500 * @param shader May be NULL. The shader to be installed in the paint 501 * @return shader 502 */ 503 SkShader* setShader(SkShader* shader); 504 505 /** Get the paint's colorfilter. If there is a colorfilter, its reference 506 count is not changed. 507 @return the paint's colorfilter (or NULL) 508 */ 509 SkColorFilter* getColorFilter() const { return fColorFilter; } 510 511 /** Set or clear the paint's colorfilter, returning the parameter. 512 <p /> 513 If the paint already has a filter, its reference count is decremented. 514 If filter is not NULL, its reference count is incremented. 515 @param filter May be NULL. The filter to be installed in the paint 516 @return filter 517 */ 518 SkColorFilter* setColorFilter(SkColorFilter* filter); 519 520 /** Get the paint's xfermode object. 521 <p /> 522 The xfermode's reference count is not affected. 523 @return the paint's xfermode (or NULL) 524 */ 525 SkXfermode* getXfermode() const { return fXfermode; } 526 527 /** Set or clear the xfermode object. 528 <p /> 529 Pass NULL to clear any previous xfermode. 530 As a convenience, the parameter passed is also returned. 531 If a previous xfermode exists, its reference count is decremented. 532 If xfermode is not NULL, its reference count is incremented. 533 @param xfermode May be NULL. The new xfermode to be installed in the 534 paint 535 @return xfermode 536 */ 537 SkXfermode* setXfermode(SkXfermode* xfermode); 538 539 /** Create an xfermode based on the specified Mode, and assign it into the 540 paint, returning the mode that was set. If the Mode is SrcOver, then 541 the paint's xfermode is set to null. 542 */ 543 SkXfermode* setXfermodeMode(SkXfermode::Mode); 544 545 /** Get the paint's patheffect object. 546 <p /> 547 The patheffect reference count is not affected. 548 @return the paint's patheffect (or NULL) 549 */ 550 SkPathEffect* getPathEffect() const { return fPathEffect; } 551 552 /** Set or clear the patheffect object. 553 <p /> 554 Pass NULL to clear any previous patheffect. 555 As a convenience, the parameter passed is also returned. 556 If a previous patheffect exists, its reference count is decremented. 557 If patheffect is not NULL, its reference count is incremented. 558 @param effect May be NULL. The new patheffect to be installed in the 559 paint 560 @return effect 561 */ 562 SkPathEffect* setPathEffect(SkPathEffect* effect); 563 564 /** Get the paint's maskfilter object. 565 <p /> 566 The maskfilter reference count is not affected. 567 @return the paint's maskfilter (or NULL) 568 */ 569 SkMaskFilter* getMaskFilter() const { return fMaskFilter; } 570 571 /** Set or clear the maskfilter object. 572 <p /> 573 Pass NULL to clear any previous maskfilter. 574 As a convenience, the parameter passed is also returned. 575 If a previous maskfilter exists, its reference count is decremented. 576 If maskfilter is not NULL, its reference count is incremented. 577 @param maskfilter May be NULL. The new maskfilter to be installed in 578 the paint 579 @return maskfilter 580 */ 581 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter); 582 583 // These attributes are for text/fonts 584 585 /** Get the paint's typeface object. 586 <p /> 587 The typeface object identifies which font to use when drawing or 588 measuring text. The typeface reference count is not affected. 589 @return the paint's typeface (or NULL) 590 */ 591 SkTypeface* getTypeface() const { return fTypeface; } 592 593 /** Set or clear the typeface object. 594 <p /> 595 Pass NULL to clear any previous typeface. 596 As a convenience, the parameter passed is also returned. 597 If a previous typeface exists, its reference count is decremented. 598 If typeface is not NULL, its reference count is incremented. 599 @param typeface May be NULL. The new typeface to be installed in the 600 paint 601 @return typeface 602 */ 603 SkTypeface* setTypeface(SkTypeface* typeface); 604 605 /** Get the paint's rasterizer (or NULL). 606 <p /> 607 The raster controls how paths/text are turned into alpha masks. 608 @return the paint's rasterizer (or NULL) 609 */ 610 SkRasterizer* getRasterizer() const { return fRasterizer; } 611 612 /** Set or clear the rasterizer object. 613 <p /> 614 Pass NULL to clear any previous rasterizer. 615 As a convenience, the parameter passed is also returned. 616 If a previous rasterizer exists in the paint, its reference count is 617 decremented. If rasterizer is not NULL, its reference count is 618 incremented. 619 @param rasterizer May be NULL. The new rasterizer to be installed in 620 the paint. 621 @return rasterizer 622 */ 623 SkRasterizer* setRasterizer(SkRasterizer* rasterizer); 624 625 SkImageFilter* getImageFilter() const { return fImageFilter; } 626 SkImageFilter* setImageFilter(SkImageFilter*); 627 628 SkAnnotation* getAnnotation() const { return fAnnotation; } 629 SkAnnotation* setAnnotation(SkAnnotation*); 630 631 /** 632 * Return the paint's SkDrawLooper (if any). Does not affect the looper's 633 * reference count. 634 */ 635 SkDrawLooper* getLooper() const { return fLooper; } 636 637 /** 638 * Set or clear the looper object. 639 * <p /> 640 * Pass NULL to clear any previous looper. 641 * As a convenience, the parameter passed is also returned. 642 * If a previous looper exists in the paint, its reference count is 643 * decremented. If looper is not NULL, its reference count is 644 * incremented. 645 * @param looper May be NULL. The new looper to be installed in the paint. 646 * @return looper 647 */ 648 SkDrawLooper* setLooper(SkDrawLooper* looper); 649 650 enum Align { 651 kLeft_Align, 652 kCenter_Align, 653 kRight_Align, 654 }; 655 enum { 656 kAlignCount = 3 657 }; 658 659 /** Return the paint's Align value for drawing text. 660 @return the paint's Align value for drawing text. 661 */ 662 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; } 663 664 /** Set the paint's text alignment. 665 @param align set the paint's Align value for drawing text. 666 */ 667 void setTextAlign(Align align); 668 669 /** Return the paint's text size. 670 @return the paint's text size. 671 */ 672 SkScalar getTextSize() const { return fTextSize; } 673 674 /** Set the paint's text size. This value must be > 0 675 @param textSize set the paint's text size. 676 */ 677 void setTextSize(SkScalar textSize); 678 679 /** Return the paint's horizontal scale factor for text. The default value 680 is 1.0. 681 @return the paint's scale factor in X for drawing/measuring text 682 */ 683 SkScalar getTextScaleX() const { return fTextScaleX; } 684 685 /** Set the paint's horizontal scale factor for text. The default value 686 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will 687 stretch the text narrower. 688 @param scaleX set the paint's scale factor in X for drawing/measuring 689 text. 690 */ 691 void setTextScaleX(SkScalar scaleX); 692 693 /** Return the paint's horizontal skew factor for text. The default value 694 is 0. 695 @return the paint's skew factor in X for drawing text. 696 */ 697 SkScalar getTextSkewX() const { return fTextSkewX; } 698 699 /** Set the paint's horizontal skew factor for text. The default value 700 is 0. For approximating oblique text, use values around -0.25. 701 @param skewX set the paint's skew factor in X for drawing text. 702 */ 703 void setTextSkewX(SkScalar skewX); 704 705 /** Describes how to interpret the text parameters that are passed to paint 706 methods like measureText() and getTextWidths(). 707 */ 708 enum TextEncoding { 709 kUTF8_TextEncoding, //!< the text parameters are UTF8 710 kUTF16_TextEncoding, //!< the text parameters are UTF16 711 kUTF32_TextEncoding, //!< the text parameters are UTF32 712 kGlyphID_TextEncoding //!< the text parameters are glyph indices 713 }; 714 715 TextEncoding getTextEncoding() const { 716 return (TextEncoding)fBitfields.fTextEncoding; 717 } 718 719 void setTextEncoding(TextEncoding encoding); 720 721 struct FontMetrics { 722 /** Flags which indicate the confidence level of various metrics. 723 A set flag indicates that the metric may be trusted. 724 */ 725 enum FontMetricsFlags { 726 kUnderlineThinknessIsValid_Flag = 1 << 0, 727 kUnderlinePositionIsValid_Flag = 1 << 1, 728 }; 729 730 uint32_t fFlags; //!< Bit field to identify which values are unknown 731 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0) 732 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0) 733 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0) 734 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0) 735 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0) 736 SkScalar fAvgCharWidth; //!< the average character width (>= 0) 737 SkScalar fMaxCharWidth; //!< the max character width (>= 0) 738 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs 739 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs 740 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face 741 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined. 742 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined 743 744 /** Underline Position - position of the top of the Underline stroke 745 relative to the baseline, this can have following values 746 - Negative - means underline should be drawn above baseline. 747 - Positive - means below baseline. 748 - Zero - mean underline should be drawn on baseline. 749 */ 750 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined 751 752 /** If the fontmetrics has a valid underlinethickness, return true, and set the 753 thickness param to that value. If it doesn't return false and ignore the 754 thickness param. 755 */ 756 bool hasUnderlineThickness(SkScalar* thickness) const { 757 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) { 758 *thickness = fUnderlineThickness; 759 return true; 760 } 761 return false; 762 } 763 764 /** If the fontmetrics has a valid underlineposition, return true, and set the 765 thickness param to that value. If it doesn't return false and ignore the 766 thickness param. 767 */ 768 bool hasUnderlinePosition(SkScalar* position) const { 769 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) { 770 *position = fUnderlinePosition; 771 return true; 772 } 773 return false; 774 } 775 776 }; 777 778 /** Return the recommend spacing between lines (which will be 779 fDescent - fAscent + fLeading). 780 If metrics is not null, return in it the font metrics for the 781 typeface/pointsize/etc. currently set in the paint. 782 @param metrics If not null, returns the font metrics for the 783 current typeface/pointsize/etc setting in this 784 paint. 785 @param scale If not 0, return width as if the canvas were scaled 786 by this value 787 @param return the recommended spacing between lines 788 */ 789 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const; 790 791 /** Return the recommend line spacing. This will be 792 fDescent - fAscent + fLeading 793 */ 794 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); } 795 796 /** Convert the specified text into glyph IDs, returning the number of 797 glyphs ID written. If glyphs is NULL, it is ignore and only the count 798 is returned. 799 */ 800 int textToGlyphs(const void* text, size_t byteLength, 801 uint16_t glyphs[]) const; 802 803 /** Return true if all of the specified text has a corresponding non-zero 804 glyph ID. If any of the code-points in the text are not supported in 805 the typeface (i.e. the glyph ID would be zero), then return false. 806 807 If the text encoding for the paint is kGlyph_TextEncoding, then this 808 returns true if all of the specified glyph IDs are non-zero. 809 */ 810 bool containsText(const void* text, size_t byteLength) const; 811 812 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped 813 to zero. Note: this does not look at the text-encoding setting in the 814 paint, only at the typeface. 815 */ 816 void glyphsToUnichars(const uint16_t glyphs[], int count, SkUnichar text[]) const; 817 818 /** Return the number of drawable units in the specified text buffer. 819 This looks at the current TextEncoding field of the paint. If you also 820 want to have the text converted into glyph IDs, call textToGlyphs 821 instead. 822 */ 823 int countText(const void* text, size_t byteLength) const { 824 return this->textToGlyphs(text, byteLength, NULL); 825 } 826 827 /** Return the width of the text. This will return the vertical measure 828 * if isVerticalText() is true, in which case the returned value should 829 * be treated has a height instead of a width. 830 * 831 * @param text The text to be measured 832 * @param length Number of bytes of text to measure 833 * @param bounds If not NULL, returns the bounds of the text, 834 * relative to (0, 0). 835 * @return The advance width of the text 836 */ 837 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const; 838 839 /** Return the width of the text. This will return the vertical measure 840 * if isVerticalText() is true, in which case the returned value should 841 * be treated has a height instead of a width. 842 * 843 * @param text Address of the text 844 * @param length Number of bytes of text to measure 845 * @return The advance width of the text 846 */ 847 SkScalar measureText(const void* text, size_t length) const { 848 return this->measureText(text, length, NULL); 849 } 850 851 /** Return the number of bytes of text that were measured. If 852 * isVerticalText() is true, then the vertical advances are used for 853 * the measurement. 854 * 855 * @param text The text to be measured 856 * @param length Number of bytes of text to measure 857 * @param maxWidth Maximum width. Only the subset of text whose accumulated 858 * widths are <= maxWidth are measured. 859 * @param measuredWidth Optional. If non-null, this returns the actual 860 * width of the measured text. 861 * @return The number of bytes of text that were measured. Will be 862 * <= length. 863 */ 864 size_t breakText(const void* text, size_t length, SkScalar maxWidth, 865 SkScalar* measuredWidth = NULL) const; 866 867 /** Return the advances for the text. These will be vertical advances if 868 * isVerticalText() returns true. 869 * 870 * @param text the text 871 * @param byteLength number of bytes to of text 872 * @param widths If not null, returns the array of advances for 873 * the glyphs. If not NULL, must be at least a large 874 * as the number of unichars in the specified text. 875 * @param bounds If not null, returns the bounds for each of 876 * character, relative to (0, 0) 877 * @return the number of unichars in the specified text. 878 */ 879 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[], 880 SkRect bounds[] = NULL) const; 881 882 /** Return the path (outline) for the specified text. 883 * Note: just like SkCanvas::drawText, this will respect the Align setting 884 * in the paint. 885 * 886 * @param text the text 887 * @param length number of bytes of text 888 * @param x The x-coordinate of the origin of the text. 889 * @param y The y-coordinate of the origin of the text. 890 * @param path The outline of the text. 891 */ 892 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y, 893 SkPath* path) const; 894 895 /** Return the path (outline) for the specified text. 896 * Note: just like SkCanvas::drawText, this will respect the Align setting 897 * in the paint. 898 * 899 * @param text the text 900 * @param length number of bytes of text 901 * @param pos array of positions, used to position each character 902 * @param path The outline of the text. 903 */ 904 void getPosTextPath(const void* text, size_t length, 905 const SkPoint pos[], SkPath* path) const; 906 907 /** Return the number of intervals that intersect the intercept along the axis of the advance. 908 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in 909 * the string. The caller may pass nullptr for intervals to determine the size of the interval 910 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed 911 * intervals are cached by glyph to improve performance for multiple calls. 912 * This permits constructing an underline that skips the descenders. 913 * 914 * @param text the text 915 * @param length number of bytes of text 916 * @param x The x-coordinate of the origin of the text. 917 * @param y The y-coordinate of the origin of the text. 918 * @param bounds The lower and upper line parallel to the advance. 919 * @param array If not null, the found intersections. 920 * 921 * @return The number of intersections, which may be zero. 922 */ 923 int getTextIntercepts(const void* text, size_t length, SkScalar x, SkScalar y, 924 const SkScalar bounds[2], SkScalar* intervals) const; 925 926 /** Return the number of intervals that intersect the intercept along the axis of the advance. 927 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in 928 * string. The caller may pass nullptr for intervals to determine the size of the interval 929 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed 930 * intervals are cached by glyph to improve performance for multiple calls. 931 * This permits constructing an underline that skips the descenders. 932 * 933 * @param text the text 934 * @param length number of bytes of text 935 * @param pos array of positions, used to position each character 936 * @param bounds The lower and upper line parallel to the advance. 937 * @param array If not null, the glyph bounds contained by the advance parallel lines. 938 * 939 * @return The number of intersections, which may be zero. 940 */ 941 int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[], 942 const SkScalar bounds[2], SkScalar* intervals) const; 943 944 /** 945 * Return a rectangle that represents the union of the bounds of all 946 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and 947 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x 948 * on this paint. 949 */ 950 SkRect getFontBounds() const; 951 952 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to 953 // mean that we need not draw at all (e.g. SrcOver + 0-alpha) 954 bool nothingToDraw() const; 955 956 /////////////////////////////////////////////////////////////////////////// 957 // would prefer to make these private... 958 959 /** Returns true if the current paint settings allow for fast computation of 960 bounds (i.e. there is nothing complex like a patheffect that would make 961 the bounds computation expensive. 962 */ 963 bool canComputeFastBounds() const; 964 965 /** Only call this if canComputeFastBounds() returned true. This takes a 966 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic 967 effects in the paint (e.g. stroking). If needed, it uses the storage 968 rect parameter. It returns the adjusted bounds that can then be used 969 for quickReject tests. 970 971 The returned rect will either be orig or storage, thus the caller 972 should not rely on storage being set to the result, but should always 973 use the retured value. It is legal for orig and storage to be the same 974 rect. 975 976 e.g. 977 if (paint.canComputeFastBounds()) { 978 SkRect r, storage; 979 path.computeBounds(&r, SkPath::kFast_BoundsType); 980 const SkRect& fastR = paint.computeFastBounds(r, &storage); 981 if (canvas->quickReject(fastR, ...)) { 982 // don't draw the path 983 } 984 } 985 */ 986 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const { 987 SkPaint::Style style = this->getStyle(); 988 // ultra fast-case: filling with no effects that affect geometry 989 if (kFill_Style == style) { 990 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper()); 991 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter()); 992 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect()); 993 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter()); 994 if (!effects) { 995 return orig; 996 } 997 } 998 999 return this->doComputeFastBounds(orig, storage, style); 1000 } 1001 1002 const SkRect& computeFastStrokeBounds(const SkRect& orig, 1003 SkRect* storage) const { 1004 return this->doComputeFastBounds(orig, storage, kStroke_Style); 1005 } 1006 1007 // Take the style explicitly, so the caller can force us to be stroked 1008 // without having to make a copy of the paint just to change that field. 1009 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage, 1010 Style) const; 1011 1012 /** 1013 * Return a matrix that applies the paint's text values: size, scale, skew 1014 */ 1015 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size, 1016 SkScalar scaleX, SkScalar skewX) { 1017 matrix->setScale(size * scaleX, size); 1018 if (skewX) { 1019 matrix->postSkew(skewX, 0); 1020 } 1021 return matrix; 1022 } 1023 1024 SkMatrix* setTextMatrix(SkMatrix* matrix) const { 1025 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX); 1026 } 1027 1028 typedef const SkGlyph& (*GlyphCacheProc)(SkGlyphCache*, const char**); 1029 1030 SK_TO_STRING_NONVIRT() 1031 1032 private: 1033 SkTypeface* fTypeface; 1034 SkPathEffect* fPathEffect; 1035 SkShader* fShader; 1036 SkXfermode* fXfermode; 1037 SkMaskFilter* fMaskFilter; 1038 SkColorFilter* fColorFilter; 1039 SkRasterizer* fRasterizer; 1040 SkDrawLooper* fLooper; 1041 SkImageFilter* fImageFilter; 1042 SkAnnotation* fAnnotation; 1043 1044 SkScalar fTextSize; 1045 SkScalar fTextScaleX; 1046 SkScalar fTextSkewX; 1047 SkColor fColor; 1048 SkScalar fWidth; 1049 SkScalar fMiterLimit; 1050 union { 1051 struct { 1052 // all of these bitfields should add up to 32 1053 unsigned fFlags : 16; 1054 unsigned fTextAlign : 2; 1055 unsigned fCapType : 2; 1056 unsigned fJoinType : 2; 1057 unsigned fStyle : 2; 1058 unsigned fTextEncoding : 2; // 3 values 1059 unsigned fHinting : 2; 1060 unsigned fFilterQuality : 2; 1061 //unsigned fFreeBits : 2; 1062 } fBitfields; 1063 uint32_t fBitfieldsUInt; 1064 }; 1065 1066 GlyphCacheProc getGlyphCacheProc(bool needFullMetrics) const; 1067 1068 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length, 1069 int* count, SkRect* bounds) const; 1070 1071 enum class FakeGamma { 1072 Off = 0, On 1073 }; 1074 1075 /* 1076 * Allocs an SkDescriptor on the heap and return it to the caller as a refcnted 1077 * SkData. Caller is responsible for managing the lifetime of this object. 1078 */ 1079 void getScalerContextDescriptor(SkAutoDescriptor*, const SkSurfaceProps& surfaceProps, 1080 FakeGamma fakeGamma, const SkMatrix*) const; 1081 1082 SkGlyphCache* detachCache(const SkSurfaceProps* surfaceProps, FakeGamma fakeGamma, 1083 const SkMatrix*) const; 1084 1085 void descriptorProc(const SkSurfaceProps* surfaceProps, FakeGamma fakeGamma, 1086 const SkMatrix* deviceMatrix, 1087 void (*proc)(SkTypeface*, const SkDescriptor*, void*), 1088 void* context) const; 1089 1090 /* 1091 * The luminance color is used to determine which Gamma Canonical color to map to. This is 1092 * really only used by backends which want to cache glyph masks, and need some way to know if 1093 * they need to generate new masks based off a given color. 1094 */ 1095 SkColor computeLuminanceColor() const; 1096 1097 enum { 1098 /* This is the size we use when we ask for a glyph's path. We then 1099 * post-transform it as we draw to match the request. 1100 * This is done to try to re-use cache entries for the path. 1101 * 1102 * This value is somewhat arbitrary. In theory, it could be 1, since 1103 * we store paths as floats. However, we get the path from the font 1104 * scaler, and it may represent its paths as fixed-point (or 26.6), 1105 * so we shouldn't ask for something too big (might overflow 16.16) 1106 * or too small (underflow 26.6). 1107 * 1108 * This value could track kMaxSizeForGlyphCache, assuming the above 1109 * constraints, but since we ask for unhinted paths, the two values 1110 * need not match per-se. 1111 */ 1112 kCanonicalTextSizeForPaths = 64, 1113 1114 /* 1115 * Above this size (taking into account CTM and textSize), we never use 1116 * the cache for bits or metrics (we might overflow), so we just ask 1117 * for a caononical size and post-transform that. 1118 */ 1119 kMaxSizeForGlyphCache = 256, 1120 }; 1121 1122 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM); 1123 1124 // Set flags/hinting/textSize up to use for drawing text as paths. 1125 // Returns scale factor to restore the original textSize, since will will 1126 // have change it to kCanonicalTextSizeForPaths. 1127 SkScalar setupForAsPaths(); 1128 1129 static SkScalar MaxCacheSize2() { 1130 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache); 1131 static const SkScalar kMag2Max = kMaxSize * kMaxSize; 1132 return kMag2Max; 1133 } 1134 1135 friend class SkAutoGlyphCache; 1136 friend class SkAutoGlyphCacheNoGamma; 1137 friend class SkCanvas; 1138 friend class SkDraw; 1139 friend class SkPDFDevice; 1140 friend class GrAtlasTextBlob; 1141 friend class GrAtlasTextContext; 1142 friend class GrStencilAndCoverTextContext; 1143 friend class GrPathRendering; 1144 friend class GrTextUtils; 1145 friend class GrGLPathRendering; 1146 friend class SkScalerContext; 1147 friend class SkTextBaseIter; 1148 friend class SkCanonicalizePaint; 1149 }; 1150 1151 #endif 1152