blob: af5978d7b3fd41b1650af688e9fdf6ae54530a19 [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@google.comed43dff2013-06-04 16:56:27 +000016#include "SkMatrix.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000017#include "SkXfermode.h"
18
reed@google.comb0a34d82012-07-11 19:57:55 +000019class SkAnnotation;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020class SkAutoGlyphCache;
21class SkColorFilter;
22class SkDescriptor;
bungeman@google.com532470f2013-01-22 19:25:14 +000023struct SkDeviceProperties;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000024class SkReadBuffer;
25class SkWriteBuffer;
reed@android.com8a1c16f2008-12-17 15:59:43 +000026struct SkGlyph;
27struct SkRect;
28class SkGlyphCache;
reed@google.com15356a62011-11-03 19:29:08 +000029class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000030class SkMaskFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031class SkPath;
32class SkPathEffect;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000033struct SkPoint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000034class SkRasterizer;
35class SkShader;
reed@android.com8a1c16f2008-12-17 15:59:43 +000036class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037
38typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
39 SkFixed x, SkFixed y);
40
41typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
42
humper@google.comb0889472013-07-09 21:37:14 +000043#define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag
44
reed@android.com8a1c16f2008-12-17 15:59:43 +000045/** \class SkPaint
46
47 The SkPaint class holds the style and color information about how to draw
48 geometries, text and bitmaps.
49*/
humper@google.comb0889472013-07-09 21:37:14 +000050
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000051class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052public:
53 SkPaint();
54 SkPaint(const SkPaint& paint);
55 ~SkPaint();
56
57 SkPaint& operator=(const SkPaint&);
58
mtkleinbc97ef42014-08-25 10:10:47 -070059 /** operator== may give false negatives: two paints that draw equivalently
60 may return false. It will never give false positives: two paints that
61 are not equivalent always return false.
62 */
robertphillips@google.comb2657412013-08-07 22:36:29 +000063 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
64 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
65 return !(a == b);
66 }
67
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000068 void flatten(SkWriteBuffer&) const;
69 void unflatten(SkReadBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
71 /** Restores the paint to its initial settings.
72 */
73 void reset();
74
agl@chromium.org309485b2009-07-21 17:41:32 +000075 /** Specifies the level of hinting to be performed. These names are taken
76 from the Gnome/Cairo names for the same. They are translated into
77 Freetype concepts the same as in cairo-ft-font.c:
78 kNo_Hinting -> FT_LOAD_NO_HINTING
79 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
80 kNormal_Hinting -> <default, no option>
81 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
82 subpixel glyphs, in which case TARGET_LCD or
83 TARGET_LCD_V is used>
84 */
85 enum Hinting {
86 kNo_Hinting = 0,
87 kSlight_Hinting = 1,
88 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000089 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000090 };
91
reed@google.com9d07fec2011-03-16 20:02:59 +000092 Hinting getHinting() const {
reedf59eab22014-07-14 14:39:15 -070093 return static_cast<Hinting>(fBitfields.fHinting);
agl@chromium.org309485b2009-07-21 17:41:32 +000094 }
95
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +000096 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 /** Specifies the bit values that are stored in the paint's flags.
99 */
100 enum Flags {
101 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 kDither_Flag = 0x04, //!< mask to enable dithering
103 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
104 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
105 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
106 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000107 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000109 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000110 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000111 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000112 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000113 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000114 kDistanceFieldTextTEMP_Flag = 0x4000, //!< TEMPORARY mask to enable distance fields
115 // currently overrides LCD and subpixel rendering
agl@chromium.org309485b2009-07-21 17:41:32 +0000116 // when adding extra flags, note that the fFlags member is specified
117 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
humper@google.com387db0a2013-07-09 14:13:04 +0000119 kAllFlags = 0xFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 };
121
122 /** Return the paint's flags. Use the Flag enum to test flag values.
123 @return the paint's flags (see enums ending in _Flag for bit masks)
124 */
reedf59eab22014-07-14 14:39:15 -0700125 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126
127 /** Set the paint's flags. Use the Flag enum to specific flag values.
128 @param flags The new flag bits for the paint (see Flags enum)
129 */
130 void setFlags(uint32_t flags);
131
132 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
133 @return true if the antialias bit is set in the paint's flags.
134 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000135 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 return SkToBool(this->getFlags() & kAntiAlias_Flag);
137 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
140 @param aa true to enable antialiasing, false to disable it
141 */
142 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 /** Helper for getFlags(), returning true if kDither_Flag bit is set
145 @return true if the dithering bit is set in the paint's flags.
146 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000147 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 return SkToBool(this->getFlags() & kDither_Flag);
149 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
152 @param dither true to enable dithering, false to disable it
153 */
154 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
157 @return true if the lineartext bit is set in the paint's flags
158 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000159 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 return SkToBool(this->getFlags() & kLinearText_Flag);
161 }
162
163 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
164 @param linearText true to set the linearText bit in the paint's flags,
165 false to clear it.
166 */
167 void setLinearText(bool linearText);
168
169 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
170 @return true if the lineartext bit is set in the paint's flags
171 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000172 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 return SkToBool(this->getFlags() & kSubpixelText_Flag);
174 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000175
reed@google.com84b437e2011-08-01 12:45:35 +0000176 /**
177 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
178 * @param subpixelText true to set the subpixelText bit in the paint's
179 * flags, false to clear it.
180 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000182
reed@google.com9d07fec2011-03-16 20:02:59 +0000183 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000184 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
185 }
186
reed@google.com84b437e2011-08-01 12:45:35 +0000187 /**
188 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
189 * Note: antialiasing must also be on for lcd rendering
190 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
191 * false to clear it.
192 */
193 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000194
reed@google.com9d07fec2011-03-16 20:02:59 +0000195 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000196 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
197 }
198
199 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
200 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
201 false to clear it.
202 */
203 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
204
reed@google.com9d07fec2011-03-16 20:02:59 +0000205 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000206 return SkToBool(this->getFlags() & kAutoHinting_Flag);
207 }
208
209 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
210 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
211 paint's flags,
212 false to clear it.
213 */
214 void setAutohinted(bool useAutohinter);
215
reed@google.com830a23e2011-11-10 15:20:49 +0000216 bool isVerticalText() const {
217 return SkToBool(this->getFlags() & kVerticalText_Flag);
218 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000219
reed@google.com830a23e2011-11-10 15:20:49 +0000220 /**
221 * Helper for setting or clearing the kVerticalText_Flag bit in
222 * setFlags(...).
223 *
224 * If this bit is set, then advances are treated as Y values rather than
225 * X values, and drawText will places its glyphs vertically rather than
226 * horizontally.
227 */
228 void setVerticalText(bool);
229
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
231 @return true if the underlineText bit is set in the paint's flags.
232 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000233 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 return SkToBool(this->getFlags() & kUnderlineText_Flag);
235 }
236
237 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
238 @param underlineText true to set the underlineText bit in the paint's
239 flags, false to clear it.
240 */
241 void setUnderlineText(bool underlineText);
242
243 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
244 @return true if the strikeThruText bit is set in the paint's flags.
245 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000246 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
248 }
249
250 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
251 @param strikeThruText true to set the strikeThruText bit in the
252 paint's flags, false to clear it.
253 */
254 void setStrikeThruText(bool strikeThruText);
255
256 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
257 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
258 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000259 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
261 }
262
263 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
264 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
265 flags, false to clear it.
266 */
267 void setFakeBoldText(bool fakeBoldText);
268
269 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
270 @return true if the kernText bit is set in the paint's flags.
271 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000272 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 return SkToBool(this->getFlags() & kDevKernText_Flag);
274 }
275
276 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
277 @param kernText true to set the kKernText_Flag bit in the paint's
278 flags, false to clear it.
279 */
280 void setDevKernText(bool devKernText);
281
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000282 /** Helper for getFlags(), returns true if kDistanceFieldTextTEMP_Flag bit is set
283 @return true if the distanceFieldText bit is set in the paint's flags.
284 */
285 bool isDistanceFieldTextTEMP() const {
286 return SkToBool(this->getFlags() & kDistanceFieldTextTEMP_Flag);
287 }
288
289 /** Helper for setFlags(), setting or clearing the kDistanceFieldTextTEMP_Flag bit
290 @param distanceFieldText true to set the kDistanceFieldTextTEMP_Flag bit in the paint's
291 flags, false to clear it.
292 */
293 void setDistanceFieldTextTEMP(bool distanceFieldText);
294
reed@google.comc9683152013-07-18 13:47:01 +0000295 enum FilterLevel {
296 kNone_FilterLevel,
297 kLow_FilterLevel,
298 kMedium_FilterLevel,
299 kHigh_FilterLevel
300 };
301
302 /**
303 * Return the filter level. This affects the quality (and performance) of
304 * drawing scaled images.
305 */
reedf59eab22014-07-14 14:39:15 -0700306 FilterLevel getFilterLevel() const {
307 return (FilterLevel)fBitfields.fFilterLevel;
308 }
reed@google.comc9683152013-07-18 13:47:01 +0000309
310 /**
311 * Set the filter level. This affects the quality (and performance) of
312 * drawing scaled images.
313 */
314 void setFilterLevel(FilterLevel);
315
316 /**
reed@google.comc9683152013-07-18 13:47:01 +0000317 * If the predicate is true, set the filterLevel to Low, else set it to
318 * None.
319 */
reed@google.com44699382013-10-31 17:28:30 +0000320 SK_ATTR_DEPRECATED("use setFilterLevel")
reed@google.comc9683152013-07-18 13:47:01 +0000321 void setFilterBitmap(bool doFilter) {
322 this->setFilterLevel(doFilter ? kLow_FilterLevel : kNone_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000324
reed@google.comc9683152013-07-18 13:47:01 +0000325 /**
reed@google.comc9683152013-07-18 13:47:01 +0000326 * Returns true if getFilterLevel() returns anything other than None.
327 */
reed@google.com44699382013-10-31 17:28:30 +0000328 SK_ATTR_DEPRECATED("use getFilterLevel")
reed@google.comc9683152013-07-18 13:47:01 +0000329 bool isFilterBitmap() const {
330 return kNone_FilterLevel != this->getFilterLevel();
331 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332
333 /** Styles apply to rect, oval, path, and text.
334 Bitmaps are always drawn in "fill", and lines are always drawn in
335 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000336
reed@android.comed881c22009-09-15 14:10:42 +0000337 Note: strokeandfill implicitly draws the result with
338 SkPath::kWinding_FillType, so if the original path is even-odd, the
339 results may not appear the same as if it was drawn twice, filled and
340 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 */
342 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000343 kFill_Style, //!< fill the geometry
344 kStroke_Style, //!< stroke the geometry
345 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000346 };
347 enum {
348 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 };
350
351 /** Return the paint's style, used for controlling how primitives'
352 geometries are interpreted (except for drawBitmap, which always assumes
353 kFill_Style).
354 @return the paint's Style
355 */
reedf59eab22014-07-14 14:39:15 -0700356 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357
358 /** Set the paint's style, used for controlling how primitives'
359 geometries are interpreted (except for drawBitmap, which always assumes
360 Fill).
361 @param style The new style to set in the paint
362 */
363 void setStyle(Style style);
364
365 /** Return the paint's color. Note that the color is a 32bit value
366 containing alpha as well as r,g,b. This 32bit value is not
367 premultiplied, meaning that its alpha can be any value, regardless of
368 the values of r,g,b.
369 @return the paint's color (and alpha).
370 */
371 SkColor getColor() const { return fColor; }
372
373 /** Set the paint's color. Note that the color is a 32bit value containing
374 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
375 that its alpha can be any value, regardless of the values of r,g,b.
376 @param color The new color (including alpha) to set in the paint.
377 */
378 void setColor(SkColor color);
379
380 /** Helper to getColor() that just returns the color's alpha value.
381 @return the alpha component of the paint's color.
382 */
383 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000384
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 /** Helper to setColor(), that only assigns the color's alpha value,
386 leaving its r,g,b values unchanged.
387 @param a set the alpha component (0..255) of the paint's color.
388 */
389 void setAlpha(U8CPU a);
390
391 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
392 using SkColorSetARGB()
393 @param a The new alpha component (0..255) of the paint's color.
394 @param r The new red component (0..255) of the paint's color.
395 @param g The new green component (0..255) of the paint's color.
396 @param b The new blue component (0..255) of the paint's color.
397 */
398 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
399
reed@google.com9d07fec2011-03-16 20:02:59 +0000400 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 <p />
402 A value of 0 strokes in hairline mode.
403 Hairlines always draw 1-pixel wide, regardless of the matrix.
404 @return the paint's stroke width, used whenever the paint's style is
405 Stroke or StrokeAndFill.
406 */
407 SkScalar getStrokeWidth() const { return fWidth; }
408
reed@google.com9d07fec2011-03-16 20:02:59 +0000409 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 Pass 0 to stroke in hairline mode.
411 Hairlines always draw 1-pixel wide, regardless of the matrix.
412 @param width set the paint's stroke width, used whenever the paint's
413 style is Stroke or StrokeAndFill.
414 */
415 void setStrokeWidth(SkScalar width);
416
417 /** Return the paint's stroke miter value. This is used to control the
418 behavior of miter joins when the joins angle is sharp.
419 @return the paint's miter limit, used whenever the paint's style is
420 Stroke or StrokeAndFill.
421 */
422 SkScalar getStrokeMiter() const { return fMiterLimit; }
423
424 /** Set the paint's stroke miter value. This is used to control the
425 behavior of miter joins when the joins angle is sharp. This value must
426 be >= 0.
427 @param miter set the miter limit on the paint, used whenever the
428 paint's style is Stroke or StrokeAndFill.
429 */
430 void setStrokeMiter(SkScalar miter);
431
432 /** Cap enum specifies the settings for the paint's strokecap. This is the
433 treatment that is applied to the beginning and end of each non-closed
434 contour (e.g. lines).
435 */
436 enum Cap {
437 kButt_Cap, //!< begin/end contours with no extension
438 kRound_Cap, //!< begin/end contours with a semi-circle extension
439 kSquare_Cap, //!< begin/end contours with a half square extension
440
441 kCapCount,
442 kDefault_Cap = kButt_Cap
443 };
444
445 /** Join enum specifies the settings for the paint's strokejoin. This is
446 the treatment that is applied to corners in paths and rectangles.
447 */
448 enum Join {
449 kMiter_Join, //!< connect path segments with a sharp join
450 kRound_Join, //!< connect path segments with a round join
451 kBevel_Join, //!< connect path segments with a flat bevel join
452
453 kJoinCount,
454 kDefault_Join = kMiter_Join
455 };
456
457 /** Return the paint's stroke cap type, controlling how the start and end
458 of stroked lines and paths are treated.
459 @return the line cap style for the paint, used whenever the paint's
460 style is Stroke or StrokeAndFill.
461 */
reedf59eab22014-07-14 14:39:15 -0700462 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463
464 /** Set the paint's stroke cap type.
465 @param cap set the paint's line cap style, used whenever the paint's
466 style is Stroke or StrokeAndFill.
467 */
468 void setStrokeCap(Cap cap);
469
470 /** Return the paint's stroke join type.
471 @return the paint's line join style, used whenever the paint's style is
472 Stroke or StrokeAndFill.
473 */
reedf59eab22014-07-14 14:39:15 -0700474 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475
476 /** Set the paint's stroke join type.
477 @param join set the paint's line join style, used whenever the paint's
478 style is Stroke or StrokeAndFill.
479 */
480 void setStrokeJoin(Join join);
481
reed@google.com4bbdeac2013-01-24 21:03:11 +0000482 /**
483 * Applies any/all effects (patheffect, stroking) to src, returning the
484 * result in dst. The result is that drawing src with this paint will be
485 * the same as drawing dst with a default paint (at least from the
486 * geometric perspective).
487 *
488 * @param src input path
489 * @param dst output path (may be the same as src)
490 * @param cullRect If not null, the dst path may be culled to this rect.
491 * @return true if the path should be filled, or false if it should be
492 * drawn with a hairline (width == 0)
493 */
494 bool getFillPath(const SkPath& src, SkPath* dst,
495 const SkRect* cullRect = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496
reed@android.com8a1c16f2008-12-17 15:59:43 +0000497 /** Get the paint's shader object.
498 <p />
499 The shader's reference count is not affected.
500 @return the paint's shader (or NULL)
501 */
502 SkShader* getShader() const { return fShader; }
503
504 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000505 * Shaders specify the source color(s) for what is being drawn. If a paint
506 * has no shader, then the paint's color is used. If the paint has a
507 * shader, then the shader's color(s) are use instead, but they are
508 * modulated by the paint's alpha. This makes it easy to create a shader
509 * once (e.g. bitmap tiling or gradient) and then change its transparency
510 * w/o having to modify the original shader... only the paint's alpha needs
511 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000512 *
513 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
514 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
515 * then the shader will use the paint's entire color to "colorize" its output (modulating the
516 * bitmap's alpha with the paint's color+alpha).
517 *
reed@google.com880dc472012-05-11 14:47:03 +0000518 * Pass NULL to clear any previous shader.
519 * As a convenience, the parameter passed is also returned.
520 * If a previous shader exists, its reference count is decremented.
521 * If shader is not NULL, its reference count is incremented.
522 * @param shader May be NULL. The shader to be installed in the paint
523 * @return shader
524 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000525 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000526
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527 /** Get the paint's colorfilter. If there is a colorfilter, its reference
528 count is not changed.
529 @return the paint's colorfilter (or NULL)
530 */
531 SkColorFilter* getColorFilter() const { return fColorFilter; }
532
533 /** Set or clear the paint's colorfilter, returning the parameter.
534 <p />
535 If the paint already has a filter, its reference count is decremented.
536 If filter is not NULL, its reference count is incremented.
537 @param filter May be NULL. The filter to be installed in the paint
538 @return filter
539 */
540 SkColorFilter* setColorFilter(SkColorFilter* filter);
541
542 /** Get the paint's xfermode object.
543 <p />
544 The xfermode's reference count is not affected.
545 @return the paint's xfermode (or NULL)
546 */
547 SkXfermode* getXfermode() const { return fXfermode; }
548
549 /** Set or clear the xfermode object.
550 <p />
551 Pass NULL to clear any previous xfermode.
552 As a convenience, the parameter passed is also returned.
553 If a previous xfermode exists, its reference count is decremented.
554 If xfermode is not NULL, its reference count is incremented.
555 @param xfermode May be NULL. The new xfermode to be installed in the
556 paint
557 @return xfermode
558 */
559 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000560
561 /** Create an xfermode based on the specified Mode, and assign it into the
562 paint, returning the mode that was set. If the Mode is SrcOver, then
563 the paint's xfermode is set to null.
564 */
reed@android.com0baf1932009-06-24 12:41:42 +0000565 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000566
reed@android.com8a1c16f2008-12-17 15:59:43 +0000567 /** Get the paint's patheffect object.
568 <p />
569 The patheffect reference count is not affected.
570 @return the paint's patheffect (or NULL)
571 */
572 SkPathEffect* getPathEffect() const { return fPathEffect; }
573
574 /** Set or clear the patheffect object.
575 <p />
576 Pass NULL to clear any previous patheffect.
577 As a convenience, the parameter passed is also returned.
578 If a previous patheffect exists, its reference count is decremented.
579 If patheffect is not NULL, its reference count is incremented.
580 @param effect May be NULL. The new patheffect to be installed in the
581 paint
582 @return effect
583 */
584 SkPathEffect* setPathEffect(SkPathEffect* effect);
585
586 /** Get the paint's maskfilter object.
587 <p />
588 The maskfilter reference count is not affected.
589 @return the paint's maskfilter (or NULL)
590 */
591 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
592
593 /** Set or clear the maskfilter object.
594 <p />
595 Pass NULL to clear any previous maskfilter.
596 As a convenience, the parameter passed is also returned.
597 If a previous maskfilter exists, its reference count is decremented.
598 If maskfilter is not NULL, its reference count is incremented.
599 @param maskfilter May be NULL. The new maskfilter to be installed in
600 the paint
601 @return maskfilter
602 */
603 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
604
605 // These attributes are for text/fonts
606
607 /** Get the paint's typeface object.
608 <p />
609 The typeface object identifies which font to use when drawing or
610 measuring text. The typeface reference count is not affected.
611 @return the paint's typeface (or NULL)
612 */
613 SkTypeface* getTypeface() const { return fTypeface; }
614
615 /** Set or clear the typeface object.
616 <p />
617 Pass NULL to clear any previous typeface.
618 As a convenience, the parameter passed is also returned.
619 If a previous typeface exists, its reference count is decremented.
620 If typeface is not NULL, its reference count is incremented.
621 @param typeface May be NULL. The new typeface to be installed in the
622 paint
623 @return typeface
624 */
625 SkTypeface* setTypeface(SkTypeface* typeface);
626
627 /** Get the paint's rasterizer (or NULL).
628 <p />
629 The raster controls how paths/text are turned into alpha masks.
630 @return the paint's rasterizer (or NULL)
631 */
632 SkRasterizer* getRasterizer() const { return fRasterizer; }
633
634 /** Set or clear the rasterizer object.
635 <p />
636 Pass NULL to clear any previous rasterizer.
637 As a convenience, the parameter passed is also returned.
638 If a previous rasterizer exists in the paint, its reference count is
639 decremented. If rasterizer is not NULL, its reference count is
640 incremented.
641 @param rasterizer May be NULL. The new rasterizer to be installed in
642 the paint.
643 @return rasterizer
644 */
645 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
646
reed@google.com15356a62011-11-03 19:29:08 +0000647 SkImageFilter* getImageFilter() const { return fImageFilter; }
648 SkImageFilter* setImageFilter(SkImageFilter*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000649
reed@google.comb0a34d82012-07-11 19:57:55 +0000650 SkAnnotation* getAnnotation() const { return fAnnotation; }
651 SkAnnotation* setAnnotation(SkAnnotation*);
652
653 /**
654 * Returns true if there is an annotation installed on this paint, and
655 * the annotation specifics no-drawing.
656 */
reed@google.com44699382013-10-31 17:28:30 +0000657 SK_ATTR_DEPRECATED("use getAnnotation and check for non-null")
commit-bot@chromium.org950923b2013-10-29 20:44:39 +0000658 bool isNoDrawAnnotation() const { return this->getAnnotation() != NULL; }
reed@google.com15356a62011-11-03 19:29:08 +0000659
reed@google.com9d07fec2011-03-16 20:02:59 +0000660 /**
661 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
662 * reference count.
663 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000664 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000665
666 /**
667 * Set or clear the looper object.
668 * <p />
669 * Pass NULL to clear any previous looper.
670 * As a convenience, the parameter passed is also returned.
671 * If a previous looper exists in the paint, its reference count is
672 * decremented. If looper is not NULL, its reference count is
673 * incremented.
674 * @param looper May be NULL. The new looper to be installed in the paint.
675 * @return looper
676 */
677 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678
679 enum Align {
680 kLeft_Align,
681 kCenter_Align,
682 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000683 };
684 enum {
685 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000686 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000687
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688 /** Return the paint's Align value for drawing text.
689 @return the paint's Align value for drawing text.
690 */
reedf59eab22014-07-14 14:39:15 -0700691 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000692
reed@android.com8a1c16f2008-12-17 15:59:43 +0000693 /** Set the paint's text alignment.
694 @param align set the paint's Align value for drawing text.
695 */
696 void setTextAlign(Align align);
697
698 /** Return the paint's text size.
699 @return the paint's text size.
700 */
701 SkScalar getTextSize() const { return fTextSize; }
702
703 /** Set the paint's text size. This value must be > 0
704 @param textSize set the paint's text size.
705 */
706 void setTextSize(SkScalar textSize);
707
708 /** Return the paint's horizontal scale factor for text. The default value
709 is 1.0.
710 @return the paint's scale factor in X for drawing/measuring text
711 */
712 SkScalar getTextScaleX() const { return fTextScaleX; }
713
714 /** Set the paint's horizontal scale factor for text. The default value
715 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
716 stretch the text narrower.
717 @param scaleX set the paint's scale factor in X for drawing/measuring
718 text.
719 */
720 void setTextScaleX(SkScalar scaleX);
721
722 /** Return the paint's horizontal skew factor for text. The default value
723 is 0.
724 @return the paint's skew factor in X for drawing text.
725 */
726 SkScalar getTextSkewX() const { return fTextSkewX; }
727
728 /** Set the paint's horizontal skew factor for text. The default value
729 is 0. For approximating oblique text, use values around -0.25.
730 @param skewX set the paint's skew factor in X for drawing text.
731 */
732 void setTextSkewX(SkScalar skewX);
733
734 /** Describes how to interpret the text parameters that are passed to paint
735 methods like measureText() and getTextWidths().
736 */
737 enum TextEncoding {
738 kUTF8_TextEncoding, //!< the text parameters are UTF8
739 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000740 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000741 kGlyphID_TextEncoding //!< the text parameters are glyph indices
742 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000743
reedf59eab22014-07-14 14:39:15 -0700744 TextEncoding getTextEncoding() const {
745 return (TextEncoding)fBitfields.fTextEncoding;
746 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747
748 void setTextEncoding(TextEncoding encoding);
749
750 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000751 /** Flags which indicate the confidence level of various metrics.
752 A set flag indicates that the metric may be trusted.
753 */
754 enum FontMetricsFlags {
755 kUnderlineThinknessIsValid_Flag = 1 << 0,
756 kUnderlinePositionIsValid_Flag = 1 << 1,
757 };
758
759 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000760 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
761 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
762 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
763 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
764 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000765 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
766 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000767 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
768 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000769 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
770 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000771 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
772
773 /** Underline Position - position of the top of the Underline stroke
774 relative to the baseline, this can have following values
775 - Negative - means underline should be drawn above baseline.
776 - Positive - means below baseline.
777 - Zero - mean underline should be drawn on baseline.
778 */
779 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
780
781 /** If the fontmetrics has a valid underlinethickness, return true, and set the
782 thickness param to that value. If it doesn't return false and ignore the
783 thickness param.
784 */
785 bool hasUnderlineThickness(SkScalar* thickness) const {
786 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
787 *thickness = fUnderlineThickness;
788 return true;
789 }
790 return false;
791 }
792
793 /** If the fontmetrics has a valid underlineposition, return true, and set the
794 thickness param to that value. If it doesn't return false and ignore the
795 thickness param.
796 */
797 bool hasUnderlinePosition(SkScalar* position) const {
798 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
799 *position = fUnderlinePosition;
800 return true;
801 }
802 return false;
803 }
804
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000806
reed@android.com8a1c16f2008-12-17 15:59:43 +0000807 /** Return the recommend spacing between lines (which will be
808 fDescent - fAscent + fLeading).
809 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000810 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000811 @param metrics If not null, returns the font metrics for the
812 current typeface/pointsize/etc setting in this
813 paint.
814 @param scale If not 0, return width as if the canvas were scaled
815 by this value
816 @param return the recommended spacing between lines
817 */
818 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000819
reed@android.com8a1c16f2008-12-17 15:59:43 +0000820 /** Return the recommend line spacing. This will be
821 fDescent - fAscent + fLeading
822 */
823 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
824
825 /** Convert the specified text into glyph IDs, returning the number of
826 glyphs ID written. If glyphs is NULL, it is ignore and only the count
827 is returned.
828 */
829 int textToGlyphs(const void* text, size_t byteLength,
830 uint16_t glyphs[]) const;
831
reed@android.coma5dcaf62010-02-05 17:12:32 +0000832 /** Return true if all of the specified text has a corresponding non-zero
833 glyph ID. If any of the code-points in the text are not supported in
834 the typeface (i.e. the glyph ID would be zero), then return false.
835
836 If the text encoding for the paint is kGlyph_TextEncoding, then this
837 returns true if all of the specified glyph IDs are non-zero.
838 */
839 bool containsText(const void* text, size_t byteLength) const;
840
reed@android.com9d3a9852010-01-08 14:07:42 +0000841 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
842 to zero. Note: this does not look at the text-encoding setting in the
843 paint, only at the typeface.
844 */
845 void glyphsToUnichars(const uint16_t glyphs[], int count,
846 SkUnichar text[]) const;
847
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848 /** Return the number of drawable units in the specified text buffer.
849 This looks at the current TextEncoding field of the paint. If you also
850 want to have the text converted into glyph IDs, call textToGlyphs
851 instead.
852 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000853 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000854 return this->textToGlyphs(text, byteLength, NULL);
855 }
856
reed@google.com44da42e2011-11-10 20:04:47 +0000857 /** Return the width of the text. This will return the vertical measure
858 * if isVerticalText() is true, in which case the returned value should
859 * be treated has a height instead of a width.
860 *
861 * @param text The text to be measured
862 * @param length Number of bytes of text to measure
863 * @param bounds If not NULL, returns the bounds of the text,
864 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000865 * @return The advance width of the text
866 */
reed99ae8812014-08-26 11:30:01 -0700867 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868
reed@google.com44da42e2011-11-10 20:04:47 +0000869 /** Return the width of the text. This will return the vertical measure
870 * if isVerticalText() is true, in which case the returned value should
871 * be treated has a height instead of a width.
872 *
873 * @param text Address of the text
874 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000875 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000876 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000877 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700878 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000879 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000880
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881 /** Specify the direction the text buffer should be processed in breakText()
882 */
883 enum TextBufferDirection {
884 /** When measuring text for breakText(), begin at the start of the text
885 buffer and proceed forward through the data. This is the default.
886 */
887 kForward_TextBufferDirection,
888 /** When measuring text for breakText(), begin at the end of the text
889 buffer and proceed backwards through the data.
890 */
891 kBackward_TextBufferDirection
892 };
893
reed@google.com44da42e2011-11-10 20:04:47 +0000894 /** Return the number of bytes of text that were measured. If
895 * isVerticalText() is true, then the vertical advances are used for
896 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000897 *
reed@google.com44da42e2011-11-10 20:04:47 +0000898 * @param text The text to be measured
899 * @param length Number of bytes of text to measure
900 * @param maxWidth Maximum width. Only the subset of text whose accumulated
901 * widths are <= maxWidth are measured.
902 * @param measuredWidth Optional. If non-null, this returns the actual
903 * width of the measured text.
904 * @param tbd Optional. The direction the text buffer should be
905 * traversed during measuring.
906 * @return The number of bytes of text that were measured. Will be
907 * <= length.
908 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000909 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
910 SkScalar* measuredWidth = NULL,
911 TextBufferDirection tbd = kForward_TextBufferDirection)
912 const;
913
reed@google.com44da42e2011-11-10 20:04:47 +0000914 /** Return the advances for the text. These will be vertical advances if
915 * isVerticalText() returns true.
916 *
917 * @param text the text
918 * @param byteLength number of bytes to of text
919 * @param widths If not null, returns the array of advances for
920 * the glyphs. If not NULL, must be at least a large
921 * as the number of unichars in the specified text.
922 * @param bounds If not null, returns the bounds for each of
923 * character, relative to (0, 0)
924 * @return the number of unichars in the specified text.
925 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000926 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
927 SkRect bounds[] = NULL) const;
928
929 /** Return the path (outline) for the specified text.
930 Note: just like SkCanvas::drawText, this will respect the Align setting
931 in the paint.
932 */
933 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
934 SkPath* path) const;
935
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000936 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000937 const SkPoint pos[], SkPath* path) const;
938
djsollen@google.com56c69772011-11-08 19:00:26 +0000939#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000940 uint32_t getGenerationID() const;
djsollen@google.com4bd2bdb2013-03-08 18:35:13 +0000941 void setGenerationID(uint32_t generationID);
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000942#endif
943
reed@google.com632e1a22011-10-06 12:37:00 +0000944 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
945 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
946 bool nothingToDraw() const;
947
reed@google.coma584aed2012-05-16 14:06:02 +0000948 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000949 // would prefer to make these private...
950
reed@google.coma584aed2012-05-16 14:06:02 +0000951 /** Returns true if the current paint settings allow for fast computation of
952 bounds (i.e. there is nothing complex like a patheffect that would make
953 the bounds computation expensive.
954 */
955 bool canComputeFastBounds() const {
956 if (this->getLooper()) {
957 return this->getLooper()->canComputeFastBounds(*this);
958 }
959 return !this->getRasterizer();
960 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000961
reed@google.coma584aed2012-05-16 14:06:02 +0000962 /** Only call this if canComputeFastBounds() returned true. This takes a
963 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
964 effects in the paint (e.g. stroking). If needed, it uses the storage
965 rect parameter. It returns the adjusted bounds that can then be used
966 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000967
reed@google.coma584aed2012-05-16 14:06:02 +0000968 The returned rect will either be orig or storage, thus the caller
969 should not rely on storage being set to the result, but should always
970 use the retured value. It is legal for orig and storage to be the same
971 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000972
reed@google.coma584aed2012-05-16 14:06:02 +0000973 e.g.
974 if (paint.canComputeFastBounds()) {
975 SkRect r, storage;
976 path.computeBounds(&r, SkPath::kFast_BoundsType);
977 const SkRect& fastR = paint.computeFastBounds(r, &storage);
978 if (canvas->quickReject(fastR, ...)) {
979 // don't draw the path
980 }
981 }
982 */
983 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
984 SkPaint::Style style = this->getStyle();
985 // ultra fast-case: filling with no effects that affect geometry
986 if (kFill_Style == style) {
987 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
988 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
989 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000990 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +0000991 if (!effects) {
992 return orig;
993 }
994 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000995
reed@google.coma584aed2012-05-16 14:06:02 +0000996 return this->doComputeFastBounds(orig, storage, style);
997 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000998
reed@google.coma584aed2012-05-16 14:06:02 +0000999 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1000 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001001 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001002 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001003
reed@google.coma584aed2012-05-16 14:06:02 +00001004 // Take the style explicitly, so the caller can force us to be stroked
1005 // without having to make a copy of the paint just to change that field.
1006 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1007 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001008
reed@google.comed43dff2013-06-04 16:56:27 +00001009 /**
1010 * Return a matrix that applies the paint's text values: size, scale, skew
1011 */
1012 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1013 SkScalar scaleX, SkScalar skewX) {
1014 matrix->setScale(size * scaleX, size);
1015 if (skewX) {
1016 matrix->postSkew(skewX, 0);
1017 }
1018 return matrix;
1019 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001020
reed@google.comed43dff2013-06-04 16:56:27 +00001021 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1022 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1023 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001024
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001025 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001026
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001027 struct FlatteningTraits {
1028 static void Flatten(SkWriteBuffer& buffer, const SkPaint& paint);
1029 static void Unflatten(SkReadBuffer& buffer, SkPaint* paint);
1030 };
1031
reed@google.comd5f20792012-05-16 14:15:02 +00001032private:
1033 SkTypeface* fTypeface;
reed@google.comd5f20792012-05-16 14:15:02 +00001034 SkPathEffect* fPathEffect;
1035 SkShader* fShader;
1036 SkXfermode* fXfermode;
1037 SkMaskFilter* fMaskFilter;
1038 SkColorFilter* fColorFilter;
1039 SkRasterizer* fRasterizer;
1040 SkDrawLooper* fLooper;
1041 SkImageFilter* fImageFilter;
reed@google.comb0a34d82012-07-11 19:57:55 +00001042 SkAnnotation* fAnnotation;
reed@google.comd5f20792012-05-16 14:15:02 +00001043
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001044 SkScalar fTextSize;
1045 SkScalar fTextScaleX;
1046 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001047 SkColor fColor;
1048 SkScalar fWidth;
1049 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001050 union {
1051 struct {
1052 // all of these bitfields should add up to 32
1053 unsigned fFlags : 16;
1054 unsigned fTextAlign : 2;
1055 unsigned fCapType : 2;
1056 unsigned fJoinType : 2;
1057 unsigned fStyle : 2;
1058 unsigned fTextEncoding : 2; // 3 values
1059 unsigned fHinting : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001060 unsigned fFilterLevel : 2;
1061 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001062 } fBitfields;
1063 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001064 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001065 uint32_t fDirtyBits;
1066
reed@google.comd5f20792012-05-16 14:15:02 +00001067 SkDrawCacheProc getDrawCacheProc() const;
1068 SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
1069 bool needFullMetrics) const;
1070
1071 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1072 int* count, SkRect* bounds) const;
1073
jvanverth2d2a68c2014-06-10 06:42:56 -07001074 SkGlyphCache* detachCache(const SkDeviceProperties* deviceProperties, const SkMatrix*,
1075 bool ignoreGamma) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001076
bungeman@google.com532470f2013-01-22 19:25:14 +00001077 void descriptorProc(const SkDeviceProperties* deviceProperties, const SkMatrix* deviceMatrix,
reed@google.com90808e82013-03-19 14:44:54 +00001078 void (*proc)(SkTypeface*, const SkDescriptor*, void*),
reed@google.comd5f20792012-05-16 14:15:02 +00001079 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +00001080
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001081 static void Term();
1082
reed@android.com8a1c16f2008-12-17 15:59:43 +00001083 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001084 /* This is the size we use when we ask for a glyph's path. We then
1085 * post-transform it as we draw to match the request.
1086 * This is done to try to re-use cache entries for the path.
1087 *
1088 * This value is somewhat arbitrary. In theory, it could be 1, since
1089 * we store paths as floats. However, we get the path from the font
1090 * scaler, and it may represent its paths as fixed-point (or 26.6),
1091 * so we shouldn't ask for something too big (might overflow 16.16)
1092 * or too small (underflow 26.6).
1093 *
1094 * This value could track kMaxSizeForGlyphCache, assuming the above
1095 * constraints, but since we ask for unhinted paths, the two values
1096 * need not match per-se.
1097 */
1098 kCanonicalTextSizeForPaths = 64,
1099
1100 /*
1101 * Above this size (taking into account CTM and textSize), we never use
1102 * the cache for bits or metrics (we might overflow), so we just ask
1103 * for a caononical size and post-transform that.
1104 */
1105 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001106 };
reed@google.comed43dff2013-06-04 16:56:27 +00001107
1108 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1109
reed@google.comed43dff2013-06-04 16:56:27 +00001110 // Set flags/hinting/textSize up to use for drawing text as paths.
1111 // Returns scale factor to restore the original textSize, since will will
1112 // have change it to kCanonicalTextSizeForPaths.
1113 SkScalar setupForAsPaths();
1114
1115 static SkScalar MaxCacheSize2() {
1116 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1117 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1118 return kMag2Max;
1119 }
1120
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001121 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001122 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 friend class SkCanvas;
1124 friend class SkDraw;
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001125 friend class SkGraphics; // So Term() can be called.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001126 friend class SkPDFDevice;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00001127 friend class GrBitmapTextContext;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001128 friend class GrDistanceFieldTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001129 friend class GrStencilAndCoverTextContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001130 friend class SkTextToPathIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001131 friend class SkCanonicalizePaint;
djsollen@google.comb44cd652011-12-01 17:09:21 +00001132
1133#ifdef SK_BUILD_FOR_ANDROID
1134 // In order for the == operator to work properly this must be the last field
1135 // in the struct so that we can do a memcmp to this field's offset.
1136 uint32_t fGenerationID;
1137#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001138};
1139
reed@android.com8a1c16f2008-12-17 15:59:43 +00001140#endif