blob: 464e5357580b4ad05682d69f8481529f96c72d38 [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
reed374772b2016-10-05 17:33:02 -070011#include "SkBlendMode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkColor.h"
reedf803da12015-01-23 05:58:07 -080013#include "SkFilterQuality.h"
reed@google.comed43dff2013-06-04 16:56:27 +000014#include "SkMatrix.h"
Mike Reed71fecc32016-11-18 17:19:54 -050015#include "SkRefCnt.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000016
joshualitt2b6acb42015-04-01 11:30:27 -070017class SkAutoDescriptor;
reed@android.com8a1c16f2008-12-17 15:59:43 +000018class SkAutoGlyphCache;
19class SkColorFilter;
joshualittfd450792015-03-13 08:38:43 -070020class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000021class SkDescriptor;
senorblanco0abdf762015-08-20 11:10:41 -070022class SkDrawLooper;
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;
reeda9322c22016-04-12 06:47:05 -070034struct SkScalerContextEffects;
reed@android.com8a1c16f2008-12-17 15:59:43 +000035class SkShader;
robertphillipsfcf78292015-06-19 11:49:52 -070036class SkSurfaceProps;
fmalitaeae6a912016-07-28 09:47:24 -070037class SkTextBlob;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040/** \class SkPaint
41
42 The SkPaint class holds the style and color information about how to draw
43 geometries, text and bitmaps.
44*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000045class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000046public:
47 SkPaint();
48 SkPaint(const SkPaint& paint);
bungemanccce0e02016-02-07 14:37:23 -080049 SkPaint(SkPaint&& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 ~SkPaint();
51
Cary Clark0418a882017-05-10 09:07:42 -040052 SkPaint& operator=(const SkPaint& paint);
53 SkPaint& operator=(SkPaint&& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000054
mtkleinbc97ef42014-08-25 10:10:47 -070055 /** operator== may give false negatives: two paints that draw equivalently
56 may return false. It will never give false positives: two paints that
57 are not equivalent always return false.
58 */
robertphillips@google.comb2657412013-08-07 22:36:29 +000059 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
60 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
61 return !(a == b);
62 }
63
mtkleinfb1fe4f2014-10-07 09:26:10 -070064 /** getHash() is a shallow hash, with the same limitations as operator==.
65 * If operator== returns true for two paints, getHash() returns the same value for each.
66 */
67 uint32_t getHash() const;
68
Cary Clark0418a882017-05-10 09:07:42 -040069 void flatten(SkWriteBuffer& buffer) const;
70 void unflatten(SkReadBuffer& buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +000071
72 /** Restores the paint to its initial settings.
73 */
74 void reset();
75
agl@chromium.org309485b2009-07-21 17:41:32 +000076 /** Specifies the level of hinting to be performed. These names are taken
77 from the Gnome/Cairo names for the same. They are translated into
78 Freetype concepts the same as in cairo-ft-font.c:
79 kNo_Hinting -> FT_LOAD_NO_HINTING
80 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
81 kNormal_Hinting -> <default, no option>
82 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
83 subpixel glyphs, in which case TARGET_LCD or
84 TARGET_LCD_V is used>
85 */
86 enum Hinting {
87 kNo_Hinting = 0,
88 kSlight_Hinting = 1,
89 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000090 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000091 };
92
reed@google.com9d07fec2011-03-16 20:02:59 +000093 Hinting getHinting() const {
reedf59eab22014-07-14 14:39:15 -070094 return static_cast<Hinting>(fBitfields.fHinting);
agl@chromium.org309485b2009-07-21 17:41:32 +000095 }
96
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +000097 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +000098
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 /** Specifies the bit values that are stored in the paint's flags.
100 */
101 enum Flags {
102 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
Mike Reed44353112017-05-09 12:20:02 -0400103 kDither_Flag = 0x04, //!< mask to enable dithering. see setDither()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 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
agl@chromium.org309485b2009-07-21 17:41:32 +0000113 // when adding extra flags, note that the fFlags member is specified
114 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115
Mike Reedbcfb8f62017-02-23 14:19:35 +0000116 kAllFlags = 0xFFFF,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 };
118
Mike Reedddbd37e2017-02-21 15:07:44 -0500119#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
120 enum ReserveFlags {
121 // These are not used by paint, but the bits are reserved for private use by the
122 // android framework.
123 kUnderlineText_ReserveFlag = 0x08, //!< mask to enable underline text
124 kStrikeThruText_ReserveFlag = 0x10, //!< mask to enable strike-thru text
125 };
126#endif
127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 /** Return the paint's flags. Use the Flag enum to test flag values.
129 @return the paint's flags (see enums ending in _Flag for bit masks)
130 */
reedf59eab22014-07-14 14:39:15 -0700131 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132
133 /** Set the paint's flags. Use the Flag enum to specific flag values.
134 @param flags The new flag bits for the paint (see Flags enum)
135 */
136 void setFlags(uint32_t flags);
137
138 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
139 @return true if the antialias bit is set in the paint's flags.
140 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000141 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 return SkToBool(this->getFlags() & kAntiAlias_Flag);
143 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
146 @param aa true to enable antialiasing, false to disable it
147 */
148 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 /** Helper for getFlags(), returning true if kDither_Flag bit is set
151 @return true if the dithering bit is set in the paint's flags.
152 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000153 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 return SkToBool(this->getFlags() & kDither_Flag);
155 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000156
Mike Reed44353112017-05-09 12:20:02 -0400157 /**
158 * Helper for setFlags(), setting or clearing the kDither_Flag bit
159 * @param dither true to enable dithering, false to disable it
160 *
161 * Note: gradients ignore this setting and always dither.
162 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
166 @return true if the lineartext bit is set in the paint's flags
167 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000168 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 return SkToBool(this->getFlags() & kLinearText_Flag);
170 }
171
172 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
173 @param linearText true to set the linearText bit in the paint's flags,
174 false to clear it.
175 */
176 void setLinearText(bool linearText);
177
178 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
179 @return true if the lineartext bit is set in the paint's flags
180 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000181 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 return SkToBool(this->getFlags() & kSubpixelText_Flag);
183 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000184
reed@google.com84b437e2011-08-01 12:45:35 +0000185 /**
186 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
187 * @param subpixelText true to set the subpixelText bit in the paint's
188 * flags, false to clear it.
189 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000191
reed@google.com9d07fec2011-03-16 20:02:59 +0000192 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000193 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
194 }
195
reed@google.com84b437e2011-08-01 12:45:35 +0000196 /**
197 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
198 * Note: antialiasing must also be on for lcd rendering
199 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
200 * false to clear it.
201 */
202 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000203
reed@google.com9d07fec2011-03-16 20:02:59 +0000204 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000205 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
206 }
207
208 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
209 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
210 false to clear it.
211 */
212 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
213
reed@google.com9d07fec2011-03-16 20:02:59 +0000214 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000215 return SkToBool(this->getFlags() & kAutoHinting_Flag);
216 }
217
218 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
219 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
220 paint's flags,
221 false to clear it.
222 */
223 void setAutohinted(bool useAutohinter);
224
reed@google.com830a23e2011-11-10 15:20:49 +0000225 bool isVerticalText() const {
226 return SkToBool(this->getFlags() & kVerticalText_Flag);
227 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000228
reed@google.com830a23e2011-11-10 15:20:49 +0000229 /**
230 * Helper for setting or clearing the kVerticalText_Flag bit in
231 * setFlags(...).
232 *
233 * If this bit is set, then advances are treated as Y values rather than
234 * X values, and drawText will places its glyphs vertically rather than
235 * horizontally.
236 */
Cary Clark0418a882017-05-10 09:07:42 -0400237 void setVerticalText(bool verticalText);
reed@google.com830a23e2011-11-10 15:20:49 +0000238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
240 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
241 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000242 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
244 }
245
246 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
247 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
248 flags, false to clear it.
249 */
250 void setFakeBoldText(bool fakeBoldText);
251
252 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
253 @return true if the kernText bit is set in the paint's flags.
254 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000255 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 return SkToBool(this->getFlags() & kDevKernText_Flag);
257 }
258
259 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
260 @param kernText true to set the kKernText_Flag bit in the paint's
261 flags, false to clear it.
262 */
263 void setDevKernText(bool devKernText);
264
reedf803da12015-01-23 05:58:07 -0800265 /**
266 * Return the filter level. This affects the quality (and performance) of
267 * drawing scaled images.
268 */
269 SkFilterQuality getFilterQuality() const {
270 return (SkFilterQuality)fBitfields.fFilterQuality;
271 }
mtkleinfe81e2d2015-09-09 07:35:42 -0700272
reedf803da12015-01-23 05:58:07 -0800273 /**
274 * Set the filter quality. This affects the quality (and performance) of
275 * drawing scaled images.
276 */
277 void setFilterQuality(SkFilterQuality quality);
reed@google.comc9683152013-07-18 13:47:01 +0000278
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 /** Styles apply to rect, oval, path, and text.
280 Bitmaps are always drawn in "fill", and lines are always drawn in
281 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000282
reed@android.comed881c22009-09-15 14:10:42 +0000283 Note: strokeandfill implicitly draws the result with
284 SkPath::kWinding_FillType, so if the original path is even-odd, the
285 results may not appear the same as if it was drawn twice, filled and
286 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 */
288 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000289 kFill_Style, //!< fill the geometry
290 kStroke_Style, //!< stroke the geometry
291 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000292 };
293 enum {
294 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 };
296
297 /** Return the paint's style, used for controlling how primitives'
298 geometries are interpreted (except for drawBitmap, which always assumes
299 kFill_Style).
300 @return the paint's Style
301 */
reedf59eab22014-07-14 14:39:15 -0700302 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303
304 /** Set the paint's style, used for controlling how primitives'
305 geometries are interpreted (except for drawBitmap, which always assumes
306 Fill).
307 @param style The new style to set in the paint
308 */
309 void setStyle(Style style);
310
311 /** Return the paint's color. Note that the color is a 32bit value
312 containing alpha as well as r,g,b. This 32bit value is not
313 premultiplied, meaning that its alpha can be any value, regardless of
314 the values of r,g,b.
315 @return the paint's color (and alpha).
316 */
317 SkColor getColor() const { return fColor; }
318
319 /** Set the paint's color. Note that the color is a 32bit value containing
320 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
321 that its alpha can be any value, regardless of the values of r,g,b.
322 @param color The new color (including alpha) to set in the paint.
323 */
324 void setColor(SkColor color);
325
326 /** Helper to getColor() that just returns the color's alpha value.
327 @return the alpha component of the paint's color.
328 */
329 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000330
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331 /** Helper to setColor(), that only assigns the color's alpha value,
332 leaving its r,g,b values unchanged.
333 @param a set the alpha component (0..255) of the paint's color.
334 */
335 void setAlpha(U8CPU a);
336
337 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
338 using SkColorSetARGB()
339 @param a The new alpha component (0..255) of the paint's color.
340 @param r The new red component (0..255) of the paint's color.
341 @param g The new green component (0..255) of the paint's color.
342 @param b The new blue component (0..255) of the paint's color.
343 */
344 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
345
reed@google.com9d07fec2011-03-16 20:02:59 +0000346 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 <p />
348 A value of 0 strokes in hairline mode.
349 Hairlines always draw 1-pixel wide, regardless of the matrix.
350 @return the paint's stroke width, used whenever the paint's style is
351 Stroke or StrokeAndFill.
352 */
353 SkScalar getStrokeWidth() const { return fWidth; }
354
reed@google.com9d07fec2011-03-16 20:02:59 +0000355 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 Pass 0 to stroke in hairline mode.
357 Hairlines always draw 1-pixel wide, regardless of the matrix.
358 @param width set the paint's stroke width, used whenever the paint's
359 style is Stroke or StrokeAndFill.
360 */
361 void setStrokeWidth(SkScalar width);
362
363 /** Return the paint's stroke miter value. This is used to control the
364 behavior of miter joins when the joins angle is sharp.
365 @return the paint's miter limit, used whenever the paint's style is
366 Stroke or StrokeAndFill.
367 */
368 SkScalar getStrokeMiter() const { return fMiterLimit; }
369
370 /** Set the paint's stroke miter value. This is used to control the
371 behavior of miter joins when the joins angle is sharp. This value must
372 be >= 0.
373 @param miter set the miter limit on the paint, used whenever the
374 paint's style is Stroke or StrokeAndFill.
375 */
376 void setStrokeMiter(SkScalar miter);
377
378 /** Cap enum specifies the settings for the paint's strokecap. This is the
379 treatment that is applied to the beginning and end of each non-closed
380 contour (e.g. lines).
caryclark5cb00a92015-08-26 09:04:55 -0700381
382 If the cap is round or square, the caps are drawn when the contour has
383 a zero length. Zero length contours can be created by following moveTo
384 with a lineTo at the same point, or a moveTo followed by a close.
385
386 A dash with an on interval of zero also creates a zero length contour.
387
388 The zero length contour draws the square cap without rotation, since
389 the no direction can be inferred.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 */
391 enum Cap {
392 kButt_Cap, //!< begin/end contours with no extension
393 kRound_Cap, //!< begin/end contours with a semi-circle extension
394 kSquare_Cap, //!< begin/end contours with a half square extension
395
bsalomona7d85ba2016-07-06 11:54:59 -0700396 kLast_Cap = kSquare_Cap,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397 kDefault_Cap = kButt_Cap
398 };
bsalomona7d85ba2016-07-06 11:54:59 -0700399 static constexpr int kCapCount = kLast_Cap + 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400
401 /** Join enum specifies the settings for the paint's strokejoin. This is
402 the treatment that is applied to corners in paths and rectangles.
403 */
404 enum Join {
405 kMiter_Join, //!< connect path segments with a sharp join
406 kRound_Join, //!< connect path segments with a round join
407 kBevel_Join, //!< connect path segments with a flat bevel join
408
bsalomona7d85ba2016-07-06 11:54:59 -0700409 kLast_Join = kBevel_Join,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 kDefault_Join = kMiter_Join
411 };
bsalomona7d85ba2016-07-06 11:54:59 -0700412 static constexpr int kJoinCount = kLast_Join + 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413
414 /** Return the paint's stroke cap type, controlling how the start and end
415 of stroked lines and paths are treated.
416 @return the line cap style for the paint, used whenever the paint's
417 style is Stroke or StrokeAndFill.
418 */
reedf59eab22014-07-14 14:39:15 -0700419 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420
421 /** Set the paint's stroke cap type.
422 @param cap set the paint's line cap style, used whenever the paint's
423 style is Stroke or StrokeAndFill.
424 */
425 void setStrokeCap(Cap cap);
426
427 /** Return the paint's stroke join type.
428 @return the paint's line join style, used whenever the paint's style is
429 Stroke or StrokeAndFill.
430 */
reedf59eab22014-07-14 14:39:15 -0700431 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432
433 /** Set the paint's stroke join type.
434 @param join set the paint's line join style, used whenever the paint's
435 style is Stroke or StrokeAndFill.
436 */
437 void setStrokeJoin(Join join);
438
reed@google.com4bbdeac2013-01-24 21:03:11 +0000439 /**
440 * Applies any/all effects (patheffect, stroking) to src, returning the
441 * result in dst. The result is that drawing src with this paint will be
442 * the same as drawing dst with a default paint (at least from the
443 * geometric perspective).
444 *
445 * @param src input path
446 * @param dst output path (may be the same as src)
447 * @param cullRect If not null, the dst path may be culled to this rect.
reed05d90442015-02-12 13:35:52 -0800448 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision
449 * in favor of speed/size.
reed@google.com4bbdeac2013-01-24 21:03:11 +0000450 * @return true if the path should be filled, or false if it should be
451 * drawn with a hairline (width == 0)
452 */
reed05d90442015-02-12 13:35:52 -0800453 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
454 SkScalar resScale = 1) const;
455
456 bool getFillPath(const SkPath& src, SkPath* dst) const {
457 return this->getFillPath(src, dst, NULL, 1);
458 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 /** Get the paint's shader object.
461 <p />
462 The shader's reference count is not affected.
463 @return the paint's shader (or NULL)
464 */
reeda5ab9ec2016-03-06 18:10:48 -0800465 SkShader* getShader() const { return fShader.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500466 sk_sp<SkShader> refShader() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467
468 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000469 * Shaders specify the source color(s) for what is being drawn. If a paint
470 * has no shader, then the paint's color is used. If the paint has a
471 * shader, then the shader's color(s) are use instead, but they are
472 * modulated by the paint's alpha. This makes it easy to create a shader
473 * once (e.g. bitmap tiling or gradient) and then change its transparency
474 * w/o having to modify the original shader... only the paint's alpha needs
475 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000476 *
477 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
478 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
479 * then the shader will use the paint's entire color to "colorize" its output (modulating the
480 * bitmap's alpha with the paint's color+alpha).
481 *
reed@google.com880dc472012-05-11 14:47:03 +0000482 * Pass NULL to clear any previous shader.
483 * As a convenience, the parameter passed is also returned.
484 * If a previous shader exists, its reference count is decremented.
485 * If shader is not NULL, its reference count is incremented.
486 * @param shader May be NULL. The shader to be installed in the paint
reed@google.com880dc472012-05-11 14:47:03 +0000487 */
Cary Clark0418a882017-05-10 09:07:42 -0400488 void setShader(sk_sp<SkShader> shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000489
reed@android.com8a1c16f2008-12-17 15:59:43 +0000490 /** Get the paint's colorfilter. If there is a colorfilter, its reference
491 count is not changed.
492 @return the paint's colorfilter (or NULL)
493 */
reeda5ab9ec2016-03-06 18:10:48 -0800494 SkColorFilter* getColorFilter() const { return fColorFilter.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500495 sk_sp<SkColorFilter> refColorFilter() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496
Mike Reed958788a2016-10-20 16:40:26 -0400497 /** Set or clear the paint's colorfilter.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498 <p />
499 If the paint already has a filter, its reference count is decremented.
500 If filter is not NULL, its reference count is incremented.
501 @param filter May be NULL. The filter to be installed in the paint
reed@android.com8a1c16f2008-12-17 15:59:43 +0000502 */
Cary Clark0418a882017-05-10 09:07:42 -0400503 void setColorFilter(sk_sp<SkColorFilter> colorFilter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504
reed374772b2016-10-05 17:33:02 -0700505 SkBlendMode getBlendMode() const { return (SkBlendMode)fBlendMode; }
506 bool isSrcOver() const { return (SkBlendMode)fBlendMode == SkBlendMode::kSrcOver; }
507 void setBlendMode(SkBlendMode mode) { fBlendMode = (unsigned)mode; }
reed@android.coma0f5d152009-06-22 17:38:10 +0000508
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 /** Get the paint's patheffect object.
510 <p />
511 The patheffect reference count is not affected.
512 @return the paint's patheffect (or NULL)
513 */
reeda5ab9ec2016-03-06 18:10:48 -0800514 SkPathEffect* getPathEffect() const { return fPathEffect.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500515 sk_sp<SkPathEffect> refPathEffect() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000516
517 /** Set or clear the patheffect object.
518 <p />
519 Pass NULL to clear any previous patheffect.
520 As a convenience, the parameter passed is also returned.
521 If a previous patheffect exists, its reference count is decremented.
522 If patheffect is not NULL, its reference count is incremented.
523 @param effect May be NULL. The new patheffect to be installed in the
524 paint
525 @return effect
526 */
Cary Clark0418a882017-05-10 09:07:42 -0400527 void setPathEffect(sk_sp<SkPathEffect> pathEffect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528
529 /** Get the paint's maskfilter object.
530 <p />
531 The maskfilter reference count is not affected.
532 @return the paint's maskfilter (or NULL)
533 */
reeda5ab9ec2016-03-06 18:10:48 -0800534 SkMaskFilter* getMaskFilter() const { return fMaskFilter.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500535 sk_sp<SkMaskFilter> refMaskFilter() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536
537 /** Set or clear the maskfilter object.
538 <p />
539 Pass NULL to clear any previous maskfilter.
540 As a convenience, the parameter passed is also returned.
541 If a previous maskfilter exists, its reference count is decremented.
542 If maskfilter is not NULL, its reference count is incremented.
543 @param maskfilter May be NULL. The new maskfilter to be installed in
544 the paint
545 @return maskfilter
546 */
Cary Clark0418a882017-05-10 09:07:42 -0400547 void setMaskFilter(sk_sp<SkMaskFilter> maskFilter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548
549 // These attributes are for text/fonts
550
551 /** Get the paint's typeface object.
552 <p />
553 The typeface object identifies which font to use when drawing or
554 measuring text. The typeface reference count is not affected.
555 @return the paint's typeface (or NULL)
556 */
reeda5ab9ec2016-03-06 18:10:48 -0800557 SkTypeface* getTypeface() const { return fTypeface.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500558 sk_sp<SkTypeface> refTypeface() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000559
560 /** Set or clear the typeface object.
561 <p />
562 Pass NULL to clear any previous typeface.
563 As a convenience, the parameter passed is also returned.
564 If a previous typeface exists, its reference count is decremented.
565 If typeface is not NULL, its reference count is incremented.
566 @param typeface May be NULL. The new typeface to be installed in the
567 paint
568 @return typeface
569 */
Cary Clark0418a882017-05-10 09:07:42 -0400570 void setTypeface(sk_sp<SkTypeface> typeface);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000571
572 /** Get the paint's rasterizer (or NULL).
573 <p />
574 The raster controls how paths/text are turned into alpha masks.
575 @return the paint's rasterizer (or NULL)
576 */
reeda5ab9ec2016-03-06 18:10:48 -0800577 SkRasterizer* getRasterizer() const { return fRasterizer.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500578 sk_sp<SkRasterizer> refRasterizer() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579
580 /** Set or clear the rasterizer object.
581 <p />
582 Pass NULL to clear any previous rasterizer.
583 As a convenience, the parameter passed is also returned.
584 If a previous rasterizer exists in the paint, its reference count is
585 decremented. If rasterizer is not NULL, its reference count is
586 incremented.
587 @param rasterizer May be NULL. The new rasterizer to be installed in
588 the paint.
589 @return rasterizer
590 */
Cary Clark0418a882017-05-10 09:07:42 -0400591 void setRasterizer(sk_sp<SkRasterizer> rasterizer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000592
reeda5ab9ec2016-03-06 18:10:48 -0800593 SkImageFilter* getImageFilter() const { return fImageFilter.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500594 sk_sp<SkImageFilter> refImageFilter() const;
Cary Clark0418a882017-05-10 09:07:42 -0400595 void setImageFilter(sk_sp<SkImageFilter> imageFilter);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000596
reed@google.comb0a34d82012-07-11 19:57:55 +0000597 /**
reed@google.com9d07fec2011-03-16 20:02:59 +0000598 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
599 * reference count.
600 */
reed46f2d0a2016-09-11 05:40:31 -0700601 SkDrawLooper* getDrawLooper() const { return fDrawLooper.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500602 sk_sp<SkDrawLooper> refDrawLooper() const;
603
reed46f2d0a2016-09-11 05:40:31 -0700604 SkDrawLooper* getLooper() const { return fDrawLooper.get(); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000605 /**
606 * Set or clear the looper object.
607 * <p />
608 * Pass NULL to clear any previous looper.
reed@google.com9d07fec2011-03-16 20:02:59 +0000609 * If a previous looper exists in the paint, its reference count is
610 * decremented. If looper is not NULL, its reference count is
611 * incremented.
612 * @param looper May be NULL. The new looper to be installed in the paint.
reed@google.com9d07fec2011-03-16 20:02:59 +0000613 */
Cary Clark0418a882017-05-10 09:07:42 -0400614 void setDrawLooper(sk_sp<SkDrawLooper> drawLooper);
Mike Reed09d94352016-10-31 15:11:04 -0400615
Cary Clark0418a882017-05-10 09:07:42 -0400616 void setLooper(sk_sp<SkDrawLooper> drawLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000617
618 enum Align {
619 kLeft_Align,
620 kCenter_Align,
621 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000622 };
623 enum {
624 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000626
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 /** Return the paint's Align value for drawing text.
628 @return the paint's Align value for drawing text.
629 */
reedf59eab22014-07-14 14:39:15 -0700630 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000631
reed@android.com8a1c16f2008-12-17 15:59:43 +0000632 /** Set the paint's text alignment.
633 @param align set the paint's Align value for drawing text.
634 */
635 void setTextAlign(Align align);
636
637 /** Return the paint's text size.
638 @return the paint's text size.
639 */
640 SkScalar getTextSize() const { return fTextSize; }
641
642 /** Set the paint's text size. This value must be > 0
643 @param textSize set the paint's text size.
644 */
645 void setTextSize(SkScalar textSize);
646
647 /** Return the paint's horizontal scale factor for text. The default value
648 is 1.0.
649 @return the paint's scale factor in X for drawing/measuring text
650 */
651 SkScalar getTextScaleX() const { return fTextScaleX; }
652
653 /** Set the paint's horizontal scale factor for text. The default value
654 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
655 stretch the text narrower.
656 @param scaleX set the paint's scale factor in X for drawing/measuring
657 text.
658 */
659 void setTextScaleX(SkScalar scaleX);
660
661 /** Return the paint's horizontal skew factor for text. The default value
662 is 0.
663 @return the paint's skew factor in X for drawing text.
664 */
665 SkScalar getTextSkewX() const { return fTextSkewX; }
666
667 /** Set the paint's horizontal skew factor for text. The default value
668 is 0. For approximating oblique text, use values around -0.25.
669 @param skewX set the paint's skew factor in X for drawing text.
670 */
671 void setTextSkewX(SkScalar skewX);
672
673 /** Describes how to interpret the text parameters that are passed to paint
674 methods like measureText() and getTextWidths().
675 */
676 enum TextEncoding {
677 kUTF8_TextEncoding, //!< the text parameters are UTF8
678 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000679 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680 kGlyphID_TextEncoding //!< the text parameters are glyph indices
681 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000682
reedf59eab22014-07-14 14:39:15 -0700683 TextEncoding getTextEncoding() const {
684 return (TextEncoding)fBitfields.fTextEncoding;
685 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000686
687 void setTextEncoding(TextEncoding encoding);
688
689 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000690 /** Flags which indicate the confidence level of various metrics.
691 A set flag indicates that the metric may be trusted.
692 */
693 enum FontMetricsFlags {
Ben Wagner3318da52017-03-23 14:01:22 -0400694 kUnderlineThicknessIsValid_Flag = 1 << 0,
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000695 kUnderlinePositionIsValid_Flag = 1 << 1,
696 };
697
698 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000699 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
700 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
701 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
702 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
703 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000704 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
705 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000706 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
707 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000708 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
709 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000710 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
711
712 /** Underline Position - position of the top of the Underline stroke
713 relative to the baseline, this can have following values
714 - Negative - means underline should be drawn above baseline.
715 - Positive - means below baseline.
716 - Zero - mean underline should be drawn on baseline.
717 */
718 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
719
Ben Wagner3318da52017-03-23 14:01:22 -0400720 /** If the fontmetrics has a valid underline thickness, return true, and set the
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000721 thickness param to that value. If it doesn't return false and ignore the
722 thickness param.
723 */
724 bool hasUnderlineThickness(SkScalar* thickness) const {
Ben Wagner3318da52017-03-23 14:01:22 -0400725 if (SkToBool(fFlags & kUnderlineThicknessIsValid_Flag)) {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000726 *thickness = fUnderlineThickness;
727 return true;
728 }
729 return false;
730 }
731
Ben Wagner3318da52017-03-23 14:01:22 -0400732 /** If the fontmetrics has a valid underline position, return true, and set the
733 position param to that value. If it doesn't return false and ignore the
734 position param.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000735 */
736 bool hasUnderlinePosition(SkScalar* position) const {
737 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
738 *position = fUnderlinePosition;
739 return true;
740 }
741 return false;
742 }
743
reed@android.com8a1c16f2008-12-17 15:59:43 +0000744 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000745
reed@android.com8a1c16f2008-12-17 15:59:43 +0000746 /** Return the recommend spacing between lines (which will be
747 fDescent - fAscent + fLeading).
748 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000749 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750 @param metrics If not null, returns the font metrics for the
751 current typeface/pointsize/etc setting in this
752 paint.
753 @param scale If not 0, return width as if the canvas were scaled
754 by this value
755 @param return the recommended spacing between lines
756 */
757 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000758
reed@android.com8a1c16f2008-12-17 15:59:43 +0000759 /** Return the recommend line spacing. This will be
760 fDescent - fAscent + fLeading
761 */
762 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
763
764 /** Convert the specified text into glyph IDs, returning the number of
765 glyphs ID written. If glyphs is NULL, it is ignore and only the count
766 is returned.
767 */
768 int textToGlyphs(const void* text, size_t byteLength,
halcanaryd0e95a52016-07-25 07:18:12 -0700769 SkGlyphID glyphs[]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770
reed@android.coma5dcaf62010-02-05 17:12:32 +0000771 /** Return true if all of the specified text has a corresponding non-zero
772 glyph ID. If any of the code-points in the text are not supported in
773 the typeface (i.e. the glyph ID would be zero), then return false.
774
775 If the text encoding for the paint is kGlyph_TextEncoding, then this
776 returns true if all of the specified glyph IDs are non-zero.
777 */
778 bool containsText(const void* text, size_t byteLength) const;
779
reed@android.com9d3a9852010-01-08 14:07:42 +0000780 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
781 to zero. Note: this does not look at the text-encoding setting in the
782 paint, only at the typeface.
783 */
halcanaryd0e95a52016-07-25 07:18:12 -0700784 void glyphsToUnichars(const SkGlyphID glyphs[], int count, SkUnichar text[]) const;
reed@android.com9d3a9852010-01-08 14:07:42 +0000785
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 /** Return the number of drawable units in the specified text buffer.
787 This looks at the current TextEncoding field of the paint. If you also
788 want to have the text converted into glyph IDs, call textToGlyphs
789 instead.
790 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000791 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000792 return this->textToGlyphs(text, byteLength, NULL);
793 }
794
reed@google.com44da42e2011-11-10 20:04:47 +0000795 /** Return the width of the text. This will return the vertical measure
796 * if isVerticalText() is true, in which case the returned value should
797 * be treated has a height instead of a width.
798 *
799 * @param text The text to be measured
800 * @param length Number of bytes of text to measure
801 * @param bounds If not NULL, returns the bounds of the text,
802 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000803 * @return The advance width of the text
804 */
reed99ae8812014-08-26 11:30:01 -0700805 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000806
reed@google.com44da42e2011-11-10 20:04:47 +0000807 /** Return the width of the text. This will return the vertical measure
808 * if isVerticalText() is true, in which case the returned value should
809 * be treated has a height instead of a width.
810 *
811 * @param text Address of the text
812 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000813 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000814 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000815 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700816 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000818
reed@google.com44da42e2011-11-10 20:04:47 +0000819 /** Return the number of bytes of text that were measured. If
820 * isVerticalText() is true, then the vertical advances are used for
821 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000822 *
reed@google.com44da42e2011-11-10 20:04:47 +0000823 * @param text The text to be measured
824 * @param length Number of bytes of text to measure
825 * @param maxWidth Maximum width. Only the subset of text whose accumulated
826 * widths are <= maxWidth are measured.
827 * @param measuredWidth Optional. If non-null, this returns the actual
828 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000829 * @return The number of bytes of text that were measured. Will be
830 * <= length.
831 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000832 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700833 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000834
reed@google.com44da42e2011-11-10 20:04:47 +0000835 /** Return the advances for the text. These will be vertical advances if
836 * isVerticalText() returns true.
837 *
838 * @param text the text
839 * @param byteLength number of bytes to of text
840 * @param widths If not null, returns the array of advances for
841 * the glyphs. If not NULL, must be at least a large
842 * as the number of unichars in the specified text.
843 * @param bounds If not null, returns the bounds for each of
844 * character, relative to (0, 0)
845 * @return the number of unichars in the specified text.
846 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
848 SkRect bounds[] = NULL) const;
849
850 /** Return the path (outline) for the specified text.
caryclark0449bcf2016-02-09 13:25:45 -0800851 * Note: just like SkCanvas::drawText, this will respect the Align setting
852 * in the paint.
853 *
854 * @param text the text
855 * @param length number of bytes of text
856 * @param x The x-coordinate of the origin of the text.
857 * @param y The y-coordinate of the origin of the text.
858 * @param path The outline of the text.
859 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000860 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
861 SkPath* path) const;
862
caryclark0449bcf2016-02-09 13:25:45 -0800863 /** Return the path (outline) for the specified text.
864 * Note: just like SkCanvas::drawText, this will respect the Align setting
865 * in the paint.
866 *
867 * @param text the text
868 * @param length number of bytes of text
869 * @param pos array of positions, used to position each character
870 * @param path The outline of the text.
871 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000872 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000873 const SkPoint pos[], SkPath* path) const;
874
caryclark0449bcf2016-02-09 13:25:45 -0800875 /** Return the number of intervals that intersect the intercept along the axis of the advance.
876 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
877 * the string. The caller may pass nullptr for intervals to determine the size of the interval
878 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
879 * intervals are cached by glyph to improve performance for multiple calls.
880 * This permits constructing an underline that skips the descenders.
881 *
882 * @param text the text
883 * @param length number of bytes of text
884 * @param x The x-coordinate of the origin of the text.
885 * @param y The y-coordinate of the origin of the text.
886 * @param bounds The lower and upper line parallel to the advance.
887 * @param array If not null, the found intersections.
888 *
889 * @return The number of intersections, which may be zero.
890 */
891 int getTextIntercepts(const void* text, size_t length, SkScalar x, SkScalar y,
892 const SkScalar bounds[2], SkScalar* intervals) const;
893
894 /** Return the number of intervals that intersect the intercept along the axis of the advance.
895 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
896 * string. The caller may pass nullptr for intervals to determine the size of the interval
897 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
898 * intervals are cached by glyph to improve performance for multiple calls.
899 * This permits constructing an underline that skips the descenders.
900 *
901 * @param text the text
902 * @param length number of bytes of text
903 * @param pos array of positions, used to position each character
904 * @param bounds The lower and upper line parallel to the advance.
905 * @param array If not null, the glyph bounds contained by the advance parallel lines.
906 *
907 * @return The number of intersections, which may be zero.
908 */
909 int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[],
910 const SkScalar bounds[2], SkScalar* intervals) const;
911
fmalitaeae6a912016-07-28 09:47:24 -0700912 /** Return the number of intervals that intersect the intercept along the axis of the advance.
913 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
914 * string. The caller may pass nullptr for intervals to determine the size of the interval
915 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
916 * intervals are cached by glyph to improve performance for multiple calls.
917 * This permits constructing an underline that skips the descenders.
918 *
919 * @param text The text.
920 * @param length Number of bytes of text.
921 * @param xpos Array of x-positions, used to position each character.
922 * @param constY The shared Y coordinate for all of the positions.
923 * @param bounds The lower and upper line parallel to the advance.
924 * @param array If not null, the glyph bounds contained by the advance parallel lines.
925 *
926 * @return The number of intersections, which may be zero.
927 */
928 int getPosTextHIntercepts(const void* text, size_t length, const SkScalar xpos[],
929 SkScalar constY, const SkScalar bounds[2], SkScalar* intervals) const;
930
931 /** Return the number of intervals that intersect the intercept along the axis of the advance.
932 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
933 * text blob. The caller may pass nullptr for intervals to determine the size of the interval
934 * array. The computed intervals are cached by glyph to improve performance for multiple calls.
935 * This permits constructing an underline that skips the descenders.
936 *
937 * @param blob The text blob.
938 * @param bounds The lower and upper line parallel to the advance.
939 * @param array If not null, the glyph bounds contained by the advance parallel lines.
940 *
941 * @return The number of intersections, which may be zero.
942 */
943 int getTextBlobIntercepts(const SkTextBlob* blob, const SkScalar bounds[2],
944 SkScalar* intervals) const;
945
reed8893e5f2014-12-15 13:27:26 -0800946 /**
947 * Return a rectangle that represents the union of the bounds of all
948 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
949 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
950 * on this paint.
951 */
952 SkRect getFontBounds() const;
953
reed@google.com632e1a22011-10-06 12:37:00 +0000954 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
955 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
956 bool nothingToDraw() const;
957
reed@google.coma584aed2012-05-16 14:06:02 +0000958 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000959 // would prefer to make these private...
960
reed@google.coma584aed2012-05-16 14:06:02 +0000961 /** Returns true if the current paint settings allow for fast computation of
962 bounds (i.e. there is nothing complex like a patheffect that would make
963 the bounds computation expensive.
964 */
senorblanco0abdf762015-08-20 11:10:41 -0700965 bool canComputeFastBounds() const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000966
reed@google.coma584aed2012-05-16 14:06:02 +0000967 /** Only call this if canComputeFastBounds() returned true. This takes a
968 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
969 effects in the paint (e.g. stroking). If needed, it uses the storage
970 rect parameter. It returns the adjusted bounds that can then be used
971 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000972
reed@google.coma584aed2012-05-16 14:06:02 +0000973 The returned rect will either be orig or storage, thus the caller
974 should not rely on storage being set to the result, but should always
975 use the retured value. It is legal for orig and storage to be the same
976 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000977
reed@google.coma584aed2012-05-16 14:06:02 +0000978 e.g.
979 if (paint.canComputeFastBounds()) {
980 SkRect r, storage;
981 path.computeBounds(&r, SkPath::kFast_BoundsType);
982 const SkRect& fastR = paint.computeFastBounds(r, &storage);
983 if (canvas->quickReject(fastR, ...)) {
984 // don't draw the path
985 }
986 }
987 */
988 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
Brian Osman60751d72017-05-12 11:21:36 -0400989 // Things like stroking, etc... will do math on the bounds rect, assuming that it's sorted.
990 SkASSERT(orig.isSorted());
reed@google.coma584aed2012-05-16 14:06:02 +0000991 SkPaint::Style style = this->getStyle();
992 // ultra fast-case: filling with no effects that affect geometry
993 if (kFill_Style == style) {
994 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
995 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
996 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000997 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +0000998 if (!effects) {
999 return orig;
1000 }
1001 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001002
reed@google.coma584aed2012-05-16 14:06:02 +00001003 return this->doComputeFastBounds(orig, storage, style);
1004 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001005
reed@google.coma584aed2012-05-16 14:06:02 +00001006 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1007 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001008 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001009 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001010
reed@google.coma584aed2012-05-16 14:06:02 +00001011 // Take the style explicitly, so the caller can force us to be stroked
1012 // without having to make a copy of the paint just to change that field.
1013 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
Cary Clark0418a882017-05-10 09:07:42 -04001014 Style style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001015
Cary Clark0418a882017-05-10 09:07:42 -04001016
benjaminwagnerd936f632016-02-23 10:44:31 -08001017
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001018 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001019
reed@google.comd5f20792012-05-16 14:15:02 +00001020private:
Cary Clark0418a882017-05-10 09:07:42 -04001021 typedef const SkGlyph& (*GlyphCacheProc)(SkGlyphCache*, const char**);
1022
reeda5ab9ec2016-03-06 18:10:48 -08001023 sk_sp<SkTypeface> fTypeface;
1024 sk_sp<SkPathEffect> fPathEffect;
1025 sk_sp<SkShader> fShader;
reeda5ab9ec2016-03-06 18:10:48 -08001026 sk_sp<SkMaskFilter> fMaskFilter;
1027 sk_sp<SkColorFilter> fColorFilter;
1028 sk_sp<SkRasterizer> fRasterizer;
reed46f2d0a2016-09-11 05:40:31 -07001029 sk_sp<SkDrawLooper> fDrawLooper;
reeda5ab9ec2016-03-06 18:10:48 -08001030 sk_sp<SkImageFilter> fImageFilter;
reed@google.comd5f20792012-05-16 14:15:02 +00001031
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001032 SkScalar fTextSize;
1033 SkScalar fTextScaleX;
1034 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001035 SkColor fColor;
1036 SkScalar fWidth;
1037 SkScalar fMiterLimit;
Mike Reed71fecc32016-11-18 17:19:54 -05001038 uint32_t fBlendMode; // just need 5-6 bits
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001039 union {
1040 struct {
1041 // all of these bitfields should add up to 32
1042 unsigned fFlags : 16;
1043 unsigned fTextAlign : 2;
1044 unsigned fCapType : 2;
1045 unsigned fJoinType : 2;
1046 unsigned fStyle : 2;
1047 unsigned fTextEncoding : 2; // 3 values
1048 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001049 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001050 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001051 } fBitfields;
1052 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001053 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001054
robertphillipse34f17d2016-07-19 07:59:22 -07001055 static GlyphCacheProc GetGlyphCacheProc(TextEncoding encoding,
1056 bool isDevKern,
1057 bool needFullMetrics);
reed@google.comd5f20792012-05-16 14:15:02 +00001058
1059 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1060 int* count, SkRect* bounds) const;
1061
brianosmana1e8f8d2016-04-08 06:47:54 -07001062 enum ScalerContextFlags : uint32_t {
1063 kNone_ScalerContextFlags = 0,
1064
1065 kFakeGamma_ScalerContextFlag = 1 << 0,
1066 kBoostContrast_ScalerContextFlag = 1 << 1,
1067
1068 kFakeGammaAndBoostContrast_ScalerContextFlags =
1069 kFakeGamma_ScalerContextFlag | kBoostContrast_ScalerContextFlag,
bungemanf6d1e602016-02-22 13:20:28 -08001070 };
1071
joshualittfd450792015-03-13 08:38:43 -07001072 /*
1073 * Allocs an SkDescriptor on the heap and return it to the caller as a refcnted
1074 * SkData. Caller is responsible for managing the lifetime of this object.
1075 */
reeda9322c22016-04-12 06:47:05 -07001076 void getScalerContextDescriptor(SkScalerContextEffects*, SkAutoDescriptor*,
1077 const SkSurfaceProps& surfaceProps,
brianosmana1e8f8d2016-04-08 06:47:54 -07001078 uint32_t scalerContextFlags, const SkMatrix*) const;
joshualittfd450792015-03-13 08:38:43 -07001079
brianosmana1e8f8d2016-04-08 06:47:54 -07001080 SkGlyphCache* detachCache(const SkSurfaceProps* surfaceProps, uint32_t scalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -08001081 const SkMatrix*) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001082
brianosmana1e8f8d2016-04-08 06:47:54 -07001083 void descriptorProc(const SkSurfaceProps* surfaceProps, uint32_t scalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -08001084 const SkMatrix* deviceMatrix,
reeda9322c22016-04-12 06:47:05 -07001085 void (*proc)(SkTypeface*, const SkScalerContextEffects&,
1086 const SkDescriptor*, void*),
bungemanf6d1e602016-02-22 13:20:28 -08001087 void* context) const;
reed@android.comd252db02009-04-01 18:31:44 +00001088
joshualitt9e36c1a2015-04-14 12:17:27 -07001089 /*
1090 * The luminance color is used to determine which Gamma Canonical color to map to. This is
1091 * really only used by backends which want to cache glyph masks, and need some way to know if
1092 * they need to generate new masks based off a given color.
1093 */
1094 SkColor computeLuminanceColor() const;
1095
reed@android.com8a1c16f2008-12-17 15:59:43 +00001096 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001097 /* This is the size we use when we ask for a glyph's path. We then
1098 * post-transform it as we draw to match the request.
1099 * This is done to try to re-use cache entries for the path.
1100 *
1101 * This value is somewhat arbitrary. In theory, it could be 1, since
1102 * we store paths as floats. However, we get the path from the font
1103 * scaler, and it may represent its paths as fixed-point (or 26.6),
1104 * so we shouldn't ask for something too big (might overflow 16.16)
1105 * or too small (underflow 26.6).
1106 *
1107 * This value could track kMaxSizeForGlyphCache, assuming the above
1108 * constraints, but since we ask for unhinted paths, the two values
1109 * need not match per-se.
1110 */
1111 kCanonicalTextSizeForPaths = 64,
1112
1113 /*
1114 * Above this size (taking into account CTM and textSize), we never use
1115 * the cache for bits or metrics (we might overflow), so we just ask
1116 * for a caononical size and post-transform that.
1117 */
1118 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001119 };
reed@google.comed43dff2013-06-04 16:56:27 +00001120
1121 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1122
reed@google.comed43dff2013-06-04 16:56:27 +00001123 // Set flags/hinting/textSize up to use for drawing text as paths.
1124 // Returns scale factor to restore the original textSize, since will will
1125 // have change it to kCanonicalTextSizeForPaths.
1126 SkScalar setupForAsPaths();
1127
1128 static SkScalar MaxCacheSize2() {
1129 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1130 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1131 return kMag2Max;
1132 }
1133
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001134 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001135 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136 friend class SkCanvas;
1137 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001138 friend class SkPDFDevice;
joshualitte76b4bb2015-12-28 07:23:58 -08001139 friend class GrAtlasTextBlob;
joshualittdbd35932015-04-02 09:19:04 -07001140 friend class GrAtlasTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001141 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001142 friend class GrPathRendering;
joshualitt0a42e682015-12-10 13:20:58 -08001143 friend class GrTextUtils;
cdalton855d83f2014-09-18 13:51:53 -07001144 friend class GrGLPathRendering;
joshualitt9e36c1a2015-04-14 12:17:27 -07001145 friend class SkScalerContext;
caryclark0449bcf2016-02-09 13:25:45 -08001146 friend class SkTextBaseIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001147 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148};
1149
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150#endif