blob: 3874d98869c4f7f95cb97f69d31983b39edad5c3 [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"
commit-bot@chromium.orgc7a20e42013-05-13 14:09:13 +000018#ifdef SK_BUILD_FOR_ANDROID
19#include "SkPaintOptionsAndroid.h"
20#endif
reed@android.coma0f5d152009-06-22 17:38:10 +000021
reed@google.comb0a34d82012-07-11 19:57:55 +000022class SkAnnotation;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023class SkAutoGlyphCache;
24class SkColorFilter;
25class SkDescriptor;
bungeman@google.com532470f2013-01-22 19:25:14 +000026struct SkDeviceProperties;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000027class SkReadBuffer;
28class SkWriteBuffer;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029struct SkGlyph;
30struct SkRect;
31class SkGlyphCache;
reed@google.com15356a62011-11-03 19:29:08 +000032class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkMaskFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000034class SkPath;
35class SkPathEffect;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000036struct SkPoint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037class SkRasterizer;
38class SkShader;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
41typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
42 SkFixed x, SkFixed y);
43
44typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
45
humper@google.comb0889472013-07-09 21:37:14 +000046#define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag
47
reed@android.com8a1c16f2008-12-17 15:59:43 +000048/** \class SkPaint
49
50 The SkPaint class holds the style and color information about how to draw
51 geometries, text and bitmaps.
52*/
humper@google.comb0889472013-07-09 21:37:14 +000053
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000054class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055public:
56 SkPaint();
57 SkPaint(const SkPaint& paint);
58 ~SkPaint();
59
60 SkPaint& operator=(const SkPaint&);
61
robertphillips@google.comb2657412013-08-07 22:36:29 +000062 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
63 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
64 return !(a == b);
65 }
66
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000067 void flatten(SkWriteBuffer&) const;
68 void unflatten(SkReadBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
70 /** Restores the paint to its initial settings.
71 */
72 void reset();
73
agl@chromium.org309485b2009-07-21 17:41:32 +000074 /** Specifies the level of hinting to be performed. These names are taken
75 from the Gnome/Cairo names for the same. They are translated into
76 Freetype concepts the same as in cairo-ft-font.c:
77 kNo_Hinting -> FT_LOAD_NO_HINTING
78 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
79 kNormal_Hinting -> <default, no option>
80 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
81 subpixel glyphs, in which case TARGET_LCD or
82 TARGET_LCD_V is used>
83 */
84 enum Hinting {
85 kNo_Hinting = 0,
86 kSlight_Hinting = 1,
87 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000088 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000089 };
90
reed@google.com9d07fec2011-03-16 20:02:59 +000091 Hinting getHinting() const {
reedf59eab22014-07-14 14:39:15 -070092 return static_cast<Hinting>(fBitfields.fHinting);
agl@chromium.org309485b2009-07-21 17:41:32 +000093 }
94
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +000095 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 /** Specifies the bit values that are stored in the paint's flags.
98 */
99 enum Flags {
100 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 kDither_Flag = 0x04, //!< mask to enable dithering
102 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
103 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
104 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
105 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000106 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000108 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000109 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000110 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000111 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000112 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000113 kDistanceFieldTextTEMP_Flag = 0x4000, //!< TEMPORARY mask to enable distance fields
114 // currently overrides LCD and subpixel rendering
agl@chromium.org309485b2009-07-21 17:41:32 +0000115 // when adding extra flags, note that the fFlags member is specified
116 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117
humper@google.com387db0a2013-07-09 14:13:04 +0000118 kAllFlags = 0xFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 };
120
121 /** Return the paint's flags. Use the Flag enum to test flag values.
122 @return the paint's flags (see enums ending in _Flag for bit masks)
123 */
reedf59eab22014-07-14 14:39:15 -0700124 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125
126 /** Set the paint's flags. Use the Flag enum to specific flag values.
127 @param flags The new flag bits for the paint (see Flags enum)
128 */
129 void setFlags(uint32_t flags);
130
131 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
132 @return true if the antialias bit is set in the paint's flags.
133 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000134 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 return SkToBool(this->getFlags() & kAntiAlias_Flag);
136 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
139 @param aa true to enable antialiasing, false to disable it
140 */
141 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 /** Helper for getFlags(), returning true if kDither_Flag bit is set
144 @return true if the dithering bit is set in the paint's flags.
145 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000146 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 return SkToBool(this->getFlags() & kDither_Flag);
148 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
151 @param dither true to enable dithering, false to disable it
152 */
153 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 /** Helper for getFlags(), returning true if kLinearText_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 isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 return SkToBool(this->getFlags() & kLinearText_Flag);
160 }
161
162 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
163 @param linearText true to set the linearText bit in the paint's flags,
164 false to clear it.
165 */
166 void setLinearText(bool linearText);
167
168 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
169 @return true if the lineartext bit is set in the paint's flags
170 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000171 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 return SkToBool(this->getFlags() & kSubpixelText_Flag);
173 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000174
reed@google.com84b437e2011-08-01 12:45:35 +0000175 /**
176 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
177 * @param subpixelText true to set the subpixelText bit in the paint's
178 * flags, false to clear it.
179 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000181
reed@google.com9d07fec2011-03-16 20:02:59 +0000182 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000183 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
184 }
185
reed@google.com84b437e2011-08-01 12:45:35 +0000186 /**
187 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
188 * Note: antialiasing must also be on for lcd rendering
189 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
190 * false to clear it.
191 */
192 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000193
reed@google.com9d07fec2011-03-16 20:02:59 +0000194 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000195 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
196 }
197
198 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
199 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
200 false to clear it.
201 */
202 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
203
reed@google.com9d07fec2011-03-16 20:02:59 +0000204 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000205 return SkToBool(this->getFlags() & kAutoHinting_Flag);
206 }
207
208 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
209 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
210 paint's flags,
211 false to clear it.
212 */
213 void setAutohinted(bool useAutohinter);
214
reed@google.com830a23e2011-11-10 15:20:49 +0000215 bool isVerticalText() const {
216 return SkToBool(this->getFlags() & kVerticalText_Flag);
217 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000218
reed@google.com830a23e2011-11-10 15:20:49 +0000219 /**
220 * Helper for setting or clearing the kVerticalText_Flag bit in
221 * setFlags(...).
222 *
223 * If this bit is set, then advances are treated as Y values rather than
224 * X values, and drawText will places its glyphs vertically rather than
225 * horizontally.
226 */
227 void setVerticalText(bool);
228
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
230 @return true if the underlineText bit is set in the paint's flags.
231 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000232 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 return SkToBool(this->getFlags() & kUnderlineText_Flag);
234 }
235
236 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
237 @param underlineText true to set the underlineText bit in the paint's
238 flags, false to clear it.
239 */
240 void setUnderlineText(bool underlineText);
241
242 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
243 @return true if the strikeThruText bit is set in the paint's flags.
244 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000245 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
247 }
248
249 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
250 @param strikeThruText true to set the strikeThruText bit in the
251 paint's flags, false to clear it.
252 */
253 void setStrikeThruText(bool strikeThruText);
254
255 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
256 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
257 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000258 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
260 }
261
262 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
263 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
264 flags, false to clear it.
265 */
266 void setFakeBoldText(bool fakeBoldText);
267
268 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
269 @return true if the kernText bit is set in the paint's flags.
270 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000271 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 return SkToBool(this->getFlags() & kDevKernText_Flag);
273 }
274
275 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
276 @param kernText true to set the kKernText_Flag bit in the paint's
277 flags, false to clear it.
278 */
279 void setDevKernText(bool devKernText);
280
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000281 /** Helper for getFlags(), returns true if kDistanceFieldTextTEMP_Flag bit is set
282 @return true if the distanceFieldText bit is set in the paint's flags.
283 */
284 bool isDistanceFieldTextTEMP() const {
285 return SkToBool(this->getFlags() & kDistanceFieldTextTEMP_Flag);
286 }
287
288 /** Helper for setFlags(), setting or clearing the kDistanceFieldTextTEMP_Flag bit
289 @param distanceFieldText true to set the kDistanceFieldTextTEMP_Flag bit in the paint's
290 flags, false to clear it.
291 */
292 void setDistanceFieldTextTEMP(bool distanceFieldText);
293
reed@google.comc9683152013-07-18 13:47:01 +0000294 enum FilterLevel {
295 kNone_FilterLevel,
296 kLow_FilterLevel,
297 kMedium_FilterLevel,
298 kHigh_FilterLevel
299 };
300
301 /**
302 * Return the filter level. This affects the quality (and performance) of
303 * drawing scaled images.
304 */
reedf59eab22014-07-14 14:39:15 -0700305 FilterLevel getFilterLevel() const {
306 return (FilterLevel)fBitfields.fFilterLevel;
307 }
reed@google.comc9683152013-07-18 13:47:01 +0000308
309 /**
310 * Set the filter level. This affects the quality (and performance) of
311 * drawing scaled images.
312 */
313 void setFilterLevel(FilterLevel);
314
315 /**
reed@google.comc9683152013-07-18 13:47:01 +0000316 * If the predicate is true, set the filterLevel to Low, else set it to
317 * None.
318 */
reed@google.com44699382013-10-31 17:28:30 +0000319 SK_ATTR_DEPRECATED("use setFilterLevel")
reed@google.comc9683152013-07-18 13:47:01 +0000320 void setFilterBitmap(bool doFilter) {
321 this->setFilterLevel(doFilter ? kLow_FilterLevel : kNone_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000323
reed@google.comc9683152013-07-18 13:47:01 +0000324 /**
reed@google.comc9683152013-07-18 13:47:01 +0000325 * Returns true if getFilterLevel() returns anything other than None.
326 */
reed@google.com44699382013-10-31 17:28:30 +0000327 SK_ATTR_DEPRECATED("use getFilterLevel")
reed@google.comc9683152013-07-18 13:47:01 +0000328 bool isFilterBitmap() const {
329 return kNone_FilterLevel != this->getFilterLevel();
330 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331
332 /** Styles apply to rect, oval, path, and text.
333 Bitmaps are always drawn in "fill", and lines are always drawn in
334 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000335
reed@android.comed881c22009-09-15 14:10:42 +0000336 Note: strokeandfill implicitly draws the result with
337 SkPath::kWinding_FillType, so if the original path is even-odd, the
338 results may not appear the same as if it was drawn twice, filled and
339 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 */
341 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000342 kFill_Style, //!< fill the geometry
343 kStroke_Style, //!< stroke the geometry
344 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000345 };
346 enum {
347 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 };
349
350 /** Return the paint's style, used for controlling how primitives'
351 geometries are interpreted (except for drawBitmap, which always assumes
352 kFill_Style).
353 @return the paint's Style
354 */
reedf59eab22014-07-14 14:39:15 -0700355 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356
357 /** Set the paint's style, used for controlling how primitives'
358 geometries are interpreted (except for drawBitmap, which always assumes
359 Fill).
360 @param style The new style to set in the paint
361 */
362 void setStyle(Style style);
363
364 /** Return the paint's color. Note that the color is a 32bit value
365 containing alpha as well as r,g,b. This 32bit value is not
366 premultiplied, meaning that its alpha can be any value, regardless of
367 the values of r,g,b.
368 @return the paint's color (and alpha).
369 */
370 SkColor getColor() const { return fColor; }
371
372 /** Set the paint's color. Note that the color is a 32bit value containing
373 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
374 that its alpha can be any value, regardless of the values of r,g,b.
375 @param color The new color (including alpha) to set in the paint.
376 */
377 void setColor(SkColor color);
378
379 /** Helper to getColor() that just returns the color's alpha value.
380 @return the alpha component of the paint's color.
381 */
382 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000383
reed@android.com8a1c16f2008-12-17 15:59:43 +0000384 /** Helper to setColor(), that only assigns the color's alpha value,
385 leaving its r,g,b values unchanged.
386 @param a set the alpha component (0..255) of the paint's color.
387 */
388 void setAlpha(U8CPU a);
389
390 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
391 using SkColorSetARGB()
392 @param a The new alpha component (0..255) of the paint's color.
393 @param r The new red component (0..255) of the paint's color.
394 @param g The new green component (0..255) of the paint's color.
395 @param b The new blue component (0..255) of the paint's color.
396 */
397 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
398
reed@google.com9d07fec2011-03-16 20:02:59 +0000399 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 <p />
401 A value of 0 strokes in hairline mode.
402 Hairlines always draw 1-pixel wide, regardless of the matrix.
403 @return the paint's stroke width, used whenever the paint's style is
404 Stroke or StrokeAndFill.
405 */
406 SkScalar getStrokeWidth() const { return fWidth; }
407
reed@google.com9d07fec2011-03-16 20:02:59 +0000408 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 Pass 0 to stroke in hairline mode.
410 Hairlines always draw 1-pixel wide, regardless of the matrix.
411 @param width set the paint's stroke width, used whenever the paint's
412 style is Stroke or StrokeAndFill.
413 */
414 void setStrokeWidth(SkScalar width);
415
416 /** Return the paint's stroke miter value. This is used to control the
417 behavior of miter joins when the joins angle is sharp.
418 @return the paint's miter limit, used whenever the paint's style is
419 Stroke or StrokeAndFill.
420 */
421 SkScalar getStrokeMiter() const { return fMiterLimit; }
422
423 /** Set the paint's stroke miter value. This is used to control the
424 behavior of miter joins when the joins angle is sharp. This value must
425 be >= 0.
426 @param miter set the miter limit on the paint, used whenever the
427 paint's style is Stroke or StrokeAndFill.
428 */
429 void setStrokeMiter(SkScalar miter);
430
431 /** Cap enum specifies the settings for the paint's strokecap. This is the
432 treatment that is applied to the beginning and end of each non-closed
433 contour (e.g. lines).
434 */
435 enum Cap {
436 kButt_Cap, //!< begin/end contours with no extension
437 kRound_Cap, //!< begin/end contours with a semi-circle extension
438 kSquare_Cap, //!< begin/end contours with a half square extension
439
440 kCapCount,
441 kDefault_Cap = kButt_Cap
442 };
443
444 /** Join enum specifies the settings for the paint's strokejoin. This is
445 the treatment that is applied to corners in paths and rectangles.
446 */
447 enum Join {
448 kMiter_Join, //!< connect path segments with a sharp join
449 kRound_Join, //!< connect path segments with a round join
450 kBevel_Join, //!< connect path segments with a flat bevel join
451
452 kJoinCount,
453 kDefault_Join = kMiter_Join
454 };
455
456 /** Return the paint's stroke cap type, controlling how the start and end
457 of stroked lines and paths are treated.
458 @return the line cap style for the paint, used whenever the paint's
459 style is Stroke or StrokeAndFill.
460 */
reedf59eab22014-07-14 14:39:15 -0700461 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462
463 /** Set the paint's stroke cap type.
464 @param cap set the paint's line cap style, used whenever the paint's
465 style is Stroke or StrokeAndFill.
466 */
467 void setStrokeCap(Cap cap);
468
469 /** Return the paint's stroke join type.
470 @return the paint's line join style, used whenever the paint's style is
471 Stroke or StrokeAndFill.
472 */
reedf59eab22014-07-14 14:39:15 -0700473 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474
475 /** Set the paint's stroke join type.
476 @param join set the paint's line join style, used whenever the paint's
477 style is Stroke or StrokeAndFill.
478 */
479 void setStrokeJoin(Join join);
480
reed@google.com4bbdeac2013-01-24 21:03:11 +0000481 /**
482 * Applies any/all effects (patheffect, stroking) to src, returning the
483 * result in dst. The result is that drawing src with this paint will be
484 * the same as drawing dst with a default paint (at least from the
485 * geometric perspective).
486 *
487 * @param src input path
488 * @param dst output path (may be the same as src)
489 * @param cullRect If not null, the dst path may be culled to this rect.
490 * @return true if the path should be filled, or false if it should be
491 * drawn with a hairline (width == 0)
492 */
493 bool getFillPath(const SkPath& src, SkPath* dst,
494 const SkRect* cullRect = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496 /** Get the paint's shader object.
497 <p />
498 The shader's reference count is not affected.
499 @return the paint's shader (or NULL)
500 */
501 SkShader* getShader() const { return fShader; }
502
503 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000504 * Shaders specify the source color(s) for what is being drawn. If a paint
505 * has no shader, then the paint's color is used. If the paint has a
506 * shader, then the shader's color(s) are use instead, but they are
507 * modulated by the paint's alpha. This makes it easy to create a shader
508 * once (e.g. bitmap tiling or gradient) and then change its transparency
509 * w/o having to modify the original shader... only the paint's alpha needs
510 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000511 *
512 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
513 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
514 * then the shader will use the paint's entire color to "colorize" its output (modulating the
515 * bitmap's alpha with the paint's color+alpha).
516 *
reed@google.com880dc472012-05-11 14:47:03 +0000517 * Pass NULL to clear any previous shader.
518 * As a convenience, the parameter passed is also returned.
519 * If a previous shader exists, its reference count is decremented.
520 * If shader is not NULL, its reference count is incremented.
521 * @param shader May be NULL. The shader to be installed in the paint
522 * @return shader
523 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000525
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526 /** Get the paint's colorfilter. If there is a colorfilter, its reference
527 count is not changed.
528 @return the paint's colorfilter (or NULL)
529 */
530 SkColorFilter* getColorFilter() const { return fColorFilter; }
531
532 /** Set or clear the paint's colorfilter, returning the parameter.
533 <p />
534 If the paint already has a filter, its reference count is decremented.
535 If filter is not NULL, its reference count is incremented.
536 @param filter May be NULL. The filter to be installed in the paint
537 @return filter
538 */
539 SkColorFilter* setColorFilter(SkColorFilter* filter);
540
541 /** Get the paint's xfermode object.
542 <p />
543 The xfermode's reference count is not affected.
544 @return the paint's xfermode (or NULL)
545 */
546 SkXfermode* getXfermode() const { return fXfermode; }
547
548 /** Set or clear the xfermode object.
549 <p />
550 Pass NULL to clear any previous xfermode.
551 As a convenience, the parameter passed is also returned.
552 If a previous xfermode exists, its reference count is decremented.
553 If xfermode is not NULL, its reference count is incremented.
554 @param xfermode May be NULL. The new xfermode to be installed in the
555 paint
556 @return xfermode
557 */
558 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000559
560 /** Create an xfermode based on the specified Mode, and assign it into the
561 paint, returning the mode that was set. If the Mode is SrcOver, then
562 the paint's xfermode is set to null.
563 */
reed@android.com0baf1932009-06-24 12:41:42 +0000564 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000565
reed@android.com8a1c16f2008-12-17 15:59:43 +0000566 /** Get the paint's patheffect object.
567 <p />
568 The patheffect reference count is not affected.
569 @return the paint's patheffect (or NULL)
570 */
571 SkPathEffect* getPathEffect() const { return fPathEffect; }
572
573 /** Set or clear the patheffect object.
574 <p />
575 Pass NULL to clear any previous patheffect.
576 As a convenience, the parameter passed is also returned.
577 If a previous patheffect exists, its reference count is decremented.
578 If patheffect is not NULL, its reference count is incremented.
579 @param effect May be NULL. The new patheffect to be installed in the
580 paint
581 @return effect
582 */
583 SkPathEffect* setPathEffect(SkPathEffect* effect);
584
585 /** Get the paint's maskfilter object.
586 <p />
587 The maskfilter reference count is not affected.
588 @return the paint's maskfilter (or NULL)
589 */
590 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
591
592 /** Set or clear the maskfilter object.
593 <p />
594 Pass NULL to clear any previous maskfilter.
595 As a convenience, the parameter passed is also returned.
596 If a previous maskfilter exists, its reference count is decremented.
597 If maskfilter is not NULL, its reference count is incremented.
598 @param maskfilter May be NULL. The new maskfilter to be installed in
599 the paint
600 @return maskfilter
601 */
602 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
603
604 // These attributes are for text/fonts
605
606 /** Get the paint's typeface object.
607 <p />
608 The typeface object identifies which font to use when drawing or
609 measuring text. The typeface reference count is not affected.
610 @return the paint's typeface (or NULL)
611 */
612 SkTypeface* getTypeface() const { return fTypeface; }
613
614 /** Set or clear the typeface object.
615 <p />
616 Pass NULL to clear any previous typeface.
617 As a convenience, the parameter passed is also returned.
618 If a previous typeface exists, its reference count is decremented.
619 If typeface is not NULL, its reference count is incremented.
620 @param typeface May be NULL. The new typeface to be installed in the
621 paint
622 @return typeface
623 */
624 SkTypeface* setTypeface(SkTypeface* typeface);
625
626 /** Get the paint's rasterizer (or NULL).
627 <p />
628 The raster controls how paths/text are turned into alpha masks.
629 @return the paint's rasterizer (or NULL)
630 */
631 SkRasterizer* getRasterizer() const { return fRasterizer; }
632
633 /** Set or clear the rasterizer object.
634 <p />
635 Pass NULL to clear any previous rasterizer.
636 As a convenience, the parameter passed is also returned.
637 If a previous rasterizer exists in the paint, its reference count is
638 decremented. If rasterizer is not NULL, its reference count is
639 incremented.
640 @param rasterizer May be NULL. The new rasterizer to be installed in
641 the paint.
642 @return rasterizer
643 */
644 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
645
reed@google.com15356a62011-11-03 19:29:08 +0000646 SkImageFilter* getImageFilter() const { return fImageFilter; }
647 SkImageFilter* setImageFilter(SkImageFilter*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000648
reed@google.comb0a34d82012-07-11 19:57:55 +0000649 SkAnnotation* getAnnotation() const { return fAnnotation; }
650 SkAnnotation* setAnnotation(SkAnnotation*);
651
652 /**
653 * Returns true if there is an annotation installed on this paint, and
654 * the annotation specifics no-drawing.
655 */
reed@google.com44699382013-10-31 17:28:30 +0000656 SK_ATTR_DEPRECATED("use getAnnotation and check for non-null")
commit-bot@chromium.org950923b2013-10-29 20:44:39 +0000657 bool isNoDrawAnnotation() const { return this->getAnnotation() != NULL; }
reed@google.com15356a62011-11-03 19:29:08 +0000658
reed@google.com9d07fec2011-03-16 20:02:59 +0000659 /**
660 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
661 * reference count.
662 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000664
665 /**
666 * Set or clear the looper object.
667 * <p />
668 * Pass NULL to clear any previous looper.
669 * As a convenience, the parameter passed is also returned.
670 * If a previous looper exists in the paint, its reference count is
671 * decremented. If looper is not NULL, its reference count is
672 * incremented.
673 * @param looper May be NULL. The new looper to be installed in the paint.
674 * @return looper
675 */
676 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000677
678 enum Align {
679 kLeft_Align,
680 kCenter_Align,
681 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000682 };
683 enum {
684 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000686
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687 /** Return the paint's Align value for drawing text.
688 @return the paint's Align value for drawing text.
689 */
reedf59eab22014-07-14 14:39:15 -0700690 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000691
reed@android.com8a1c16f2008-12-17 15:59:43 +0000692 /** Set the paint's text alignment.
693 @param align set the paint's Align value for drawing text.
694 */
695 void setTextAlign(Align align);
696
697 /** Return the paint's text size.
698 @return the paint's text size.
699 */
700 SkScalar getTextSize() const { return fTextSize; }
701
702 /** Set the paint's text size. This value must be > 0
703 @param textSize set the paint's text size.
704 */
705 void setTextSize(SkScalar textSize);
706
707 /** Return the paint's horizontal scale factor for text. The default value
708 is 1.0.
709 @return the paint's scale factor in X for drawing/measuring text
710 */
711 SkScalar getTextScaleX() const { return fTextScaleX; }
712
713 /** Set the paint's horizontal scale factor for text. The default value
714 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
715 stretch the text narrower.
716 @param scaleX set the paint's scale factor in X for drawing/measuring
717 text.
718 */
719 void setTextScaleX(SkScalar scaleX);
720
721 /** Return the paint's horizontal skew factor for text. The default value
722 is 0.
723 @return the paint's skew factor in X for drawing text.
724 */
725 SkScalar getTextSkewX() const { return fTextSkewX; }
726
727 /** Set the paint's horizontal skew factor for text. The default value
728 is 0. For approximating oblique text, use values around -0.25.
729 @param skewX set the paint's skew factor in X for drawing text.
730 */
731 void setTextSkewX(SkScalar skewX);
732
733 /** Describes how to interpret the text parameters that are passed to paint
734 methods like measureText() and getTextWidths().
735 */
736 enum TextEncoding {
737 kUTF8_TextEncoding, //!< the text parameters are UTF8
738 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000739 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740 kGlyphID_TextEncoding //!< the text parameters are glyph indices
741 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000742
reedf59eab22014-07-14 14:39:15 -0700743 TextEncoding getTextEncoding() const {
744 return (TextEncoding)fBitfields.fTextEncoding;
745 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000746
747 void setTextEncoding(TextEncoding encoding);
748
749 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000750 /** Flags which indicate the confidence level of various metrics.
751 A set flag indicates that the metric may be trusted.
752 */
753 enum FontMetricsFlags {
754 kUnderlineThinknessIsValid_Flag = 1 << 0,
755 kUnderlinePositionIsValid_Flag = 1 << 1,
756 };
757
758 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000759 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
760 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
761 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
762 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
763 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000764 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
765 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000766 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
767 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000768 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
769 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000770 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
771
772 /** Underline Position - position of the top of the Underline stroke
773 relative to the baseline, this can have following values
774 - Negative - means underline should be drawn above baseline.
775 - Positive - means below baseline.
776 - Zero - mean underline should be drawn on baseline.
777 */
778 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
779
780 /** If the fontmetrics has a valid underlinethickness, return true, and set the
781 thickness param to that value. If it doesn't return false and ignore the
782 thickness param.
783 */
784 bool hasUnderlineThickness(SkScalar* thickness) const {
785 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
786 *thickness = fUnderlineThickness;
787 return true;
788 }
789 return false;
790 }
791
792 /** If the fontmetrics has a valid underlineposition, return true, and set the
793 thickness param to that value. If it doesn't return false and ignore the
794 thickness param.
795 */
796 bool hasUnderlinePosition(SkScalar* position) const {
797 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
798 *position = fUnderlinePosition;
799 return true;
800 }
801 return false;
802 }
803
reed@android.com8a1c16f2008-12-17 15:59:43 +0000804 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000805
reed@android.com8a1c16f2008-12-17 15:59:43 +0000806 /** Return the recommend spacing between lines (which will be
807 fDescent - fAscent + fLeading).
808 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000809 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000810 @param metrics If not null, returns the font metrics for the
811 current typeface/pointsize/etc setting in this
812 paint.
813 @param scale If not 0, return width as if the canvas were scaled
814 by this value
815 @param return the recommended spacing between lines
816 */
817 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000818
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 /** Return the recommend line spacing. This will be
820 fDescent - fAscent + fLeading
821 */
822 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
823
824 /** Convert the specified text into glyph IDs, returning the number of
825 glyphs ID written. If glyphs is NULL, it is ignore and only the count
826 is returned.
827 */
828 int textToGlyphs(const void* text, size_t byteLength,
829 uint16_t glyphs[]) const;
830
reed@android.coma5dcaf62010-02-05 17:12:32 +0000831 /** Return true if all of the specified text has a corresponding non-zero
832 glyph ID. If any of the code-points in the text are not supported in
833 the typeface (i.e. the glyph ID would be zero), then return false.
834
835 If the text encoding for the paint is kGlyph_TextEncoding, then this
836 returns true if all of the specified glyph IDs are non-zero.
837 */
838 bool containsText(const void* text, size_t byteLength) const;
839
reed@android.com9d3a9852010-01-08 14:07:42 +0000840 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
841 to zero. Note: this does not look at the text-encoding setting in the
842 paint, only at the typeface.
843 */
844 void glyphsToUnichars(const uint16_t glyphs[], int count,
845 SkUnichar text[]) const;
846
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847 /** Return the number of drawable units in the specified text buffer.
848 This looks at the current TextEncoding field of the paint. If you also
849 want to have the text converted into glyph IDs, call textToGlyphs
850 instead.
851 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000852 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000853 return this->textToGlyphs(text, byteLength, NULL);
854 }
855
reed@google.com44da42e2011-11-10 20:04:47 +0000856 /** Return the width of the text. This will return the vertical measure
857 * if isVerticalText() is true, in which case the returned value should
858 * be treated has a height instead of a width.
859 *
860 * @param text The text to be measured
861 * @param length Number of bytes of text to measure
862 * @param bounds If not NULL, returns the bounds of the text,
863 * relative to (0, 0).
864 * @param scale If not 0, return width as if the canvas were scaled
865 * by this value
866 * @return The advance width of the text
867 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000868 SkScalar measureText(const void* text, size_t length,
869 SkRect* bounds, SkScalar scale = 0) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000870
reed@google.com44da42e2011-11-10 20:04:47 +0000871 /** Return the width of the text. This will return the vertical measure
872 * if isVerticalText() is true, in which case the returned value should
873 * be treated has a height instead of a width.
874 *
875 * @param text Address of the text
876 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000877 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000878 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000879 SkScalar measureText(const void* text, size_t length) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880 return this->measureText(text, length, NULL, 0);
881 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000882
reed@android.com8a1c16f2008-12-17 15:59:43 +0000883 /** Specify the direction the text buffer should be processed in breakText()
884 */
885 enum TextBufferDirection {
886 /** When measuring text for breakText(), begin at the start of the text
887 buffer and proceed forward through the data. This is the default.
888 */
889 kForward_TextBufferDirection,
890 /** When measuring text for breakText(), begin at the end of the text
891 buffer and proceed backwards through the data.
892 */
893 kBackward_TextBufferDirection
894 };
895
reed@google.com44da42e2011-11-10 20:04:47 +0000896 /** Return the number of bytes of text that were measured. If
897 * isVerticalText() is true, then the vertical advances are used for
898 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000899 *
reed@google.com44da42e2011-11-10 20:04:47 +0000900 * @param text The text to be measured
901 * @param length Number of bytes of text to measure
902 * @param maxWidth Maximum width. Only the subset of text whose accumulated
903 * widths are <= maxWidth are measured.
904 * @param measuredWidth Optional. If non-null, this returns the actual
905 * width of the measured text.
906 * @param tbd Optional. The direction the text buffer should be
907 * traversed during measuring.
908 * @return The number of bytes of text that were measured. Will be
909 * <= length.
910 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000911 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
912 SkScalar* measuredWidth = NULL,
913 TextBufferDirection tbd = kForward_TextBufferDirection)
914 const;
915
reed@google.com44da42e2011-11-10 20:04:47 +0000916 /** Return the advances for the text. These will be vertical advances if
917 * isVerticalText() returns true.
918 *
919 * @param text the text
920 * @param byteLength number of bytes to of text
921 * @param widths If not null, returns the array of advances for
922 * the glyphs. If not NULL, must be at least a large
923 * as the number of unichars in the specified text.
924 * @param bounds If not null, returns the bounds for each of
925 * character, relative to (0, 0)
926 * @return the number of unichars in the specified text.
927 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
929 SkRect bounds[] = NULL) const;
930
931 /** Return the path (outline) for the specified text.
932 Note: just like SkCanvas::drawText, this will respect the Align setting
933 in the paint.
934 */
935 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
936 SkPath* path) const;
937
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000938 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000939 const SkPoint pos[], SkPath* path) const;
940
djsollen@google.com56c69772011-11-08 19:00:26 +0000941#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000942 uint32_t getGenerationID() const;
djsollen@google.com4bd2bdb2013-03-08 18:35:13 +0000943 void setGenerationID(uint32_t generationID);
djsollen@google.com60abb072012-02-15 18:49:15 +0000944
945 /** Returns the base glyph count for the strike associated with this paint
946 */
947 unsigned getBaseGlyphCount(SkUnichar text) const;
commit-bot@chromium.orgc7a20e42013-05-13 14:09:13 +0000948
949 const SkPaintOptionsAndroid& getPaintOptionsAndroid() const {
950 return fPaintOptionsAndroid;
951 }
952 void setPaintOptionsAndroid(const SkPaintOptionsAndroid& options);
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000953#endif
954
reed@google.com632e1a22011-10-06 12:37:00 +0000955 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
956 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
957 bool nothingToDraw() const;
958
reed@google.coma584aed2012-05-16 14:06:02 +0000959 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000960 // would prefer to make these private...
961
reed@google.coma584aed2012-05-16 14:06:02 +0000962 /** Returns true if the current paint settings allow for fast computation of
963 bounds (i.e. there is nothing complex like a patheffect that would make
964 the bounds computation expensive.
965 */
966 bool canComputeFastBounds() const {
967 if (this->getLooper()) {
968 return this->getLooper()->canComputeFastBounds(*this);
969 }
970 return !this->getRasterizer();
971 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000972
reed@google.coma584aed2012-05-16 14:06:02 +0000973 /** Only call this if canComputeFastBounds() returned true. This takes a
974 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
975 effects in the paint (e.g. stroking). If needed, it uses the storage
976 rect parameter. It returns the adjusted bounds that can then be used
977 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000978
reed@google.coma584aed2012-05-16 14:06:02 +0000979 The returned rect will either be orig or storage, thus the caller
980 should not rely on storage being set to the result, but should always
981 use the retured value. It is legal for orig and storage to be the same
982 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000983
reed@google.coma584aed2012-05-16 14:06:02 +0000984 e.g.
985 if (paint.canComputeFastBounds()) {
986 SkRect r, storage;
987 path.computeBounds(&r, SkPath::kFast_BoundsType);
988 const SkRect& fastR = paint.computeFastBounds(r, &storage);
989 if (canvas->quickReject(fastR, ...)) {
990 // don't draw the path
991 }
992 }
993 */
994 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
995 SkPaint::Style style = this->getStyle();
996 // ultra fast-case: filling with no effects that affect geometry
997 if (kFill_Style == style) {
998 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
999 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
1000 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +00001001 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +00001002 if (!effects) {
1003 return orig;
1004 }
1005 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001006
reed@google.coma584aed2012-05-16 14:06:02 +00001007 return this->doComputeFastBounds(orig, storage, style);
1008 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001009
reed@google.coma584aed2012-05-16 14:06:02 +00001010 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1011 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001012 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001013 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001014
reed@google.coma584aed2012-05-16 14:06:02 +00001015 // Take the style explicitly, so the caller can force us to be stroked
1016 // without having to make a copy of the paint just to change that field.
1017 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1018 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001019
reed@google.comed43dff2013-06-04 16:56:27 +00001020 /**
1021 * Return a matrix that applies the paint's text values: size, scale, skew
1022 */
1023 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1024 SkScalar scaleX, SkScalar skewX) {
1025 matrix->setScale(size * scaleX, size);
1026 if (skewX) {
1027 matrix->postSkew(skewX, 0);
1028 }
1029 return matrix;
1030 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001031
reed@google.comed43dff2013-06-04 16:56:27 +00001032 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1033 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1034 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001035
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001036 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001037
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001038 struct FlatteningTraits {
1039 static void Flatten(SkWriteBuffer& buffer, const SkPaint& paint);
1040 static void Unflatten(SkReadBuffer& buffer, SkPaint* paint);
1041 };
1042
reed@google.comd5f20792012-05-16 14:15:02 +00001043private:
1044 SkTypeface* fTypeface;
reed@google.comd5f20792012-05-16 14:15:02 +00001045 SkPathEffect* fPathEffect;
1046 SkShader* fShader;
1047 SkXfermode* fXfermode;
1048 SkMaskFilter* fMaskFilter;
1049 SkColorFilter* fColorFilter;
1050 SkRasterizer* fRasterizer;
1051 SkDrawLooper* fLooper;
1052 SkImageFilter* fImageFilter;
reed@google.comb0a34d82012-07-11 19:57:55 +00001053 SkAnnotation* fAnnotation;
reed@google.comd5f20792012-05-16 14:15:02 +00001054
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001055 SkScalar fTextSize;
1056 SkScalar fTextScaleX;
1057 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001058 SkColor fColor;
1059 SkScalar fWidth;
1060 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001061 union {
1062 struct {
1063 // all of these bitfields should add up to 32
1064 unsigned fFlags : 16;
1065 unsigned fTextAlign : 2;
1066 unsigned fCapType : 2;
1067 unsigned fJoinType : 2;
1068 unsigned fStyle : 2;
1069 unsigned fTextEncoding : 2; // 3 values
1070 unsigned fHinting : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001071 unsigned fFilterLevel : 2;
1072 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001073 } fBitfields;
1074 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001075 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001076 uint32_t fDirtyBits;
1077
reed@google.comd5f20792012-05-16 14:15:02 +00001078 SkDrawCacheProc getDrawCacheProc() const;
1079 SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
1080 bool needFullMetrics) const;
1081
1082 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1083 int* count, SkRect* bounds) const;
1084
jvanverth2d2a68c2014-06-10 06:42:56 -07001085 SkGlyphCache* detachCache(const SkDeviceProperties* deviceProperties, const SkMatrix*,
1086 bool ignoreGamma) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001087
bungeman@google.com532470f2013-01-22 19:25:14 +00001088 void descriptorProc(const SkDeviceProperties* deviceProperties, const SkMatrix* deviceMatrix,
reed@google.com90808e82013-03-19 14:44:54 +00001089 void (*proc)(SkTypeface*, const SkDescriptor*, void*),
reed@google.comd5f20792012-05-16 14:15:02 +00001090 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +00001091
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001092 static void Term();
1093
reed@android.com8a1c16f2008-12-17 15:59:43 +00001094 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001095 /* This is the size we use when we ask for a glyph's path. We then
1096 * post-transform it as we draw to match the request.
1097 * This is done to try to re-use cache entries for the path.
1098 *
1099 * This value is somewhat arbitrary. In theory, it could be 1, since
1100 * we store paths as floats. However, we get the path from the font
1101 * scaler, and it may represent its paths as fixed-point (or 26.6),
1102 * so we shouldn't ask for something too big (might overflow 16.16)
1103 * or too small (underflow 26.6).
1104 *
1105 * This value could track kMaxSizeForGlyphCache, assuming the above
1106 * constraints, but since we ask for unhinted paths, the two values
1107 * need not match per-se.
1108 */
1109 kCanonicalTextSizeForPaths = 64,
1110
1111 /*
1112 * Above this size (taking into account CTM and textSize), we never use
1113 * the cache for bits or metrics (we might overflow), so we just ask
1114 * for a caononical size and post-transform that.
1115 */
1116 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117 };
reed@google.comed43dff2013-06-04 16:56:27 +00001118
1119 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1120
reed@google.comed43dff2013-06-04 16:56:27 +00001121 // Set flags/hinting/textSize up to use for drawing text as paths.
1122 // Returns scale factor to restore the original textSize, since will will
1123 // have change it to kCanonicalTextSizeForPaths.
1124 SkScalar setupForAsPaths();
1125
1126 static SkScalar MaxCacheSize2() {
1127 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1128 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1129 return kMag2Max;
1130 }
1131
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001132 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001133 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001134 friend class SkCanvas;
1135 friend class SkDraw;
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001136 friend class SkGraphics; // So Term() can be called.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001137 friend class SkPDFDevice;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00001138 friend class GrBitmapTextContext;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001139 friend class GrDistanceFieldTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001140 friend class GrStencilAndCoverTextContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141 friend class SkTextToPathIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001142 friend class SkCanonicalizePaint;
djsollen@google.comb44cd652011-12-01 17:09:21 +00001143
1144#ifdef SK_BUILD_FOR_ANDROID
commit-bot@chromium.orgc7a20e42013-05-13 14:09:13 +00001145 SkPaintOptionsAndroid fPaintOptionsAndroid;
1146
djsollen@google.comb44cd652011-12-01 17:09:21 +00001147 // In order for the == operator to work properly this must be the last field
1148 // in the struct so that we can do a memcmp to this field's offset.
1149 uint32_t fGenerationID;
1150#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001151};
1152
reed@android.com8a1c16f2008-12-17 15:59:43 +00001153#endif