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