blob: 5e3e26ba7debf1c0b8758ac53cd0842d25794abb [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkPaint_DEFINED
11#define SkPaint_DEFINED
12
13#include "SkColor.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000014#include "SkXfermode.h"
15
reed@android.com8a1c16f2008-12-17 15:59:43 +000016class SkAutoGlyphCache;
17class SkColorFilter;
18class SkDescriptor;
19class SkFlattenableReadBuffer;
20class SkFlattenableWriteBuffer;
21struct SkGlyph;
22struct SkRect;
23class SkGlyphCache;
24class SkMaskFilter;
25class SkMatrix;
26class SkPath;
27class SkPathEffect;
28class SkRasterizer;
29class SkShader;
30class SkDrawLooper;
31class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
33typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
34 SkFixed x, SkFixed y);
35
36typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
37
38/** \class SkPaint
39
40 The SkPaint class holds the style and color information about how to draw
41 geometries, text and bitmaps.
42*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000043class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044public:
45 SkPaint();
46 SkPaint(const SkPaint& paint);
47 ~SkPaint();
48
49 SkPaint& operator=(const SkPaint&);
50
reed@google.comb530ef52011-07-20 19:55:42 +000051 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
52 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 return !(a == b);
54 }
reed@google.com9d07fec2011-03-16 20:02:59 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 void flatten(SkFlattenableWriteBuffer&) const;
57 void unflatten(SkFlattenableReadBuffer&);
58
59 /** Restores the paint to its initial settings.
60 */
61 void reset();
62
agl@chromium.org309485b2009-07-21 17:41:32 +000063 /** Specifies the level of hinting to be performed. These names are taken
64 from the Gnome/Cairo names for the same. They are translated into
65 Freetype concepts the same as in cairo-ft-font.c:
66 kNo_Hinting -> FT_LOAD_NO_HINTING
67 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
68 kNormal_Hinting -> <default, no option>
69 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
70 subpixel glyphs, in which case TARGET_LCD or
71 TARGET_LCD_V is used>
72 */
73 enum Hinting {
74 kNo_Hinting = 0,
75 kSlight_Hinting = 1,
76 kNormal_Hinting = 2, //!< this is the default
77 kFull_Hinting = 3,
78 };
79
reed@google.com9d07fec2011-03-16 20:02:59 +000080 Hinting getHinting() const {
agl@chromium.org309485b2009-07-21 17:41:32 +000081 return static_cast<Hinting>(fHinting);
82 }
83
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +000084 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +000085
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 /** Specifies the bit values that are stored in the paint's flags.
87 */
88 enum Flags {
89 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
90 kFilterBitmap_Flag = 0x02, //!< mask to enable bitmap filtering
91 kDither_Flag = 0x04, //!< mask to enable dithering
92 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
93 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
94 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
95 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +000096 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +000098 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +000099 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000100 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com32538982011-09-30 20:57:05 +0000101
102 // experimental/private
103 kForceAAText_Flag = 0x1000,
104
agl@chromium.org309485b2009-07-21 17:41:32 +0000105 // when adding extra flags, note that the fFlags member is specified
106 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107
reed@google.com32538982011-09-30 20:57:05 +0000108 kAllFlags = 0x1FFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 };
110
111 /** Return the paint's flags. Use the Flag enum to test flag values.
112 @return the paint's flags (see enums ending in _Flag for bit masks)
113 */
114 uint32_t getFlags() const { return fFlags; }
115
116 /** Set the paint's flags. Use the Flag enum to specific flag values.
117 @param flags The new flag bits for the paint (see Flags enum)
118 */
119 void setFlags(uint32_t flags);
120
121 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
122 @return true if the antialias bit is set in the paint's flags.
123 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000124 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 return SkToBool(this->getFlags() & kAntiAlias_Flag);
126 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
129 @param aa true to enable antialiasing, false to disable it
130 */
131 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 /** Helper for getFlags(), returning true if kDither_Flag bit is set
134 @return true if the dithering bit is set in the paint's flags.
135 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000136 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 return SkToBool(this->getFlags() & kDither_Flag);
138 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
141 @param dither true to enable dithering, false to disable it
142 */
143 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
146 @return true if the lineartext bit is set in the paint's flags
147 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000148 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 return SkToBool(this->getFlags() & kLinearText_Flag);
150 }
151
152 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
153 @param linearText true to set the linearText bit in the paint's flags,
154 false to clear it.
155 */
156 void setLinearText(bool linearText);
157
158 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
159 @return true if the lineartext bit is set in the paint's flags
160 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000161 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 return SkToBool(this->getFlags() & kSubpixelText_Flag);
163 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000164
reed@google.com84b437e2011-08-01 12:45:35 +0000165 /**
166 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
167 * @param subpixelText true to set the subpixelText bit in the paint's
168 * flags, false to clear it.
169 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000171
reed@google.com9d07fec2011-03-16 20:02:59 +0000172 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000173 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
174 }
175
reed@google.com84b437e2011-08-01 12:45:35 +0000176 /**
177 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
178 * Note: antialiasing must also be on for lcd rendering
179 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
180 * false to clear it.
181 */
182 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000183
reed@google.com9d07fec2011-03-16 20:02:59 +0000184 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000185 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
186 }
187
188 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
189 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
190 false to clear it.
191 */
192 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
193
reed@google.com9d07fec2011-03-16 20:02:59 +0000194 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000195 return SkToBool(this->getFlags() & kAutoHinting_Flag);
196 }
197
198 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
199 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
200 paint's flags,
201 false to clear it.
202 */
203 void setAutohinted(bool useAutohinter);
204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
206 @return true if the underlineText bit is set in the paint's flags.
207 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000208 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 return SkToBool(this->getFlags() & kUnderlineText_Flag);
210 }
211
212 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
213 @param underlineText true to set the underlineText bit in the paint's
214 flags, false to clear it.
215 */
216 void setUnderlineText(bool underlineText);
217
218 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
219 @return true if the strikeThruText bit is set in the paint's flags.
220 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000221 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
223 }
224
225 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
226 @param strikeThruText true to set the strikeThruText bit in the
227 paint's flags, false to clear it.
228 */
229 void setStrikeThruText(bool strikeThruText);
230
231 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
232 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
233 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000234 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
236 }
237
238 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
239 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
240 flags, false to clear it.
241 */
242 void setFakeBoldText(bool fakeBoldText);
243
244 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
245 @return true if the kernText bit is set in the paint's flags.
246 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000247 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 return SkToBool(this->getFlags() & kDevKernText_Flag);
249 }
250
251 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
252 @param kernText true to set the kKernText_Flag bit in the paint's
253 flags, false to clear it.
254 */
255 void setDevKernText(bool devKernText);
256
reed@google.com9d07fec2011-03-16 20:02:59 +0000257 bool isFilterBitmap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 return SkToBool(this->getFlags() & kFilterBitmap_Flag);
259 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000260
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261 void setFilterBitmap(bool filterBitmap);
262
263 /** Styles apply to rect, oval, path, and text.
264 Bitmaps are always drawn in "fill", and lines are always drawn in
265 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000266
reed@android.comed881c22009-09-15 14:10:42 +0000267 Note: strokeandfill implicitly draws the result with
268 SkPath::kWinding_FillType, so if the original path is even-odd, the
269 results may not appear the same as if it was drawn twice, filled and
270 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 */
272 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000273 kFill_Style, //!< fill the geometry
274 kStroke_Style, //!< stroke the geometry
275 kStrokeAndFill_Style, //!< fill and stroke the geometry
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276
277 kStyleCount,
278 };
279
280 /** Return the paint's style, used for controlling how primitives'
281 geometries are interpreted (except for drawBitmap, which always assumes
282 kFill_Style).
283 @return the paint's Style
284 */
285 Style getStyle() const { return (Style)fStyle; }
286
287 /** Set the paint's style, used for controlling how primitives'
288 geometries are interpreted (except for drawBitmap, which always assumes
289 Fill).
290 @param style The new style to set in the paint
291 */
292 void setStyle(Style style);
293
294 /** Return the paint's color. Note that the color is a 32bit value
295 containing alpha as well as r,g,b. This 32bit value is not
296 premultiplied, meaning that its alpha can be any value, regardless of
297 the values of r,g,b.
298 @return the paint's color (and alpha).
299 */
300 SkColor getColor() const { return fColor; }
301
302 /** Set the paint's color. Note that the color is a 32bit value containing
303 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
304 that its alpha can be any value, regardless of the values of r,g,b.
305 @param color The new color (including alpha) to set in the paint.
306 */
307 void setColor(SkColor color);
308
309 /** Helper to getColor() that just returns the color's alpha value.
310 @return the alpha component of the paint's color.
311 */
312 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000313
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 /** Helper to setColor(), that only assigns the color's alpha value,
315 leaving its r,g,b values unchanged.
316 @param a set the alpha component (0..255) of the paint's color.
317 */
318 void setAlpha(U8CPU a);
319
320 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
321 using SkColorSetARGB()
322 @param a The new alpha component (0..255) of the paint's color.
323 @param r The new red component (0..255) of the paint's color.
324 @param g The new green component (0..255) of the paint's color.
325 @param b The new blue component (0..255) of the paint's color.
326 */
327 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
328
reed@google.com9d07fec2011-03-16 20:02:59 +0000329 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 <p />
331 A value of 0 strokes in hairline mode.
332 Hairlines always draw 1-pixel wide, regardless of the matrix.
333 @return the paint's stroke width, used whenever the paint's style is
334 Stroke or StrokeAndFill.
335 */
336 SkScalar getStrokeWidth() const { return fWidth; }
337
reed@google.com9d07fec2011-03-16 20:02:59 +0000338 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 Pass 0 to stroke in hairline mode.
340 Hairlines always draw 1-pixel wide, regardless of the matrix.
341 @param width set the paint's stroke width, used whenever the paint's
342 style is Stroke or StrokeAndFill.
343 */
344 void setStrokeWidth(SkScalar width);
345
346 /** Return the paint's stroke miter value. This is used to control the
347 behavior of miter joins when the joins angle is sharp.
348 @return the paint's miter limit, used whenever the paint's style is
349 Stroke or StrokeAndFill.
350 */
351 SkScalar getStrokeMiter() const { return fMiterLimit; }
352
353 /** Set the paint's stroke miter value. This is used to control the
354 behavior of miter joins when the joins angle is sharp. This value must
355 be >= 0.
356 @param miter set the miter limit on the paint, used whenever the
357 paint's style is Stroke or StrokeAndFill.
358 */
359 void setStrokeMiter(SkScalar miter);
360
361 /** Cap enum specifies the settings for the paint's strokecap. This is the
362 treatment that is applied to the beginning and end of each non-closed
363 contour (e.g. lines).
364 */
365 enum Cap {
366 kButt_Cap, //!< begin/end contours with no extension
367 kRound_Cap, //!< begin/end contours with a semi-circle extension
368 kSquare_Cap, //!< begin/end contours with a half square extension
369
370 kCapCount,
371 kDefault_Cap = kButt_Cap
372 };
373
374 /** Join enum specifies the settings for the paint's strokejoin. This is
375 the treatment that is applied to corners in paths and rectangles.
376 */
377 enum Join {
378 kMiter_Join, //!< connect path segments with a sharp join
379 kRound_Join, //!< connect path segments with a round join
380 kBevel_Join, //!< connect path segments with a flat bevel join
381
382 kJoinCount,
383 kDefault_Join = kMiter_Join
384 };
385
386 /** Return the paint's stroke cap type, controlling how the start and end
387 of stroked lines and paths are treated.
388 @return the line cap style for the paint, used whenever the paint's
389 style is Stroke or StrokeAndFill.
390 */
391 Cap getStrokeCap() const { return (Cap)fCapType; }
392
393 /** Set the paint's stroke cap type.
394 @param cap set the paint's line cap style, used whenever the paint's
395 style is Stroke or StrokeAndFill.
396 */
397 void setStrokeCap(Cap cap);
398
399 /** Return the paint's stroke join type.
400 @return the paint's line join style, used whenever the paint's style is
401 Stroke or StrokeAndFill.
402 */
403 Join getStrokeJoin() const { return (Join)fJoinType; }
404
405 /** Set the paint's stroke join type.
406 @param join set the paint's line join style, used whenever the paint's
407 style is Stroke or StrokeAndFill.
408 */
409 void setStrokeJoin(Join join);
410
411 /** Applies any/all effects (patheffect, stroking) to src, returning the
412 result in dst. The result is that drawing src with this paint will be
413 the same as drawing dst with a default paint (at least from the
414 geometric perspective).
415 @param src input path
416 @param dst output path (may be the same as src)
417 @return true if the path should be filled, or false if it should be
418 drawn with a hairline (width == 0)
419 */
420 bool getFillPath(const SkPath& src, SkPath* dst) const;
421
422 /** Returns true if the current paint settings allow for fast computation of
423 bounds (i.e. there is nothing complex like a patheffect that would make
424 the bounds computation expensive.
425 */
reed@android.comd252db02009-04-01 18:31:44 +0000426 bool canComputeFastBounds() const {
427 // use bit-or since no need for early exit
428 return (reinterpret_cast<uintptr_t>(this->getMaskFilter()) |
429 reinterpret_cast<uintptr_t>(this->getLooper()) |
430 reinterpret_cast<uintptr_t>(this->getRasterizer()) |
431 reinterpret_cast<uintptr_t>(this->getPathEffect())) == 0;
432 }
433
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 /** Only call this if canComputeFastBounds() returned true. This takes a
435 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
436 effects in the paint (e.g. stroking). If needed, it uses the storage
437 rect parameter. It returns the adjusted bounds that can then be used
438 for quickReject tests.
reed@google.com9d07fec2011-03-16 20:02:59 +0000439
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 The returned rect will either be orig or storage, thus the caller
441 should not rely on storage being set to the result, but should always
442 use the retured value. It is legal for orig and storage to be the same
443 rect.
reed@google.com9d07fec2011-03-16 20:02:59 +0000444
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445 e.g.
446 if (paint.canComputeFastBounds()) {
447 SkRect r, storage;
448 path.computeBounds(&r, SkPath::kFast_BoundsType);
449 const SkRect& fastR = paint.computeFastBounds(r, &storage);
450 if (canvas->quickReject(fastR, ...)) {
451 // don't draw the path
452 }
453 }
454 */
reed@android.comd252db02009-04-01 18:31:44 +0000455 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
456 return this->getStyle() == kFill_Style ? orig :
457 this->computeStrokeFastBounds(orig, storage);
458 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459
460 /** Get the paint's shader object.
461 <p />
462 The shader's reference count is not affected.
463 @return the paint's shader (or NULL)
464 */
465 SkShader* getShader() const { return fShader; }
466
467 /** Set or clear the shader object.
468 <p />
469 Pass NULL to clear any previous shader.
470 As a convenience, the parameter passed is also returned.
471 If a previous shader exists, its reference count is decremented.
472 If shader is not NULL, its reference count is incremented.
473 @param shader May be NULL. The shader to be installed in the paint
474 @return shader
475 */
476 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000477
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 /** Get the paint's colorfilter. If there is a colorfilter, its reference
479 count is not changed.
480 @return the paint's colorfilter (or NULL)
481 */
482 SkColorFilter* getColorFilter() const { return fColorFilter; }
483
484 /** Set or clear the paint's colorfilter, returning the parameter.
485 <p />
486 If the paint already has a filter, its reference count is decremented.
487 If filter is not NULL, its reference count is incremented.
488 @param filter May be NULL. The filter to be installed in the paint
489 @return filter
490 */
491 SkColorFilter* setColorFilter(SkColorFilter* filter);
492
493 /** Get the paint's xfermode object.
494 <p />
495 The xfermode's reference count is not affected.
496 @return the paint's xfermode (or NULL)
497 */
498 SkXfermode* getXfermode() const { return fXfermode; }
499
500 /** Set or clear the xfermode object.
501 <p />
502 Pass NULL to clear any previous xfermode.
503 As a convenience, the parameter passed is also returned.
504 If a previous xfermode exists, its reference count is decremented.
505 If xfermode is not NULL, its reference count is incremented.
506 @param xfermode May be NULL. The new xfermode to be installed in the
507 paint
508 @return xfermode
509 */
510 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000511
512 /** Create an xfermode based on the specified Mode, and assign it into the
513 paint, returning the mode that was set. If the Mode is SrcOver, then
514 the paint's xfermode is set to null.
515 */
reed@android.com0baf1932009-06-24 12:41:42 +0000516 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000517
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518 /** Get the paint's patheffect object.
519 <p />
520 The patheffect reference count is not affected.
521 @return the paint's patheffect (or NULL)
522 */
523 SkPathEffect* getPathEffect() const { return fPathEffect; }
524
525 /** Set or clear the patheffect object.
526 <p />
527 Pass NULL to clear any previous patheffect.
528 As a convenience, the parameter passed is also returned.
529 If a previous patheffect exists, its reference count is decremented.
530 If patheffect is not NULL, its reference count is incremented.
531 @param effect May be NULL. The new patheffect to be installed in the
532 paint
533 @return effect
534 */
535 SkPathEffect* setPathEffect(SkPathEffect* effect);
536
537 /** Get the paint's maskfilter object.
538 <p />
539 The maskfilter reference count is not affected.
540 @return the paint's maskfilter (or NULL)
541 */
542 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
543
544 /** Set or clear the maskfilter object.
545 <p />
546 Pass NULL to clear any previous maskfilter.
547 As a convenience, the parameter passed is also returned.
548 If a previous maskfilter exists, its reference count is decremented.
549 If maskfilter is not NULL, its reference count is incremented.
550 @param maskfilter May be NULL. The new maskfilter to be installed in
551 the paint
552 @return maskfilter
553 */
554 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
555
556 // These attributes are for text/fonts
557
558 /** Get the paint's typeface object.
559 <p />
560 The typeface object identifies which font to use when drawing or
561 measuring text. The typeface reference count is not affected.
562 @return the paint's typeface (or NULL)
563 */
564 SkTypeface* getTypeface() const { return fTypeface; }
565
566 /** Set or clear the typeface object.
567 <p />
568 Pass NULL to clear any previous typeface.
569 As a convenience, the parameter passed is also returned.
570 If a previous typeface exists, its reference count is decremented.
571 If typeface is not NULL, its reference count is incremented.
572 @param typeface May be NULL. The new typeface to be installed in the
573 paint
574 @return typeface
575 */
576 SkTypeface* setTypeface(SkTypeface* typeface);
577
578 /** Get the paint's rasterizer (or NULL).
579 <p />
580 The raster controls how paths/text are turned into alpha masks.
581 @return the paint's rasterizer (or NULL)
582 */
583 SkRasterizer* getRasterizer() const { return fRasterizer; }
584
585 /** Set or clear the rasterizer object.
586 <p />
587 Pass NULL to clear any previous rasterizer.
588 As a convenience, the parameter passed is also returned.
589 If a previous rasterizer exists in the paint, its reference count is
590 decremented. If rasterizer is not NULL, its reference count is
591 incremented.
592 @param rasterizer May be NULL. The new rasterizer to be installed in
593 the paint.
594 @return rasterizer
595 */
596 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
597
reed@google.com9d07fec2011-03-16 20:02:59 +0000598 /**
599 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
600 * reference count.
601 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000603
604 /**
605 * Set or clear the looper object.
606 * <p />
607 * Pass NULL to clear any previous looper.
608 * As a convenience, the parameter passed is also returned.
609 * If a previous looper exists in the paint, its reference count is
610 * decremented. If looper is not NULL, its reference count is
611 * incremented.
612 * @param looper May be NULL. The new looper to be installed in the paint.
613 * @return looper
614 */
615 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616
617 enum Align {
618 kLeft_Align,
619 kCenter_Align,
620 kRight_Align,
621
622 kAlignCount
623 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000624
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 /** Return the paint's Align value for drawing text.
626 @return the paint's Align value for drawing text.
627 */
628 Align getTextAlign() const { return (Align)fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000629
reed@android.com8a1c16f2008-12-17 15:59:43 +0000630 /** Set the paint's text alignment.
631 @param align set the paint's Align value for drawing text.
632 */
633 void setTextAlign(Align align);
634
635 /** Return the paint's text size.
636 @return the paint's text size.
637 */
638 SkScalar getTextSize() const { return fTextSize; }
639
640 /** Set the paint's text size. This value must be > 0
641 @param textSize set the paint's text size.
642 */
643 void setTextSize(SkScalar textSize);
644
645 /** Return the paint's horizontal scale factor for text. The default value
646 is 1.0.
647 @return the paint's scale factor in X for drawing/measuring text
648 */
649 SkScalar getTextScaleX() const { return fTextScaleX; }
650
651 /** Set the paint's horizontal scale factor for text. The default value
652 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
653 stretch the text narrower.
654 @param scaleX set the paint's scale factor in X for drawing/measuring
655 text.
656 */
657 void setTextScaleX(SkScalar scaleX);
658
659 /** Return the paint's horizontal skew factor for text. The default value
660 is 0.
661 @return the paint's skew factor in X for drawing text.
662 */
663 SkScalar getTextSkewX() const { return fTextSkewX; }
664
665 /** Set the paint's horizontal skew factor for text. The default value
666 is 0. For approximating oblique text, use values around -0.25.
667 @param skewX set the paint's skew factor in X for drawing text.
668 */
669 void setTextSkewX(SkScalar skewX);
670
671 /** Describes how to interpret the text parameters that are passed to paint
672 methods like measureText() and getTextWidths().
673 */
674 enum TextEncoding {
675 kUTF8_TextEncoding, //!< the text parameters are UTF8
676 kUTF16_TextEncoding, //!< the text parameters are UTF16
677 kGlyphID_TextEncoding //!< the text parameters are glyph indices
678 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000679
680 TextEncoding getTextEncoding() const { return (TextEncoding)fTextEncoding; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000681
682 void setTextEncoding(TextEncoding encoding);
683
684 struct FontMetrics {
685 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
686 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
687 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
688 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
689 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000690 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
691 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
692 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
693 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
reed@android.com8a1c16f2008-12-17 15:59:43 +0000694 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000695
reed@android.com8a1c16f2008-12-17 15:59:43 +0000696 /** Return the recommend spacing between lines (which will be
697 fDescent - fAscent + fLeading).
698 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000699 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700 @param metrics If not null, returns the font metrics for the
701 current typeface/pointsize/etc setting in this
702 paint.
703 @param scale If not 0, return width as if the canvas were scaled
704 by this value
705 @param return the recommended spacing between lines
706 */
707 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000708
reed@android.com8a1c16f2008-12-17 15:59:43 +0000709 /** Return the recommend line spacing. This will be
710 fDescent - fAscent + fLeading
711 */
712 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
713
714 /** Convert the specified text into glyph IDs, returning the number of
715 glyphs ID written. If glyphs is NULL, it is ignore and only the count
716 is returned.
717 */
718 int textToGlyphs(const void* text, size_t byteLength,
719 uint16_t glyphs[]) const;
720
reed@android.coma5dcaf62010-02-05 17:12:32 +0000721 /** Return true if all of the specified text has a corresponding non-zero
722 glyph ID. If any of the code-points in the text are not supported in
723 the typeface (i.e. the glyph ID would be zero), then return false.
724
725 If the text encoding for the paint is kGlyph_TextEncoding, then this
726 returns true if all of the specified glyph IDs are non-zero.
727 */
728 bool containsText(const void* text, size_t byteLength) const;
729
reed@android.com9d3a9852010-01-08 14:07:42 +0000730 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
731 to zero. Note: this does not look at the text-encoding setting in the
732 paint, only at the typeface.
733 */
734 void glyphsToUnichars(const uint16_t glyphs[], int count,
735 SkUnichar text[]) const;
736
reed@android.com8a1c16f2008-12-17 15:59:43 +0000737 /** Return the number of drawable units in the specified text buffer.
738 This looks at the current TextEncoding field of the paint. If you also
739 want to have the text converted into glyph IDs, call textToGlyphs
740 instead.
741 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000742 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000743 return this->textToGlyphs(text, byteLength, NULL);
744 }
745
746 /** Return the width of the text.
747 @param text The text to be measured
748 @param length Number of bytes of text to measure
749 @param bounds If not NULL, returns the bounds of the text,
750 relative to (0, 0).
751 @param scale If not 0, return width as if the canvas were scaled
752 by this value
753 @return The advance width of the text
754 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000755 SkScalar measureText(const void* text, size_t length,
756 SkRect* bounds, SkScalar scale = 0) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000757
758 /** Return the width of the text.
759 @param text Address of the text
760 @param length Number of bytes of text to measure
761 @return The width of the text
762 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000763 SkScalar measureText(const void* text, size_t length) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764 return this->measureText(text, length, NULL, 0);
765 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000766
reed@android.com8a1c16f2008-12-17 15:59:43 +0000767 /** Specify the direction the text buffer should be processed in breakText()
768 */
769 enum TextBufferDirection {
770 /** When measuring text for breakText(), begin at the start of the text
771 buffer and proceed forward through the data. This is the default.
772 */
773 kForward_TextBufferDirection,
774 /** When measuring text for breakText(), begin at the end of the text
775 buffer and proceed backwards through the data.
776 */
777 kBackward_TextBufferDirection
778 };
779
780 /** Return the width of the text.
781 @param text The text to be measured
782 @param length Number of bytes of text to measure
783 @param maxWidth Maximum width. Only the subset of text whose accumulated
784 widths are <= maxWidth are measured.
785 @param measuredWidth Optional. If non-null, this returns the actual
786 width of the measured text.
787 @param tbd Optional. The direction the text buffer should be
788 traversed during measuring.
789 @return The number of bytes of text that were measured. Will be
790 <= length.
791 */
792 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
793 SkScalar* measuredWidth = NULL,
794 TextBufferDirection tbd = kForward_TextBufferDirection)
795 const;
796
797 /** Return the advance widths for the characters in the string.
798 @param text the text
799 @param byteLength number of bytes to of text
800 @param widths If not null, returns the array of advance widths of
801 the glyphs. If not NULL, must be at least a large
802 as the number of unichars in the specified text.
803 @param bounds If not null, returns the bounds for each of
804 character, relative to (0, 0)
805 @return the number of unichars in the specified text.
806 */
807 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
808 SkRect bounds[] = NULL) const;
809
810 /** Return the path (outline) for the specified text.
811 Note: just like SkCanvas::drawText, this will respect the Align setting
812 in the paint.
813 */
814 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
815 SkPath* path) const;
816
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000817#ifdef ANDROID
818 const SkGlyph& getUnicharMetrics(SkUnichar);
819 const void* findImage(const SkGlyph&);
820
821 uint32_t getGenerationID() const;
822#endif
823
reed@google.com632e1a22011-10-06 12:37:00 +0000824 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
825 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
826 bool nothingToDraw() const;
827
reed@android.com8a1c16f2008-12-17 15:59:43 +0000828private:
829 SkTypeface* fTypeface;
830 SkScalar fTextSize;
831 SkScalar fTextScaleX;
832 SkScalar fTextSkewX;
833
834 SkPathEffect* fPathEffect;
835 SkShader* fShader;
836 SkXfermode* fXfermode;
837 SkMaskFilter* fMaskFilter;
838 SkColorFilter* fColorFilter;
839 SkRasterizer* fRasterizer;
840 SkDrawLooper* fLooper;
841
842 SkColor fColor;
843 SkScalar fWidth;
844 SkScalar fMiterLimit;
reed@google.com32538982011-09-30 20:57:05 +0000845 unsigned fFlags : 13;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000846 unsigned fTextAlign : 2;
847 unsigned fCapType : 2;
848 unsigned fJoinType : 2;
849 unsigned fStyle : 2;
850 unsigned fTextEncoding : 2; // 3 values
agl@chromium.org309485b2009-07-21 17:41:32 +0000851 unsigned fHinting : 2;
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000852#ifdef ANDROID
853 uint32_t fGenerationID;
854#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000855
856 SkDrawCacheProc getDrawCacheProc() const;
857 SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
858 bool needFullMetrics) const;
859
860 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
861 int* count, SkRect* bounds) const;
862
863 SkGlyphCache* detachCache(const SkMatrix*) const;
864
865 void descriptorProc(const SkMatrix* deviceMatrix,
866 void (*proc)(const SkDescriptor*, void*),
djsollen@google.com57f49692011-02-23 20:46:31 +0000867 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +0000868
869 const SkRect& computeStrokeFastBounds(const SkRect& orig,
870 SkRect* storage) const;
871
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 enum {
873 kCanonicalTextSizeForPaths = 64
874 };
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000875 friend class SkAutoGlyphCache;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000876 friend class SkCanvas;
877 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000878 friend class SkPDFDevice;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000879 friend class SkTextToPathIter;
880};
881
reed@google.com6bac9472011-06-21 19:24:00 +0000882///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000883
884#include "SkPathEffect.h"
885
886/** \class SkStrokePathEffect
887
888 SkStrokePathEffect simulates stroking inside a patheffect, allowing the
889 caller to have explicit control of when to stroke a path. Typically this is
890 used if the caller wants to stroke before another patheffect is applied
891 (using SkComposePathEffect or SkSumPathEffect).
892*/
893class SkStrokePathEffect : public SkPathEffect {
894public:
895 SkStrokePathEffect(const SkPaint&);
896 SkStrokePathEffect(SkScalar width, SkPaint::Style, SkPaint::Join,
897 SkPaint::Cap, SkScalar miterLimit = -1);
898
899 // overrides
reed@android.com8a1c16f2008-12-17 15:59:43 +0000900 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width);
901
902 // overrides for SkFlattenable
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903 virtual void flatten(SkFlattenableWriteBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904 virtual Factory getFactory();
905
reed@google.com6bac9472011-06-21 19:24:00 +0000906 static SkFlattenable* CreateProc(SkFlattenableReadBuffer&);
907
reed@android.com8a1c16f2008-12-17 15:59:43 +0000908private:
909 SkScalar fWidth, fMiter;
910 uint8_t fStyle, fJoin, fCap;
911
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 SkStrokePathEffect(SkFlattenableReadBuffer&);
913
914 typedef SkPathEffect INHERITED;
915
916 // illegal
917 SkStrokePathEffect(const SkStrokePathEffect&);
918 SkStrokePathEffect& operator=(const SkStrokePathEffect&);
919};
920
921#endif
922