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