blob: f2864c65064f0f4c4a880f821cfd08130f5ef073 [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 */
Cary Clark0e616cf2017-05-18 15:27:57 -040059 // cc_unittests requires SK_API to make operator== visible
robertphillips@google.comb2657412013-08-07 22:36:29 +000060 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
61 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
62 return !(a == b);
63 }
64
mtkleinfb1fe4f2014-10-07 09:26:10 -070065 /** getHash() is a shallow hash, with the same limitations as operator==.
66 * If operator== returns true for two paints, getHash() returns the same value for each.
67 */
68 uint32_t getHash() const;
69
Cary Clark0418a882017-05-10 09:07:42 -040070 void flatten(SkWriteBuffer& buffer) const;
71 void unflatten(SkReadBuffer& buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
73 /** Restores the paint to its initial settings.
74 */
75 void reset();
76
agl@chromium.org309485b2009-07-21 17:41:32 +000077 /** Specifies the level of hinting to be performed. These names are taken
78 from the Gnome/Cairo names for the same. They are translated into
79 Freetype concepts the same as in cairo-ft-font.c:
80 kNo_Hinting -> FT_LOAD_NO_HINTING
81 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
82 kNormal_Hinting -> <default, no option>
83 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
84 subpixel glyphs, in which case TARGET_LCD or
85 TARGET_LCD_V is used>
86 */
87 enum Hinting {
88 kNo_Hinting = 0,
89 kSlight_Hinting = 1,
90 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000091 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000092 };
93
reed@google.com9d07fec2011-03-16 20:02:59 +000094 Hinting getHinting() const {
reedf59eab22014-07-14 14:39:15 -070095 return static_cast<Hinting>(fBitfields.fHinting);
agl@chromium.org309485b2009-07-21 17:41:32 +000096 }
97
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +000098 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 /** Specifies the bit values that are stored in the paint's flags.
101 */
102 enum Flags {
103 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
Mike Reed44353112017-05-09 12:20:02 -0400104 kDither_Flag = 0x04, //!< mask to enable dithering. see setDither()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
106 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000107 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000109 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000110 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000111 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000112 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000113 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
agl@chromium.org309485b2009-07-21 17:41:32 +0000114 // when adding extra flags, note that the fFlags member is specified
115 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116
Mike Reedbcfb8f62017-02-23 14:19:35 +0000117 kAllFlags = 0xFFFF,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 };
119
Mike Reedddbd37e2017-02-21 15:07:44 -0500120#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
121 enum ReserveFlags {
122 // These are not used by paint, but the bits are reserved for private use by the
123 // android framework.
124 kUnderlineText_ReserveFlag = 0x08, //!< mask to enable underline text
125 kStrikeThruText_ReserveFlag = 0x10, //!< mask to enable strike-thru text
126 };
127#endif
128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 /** Return the paint's flags. Use the Flag enum to test flag values.
130 @return the paint's flags (see enums ending in _Flag for bit masks)
131 */
reedf59eab22014-07-14 14:39:15 -0700132 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133
134 /** Set the paint's flags. Use the Flag enum to specific flag values.
135 @param flags The new flag bits for the paint (see Flags enum)
136 */
137 void setFlags(uint32_t flags);
138
139 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
140 @return true if the antialias bit is set in the paint's flags.
141 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000142 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 return SkToBool(this->getFlags() & kAntiAlias_Flag);
144 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
147 @param aa true to enable antialiasing, false to disable it
148 */
149 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 /** Helper for getFlags(), returning true if kDither_Flag bit is set
152 @return true if the dithering bit is set in the paint's flags.
153 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000154 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 return SkToBool(this->getFlags() & kDither_Flag);
156 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000157
Mike Reed44353112017-05-09 12:20:02 -0400158 /**
159 * Helper for setFlags(), setting or clearing the kDither_Flag bit
160 * @param dither true to enable dithering, false to disable it
161 *
162 * Note: gradients ignore this setting and always dither.
163 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
167 @return true if the lineartext bit is set in the paint's flags
168 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000169 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 return SkToBool(this->getFlags() & kLinearText_Flag);
171 }
172
173 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
174 @param linearText true to set the linearText bit in the paint's flags,
175 false to clear it.
176 */
177 void setLinearText(bool linearText);
178
179 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
180 @return true if the lineartext bit is set in the paint's flags
181 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000182 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 return SkToBool(this->getFlags() & kSubpixelText_Flag);
184 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000185
reed@google.com84b437e2011-08-01 12:45:35 +0000186 /**
187 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
188 * @param subpixelText true to set the subpixelText bit in the paint's
189 * flags, false to clear it.
190 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000192
reed@google.com9d07fec2011-03-16 20:02:59 +0000193 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000194 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
195 }
196
reed@google.com84b437e2011-08-01 12:45:35 +0000197 /**
198 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
199 * Note: antialiasing must also be on for lcd rendering
200 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
201 * false to clear it.
202 */
203 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000204
reed@google.com9d07fec2011-03-16 20:02:59 +0000205 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000206 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
207 }
208
209 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
210 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
211 false to clear it.
212 */
213 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
214
reed@google.com9d07fec2011-03-16 20:02:59 +0000215 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000216 return SkToBool(this->getFlags() & kAutoHinting_Flag);
217 }
218
219 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
220 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
221 paint's flags,
222 false to clear it.
223 */
224 void setAutohinted(bool useAutohinter);
225
reed@google.com830a23e2011-11-10 15:20:49 +0000226 bool isVerticalText() const {
227 return SkToBool(this->getFlags() & kVerticalText_Flag);
228 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000229
reed@google.com830a23e2011-11-10 15:20:49 +0000230 /**
231 * Helper for setting or clearing the kVerticalText_Flag bit in
232 * setFlags(...).
233 *
234 * If this bit is set, then advances are treated as Y values rather than
235 * X values, and drawText will places its glyphs vertically rather than
236 * horizontally.
237 */
Cary Clark0418a882017-05-10 09:07:42 -0400238 void setVerticalText(bool verticalText);
reed@google.com830a23e2011-11-10 15:20:49 +0000239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
241 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
242 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000243 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
245 }
246
247 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
248 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
249 flags, false to clear it.
250 */
251 void setFakeBoldText(bool fakeBoldText);
252
253 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
254 @return true if the kernText bit is set in the paint's flags.
255 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000256 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 return SkToBool(this->getFlags() & kDevKernText_Flag);
258 }
259
260 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
261 @param kernText true to set the kKernText_Flag bit in the paint's
262 flags, false to clear it.
263 */
264 void setDevKernText(bool devKernText);
265
reedf803da12015-01-23 05:58:07 -0800266 /**
267 * Return the filter level. This affects the quality (and performance) of
268 * drawing scaled images.
269 */
270 SkFilterQuality getFilterQuality() const {
271 return (SkFilterQuality)fBitfields.fFilterQuality;
272 }
mtkleinfe81e2d2015-09-09 07:35:42 -0700273
reedf803da12015-01-23 05:58:07 -0800274 /**
275 * Set the filter quality. This affects the quality (and performance) of
276 * drawing scaled images.
277 */
278 void setFilterQuality(SkFilterQuality quality);
reed@google.comc9683152013-07-18 13:47:01 +0000279
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 /** Styles apply to rect, oval, path, and text.
281 Bitmaps are always drawn in "fill", and lines are always drawn in
282 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000283
reed@android.comed881c22009-09-15 14:10:42 +0000284 Note: strokeandfill implicitly draws the result with
285 SkPath::kWinding_FillType, so if the original path is even-odd, the
286 results may not appear the same as if it was drawn twice, filled and
287 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 */
289 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000290 kFill_Style, //!< fill the geometry
291 kStroke_Style, //!< stroke the geometry
292 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000293 };
294 enum {
295 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 };
297
298 /** Return the paint's style, used for controlling how primitives'
299 geometries are interpreted (except for drawBitmap, which always assumes
300 kFill_Style).
301 @return the paint's Style
302 */
reedf59eab22014-07-14 14:39:15 -0700303 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304
305 /** Set the paint's style, used for controlling how primitives'
306 geometries are interpreted (except for drawBitmap, which always assumes
307 Fill).
308 @param style The new style to set in the paint
309 */
310 void setStyle(Style style);
311
312 /** Return the paint's color. Note that the color is a 32bit value
313 containing alpha as well as r,g,b. This 32bit value is not
314 premultiplied, meaning that its alpha can be any value, regardless of
315 the values of r,g,b.
316 @return the paint's color (and alpha).
317 */
318 SkColor getColor() const { return fColor; }
319
320 /** Set the paint's color. Note that the color is a 32bit value containing
321 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
322 that its alpha can be any value, regardless of the values of r,g,b.
323 @param color The new color (including alpha) to set in the paint.
324 */
325 void setColor(SkColor color);
326
327 /** Helper to getColor() that just returns the color's alpha value.
328 @return the alpha component of the paint's color.
329 */
330 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000331
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 /** Helper to setColor(), that only assigns the color's alpha value,
333 leaving its r,g,b values unchanged.
334 @param a set the alpha component (0..255) of the paint's color.
335 */
336 void setAlpha(U8CPU a);
337
338 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
339 using SkColorSetARGB()
340 @param a The new alpha component (0..255) of the paint's color.
341 @param r The new red component (0..255) of the paint's color.
342 @param g The new green component (0..255) of the paint's color.
343 @param b The new blue component (0..255) of the paint's color.
344 */
345 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
346
reed@google.com9d07fec2011-03-16 20:02:59 +0000347 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 <p />
349 A value of 0 strokes in hairline mode.
350 Hairlines always draw 1-pixel wide, regardless of the matrix.
351 @return the paint's stroke width, used whenever the paint's style is
352 Stroke or StrokeAndFill.
353 */
354 SkScalar getStrokeWidth() const { return fWidth; }
355
reed@google.com9d07fec2011-03-16 20:02:59 +0000356 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357 Pass 0 to stroke in hairline mode.
358 Hairlines always draw 1-pixel wide, regardless of the matrix.
359 @param width set the paint's stroke width, used whenever the paint's
360 style is Stroke or StrokeAndFill.
361 */
362 void setStrokeWidth(SkScalar width);
363
364 /** Return the paint's stroke miter value. This is used to control the
365 behavior of miter joins when the joins angle is sharp.
366 @return the paint's miter limit, used whenever the paint's style is
367 Stroke or StrokeAndFill.
368 */
369 SkScalar getStrokeMiter() const { return fMiterLimit; }
370
371 /** Set the paint's stroke miter value. This is used to control the
372 behavior of miter joins when the joins angle is sharp. This value must
373 be >= 0.
374 @param miter set the miter limit on the paint, used whenever the
375 paint's style is Stroke or StrokeAndFill.
376 */
377 void setStrokeMiter(SkScalar miter);
378
379 /** Cap enum specifies the settings for the paint's strokecap. This is the
380 treatment that is applied to the beginning and end of each non-closed
381 contour (e.g. lines).
caryclark5cb00a92015-08-26 09:04:55 -0700382
383 If the cap is round or square, the caps are drawn when the contour has
384 a zero length. Zero length contours can be created by following moveTo
385 with a lineTo at the same point, or a moveTo followed by a close.
386
387 A dash with an on interval of zero also creates a zero length contour.
388
389 The zero length contour draws the square cap without rotation, since
390 the no direction can be inferred.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391 */
392 enum Cap {
393 kButt_Cap, //!< begin/end contours with no extension
394 kRound_Cap, //!< begin/end contours with a semi-circle extension
395 kSquare_Cap, //!< begin/end contours with a half square extension
396
bsalomona7d85ba2016-07-06 11:54:59 -0700397 kLast_Cap = kSquare_Cap,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 kDefault_Cap = kButt_Cap
399 };
bsalomona7d85ba2016-07-06 11:54:59 -0700400 static constexpr int kCapCount = kLast_Cap + 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401
402 /** Join enum specifies the settings for the paint's strokejoin. This is
403 the treatment that is applied to corners in paths and rectangles.
404 */
405 enum Join {
406 kMiter_Join, //!< connect path segments with a sharp join
407 kRound_Join, //!< connect path segments with a round join
408 kBevel_Join, //!< connect path segments with a flat bevel join
409
bsalomona7d85ba2016-07-06 11:54:59 -0700410 kLast_Join = kBevel_Join,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000411 kDefault_Join = kMiter_Join
412 };
bsalomona7d85ba2016-07-06 11:54:59 -0700413 static constexpr int kJoinCount = kLast_Join + 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414
415 /** Return the paint's stroke cap type, controlling how the start and end
416 of stroked lines and paths are treated.
417 @return the line cap style for the paint, used whenever the paint's
418 style is Stroke or StrokeAndFill.
419 */
reedf59eab22014-07-14 14:39:15 -0700420 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000421
422 /** Set the paint's stroke cap type.
423 @param cap set the paint's line cap style, used whenever the paint's
424 style is Stroke or StrokeAndFill.
425 */
426 void setStrokeCap(Cap cap);
427
428 /** Return the paint's stroke join type.
429 @return the paint's line join style, used whenever the paint's style is
430 Stroke or StrokeAndFill.
431 */
reedf59eab22014-07-14 14:39:15 -0700432 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433
434 /** Set the paint's stroke join type.
435 @param join set the paint's line join style, used whenever the paint's
436 style is Stroke or StrokeAndFill.
437 */
438 void setStrokeJoin(Join join);
439
reed@google.com4bbdeac2013-01-24 21:03:11 +0000440 /**
441 * Applies any/all effects (patheffect, stroking) to src, returning the
442 * result in dst. The result is that drawing src with this paint will be
443 * the same as drawing dst with a default paint (at least from the
444 * geometric perspective).
445 *
446 * @param src input path
447 * @param dst output path (may be the same as src)
448 * @param cullRect If not null, the dst path may be culled to this rect.
reed05d90442015-02-12 13:35:52 -0800449 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision
450 * in favor of speed/size.
reed@google.com4bbdeac2013-01-24 21:03:11 +0000451 * @return true if the path should be filled, or false if it should be
452 * drawn with a hairline (width == 0)
453 */
reed05d90442015-02-12 13:35:52 -0800454 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
455 SkScalar resScale = 1) const;
456
457 bool getFillPath(const SkPath& src, SkPath* dst) const {
458 return this->getFillPath(src, dst, NULL, 1);
459 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 /** Get the paint's shader object.
462 <p />
463 The shader's reference count is not affected.
464 @return the paint's shader (or NULL)
465 */
reeda5ab9ec2016-03-06 18:10:48 -0800466 SkShader* getShader() const { return fShader.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500467 sk_sp<SkShader> refShader() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468
469 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000470 * Shaders specify the source color(s) for what is being drawn. If a paint
471 * has no shader, then the paint's color is used. If the paint has a
472 * shader, then the shader's color(s) are use instead, but they are
473 * modulated by the paint's alpha. This makes it easy to create a shader
474 * once (e.g. bitmap tiling or gradient) and then change its transparency
475 * w/o having to modify the original shader... only the paint's alpha needs
476 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000477 *
478 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
479 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
480 * then the shader will use the paint's entire color to "colorize" its output (modulating the
481 * bitmap's alpha with the paint's color+alpha).
482 *
reed@google.com880dc472012-05-11 14:47:03 +0000483 * Pass NULL to clear any previous shader.
484 * As a convenience, the parameter passed is also returned.
485 * If a previous shader exists, its reference count is decremented.
486 * If shader is not NULL, its reference count is incremented.
487 * @param shader May be NULL. The shader to be installed in the paint
reed@google.com880dc472012-05-11 14:47:03 +0000488 */
Cary Clark0418a882017-05-10 09:07:42 -0400489 void setShader(sk_sp<SkShader> shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000490
reed@android.com8a1c16f2008-12-17 15:59:43 +0000491 /** Get the paint's colorfilter. If there is a colorfilter, its reference
492 count is not changed.
493 @return the paint's colorfilter (or NULL)
494 */
reeda5ab9ec2016-03-06 18:10:48 -0800495 SkColorFilter* getColorFilter() const { return fColorFilter.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500496 sk_sp<SkColorFilter> refColorFilter() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000497
Mike Reed958788a2016-10-20 16:40:26 -0400498 /** Set or clear the paint's colorfilter.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499 <p />
500 If the paint already has a filter, its reference count is decremented.
501 If filter is not NULL, its reference count is incremented.
502 @param filter May be NULL. The filter to be installed in the paint
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503 */
Cary Clark0418a882017-05-10 09:07:42 -0400504 void setColorFilter(sk_sp<SkColorFilter> colorFilter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000505
reed374772b2016-10-05 17:33:02 -0700506 SkBlendMode getBlendMode() const { return (SkBlendMode)fBlendMode; }
507 bool isSrcOver() const { return (SkBlendMode)fBlendMode == SkBlendMode::kSrcOver; }
508 void setBlendMode(SkBlendMode mode) { fBlendMode = (unsigned)mode; }
reed@android.coma0f5d152009-06-22 17:38:10 +0000509
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 /** Get the paint's patheffect object.
511 <p />
512 The patheffect reference count is not affected.
513 @return the paint's patheffect (or NULL)
514 */
reeda5ab9ec2016-03-06 18:10:48 -0800515 SkPathEffect* getPathEffect() const { return fPathEffect.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500516 sk_sp<SkPathEffect> refPathEffect() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517
518 /** Set or clear the patheffect object.
519 <p />
520 Pass NULL to clear any previous patheffect.
521 As a convenience, the parameter passed is also returned.
522 If a previous patheffect exists, its reference count is decremented.
523 If patheffect is not NULL, its reference count is incremented.
524 @param effect May be NULL. The new patheffect to be installed in the
525 paint
526 @return effect
527 */
Cary Clark0418a882017-05-10 09:07:42 -0400528 void setPathEffect(sk_sp<SkPathEffect> pathEffect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529
530 /** Get the paint's maskfilter object.
531 <p />
532 The maskfilter reference count is not affected.
533 @return the paint's maskfilter (or NULL)
534 */
reeda5ab9ec2016-03-06 18:10:48 -0800535 SkMaskFilter* getMaskFilter() const { return fMaskFilter.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500536 sk_sp<SkMaskFilter> refMaskFilter() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537
538 /** Set or clear the maskfilter object.
539 <p />
540 Pass NULL to clear any previous maskfilter.
541 As a convenience, the parameter passed is also returned.
542 If a previous maskfilter exists, its reference count is decremented.
543 If maskfilter is not NULL, its reference count is incremented.
544 @param maskfilter May be NULL. The new maskfilter to be installed in
545 the paint
546 @return maskfilter
547 */
Cary Clark0418a882017-05-10 09:07:42 -0400548 void setMaskFilter(sk_sp<SkMaskFilter> maskFilter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000549
550 // These attributes are for text/fonts
551
552 /** Get the paint's typeface object.
553 <p />
554 The typeface object identifies which font to use when drawing or
555 measuring text. The typeface reference count is not affected.
556 @return the paint's typeface (or NULL)
557 */
reeda5ab9ec2016-03-06 18:10:48 -0800558 SkTypeface* getTypeface() const { return fTypeface.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500559 sk_sp<SkTypeface> refTypeface() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560
561 /** Set or clear the typeface object.
562 <p />
563 Pass NULL to clear any previous typeface.
564 As a convenience, the parameter passed is also returned.
565 If a previous typeface exists, its reference count is decremented.
566 If typeface is not NULL, its reference count is incremented.
567 @param typeface May be NULL. The new typeface to be installed in the
568 paint
569 @return typeface
570 */
Cary Clark0418a882017-05-10 09:07:42 -0400571 void setTypeface(sk_sp<SkTypeface> typeface);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000572
573 /** Get the paint's rasterizer (or NULL).
574 <p />
575 The raster controls how paths/text are turned into alpha masks.
576 @return the paint's rasterizer (or NULL)
577 */
reeda5ab9ec2016-03-06 18:10:48 -0800578 SkRasterizer* getRasterizer() const { return fRasterizer.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500579 sk_sp<SkRasterizer> refRasterizer() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000580
581 /** Set or clear the rasterizer object.
582 <p />
583 Pass NULL to clear any previous rasterizer.
584 As a convenience, the parameter passed is also returned.
585 If a previous rasterizer exists in the paint, its reference count is
586 decremented. If rasterizer is not NULL, its reference count is
587 incremented.
588 @param rasterizer May be NULL. The new rasterizer to be installed in
589 the paint.
590 @return rasterizer
591 */
Cary Clark0418a882017-05-10 09:07:42 -0400592 void setRasterizer(sk_sp<SkRasterizer> rasterizer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000593
reeda5ab9ec2016-03-06 18:10:48 -0800594 SkImageFilter* getImageFilter() const { return fImageFilter.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500595 sk_sp<SkImageFilter> refImageFilter() const;
Cary Clark0418a882017-05-10 09:07:42 -0400596 void setImageFilter(sk_sp<SkImageFilter> imageFilter);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000597
reed@google.comb0a34d82012-07-11 19:57:55 +0000598 /**
reed@google.com9d07fec2011-03-16 20:02:59 +0000599 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
600 * reference count.
601 */
reed46f2d0a2016-09-11 05:40:31 -0700602 SkDrawLooper* getDrawLooper() const { return fDrawLooper.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500603 sk_sp<SkDrawLooper> refDrawLooper() const;
604
reed46f2d0a2016-09-11 05:40:31 -0700605 SkDrawLooper* getLooper() const { return fDrawLooper.get(); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000606 /**
607 * Set or clear the looper object.
608 * <p />
609 * Pass NULL to clear any previous looper.
reed@google.com9d07fec2011-03-16 20:02:59 +0000610 * If a previous looper exists in the paint, its reference count is
611 * decremented. If looper is not NULL, its reference count is
612 * incremented.
613 * @param looper May be NULL. The new looper to be installed in the paint.
reed@google.com9d07fec2011-03-16 20:02:59 +0000614 */
Cary Clark0418a882017-05-10 09:07:42 -0400615 void setDrawLooper(sk_sp<SkDrawLooper> drawLooper);
Mike Reed09d94352016-10-31 15:11:04 -0400616
Cary Clark0418a882017-05-10 09:07:42 -0400617 void setLooper(sk_sp<SkDrawLooper> drawLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000618
619 enum Align {
620 kLeft_Align,
621 kCenter_Align,
622 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000623 };
624 enum {
625 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000627
reed@android.com8a1c16f2008-12-17 15:59:43 +0000628 /** Return the paint's Align value for drawing text.
629 @return the paint's Align value for drawing text.
630 */
reedf59eab22014-07-14 14:39:15 -0700631 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000632
reed@android.com8a1c16f2008-12-17 15:59:43 +0000633 /** Set the paint's text alignment.
634 @param align set the paint's Align value for drawing text.
635 */
636 void setTextAlign(Align align);
637
638 /** Return the paint's text size.
639 @return the paint's text size.
640 */
641 SkScalar getTextSize() const { return fTextSize; }
642
643 /** Set the paint's text size. This value must be > 0
644 @param textSize set the paint's text size.
645 */
646 void setTextSize(SkScalar textSize);
647
648 /** Return the paint's horizontal scale factor for text. The default value
649 is 1.0.
650 @return the paint's scale factor in X for drawing/measuring text
651 */
652 SkScalar getTextScaleX() const { return fTextScaleX; }
653
654 /** Set the paint's horizontal scale factor for text. The default value
655 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
656 stretch the text narrower.
657 @param scaleX set the paint's scale factor in X for drawing/measuring
658 text.
659 */
660 void setTextScaleX(SkScalar scaleX);
661
662 /** Return the paint's horizontal skew factor for text. The default value
663 is 0.
664 @return the paint's skew factor in X for drawing text.
665 */
666 SkScalar getTextSkewX() const { return fTextSkewX; }
667
668 /** Set the paint's horizontal skew factor for text. The default value
669 is 0. For approximating oblique text, use values around -0.25.
670 @param skewX set the paint's skew factor in X for drawing text.
671 */
672 void setTextSkewX(SkScalar skewX);
673
674 /** Describes how to interpret the text parameters that are passed to paint
675 methods like measureText() and getTextWidths().
676 */
677 enum TextEncoding {
678 kUTF8_TextEncoding, //!< the text parameters are UTF8
679 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000680 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000681 kGlyphID_TextEncoding //!< the text parameters are glyph indices
682 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000683
reedf59eab22014-07-14 14:39:15 -0700684 TextEncoding getTextEncoding() const {
685 return (TextEncoding)fBitfields.fTextEncoding;
686 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687
688 void setTextEncoding(TextEncoding encoding);
689
690 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000691 /** Flags which indicate the confidence level of various metrics.
692 A set flag indicates that the metric may be trusted.
693 */
694 enum FontMetricsFlags {
Ben Wagner3318da52017-03-23 14:01:22 -0400695 kUnderlineThicknessIsValid_Flag = 1 << 0,
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000696 kUnderlinePositionIsValid_Flag = 1 << 1,
Ben Wagner219f3622017-07-17 15:32:25 -0400697 kStrikeoutThicknessIsValid_Flag = 1 << 2,
698 kStrikeoutPositionIsValid_Flag = 1 << 3,
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000699 };
700
701 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000702 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
703 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
704 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
705 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
706 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000707 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
708 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000709 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
710 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000711 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
712 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000713 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
714
Ben Wagner219f3622017-07-17 15:32:25 -0400715 /** Position of the top of the underline stroke relative to the baseline.
716 * A positive value means draw below the baseline; negative values above.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000717 */
Ben Wagner219f3622017-07-17 15:32:25 -0400718 SkScalar fUnderlinePosition;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000719
Ben Wagner219f3622017-07-17 15:32:25 -0400720 SkScalar fStrikeoutThickness; //!< strikeout thickness, or 0 if cannot be determined
721
722 /** Position of the bottom of the strikeout stroke relative to the baseline.
723 * A positive value means draw below the baseline; negative values above.
724 */
725 SkScalar fStrikeoutPosition;
726
727 /** If the underline thickness is valid return true and set thickness.
728 * Otherwise return false and ignore the thickness param.
729 */
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000730 bool hasUnderlineThickness(SkScalar* thickness) const {
Ben Wagner3318da52017-03-23 14:01:22 -0400731 if (SkToBool(fFlags & kUnderlineThicknessIsValid_Flag)) {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000732 *thickness = fUnderlineThickness;
733 return true;
734 }
735 return false;
736 }
737
Ben Wagner219f3622017-07-17 15:32:25 -0400738 /** If the underline position is valid return true and set position.
739 * Otherwise return false and ignore the position param.
740 */
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000741 bool hasUnderlinePosition(SkScalar* position) const {
742 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
743 *position = fUnderlinePosition;
744 return true;
745 }
746 return false;
747 }
748
Ben Wagner219f3622017-07-17 15:32:25 -0400749 /** If the strikeout thickness is valid return true and set thickness.
750 * Otherwise return false and ignore the thickness param.
751 */
752 bool hasStrikeoutThickness(SkScalar* thickness) const {
753 if (SkToBool(fFlags & kStrikeoutThicknessIsValid_Flag)) {
754 *thickness = fStrikeoutThickness;
755 return true;
756 }
757 return false;
758 }
759
760 /** If the strikeout position is valid return true and set position.
761 * Otherwise return false and ignore the position param.
762 */
763 bool hasStrikeoutPosition(SkScalar* position) const {
764 if (SkToBool(fFlags & kStrikeoutPositionIsValid_Flag)) {
765 *position = fStrikeoutPosition;
766 return true;
767 }
768 return false;
769 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000771
reed@android.com8a1c16f2008-12-17 15:59:43 +0000772 /** Return the recommend spacing between lines (which will be
773 fDescent - fAscent + fLeading).
774 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000775 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000776 @param metrics If not null, returns the font metrics for the
777 current typeface/pointsize/etc setting in this
778 paint.
779 @param scale If not 0, return width as if the canvas were scaled
780 by this value
781 @param return the recommended spacing between lines
782 */
783 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000784
reed@android.com8a1c16f2008-12-17 15:59:43 +0000785 /** Return the recommend line spacing. This will be
786 fDescent - fAscent + fLeading
787 */
788 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
789
790 /** Convert the specified text into glyph IDs, returning the number of
791 glyphs ID written. If glyphs is NULL, it is ignore and only the count
792 is returned.
793 */
794 int textToGlyphs(const void* text, size_t byteLength,
halcanaryd0e95a52016-07-25 07:18:12 -0700795 SkGlyphID glyphs[]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000796
reed@android.coma5dcaf62010-02-05 17:12:32 +0000797 /** Return true if all of the specified text has a corresponding non-zero
798 glyph ID. If any of the code-points in the text are not supported in
799 the typeface (i.e. the glyph ID would be zero), then return false.
800
801 If the text encoding for the paint is kGlyph_TextEncoding, then this
802 returns true if all of the specified glyph IDs are non-zero.
803 */
804 bool containsText(const void* text, size_t byteLength) const;
805
reed@android.com9d3a9852010-01-08 14:07:42 +0000806 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
807 to zero. Note: this does not look at the text-encoding setting in the
808 paint, only at the typeface.
809 */
halcanaryd0e95a52016-07-25 07:18:12 -0700810 void glyphsToUnichars(const SkGlyphID glyphs[], int count, SkUnichar text[]) const;
reed@android.com9d3a9852010-01-08 14:07:42 +0000811
reed@android.com8a1c16f2008-12-17 15:59:43 +0000812 /** Return the number of drawable units in the specified text buffer.
813 This looks at the current TextEncoding field of the paint. If you also
814 want to have the text converted into glyph IDs, call textToGlyphs
815 instead.
816 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000817 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 return this->textToGlyphs(text, byteLength, NULL);
819 }
820
reed@google.com44da42e2011-11-10 20:04:47 +0000821 /** Return the width of the text. This will return the vertical measure
822 * if isVerticalText() is true, in which case the returned value should
823 * be treated has a height instead of a width.
824 *
825 * @param text The text to be measured
826 * @param length Number of bytes of text to measure
827 * @param bounds If not NULL, returns the bounds of the text,
828 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000829 * @return The advance width of the text
830 */
reed99ae8812014-08-26 11:30:01 -0700831 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000832
reed@google.com44da42e2011-11-10 20:04:47 +0000833 /** Return the width of the text. This will return the vertical measure
834 * if isVerticalText() is true, in which case the returned value should
835 * be treated has a height instead of a width.
836 *
837 * @param text Address of the text
838 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000839 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000840 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000841 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700842 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000843 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000844
reed@google.com44da42e2011-11-10 20:04:47 +0000845 /** Return the number of bytes of text that were measured. If
846 * isVerticalText() is true, then the vertical advances are used for
847 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000848 *
reed@google.com44da42e2011-11-10 20:04:47 +0000849 * @param text The text to be measured
850 * @param length Number of bytes of text to measure
851 * @param maxWidth Maximum width. Only the subset of text whose accumulated
852 * widths are <= maxWidth are measured.
853 * @param measuredWidth Optional. If non-null, this returns the actual
854 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000855 * @return The number of bytes of text that were measured. Will be
856 * <= length.
857 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000858 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700859 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000860
reed@google.com44da42e2011-11-10 20:04:47 +0000861 /** Return the advances for the text. These will be vertical advances if
862 * isVerticalText() returns true.
863 *
864 * @param text the text
865 * @param byteLength number of bytes to of text
866 * @param widths If not null, returns the array of advances for
867 * the glyphs. If not NULL, must be at least a large
868 * as the number of unichars in the specified text.
869 * @param bounds If not null, returns the bounds for each of
870 * character, relative to (0, 0)
871 * @return the number of unichars in the specified text.
872 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000873 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
874 SkRect bounds[] = NULL) const;
875
876 /** Return the path (outline) for the specified text.
caryclark0449bcf2016-02-09 13:25:45 -0800877 * Note: just like SkCanvas::drawText, this will respect the Align setting
878 * in the paint.
879 *
880 * @param text the text
881 * @param length number of bytes of text
882 * @param x The x-coordinate of the origin of the text.
883 * @param y The y-coordinate of the origin of the text.
884 * @param path The outline of the text.
885 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000886 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
887 SkPath* path) const;
888
caryclark0449bcf2016-02-09 13:25:45 -0800889 /** Return the path (outline) for the specified text.
890 * Note: just like SkCanvas::drawText, this will respect the Align setting
891 * in the paint.
892 *
893 * @param text the text
894 * @param length number of bytes of text
895 * @param pos array of positions, used to position each character
896 * @param path The outline of the text.
897 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000898 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000899 const SkPoint pos[], SkPath* path) const;
900
caryclark0449bcf2016-02-09 13:25:45 -0800901 /** Return the number of intervals that intersect the intercept along the axis of the advance.
902 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
903 * the string. The caller may pass nullptr for intervals to determine the size of the interval
904 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
905 * intervals are cached by glyph to improve performance for multiple calls.
906 * This permits constructing an underline that skips the descenders.
907 *
908 * @param text the text
909 * @param length number of bytes of text
910 * @param x The x-coordinate of the origin of the text.
911 * @param y The y-coordinate of the origin of the text.
912 * @param bounds The lower and upper line parallel to the advance.
913 * @param array If not null, the found intersections.
914 *
915 * @return The number of intersections, which may be zero.
916 */
917 int getTextIntercepts(const void* text, size_t length, SkScalar x, SkScalar y,
918 const SkScalar bounds[2], SkScalar* intervals) const;
919
920 /** Return the number of intervals that intersect the intercept along the axis of the advance.
921 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
922 * string. The caller may pass nullptr for intervals to determine the size of the interval
923 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
924 * intervals are cached by glyph to improve performance for multiple calls.
925 * This permits constructing an underline that skips the descenders.
926 *
927 * @param text the text
928 * @param length number of bytes of text
929 * @param pos array of positions, used to position each character
930 * @param bounds The lower and upper line parallel to the advance.
931 * @param array If not null, the glyph bounds contained by the advance parallel lines.
932 *
933 * @return The number of intersections, which may be zero.
934 */
935 int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[],
936 const SkScalar bounds[2], SkScalar* intervals) const;
937
fmalitaeae6a912016-07-28 09:47:24 -0700938 /** Return the number of intervals that intersect the intercept along the axis of the advance.
939 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
940 * string. The caller may pass nullptr for intervals to determine the size of the interval
941 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
942 * intervals are cached by glyph to improve performance for multiple calls.
943 * This permits constructing an underline that skips the descenders.
944 *
945 * @param text The text.
946 * @param length Number of bytes of text.
947 * @param xpos Array of x-positions, used to position each character.
948 * @param constY The shared Y coordinate for all of the positions.
949 * @param bounds The lower and upper line parallel to the advance.
950 * @param array If not null, the glyph bounds contained by the advance parallel lines.
951 *
952 * @return The number of intersections, which may be zero.
953 */
954 int getPosTextHIntercepts(const void* text, size_t length, const SkScalar xpos[],
955 SkScalar constY, const SkScalar bounds[2], SkScalar* intervals) const;
956
957 /** Return the number of intervals that intersect the intercept along the axis of the advance.
958 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
959 * text blob. The caller may pass nullptr for intervals to determine the size of the interval
960 * array. The computed intervals are cached by glyph to improve performance for multiple calls.
961 * This permits constructing an underline that skips the descenders.
962 *
963 * @param blob The text blob.
964 * @param bounds The lower and upper line parallel to the advance.
965 * @param array If not null, the glyph bounds contained by the advance parallel lines.
966 *
967 * @return The number of intersections, which may be zero.
968 */
969 int getTextBlobIntercepts(const SkTextBlob* blob, const SkScalar bounds[2],
970 SkScalar* intervals) const;
971
reed8893e5f2014-12-15 13:27:26 -0800972 /**
973 * Return a rectangle that represents the union of the bounds of all
974 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
975 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
976 * on this paint.
977 */
978 SkRect getFontBounds() const;
979
reed@google.com632e1a22011-10-06 12:37:00 +0000980 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
981 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
982 bool nothingToDraw() const;
983
reed@google.coma584aed2012-05-16 14:06:02 +0000984 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000985 // would prefer to make these private...
986
reed@google.coma584aed2012-05-16 14:06:02 +0000987 /** Returns true if the current paint settings allow for fast computation of
988 bounds (i.e. there is nothing complex like a patheffect that would make
989 the bounds computation expensive.
990 */
senorblanco0abdf762015-08-20 11:10:41 -0700991 bool canComputeFastBounds() const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000992
reed@google.coma584aed2012-05-16 14:06:02 +0000993 /** Only call this if canComputeFastBounds() returned true. This takes a
994 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
995 effects in the paint (e.g. stroking). If needed, it uses the storage
996 rect parameter. It returns the adjusted bounds that can then be used
997 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000998
reed@google.coma584aed2012-05-16 14:06:02 +0000999 The returned rect will either be orig or storage, thus the caller
1000 should not rely on storage being set to the result, but should always
1001 use the retured value. It is legal for orig and storage to be the same
1002 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001003
reed@google.coma584aed2012-05-16 14:06:02 +00001004 e.g.
1005 if (paint.canComputeFastBounds()) {
1006 SkRect r, storage;
1007 path.computeBounds(&r, SkPath::kFast_BoundsType);
1008 const SkRect& fastR = paint.computeFastBounds(r, &storage);
1009 if (canvas->quickReject(fastR, ...)) {
1010 // don't draw the path
1011 }
1012 }
1013 */
1014 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
Brian Osman60751d72017-05-12 11:21:36 -04001015 // Things like stroking, etc... will do math on the bounds rect, assuming that it's sorted.
1016 SkASSERT(orig.isSorted());
reed@google.coma584aed2012-05-16 14:06:02 +00001017 SkPaint::Style style = this->getStyle();
1018 // ultra fast-case: filling with no effects that affect geometry
1019 if (kFill_Style == style) {
1020 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
1021 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
1022 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +00001023 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +00001024 if (!effects) {
1025 return orig;
1026 }
1027 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001028
reed@google.coma584aed2012-05-16 14:06:02 +00001029 return this->doComputeFastBounds(orig, storage, style);
1030 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001031
reed@google.coma584aed2012-05-16 14:06:02 +00001032 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1033 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001034 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001035 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001036
reed@google.coma584aed2012-05-16 14:06:02 +00001037 // Take the style explicitly, so the caller can force us to be stroked
1038 // without having to make a copy of the paint just to change that field.
1039 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
Cary Clark0418a882017-05-10 09:07:42 -04001040 Style style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001041
Cary Clark0418a882017-05-10 09:07:42 -04001042
benjaminwagnerd936f632016-02-23 10:44:31 -08001043
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001044 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001045
reed@google.comd5f20792012-05-16 14:15:02 +00001046private:
Cary Clark0418a882017-05-10 09:07:42 -04001047 typedef const SkGlyph& (*GlyphCacheProc)(SkGlyphCache*, const char**);
1048
reeda5ab9ec2016-03-06 18:10:48 -08001049 sk_sp<SkTypeface> fTypeface;
1050 sk_sp<SkPathEffect> fPathEffect;
1051 sk_sp<SkShader> fShader;
reeda5ab9ec2016-03-06 18:10:48 -08001052 sk_sp<SkMaskFilter> fMaskFilter;
1053 sk_sp<SkColorFilter> fColorFilter;
1054 sk_sp<SkRasterizer> fRasterizer;
reed46f2d0a2016-09-11 05:40:31 -07001055 sk_sp<SkDrawLooper> fDrawLooper;
reeda5ab9ec2016-03-06 18:10:48 -08001056 sk_sp<SkImageFilter> fImageFilter;
reed@google.comd5f20792012-05-16 14:15:02 +00001057
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001058 SkScalar fTextSize;
1059 SkScalar fTextScaleX;
1060 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001061 SkColor fColor;
1062 SkScalar fWidth;
1063 SkScalar fMiterLimit;
Mike Reed71fecc32016-11-18 17:19:54 -05001064 uint32_t fBlendMode; // just need 5-6 bits
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001065 union {
1066 struct {
1067 // all of these bitfields should add up to 32
1068 unsigned fFlags : 16;
1069 unsigned fTextAlign : 2;
1070 unsigned fCapType : 2;
1071 unsigned fJoinType : 2;
1072 unsigned fStyle : 2;
1073 unsigned fTextEncoding : 2; // 3 values
1074 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001075 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001076 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001077 } fBitfields;
1078 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001079 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001080
robertphillipse34f17d2016-07-19 07:59:22 -07001081 static GlyphCacheProc GetGlyphCacheProc(TextEncoding encoding,
1082 bool isDevKern,
1083 bool needFullMetrics);
reed@google.comd5f20792012-05-16 14:15:02 +00001084
1085 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1086 int* count, SkRect* bounds) const;
1087
brianosmana1e8f8d2016-04-08 06:47:54 -07001088 enum ScalerContextFlags : uint32_t {
1089 kNone_ScalerContextFlags = 0,
1090
1091 kFakeGamma_ScalerContextFlag = 1 << 0,
1092 kBoostContrast_ScalerContextFlag = 1 << 1,
1093
1094 kFakeGammaAndBoostContrast_ScalerContextFlags =
1095 kFakeGamma_ScalerContextFlag | kBoostContrast_ScalerContextFlag,
bungemanf6d1e602016-02-22 13:20:28 -08001096 };
1097
joshualittfd450792015-03-13 08:38:43 -07001098 /*
1099 * Allocs an SkDescriptor on the heap and return it to the caller as a refcnted
1100 * SkData. Caller is responsible for managing the lifetime of this object.
1101 */
reeda9322c22016-04-12 06:47:05 -07001102 void getScalerContextDescriptor(SkScalerContextEffects*, SkAutoDescriptor*,
1103 const SkSurfaceProps& surfaceProps,
brianosmana1e8f8d2016-04-08 06:47:54 -07001104 uint32_t scalerContextFlags, const SkMatrix*) const;
joshualittfd450792015-03-13 08:38:43 -07001105
brianosmana1e8f8d2016-04-08 06:47:54 -07001106 SkGlyphCache* detachCache(const SkSurfaceProps* surfaceProps, uint32_t scalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -08001107 const SkMatrix*) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001108
brianosmana1e8f8d2016-04-08 06:47:54 -07001109 void descriptorProc(const SkSurfaceProps* surfaceProps, uint32_t scalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -08001110 const SkMatrix* deviceMatrix,
reeda9322c22016-04-12 06:47:05 -07001111 void (*proc)(SkTypeface*, const SkScalerContextEffects&,
1112 const SkDescriptor*, void*),
bungemanf6d1e602016-02-22 13:20:28 -08001113 void* context) const;
reed@android.comd252db02009-04-01 18:31:44 +00001114
joshualitt9e36c1a2015-04-14 12:17:27 -07001115 /*
1116 * The luminance color is used to determine which Gamma Canonical color to map to. This is
1117 * really only used by backends which want to cache glyph masks, and need some way to know if
1118 * they need to generate new masks based off a given color.
1119 */
1120 SkColor computeLuminanceColor() const;
1121
reed@android.com8a1c16f2008-12-17 15:59:43 +00001122 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001123 /* This is the size we use when we ask for a glyph's path. We then
1124 * post-transform it as we draw to match the request.
1125 * This is done to try to re-use cache entries for the path.
1126 *
1127 * This value is somewhat arbitrary. In theory, it could be 1, since
1128 * we store paths as floats. However, we get the path from the font
1129 * scaler, and it may represent its paths as fixed-point (or 26.6),
1130 * so we shouldn't ask for something too big (might overflow 16.16)
1131 * or too small (underflow 26.6).
1132 *
1133 * This value could track kMaxSizeForGlyphCache, assuming the above
1134 * constraints, but since we ask for unhinted paths, the two values
1135 * need not match per-se.
1136 */
1137 kCanonicalTextSizeForPaths = 64,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001138 };
reed@google.comed43dff2013-06-04 16:56:27 +00001139
1140 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1141
reed@google.comed43dff2013-06-04 16:56:27 +00001142 // Set flags/hinting/textSize up to use for drawing text as paths.
1143 // Returns scale factor to restore the original textSize, since will will
1144 // have change it to kCanonicalTextSizeForPaths.
1145 SkScalar setupForAsPaths();
1146
Mike Reedd5bee5d2017-06-01 14:45:44 -04001147 static SkScalar MaxCacheSize2();
reed@google.comed43dff2013-06-04 16:56:27 +00001148
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001149 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001150 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001151 friend class SkCanvas;
1152 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001153 friend class SkPDFDevice;
joshualitte76b4bb2015-12-28 07:23:58 -08001154 friend class GrAtlasTextBlob;
joshualittdbd35932015-04-02 09:19:04 -07001155 friend class GrAtlasTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001156 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001157 friend class GrPathRendering;
joshualitt0a42e682015-12-10 13:20:58 -08001158 friend class GrTextUtils;
cdalton855d83f2014-09-18 13:51:53 -07001159 friend class GrGLPathRendering;
joshualitt9e36c1a2015-04-14 12:17:27 -07001160 friend class SkScalerContext;
caryclark0449bcf2016-02-09 13:25:45 -08001161 friend class SkTextBaseIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001162 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001163};
1164
reed@android.com8a1c16f2008-12-17 15:59:43 +00001165#endif