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 "SkBlendMode.h" 12#include "SkColor.h" 13#include "SkFilterQuality.h" 14#include "SkMatrix.h" 15#include "SkRefCnt.h" 16 17class SkAutoDescriptor; 18class SkAutoGlyphCache; 19class SkColorFilter; 20class SkData; 21class SkDescriptor; 22class SkDrawLooper; 23class SkReadBuffer; 24class SkWriteBuffer; 25class SkGlyph; 26struct SkRect; 27class SkGlyphCache; 28class SkImageFilter; 29class SkMaskFilter; 30class SkPath; 31class SkPathEffect; 32struct SkPoint; 33class SkShader; 34class SkSurfaceProps; 35class SkTextBlob; 36class SkTypeface; 37 38/** \class SkPaint 39 SkPaint controls options applied when drawing and measuring. SkPaint collects all 40 options outside of the SkCanvas clip and SkCanvas matrix. 41 42 Various options apply to text, strokes and fills, and images. 43 44 Some options may not be implemented on all platforms; in these cases, setting 45 the option has no effect. Some options are conveniences that duplicate SkCanvas 46 functionality; for instance, text size is identical to matrix scale. 47 48 SkPaint options are rarely exclusive; each option modifies a stage of the drawing 49 pipeline and multiple pipeline stages may be affected by a single SkPaint. 50 51 SkPaint collects effects and filters that describe single-pass and multiple-pass 52 algorithms that alter the drawing geometry, color, and transparency. For instance, 53 SkPaint does not directly implement dashing or blur, but contains the objects that do so. 54 55 The objects contained by SkPaint are opaque, and cannot be edited outside of the SkPaint 56 to affect it. The implementation is free to defer computations associated with the 57 SkPaint, or ignore them altogether. For instance, some GPU implementations draw all 58 SkPath geometries with anti-aliasing, regardless of how SkPaint::kAntiAlias_Flag 59 is set in SkPaint. 60 61 SkPaint describes a single color, a single font, a single image quality, and so on. 62 Multiple colors are drawn either by using multiple paints or with objects like 63 SkShader attached to SkPaint. 64*/ 65class SK_API SkPaint { 66public: 67 68 /** Constructs SkPaint with default values. 69 70 @return default initialized SkPaint 71 */ 72 SkPaint(); 73 74 /** Makes a shallow copy of SkPaint. SkTypeface, SkPathEffect, SkShader, 75 SkMaskFilter, SkColorFilter, SkDrawLooper, and SkImageFilter are shared 76 between the original paint and the copy. Objects containing SkRefCnt increment 77 their references by one. 78 79 The referenced objects SkPathEffect, SkShader, SkMaskFilter, SkColorFilter, 80 SkDrawLooper, and SkImageFilter cannot be modified after they are created. 81 This prevents objects with SkRefCnt from being modified once SkPaint refers to them. 82 83 @param paint original to copy 84 @return shallow copy of paint 85 */ 86 SkPaint(const SkPaint& paint); 87 88 /** Implements a move constructor to avoid increasing the reference counts 89 of objects referenced by the paint. 90 91 After the call, paint is undefined, and can be safely destructed. 92 93 @param paint original to move 94 @return content of paint 95 */ 96 SkPaint(SkPaint&& paint); 97 98 /** Decreases SkPaint SkRefCnt of owned objects: SkTypeface, SkPathEffect, SkShader, 99 SkMaskFilter, SkColorFilter, SkDrawLooper, and SkImageFilter. If the 100 objects containing SkRefCnt go to zero, they are deleted. 101 */ 102 ~SkPaint(); 103 104 /** Makes a shallow copy of SkPaint. SkTypeface, SkPathEffect, SkShader, 105 SkMaskFilter, SkColorFilter, SkDrawLooper, and SkImageFilter are shared 106 between the original paint and the copy. Objects containing SkRefCnt in the 107 prior destination are decreased by one, and the referenced objects are deleted if the 108 resulting count is zero. Objects containing SkRefCnt in the parameter paint 109 are increased by one. paint is unmodified. 110 111 @param paint original to copy 112 @return content of paint 113 */ 114 SkPaint& operator=(const SkPaint& paint); 115 116 /** Moves the paint to avoid increasing the reference counts 117 of objects referenced by the paint parameter. Objects containing SkRefCnt in the 118 prior destination are decreased by one; those objects are deleted if the resulting count 119 is zero. 120 121 After the call, paint is undefined, and can be safely destructed. 122 123 @param paint original to move 124 @return content of paint 125 */ 126 SkPaint& operator=(SkPaint&& paint); 127 128 /** Compares a and b, and returns true if a and b are equivalent. May return false 129 if SkTypeface, SkPathEffect, SkShader, SkMaskFilter, SkColorFilter, 130 SkDrawLooper, or SkImageFilter have identical contents but different pointers. 131 132 @param a SkPaint to compare 133 @param b SkPaint to compare 134 @return true if SkPaint pair are equivalent 135 */ 136 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b); 137 138 /** Compares a and b, and returns true if a and b are not equivalent. May return true 139 if SkTypeface, SkPathEffect, SkShader, SkMaskFilter, SkColorFilter, 140 SkDrawLooper, or SkImageFilter have identical contents but different pointers. 141 142 @param a SkPaint to compare 143 @param b SkPaint to compare 144 @return true if SkPaint pair are not equivalent 145 */ 146 friend bool operator!=(const SkPaint& a, const SkPaint& b) { 147 return !(a == b); 148 } 149 150 /** Returns a hash generated from SkPaint values and pointers. 151 Identical hashes guarantee that the paints are 152 equivalent, but differing hashes do not guarantee that the paints have differing 153 contents. 154 155 If operator==(const SkPaint& a, const SkPaint& b) returns true for two paints, 156 their hashes are also equal. 157 158 The hash returned is platform and implementation specific. 159 160 @return a shallow hash 161 */ 162 uint32_t getHash() const; 163 164 /** Serializes SkPaint into a buffer. A companion unflatten() call 165 can reconstitute the paint at a later time. 166 167 @param buffer SkWriteBuffer receiving the flattened SkPaint data 168 */ 169 void flatten(SkWriteBuffer& buffer) const; 170 171 /** Populates SkPaint, typically from a serialized stream, created by calling 172 flatten() at an earlier time. 173 174 SkReadBuffer class is not public, so unflatten() cannot be meaningfully called 175 by the client. 176 177 @param buffer serialized data describing SkPaint content 178 @return false if the buffer contained invalid data to initialize the paint, in which case 179 the paint will be reset(). 180 */ 181 bool unflatten(SkReadBuffer& buffer); 182 183 /** Sets all SkPaint contents to their initial values. This is equivalent to replacing 184 SkPaint with the result of SkPaint(). 185 */ 186 void reset(); 187 188 /** \enum SkPaint::Hinting 189 Hinting adjusts the glyph outlines so that the shape provides a uniform 190 look at a given point size on font engines that support it. Hinting may have a 191 muted effect or no effect at all depending on the platform. 192 193 The four levels roughly control corresponding features on platforms that use FreeType 194 as the font engine. 195 */ 196 enum Hinting { 197 /** Leaves glyph outlines unchanged from their native representation. 198 With FreeType, this is equivalent to the FT_LOAD_NO_HINTING 199 bit-field constant supplied to FT_Load_Glyph, which indicates that the vector 200 outline being loaded should not be fitted to the pixel grid but simply scaled 201 to 26.6 fractional pixels. 202 */ 203 kNo_Hinting = 0, 204 205 /** Modifies glyph outlines minimally to improve constrast. 206 With FreeType, this is equivalent in spirit to the 207 FT_LOAD_TARGET_LIGHT value supplied to FT_Load_Glyph. It chooses a 208 lighter hinting algorithm for non-monochrome modes. 209 Generated glyphs may be fuzzy but better resemble their original shape. 210 */ 211 kSlight_Hinting = 1, 212 213 /** Modifies glyph outlines to improve constrast. This is the default. 214 With FreeType, this supplies FT_LOAD_TARGET_NORMAL to FT_Load_Glyph, 215 choosing the default hinting algorithm, which is optimized for standard 216 gray-level rendering. 217 */ 218 kNormal_Hinting = 2, 219 220 /** Modifies glyph outlines for maxiumum constrast. With FreeType, this selects 221 FT_LOAD_TARGET_LCD or FT_LOAD_TARGET_LCD_V if kLCDRenderText_Flag is set. 222 FT_LOAD_TARGET_LCD is a variant of FT_LOAD_TARGET_NORMAL optimized for 223 horizontally decimated LCD displays; FT_LOAD_TARGET_LCD_V is a 224 variant of FT_LOAD_TARGET_NORMAL optimized for vertically decimated LCD displays. 225 */ 226 kFull_Hinting = 3, 227 }; 228 229 /** Returns level of glyph outline adjustment. 230 231 @return one of: kNo_Hinting, kSlight_Hinting, kNormal_Hinting, kFull_Hinting 232 */ 233 Hinting getHinting() const { 234 return static_cast<Hinting>(fBitfields.fHinting); 235 } 236 237 /** Sets level of glyph outline adjustment. 238 Does not check for valid values of hintingLevel. 239 240 @param hintingLevel one of: kNo_Hinting, kSlight_Hinting, kNormal_Hinting, kFull_Hinting 241 */ 242 void setHinting(Hinting hintingLevel); 243 244 /** \enum SkPaint::Flags 245 The bit values stored in Flags. 246 The default value for Flags, normally zero, can be changed at compile time 247 with a custom definition of SkPaintDefaults_Flags. 248 All flags can be read and written explicitly; Flags allows manipulating 249 multiple settings at once. 250 */ 251 enum Flags { 252 kAntiAlias_Flag = 0x01, //!< mask for setting anti-alias 253 kDither_Flag = 0x04, //!< mask for setting dither 254 kFakeBoldText_Flag = 0x20, //!< mask for setting fake bold 255 kLinearText_Flag = 0x40, //!< mask for setting linear text 256 kSubpixelText_Flag = 0x80, //!< mask for setting subpixel text 257 kDevKernText_Flag = 0x100, //!< mask for setting full hinting spacing 258 kLCDRenderText_Flag = 0x200, //!< mask for setting lcd text 259 kEmbeddedBitmapText_Flag = 0x400, //!< mask for setting font embedded bitmaps 260 kAutoHinting_Flag = 0x800, //!< mask for setting auto-hinting 261 kVerticalText_Flag = 0x1000, //!< mask for setting vertical text 262 263 /** Hack for GDI -- do not use if you can help it */ 264 kGenA8FromLCD_Flag = 0x2000, 265 266 /** mask of all Flags, including private flags and flags reserved for future use */ 267 kAllFlags = 0xFFFF, 268 }; 269 270 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 271 enum ReserveFlags { 272 kUnderlineText_ReserveFlag = 0x08, //!< mask for underline text 273 kStrikeThruText_ReserveFlag = 0x10, //!< mask for strike-thru text 274 }; 275 #endif 276 277 /** Returns paint settings described by SkPaint::Flags. Each setting uses one 278 bit, and can be tested with SkPaint::Flags members. 279 280 @return zero, one, or more bits described by SkPaint::Flags 281 */ 282 uint32_t getFlags() const { return fBitfields.fFlags; } 283 284 /** Replaces SkPaint::Flags with flags, the union of the SkPaint::Flags members. 285 All SkPaint::Flags members may be cleared, or one or more may be set. 286 287 @param flags union of SkPaint::Flags for SkPaint 288 */ 289 void setFlags(uint32_t flags); 290 291 /** If true, pixels on the active edges of SkPath may be drawn with partial transparency. 292 293 Equivalent to getFlags() masked with kAntiAlias_Flag. 294 295 @return kAntiAlias_Flag state 296 */ 297 bool isAntiAlias() const { 298 return SkToBool(this->getFlags() & kAntiAlias_Flag); 299 } 300 301 /** Requests, but does not require, that SkPath edge pixels draw opaque or with 302 partial transparency. 303 304 Sets kAntiAlias_Flag if aa is true. 305 Clears kAntiAlias_Flag if aa is false. 306 307 @param aa setting for kAntiAlias_Flag 308 */ 309 void setAntiAlias(bool aa); 310 311 /** If true, color error may be distributed to smooth color transition. 312 313 Equivalent to getFlags() masked with kDither_Flag. 314 315 @return kDither_Flag state 316 */ 317 bool isDither() const { 318 return SkToBool(this->getFlags() & kDither_Flag); 319 } 320 321 /** Requests, but does not require, to distribute color error. 322 323 Sets kDither_Flag if dither is true. 324 Clears kDither_Flag if dither is false. 325 326 @param dither setting for kDither_Flag 327 */ 328 void setDither(bool dither); 329 330 /** If true, text is converted to SkPath before drawing and measuring. 331 332 Equivalent to getFlags() masked with kLinearText_Flag. 333 334 @return kLinearText_Flag state 335 */ 336 bool isLinearText() const { 337 return SkToBool(this->getFlags() & kLinearText_Flag); 338 } 339 340 /** If true, text is converted to SkPath before drawing and measuring. 341 By default, kLinearText_Flag is clear. 342 343 Sets kLinearText_Flag if linearText is true. 344 Clears kLinearText_Flag if linearText is false. 345 346 @param linearText setting for kLinearText_Flag 347 */ 348 void setLinearText(bool linearText); 349 350 /** If true, glyphs at different sub-pixel positions may differ on pixel edge coverage. 351 352 Equivalent to getFlags() masked with kSubpixelText_Flag. 353 354 @return kSubpixelText_Flag state 355 */ 356 bool isSubpixelText() const { 357 return SkToBool(this->getFlags() & kSubpixelText_Flag); 358 } 359 360 /** Requests, but does not require, that glyphs respect sub-pixel positioning. 361 362 Sets kSubpixelText_Flag if subpixelText is true. 363 Clears kSubpixelText_Flag if subpixelText is false. 364 365 @param subpixelText setting for kSubpixelText_Flag 366 */ 367 void setSubpixelText(bool subpixelText); 368 369 /** If true, glyphs may use LCD striping to improve glyph edges. 370 371 Returns true if SkPaint::Flags kLCDRenderText_Flag is set. 372 373 @return kLCDRenderText_Flag state 374 */ 375 bool isLCDRenderText() const { 376 return SkToBool(this->getFlags() & kLCDRenderText_Flag); 377 } 378 379 /** Requests, but does not require, that glyphs use LCD striping for glyph edges. 380 381 Sets kLCDRenderText_Flag if lcdText is true. 382 Clears kLCDRenderText_Flag if lcdText is false. 383 384 @param lcdText setting for kLCDRenderText_Flag 385 */ 386 void setLCDRenderText(bool lcdText); 387 388 /** If true, font engine may return glyphs from font bitmaps instead of from outlines. 389 390 Equivalent to getFlags() masked with kEmbeddedBitmapText_Flag. 391 392 @return kEmbeddedBitmapText_Flag state 393 */ 394 bool isEmbeddedBitmapText() const { 395 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag); 396 } 397 398 /** Requests, but does not require, to use bitmaps in fonts instead of outlines. 399 400 Sets kEmbeddedBitmapText_Flag if useEmbeddedBitmapText is true. 401 Clears kEmbeddedBitmapText_Flag if useEmbeddedBitmapText is false. 402 403 @param useEmbeddedBitmapText setting for kEmbeddedBitmapText_Flag 404 */ 405 void setEmbeddedBitmapText(bool useEmbeddedBitmapText); 406 407 /** If true, and if SkPaint::Hinting is set to kNormal_Hinting or kFull_Hinting, and if 408 platform uses FreeType as the font manager, instruct the font manager to always hint 409 glyphs. 410 411 Equivalent to getFlags() masked with kAutoHinting_Flag. 412 413 @return kAutoHinting_Flag state 414 */ 415 bool isAutohinted() const { 416 return SkToBool(this->getFlags() & kAutoHinting_Flag); 417 } 418 419 /** If SkPaint::Hinting is set to kNormal_Hinting or kFull_Hinting and useAutohinter is set, 420 instruct the font manager to always hint glyphs. 421 auto-hinting has no effect if SkPaint::Hinting is set to kNo_Hinting or 422 kSlight_Hinting. 423 424 Only affects platforms that use FreeType as the font manager. 425 426 Sets kAutoHinting_Flag if useAutohinter is true. 427 Clears kAutoHinting_Flag if useAutohinter is false. 428 429 @param useAutohinter setting for kAutoHinting_Flag 430 */ 431 void setAutohinted(bool useAutohinter); 432 433 /** If true, glyphs are drawn top to bottom instead of left to right. 434 435 Equivalent to getFlags() masked with kVerticalText_Flag. 436 437 @return kVerticalText_Flag state 438 */ 439 bool isVerticalText() const { 440 return SkToBool(this->getFlags() & kVerticalText_Flag); 441 } 442 443 /** If true, text advance positions the next glyph below the previous glyph instead of to the 444 right of previous glyph. 445 446 Sets kVerticalText_Flag if vertical is true. 447 Clears kVerticalText_Flag if vertical is false. 448 449 @param verticalText setting for kVerticalText_Flag 450 */ 451 void setVerticalText(bool verticalText); 452 453 /** If true, approximate bold by increasing the stroke width when creating glyph bitmaps 454 from outlines. 455 456 Equivalent to getFlags() masked with kFakeBoldText_Flag. 457 458 @return kFakeBoldText_Flag state 459 */ 460 bool isFakeBoldText() const { 461 return SkToBool(this->getFlags() & kFakeBoldText_Flag); 462 } 463 464 /** Use increased stroke width when creating glyph bitmaps to approximate a bold typeface. 465 466 Sets kFakeBoldText_Flag if fakeBoldText is true. 467 Clears kFakeBoldText_Flag if fakeBoldText is false. 468 469 @param fakeBoldText setting for kFakeBoldText_Flag 470 */ 471 void setFakeBoldText(bool fakeBoldText); 472 473 /** Returns if character spacing may be adjusted by the hinting difference. 474 475 Equivalent to getFlags() masked with kDevKernText_Flag. 476 477 @return kDevKernText_Flag state 478 */ 479 bool isDevKernText() const { 480 return SkToBool(this->getFlags() & kDevKernText_Flag); 481 } 482 483 /** Requests, but does not require, to use hinting to adjust glyph spacing. 484 485 Sets kDevKernText_Flag if devKernText is true. 486 Clears kDevKernText_Flag if devKernText is false. 487 488 @param devKernText setting for devKernText 489 */ 490 void setDevKernText(bool devKernText); 491 492 /** Returns SkFilterQuality, the image filtering level. A lower setting 493 draws faster; a higher setting looks better when the image is scaled. 494 495 @return one of: kNone_SkFilterQuality, kLow_SkFilterQuality, 496 kMedium_SkFilterQuality, kHigh_SkFilterQuality 497 */ 498 SkFilterQuality getFilterQuality() const { 499 return (SkFilterQuality)fBitfields.fFilterQuality; 500 } 501 502 /** Sets SkFilterQuality, the image filtering level. A lower setting 503 draws faster; a higher setting looks better when the image is scaled. 504 Does not check to see if quality is valid. 505 506 @param quality one of: kNone_SkFilterQuality, kLow_SkFilterQuality, 507 kMedium_SkFilterQuality, kHigh_SkFilterQuality 508 */ 509 void setFilterQuality(SkFilterQuality quality); 510 511 /** \enum SkPaint::Style 512 Set Style to fill, stroke, or both fill and stroke geometry. 513 The stroke and fill 514 share all paint attributes; for instance, they are drawn with the same color. 515 516 Use kStrokeAndFill_Style to avoid hitting the same pixels twice with a stroke draw and 517 a fill draw. 518 */ 519 enum Style { 520 /** Set to fill geometry. 521 Applies to SkRect, SkRegion, SkRRect, circles, ovals, SkPath, and text. 522 SkBitmap, SkImage, patches, SkRegion, sprites, and vertices are painted as if 523 kFill_Style is set, and ignore the set Style. 524 The FillType specifies additional rules to fill the area outside the path edge, 525 and to create an unfilled hole inside the shape. 526 Style is set to kFill_Style by default. 527 */ 528 kFill_Style, 529 530 /** Set to stroke geometry. 531 Applies to SkRect, SkRegion, SkRRect, arcs, circles, ovals, SkPath, and text. 532 Arcs, lines, and points, are always drawn as if kStroke_Style is set, 533 and ignore the set Style. 534 The stroke construction is unaffected by the FillType. 535 */ 536 kStroke_Style, 537 538 /** Set to stroke and fill geometry. 539 Applies to SkRect, SkRegion, SkRRect, circles, ovals, SkPath, and text. 540 SkPath is treated as if it is set to SkPath::kWinding_FillType, 541 and the set FillType is ignored. 542 */ 543 kStrokeAndFill_Style, 544 }; 545 546 enum { 547 /** The number of different Style values defined. 548 May be used to verify that Style is a legal value. 549 */ 550 kStyleCount = kStrokeAndFill_Style + 1, 551 }; 552 553 /** Whether the geometry is filled, stroked, or filled and stroked. 554 555 @return one of:kFill_Style, kStroke_Style, kStrokeAndFill_Style 556 */ 557 Style getStyle() const { return (Style)fBitfields.fStyle; } 558 559 /** Sets whether the geometry is filled, stroked, or filled and stroked. 560 Has no effect if style is not a legal SkPaint::Style value. 561 562 @param style one of: kFill_Style, kStroke_Style, kStrokeAndFill_Style 563 */ 564 void setStyle(Style style); 565 566 /** Retrieves alpha and RGB, unpremultiplied, packed into 32 bits. 567 Use helpers SkColorGetA(), SkColorGetR(), SkColorGetG(), and SkColorGetB() to extract 568 a color component. 569 570 @return unpremultiplied ARGB 571 */ 572 SkColor getColor() const { return fColor; } 573 574 /** Sets alpha and RGB used when stroking and filling. The color is a 32-bit value, 575 unpremultiplied, packing 8-bit components for alpha, red, blue, and green. 576 577 @param color unpremultiplied ARGB 578 */ 579 void setColor(SkColor color); 580 581 /** Retrieves alpha from the color used when stroking and filling. 582 583 @return alpha ranging from zero, fully transparent, to 255, fully opaque 584 */ 585 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); } 586 587 /** Replaces alpha, leaving RGB 588 unchanged. An out of range value triggers an assert in the debug 589 build. a is a value from zero to 255. 590 a set to zero makes color fully transparent; a set to 255 makes color 591 fully opaque. 592 593 @param a alpha component of color 594 */ 595 void setAlpha(U8CPU a); 596 597 /** Sets color used when drawing solid fills. The color components range from 0 to 255. 598 The color is unpremultiplied; alpha sets the transparency independent of RGB. 599 600 @param a amount of color alpha, from fully transparent (0) to fully opaque (255) 601 @param r amount of color rgb red, from no red (0) to full red (255) 602 @param g amount of color rgb green, from no green (0) to full green (255) 603 @param b amount of color rgb blue, from no blue (0) to full blue (255) 604 */ 605 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b); 606 607 /** Returns the thickness of the pen used by SkPaint to 608 outline the shape. 609 610 @return zero for hairline, greater than zero for pen thickness 611 */ 612 SkScalar getStrokeWidth() const { return fWidth; } 613 614 /** Sets the thickness of the pen used by the paint to 615 outline the shape. 616 Has no effect if width is less than zero. 617 618 @param width zero thickness for hairline; greater than zero for pen thickness 619 */ 620 void setStrokeWidth(SkScalar width); 621 622 /** The limit at which a sharp corner is drawn beveled. 623 624 @return zero and greater miter limit 625 */ 626 SkScalar getStrokeMiter() const { return fMiterLimit; } 627 628 /** The limit at which a sharp corner is drawn beveled. 629 Valid values are zero and greater. 630 Has no effect if miter is less than zero. 631 632 @param miter zero and greater miter limit 633 */ 634 void setStrokeMiter(SkScalar miter); 635 636 /** \enum SkPaint::Cap 637 Cap draws at the beginning and end of an open path contour. 638 */ 639 enum Cap { 640 kButt_Cap, //!< Does not extend the stroke past the beginning or the end. 641 642 /** Adds a circle with a diameter equal to stroke width at the beginning 643 and end. 644 */ 645 kRound_Cap, 646 647 /** Adds a square with sides equal to stroke width at the beginning 648 and end. The square sides are parallel to the initial and final direction 649 of the stroke. 650 */ 651 kSquare_Cap, 652 kLast_Cap = kSquare_Cap, //!< Equivalent to the largest value for Cap. 653 654 /** Equivalent to kButt_Cap. 655 Cap is set to kButt_Cap by default. 656 */ 657 kDefault_Cap = kButt_Cap, 658 }; 659 660 static constexpr int kCapCount = kLast_Cap + 1; 661 662 /** \enum SkPaint::Join 663 Join specifies how corners are drawn when a shape is stroked. Join 664 affects the four corners of a stroked rectangle, and the connected segments in a 665 stroked path. 666 667 Choose miter join to draw sharp corners. Choose round join to draw a circle with a 668 radius equal to the stroke width on top of the corner. Choose bevel join to minimally 669 connect the thick strokes. 670 671 The fill path constructed to describe the stroked path respects the join setting but may 672 not contain the actual join. For instance, a fill path constructed with round joins does 673 not necessarily include circles at each connected segment. 674 */ 675 enum Join { 676 /** Extends the outside corner to the extent allowed by miter limit. 677 If the extension exceeds miter limit, kBevel_Join is used instead. 678 */ 679 kMiter_Join, 680 681 /** Adds a circle with a diameter of stroke width at the sharp corner. */ 682 kRound_Join, 683 kBevel_Join, //!< Connects the outside edges of the sharp corner. 684 kLast_Join = kBevel_Join, //!< Equivalent to the largest value for Join. 685 686 /** Equivalent to kMiter_Join. 687 Join is set to kMiter_Join by default. 688 */ 689 kDefault_Join = kMiter_Join, 690 }; 691 692 static constexpr int kJoinCount = kLast_Join + 1; 693 694 /** The geometry drawn at the beginning and end of strokes. 695 696 @return one of: kButt_Cap, kRound_Cap, kSquare_Cap 697 */ 698 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; } 699 700 /** The geometry drawn at the beginning and end of strokes. 701 702 @param cap one of: kButt_Cap, kRound_Cap, kSquare_Cap; 703 has no effect if cap is not valid 704 */ 705 void setStrokeCap(Cap cap); 706 707 /** The geometry drawn at the corners of strokes. 708 709 @return one of: kMiter_Join, kRound_Join, kBevel_Join 710 */ 711 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; } 712 713 /** The geometry drawn at the corners of strokes. 714 715 @param join one of: kMiter_Join, kRound_Join, kBevel_Join; 716 otherwise, has no effect 717 */ 718 void setStrokeJoin(Join join); 719 720 /** The filled equivalent of the stroked path. 721 722 @param src SkPath read to create a filled version 723 @param dst resulting SkPath; may be the same as src, but may not be nullptr 724 @param cullRect optional limit passed to SkPathEffect 725 @param resScale if > 1, increase precision, else if (0 < res < 1) reduce precision 726 to favor speed and size 727 @return true if the path represents style fill, or false if it represents hairline 728 */ 729 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect, 730 SkScalar resScale = 1) const; 731 732 /** The filled equivalent of the stroked path. 733 734 Replaces dst with the src path modified by SkPathEffect and style stroke. 735 SkPathEffect, if any, is not culled. stroke width is created with default precision. 736 737 @param src SkPath read to create a filled version 738 @param dst resulting SkPath dst may be the same as src, but may not be nullptr 739 @return true if the path represents style fill, or false if it represents hairline 740 */ 741 bool getFillPath(const SkPath& src, SkPath* dst) const { 742 return this->getFillPath(src, dst, nullptr, 1); 743 } 744 745 /** Optional colors used when filling a path, such as a gradient. 746 747 Does not alter SkShader SkRefCnt. 748 749 @return SkShader if previously set, nullptr otherwise 750 */ 751 SkShader* getShader() const { return fShader.get(); } 752 753 /** Optional colors used when filling a path, such as a gradient. 754 755 Increases SkShader SkRefCnt by one. 756 757 @return SkShader if previously set, nullptr otherwise 758 */ 759 sk_sp<SkShader> refShader() const; 760 761 /** Optional colors used when filling a path, such as a gradient. 762 763 Sets SkShader to shader, decreasing SkRefCnt of the previous SkShader. 764 Increments shader SkRefCnt by one. 765 766 @param shader how geometry is filled with color; if nullptr, color is used instead 767 */ 768 void setShader(sk_sp<SkShader> shader); 769 770 /** Returns SkColorFilter if set, or nullptr. 771 Does not alter SkColorFilter SkRefCnt. 772 773 @return SkColorFilter if previously set, nullptr otherwise 774 */ 775 SkColorFilter* getColorFilter() const { return fColorFilter.get(); } 776 777 /** Returns SkColorFilter if set, or nullptr. 778 Increases SkColorFilter SkRefCnt by one. 779 780 @return SkColorFilter if set, or nullptr 781 */ 782 sk_sp<SkColorFilter> refColorFilter() const; 783 784 /** Sets SkColorFilter to filter, decreasing SkRefCnt of the previous 785 SkColorFilter. Pass nullptr to clear SkColorFilter. 786 787 Increments filter SkRefCnt by one. 788 789 @param colorFilter SkColorFilter to apply to subsequent draw 790 */ 791 void setColorFilter(sk_sp<SkColorFilter> colorFilter); 792 793 /** Returns SkBlendMode. 794 By default, returns SkBlendMode::kSrcOver. 795 796 @return mode used to combine source color with destination color 797 */ 798 SkBlendMode getBlendMode() const { return (SkBlendMode)fBlendMode; } 799 800 /** Returns true if SkBlendMode is SkBlendMode::kSrcOver, the default. 801 802 @return true if SkBlendMode is SkBlendMode::kSrcOver 803 */ 804 bool isSrcOver() const { return (SkBlendMode)fBlendMode == SkBlendMode::kSrcOver; } 805 806 /** Sets SkBlendMode to mode. 807 Does not check for valid input. 808 809 @param mode SkBlendMode used to combine source color and destination 810 */ 811 void setBlendMode(SkBlendMode mode) { fBlendMode = (unsigned)mode; } 812 813 /** Returns SkPathEffect if set, or nullptr. 814 Does not alter SkPathEffect SkRefCnt. 815 816 @return SkPathEffect if previously set, nullptr otherwise 817 */ 818 SkPathEffect* getPathEffect() const { return fPathEffect.get(); } 819 820 /** Returns SkPathEffect if set, or nullptr. 821 Increases SkPathEffect SkRefCnt by one. 822 823 @return SkPathEffect if previously set, nullptr otherwise 824 */ 825 sk_sp<SkPathEffect> refPathEffect() const; 826 827 /** Sets SkPathEffect to pathEffect, decreasing SkRefCnt of the previous 828 SkPathEffect. Pass nullptr to leave the path geometry unaltered. 829 830 Increments pathEffect SkRefCnt by one. 831 832 @param pathEffect replace SkPath with a modification when drawn 833 */ 834 void setPathEffect(sk_sp<SkPathEffect> pathEffect); 835 836 /** Returns SkMaskFilter if set, or nullptr. 837 Does not alter SkMaskFilter SkRefCnt. 838 839 @return SkMaskFilter if previously set, nullptr otherwise 840 */ 841 SkMaskFilter* getMaskFilter() const { return fMaskFilter.get(); } 842 843 /** Returns SkMaskFilter if set, or nullptr. 844 845 Increases SkMaskFilter SkRefCnt by one. 846 847 @return SkMaskFilter if previously set, nullptr otherwise 848 */ 849 sk_sp<SkMaskFilter> refMaskFilter() const; 850 851 /** Sets SkMaskFilter to maskFilter, decreasing SkRefCnt of the previous 852 SkMaskFilter. Pass nullptr to clear SkMaskFilter and leave SkMaskFilter effect on 853 mask alpha unaltered. 854 855 @param maskFilter modifies clipping mask generated from drawn geometry 856 */ 857 void setMaskFilter(sk_sp<SkMaskFilter> maskFilter); 858 859 /** Returns SkTypeface if set, or nullptr. 860 Increments SkTypeface SkRefCnt by one. 861 862 @return SkTypeface if previously set, nullptr otherwise 863 */ 864 SkTypeface* getTypeface() const { return fTypeface.get(); } 865 866 /** Increases SkTypeface SkRefCnt by one. 867 868 @return SkTypeface if previously set, nullptr otherwise 869 */ 870 sk_sp<SkTypeface> refTypeface() const; 871 872 /** Sets SkTypeface to typeface, decreasing SkRefCnt of the previous SkTypeface. 873 Pass nullptr to clear SkTypeface and use the default typeface. Increments 874 typeface SkRefCnt by one. 875 876 @param typeface font and style used to draw text 877 */ 878 void setTypeface(sk_sp<SkTypeface> typeface); 879 880 /** Returns SkImageFilter if set, or nullptr. 881 Does not alter SkImageFilter SkRefCnt. 882 883 @return SkImageFilter if previously set, nullptr otherwise 884 */ 885 SkImageFilter* getImageFilter() const { return fImageFilter.get(); } 886 887 /** Returns SkImageFilter if set, or nullptr. 888 Increases SkImageFilter SkRefCnt by one. 889 890 @return SkImageFilter if previously set, nullptr otherwise 891 */ 892 sk_sp<SkImageFilter> refImageFilter() const; 893 894 /** Sets SkImageFilter to imageFilter, decreasing SkRefCnt of the previous 895 SkImageFilter. Pass nullptr to clear SkImageFilter, and remove SkImageFilter effect 896 on drawing. 897 898 @param imageFilter how SkImage is sampled when transformed 899 */ 900 void setImageFilter(sk_sp<SkImageFilter> imageFilter); 901 902 /** Returns SkDrawLooper if set, or nullptr. 903 Does not alter SkDrawLooper SkRefCnt. 904 905 @return SkDrawLooper if previously set, nullptr otherwise 906 */ 907 SkDrawLooper* getDrawLooper() const { return fDrawLooper.get(); } 908 909 /** Returns SkDrawLooper if set, or nullptr. 910 Increases SkDrawLooper SkRefCnt by one. 911 912 @return SkDrawLooper if previously set, nullptr otherwise 913 */ 914 sk_sp<SkDrawLooper> refDrawLooper() const; 915 916 /** Deprecated. 917 (see bug.skia.org/6259) 918 919 @return SkDrawLooper if previously set, nullptr otherwise 920 */ 921 SkDrawLooper* getLooper() const { return fDrawLooper.get(); } 922 923 /** Sets SkDrawLooper to drawLooper, decreasing SkRefCnt of the previous 924 drawLooper. Pass nullptr to clear SkDrawLooper and leave SkDrawLooper effect on 925 drawing unaltered. 926 927 Increments drawLooper SkRefCnt by one. 928 929 @param drawLooper iterates through drawing one or more time, altering SkPaint 930 */ 931 void setDrawLooper(sk_sp<SkDrawLooper> drawLooper); 932 933 /** Deprecated. 934 (see bug.skia.org/6259) 935 936 @param drawLooper sets SkDrawLooper to drawLooper 937 */ 938 void setLooper(sk_sp<SkDrawLooper> drawLooper); 939 940 /** \enum SkPaint::Align 941 Align adjusts the text relative to the text position. 942 Align affects glyphs drawn with: SkCanvas::drawText, SkCanvas::drawPosText, 943 SkCanvas::drawPosTextH, SkCanvas::drawTextOnPath, 944 SkCanvas::drawTextOnPathHV, SkCanvas::drawTextRSXform, SkCanvas::drawTextBlob, 945 and SkCanvas::drawString; 946 as well as calls that place text glyphs like getTextWidths() and getTextPath(). 947 948 The text position is set by the font for both horizontal and vertical text. 949 Typically, for horizontal text, the position is to the left side of the glyph on the 950 base line; and for vertical text, the position is the horizontal center of the glyph 951 at the caps height. 952 953 Align adjusts the glyph position to center it or move it to abut the position 954 using the metrics returned by the font. 955 956 Align defaults to kLeft_Align. 957 */ 958 enum Align { 959 /** Leaves the glyph at the position computed by the font offset by the text position. */ 960 kLeft_Align, 961 962 /** Moves the glyph half its width if Flags has kVerticalText_Flag clear, and 963 half its height if Flags has kVerticalText_Flag set. 964 */ 965 kCenter_Align, 966 967 /** Moves the glyph by its width if Flags has kVerticalText_Flag clear, 968 and by its height if Flags has kVerticalText_Flag set. 969 */ 970 kRight_Align, 971 }; 972 973 enum { 974 kAlignCount = 3, //!< The number of different Align values defined. 975 }; 976 977 /** Returns SkPaint::Align. 978 Returns kLeft_Align if SkPaint::Align has not been set. 979 980 @return text placement relative to position 981 */ 982 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; } 983 984 /** Sets SkPaint::Align to align. 985 Has no effect if align is an invalid value. 986 987 @param align text placement relative to position 988 */ 989 void setTextAlign(Align align); 990 991 /** Returns text size in points. 992 993 @return typographic height of text 994 */ 995 SkScalar getTextSize() const { return fTextSize; } 996 997 /** Sets text size in points. 998 Has no effect if textSize is not greater than or equal to zero. 999 1000 @param textSize typographic height of text 1001 */ 1002 void setTextSize(SkScalar textSize); 1003 1004 /** Returns text scale x. 1005 Default value is 1. 1006 1007 @return text horizontal scale 1008 */ 1009 SkScalar getTextScaleX() const { return fTextScaleX; } 1010 1011 /** Sets text scale x. 1012 Default value is 1. 1013 1014 @param scaleX text horizontal scale 1015 */ 1016 void setTextScaleX(SkScalar scaleX); 1017 1018 /** Returns text skew x. 1019 Default value is zero. 1020 1021 @return additional shear in x-axis relative to y-axis 1022 */ 1023 SkScalar getTextSkewX() const { return fTextSkewX; } 1024 1025 /** Sets text skew x. 1026 Default value is zero. 1027 1028 @param skewX additional shear in x-axis relative to y-axis 1029 */ 1030 void setTextSkewX(SkScalar skewX); 1031 1032 /** \enum SkPaint::TextEncoding 1033 TextEncoding determines whether text specifies character codes and their encoded 1034 size, or glyph indices. Characters are encoded as specified by the Unicode standard. 1035 1036 Character codes encoded size are specified by UTF-8, UTF-16, or UTF-32. 1037 All character code formats are able to represent all of Unicode, differing only 1038 in the total storage required. 1039 1040 UTF-8 (RFC 3629) encodes each character as one or more 8-bit bytes. 1041 1042 UTF-16 (RFC 2781) encodes each character as one or two 16-bit words. 1043 1044 UTF-32 encodes each character as one 32-bit word. 1045 1046 font manager uses font data to convert character code points into glyph indices. 1047 A glyph index is a 16-bit word. 1048 1049 TextEncoding is set to kUTF8_TextEncoding by default. 1050 */ 1051 enum TextEncoding { 1052 kUTF8_TextEncoding, //!< Uses bytes to represent UTF-8 or ASCII. 1053 kUTF16_TextEncoding, //!< Uses two byte words to represent most of Unicode. 1054 kUTF32_TextEncoding, //!< Uses four byte words to represent all of Unicode. 1055 kGlyphID_TextEncoding, //!< Uses two byte words to represent glyph indices. 1056 }; 1057 1058 /** Returns SkPaint::TextEncoding. 1059 SkPaint::TextEncoding determines how character code points are mapped to font glyph indices. 1060 1061 @return one of: kUTF8_TextEncoding, kUTF16_TextEncoding, kUTF32_TextEncoding, or 1062 kGlyphID_TextEncoding 1063 */ 1064 TextEncoding getTextEncoding() const { 1065 return (TextEncoding)fBitfields.fTextEncoding; 1066 } 1067 1068 /** Sets SkPaint::TextEncoding to encoding. 1069 SkPaint::TextEncoding determines how character code points are mapped to font glyph indices. 1070 Invalid values for encoding are ignored. 1071 1072 @param encoding one of: kUTF8_TextEncoding, kUTF16_TextEncoding, kUTF32_TextEncoding, or 1073 kGlyphID_TextEncoding 1074 */ 1075 void setTextEncoding(TextEncoding encoding); 1076 1077 /** \struct SkPaint::FontMetrics 1078 FontMetrics is filled out by getFontMetrics(). FontMetrics contents reflect the values 1079 computed by font manager using SkTypeface. Values are set to zero if they are 1080 not available. 1081 1082 All vertical values relative to the baseline are given y-down. As such, zero is on the 1083 baseline, negative values are above the baseline, and positive values are below the 1084 baseline. 1085 1086 fUnderlineThickness and fUnderlinePosition have a bit set in fFlags if their values 1087 are valid, since their value may be zero. 1088 1089 fStrikeoutThickness and fStrikeoutPosition have a bit set in fFlags if their values 1090 are valid, since their value may be zero. 1091 */ 1092 struct FontMetrics { 1093 1094 /** \enum SkPaint::FontMetrics::FontMetricsFlags 1095 FontMetricsFlags are set in fFlags when underline and strikeout metrics are valid; 1096 the underline or strikeout metric may be valid and zero. 1097 Fonts with embedded bitmaps may not have valid underline or strikeout metrics. 1098 */ 1099 enum FontMetricsFlags { 1100 kUnderlineThicknessIsValid_Flag = 1 << 0, //!< Set if fUnderlineThickness is valid. 1101 kUnderlinePositionIsValid_Flag = 1 << 1, //!< Set if fUnderlinePosition is valid. 1102 kStrikeoutThicknessIsValid_Flag = 1 << 2, //!< Set if fStrikeoutThickness is valid. 1103 kStrikeoutPositionIsValid_Flag = 1 << 3, //!< Set if fStrikeoutPosition is valid. 1104 }; 1105 1106 uint32_t fFlags; //!< fFlags is set when underline metrics are valid. 1107 1108 /** Greatest extent above the baseline for any glyph. 1109 Typically less than zero. 1110 */ 1111 SkScalar fTop; 1112 1113 /** Recommended distance above the baseline to reserve for a line of text. 1114 Typically less than zero. 1115 */ 1116 SkScalar fAscent; 1117 1118 /** Recommended distance below the baseline to reserve for a line of text. 1119 Typically greater than zero. 1120 */ 1121 SkScalar fDescent; 1122 1123 /** Greatest extent below the baseline for any glyph. 1124 Typically greater than zero. 1125 */ 1126 SkScalar fBottom; 1127 1128 /** Recommended distance to add between lines of text. 1129 Typically greater than or equal to zero. 1130 */ 1131 SkScalar fLeading; 1132 1133 /** Average character width, if it is available. 1134 Zero if no average width is stored in the font. 1135 */ 1136 SkScalar fAvgCharWidth; 1137 1138 SkScalar fMaxCharWidth; //!< Maximum character width. 1139 1140 /** Minimum bounding box x value for all glyphs. 1141 Typically less than zero. 1142 */ 1143 SkScalar fXMin; 1144 1145 /** Maximum bounding box x value for all glyphs. 1146 Typically greater than zero. 1147 */ 1148 SkScalar fXMax; 1149 1150 /** Height of a lower-case 'x'. 1151 May be zero if no lower-case height is stored in the font. 1152 */ 1153 SkScalar fXHeight; 1154 1155 /** Height of an upper-case letter. 1156 May be zero if no upper-case height is stored in the font. 1157 */ 1158 SkScalar fCapHeight; 1159 1160 /** Underline thickness. 1161 1162 If the metric is valid, the kUnderlineThicknessIsValid_Flag is set in fFlags. 1163 If kUnderlineThicknessIsValid_Flag is clear, fUnderlineThickness is zero. 1164 */ 1165 SkScalar fUnderlineThickness; 1166 1167 /** Position of the top of the underline stroke relative to the baseline. 1168 Typically positive when valid. 1169 1170 If the metric is valid, the kUnderlinePositionIsValid_Flag is set in fFlags. 1171 If kUnderlinePositionIsValid_Flag is clear, fUnderlinePosition is zero. 1172 */ 1173 SkScalar fUnderlinePosition; 1174 1175 /** Strikeout thickness. 1176 1177 If the metric is valid, the kStrikeoutThicknessIsValid_Flag is set in fFlags. 1178 If kStrikeoutThicknessIsValid_Flag is clear, fStrikeoutThickness is zero. 1179 */ 1180 SkScalar fStrikeoutThickness; 1181 1182 /** Position of the bottom of the strikeout stroke relative to the baseline. 1183 Typically negative when valid. 1184 1185 If the metric is valid, the kStrikeoutPositionIsValid_Flag is set in fFlags. 1186 If kStrikeoutPositionIsValid_Flag is clear, fStrikeoutPosition is zero. 1187 */ 1188 SkScalar fStrikeoutPosition; 1189 1190 /** If SkPaint::FontMetrics has a valid underline thickness, return true, and set 1191 thickness to that value. If the underline thickness is not valid, 1192 return false, and ignore thickness. 1193 1194 @param thickness storage for underline width 1195 @return true if font specifies underline width 1196 */ 1197 bool hasUnderlineThickness(SkScalar* thickness) const { 1198 if (SkToBool(fFlags & kUnderlineThicknessIsValid_Flag)) { 1199 *thickness = fUnderlineThickness; 1200 return true; 1201 } 1202 return false; 1203 } 1204 1205 /** If SkPaint::FontMetrics has a valid underline position, return true, and set 1206 position to that value. If the underline position is not valid, 1207 return false, and ignore position. 1208 1209 @param position storage for underline position 1210 @return true if font specifies underline position 1211 */ 1212 bool hasUnderlinePosition(SkScalar* position) const { 1213 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) { 1214 *position = fUnderlinePosition; 1215 return true; 1216 } 1217 return false; 1218 } 1219 1220 /** If SkPaint::FontMetrics has a valid strikeout thickness, return true, and set 1221 thickness to that value. If the underline thickness is not valid, 1222 return false, and ignore thickness. 1223 1224 @param thickness storage for strikeout width 1225 @return true if font specifies strikeout width 1226 */ 1227 bool hasStrikeoutThickness(SkScalar* thickness) const { 1228 if (SkToBool(fFlags & kStrikeoutThicknessIsValid_Flag)) { 1229 *thickness = fStrikeoutThickness; 1230 return true; 1231 } 1232 return false; 1233 } 1234 1235 /** If SkPaint::FontMetrics has a valid strikeout position, return true, and set 1236 position to that value. If the underline position is not valid, 1237 return false, and ignore position. 1238 1239 @param position storage for strikeout position 1240 @return true if font specifies strikeout position 1241 */ 1242 bool hasStrikeoutPosition(SkScalar* position) const { 1243 if (SkToBool(fFlags & kStrikeoutPositionIsValid_Flag)) { 1244 *position = fStrikeoutPosition; 1245 return true; 1246 } 1247 return false; 1248 } 1249 1250 }; 1251 1252 /** Returns SkPaint::FontMetrics associated with SkTypeface. 1253 The return value is the recommended spacing between lines: the sum of metrics 1254 descent, ascent, and leading. 1255 If metrics is not nullptr, SkPaint::FontMetrics is copied to metrics. 1256 Results are scaled by text size but does not take into account 1257 dimensions required by text scale x, text skew x, fake bold, 1258 style stroke, and SkPathEffect. 1259 Results can be additionally scaled by scale; a scale of zero 1260 is ignored. 1261 1262 @param metrics storage for SkPaint::FontMetrics from SkTypeface; may be nullptr 1263 @param scale additional multiplier for returned values 1264 @return recommended spacing between lines 1265 */ 1266 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const; 1267 1268 /** Returns the recommended spacing between lines: the sum of metrics 1269 descent, ascent, and leading. 1270 Result is scaled by text size but does not take into account 1271 dimensions required by stroking and SkPathEffect. 1272 Returns the same result as getFontMetrics(). 1273 1274 @return recommended spacing between lines 1275 */ 1276 SkScalar getFontSpacing() const { return this->getFontMetrics(nullptr, 0); } 1277 1278 /** Converts text into glyph indices. 1279 Returns the number of glyph indices represented by text. 1280 SkPaint::TextEncoding specifies how text represents characters or glyphs. 1281 glyphs may be nullptr, to compute the glyph count. 1282 1283 Does not check text for valid character codes or valid glyph indices. 1284 1285 If byteLength equals zero, returns zero. 1286 If byteLength includes a partial character, the partial character is ignored. 1287 1288 If SkPaint::TextEncoding is kUTF8_TextEncoding and 1289 text contains an invalid UTF-8 sequence, zero is returned. 1290 1291 @param text character storage encoded with SkPaint::TextEncoding 1292 @param byteLength length of character storage in bytes 1293 @param glyphs storage for glyph indices; may be nullptr 1294 @return number of glyphs represented by text of length byteLength 1295 */ 1296 int textToGlyphs(const void* text, size_t byteLength, 1297 SkGlyphID glyphs[]) const; 1298 1299 /** Returns true if all text corresponds to a non-zero glyph index. 1300 Returns false if any characters in text are not supported in 1301 SkTypeface. 1302 1303 If SkPaint::TextEncoding is kGlyphID_TextEncoding, 1304 returns true if all glyph indices in text are non-zero; 1305 does not check to see if text contains valid glyph indices for SkTypeface. 1306 1307 Returns true if byteLength is zero. 1308 1309 @param text array of characters or glyphs 1310 @param byteLength number of bytes in text array 1311 @return true if all text corresponds to a non-zero glyph index 1312 */ 1313 bool containsText(const void* text, size_t byteLength) const; 1314 1315 /** Converts glyphs into text if possible. 1316 Glyph values without direct Unicode equivalents are mapped to zero. 1317 Uses the SkTypeface, but is unaffected 1318 by SkPaint::TextEncoding; the text values returned are equivalent to kUTF32_TextEncoding. 1319 1320 Only supported on platforms that use FreeType as the font engine. 1321 1322 @param glyphs array of indices into font 1323 @param count length of glyph array 1324 @param text storage for character codes, one per glyph 1325 */ 1326 void glyphsToUnichars(const SkGlyphID glyphs[], int count, SkUnichar text[]) const; 1327 1328 /** Returns the number of glyphs in text. 1329 Uses SkPaint::TextEncoding to count the glyphs. 1330 Returns the same result as textToGlyphs(). 1331 1332 @param text character storage encoded with SkPaint::TextEncoding 1333 @param byteLength length of character storage in bytes 1334 @return number of glyphs represented by text of length byteLength 1335 */ 1336 int countText(const void* text, size_t byteLength) const { 1337 return this->textToGlyphs(text, byteLength, nullptr); 1338 } 1339 1340 /** Returns the advance width of text if kVerticalText_Flag is clear, 1341 and the height of text if kVerticalText_Flag is set. 1342 The advance is the normal distance to move before drawing additional text. 1343 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the font metrics, 1344 and text size, text scale x, text skew x, stroke width, and 1345 SkPathEffect to scale the metrics and bounds. 1346 Returns the bounding box of text if bounds is not nullptr. 1347 The bounding box is computed as if the text was drawn at the origin. 1348 1349 @param text character codes or glyph indices to be measured 1350 @param length number of bytes of text to measure 1351 @param bounds returns bounding box relative to (0, 0) if not nullptr 1352 @return advance width or height 1353 */ 1354 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const; 1355 1356 /** Returns the advance width of text if kVerticalText_Flag is clear, 1357 and the height of text if kVerticalText_Flag is set. 1358 The advance is the normal distance to move before drawing additional text. 1359 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the font metrics, 1360 and text size to scale the metrics. 1361 Does not scale the advance or bounds by fake bold or SkPathEffect. 1362 1363 @param text character codes or glyph indices to be measured 1364 @param length number of bytes of text to measure 1365 @return advance width or height 1366 */ 1367 SkScalar measureText(const void* text, size_t length) const { 1368 return this->measureText(text, length, nullptr); 1369 } 1370 1371 /** Returns the bytes of text that fit within maxWidth. 1372 If kVerticalText_Flag is clear, the text fragment fits if its advance width is less than or 1373 equal to maxWidth. 1374 If kVerticalText_Flag is set, the text fragment fits if its advance height is less than or 1375 equal to maxWidth. 1376 Measures only while the advance is less than or equal to maxWidth. 1377 Returns the advance or the text fragment in measuredWidth if it not nullptr. 1378 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the font metrics, 1379 and text size to scale the metrics. 1380 Does not scale the advance or bounds by fake bold or SkPathEffect. 1381 1382 @param text character codes or glyph indices to be measured 1383 @param length number of bytes of text to measure 1384 @param maxWidth advance limit; text is measured while advance is less than maxWidth 1385 @param measuredWidth returns the width of the text less than or equal to maxWidth 1386 @return bytes of text that fit, always less than or equal to length 1387 */ 1388 size_t breakText(const void* text, size_t length, SkScalar maxWidth, 1389 SkScalar* measuredWidth = nullptr) const; 1390 1391 /** Retrieves the advance and bounds for each glyph in text, and returns 1392 the glyph count in text. 1393 Both widths and bounds may be nullptr. 1394 If widths is not nullptr, widths must be an array of glyph count entries. 1395 if bounds is not nullptr, bounds must be an array of glyph count entries. 1396 If kVerticalText_Flag is clear, widths returns the horizontal advance. 1397 If kVerticalText_Flag is set, widths returns the vertical advance. 1398 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the font metrics, 1399 and text size to scale the widths and bounds. 1400 Does not scale the advance by fake bold or SkPathEffect. 1401 Does include fake bold and SkPathEffect in the bounds. 1402 1403 @param text character codes or glyph indices to be measured 1404 @param byteLength number of bytes of text to measure 1405 @param widths returns text advances for each glyph; may be nullptr 1406 @param bounds returns bounds for each glyph relative to (0, 0); may be nullptr 1407 @return glyph count in text 1408 */ 1409 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[], 1410 SkRect bounds[] = nullptr) const; 1411 1412 /** Returns the geometry as SkPath equivalent to the drawn text. 1413 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths, 1414 and text size, fake bold, and SkPathEffect to scale and modify the glyph paths. 1415 All of the glyph paths are stored in path. 1416 Uses x, y, and SkPaint::Align to position path. 1417 1418 @param text character codes or glyph indices 1419 @param length number of bytes of text 1420 @param x x-coordinate of the origin of the text 1421 @param y y-coordinate of the origin of the text 1422 @param path geometry of the glyphs 1423 */ 1424 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y, 1425 SkPath* path) const; 1426 1427 /** Returns the geometry as SkPath equivalent to the drawn text. 1428 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths, 1429 and text size, fake bold, and SkPathEffect to scale and modify the glyph paths. 1430 All of the glyph paths are stored in path. 1431 Uses pos array and SkPaint::Align to position path. 1432 pos contains a position for each glyph. 1433 1434 @param text character codes or glyph indices 1435 @param length number of bytes of text 1436 @param pos positions of each glyph 1437 @param path geometry of the glyphs 1438 */ 1439 void getPosTextPath(const void* text, size_t length, 1440 const SkPoint pos[], SkPath* path) const; 1441 1442 /** Returns the number of intervals that intersect bounds. 1443 bounds describes a pair of lines parallel to the text advance. 1444 The return count is zero or a multiple of two, and is at most twice the number of glyphs in 1445 the string. 1446 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths, 1447 and text size, fake bold, and SkPathEffect to scale and modify the glyph paths. 1448 Uses x, y, and SkPaint::Align to position intervals. 1449 1450 Pass nullptr for intervals to determine the size of the interval array. 1451 1452 intervals are cached to improve performance for multiple calls. 1453 1454 @param text character codes or glyph indices 1455 @param length number of bytes of text 1456 @param x x-coordinate of the origin of the text 1457 @param y y-coordinate of the origin of the text 1458 @param bounds lower and upper line parallel to the advance 1459 @param intervals returned intersections; may be nullptr 1460 @return number of intersections; may be zero 1461 */ 1462 int getTextIntercepts(const void* text, size_t length, SkScalar x, SkScalar y, 1463 const SkScalar bounds[2], SkScalar* intervals) const; 1464 1465 /** Returns the number of intervals that intersect bounds. 1466 bounds describes a pair of lines parallel to the text advance. 1467 The return count is zero or a multiple of two, and is at most twice the number of glyphs in 1468 the string. 1469 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths, 1470 and text size, fake bold, and SkPathEffect to scale and modify the glyph paths. 1471 Uses pos array and SkPaint::Align to position intervals. 1472 1473 Pass nullptr for intervals to determine the size of the interval array. 1474 1475 intervals are cached to improve performance for multiple calls. 1476 1477 @param text character codes or glyph indices 1478 @param length number of bytes of text 1479 @param pos positions of each glyph 1480 @param bounds lower and upper line parallel to the advance 1481 @param intervals returned intersections; may be nullptr 1482 @return number of intersections; may be zero 1483 */ 1484 int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[], 1485 const SkScalar bounds[2], SkScalar* intervals) const; 1486 1487 /** Returns the number of intervals that intersect bounds. 1488 bounds describes a pair of lines parallel to the text advance. 1489 The return count is zero or a multiple of two, and is at most twice the number of glyphs in 1490 the string. 1491 Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths, 1492 and text size, fake bold, and SkPathEffect to scale and modify the glyph paths. 1493 Uses xpos array, constY, and SkPaint::Align to position intervals. 1494 1495 Pass nullptr for intervals to determine the size of the interval array. 1496 1497 intervals are cached to improve performance for multiple calls. 1498 1499 @param text character codes or glyph indices 1500 @param length number of bytes of text 1501 @param xpos positions of each glyph in x 1502 @param constY position of each glyph in y 1503 @param bounds lower and upper line parallel to the advance 1504 @param intervals returned intersections; may be nullptr 1505 @return number of intersections; may be zero 1506 */ 1507 int getPosTextHIntercepts(const void* text, size_t length, const SkScalar xpos[], 1508 SkScalar constY, const SkScalar bounds[2], SkScalar* intervals) const; 1509 1510 /** Returns the number of intervals that intersect bounds. 1511 bounds describes a pair of lines parallel to the text advance. 1512 The return count is zero or a multiple of two, and is at most twice the number of glyphs in 1513 the string. 1514 Uses SkTypeface to get the glyph paths, 1515 and text size, fake bold, and SkPathEffect to scale and modify the glyph paths. 1516 Uses run array and SkPaint::Align to position intervals. 1517 1518 SkPaint::TextEncoding must be set to SkPaint::kGlyphID_TextEncoding. 1519 1520 Pass nullptr for intervals to determine the size of the interval array. 1521 1522 intervals are cached to improve performance for multiple calls. 1523 1524 @param blob glyphs, positions, and text paint attributes 1525 @param bounds lower and upper line parallel to the advance 1526 @param intervals returned intersections; may be nullptr 1527 @return number of intersections; may be zero 1528 */ 1529 int getTextBlobIntercepts(const SkTextBlob* blob, const SkScalar bounds[2], 1530 SkScalar* intervals) const; 1531 1532 /** Returns the union of bounds of all glyphs. 1533 Returned dimensions are computed by font manager from font data, 1534 ignoring SkPaint::Hinting. Includes text size, text scale x, 1535 and text skew x, but not fake bold or SkPathEffect. 1536 1537 If text size is large, text scale x is one, and text skew x is zero, 1538 returns the same bounds as SkPaint::FontMetrics { FontMetrics::fXMin, 1539 FontMetrics::fTop, FontMetrics::fXMax, FontMetrics::fBottom }. 1540 1541 @return union of bounds of all glyphs 1542 */ 1543 SkRect getFontBounds() const; 1544 1545 /** Returns true if SkPaint prevents all drawing; 1546 otherwise, the SkPaint may or may not allow drawing. 1547 1548 Returns true if, for example, SkBlendMode combined with color alpha computes a 1549 new alpha of zero. 1550 1551 @return true if SkPaint prevents all drawing 1552 */ 1553 bool nothingToDraw() const; 1554 1555 /** (to be made private) 1556 Returns true if SkPaint does not include elements requiring extensive computation 1557 to compute SkBaseDevice bounds of drawn geometry. For instance, SkPaint with SkPathEffect 1558 always returns false. 1559 1560 @return true if SkPaint allows for fast computation of bounds 1561 */ 1562 bool canComputeFastBounds() const; 1563 1564 /** (to be made private) 1565 Only call this if canComputeFastBounds() returned true. This takes a 1566 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic 1567 effects in the paint (e.g. stroking). If needed, it uses the storage 1568 parameter. It returns the adjusted bounds that can then be used 1569 for SkCanvas::quickReject tests. 1570 1571 The returned SkRect will either be orig or storage, thus the caller 1572 should not rely on storage being set to the result, but should always 1573 use the returned value. It is legal for orig and storage to be the same 1574 SkRect. 1575 e.g. 1576 if (paint.canComputeFastBounds()) { 1577 SkRect r, storage; 1578 path.computeBounds(&r, SkPath::kFast_BoundsType); 1579 const SkRect& fastR = paint.computeFastBounds(r, &storage); 1580 if (canvas->quickReject(fastR, ...)) { 1581 // don't draw the path 1582 } 1583 } 1584 1585 @param orig geometry modified by SkPaint when drawn 1586 @param storage computed bounds of geometry; may not be nullptr 1587 @return fast computed bounds 1588 */ 1589 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const { 1590 // Things like stroking, etc... will do math on the bounds rect, assuming that it's sorted. 1591 SkASSERT(orig.isSorted()); 1592 SkPaint::Style style = this->getStyle(); 1593 // ultra fast-case: filling with no effects that affect geometry 1594 if (kFill_Style == style) { 1595 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper()); 1596 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter()); 1597 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect()); 1598 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter()); 1599 if (!effects) { 1600 return orig; 1601 } 1602 } 1603 1604 return this->doComputeFastBounds(orig, storage, style); 1605 } 1606 1607 /** (to be made private) 1608 1609 @param orig geometry modified by SkPaint when drawn 1610 @param storage computed bounds of geometry 1611 @return fast computed bounds 1612 */ 1613 const SkRect& computeFastStrokeBounds(const SkRect& orig, 1614 SkRect* storage) const { 1615 return this->doComputeFastBounds(orig, storage, kStroke_Style); 1616 } 1617 1618 /** (to be made private) 1619 Computes the bounds, overriding the SkPaint SkPaint::Style. This can be used to 1620 account for additional width required by stroking orig, without 1621 altering SkPaint::Style set to fill. 1622 1623 @param orig geometry modified by SkPaint when drawn 1624 @param storage computed bounds of geometry 1625 @param style overrides SkPaint::Style 1626 @return fast computed bounds 1627 */ 1628 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage, 1629 Style style) const; 1630 1631 /** macro expands to: void toString(SkString* str) const; 1632 Creates string representation of SkPaint. The representation is read by 1633 internal debugging tools. The interface and implementation may be 1634 suppressed by defining SK_IGNORE_TO_STRING. 1635 1636 @param str storage for string representation of SkPaint 1637 */ 1638 SK_TO_STRING_NONVIRT() 1639 1640private: 1641 typedef const SkGlyph& (*GlyphCacheProc)(SkGlyphCache*, const char**); 1642 1643 sk_sp<SkTypeface> fTypeface; 1644 sk_sp<SkPathEffect> fPathEffect; 1645 sk_sp<SkShader> fShader; 1646 sk_sp<SkMaskFilter> fMaskFilter; 1647 sk_sp<SkColorFilter> fColorFilter; 1648 sk_sp<SkDrawLooper> fDrawLooper; 1649 sk_sp<SkImageFilter> fImageFilter; 1650 1651 SkScalar fTextSize; 1652 SkScalar fTextScaleX; 1653 SkScalar fTextSkewX; 1654 SkColor fColor; 1655 SkScalar fWidth; 1656 SkScalar fMiterLimit; 1657 uint32_t fBlendMode; // just need 5-6 bits 1658 union { 1659 struct { 1660 // all of these bitfields should add up to 32 1661 unsigned fFlags : 16; 1662 unsigned fTextAlign : 2; 1663 unsigned fCapType : 2; 1664 unsigned fJoinType : 2; 1665 unsigned fStyle : 2; 1666 unsigned fTextEncoding : 2; // 3 values 1667 unsigned fHinting : 2; 1668 unsigned fFilterQuality : 2; 1669 //unsigned fFreeBits : 2; 1670 } fBitfields; 1671 uint32_t fBitfieldsUInt; 1672 }; 1673 1674 static GlyphCacheProc GetGlyphCacheProc(TextEncoding encoding, 1675 bool isDevKern, 1676 bool needFullMetrics); 1677 1678 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length, 1679 int* count, SkRect* bounds) const; 1680 1681 /* 1682 * The luminance color is used to determine which Gamma Canonical color to map to. This is 1683 * really only used by backends which want to cache glyph masks, and need some way to know if 1684 * they need to generate new masks based off a given color. 1685 */ 1686 SkColor computeLuminanceColor() const; 1687 1688 enum { 1689 /* This is the size we use when we ask for a glyph's path. We then 1690 * post-transform it as we draw to match the request. 1691 * This is done to try to re-use cache entries for the path. 1692 * 1693 * This value is somewhat arbitrary. In theory, it could be 1, since 1694 * we store paths as floats. However, we get the path from the font 1695 * scaler, and it may represent its paths as fixed-point (or 26.6), 1696 * so we shouldn't ask for something too big (might overflow 16.16) 1697 * or too small (underflow 26.6). 1698 * 1699 * This value could track kMaxSizeForGlyphCache, assuming the above 1700 * constraints, but since we ask for unhinted paths, the two values 1701 * need not match per-se. 1702 */ 1703 kCanonicalTextSizeForPaths = 64, 1704 }; 1705 1706 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM, SkScalar maxLimit); 1707 1708 // Set flags/hinting/textSize up to use for drawing text as paths. 1709 // Returns scale factor to restore the original textSize, since will will 1710 // have change it to kCanonicalTextSizeForPaths. 1711 SkScalar setupForAsPaths(); 1712 1713 static SkScalar MaxCacheSize2(SkScalar maxLimit); 1714 1715 friend class GrAtlasTextBlob; 1716 friend class GrAtlasTextContext; 1717 friend class GrGLPathRendering; 1718 friend class GrPathRendering; 1719 friend class GrStencilAndCoverTextContext; 1720 friend class GrTextUtils; 1721 friend class SkAutoGlyphCache; 1722 friend class SkAutoGlyphCacheNoGamma; 1723 friend class SkCanonicalizePaint; 1724 friend class SkCanvas; 1725 friend class SkDraw; 1726 friend class SkPDFDevice; 1727 friend class SkScalerContext; // for computeLuminanceColor() 1728 friend class SkTextBaseIter; 1729}; 1730 1731#endif 1732