blob: 4defe8b66821cdfbe2d6fe3ef8cea3030733820b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.come4f10a72012-05-15 20:47:50 +00002
reed@android.com8a1c16f2008-12-17 15:59:43 +00003/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00005 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00006 * Use of this source code is governed by a BSD-style license that can be
7 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00008 */
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#ifndef SkPaint_DEFINED
12#define SkPaint_DEFINED
13
14#include "SkColor.h"
reed@google.com9efd9a02012-01-30 15:41:43 +000015#include "SkDrawLooper.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000016#include "SkXfermode.h"
17
reed@google.comb0a34d82012-07-11 19:57:55 +000018class SkAnnotation;
reed@android.com8a1c16f2008-12-17 15:59:43 +000019class SkAutoGlyphCache;
20class SkColorFilter;
21class SkDescriptor;
22class SkFlattenableReadBuffer;
23class SkFlattenableWriteBuffer;
24struct SkGlyph;
25struct SkRect;
26class SkGlyphCache;
reed@google.com15356a62011-11-03 19:29:08 +000027class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000028class SkMaskFilter;
29class SkMatrix;
30class SkPath;
31class SkPathEffect;
32class SkRasterizer;
33class SkShader;
reed@android.com8a1c16f2008-12-17 15:59:43 +000034class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
36typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
37 SkFixed x, SkFixed y);
38
39typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
40
41/** \class SkPaint
42
43 The SkPaint class holds the style and color information about how to draw
44 geometries, text and bitmaps.
45*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000046class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047public:
48 SkPaint();
49 SkPaint(const SkPaint& paint);
50 ~SkPaint();
51
52 SkPaint& operator=(const SkPaint&);
53
reed@google.comb530ef52011-07-20 19:55:42 +000054 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
55 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 return !(a == b);
57 }
reed@google.com9d07fec2011-03-16 20:02:59 +000058
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 void flatten(SkFlattenableWriteBuffer&) const;
60 void unflatten(SkFlattenableReadBuffer&);
61
62 /** Restores the paint to its initial settings.
63 */
64 void reset();
65
agl@chromium.org309485b2009-07-21 17:41:32 +000066 /** Specifies the level of hinting to be performed. These names are taken
67 from the Gnome/Cairo names for the same. They are translated into
68 Freetype concepts the same as in cairo-ft-font.c:
69 kNo_Hinting -> FT_LOAD_NO_HINTING
70 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
71 kNormal_Hinting -> <default, no option>
72 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
73 subpixel glyphs, in which case TARGET_LCD or
74 TARGET_LCD_V is used>
75 */
76 enum Hinting {
77 kNo_Hinting = 0,
78 kSlight_Hinting = 1,
79 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000080 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000081 };
82
reed@google.com9d07fec2011-03-16 20:02:59 +000083 Hinting getHinting() const {
agl@chromium.org309485b2009-07-21 17:41:32 +000084 return static_cast<Hinting>(fHinting);
85 }
86
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +000087 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 /** Specifies the bit values that are stored in the paint's flags.
90 */
91 enum Flags {
92 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
93 kFilterBitmap_Flag = 0x02, //!< mask to enable bitmap filtering
94 kDither_Flag = 0x04, //!< mask to enable dithering
95 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
96 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
97 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
98 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +000099 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000101 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000102 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000103 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000104 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000105 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
reed@google.com32538982011-09-30 20:57:05 +0000106
agl@chromium.org309485b2009-07-21 17:41:32 +0000107 // when adding extra flags, note that the fFlags member is specified
108 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109
reed@google.com8351aab2012-01-18 17:06:35 +0000110 kAllFlags = 0x3FFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 };
112
113 /** Return the paint's flags. Use the Flag enum to test flag values.
114 @return the paint's flags (see enums ending in _Flag for bit masks)
115 */
116 uint32_t getFlags() const { return fFlags; }
117
118 /** Set the paint's flags. Use the Flag enum to specific flag values.
119 @param flags The new flag bits for the paint (see Flags enum)
120 */
121 void setFlags(uint32_t flags);
122
123 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
124 @return true if the antialias bit is set in the paint's flags.
125 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000126 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 return SkToBool(this->getFlags() & kAntiAlias_Flag);
128 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
131 @param aa true to enable antialiasing, false to disable it
132 */
133 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 /** Helper for getFlags(), returning true if kDither_Flag bit is set
136 @return true if the dithering bit is set in the paint's flags.
137 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000138 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 return SkToBool(this->getFlags() & kDither_Flag);
140 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
143 @param dither true to enable dithering, false to disable it
144 */
145 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
148 @return true if the lineartext bit is set in the paint's flags
149 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000150 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 return SkToBool(this->getFlags() & kLinearText_Flag);
152 }
153
154 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
155 @param linearText true to set the linearText bit in the paint's flags,
156 false to clear it.
157 */
158 void setLinearText(bool linearText);
159
160 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
161 @return true if the lineartext bit is set in the paint's flags
162 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000163 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 return SkToBool(this->getFlags() & kSubpixelText_Flag);
165 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000166
reed@google.com84b437e2011-08-01 12:45:35 +0000167 /**
168 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
169 * @param subpixelText true to set the subpixelText bit in the paint's
170 * flags, false to clear it.
171 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000173
reed@google.com9d07fec2011-03-16 20:02:59 +0000174 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000175 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
176 }
177
reed@google.com84b437e2011-08-01 12:45:35 +0000178 /**
179 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
180 * Note: antialiasing must also be on for lcd rendering
181 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
182 * false to clear it.
183 */
184 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000185
reed@google.com9d07fec2011-03-16 20:02:59 +0000186 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000187 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
188 }
189
190 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
191 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
192 false to clear it.
193 */
194 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
195
reed@google.com9d07fec2011-03-16 20:02:59 +0000196 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000197 return SkToBool(this->getFlags() & kAutoHinting_Flag);
198 }
199
200 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
201 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
202 paint's flags,
203 false to clear it.
204 */
205 void setAutohinted(bool useAutohinter);
206
reed@google.com830a23e2011-11-10 15:20:49 +0000207 bool isVerticalText() const {
208 return SkToBool(this->getFlags() & kVerticalText_Flag);
209 }
210
211 /**
212 * Helper for setting or clearing the kVerticalText_Flag bit in
213 * setFlags(...).
214 *
215 * If this bit is set, then advances are treated as Y values rather than
216 * X values, and drawText will places its glyphs vertically rather than
217 * horizontally.
218 */
219 void setVerticalText(bool);
220
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
222 @return true if the underlineText bit is set in the paint's flags.
223 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000224 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 return SkToBool(this->getFlags() & kUnderlineText_Flag);
226 }
227
228 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
229 @param underlineText true to set the underlineText bit in the paint's
230 flags, false to clear it.
231 */
232 void setUnderlineText(bool underlineText);
233
234 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
235 @return true if the strikeThruText bit is set in the paint's flags.
236 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000237 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
239 }
240
241 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
242 @param strikeThruText true to set the strikeThruText bit in the
243 paint's flags, false to clear it.
244 */
245 void setStrikeThruText(bool strikeThruText);
246
247 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
248 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
249 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000250 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
252 }
253
254 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
255 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
256 flags, false to clear it.
257 */
258 void setFakeBoldText(bool fakeBoldText);
259
260 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
261 @return true if the kernText bit is set in the paint's flags.
262 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000263 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264 return SkToBool(this->getFlags() & kDevKernText_Flag);
265 }
266
267 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
268 @param kernText true to set the kKernText_Flag bit in the paint's
269 flags, false to clear it.
270 */
271 void setDevKernText(bool devKernText);
272
reed@google.com9d07fec2011-03-16 20:02:59 +0000273 bool isFilterBitmap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274 return SkToBool(this->getFlags() & kFilterBitmap_Flag);
275 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000276
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 void setFilterBitmap(bool filterBitmap);
278
279 /** Styles apply to rect, oval, path, and text.
280 Bitmaps are always drawn in "fill", and lines are always drawn in
281 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000282
reed@android.comed881c22009-09-15 14:10:42 +0000283 Note: strokeandfill implicitly draws the result with
284 SkPath::kWinding_FillType, so if the original path is even-odd, the
285 results may not appear the same as if it was drawn twice, filled and
286 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 */
288 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000289 kFill_Style, //!< fill the geometry
290 kStroke_Style, //!< stroke the geometry
291 kStrokeAndFill_Style, //!< fill and stroke the geometry
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292
tomhudson@google.com1f902872012-06-01 13:15:47 +0000293 kStyleCount
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 };
295
296 /** Return the paint's style, used for controlling how primitives'
297 geometries are interpreted (except for drawBitmap, which always assumes
298 kFill_Style).
299 @return the paint's Style
300 */
301 Style getStyle() const { return (Style)fStyle; }
302
303 /** Set the paint's style, used for controlling how primitives'
304 geometries are interpreted (except for drawBitmap, which always assumes
305 Fill).
306 @param style The new style to set in the paint
307 */
308 void setStyle(Style style);
309
310 /** Return the paint's color. Note that the color is a 32bit value
311 containing alpha as well as r,g,b. This 32bit value is not
312 premultiplied, meaning that its alpha can be any value, regardless of
313 the values of r,g,b.
314 @return the paint's color (and alpha).
315 */
316 SkColor getColor() const { return fColor; }
317
318 /** Set the paint's color. Note that the color is a 32bit value containing
319 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
320 that its alpha can be any value, regardless of the values of r,g,b.
321 @param color The new color (including alpha) to set in the paint.
322 */
323 void setColor(SkColor color);
324
325 /** Helper to getColor() that just returns the color's alpha value.
326 @return the alpha component of the paint's color.
327 */
328 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000329
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 /** Helper to setColor(), that only assigns the color's alpha value,
331 leaving its r,g,b values unchanged.
332 @param a set the alpha component (0..255) of the paint's color.
333 */
334 void setAlpha(U8CPU a);
335
336 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
337 using SkColorSetARGB()
338 @param a The new alpha component (0..255) of the paint's color.
339 @param r The new red component (0..255) of the paint's color.
340 @param g The new green component (0..255) of the paint's color.
341 @param b The new blue component (0..255) of the paint's color.
342 */
343 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
344
reed@google.com9d07fec2011-03-16 20:02:59 +0000345 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 <p />
347 A value of 0 strokes in hairline mode.
348 Hairlines always draw 1-pixel wide, regardless of the matrix.
349 @return the paint's stroke width, used whenever the paint's style is
350 Stroke or StrokeAndFill.
351 */
352 SkScalar getStrokeWidth() const { return fWidth; }
353
reed@google.com9d07fec2011-03-16 20:02:59 +0000354 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 Pass 0 to stroke in hairline mode.
356 Hairlines always draw 1-pixel wide, regardless of the matrix.
357 @param width set the paint's stroke width, used whenever the paint's
358 style is Stroke or StrokeAndFill.
359 */
360 void setStrokeWidth(SkScalar width);
361
362 /** Return the paint's stroke miter value. This is used to control the
363 behavior of miter joins when the joins angle is sharp.
364 @return the paint's miter limit, used whenever the paint's style is
365 Stroke or StrokeAndFill.
366 */
367 SkScalar getStrokeMiter() const { return fMiterLimit; }
368
369 /** Set the paint's stroke miter value. This is used to control the
370 behavior of miter joins when the joins angle is sharp. This value must
371 be >= 0.
372 @param miter set the miter limit on the paint, used whenever the
373 paint's style is Stroke or StrokeAndFill.
374 */
375 void setStrokeMiter(SkScalar miter);
376
377 /** Cap enum specifies the settings for the paint's strokecap. This is the
378 treatment that is applied to the beginning and end of each non-closed
379 contour (e.g. lines).
380 */
381 enum Cap {
382 kButt_Cap, //!< begin/end contours with no extension
383 kRound_Cap, //!< begin/end contours with a semi-circle extension
384 kSquare_Cap, //!< begin/end contours with a half square extension
385
386 kCapCount,
387 kDefault_Cap = kButt_Cap
388 };
389
390 /** Join enum specifies the settings for the paint's strokejoin. This is
391 the treatment that is applied to corners in paths and rectangles.
392 */
393 enum Join {
394 kMiter_Join, //!< connect path segments with a sharp join
395 kRound_Join, //!< connect path segments with a round join
396 kBevel_Join, //!< connect path segments with a flat bevel join
397
398 kJoinCount,
399 kDefault_Join = kMiter_Join
400 };
401
402 /** Return the paint's stroke cap type, controlling how the start and end
403 of stroked lines and paths are treated.
404 @return the line cap style for the paint, used whenever the paint's
405 style is Stroke or StrokeAndFill.
406 */
407 Cap getStrokeCap() const { return (Cap)fCapType; }
408
409 /** Set the paint's stroke cap type.
410 @param cap set the paint's line cap style, used whenever the paint's
411 style is Stroke or StrokeAndFill.
412 */
413 void setStrokeCap(Cap cap);
414
415 /** Return the paint's stroke join type.
416 @return the paint's line join style, used whenever the paint's style is
417 Stroke or StrokeAndFill.
418 */
419 Join getStrokeJoin() const { return (Join)fJoinType; }
420
421 /** Set the paint's stroke join type.
422 @param join set the paint's line join style, used whenever the paint's
423 style is Stroke or StrokeAndFill.
424 */
425 void setStrokeJoin(Join join);
426
427 /** Applies any/all effects (patheffect, stroking) to src, returning the
428 result in dst. The result is that drawing src with this paint will be
429 the same as drawing dst with a default paint (at least from the
430 geometric perspective).
431 @param src input path
432 @param dst output path (may be the same as src)
433 @return true if the path should be filled, or false if it should be
434 drawn with a hairline (width == 0)
435 */
436 bool getFillPath(const SkPath& src, SkPath* dst) const;
437
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 /** Get the paint's shader object.
439 <p />
440 The shader's reference count is not affected.
441 @return the paint's shader (or NULL)
442 */
443 SkShader* getShader() const { return fShader; }
444
445 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000446 * Shaders specify the source color(s) for what is being drawn. If a paint
447 * has no shader, then the paint's color is used. If the paint has a
448 * shader, then the shader's color(s) are use instead, but they are
449 * modulated by the paint's alpha. This makes it easy to create a shader
450 * once (e.g. bitmap tiling or gradient) and then change its transparency
451 * w/o having to modify the original shader... only the paint's alpha needs
452 * to be modified.
453 * <p />
454 * Pass NULL to clear any previous shader.
455 * As a convenience, the parameter passed is also returned.
456 * If a previous shader exists, its reference count is decremented.
457 * If shader is not NULL, its reference count is incremented.
458 * @param shader May be NULL. The shader to be installed in the paint
459 * @return shader
460 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000462
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463 /** Get the paint's colorfilter. If there is a colorfilter, its reference
464 count is not changed.
465 @return the paint's colorfilter (or NULL)
466 */
467 SkColorFilter* getColorFilter() const { return fColorFilter; }
468
469 /** Set or clear the paint's colorfilter, returning the parameter.
470 <p />
471 If the paint already has a filter, its reference count is decremented.
472 If filter is not NULL, its reference count is incremented.
473 @param filter May be NULL. The filter to be installed in the paint
474 @return filter
475 */
476 SkColorFilter* setColorFilter(SkColorFilter* filter);
477
478 /** Get the paint's xfermode object.
479 <p />
480 The xfermode's reference count is not affected.
481 @return the paint's xfermode (or NULL)
482 */
483 SkXfermode* getXfermode() const { return fXfermode; }
484
485 /** Set or clear the xfermode object.
486 <p />
487 Pass NULL to clear any previous xfermode.
488 As a convenience, the parameter passed is also returned.
489 If a previous xfermode exists, its reference count is decremented.
490 If xfermode is not NULL, its reference count is incremented.
491 @param xfermode May be NULL. The new xfermode to be installed in the
492 paint
493 @return xfermode
494 */
495 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000496
497 /** Create an xfermode based on the specified Mode, and assign it into the
498 paint, returning the mode that was set. If the Mode is SrcOver, then
499 the paint's xfermode is set to null.
500 */
reed@android.com0baf1932009-06-24 12:41:42 +0000501 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000502
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503 /** Get the paint's patheffect object.
504 <p />
505 The patheffect reference count is not affected.
506 @return the paint's patheffect (or NULL)
507 */
508 SkPathEffect* getPathEffect() const { return fPathEffect; }
509
510 /** Set or clear the patheffect object.
511 <p />
512 Pass NULL to clear any previous patheffect.
513 As a convenience, the parameter passed is also returned.
514 If a previous patheffect exists, its reference count is decremented.
515 If patheffect is not NULL, its reference count is incremented.
516 @param effect May be NULL. The new patheffect to be installed in the
517 paint
518 @return effect
519 */
520 SkPathEffect* setPathEffect(SkPathEffect* effect);
521
522 /** Get the paint's maskfilter object.
523 <p />
524 The maskfilter reference count is not affected.
525 @return the paint's maskfilter (or NULL)
526 */
527 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
528
529 /** Set or clear the maskfilter object.
530 <p />
531 Pass NULL to clear any previous maskfilter.
532 As a convenience, the parameter passed is also returned.
533 If a previous maskfilter exists, its reference count is decremented.
534 If maskfilter is not NULL, its reference count is incremented.
535 @param maskfilter May be NULL. The new maskfilter to be installed in
536 the paint
537 @return maskfilter
538 */
539 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
540
541 // These attributes are for text/fonts
542
543 /** Get the paint's typeface object.
544 <p />
545 The typeface object identifies which font to use when drawing or
546 measuring text. The typeface reference count is not affected.
547 @return the paint's typeface (or NULL)
548 */
549 SkTypeface* getTypeface() const { return fTypeface; }
550
551 /** Set or clear the typeface object.
552 <p />
553 Pass NULL to clear any previous typeface.
554 As a convenience, the parameter passed is also returned.
555 If a previous typeface exists, its reference count is decremented.
556 If typeface is not NULL, its reference count is incremented.
557 @param typeface May be NULL. The new typeface to be installed in the
558 paint
559 @return typeface
560 */
561 SkTypeface* setTypeface(SkTypeface* typeface);
562
563 /** Get the paint's rasterizer (or NULL).
564 <p />
565 The raster controls how paths/text are turned into alpha masks.
566 @return the paint's rasterizer (or NULL)
567 */
568 SkRasterizer* getRasterizer() const { return fRasterizer; }
569
570 /** Set or clear the rasterizer object.
571 <p />
572 Pass NULL to clear any previous rasterizer.
573 As a convenience, the parameter passed is also returned.
574 If a previous rasterizer exists in the paint, its reference count is
575 decremented. If rasterizer is not NULL, its reference count is
576 incremented.
577 @param rasterizer May be NULL. The new rasterizer to be installed in
578 the paint.
579 @return rasterizer
580 */
581 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
582
reed@google.com15356a62011-11-03 19:29:08 +0000583 SkImageFilter* getImageFilter() const { return fImageFilter; }
584 SkImageFilter* setImageFilter(SkImageFilter*);
reed@google.comb0a34d82012-07-11 19:57:55 +0000585
586 SkAnnotation* getAnnotation() const { return fAnnotation; }
587 SkAnnotation* setAnnotation(SkAnnotation*);
588
589 /**
590 * Returns true if there is an annotation installed on this paint, and
591 * the annotation specifics no-drawing.
592 */
593 bool isNoDrawAnnotation() const {
594 return SkToBool(fPrivFlags & kNoDrawAnnotation_PrivFlag);
595 }
reed@google.com15356a62011-11-03 19:29:08 +0000596
reed@google.com9d07fec2011-03-16 20:02:59 +0000597 /**
598 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
599 * reference count.
600 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000601 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000602
603 /**
604 * Set or clear the looper object.
605 * <p />
606 * Pass NULL to clear any previous looper.
607 * As a convenience, the parameter passed is also returned.
608 * If a previous looper exists in the paint, its reference count is
609 * decremented. If looper is not NULL, its reference count is
610 * incremented.
611 * @param looper May be NULL. The new looper to be installed in the paint.
612 * @return looper
613 */
614 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615
616 enum Align {
617 kLeft_Align,
618 kCenter_Align,
619 kRight_Align,
620
621 kAlignCount
622 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000623
reed@android.com8a1c16f2008-12-17 15:59:43 +0000624 /** Return the paint's Align value for drawing text.
625 @return the paint's Align value for drawing text.
626 */
627 Align getTextAlign() const { return (Align)fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000628
reed@android.com8a1c16f2008-12-17 15:59:43 +0000629 /** Set the paint's text alignment.
630 @param align set the paint's Align value for drawing text.
631 */
632 void setTextAlign(Align align);
633
634 /** Return the paint's text size.
635 @return the paint's text size.
636 */
637 SkScalar getTextSize() const { return fTextSize; }
638
639 /** Set the paint's text size. This value must be > 0
640 @param textSize set the paint's text size.
641 */
642 void setTextSize(SkScalar textSize);
643
644 /** Return the paint's horizontal scale factor for text. The default value
645 is 1.0.
646 @return the paint's scale factor in X for drawing/measuring text
647 */
648 SkScalar getTextScaleX() const { return fTextScaleX; }
649
650 /** Set the paint's horizontal scale factor for text. The default value
651 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
652 stretch the text narrower.
653 @param scaleX set the paint's scale factor in X for drawing/measuring
654 text.
655 */
656 void setTextScaleX(SkScalar scaleX);
657
658 /** Return the paint's horizontal skew factor for text. The default value
659 is 0.
660 @return the paint's skew factor in X for drawing text.
661 */
662 SkScalar getTextSkewX() const { return fTextSkewX; }
663
664 /** Set the paint's horizontal skew factor for text. The default value
665 is 0. For approximating oblique text, use values around -0.25.
666 @param skewX set the paint's skew factor in X for drawing text.
667 */
668 void setTextSkewX(SkScalar skewX);
669
670 /** Describes how to interpret the text parameters that are passed to paint
671 methods like measureText() and getTextWidths().
672 */
673 enum TextEncoding {
674 kUTF8_TextEncoding, //!< the text parameters are UTF8
675 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000676 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000677 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
reed@google.com44da42e2011-11-10 20:04:47 +0000746 /** Return the width of the text. This will return the vertical measure
747 * if isVerticalText() is true, in which case the returned value should
748 * be treated has a height instead of a width.
749 *
750 * @param text The text to be measured
751 * @param length Number of bytes of text to measure
752 * @param bounds If not NULL, returns the bounds of the text,
753 * relative to (0, 0).
754 * @param scale If not 0, return width as if the canvas were scaled
755 * by this value
756 * @return The advance width of the text
757 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000758 SkScalar measureText(const void* text, size_t length,
759 SkRect* bounds, SkScalar scale = 0) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000760
reed@google.com44da42e2011-11-10 20:04:47 +0000761 /** Return the width of the text. This will return the vertical measure
762 * if isVerticalText() is true, in which case the returned value should
763 * be treated has a height instead of a width.
764 *
765 * @param text Address of the text
766 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000767 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000768 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000769 SkScalar measureText(const void* text, size_t length) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770 return this->measureText(text, length, NULL, 0);
771 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000772
reed@android.com8a1c16f2008-12-17 15:59:43 +0000773 /** Specify the direction the text buffer should be processed in breakText()
774 */
775 enum TextBufferDirection {
776 /** When measuring text for breakText(), begin at the start of the text
777 buffer and proceed forward through the data. This is the default.
778 */
779 kForward_TextBufferDirection,
780 /** When measuring text for breakText(), begin at the end of the text
781 buffer and proceed backwards through the data.
782 */
783 kBackward_TextBufferDirection
784 };
785
reed@google.com44da42e2011-11-10 20:04:47 +0000786 /** Return the number of bytes of text that were measured. If
787 * isVerticalText() is true, then the vertical advances are used for
788 * the measurement.
789 *
790 * @param text The text to be measured
791 * @param length Number of bytes of text to measure
792 * @param maxWidth Maximum width. Only the subset of text whose accumulated
793 * widths are <= maxWidth are measured.
794 * @param measuredWidth Optional. If non-null, this returns the actual
795 * width of the measured text.
796 * @param tbd Optional. The direction the text buffer should be
797 * traversed during measuring.
798 * @return The number of bytes of text that were measured. Will be
799 * <= length.
800 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
802 SkScalar* measuredWidth = NULL,
803 TextBufferDirection tbd = kForward_TextBufferDirection)
804 const;
805
reed@google.com44da42e2011-11-10 20:04:47 +0000806 /** Return the advances for the text. These will be vertical advances if
807 * isVerticalText() returns true.
808 *
809 * @param text the text
810 * @param byteLength number of bytes to of text
811 * @param widths If not null, returns the array of advances for
812 * the glyphs. If not NULL, must be at least a large
813 * as the number of unichars in the specified text.
814 * @param bounds If not null, returns the bounds for each of
815 * character, relative to (0, 0)
816 * @return the number of unichars in the specified text.
817 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
819 SkRect bounds[] = NULL) const;
820
821 /** Return the path (outline) for the specified text.
822 Note: just like SkCanvas::drawText, this will respect the Align setting
823 in the paint.
824 */
825 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
826 SkPath* path) const;
827
reed@google.comca0062e2012-07-20 11:20:32 +0000828 void getPosTextPath(const void* text, size_t length,
829 const SkPoint pos[], SkPath* path) const;
830
djsollen@google.com56c69772011-11-08 19:00:26 +0000831#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000832 const SkGlyph& getUnicharMetrics(SkUnichar);
djsollen@google.com60abb072012-02-15 18:49:15 +0000833 const SkGlyph& getGlyphMetrics(uint16_t);
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000834 const void* findImage(const SkGlyph&);
835
836 uint32_t getGenerationID() const;
djsollen@google.com60abb072012-02-15 18:49:15 +0000837
838 /** Returns the base glyph count for the strike associated with this paint
839 */
840 unsigned getBaseGlyphCount(SkUnichar text) const;
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000841#endif
842
reed@google.com632e1a22011-10-06 12:37:00 +0000843 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
844 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
845 bool nothingToDraw() const;
846
reed@google.coma584aed2012-05-16 14:06:02 +0000847 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000848 // would prefer to make these private...
849
reed@google.coma584aed2012-05-16 14:06:02 +0000850 /** Returns true if the current paint settings allow for fast computation of
851 bounds (i.e. there is nothing complex like a patheffect that would make
852 the bounds computation expensive.
853 */
854 bool canComputeFastBounds() const {
855 if (this->getLooper()) {
856 return this->getLooper()->canComputeFastBounds(*this);
857 }
858 return !this->getRasterizer();
859 }
860
861 /** Only call this if canComputeFastBounds() returned true. This takes a
862 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
863 effects in the paint (e.g. stroking). If needed, it uses the storage
864 rect parameter. It returns the adjusted bounds that can then be used
865 for quickReject tests.
866
867 The returned rect will either be orig or storage, thus the caller
868 should not rely on storage being set to the result, but should always
869 use the retured value. It is legal for orig and storage to be the same
870 rect.
871
872 e.g.
873 if (paint.canComputeFastBounds()) {
874 SkRect r, storage;
875 path.computeBounds(&r, SkPath::kFast_BoundsType);
876 const SkRect& fastR = paint.computeFastBounds(r, &storage);
877 if (canvas->quickReject(fastR, ...)) {
878 // don't draw the path
879 }
880 }
881 */
882 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
883 SkPaint::Style style = this->getStyle();
884 // ultra fast-case: filling with no effects that affect geometry
885 if (kFill_Style == style) {
886 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
887 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
888 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
889 if (!effects) {
890 return orig;
891 }
892 }
893
894 return this->doComputeFastBounds(orig, storage, style);
895 }
896
897 const SkRect& computeFastStrokeBounds(const SkRect& orig,
898 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +0000899 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +0000900 }
901
902 // Take the style explicitly, so the caller can force us to be stroked
903 // without having to make a copy of the paint just to change that field.
904 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
905 Style) const;
reed@google.comd5f20792012-05-16 14:15:02 +0000906
907private:
908 SkTypeface* fTypeface;
909 SkScalar fTextSize;
910 SkScalar fTextScaleX;
911 SkScalar fTextSkewX;
reed@google.coma584aed2012-05-16 14:06:02 +0000912
reed@google.comd5f20792012-05-16 14:15:02 +0000913 SkPathEffect* fPathEffect;
914 SkShader* fShader;
915 SkXfermode* fXfermode;
916 SkMaskFilter* fMaskFilter;
917 SkColorFilter* fColorFilter;
918 SkRasterizer* fRasterizer;
919 SkDrawLooper* fLooper;
920 SkImageFilter* fImageFilter;
reed@google.comb0a34d82012-07-11 19:57:55 +0000921 SkAnnotation* fAnnotation;
reed@google.comd5f20792012-05-16 14:15:02 +0000922
923 SkColor fColor;
924 SkScalar fWidth;
925 SkScalar fMiterLimit;
reed@google.comb0a34d82012-07-11 19:57:55 +0000926 // all of these bitfields should add up to 32
927 unsigned fFlags : 16;
reed@google.comd5f20792012-05-16 14:15:02 +0000928 unsigned fTextAlign : 2;
929 unsigned fCapType : 2;
930 unsigned fJoinType : 2;
931 unsigned fStyle : 2;
932 unsigned fTextEncoding : 2; // 3 values
933 unsigned fHinting : 2;
reed@google.comb0a34d82012-07-11 19:57:55 +0000934 unsigned fPrivFlags : 4; // these are not flattened/unflattened
935
936 enum PrivFlags {
937 kNoDrawAnnotation_PrivFlag = 1 << 0,
938 };
reed@google.comd5f20792012-05-16 14:15:02 +0000939
940 SkDrawCacheProc getDrawCacheProc() const;
941 SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
942 bool needFullMetrics) const;
943
944 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
945 int* count, SkRect* bounds) const;
946
947 SkGlyphCache* detachCache(const SkMatrix*) const;
948
949 void descriptorProc(const SkMatrix* deviceMatrix,
950 void (*proc)(const SkDescriptor*, void*),
951 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +0000952
reed@android.com8a1c16f2008-12-17 15:59:43 +0000953 enum {
954 kCanonicalTextSizeForPaths = 64
955 };
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000956 friend class SkAutoGlyphCache;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000957 friend class SkCanvas;
958 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000959 friend class SkPDFDevice;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000960 friend class SkTextToPathIter;
djsollen@google.comb44cd652011-12-01 17:09:21 +0000961
962#ifdef SK_BUILD_FOR_ANDROID
963 // In order for the == operator to work properly this must be the last field
964 // in the struct so that we can do a memcmp to this field's offset.
965 uint32_t fGenerationID;
966#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000967};
968
reed@android.com8a1c16f2008-12-17 15:59:43 +0000969#endif
970