blob: 9073e2049e5ab014de64a2c6fc23687f361c59b8 [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;
reed@google.com15356a62011-11-03 19:29:08 +000024class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025class SkMaskFilter;
26class SkMatrix;
27class SkPath;
28class SkPathEffect;
29class SkRasterizer;
30class SkShader;
31class SkDrawLooper;
32class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
34typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
35 SkFixed x, SkFixed y);
36
37typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
38
39/** \class SkPaint
40
41 The SkPaint class holds the style and color information about how to draw
42 geometries, text and bitmaps.
43*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000044class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045public:
46 SkPaint();
47 SkPaint(const SkPaint& paint);
48 ~SkPaint();
49
50 SkPaint& operator=(const SkPaint&);
51
reed@google.comb530ef52011-07-20 19:55:42 +000052 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
53 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 return !(a == b);
55 }
reed@google.com9d07fec2011-03-16 20:02:59 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 void flatten(SkFlattenableWriteBuffer&) const;
58 void unflatten(SkFlattenableReadBuffer&);
59
60 /** Restores the paint to its initial settings.
61 */
62 void reset();
63
agl@chromium.org309485b2009-07-21 17:41:32 +000064 /** Specifies the level of hinting to be performed. These names are taken
65 from the Gnome/Cairo names for the same. They are translated into
66 Freetype concepts the same as in cairo-ft-font.c:
67 kNo_Hinting -> FT_LOAD_NO_HINTING
68 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
69 kNormal_Hinting -> <default, no option>
70 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
71 subpixel glyphs, in which case TARGET_LCD or
72 TARGET_LCD_V is used>
73 */
74 enum Hinting {
75 kNo_Hinting = 0,
76 kSlight_Hinting = 1,
77 kNormal_Hinting = 2, //!< this is the default
78 kFull_Hinting = 3,
79 };
80
reed@google.com9d07fec2011-03-16 20:02:59 +000081 Hinting getHinting() const {
agl@chromium.org309485b2009-07-21 17:41:32 +000082 return static_cast<Hinting>(fHinting);
83 }
84
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +000085 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 /** Specifies the bit values that are stored in the paint's flags.
88 */
89 enum Flags {
90 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
91 kFilterBitmap_Flag = 0x02, //!< mask to enable bitmap filtering
92 kDither_Flag = 0x04, //!< mask to enable dithering
93 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
94 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
95 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
96 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +000097 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +000099 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000100 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000101 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000102 kVerticalText_Flag = 0x1000,
reed@google.com32538982011-09-30 20:57:05 +0000103
104 // experimental/private
reed@google.com830a23e2011-11-10 15:20:49 +0000105 kForceAAText_Flag = 0x2000,
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.com830a23e2011-11-10 15:20:49 +0000110 kAllFlags = 0x2FFF
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
293 kStyleCount,
294 };
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
438 /** Returns true if the current paint settings allow for fast computation of
439 bounds (i.e. there is nothing complex like a patheffect that would make
440 the bounds computation expensive.
441 */
reed@android.comd252db02009-04-01 18:31:44 +0000442 bool canComputeFastBounds() const {
443 // use bit-or since no need for early exit
444 return (reinterpret_cast<uintptr_t>(this->getMaskFilter()) |
445 reinterpret_cast<uintptr_t>(this->getLooper()) |
446 reinterpret_cast<uintptr_t>(this->getRasterizer()) |
447 reinterpret_cast<uintptr_t>(this->getPathEffect())) == 0;
448 }
449
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 /** Only call this if canComputeFastBounds() returned true. This takes a
451 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
452 effects in the paint (e.g. stroking). If needed, it uses the storage
453 rect parameter. It returns the adjusted bounds that can then be used
454 for quickReject tests.
reed@google.com9d07fec2011-03-16 20:02:59 +0000455
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456 The returned rect will either be orig or storage, thus the caller
457 should not rely on storage being set to the result, but should always
458 use the retured value. It is legal for orig and storage to be the same
459 rect.
reed@google.com9d07fec2011-03-16 20:02:59 +0000460
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 e.g.
462 if (paint.canComputeFastBounds()) {
463 SkRect r, storage;
464 path.computeBounds(&r, SkPath::kFast_BoundsType);
465 const SkRect& fastR = paint.computeFastBounds(r, &storage);
466 if (canvas->quickReject(fastR, ...)) {
467 // don't draw the path
468 }
469 }
470 */
reed@android.comd252db02009-04-01 18:31:44 +0000471 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
472 return this->getStyle() == kFill_Style ? orig :
473 this->computeStrokeFastBounds(orig, storage);
474 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475
476 /** Get the paint's shader object.
477 <p />
478 The shader's reference count is not affected.
479 @return the paint's shader (or NULL)
480 */
481 SkShader* getShader() const { return fShader; }
482
483 /** Set or clear the shader object.
484 <p />
485 Pass NULL to clear any previous shader.
486 As a convenience, the parameter passed is also returned.
487 If a previous shader exists, its reference count is decremented.
488 If shader is not NULL, its reference count is incremented.
489 @param shader May be NULL. The shader to be installed in the paint
490 @return shader
491 */
492 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000493
reed@android.com8a1c16f2008-12-17 15:59:43 +0000494 /** Get the paint's colorfilter. If there is a colorfilter, its reference
495 count is not changed.
496 @return the paint's colorfilter (or NULL)
497 */
498 SkColorFilter* getColorFilter() const { return fColorFilter; }
499
500 /** Set or clear the paint's colorfilter, returning the parameter.
501 <p />
502 If the paint already has a filter, its reference count is decremented.
503 If filter is not NULL, its reference count is incremented.
504 @param filter May be NULL. The filter to be installed in the paint
505 @return filter
506 */
507 SkColorFilter* setColorFilter(SkColorFilter* filter);
508
509 /** Get the paint's xfermode object.
510 <p />
511 The xfermode's reference count is not affected.
512 @return the paint's xfermode (or NULL)
513 */
514 SkXfermode* getXfermode() const { return fXfermode; }
515
516 /** Set or clear the xfermode object.
517 <p />
518 Pass NULL to clear any previous xfermode.
519 As a convenience, the parameter passed is also returned.
520 If a previous xfermode exists, its reference count is decremented.
521 If xfermode is not NULL, its reference count is incremented.
522 @param xfermode May be NULL. The new xfermode to be installed in the
523 paint
524 @return xfermode
525 */
526 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000527
528 /** Create an xfermode based on the specified Mode, and assign it into the
529 paint, returning the mode that was set. If the Mode is SrcOver, then
530 the paint's xfermode is set to null.
531 */
reed@android.com0baf1932009-06-24 12:41:42 +0000532 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000533
reed@android.com8a1c16f2008-12-17 15:59:43 +0000534 /** Get the paint's patheffect object.
535 <p />
536 The patheffect reference count is not affected.
537 @return the paint's patheffect (or NULL)
538 */
539 SkPathEffect* getPathEffect() const { return fPathEffect; }
540
541 /** Set or clear the patheffect object.
542 <p />
543 Pass NULL to clear any previous patheffect.
544 As a convenience, the parameter passed is also returned.
545 If a previous patheffect exists, its reference count is decremented.
546 If patheffect is not NULL, its reference count is incremented.
547 @param effect May be NULL. The new patheffect to be installed in the
548 paint
549 @return effect
550 */
551 SkPathEffect* setPathEffect(SkPathEffect* effect);
552
553 /** Get the paint's maskfilter object.
554 <p />
555 The maskfilter reference count is not affected.
556 @return the paint's maskfilter (or NULL)
557 */
558 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
559
560 /** Set or clear the maskfilter object.
561 <p />
562 Pass NULL to clear any previous maskfilter.
563 As a convenience, the parameter passed is also returned.
564 If a previous maskfilter exists, its reference count is decremented.
565 If maskfilter is not NULL, its reference count is incremented.
566 @param maskfilter May be NULL. The new maskfilter to be installed in
567 the paint
568 @return maskfilter
569 */
570 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
571
572 // These attributes are for text/fonts
573
574 /** Get the paint's typeface object.
575 <p />
576 The typeface object identifies which font to use when drawing or
577 measuring text. The typeface reference count is not affected.
578 @return the paint's typeface (or NULL)
579 */
580 SkTypeface* getTypeface() const { return fTypeface; }
581
582 /** Set or clear the typeface object.
583 <p />
584 Pass NULL to clear any previous typeface.
585 As a convenience, the parameter passed is also returned.
586 If a previous typeface exists, its reference count is decremented.
587 If typeface is not NULL, its reference count is incremented.
588 @param typeface May be NULL. The new typeface to be installed in the
589 paint
590 @return typeface
591 */
592 SkTypeface* setTypeface(SkTypeface* typeface);
593
594 /** Get the paint's rasterizer (or NULL).
595 <p />
596 The raster controls how paths/text are turned into alpha masks.
597 @return the paint's rasterizer (or NULL)
598 */
599 SkRasterizer* getRasterizer() const { return fRasterizer; }
600
601 /** Set or clear the rasterizer object.
602 <p />
603 Pass NULL to clear any previous rasterizer.
604 As a convenience, the parameter passed is also returned.
605 If a previous rasterizer exists in the paint, its reference count is
606 decremented. If rasterizer is not NULL, its reference count is
607 incremented.
608 @param rasterizer May be NULL. The new rasterizer to be installed in
609 the paint.
610 @return rasterizer
611 */
612 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
613
reed@google.com15356a62011-11-03 19:29:08 +0000614 SkImageFilter* getImageFilter() const { return fImageFilter; }
615 SkImageFilter* setImageFilter(SkImageFilter*);
616
reed@google.com9d07fec2011-03-16 20:02:59 +0000617 /**
618 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
619 * reference count.
620 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000621 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000622
623 /**
624 * Set or clear the looper object.
625 * <p />
626 * Pass NULL to clear any previous looper.
627 * As a convenience, the parameter passed is also returned.
628 * If a previous looper exists in the paint, its reference count is
629 * decremented. If looper is not NULL, its reference count is
630 * incremented.
631 * @param looper May be NULL. The new looper to be installed in the paint.
632 * @return looper
633 */
634 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000635
636 enum Align {
637 kLeft_Align,
638 kCenter_Align,
639 kRight_Align,
640
641 kAlignCount
642 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000643
reed@android.com8a1c16f2008-12-17 15:59:43 +0000644 /** Return the paint's Align value for drawing text.
645 @return the paint's Align value for drawing text.
646 */
647 Align getTextAlign() const { return (Align)fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000648
reed@android.com8a1c16f2008-12-17 15:59:43 +0000649 /** Set the paint's text alignment.
650 @param align set the paint's Align value for drawing text.
651 */
652 void setTextAlign(Align align);
653
654 /** Return the paint's text size.
655 @return the paint's text size.
656 */
657 SkScalar getTextSize() const { return fTextSize; }
658
659 /** Set the paint's text size. This value must be > 0
660 @param textSize set the paint's text size.
661 */
662 void setTextSize(SkScalar textSize);
663
664 /** Return the paint's horizontal scale factor for text. The default value
665 is 1.0.
666 @return the paint's scale factor in X for drawing/measuring text
667 */
668 SkScalar getTextScaleX() const { return fTextScaleX; }
669
670 /** Set the paint's horizontal scale factor for text. The default value
671 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
672 stretch the text narrower.
673 @param scaleX set the paint's scale factor in X for drawing/measuring
674 text.
675 */
676 void setTextScaleX(SkScalar scaleX);
677
678 /** Return the paint's horizontal skew factor for text. The default value
679 is 0.
680 @return the paint's skew factor in X for drawing text.
681 */
682 SkScalar getTextSkewX() const { return fTextSkewX; }
683
684 /** Set the paint's horizontal skew factor for text. The default value
685 is 0. For approximating oblique text, use values around -0.25.
686 @param skewX set the paint's skew factor in X for drawing text.
687 */
688 void setTextSkewX(SkScalar skewX);
689
690 /** Describes how to interpret the text parameters that are passed to paint
691 methods like measureText() and getTextWidths().
692 */
693 enum TextEncoding {
694 kUTF8_TextEncoding, //!< the text parameters are UTF8
695 kUTF16_TextEncoding, //!< the text parameters are UTF16
696 kGlyphID_TextEncoding //!< the text parameters are glyph indices
697 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000698
699 TextEncoding getTextEncoding() const { return (TextEncoding)fTextEncoding; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700
701 void setTextEncoding(TextEncoding encoding);
702
703 struct FontMetrics {
704 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
705 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
706 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
707 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
708 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000709 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
710 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
711 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
712 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000714
reed@android.com8a1c16f2008-12-17 15:59:43 +0000715 /** Return the recommend spacing between lines (which will be
716 fDescent - fAscent + fLeading).
717 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000718 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000719 @param metrics If not null, returns the font metrics for the
720 current typeface/pointsize/etc setting in this
721 paint.
722 @param scale If not 0, return width as if the canvas were scaled
723 by this value
724 @param return the recommended spacing between lines
725 */
726 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000727
reed@android.com8a1c16f2008-12-17 15:59:43 +0000728 /** Return the recommend line spacing. This will be
729 fDescent - fAscent + fLeading
730 */
731 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
732
733 /** Convert the specified text into glyph IDs, returning the number of
734 glyphs ID written. If glyphs is NULL, it is ignore and only the count
735 is returned.
736 */
737 int textToGlyphs(const void* text, size_t byteLength,
738 uint16_t glyphs[]) const;
739
reed@android.coma5dcaf62010-02-05 17:12:32 +0000740 /** Return true if all of the specified text has a corresponding non-zero
741 glyph ID. If any of the code-points in the text are not supported in
742 the typeface (i.e. the glyph ID would be zero), then return false.
743
744 If the text encoding for the paint is kGlyph_TextEncoding, then this
745 returns true if all of the specified glyph IDs are non-zero.
746 */
747 bool containsText(const void* text, size_t byteLength) const;
748
reed@android.com9d3a9852010-01-08 14:07:42 +0000749 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
750 to zero. Note: this does not look at the text-encoding setting in the
751 paint, only at the typeface.
752 */
753 void glyphsToUnichars(const uint16_t glyphs[], int count,
754 SkUnichar text[]) const;
755
reed@android.com8a1c16f2008-12-17 15:59:43 +0000756 /** Return the number of drawable units in the specified text buffer.
757 This looks at the current TextEncoding field of the paint. If you also
758 want to have the text converted into glyph IDs, call textToGlyphs
759 instead.
760 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000761 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000762 return this->textToGlyphs(text, byteLength, NULL);
763 }
764
765 /** Return the width of the text.
766 @param text The text to be measured
767 @param length Number of bytes of text to measure
768 @param bounds If not NULL, returns the bounds of the text,
769 relative to (0, 0).
770 @param scale If not 0, return width as if the canvas were scaled
771 by this value
772 @return The advance width of the text
773 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000774 SkScalar measureText(const void* text, size_t length,
775 SkRect* bounds, SkScalar scale = 0) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000776
777 /** Return the width of the text.
778 @param text Address of the text
779 @param length Number of bytes of text to measure
780 @return The width of the text
781 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000782 SkScalar measureText(const void* text, size_t length) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000783 return this->measureText(text, length, NULL, 0);
784 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000785
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 /** Specify the direction the text buffer should be processed in breakText()
787 */
788 enum TextBufferDirection {
789 /** When measuring text for breakText(), begin at the start of the text
790 buffer and proceed forward through the data. This is the default.
791 */
792 kForward_TextBufferDirection,
793 /** When measuring text for breakText(), begin at the end of the text
794 buffer and proceed backwards through the data.
795 */
796 kBackward_TextBufferDirection
797 };
798
799 /** Return the width of the text.
800 @param text The text to be measured
801 @param length Number of bytes of text to measure
802 @param maxWidth Maximum width. Only the subset of text whose accumulated
803 widths are <= maxWidth are measured.
804 @param measuredWidth Optional. If non-null, this returns the actual
805 width of the measured text.
806 @param tbd Optional. The direction the text buffer should be
807 traversed during measuring.
808 @return The number of bytes of text that were measured. Will be
809 <= length.
810 */
811 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
812 SkScalar* measuredWidth = NULL,
813 TextBufferDirection tbd = kForward_TextBufferDirection)
814 const;
815
816 /** Return the advance widths for the characters in the string.
817 @param text the text
818 @param byteLength number of bytes to of text
819 @param widths If not null, returns the array of advance widths of
820 the glyphs. If not NULL, must be at least a large
821 as the number of unichars in the specified text.
822 @param bounds If not null, returns the bounds for each of
823 character, relative to (0, 0)
824 @return the number of unichars in the specified text.
825 */
826 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
827 SkRect bounds[] = NULL) const;
828
829 /** Return the path (outline) for the specified text.
830 Note: just like SkCanvas::drawText, this will respect the Align setting
831 in the paint.
832 */
833 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
834 SkPath* path) const;
835
djsollen@google.com56c69772011-11-08 19:00:26 +0000836#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000837 const SkGlyph& getUnicharMetrics(SkUnichar);
838 const void* findImage(const SkGlyph&);
839
840 uint32_t getGenerationID() const;
841#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@android.com8a1c16f2008-12-17 15:59:43 +0000847private:
848 SkTypeface* fTypeface;
849 SkScalar fTextSize;
850 SkScalar fTextScaleX;
851 SkScalar fTextSkewX;
852
853 SkPathEffect* fPathEffect;
854 SkShader* fShader;
855 SkXfermode* fXfermode;
856 SkMaskFilter* fMaskFilter;
857 SkColorFilter* fColorFilter;
858 SkRasterizer* fRasterizer;
859 SkDrawLooper* fLooper;
reed@google.com15356a62011-11-03 19:29:08 +0000860 SkImageFilter* fImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000861
862 SkColor fColor;
863 SkScalar fWidth;
864 SkScalar fMiterLimit;
reed@google.com830a23e2011-11-10 15:20:49 +0000865 unsigned fFlags : 14;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000866 unsigned fTextAlign : 2;
867 unsigned fCapType : 2;
868 unsigned fJoinType : 2;
869 unsigned fStyle : 2;
870 unsigned fTextEncoding : 2; // 3 values
agl@chromium.org309485b2009-07-21 17:41:32 +0000871 unsigned fHinting : 2;
djsollen@google.com56c69772011-11-08 19:00:26 +0000872#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000873 uint32_t fGenerationID;
874#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000875
876 SkDrawCacheProc getDrawCacheProc() const;
877 SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
878 bool needFullMetrics) const;
879
880 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
881 int* count, SkRect* bounds) const;
882
883 SkGlyphCache* detachCache(const SkMatrix*) const;
884
885 void descriptorProc(const SkMatrix* deviceMatrix,
886 void (*proc)(const SkDescriptor*, void*),
djsollen@google.com57f49692011-02-23 20:46:31 +0000887 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +0000888
889 const SkRect& computeStrokeFastBounds(const SkRect& orig,
890 SkRect* storage) const;
891
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892 enum {
893 kCanonicalTextSizeForPaths = 64
894 };
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000895 friend class SkAutoGlyphCache;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 friend class SkCanvas;
897 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000898 friend class SkPDFDevice;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899 friend class SkTextToPathIter;
900};
901
reed@google.com6bac9472011-06-21 19:24:00 +0000902///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903
904#include "SkPathEffect.h"
905
906/** \class SkStrokePathEffect
907
908 SkStrokePathEffect simulates stroking inside a patheffect, allowing the
909 caller to have explicit control of when to stroke a path. Typically this is
910 used if the caller wants to stroke before another patheffect is applied
911 (using SkComposePathEffect or SkSumPathEffect).
912*/
913class SkStrokePathEffect : public SkPathEffect {
914public:
915 SkStrokePathEffect(const SkPaint&);
916 SkStrokePathEffect(SkScalar width, SkPaint::Style, SkPaint::Join,
917 SkPaint::Cap, SkScalar miterLimit = -1);
918
919 // overrides
reed@android.com8a1c16f2008-12-17 15:59:43 +0000920 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width);
921
922 // overrides for SkFlattenable
reed@android.com8a1c16f2008-12-17 15:59:43 +0000923 virtual void flatten(SkFlattenableWriteBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000924 virtual Factory getFactory();
925
reed@google.com6bac9472011-06-21 19:24:00 +0000926 static SkFlattenable* CreateProc(SkFlattenableReadBuffer&);
927
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928private:
929 SkScalar fWidth, fMiter;
930 uint8_t fStyle, fJoin, fCap;
931
reed@android.com8a1c16f2008-12-17 15:59:43 +0000932 SkStrokePathEffect(SkFlattenableReadBuffer&);
933
934 typedef SkPathEffect INHERITED;
935
936 // illegal
937 SkStrokePathEffect(const SkStrokePathEffect&);
938 SkStrokePathEffect& operator=(const SkStrokePathEffect&);
939};
940
941#endif
942