blob: f1d56950b08adfced50b4cd509e30c2be6cf53e3 [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"
14#include "SkMath.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000015#include "SkXfermode.h"
16
reed@android.com8a1c16f2008-12-17 15:59:43 +000017class SkAutoGlyphCache;
18class SkColorFilter;
19class SkDescriptor;
20class SkFlattenableReadBuffer;
21class SkFlattenableWriteBuffer;
22struct SkGlyph;
23struct SkRect;
24class SkGlyphCache;
25class 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
agl@chromium.org309485b2009-07-21 17:41:32 +0000102 // when adding extra flags, note that the fFlags member is specified
103 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000105 kAllFlags = 0xFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 };
107
108 /** Return the paint's flags. Use the Flag enum to test flag values.
109 @return the paint's flags (see enums ending in _Flag for bit masks)
110 */
111 uint32_t getFlags() const { return fFlags; }
112
113 /** Set the paint's flags. Use the Flag enum to specific flag values.
114 @param flags The new flag bits for the paint (see Flags enum)
115 */
116 void setFlags(uint32_t flags);
117
118 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
119 @return true if the antialias bit is set in the paint's flags.
120 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000121 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 return SkToBool(this->getFlags() & kAntiAlias_Flag);
123 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
126 @param aa true to enable antialiasing, false to disable it
127 */
128 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 /** Helper for getFlags(), returning true if kDither_Flag bit is set
131 @return true if the dithering bit is set in the paint's flags.
132 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000133 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 return SkToBool(this->getFlags() & kDither_Flag);
135 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
138 @param dither true to enable dithering, false to disable it
139 */
140 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
143 @return true if the lineartext bit is set in the paint's flags
144 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000145 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 return SkToBool(this->getFlags() & kLinearText_Flag);
147 }
148
149 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
150 @param linearText true to set the linearText bit in the paint's flags,
151 false to clear it.
152 */
153 void setLinearText(bool linearText);
154
155 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
156 @return true if the lineartext bit is set in the paint's flags
157 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000158 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 return SkToBool(this->getFlags() & kSubpixelText_Flag);
160 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000161
agl@chromium.org309485b2009-07-21 17:41:32 +0000162 /** Helper for setFlags(), setting or clearing the kSubpixelText_Flag
163 bit @param subpixelText true to set the subpixelText bit in the paint's flags,
164 false to clear it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 */
166 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000167
reed@google.com9d07fec2011-03-16 20:02:59 +0000168 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000169 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
170 }
171
172 /** Helper for setFlags(), setting or clearing the kLCDRenderText_Flag bit
173 @param subpixelRender true to set the subpixelRenderText bit in the paint's flags,
174 false to clear it.
175 */
176 void setLCDRenderText(bool subpixelRender);
177
reed@google.com9d07fec2011-03-16 20:02:59 +0000178 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000179 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
180 }
181
182 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
183 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
184 false to clear it.
185 */
186 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
187
reed@google.com9d07fec2011-03-16 20:02:59 +0000188 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000189 return SkToBool(this->getFlags() & kAutoHinting_Flag);
190 }
191
192 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
193 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
194 paint's flags,
195 false to clear it.
196 */
197 void setAutohinted(bool useAutohinter);
198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
200 @return true if the underlineText bit is set in the paint's flags.
201 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000202 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 return SkToBool(this->getFlags() & kUnderlineText_Flag);
204 }
205
206 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
207 @param underlineText true to set the underlineText bit in the paint's
208 flags, false to clear it.
209 */
210 void setUnderlineText(bool underlineText);
211
212 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
213 @return true if the strikeThruText bit is set in the paint's flags.
214 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000215 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
217 }
218
219 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
220 @param strikeThruText true to set the strikeThruText bit in the
221 paint's flags, false to clear it.
222 */
223 void setStrikeThruText(bool strikeThruText);
224
225 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
226 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
227 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000228 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
230 }
231
232 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
233 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
234 flags, false to clear it.
235 */
236 void setFakeBoldText(bool fakeBoldText);
237
238 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
239 @return true if the kernText bit is set in the paint's flags.
240 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000241 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 return SkToBool(this->getFlags() & kDevKernText_Flag);
243 }
244
245 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
246 @param kernText true to set the kKernText_Flag bit in the paint's
247 flags, false to clear it.
248 */
249 void setDevKernText(bool devKernText);
250
reed@google.com9d07fec2011-03-16 20:02:59 +0000251 bool isFilterBitmap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 return SkToBool(this->getFlags() & kFilterBitmap_Flag);
253 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000254
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 void setFilterBitmap(bool filterBitmap);
256
257 /** Styles apply to rect, oval, path, and text.
258 Bitmaps are always drawn in "fill", and lines are always drawn in
259 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000260
reed@android.comed881c22009-09-15 14:10:42 +0000261 Note: strokeandfill implicitly draws the result with
262 SkPath::kWinding_FillType, so if the original path is even-odd, the
263 results may not appear the same as if it was drawn twice, filled and
264 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 */
266 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000267 kFill_Style, //!< fill the geometry
268 kStroke_Style, //!< stroke the geometry
269 kStrokeAndFill_Style, //!< fill and stroke the geometry
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270
271 kStyleCount,
272 };
273
274 /** Return the paint's style, used for controlling how primitives'
275 geometries are interpreted (except for drawBitmap, which always assumes
276 kFill_Style).
277 @return the paint's Style
278 */
279 Style getStyle() const { return (Style)fStyle; }
280
281 /** Set the paint's style, used for controlling how primitives'
282 geometries are interpreted (except for drawBitmap, which always assumes
283 Fill).
284 @param style The new style to set in the paint
285 */
286 void setStyle(Style style);
287
288 /** Return the paint's color. Note that the color is a 32bit value
289 containing alpha as well as r,g,b. This 32bit value is not
290 premultiplied, meaning that its alpha can be any value, regardless of
291 the values of r,g,b.
292 @return the paint's color (and alpha).
293 */
294 SkColor getColor() const { return fColor; }
295
296 /** Set the paint's color. Note that the color is a 32bit value containing
297 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
298 that its alpha can be any value, regardless of the values of r,g,b.
299 @param color The new color (including alpha) to set in the paint.
300 */
301 void setColor(SkColor color);
302
303 /** Helper to getColor() that just returns the color's alpha value.
304 @return the alpha component of the paint's color.
305 */
306 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 /** Helper to setColor(), that only assigns the color's alpha value,
309 leaving its r,g,b values unchanged.
310 @param a set the alpha component (0..255) of the paint's color.
311 */
312 void setAlpha(U8CPU a);
313
314 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
315 using SkColorSetARGB()
316 @param a The new alpha component (0..255) of the paint's color.
317 @param r The new red component (0..255) of the paint's color.
318 @param g The new green component (0..255) of the paint's color.
319 @param b The new blue component (0..255) of the paint's color.
320 */
321 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
322
reed@google.com9d07fec2011-03-16 20:02:59 +0000323 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000324 <p />
325 A value of 0 strokes in hairline mode.
326 Hairlines always draw 1-pixel wide, regardless of the matrix.
327 @return the paint's stroke width, used whenever the paint's style is
328 Stroke or StrokeAndFill.
329 */
330 SkScalar getStrokeWidth() const { return fWidth; }
331
reed@google.com9d07fec2011-03-16 20:02:59 +0000332 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 Pass 0 to stroke in hairline mode.
334 Hairlines always draw 1-pixel wide, regardless of the matrix.
335 @param width set the paint's stroke width, used whenever the paint's
336 style is Stroke or StrokeAndFill.
337 */
338 void setStrokeWidth(SkScalar width);
339
340 /** Return the paint's stroke miter value. This is used to control the
341 behavior of miter joins when the joins angle is sharp.
342 @return the paint's miter limit, used whenever the paint's style is
343 Stroke or StrokeAndFill.
344 */
345 SkScalar getStrokeMiter() const { return fMiterLimit; }
346
347 /** Set the paint's stroke miter value. This is used to control the
348 behavior of miter joins when the joins angle is sharp. This value must
349 be >= 0.
350 @param miter set the miter limit on the paint, used whenever the
351 paint's style is Stroke or StrokeAndFill.
352 */
353 void setStrokeMiter(SkScalar miter);
354
355 /** Cap enum specifies the settings for the paint's strokecap. This is the
356 treatment that is applied to the beginning and end of each non-closed
357 contour (e.g. lines).
358 */
359 enum Cap {
360 kButt_Cap, //!< begin/end contours with no extension
361 kRound_Cap, //!< begin/end contours with a semi-circle extension
362 kSquare_Cap, //!< begin/end contours with a half square extension
363
364 kCapCount,
365 kDefault_Cap = kButt_Cap
366 };
367
368 /** Join enum specifies the settings for the paint's strokejoin. This is
369 the treatment that is applied to corners in paths and rectangles.
370 */
371 enum Join {
372 kMiter_Join, //!< connect path segments with a sharp join
373 kRound_Join, //!< connect path segments with a round join
374 kBevel_Join, //!< connect path segments with a flat bevel join
375
376 kJoinCount,
377 kDefault_Join = kMiter_Join
378 };
379
380 /** Return the paint's stroke cap type, controlling how the start and end
381 of stroked lines and paths are treated.
382 @return the line cap style for the paint, used whenever the paint's
383 style is Stroke or StrokeAndFill.
384 */
385 Cap getStrokeCap() const { return (Cap)fCapType; }
386
387 /** Set the paint's stroke cap type.
388 @param cap set the paint's line cap style, used whenever the paint's
389 style is Stroke or StrokeAndFill.
390 */
391 void setStrokeCap(Cap cap);
392
393 /** Return the paint's stroke join type.
394 @return the paint's line join style, used whenever the paint's style is
395 Stroke or StrokeAndFill.
396 */
397 Join getStrokeJoin() const { return (Join)fJoinType; }
398
399 /** Set the paint's stroke join type.
400 @param join set the paint's line join style, used whenever the paint's
401 style is Stroke or StrokeAndFill.
402 */
403 void setStrokeJoin(Join join);
404
405 /** Applies any/all effects (patheffect, stroking) to src, returning the
406 result in dst. The result is that drawing src with this paint will be
407 the same as drawing dst with a default paint (at least from the
408 geometric perspective).
409 @param src input path
410 @param dst output path (may be the same as src)
411 @return true if the path should be filled, or false if it should be
412 drawn with a hairline (width == 0)
413 */
414 bool getFillPath(const SkPath& src, SkPath* dst) const;
415
416 /** Returns true if the current paint settings allow for fast computation of
417 bounds (i.e. there is nothing complex like a patheffect that would make
418 the bounds computation expensive.
419 */
reed@android.comd252db02009-04-01 18:31:44 +0000420 bool canComputeFastBounds() const {
421 // use bit-or since no need for early exit
422 return (reinterpret_cast<uintptr_t>(this->getMaskFilter()) |
423 reinterpret_cast<uintptr_t>(this->getLooper()) |
424 reinterpret_cast<uintptr_t>(this->getRasterizer()) |
425 reinterpret_cast<uintptr_t>(this->getPathEffect())) == 0;
426 }
427
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 /** Only call this if canComputeFastBounds() returned true. This takes a
429 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
430 effects in the paint (e.g. stroking). If needed, it uses the storage
431 rect parameter. It returns the adjusted bounds that can then be used
432 for quickReject tests.
reed@google.com9d07fec2011-03-16 20:02:59 +0000433
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 The returned rect will either be orig or storage, thus the caller
435 should not rely on storage being set to the result, but should always
436 use the retured value. It is legal for orig and storage to be the same
437 rect.
reed@google.com9d07fec2011-03-16 20:02:59 +0000438
reed@android.com8a1c16f2008-12-17 15:59:43 +0000439 e.g.
440 if (paint.canComputeFastBounds()) {
441 SkRect r, storage;
442 path.computeBounds(&r, SkPath::kFast_BoundsType);
443 const SkRect& fastR = paint.computeFastBounds(r, &storage);
444 if (canvas->quickReject(fastR, ...)) {
445 // don't draw the path
446 }
447 }
448 */
reed@android.comd252db02009-04-01 18:31:44 +0000449 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
450 return this->getStyle() == kFill_Style ? orig :
451 this->computeStrokeFastBounds(orig, storage);
452 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453
454 /** Get the paint's shader object.
455 <p />
456 The shader's reference count is not affected.
457 @return the paint's shader (or NULL)
458 */
459 SkShader* getShader() const { return fShader; }
460
461 /** Set or clear the shader object.
462 <p />
463 Pass NULL to clear any previous shader.
464 As a convenience, the parameter passed is also returned.
465 If a previous shader exists, its reference count is decremented.
466 If shader is not NULL, its reference count is incremented.
467 @param shader May be NULL. The shader to be installed in the paint
468 @return shader
469 */
470 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000471
reed@android.com8a1c16f2008-12-17 15:59:43 +0000472 /** Get the paint's colorfilter. If there is a colorfilter, its reference
473 count is not changed.
474 @return the paint's colorfilter (or NULL)
475 */
476 SkColorFilter* getColorFilter() const { return fColorFilter; }
477
478 /** Set or clear the paint's colorfilter, returning the parameter.
479 <p />
480 If the paint already has a filter, its reference count is decremented.
481 If filter is not NULL, its reference count is incremented.
482 @param filter May be NULL. The filter to be installed in the paint
483 @return filter
484 */
485 SkColorFilter* setColorFilter(SkColorFilter* filter);
486
487 /** Get the paint's xfermode object.
488 <p />
489 The xfermode's reference count is not affected.
490 @return the paint's xfermode (or NULL)
491 */
492 SkXfermode* getXfermode() const { return fXfermode; }
493
494 /** Set or clear the xfermode object.
495 <p />
496 Pass NULL to clear any previous xfermode.
497 As a convenience, the parameter passed is also returned.
498 If a previous xfermode exists, its reference count is decremented.
499 If xfermode is not NULL, its reference count is incremented.
500 @param xfermode May be NULL. The new xfermode to be installed in the
501 paint
502 @return xfermode
503 */
504 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000505
506 /** Create an xfermode based on the specified Mode, and assign it into the
507 paint, returning the mode that was set. If the Mode is SrcOver, then
508 the paint's xfermode is set to null.
509 */
reed@android.com0baf1932009-06-24 12:41:42 +0000510 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000511
reed@android.com8a1c16f2008-12-17 15:59:43 +0000512 /** Get the paint's patheffect object.
513 <p />
514 The patheffect reference count is not affected.
515 @return the paint's patheffect (or NULL)
516 */
517 SkPathEffect* getPathEffect() const { return fPathEffect; }
518
519 /** Set or clear the patheffect object.
520 <p />
521 Pass NULL to clear any previous patheffect.
522 As a convenience, the parameter passed is also returned.
523 If a previous patheffect exists, its reference count is decremented.
524 If patheffect is not NULL, its reference count is incremented.
525 @param effect May be NULL. The new patheffect to be installed in the
526 paint
527 @return effect
528 */
529 SkPathEffect* setPathEffect(SkPathEffect* effect);
530
531 /** Get the paint's maskfilter object.
532 <p />
533 The maskfilter reference count is not affected.
534 @return the paint's maskfilter (or NULL)
535 */
536 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
537
538 /** Set or clear the maskfilter object.
539 <p />
540 Pass NULL to clear any previous maskfilter.
541 As a convenience, the parameter passed is also returned.
542 If a previous maskfilter exists, its reference count is decremented.
543 If maskfilter is not NULL, its reference count is incremented.
544 @param maskfilter May be NULL. The new maskfilter to be installed in
545 the paint
546 @return maskfilter
547 */
548 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
549
550 // These attributes are for text/fonts
551
552 /** Get the paint's typeface object.
553 <p />
554 The typeface object identifies which font to use when drawing or
555 measuring text. The typeface reference count is not affected.
556 @return the paint's typeface (or NULL)
557 */
558 SkTypeface* getTypeface() const { return fTypeface; }
559
560 /** Set or clear the typeface object.
561 <p />
562 Pass NULL to clear any previous typeface.
563 As a convenience, the parameter passed is also returned.
564 If a previous typeface exists, its reference count is decremented.
565 If typeface is not NULL, its reference count is incremented.
566 @param typeface May be NULL. The new typeface to be installed in the
567 paint
568 @return typeface
569 */
570 SkTypeface* setTypeface(SkTypeface* typeface);
571
572 /** Get the paint's rasterizer (or NULL).
573 <p />
574 The raster controls how paths/text are turned into alpha masks.
575 @return the paint's rasterizer (or NULL)
576 */
577 SkRasterizer* getRasterizer() const { return fRasterizer; }
578
579 /** Set or clear the rasterizer object.
580 <p />
581 Pass NULL to clear any previous rasterizer.
582 As a convenience, the parameter passed is also returned.
583 If a previous rasterizer exists in the paint, its reference count is
584 decremented. If rasterizer is not NULL, its reference count is
585 incremented.
586 @param rasterizer May be NULL. The new rasterizer to be installed in
587 the paint.
588 @return rasterizer
589 */
590 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
591
reed@google.com9d07fec2011-03-16 20:02:59 +0000592 /**
593 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
594 * reference count.
595 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000596 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000597
598 /**
599 * Set or clear the looper object.
600 * <p />
601 * Pass NULL to clear any previous looper.
602 * As a convenience, the parameter passed is also returned.
603 * If a previous looper exists in the paint, its reference count is
604 * decremented. If looper is not NULL, its reference count is
605 * incremented.
606 * @param looper May be NULL. The new looper to be installed in the paint.
607 * @return looper
608 */
609 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000610
611 enum Align {
612 kLeft_Align,
613 kCenter_Align,
614 kRight_Align,
615
616 kAlignCount
617 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000618
reed@android.com8a1c16f2008-12-17 15:59:43 +0000619 /** Return the paint's Align value for drawing text.
620 @return the paint's Align value for drawing text.
621 */
622 Align getTextAlign() const { return (Align)fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000623
reed@android.com8a1c16f2008-12-17 15:59:43 +0000624 /** Set the paint's text alignment.
625 @param align set the paint's Align value for drawing text.
626 */
627 void setTextAlign(Align align);
628
629 /** Return the paint's text size.
630 @return the paint's text size.
631 */
632 SkScalar getTextSize() const { return fTextSize; }
633
634 /** Set the paint's text size. This value must be > 0
635 @param textSize set the paint's text size.
636 */
637 void setTextSize(SkScalar textSize);
638
639 /** Return the paint's horizontal scale factor for text. The default value
640 is 1.0.
641 @return the paint's scale factor in X for drawing/measuring text
642 */
643 SkScalar getTextScaleX() const { return fTextScaleX; }
644
645 /** Set the paint's horizontal scale factor for text. The default value
646 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
647 stretch the text narrower.
648 @param scaleX set the paint's scale factor in X for drawing/measuring
649 text.
650 */
651 void setTextScaleX(SkScalar scaleX);
652
653 /** Return the paint's horizontal skew factor for text. The default value
654 is 0.
655 @return the paint's skew factor in X for drawing text.
656 */
657 SkScalar getTextSkewX() const { return fTextSkewX; }
658
659 /** Set the paint's horizontal skew factor for text. The default value
660 is 0. For approximating oblique text, use values around -0.25.
661 @param skewX set the paint's skew factor in X for drawing text.
662 */
663 void setTextSkewX(SkScalar skewX);
664
665 /** Describes how to interpret the text parameters that are passed to paint
666 methods like measureText() and getTextWidths().
667 */
668 enum TextEncoding {
669 kUTF8_TextEncoding, //!< the text parameters are UTF8
670 kUTF16_TextEncoding, //!< the text parameters are UTF16
671 kGlyphID_TextEncoding //!< the text parameters are glyph indices
672 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000673
674 TextEncoding getTextEncoding() const { return (TextEncoding)fTextEncoding; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675
676 void setTextEncoding(TextEncoding encoding);
677
678 struct FontMetrics {
679 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
680 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
681 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
682 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
683 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000684 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
685 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
686 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
687 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000689
reed@android.com8a1c16f2008-12-17 15:59:43 +0000690 /** Return the recommend spacing between lines (which will be
691 fDescent - fAscent + fLeading).
692 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000693 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000694 @param metrics If not null, returns the font metrics for the
695 current typeface/pointsize/etc setting in this
696 paint.
697 @param scale If not 0, return width as if the canvas were scaled
698 by this value
699 @param return the recommended spacing between lines
700 */
701 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000702
reed@android.com8a1c16f2008-12-17 15:59:43 +0000703 /** Return the recommend line spacing. This will be
704 fDescent - fAscent + fLeading
705 */
706 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
707
708 /** Convert the specified text into glyph IDs, returning the number of
709 glyphs ID written. If glyphs is NULL, it is ignore and only the count
710 is returned.
711 */
712 int textToGlyphs(const void* text, size_t byteLength,
713 uint16_t glyphs[]) const;
714
reed@android.coma5dcaf62010-02-05 17:12:32 +0000715 /** Return true if all of the specified text has a corresponding non-zero
716 glyph ID. If any of the code-points in the text are not supported in
717 the typeface (i.e. the glyph ID would be zero), then return false.
718
719 If the text encoding for the paint is kGlyph_TextEncoding, then this
720 returns true if all of the specified glyph IDs are non-zero.
721 */
722 bool containsText(const void* text, size_t byteLength) const;
723
reed@android.com9d3a9852010-01-08 14:07:42 +0000724 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
725 to zero. Note: this does not look at the text-encoding setting in the
726 paint, only at the typeface.
727 */
728 void glyphsToUnichars(const uint16_t glyphs[], int count,
729 SkUnichar text[]) const;
730
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731 /** Return the number of drawable units in the specified text buffer.
732 This looks at the current TextEncoding field of the paint. If you also
733 want to have the text converted into glyph IDs, call textToGlyphs
734 instead.
735 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000736 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000737 return this->textToGlyphs(text, byteLength, NULL);
738 }
739
740 /** Return the width of the text.
741 @param text The text to be measured
742 @param length Number of bytes of text to measure
743 @param bounds If not NULL, returns the bounds of the text,
744 relative to (0, 0).
745 @param scale If not 0, return width as if the canvas were scaled
746 by this value
747 @return The advance width of the text
748 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000749 SkScalar measureText(const void* text, size_t length,
750 SkRect* bounds, SkScalar scale = 0) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751
752 /** Return the width of the text.
753 @param text Address of the text
754 @param length Number of bytes of text to measure
755 @return The width of the text
756 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000757 SkScalar measureText(const void* text, size_t length) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000758 return this->measureText(text, length, NULL, 0);
759 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000760
reed@android.com8a1c16f2008-12-17 15:59:43 +0000761 /** Specify the direction the text buffer should be processed in breakText()
762 */
763 enum TextBufferDirection {
764 /** When measuring text for breakText(), begin at the start of the text
765 buffer and proceed forward through the data. This is the default.
766 */
767 kForward_TextBufferDirection,
768 /** When measuring text for breakText(), begin at the end of the text
769 buffer and proceed backwards through the data.
770 */
771 kBackward_TextBufferDirection
772 };
773
774 /** Return the width of the text.
775 @param text The text to be measured
776 @param length Number of bytes of text to measure
777 @param maxWidth Maximum width. Only the subset of text whose accumulated
778 widths are <= maxWidth are measured.
779 @param measuredWidth Optional. If non-null, this returns the actual
780 width of the measured text.
781 @param tbd Optional. The direction the text buffer should be
782 traversed during measuring.
783 @return The number of bytes of text that were measured. Will be
784 <= length.
785 */
786 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
787 SkScalar* measuredWidth = NULL,
788 TextBufferDirection tbd = kForward_TextBufferDirection)
789 const;
790
791 /** Return the advance widths for the characters in the string.
792 @param text the text
793 @param byteLength number of bytes to of text
794 @param widths If not null, returns the array of advance widths of
795 the glyphs. If not NULL, must be at least a large
796 as the number of unichars in the specified text.
797 @param bounds If not null, returns the bounds for each of
798 character, relative to (0, 0)
799 @return the number of unichars in the specified text.
800 */
801 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
802 SkRect bounds[] = NULL) const;
803
804 /** Return the path (outline) for the specified text.
805 Note: just like SkCanvas::drawText, this will respect the Align setting
806 in the paint.
807 */
808 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
809 SkPath* path) const;
810
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000811#ifdef ANDROID
812 const SkGlyph& getUnicharMetrics(SkUnichar);
813 const void* findImage(const SkGlyph&);
814
815 uint32_t getGenerationID() const;
816#endif
817
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818private:
819 SkTypeface* fTypeface;
820 SkScalar fTextSize;
821 SkScalar fTextScaleX;
822 SkScalar fTextSkewX;
823
824 SkPathEffect* fPathEffect;
825 SkShader* fShader;
826 SkXfermode* fXfermode;
827 SkMaskFilter* fMaskFilter;
828 SkColorFilter* fColorFilter;
829 SkRasterizer* fRasterizer;
830 SkDrawLooper* fLooper;
831
832 SkColor fColor;
833 SkScalar fWidth;
834 SkScalar fMiterLimit;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000835 unsigned fFlags : 12;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836 unsigned fTextAlign : 2;
837 unsigned fCapType : 2;
838 unsigned fJoinType : 2;
839 unsigned fStyle : 2;
840 unsigned fTextEncoding : 2; // 3 values
agl@chromium.org309485b2009-07-21 17:41:32 +0000841 unsigned fHinting : 2;
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000842#ifdef ANDROID
843 uint32_t fGenerationID;
844#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845
846 SkDrawCacheProc getDrawCacheProc() const;
847 SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
848 bool needFullMetrics) const;
849
850 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
851 int* count, SkRect* bounds) const;
852
853 SkGlyphCache* detachCache(const SkMatrix*) const;
854
855 void descriptorProc(const SkMatrix* deviceMatrix,
856 void (*proc)(const SkDescriptor*, void*),
djsollen@google.com57f49692011-02-23 20:46:31 +0000857 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +0000858
859 const SkRect& computeStrokeFastBounds(const SkRect& orig,
860 SkRect* storage) const;
861
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862 enum {
863 kCanonicalTextSizeForPaths = 64
864 };
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000865 friend class SkAutoGlyphCache;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000866 friend class SkCanvas;
867 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000868 friend class SkPDFDevice;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000869 friend class SkTextToPathIter;
870};
871
reed@google.com6bac9472011-06-21 19:24:00 +0000872///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000873
874#include "SkPathEffect.h"
875
876/** \class SkStrokePathEffect
877
878 SkStrokePathEffect simulates stroking inside a patheffect, allowing the
879 caller to have explicit control of when to stroke a path. Typically this is
880 used if the caller wants to stroke before another patheffect is applied
881 (using SkComposePathEffect or SkSumPathEffect).
882*/
883class SkStrokePathEffect : public SkPathEffect {
884public:
885 SkStrokePathEffect(const SkPaint&);
886 SkStrokePathEffect(SkScalar width, SkPaint::Style, SkPaint::Join,
887 SkPaint::Cap, SkScalar miterLimit = -1);
888
889 // overrides
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width);
891
892 // overrides for SkFlattenable
reed@android.com8a1c16f2008-12-17 15:59:43 +0000893 virtual void flatten(SkFlattenableWriteBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000894 virtual Factory getFactory();
895
reed@google.com6bac9472011-06-21 19:24:00 +0000896 static SkFlattenable* CreateProc(SkFlattenableReadBuffer&);
897
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898private:
899 SkScalar fWidth, fMiter;
900 uint8_t fStyle, fJoin, fCap;
901
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902 SkStrokePathEffect(SkFlattenableReadBuffer&);
903
904 typedef SkPathEffect INHERITED;
905
906 // illegal
907 SkStrokePathEffect(const SkStrokePathEffect&);
908 SkStrokePathEffect& operator=(const SkStrokePathEffect&);
909};
910
911#endif
912