blob: b52b7c1b2ddf53adf3826a1d7fc5ba2973785ab2 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkPaint_DEFINED
9#define SkPaint_DEFINED
10
11#include "SkColor.h"
reed@google.com9efd9a02012-01-30 15:41:43 +000012#include "SkDrawLooper.h"
reedf803da12015-01-23 05:58:07 -080013#include "SkFilterQuality.h"
reed@google.comed43dff2013-06-04 16:56:27 +000014#include "SkMatrix.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000015#include "SkXfermode.h"
16
reed@google.comb0a34d82012-07-11 19:57:55 +000017class SkAnnotation;
joshualitt2b6acb42015-04-01 11:30:27 -070018class SkAutoDescriptor;
reed@android.com8a1c16f2008-12-17 15:59:43 +000019class SkAutoGlyphCache;
20class SkColorFilter;
joshualittfd450792015-03-13 08:38:43 -070021class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000022class SkDescriptor;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000023class SkReadBuffer;
24class SkWriteBuffer;
herbb69d0e02015-02-25 06:47:06 -080025class SkGlyph;
reed@android.com8a1c16f2008-12-17 15:59:43 +000026struct SkRect;
27class SkGlyphCache;
reed@google.com15356a62011-11-03 19:29:08 +000028class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029class SkMaskFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000030class SkPath;
31class SkPathEffect;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000032struct SkPoint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkRasterizer;
34class SkShader;
robertphillipsfcf78292015-06-19 11:49:52 -070035class SkSurfaceProps;
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
mtkleinfb1fe4f2014-10-07 09:26:10 -070068 /** getHash() is a shallow hash, with the same limitations as operator==.
69 * If operator== returns true for two paints, getHash() returns the same value for each.
70 */
71 uint32_t getHash() const;
72
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000073 void flatten(SkWriteBuffer&) const;
74 void unflatten(SkReadBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
76 /** Restores the paint to its initial settings.
77 */
78 void reset();
79
agl@chromium.org309485b2009-07-21 17:41:32 +000080 /** Specifies the level of hinting to be performed. These names are taken
81 from the Gnome/Cairo names for the same. They are translated into
82 Freetype concepts the same as in cairo-ft-font.c:
83 kNo_Hinting -> FT_LOAD_NO_HINTING
84 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
85 kNormal_Hinting -> <default, no option>
86 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
87 subpixel glyphs, in which case TARGET_LCD or
88 TARGET_LCD_V is used>
89 */
90 enum Hinting {
91 kNo_Hinting = 0,
92 kSlight_Hinting = 1,
93 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000094 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000095 };
96
reed@google.com9d07fec2011-03-16 20:02:59 +000097 Hinting getHinting() const {
reedf59eab22014-07-14 14:39:15 -070098 return static_cast<Hinting>(fBitfields.fHinting);
agl@chromium.org309485b2009-07-21 17:41:32 +000099 }
100
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000101 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 /** Specifies the bit values that are stored in the paint's flags.
104 */
105 enum Flags {
106 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 kDither_Flag = 0x04, //!< mask to enable dithering
108 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
109 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
110 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
111 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000112 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000114 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000115 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000116 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000117 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000118 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000119 kDistanceFieldTextTEMP_Flag = 0x4000, //!< TEMPORARY mask to enable distance fields
120 // currently overrides LCD and subpixel rendering
agl@chromium.org309485b2009-07-21 17:41:32 +0000121 // when adding extra flags, note that the fFlags member is specified
122 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123
humper@google.com387db0a2013-07-09 14:13:04 +0000124 kAllFlags = 0xFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 };
126
127 /** Return the paint's flags. Use the Flag enum to test flag values.
128 @return the paint's flags (see enums ending in _Flag for bit masks)
129 */
reedf59eab22014-07-14 14:39:15 -0700130 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131
132 /** Set the paint's flags. Use the Flag enum to specific flag values.
133 @param flags The new flag bits for the paint (see Flags enum)
134 */
135 void setFlags(uint32_t flags);
136
137 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
138 @return true if the antialias bit is set in the paint's flags.
139 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000140 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 return SkToBool(this->getFlags() & kAntiAlias_Flag);
142 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
145 @param aa true to enable antialiasing, false to disable it
146 */
147 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 /** Helper for getFlags(), returning true if kDither_Flag bit is set
150 @return true if the dithering bit is set in the paint's flags.
151 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000152 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 return SkToBool(this->getFlags() & kDither_Flag);
154 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
157 @param dither true to enable dithering, false to disable it
158 */
159 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
162 @return true if the lineartext bit is set in the paint's flags
163 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000164 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 return SkToBool(this->getFlags() & kLinearText_Flag);
166 }
167
168 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
169 @param linearText true to set the linearText bit in the paint's flags,
170 false to clear it.
171 */
172 void setLinearText(bool linearText);
173
174 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
175 @return true if the lineartext bit is set in the paint's flags
176 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000177 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 return SkToBool(this->getFlags() & kSubpixelText_Flag);
179 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000180
reed@google.com84b437e2011-08-01 12:45:35 +0000181 /**
182 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
183 * @param subpixelText true to set the subpixelText bit in the paint's
184 * flags, false to clear it.
185 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000187
reed@google.com9d07fec2011-03-16 20:02:59 +0000188 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000189 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
190 }
191
reed@google.com84b437e2011-08-01 12:45:35 +0000192 /**
193 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
194 * Note: antialiasing must also be on for lcd rendering
195 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
196 * false to clear it.
197 */
198 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000199
reed@google.com9d07fec2011-03-16 20:02:59 +0000200 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000201 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
202 }
203
204 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
205 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
206 false to clear it.
207 */
208 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
209
reed@google.com9d07fec2011-03-16 20:02:59 +0000210 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000211 return SkToBool(this->getFlags() & kAutoHinting_Flag);
212 }
213
214 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
215 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
216 paint's flags,
217 false to clear it.
218 */
219 void setAutohinted(bool useAutohinter);
220
reed@google.com830a23e2011-11-10 15:20:49 +0000221 bool isVerticalText() const {
222 return SkToBool(this->getFlags() & kVerticalText_Flag);
223 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000224
reed@google.com830a23e2011-11-10 15:20:49 +0000225 /**
226 * Helper for setting or clearing the kVerticalText_Flag bit in
227 * setFlags(...).
228 *
229 * If this bit is set, then advances are treated as Y values rather than
230 * X values, and drawText will places its glyphs vertically rather than
231 * horizontally.
232 */
233 void setVerticalText(bool);
234
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
236 @return true if the underlineText bit is set in the paint's flags.
237 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000238 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 return SkToBool(this->getFlags() & kUnderlineText_Flag);
240 }
241
242 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
243 @param underlineText true to set the underlineText bit in the paint's
244 flags, false to clear it.
245 */
246 void setUnderlineText(bool underlineText);
247
248 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
249 @return true if the strikeThruText bit is set in the paint's flags.
250 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000251 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
253 }
254
255 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
256 @param strikeThruText true to set the strikeThruText bit in the
257 paint's flags, false to clear it.
258 */
259 void setStrikeThruText(bool strikeThruText);
260
261 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
262 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
263 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000264 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
266 }
267
268 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
269 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
270 flags, false to clear it.
271 */
272 void setFakeBoldText(bool fakeBoldText);
273
274 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
275 @return true if the kernText bit is set in the paint's flags.
276 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000277 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 return SkToBool(this->getFlags() & kDevKernText_Flag);
279 }
280
281 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
282 @param kernText true to set the kKernText_Flag bit in the paint's
283 flags, false to clear it.
284 */
285 void setDevKernText(bool devKernText);
286
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000287 /** Helper for getFlags(), returns true if kDistanceFieldTextTEMP_Flag bit is set
288 @return true if the distanceFieldText bit is set in the paint's flags.
289 */
290 bool isDistanceFieldTextTEMP() const {
291 return SkToBool(this->getFlags() & kDistanceFieldTextTEMP_Flag);
292 }
293
294 /** Helper for setFlags(), setting or clearing the kDistanceFieldTextTEMP_Flag bit
295 @param distanceFieldText true to set the kDistanceFieldTextTEMP_Flag bit in the paint's
296 flags, false to clear it.
297 */
298 void setDistanceFieldTextTEMP(bool distanceFieldText);
299
reedf803da12015-01-23 05:58:07 -0800300#ifdef SK_SUPPORT_LEGACY_FILTERLEVEL_ENUM
reed@google.comc9683152013-07-18 13:47:01 +0000301 enum FilterLevel {
reedf803da12015-01-23 05:58:07 -0800302 kNone_FilterLevel = kNone_SkFilterQuality,
303 kLow_FilterLevel = kLow_SkFilterQuality,
304 kMedium_FilterLevel = kMedium_SkFilterQuality,
305 kHigh_FilterLevel = kHigh_SkFilterQuality
reed@google.comc9683152013-07-18 13:47:01 +0000306 };
307
308 /**
309 * Return the filter level. This affects the quality (and performance) of
310 * drawing scaled images.
311 */
reedf59eab22014-07-14 14:39:15 -0700312 FilterLevel getFilterLevel() const {
reedf803da12015-01-23 05:58:07 -0800313 return (FilterLevel)this->getFilterQuality();
reedf59eab22014-07-14 14:39:15 -0700314 }
reed@google.comc9683152013-07-18 13:47:01 +0000315
316 /**
317 * Set the filter level. This affects the quality (and performance) of
318 * drawing scaled images.
319 */
reedf803da12015-01-23 05:58:07 -0800320 void setFilterLevel(FilterLevel level) {
321 this->setFilterQuality((SkFilterQuality)level);
322 }
323#endif
324
325 /**
326 * Return the filter level. This affects the quality (and performance) of
327 * drawing scaled images.
328 */
329 SkFilterQuality getFilterQuality() const {
330 return (SkFilterQuality)fBitfields.fFilterQuality;
331 }
332
333 /**
334 * Set the filter quality. This affects the quality (and performance) of
335 * drawing scaled images.
336 */
337 void setFilterQuality(SkFilterQuality quality);
reed@google.comc9683152013-07-18 13:47:01 +0000338
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 /** Styles apply to rect, oval, path, and text.
340 Bitmaps are always drawn in "fill", and lines are always drawn in
341 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000342
reed@android.comed881c22009-09-15 14:10:42 +0000343 Note: strokeandfill implicitly draws the result with
344 SkPath::kWinding_FillType, so if the original path is even-odd, the
345 results may not appear the same as if it was drawn twice, filled and
346 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 */
348 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000349 kFill_Style, //!< fill the geometry
350 kStroke_Style, //!< stroke the geometry
351 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000352 };
353 enum {
354 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 };
356
357 /** Return the paint's style, used for controlling how primitives'
358 geometries are interpreted (except for drawBitmap, which always assumes
359 kFill_Style).
360 @return the paint's Style
361 */
reedf59eab22014-07-14 14:39:15 -0700362 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363
364 /** Set the paint's style, used for controlling how primitives'
365 geometries are interpreted (except for drawBitmap, which always assumes
366 Fill).
367 @param style The new style to set in the paint
368 */
369 void setStyle(Style style);
370
371 /** Return the paint's color. Note that the color is a 32bit value
372 containing alpha as well as r,g,b. This 32bit value is not
373 premultiplied, meaning that its alpha can be any value, regardless of
374 the values of r,g,b.
375 @return the paint's color (and alpha).
376 */
377 SkColor getColor() const { return fColor; }
378
379 /** Set the paint's color. Note that the color is a 32bit value containing
380 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
381 that its alpha can be any value, regardless of the values of r,g,b.
382 @param color The new color (including alpha) to set in the paint.
383 */
384 void setColor(SkColor color);
385
386 /** Helper to getColor() that just returns the color's alpha value.
387 @return the alpha component of the paint's color.
388 */
389 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000390
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391 /** Helper to setColor(), that only assigns the color's alpha value,
392 leaving its r,g,b values unchanged.
393 @param a set the alpha component (0..255) of the paint's color.
394 */
395 void setAlpha(U8CPU a);
396
397 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
398 using SkColorSetARGB()
399 @param a The new alpha component (0..255) of the paint's color.
400 @param r The new red component (0..255) of the paint's color.
401 @param g The new green component (0..255) of the paint's color.
402 @param b The new blue component (0..255) of the paint's color.
403 */
404 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
405
reed@google.com9d07fec2011-03-16 20:02:59 +0000406 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 <p />
408 A value of 0 strokes in hairline mode.
409 Hairlines always draw 1-pixel wide, regardless of the matrix.
410 @return the paint's stroke width, used whenever the paint's style is
411 Stroke or StrokeAndFill.
412 */
413 SkScalar getStrokeWidth() const { return fWidth; }
414
reed@google.com9d07fec2011-03-16 20:02:59 +0000415 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416 Pass 0 to stroke in hairline mode.
417 Hairlines always draw 1-pixel wide, regardless of the matrix.
418 @param width set the paint's stroke width, used whenever the paint's
419 style is Stroke or StrokeAndFill.
420 */
421 void setStrokeWidth(SkScalar width);
422
423 /** Return the paint's stroke miter value. This is used to control the
424 behavior of miter joins when the joins angle is sharp.
425 @return the paint's miter limit, used whenever the paint's style is
426 Stroke or StrokeAndFill.
427 */
428 SkScalar getStrokeMiter() const { return fMiterLimit; }
429
430 /** Set the paint's stroke miter value. This is used to control the
431 behavior of miter joins when the joins angle is sharp. This value must
432 be >= 0.
433 @param miter set the miter limit on the paint, used whenever the
434 paint's style is Stroke or StrokeAndFill.
435 */
436 void setStrokeMiter(SkScalar miter);
437
438 /** Cap enum specifies the settings for the paint's strokecap. This is the
439 treatment that is applied to the beginning and end of each non-closed
440 contour (e.g. lines).
441 */
442 enum Cap {
443 kButt_Cap, //!< begin/end contours with no extension
444 kRound_Cap, //!< begin/end contours with a semi-circle extension
445 kSquare_Cap, //!< begin/end contours with a half square extension
446
447 kCapCount,
448 kDefault_Cap = kButt_Cap
449 };
450
451 /** Join enum specifies the settings for the paint's strokejoin. This is
452 the treatment that is applied to corners in paths and rectangles.
453 */
454 enum Join {
455 kMiter_Join, //!< connect path segments with a sharp join
456 kRound_Join, //!< connect path segments with a round join
457 kBevel_Join, //!< connect path segments with a flat bevel join
458
459 kJoinCount,
460 kDefault_Join = kMiter_Join
461 };
462
463 /** Return the paint's stroke cap type, controlling how the start and end
464 of stroked lines and paths are treated.
465 @return the line cap style for the paint, used whenever the paint's
466 style is Stroke or StrokeAndFill.
467 */
reedf59eab22014-07-14 14:39:15 -0700468 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000469
470 /** Set the paint's stroke cap type.
471 @param cap set the paint's line cap style, used whenever the paint's
472 style is Stroke or StrokeAndFill.
473 */
474 void setStrokeCap(Cap cap);
475
476 /** Return the paint's stroke join type.
477 @return the paint's line join style, used whenever the paint's style is
478 Stroke or StrokeAndFill.
479 */
reedf59eab22014-07-14 14:39:15 -0700480 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000481
482 /** Set the paint's stroke join type.
483 @param join set the paint's line join style, used whenever the paint's
484 style is Stroke or StrokeAndFill.
485 */
486 void setStrokeJoin(Join join);
487
reed@google.com4bbdeac2013-01-24 21:03:11 +0000488 /**
489 * Applies any/all effects (patheffect, stroking) to src, returning the
490 * result in dst. The result is that drawing src with this paint will be
491 * the same as drawing dst with a default paint (at least from the
492 * geometric perspective).
493 *
494 * @param src input path
495 * @param dst output path (may be the same as src)
496 * @param cullRect If not null, the dst path may be culled to this rect.
reed05d90442015-02-12 13:35:52 -0800497 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision
498 * in favor of speed/size.
reed@google.com4bbdeac2013-01-24 21:03:11 +0000499 * @return true if the path should be filled, or false if it should be
500 * drawn with a hairline (width == 0)
501 */
reed05d90442015-02-12 13:35:52 -0800502 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
503 SkScalar resScale = 1) const;
504
505 bool getFillPath(const SkPath& src, SkPath* dst) const {
506 return this->getFillPath(src, dst, NULL, 1);
507 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 /** Get the paint's shader object.
510 <p />
511 The shader's reference count is not affected.
512 @return the paint's shader (or NULL)
513 */
514 SkShader* getShader() const { return fShader; }
515
516 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000517 * Shaders specify the source color(s) for what is being drawn. If a paint
518 * has no shader, then the paint's color is used. If the paint has a
519 * shader, then the shader's color(s) are use instead, but they are
520 * modulated by the paint's alpha. This makes it easy to create a shader
521 * once (e.g. bitmap tiling or gradient) and then change its transparency
522 * w/o having to modify the original shader... only the paint's alpha needs
523 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000524 *
525 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
526 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
527 * then the shader will use the paint's entire color to "colorize" its output (modulating the
528 * bitmap's alpha with the paint's color+alpha).
529 *
reed@google.com880dc472012-05-11 14:47:03 +0000530 * Pass NULL to clear any previous shader.
531 * As a convenience, the parameter passed is also returned.
532 * If a previous shader exists, its reference count is decremented.
533 * If shader is not NULL, its reference count is incremented.
534 * @param shader May be NULL. The shader to be installed in the paint
535 * @return shader
536 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000538
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 /** Get the paint's colorfilter. If there is a colorfilter, its reference
540 count is not changed.
541 @return the paint's colorfilter (or NULL)
542 */
543 SkColorFilter* getColorFilter() const { return fColorFilter; }
544
545 /** Set or clear the paint's colorfilter, returning the parameter.
546 <p />
547 If the paint already has a filter, its reference count is decremented.
548 If filter is not NULL, its reference count is incremented.
549 @param filter May be NULL. The filter to be installed in the paint
550 @return filter
551 */
552 SkColorFilter* setColorFilter(SkColorFilter* filter);
553
554 /** Get the paint's xfermode object.
555 <p />
556 The xfermode's reference count is not affected.
557 @return the paint's xfermode (or NULL)
558 */
559 SkXfermode* getXfermode() const { return fXfermode; }
560
561 /** Set or clear the xfermode object.
562 <p />
563 Pass NULL to clear any previous xfermode.
564 As a convenience, the parameter passed is also returned.
565 If a previous xfermode exists, its reference count is decremented.
566 If xfermode is not NULL, its reference count is incremented.
567 @param xfermode May be NULL. The new xfermode to be installed in the
568 paint
569 @return xfermode
570 */
571 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000572
573 /** Create an xfermode based on the specified Mode, and assign it into the
574 paint, returning the mode that was set. If the Mode is SrcOver, then
575 the paint's xfermode is set to null.
576 */
reed@android.com0baf1932009-06-24 12:41:42 +0000577 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000578
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579 /** Get the paint's patheffect object.
580 <p />
581 The patheffect reference count is not affected.
582 @return the paint's patheffect (or NULL)
583 */
584 SkPathEffect* getPathEffect() const { return fPathEffect; }
585
586 /** Set or clear the patheffect object.
587 <p />
588 Pass NULL to clear any previous patheffect.
589 As a convenience, the parameter passed is also returned.
590 If a previous patheffect exists, its reference count is decremented.
591 If patheffect is not NULL, its reference count is incremented.
592 @param effect May be NULL. The new patheffect to be installed in the
593 paint
594 @return effect
595 */
596 SkPathEffect* setPathEffect(SkPathEffect* effect);
597
598 /** Get the paint's maskfilter object.
599 <p />
600 The maskfilter reference count is not affected.
601 @return the paint's maskfilter (or NULL)
602 */
603 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
604
605 /** Set or clear the maskfilter object.
606 <p />
607 Pass NULL to clear any previous maskfilter.
608 As a convenience, the parameter passed is also returned.
609 If a previous maskfilter exists, its reference count is decremented.
610 If maskfilter is not NULL, its reference count is incremented.
611 @param maskfilter May be NULL. The new maskfilter to be installed in
612 the paint
613 @return maskfilter
614 */
615 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
616
617 // These attributes are for text/fonts
618
619 /** Get the paint's typeface object.
620 <p />
621 The typeface object identifies which font to use when drawing or
622 measuring text. The typeface reference count is not affected.
623 @return the paint's typeface (or NULL)
624 */
625 SkTypeface* getTypeface() const { return fTypeface; }
626
627 /** Set or clear the typeface object.
628 <p />
629 Pass NULL to clear any previous typeface.
630 As a convenience, the parameter passed is also returned.
631 If a previous typeface exists, its reference count is decremented.
632 If typeface is not NULL, its reference count is incremented.
633 @param typeface May be NULL. The new typeface to be installed in the
634 paint
635 @return typeface
636 */
637 SkTypeface* setTypeface(SkTypeface* typeface);
638
639 /** Get the paint's rasterizer (or NULL).
640 <p />
641 The raster controls how paths/text are turned into alpha masks.
642 @return the paint's rasterizer (or NULL)
643 */
644 SkRasterizer* getRasterizer() const { return fRasterizer; }
645
646 /** Set or clear the rasterizer object.
647 <p />
648 Pass NULL to clear any previous rasterizer.
649 As a convenience, the parameter passed is also returned.
650 If a previous rasterizer exists in the paint, its reference count is
651 decremented. If rasterizer is not NULL, its reference count is
652 incremented.
653 @param rasterizer May be NULL. The new rasterizer to be installed in
654 the paint.
655 @return rasterizer
656 */
657 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
658
reed@google.com15356a62011-11-03 19:29:08 +0000659 SkImageFilter* getImageFilter() const { return fImageFilter; }
660 SkImageFilter* setImageFilter(SkImageFilter*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000661
reed@google.comb0a34d82012-07-11 19:57:55 +0000662 SkAnnotation* getAnnotation() const { return fAnnotation; }
663 SkAnnotation* setAnnotation(SkAnnotation*);
664
665 /**
666 * Returns true if there is an annotation installed on this paint, and
667 * the annotation specifics no-drawing.
668 */
reed@google.com44699382013-10-31 17:28:30 +0000669 SK_ATTR_DEPRECATED("use getAnnotation and check for non-null")
commit-bot@chromium.org950923b2013-10-29 20:44:39 +0000670 bool isNoDrawAnnotation() const { return this->getAnnotation() != NULL; }
reed@google.com15356a62011-11-03 19:29:08 +0000671
reed@google.com9d07fec2011-03-16 20:02:59 +0000672 /**
673 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
674 * reference count.
675 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000676 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000677
678 /**
679 * Set or clear the looper object.
680 * <p />
681 * Pass NULL to clear any previous looper.
682 * As a convenience, the parameter passed is also returned.
683 * If a previous looper exists in the paint, its reference count is
684 * decremented. If looper is not NULL, its reference count is
685 * incremented.
686 * @param looper May be NULL. The new looper to be installed in the paint.
687 * @return looper
688 */
689 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000690
691 enum Align {
692 kLeft_Align,
693 kCenter_Align,
694 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000695 };
696 enum {
697 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000698 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000699
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700 /** Return the paint's Align value for drawing text.
701 @return the paint's Align value for drawing text.
702 */
reedf59eab22014-07-14 14:39:15 -0700703 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000704
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705 /** Set the paint's text alignment.
706 @param align set the paint's Align value for drawing text.
707 */
708 void setTextAlign(Align align);
709
710 /** Return the paint's text size.
711 @return the paint's text size.
712 */
713 SkScalar getTextSize() const { return fTextSize; }
714
715 /** Set the paint's text size. This value must be > 0
716 @param textSize set the paint's text size.
717 */
718 void setTextSize(SkScalar textSize);
719
720 /** Return the paint's horizontal scale factor for text. The default value
721 is 1.0.
722 @return the paint's scale factor in X for drawing/measuring text
723 */
724 SkScalar getTextScaleX() const { return fTextScaleX; }
725
726 /** Set the paint's horizontal scale factor for text. The default value
727 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
728 stretch the text narrower.
729 @param scaleX set the paint's scale factor in X for drawing/measuring
730 text.
731 */
732 void setTextScaleX(SkScalar scaleX);
733
734 /** Return the paint's horizontal skew factor for text. The default value
735 is 0.
736 @return the paint's skew factor in X for drawing text.
737 */
738 SkScalar getTextSkewX() const { return fTextSkewX; }
739
740 /** Set the paint's horizontal skew factor for text. The default value
741 is 0. For approximating oblique text, use values around -0.25.
742 @param skewX set the paint's skew factor in X for drawing text.
743 */
744 void setTextSkewX(SkScalar skewX);
745
746 /** Describes how to interpret the text parameters that are passed to paint
747 methods like measureText() and getTextWidths().
748 */
749 enum TextEncoding {
750 kUTF8_TextEncoding, //!< the text parameters are UTF8
751 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000752 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000753 kGlyphID_TextEncoding //!< the text parameters are glyph indices
754 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000755
reedf59eab22014-07-14 14:39:15 -0700756 TextEncoding getTextEncoding() const {
757 return (TextEncoding)fBitfields.fTextEncoding;
758 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000759
760 void setTextEncoding(TextEncoding encoding);
761
762 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000763 /** Flags which indicate the confidence level of various metrics.
764 A set flag indicates that the metric may be trusted.
765 */
766 enum FontMetricsFlags {
767 kUnderlineThinknessIsValid_Flag = 1 << 0,
768 kUnderlinePositionIsValid_Flag = 1 << 1,
769 };
770
771 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000772 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
773 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
774 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
775 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
776 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000777 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
778 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000779 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
780 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000781 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
782 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000783 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
784
785 /** Underline Position - position of the top of the Underline stroke
786 relative to the baseline, this can have following values
787 - Negative - means underline should be drawn above baseline.
788 - Positive - means below baseline.
789 - Zero - mean underline should be drawn on baseline.
790 */
791 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
792
793 /** If the fontmetrics has a valid underlinethickness, 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 hasUnderlineThickness(SkScalar* thickness) const {
798 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
799 *thickness = fUnderlineThickness;
800 return true;
801 }
802 return false;
803 }
804
805 /** If the fontmetrics has a valid underlineposition, return true, and set the
806 thickness param to that value. If it doesn't return false and ignore the
807 thickness param.
808 */
809 bool hasUnderlinePosition(SkScalar* position) const {
810 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
811 *position = fUnderlinePosition;
812 return true;
813 }
814 return false;
815 }
816
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000818
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 /** Return the recommend spacing between lines (which will be
820 fDescent - fAscent + fLeading).
821 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000822 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 @param metrics If not null, returns the font metrics for the
824 current typeface/pointsize/etc setting in this
825 paint.
826 @param scale If not 0, return width as if the canvas were scaled
827 by this value
828 @param return the recommended spacing between lines
829 */
830 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000831
reed@android.com8a1c16f2008-12-17 15:59:43 +0000832 /** Return the recommend line spacing. This will be
833 fDescent - fAscent + fLeading
834 */
835 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
836
837 /** Convert the specified text into glyph IDs, returning the number of
838 glyphs ID written. If glyphs is NULL, it is ignore and only the count
839 is returned.
840 */
841 int textToGlyphs(const void* text, size_t byteLength,
842 uint16_t glyphs[]) const;
843
reed@android.coma5dcaf62010-02-05 17:12:32 +0000844 /** Return true if all of the specified text has a corresponding non-zero
845 glyph ID. If any of the code-points in the text are not supported in
846 the typeface (i.e. the glyph ID would be zero), then return false.
847
848 If the text encoding for the paint is kGlyph_TextEncoding, then this
849 returns true if all of the specified glyph IDs are non-zero.
850 */
851 bool containsText(const void* text, size_t byteLength) const;
852
reed@android.com9d3a9852010-01-08 14:07:42 +0000853 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
854 to zero. Note: this does not look at the text-encoding setting in the
855 paint, only at the typeface.
856 */
857 void glyphsToUnichars(const uint16_t glyphs[], int count,
858 SkUnichar text[]) const;
859
reed@android.com8a1c16f2008-12-17 15:59:43 +0000860 /** Return the number of drawable units in the specified text buffer.
861 This looks at the current TextEncoding field of the paint. If you also
862 want to have the text converted into glyph IDs, call textToGlyphs
863 instead.
864 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000865 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000866 return this->textToGlyphs(text, byteLength, NULL);
867 }
868
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 The text to be measured
874 * @param length Number of bytes of text to measure
875 * @param bounds If not NULL, returns the bounds of the text,
876 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000877 * @return The advance width of the text
878 */
reed99ae8812014-08-26 11:30:01 -0700879 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880
reed@google.com44da42e2011-11-10 20:04:47 +0000881 /** Return the width of the text. This will return the vertical measure
882 * if isVerticalText() is true, in which case the returned value should
883 * be treated has a height instead of a width.
884 *
885 * @param text Address of the text
886 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000887 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000888 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000889 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700890 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000891 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000892
reed@google.com44da42e2011-11-10 20:04:47 +0000893 /** Return the number of bytes of text that were measured. If
894 * isVerticalText() is true, then the vertical advances are used for
895 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000896 *
reed@google.com44da42e2011-11-10 20:04:47 +0000897 * @param text The text to be measured
898 * @param length Number of bytes of text to measure
899 * @param maxWidth Maximum width. Only the subset of text whose accumulated
900 * widths are <= maxWidth are measured.
901 * @param measuredWidth Optional. If non-null, this returns the actual
902 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000903 * @return The number of bytes of text that were measured. Will be
904 * <= length.
905 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000906 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700907 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000908
reed@google.com44da42e2011-11-10 20:04:47 +0000909 /** Return the advances for the text. These will be vertical advances if
910 * isVerticalText() returns true.
911 *
912 * @param text the text
913 * @param byteLength number of bytes to of text
914 * @param widths If not null, returns the array of advances for
915 * the glyphs. If not NULL, must be at least a large
916 * as the number of unichars in the specified text.
917 * @param bounds If not null, returns the bounds for each of
918 * character, relative to (0, 0)
919 * @return the number of unichars in the specified text.
920 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000921 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
922 SkRect bounds[] = NULL) const;
923
924 /** Return the path (outline) for the specified text.
925 Note: just like SkCanvas::drawText, this will respect the Align setting
926 in the paint.
927 */
928 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
929 SkPath* path) const;
930
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000931 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000932 const SkPoint pos[], SkPath* path) const;
933
reed8893e5f2014-12-15 13:27:26 -0800934 /**
935 * Return a rectangle that represents the union of the bounds of all
936 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
937 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
938 * on this paint.
939 */
940 SkRect getFontBounds() const;
941
reed@google.com632e1a22011-10-06 12:37:00 +0000942 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
943 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
944 bool nothingToDraw() const;
945
reed@google.coma584aed2012-05-16 14:06:02 +0000946 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000947 // would prefer to make these private...
948
reed@google.coma584aed2012-05-16 14:06:02 +0000949 /** Returns true if the current paint settings allow for fast computation of
950 bounds (i.e. there is nothing complex like a patheffect that would make
951 the bounds computation expensive.
952 */
953 bool canComputeFastBounds() const {
954 if (this->getLooper()) {
955 return this->getLooper()->canComputeFastBounds(*this);
956 }
957 return !this->getRasterizer();
958 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000959
reed@google.coma584aed2012-05-16 14:06:02 +0000960 /** Only call this if canComputeFastBounds() returned true. This takes a
961 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
962 effects in the paint (e.g. stroking). If needed, it uses the storage
963 rect parameter. It returns the adjusted bounds that can then be used
964 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000965
reed@google.coma584aed2012-05-16 14:06:02 +0000966 The returned rect will either be orig or storage, thus the caller
967 should not rely on storage being set to the result, but should always
968 use the retured value. It is legal for orig and storage to be the same
969 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000970
reed@google.coma584aed2012-05-16 14:06:02 +0000971 e.g.
972 if (paint.canComputeFastBounds()) {
973 SkRect r, storage;
974 path.computeBounds(&r, SkPath::kFast_BoundsType);
975 const SkRect& fastR = paint.computeFastBounds(r, &storage);
976 if (canvas->quickReject(fastR, ...)) {
977 // don't draw the path
978 }
979 }
980 */
981 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
982 SkPaint::Style style = this->getStyle();
983 // ultra fast-case: filling with no effects that affect geometry
984 if (kFill_Style == style) {
985 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
986 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
987 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000988 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +0000989 if (!effects) {
990 return orig;
991 }
992 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000993
reed@google.coma584aed2012-05-16 14:06:02 +0000994 return this->doComputeFastBounds(orig, storage, style);
995 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000996
reed@google.coma584aed2012-05-16 14:06:02 +0000997 const SkRect& computeFastStrokeBounds(const SkRect& orig,
998 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +0000999 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001000 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001001
reed@google.coma584aed2012-05-16 14:06:02 +00001002 // Take the style explicitly, so the caller can force us to be stroked
1003 // without having to make a copy of the paint just to change that field.
1004 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1005 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001006
reed@google.comed43dff2013-06-04 16:56:27 +00001007 /**
1008 * Return a matrix that applies the paint's text values: size, scale, skew
1009 */
1010 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1011 SkScalar scaleX, SkScalar skewX) {
1012 matrix->setScale(size * scaleX, size);
1013 if (skewX) {
1014 matrix->postSkew(skewX, 0);
1015 }
1016 return matrix;
1017 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001018
reed@google.comed43dff2013-06-04 16:56:27 +00001019 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1020 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1021 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001022
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001023 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001024
reed@google.comd5f20792012-05-16 14:15:02 +00001025private:
1026 SkTypeface* fTypeface;
reed@google.comd5f20792012-05-16 14:15:02 +00001027 SkPathEffect* fPathEffect;
1028 SkShader* fShader;
1029 SkXfermode* fXfermode;
1030 SkMaskFilter* fMaskFilter;
1031 SkColorFilter* fColorFilter;
1032 SkRasterizer* fRasterizer;
1033 SkDrawLooper* fLooper;
1034 SkImageFilter* fImageFilter;
reed@google.comb0a34d82012-07-11 19:57:55 +00001035 SkAnnotation* fAnnotation;
reed@google.comd5f20792012-05-16 14:15:02 +00001036
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001037 SkScalar fTextSize;
1038 SkScalar fTextScaleX;
1039 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001040 SkColor fColor;
1041 SkScalar fWidth;
1042 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001043 union {
1044 struct {
1045 // all of these bitfields should add up to 32
1046 unsigned fFlags : 16;
1047 unsigned fTextAlign : 2;
1048 unsigned fCapType : 2;
1049 unsigned fJoinType : 2;
1050 unsigned fStyle : 2;
1051 unsigned fTextEncoding : 2; // 3 values
1052 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001053 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001054 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001055 } fBitfields;
1056 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001057 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001058
reed@google.comd5f20792012-05-16 14:15:02 +00001059 SkDrawCacheProc getDrawCacheProc() const;
reed9e96aa02014-10-03 12:44:37 -07001060 SkMeasureCacheProc getMeasureCacheProc(bool needFullMetrics) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001061
1062 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1063 int* count, SkRect* bounds) const;
1064
joshualittfd450792015-03-13 08:38:43 -07001065 /*
1066 * Allocs an SkDescriptor on the heap and return it to the caller as a refcnted
1067 * SkData. Caller is responsible for managing the lifetime of this object.
1068 */
robertphillipsfcf78292015-06-19 11:49:52 -07001069 void getScalerContextDescriptor(SkAutoDescriptor*, const SkSurfaceProps& surfaceProps,
joshualitt2b6acb42015-04-01 11:30:27 -07001070 const SkMatrix*, bool ignoreGamma) const;
joshualittfd450792015-03-13 08:38:43 -07001071
robertphillipsfcf78292015-06-19 11:49:52 -07001072 SkGlyphCache* detachCache(const SkSurfaceProps* surfaceProps, const SkMatrix*,
jvanverth2d2a68c2014-06-10 06:42:56 -07001073 bool ignoreGamma) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001074
robertphillipsfcf78292015-06-19 11:49:52 -07001075 void descriptorProc(const SkSurfaceProps* surfaceProps, const SkMatrix* deviceMatrix,
reed@google.com90808e82013-03-19 14:44:54 +00001076 void (*proc)(SkTypeface*, const SkDescriptor*, void*),
robertphillipsfcf78292015-06-19 11:49:52 -07001077 void* context, bool ignoreGamma) const;
reed@android.comd252db02009-04-01 18:31:44 +00001078
joshualitt9e36c1a2015-04-14 12:17:27 -07001079 /*
1080 * The luminance color is used to determine which Gamma Canonical color to map to. This is
1081 * really only used by backends which want to cache glyph masks, and need some way to know if
1082 * they need to generate new masks based off a given color.
1083 */
1084 SkColor computeLuminanceColor() const;
1085
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001086 static void Term();
1087
reed@android.com8a1c16f2008-12-17 15:59:43 +00001088 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001089 /* This is the size we use when we ask for a glyph's path. We then
1090 * post-transform it as we draw to match the request.
1091 * This is done to try to re-use cache entries for the path.
1092 *
1093 * This value is somewhat arbitrary. In theory, it could be 1, since
1094 * we store paths as floats. However, we get the path from the font
1095 * scaler, and it may represent its paths as fixed-point (or 26.6),
1096 * so we shouldn't ask for something too big (might overflow 16.16)
1097 * or too small (underflow 26.6).
1098 *
1099 * This value could track kMaxSizeForGlyphCache, assuming the above
1100 * constraints, but since we ask for unhinted paths, the two values
1101 * need not match per-se.
1102 */
1103 kCanonicalTextSizeForPaths = 64,
1104
1105 /*
1106 * Above this size (taking into account CTM and textSize), we never use
1107 * the cache for bits or metrics (we might overflow), so we just ask
1108 * for a caononical size and post-transform that.
1109 */
1110 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001111 };
reed@google.comed43dff2013-06-04 16:56:27 +00001112
1113 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1114
reed@google.comed43dff2013-06-04 16:56:27 +00001115 // Set flags/hinting/textSize up to use for drawing text as paths.
1116 // Returns scale factor to restore the original textSize, since will will
1117 // have change it to kCanonicalTextSizeForPaths.
1118 SkScalar setupForAsPaths();
1119
1120 static SkScalar MaxCacheSize2() {
1121 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1122 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1123 return kMag2Max;
1124 }
1125
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001126 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001127 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128 friend class SkCanvas;
1129 friend class SkDraw;
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001130 friend class SkGraphics; // So Term() can be called.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001131 friend class SkPDFDevice;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00001132 friend class GrBitmapTextContext;
joshualittdbd35932015-04-02 09:19:04 -07001133 friend class GrAtlasTextContext;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001134 friend class GrDistanceFieldTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001135 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001136 friend class GrPathRendering;
joshualitt6e8cd962015-03-20 10:30:14 -07001137 friend class GrTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001138 friend class GrGLPathRendering;
joshualitt9e36c1a2015-04-14 12:17:27 -07001139 friend class SkScalerContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001140 friend class SkTextToPathIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001141 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001142};
1143
reed@android.com8a1c16f2008-12-17 15:59:43 +00001144#endif