blob: ef80b73f21f7b0f1d6b01755f9ffebc92d809b0c [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkPaint_DEFINED
9#define SkPaint_DEFINED
10
11#include "SkColor.h"
reedf803da12015-01-23 05:58:07 -080012#include "SkFilterQuality.h"
reed@google.comed43dff2013-06-04 16:56:27 +000013#include "SkMatrix.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000014#include "SkXfermode.h"
15
joshualitt2b6acb42015-04-01 11:30:27 -070016class SkAutoDescriptor;
reed@android.com8a1c16f2008-12-17 15:59:43 +000017class SkAutoGlyphCache;
18class SkColorFilter;
joshualittfd450792015-03-13 08:38:43 -070019class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020class SkDescriptor;
senorblanco0abdf762015-08-20 11:10:41 -070021class SkDrawLooper;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000022class SkReadBuffer;
23class SkWriteBuffer;
herbb69d0e02015-02-25 06:47:06 -080024class SkGlyph;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025struct SkRect;
26class SkGlyphCache;
reed@google.com15356a62011-11-03 19:29:08 +000027class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000028class SkMaskFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029class SkPath;
30class SkPathEffect;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000031struct SkPoint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000032class SkRasterizer;
reeda9322c22016-04-12 06:47:05 -070033struct SkScalerContextEffects;
reed@android.com8a1c16f2008-12-17 15:59:43 +000034class SkShader;
robertphillipsfcf78292015-06-19 11:49:52 -070035class SkSurfaceProps;
reed@android.com8a1c16f2008-12-17 15:59:43 +000036class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037
humper@google.comb0889472013-07-09 21:37:14 +000038#define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag
39
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
52 SkPaint& operator=(const SkPaint&);
bungemanccce0e02016-02-07 14:37:23 -080053 SkPaint& operator=(SkPaint&&);
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
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000069 void flatten(SkWriteBuffer&) const;
70 void unflatten(SkReadBuffer&);
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 kDither_Flag = 0x04, //!< mask to enable dithering
104 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
105 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
106 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
107 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000108 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000110 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000111 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000112 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000113 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000114 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
agl@chromium.org309485b2009-07-21 17:41:32 +0000115 // when adding extra flags, note that the fFlags member is specified
116 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117
humper@google.com387db0a2013-07-09 14:13:04 +0000118 kAllFlags = 0xFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 };
120
121 /** Return the paint's flags. Use the Flag enum to test flag values.
122 @return the paint's flags (see enums ending in _Flag for bit masks)
123 */
reedf59eab22014-07-14 14:39:15 -0700124 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125
126 /** Set the paint's flags. Use the Flag enum to specific flag values.
127 @param flags The new flag bits for the paint (see Flags enum)
128 */
129 void setFlags(uint32_t flags);
130
131 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
132 @return true if the antialias bit is set in the paint's flags.
133 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000134 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 return SkToBool(this->getFlags() & kAntiAlias_Flag);
136 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
139 @param aa true to enable antialiasing, false to disable it
140 */
141 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 /** Helper for getFlags(), returning true if kDither_Flag bit is set
144 @return true if the dithering bit is set in the paint's flags.
145 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000146 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 return SkToBool(this->getFlags() & kDither_Flag);
148 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
151 @param dither true to enable dithering, false to disable it
152 */
153 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
156 @return true if the lineartext bit is set in the paint's flags
157 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000158 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 return SkToBool(this->getFlags() & kLinearText_Flag);
160 }
161
162 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
163 @param linearText true to set the linearText bit in the paint's flags,
164 false to clear it.
165 */
166 void setLinearText(bool linearText);
167
168 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
169 @return true if the lineartext bit is set in the paint's flags
170 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000171 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 return SkToBool(this->getFlags() & kSubpixelText_Flag);
173 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000174
reed@google.com84b437e2011-08-01 12:45:35 +0000175 /**
176 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
177 * @param subpixelText true to set the subpixelText bit in the paint's
178 * flags, false to clear it.
179 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000181
reed@google.com9d07fec2011-03-16 20:02:59 +0000182 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000183 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
184 }
185
reed@google.com84b437e2011-08-01 12:45:35 +0000186 /**
187 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
188 * Note: antialiasing must also be on for lcd rendering
189 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
190 * false to clear it.
191 */
192 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000193
reed@google.com9d07fec2011-03-16 20:02:59 +0000194 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000195 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
196 }
197
198 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
199 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
200 false to clear it.
201 */
202 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
203
reed@google.com9d07fec2011-03-16 20:02:59 +0000204 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000205 return SkToBool(this->getFlags() & kAutoHinting_Flag);
206 }
207
208 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
209 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
210 paint's flags,
211 false to clear it.
212 */
213 void setAutohinted(bool useAutohinter);
214
reed@google.com830a23e2011-11-10 15:20:49 +0000215 bool isVerticalText() const {
216 return SkToBool(this->getFlags() & kVerticalText_Flag);
217 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000218
reed@google.com830a23e2011-11-10 15:20:49 +0000219 /**
220 * Helper for setting or clearing the kVerticalText_Flag bit in
221 * setFlags(...).
222 *
223 * If this bit is set, then advances are treated as Y values rather than
224 * X values, and drawText will places its glyphs vertically rather than
225 * horizontally.
226 */
227 void setVerticalText(bool);
228
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
230 @return true if the underlineText bit is set in the paint's flags.
231 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000232 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 return SkToBool(this->getFlags() & kUnderlineText_Flag);
234 }
235
236 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
237 @param underlineText true to set the underlineText bit in the paint's
238 flags, false to clear it.
239 */
240 void setUnderlineText(bool underlineText);
241
242 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
243 @return true if the strikeThruText bit is set in the paint's flags.
244 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000245 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
247 }
248
249 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
250 @param strikeThruText true to set the strikeThruText bit in the
251 paint's flags, false to clear it.
252 */
253 void setStrikeThruText(bool strikeThruText);
254
255 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
256 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
257 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000258 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
260 }
261
262 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
263 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
264 flags, false to clear it.
265 */
266 void setFakeBoldText(bool fakeBoldText);
267
268 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
269 @return true if the kernText bit is set in the paint's flags.
270 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000271 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 return SkToBool(this->getFlags() & kDevKernText_Flag);
273 }
274
275 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
276 @param kernText true to set the kKernText_Flag bit in the paint's
277 flags, false to clear it.
278 */
279 void setDevKernText(bool devKernText);
280
reedf803da12015-01-23 05:58:07 -0800281 /**
282 * Return the filter level. This affects the quality (and performance) of
283 * drawing scaled images.
284 */
285 SkFilterQuality getFilterQuality() const {
286 return (SkFilterQuality)fBitfields.fFilterQuality;
287 }
mtkleinfe81e2d2015-09-09 07:35:42 -0700288
reedf803da12015-01-23 05:58:07 -0800289 /**
290 * Set the filter quality. This affects the quality (and performance) of
291 * drawing scaled images.
292 */
293 void setFilterQuality(SkFilterQuality quality);
reed@google.comc9683152013-07-18 13:47:01 +0000294
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 /** Styles apply to rect, oval, path, and text.
296 Bitmaps are always drawn in "fill", and lines are always drawn in
297 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000298
reed@android.comed881c22009-09-15 14:10:42 +0000299 Note: strokeandfill implicitly draws the result with
300 SkPath::kWinding_FillType, so if the original path is even-odd, the
301 results may not appear the same as if it was drawn twice, filled and
302 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 */
304 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000305 kFill_Style, //!< fill the geometry
306 kStroke_Style, //!< stroke the geometry
307 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000308 };
309 enum {
310 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 };
312
313 /** Return the paint's style, used for controlling how primitives'
314 geometries are interpreted (except for drawBitmap, which always assumes
315 kFill_Style).
316 @return the paint's Style
317 */
reedf59eab22014-07-14 14:39:15 -0700318 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319
320 /** Set the paint's style, used for controlling how primitives'
321 geometries are interpreted (except for drawBitmap, which always assumes
322 Fill).
323 @param style The new style to set in the paint
324 */
325 void setStyle(Style style);
326
327 /** Return the paint's color. Note that the color is a 32bit value
328 containing alpha as well as r,g,b. This 32bit value is not
329 premultiplied, meaning that its alpha can be any value, regardless of
330 the values of r,g,b.
331 @return the paint's color (and alpha).
332 */
333 SkColor getColor() const { return fColor; }
334
335 /** Set the paint's color. Note that the color is a 32bit value containing
336 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
337 that its alpha can be any value, regardless of the values of r,g,b.
338 @param color The new color (including alpha) to set in the paint.
339 */
340 void setColor(SkColor color);
341
342 /** Helper to getColor() that just returns the color's alpha value.
343 @return the alpha component of the paint's color.
344 */
345 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000346
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 /** Helper to setColor(), that only assigns the color's alpha value,
348 leaving its r,g,b values unchanged.
349 @param a set the alpha component (0..255) of the paint's color.
350 */
351 void setAlpha(U8CPU a);
352
353 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
354 using SkColorSetARGB()
355 @param a The new alpha component (0..255) of the paint's color.
356 @param r The new red component (0..255) of the paint's color.
357 @param g The new green component (0..255) of the paint's color.
358 @param b The new blue component (0..255) of the paint's color.
359 */
360 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
361
reed@google.com9d07fec2011-03-16 20:02:59 +0000362 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363 <p />
364 A value of 0 strokes in hairline mode.
365 Hairlines always draw 1-pixel wide, regardless of the matrix.
366 @return the paint's stroke width, used whenever the paint's style is
367 Stroke or StrokeAndFill.
368 */
369 SkScalar getStrokeWidth() const { return fWidth; }
370
reed@google.com9d07fec2011-03-16 20:02:59 +0000371 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372 Pass 0 to stroke in hairline mode.
373 Hairlines always draw 1-pixel wide, regardless of the matrix.
374 @param width set the paint's stroke width, used whenever the paint's
375 style is Stroke or StrokeAndFill.
376 */
377 void setStrokeWidth(SkScalar width);
378
379 /** Return the paint's stroke miter value. This is used to control the
380 behavior of miter joins when the joins angle is sharp.
381 @return the paint's miter limit, used whenever the paint's style is
382 Stroke or StrokeAndFill.
383 */
384 SkScalar getStrokeMiter() const { return fMiterLimit; }
385
386 /** Set the paint's stroke miter value. This is used to control the
387 behavior of miter joins when the joins angle is sharp. This value must
388 be >= 0.
389 @param miter set the miter limit on the paint, used whenever the
390 paint's style is Stroke or StrokeAndFill.
391 */
392 void setStrokeMiter(SkScalar miter);
393
394 /** Cap enum specifies the settings for the paint's strokecap. This is the
395 treatment that is applied to the beginning and end of each non-closed
396 contour (e.g. lines).
caryclark5cb00a92015-08-26 09:04:55 -0700397
398 If the cap is round or square, the caps are drawn when the contour has
399 a zero length. Zero length contours can be created by following moveTo
400 with a lineTo at the same point, or a moveTo followed by a close.
401
402 A dash with an on interval of zero also creates a zero length contour.
403
404 The zero length contour draws the square cap without rotation, since
405 the no direction can be inferred.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406 */
407 enum Cap {
408 kButt_Cap, //!< begin/end contours with no extension
409 kRound_Cap, //!< begin/end contours with a semi-circle extension
410 kSquare_Cap, //!< begin/end contours with a half square extension
411
bsalomona7d85ba2016-07-06 11:54:59 -0700412 kLast_Cap = kSquare_Cap,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413 kDefault_Cap = kButt_Cap
414 };
bsalomona7d85ba2016-07-06 11:54:59 -0700415 static constexpr int kCapCount = kLast_Cap + 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416
417 /** Join enum specifies the settings for the paint's strokejoin. This is
418 the treatment that is applied to corners in paths and rectangles.
419 */
420 enum Join {
421 kMiter_Join, //!< connect path segments with a sharp join
422 kRound_Join, //!< connect path segments with a round join
423 kBevel_Join, //!< connect path segments with a flat bevel join
424
bsalomona7d85ba2016-07-06 11:54:59 -0700425 kLast_Join = kBevel_Join,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 kDefault_Join = kMiter_Join
427 };
bsalomona7d85ba2016-07-06 11:54:59 -0700428 static constexpr int kJoinCount = kLast_Join + 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429
430 /** Return the paint's stroke cap type, controlling how the start and end
431 of stroked lines and paths are treated.
432 @return the line cap style for the paint, used whenever the paint's
433 style is Stroke or StrokeAndFill.
434 */
reedf59eab22014-07-14 14:39:15 -0700435 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436
437 /** Set the paint's stroke cap type.
438 @param cap set the paint's line cap style, used whenever the paint's
439 style is Stroke or StrokeAndFill.
440 */
441 void setStrokeCap(Cap cap);
442
443 /** Return the paint's stroke join type.
444 @return the paint's line join style, used whenever the paint's style is
445 Stroke or StrokeAndFill.
446 */
reedf59eab22014-07-14 14:39:15 -0700447 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448
449 /** Set the paint's stroke join type.
450 @param join set the paint's line join style, used whenever the paint's
451 style is Stroke or StrokeAndFill.
452 */
453 void setStrokeJoin(Join join);
454
reed@google.com4bbdeac2013-01-24 21:03:11 +0000455 /**
456 * Applies any/all effects (patheffect, stroking) to src, returning the
457 * result in dst. The result is that drawing src with this paint will be
458 * the same as drawing dst with a default paint (at least from the
459 * geometric perspective).
460 *
461 * @param src input path
462 * @param dst output path (may be the same as src)
463 * @param cullRect If not null, the dst path may be culled to this rect.
reed05d90442015-02-12 13:35:52 -0800464 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision
465 * in favor of speed/size.
reed@google.com4bbdeac2013-01-24 21:03:11 +0000466 * @return true if the path should be filled, or false if it should be
467 * drawn with a hairline (width == 0)
468 */
reed05d90442015-02-12 13:35:52 -0800469 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
470 SkScalar resScale = 1) const;
471
472 bool getFillPath(const SkPath& src, SkPath* dst) const {
473 return this->getFillPath(src, dst, NULL, 1);
474 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476 /** Get the paint's shader object.
477 <p />
478 The shader's reference count is not affected.
479 @return the paint's shader (or NULL)
480 */
reeda5ab9ec2016-03-06 18:10:48 -0800481 SkShader* getShader() const { return fShader.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482
483 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000484 * Shaders specify the source color(s) for what is being drawn. If a paint
485 * has no shader, then the paint's color is used. If the paint has a
486 * shader, then the shader's color(s) are use instead, but they are
487 * modulated by the paint's alpha. This makes it easy to create a shader
488 * once (e.g. bitmap tiling or gradient) and then change its transparency
489 * w/o having to modify the original shader... only the paint's alpha needs
490 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000491 *
492 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
493 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
494 * then the shader will use the paint's entire color to "colorize" its output (modulating the
495 * bitmap's alpha with the paint's color+alpha).
496 *
reed@google.com880dc472012-05-11 14:47:03 +0000497 * Pass NULL to clear any previous shader.
498 * As a convenience, the parameter passed is also returned.
499 * If a previous shader exists, its reference count is decremented.
500 * If shader is not NULL, its reference count is incremented.
501 * @param shader May be NULL. The shader to be installed in the paint
502 * @return shader
503 */
reeda5ab9ec2016-03-06 18:10:48 -0800504 void setShader(sk_sp<SkShader>);
reedfe630452016-03-25 09:08:00 -0700505#ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR
506 SkShader* setShader(SkShader* shader);
507#endif
reed@google.com9d07fec2011-03-16 20:02:59 +0000508
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 /** Get the paint's colorfilter. If there is a colorfilter, its reference
510 count is not changed.
511 @return the paint's colorfilter (or NULL)
512 */
reeda5ab9ec2016-03-06 18:10:48 -0800513 SkColorFilter* getColorFilter() const { return fColorFilter.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514
515 /** Set or clear the paint's colorfilter, returning the parameter.
516 <p />
517 If the paint already has a filter, its reference count is decremented.
518 If filter is not NULL, its reference count is incremented.
519 @param filter May be NULL. The filter to be installed in the paint
520 @return filter
521 */
reedd053ce92016-03-22 10:17:23 -0700522#ifdef SK_SUPPORT_LEGACY_COLORFILTER_PTR
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 SkColorFilter* setColorFilter(SkColorFilter* filter);
reedd053ce92016-03-22 10:17:23 -0700524#endif
reeda5ab9ec2016-03-06 18:10:48 -0800525 void setColorFilter(sk_sp<SkColorFilter>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526
527 /** Get the paint's xfermode object.
528 <p />
529 The xfermode's reference count is not affected.
530 @return the paint's xfermode (or NULL)
531 */
reeda5ab9ec2016-03-06 18:10:48 -0800532 SkXfermode* getXfermode() const { return fXfermode.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533
534 /** Set or clear the xfermode object.
535 <p />
536 Pass NULL to clear any previous xfermode.
537 As a convenience, the parameter passed is also returned.
538 If a previous xfermode exists, its reference count is decremented.
539 If xfermode is not NULL, its reference count is incremented.
540 @param xfermode May be NULL. The new xfermode to be installed in the
541 paint
542 @return xfermode
543 */
reeda5ab9ec2016-03-06 18:10:48 -0800544 void setXfermode(sk_sp<SkXfermode>);
reedcfb6bdf2016-03-29 11:32:50 -0700545#ifdef SK_SUPPORT_LEGACY_XFERMODE_PTR
546 SkXfermode* setXfermode(SkXfermode* xfermode);
547#endif
reed@android.coma0f5d152009-06-22 17:38:10 +0000548
549 /** Create an xfermode based on the specified Mode, and assign it into the
550 paint, returning the mode that was set. If the Mode is SrcOver, then
551 the paint's xfermode is set to null.
552 */
reed@android.com0baf1932009-06-24 12:41:42 +0000553 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000554
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 /** Get the paint's patheffect object.
556 <p />
557 The patheffect reference count is not affected.
558 @return the paint's patheffect (or NULL)
559 */
reeda5ab9ec2016-03-06 18:10:48 -0800560 SkPathEffect* getPathEffect() const { return fPathEffect.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000561
562 /** Set or clear the patheffect object.
563 <p />
564 Pass NULL to clear any previous patheffect.
565 As a convenience, the parameter passed is also returned.
566 If a previous patheffect exists, its reference count is decremented.
567 If patheffect is not NULL, its reference count is incremented.
568 @param effect May be NULL. The new patheffect to be installed in the
569 paint
570 @return effect
571 */
reeda5ab9ec2016-03-06 18:10:48 -0800572 void setPathEffect(sk_sp<SkPathEffect>);
reeda4393342016-03-18 11:22:57 -0700573#ifdef SK_SUPPORT_LEGACY_PATHEFFECT_PTR
574 SkPathEffect* setPathEffect(SkPathEffect* effect);
575#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576
577 /** Get the paint's maskfilter object.
578 <p />
579 The maskfilter reference count is not affected.
580 @return the paint's maskfilter (or NULL)
581 */
reeda5ab9ec2016-03-06 18:10:48 -0800582 SkMaskFilter* getMaskFilter() const { return fMaskFilter.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583
584 /** Set or clear the maskfilter object.
585 <p />
586 Pass NULL to clear any previous maskfilter.
587 As a convenience, the parameter passed is also returned.
588 If a previous maskfilter exists, its reference count is decremented.
589 If maskfilter is not NULL, its reference count is incremented.
590 @param maskfilter May be NULL. The new maskfilter to be installed in
591 the paint
592 @return maskfilter
593 */
reedefdfd512016-04-04 10:02:58 -0700594#ifdef SK_SUPPORT_LEGACY_MASKFILTER_PTR
reed@android.com8a1c16f2008-12-17 15:59:43 +0000595 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
reedefdfd512016-04-04 10:02:58 -0700596#endif
reeda5ab9ec2016-03-06 18:10:48 -0800597 void setMaskFilter(sk_sp<SkMaskFilter>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598
599 // These attributes are for text/fonts
600
601 /** Get the paint's typeface object.
602 <p />
603 The typeface object identifies which font to use when drawing or
604 measuring text. The typeface reference count is not affected.
605 @return the paint's typeface (or NULL)
606 */
reeda5ab9ec2016-03-06 18:10:48 -0800607 SkTypeface* getTypeface() const { return fTypeface.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000608
609 /** Set or clear the typeface object.
610 <p />
611 Pass NULL to clear any previous typeface.
612 As a convenience, the parameter passed is also returned.
613 If a previous typeface exists, its reference count is decremented.
614 If typeface is not NULL, its reference count is incremented.
615 @param typeface May be NULL. The new typeface to be installed in the
616 paint
617 @return typeface
618 */
scroggo9a9a7b22016-05-12 06:22:30 -0700619 void setTypeface(sk_sp<SkTypeface>);
bungeman13b9c952016-05-12 10:09:30 -0700620#ifdef SK_SUPPORT_LEGACY_TYPEFACE_PTR
621 SkTypeface* setTypeface(SkTypeface* typeface);
622#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623
624 /** Get the paint's rasterizer (or NULL).
625 <p />
626 The raster controls how paths/text are turned into alpha masks.
627 @return the paint's rasterizer (or NULL)
628 */
reeda5ab9ec2016-03-06 18:10:48 -0800629 SkRasterizer* getRasterizer() const { return fRasterizer.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000630
631 /** Set or clear the rasterizer object.
632 <p />
633 Pass NULL to clear any previous rasterizer.
634 As a convenience, the parameter passed is also returned.
635 If a previous rasterizer exists in the paint, its reference count is
636 decremented. If rasterizer is not NULL, its reference count is
637 incremented.
638 @param rasterizer May be NULL. The new rasterizer to be installed in
639 the paint.
640 @return rasterizer
641 */
reed7b380d02016-03-21 13:25:16 -0700642#ifdef SK_SUPPORT_LEGACY_MINOR_EFFECT_PTR
reed@android.com8a1c16f2008-12-17 15:59:43 +0000643 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
reed7b380d02016-03-21 13:25:16 -0700644#endif
reeda5ab9ec2016-03-06 18:10:48 -0800645 void setRasterizer(sk_sp<SkRasterizer>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000646
reeda5ab9ec2016-03-06 18:10:48 -0800647 SkImageFilter* getImageFilter() const { return fImageFilter.get(); }
reed@google.com15356a62011-11-03 19:29:08 +0000648 SkImageFilter* setImageFilter(SkImageFilter*);
reeda5ab9ec2016-03-06 18:10:48 -0800649 void setImageFilter(sk_sp<SkImageFilter>);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000650
reed@google.comb0a34d82012-07-11 19:57:55 +0000651 /**
reed@google.com9d07fec2011-03-16 20:02:59 +0000652 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
653 * reference count.
654 */
reeda5ab9ec2016-03-06 18:10:48 -0800655 SkDrawLooper* getLooper() const { return fLooper.get(); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000656
657 /**
658 * Set or clear the looper object.
659 * <p />
660 * Pass NULL to clear any previous looper.
661 * As a convenience, the parameter passed is also returned.
662 * If a previous looper exists in the paint, its reference count is
663 * decremented. If looper is not NULL, its reference count is
664 * incremented.
665 * @param looper May be NULL. The new looper to be installed in the paint.
666 * @return looper
667 */
reed7b380d02016-03-21 13:25:16 -0700668#ifdef SK_SUPPORT_LEGACY_MINOR_EFFECT_PTR
reed@google.com9d07fec2011-03-16 20:02:59 +0000669 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed7b380d02016-03-21 13:25:16 -0700670#endif
reeda5ab9ec2016-03-06 18:10:48 -0800671 void setLooper(sk_sp<SkDrawLooper>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000672
673 enum Align {
674 kLeft_Align,
675 kCenter_Align,
676 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000677 };
678 enum {
679 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000681
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682 /** Return the paint's Align value for drawing text.
683 @return the paint's Align value for drawing text.
684 */
reedf59eab22014-07-14 14:39:15 -0700685 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000686
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687 /** Set the paint's text alignment.
688 @param align set the paint's Align value for drawing text.
689 */
690 void setTextAlign(Align align);
691
692 /** Return the paint's text size.
693 @return the paint's text size.
694 */
695 SkScalar getTextSize() const { return fTextSize; }
696
697 /** Set the paint's text size. This value must be > 0
698 @param textSize set the paint's text size.
699 */
700 void setTextSize(SkScalar textSize);
701
702 /** Return the paint's horizontal scale factor for text. The default value
703 is 1.0.
704 @return the paint's scale factor in X for drawing/measuring text
705 */
706 SkScalar getTextScaleX() const { return fTextScaleX; }
707
708 /** Set the paint's horizontal scale factor for text. The default value
709 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
710 stretch the text narrower.
711 @param scaleX set the paint's scale factor in X for drawing/measuring
712 text.
713 */
714 void setTextScaleX(SkScalar scaleX);
715
716 /** Return the paint's horizontal skew factor for text. The default value
717 is 0.
718 @return the paint's skew factor in X for drawing text.
719 */
720 SkScalar getTextSkewX() const { return fTextSkewX; }
721
722 /** Set the paint's horizontal skew factor for text. The default value
723 is 0. For approximating oblique text, use values around -0.25.
724 @param skewX set the paint's skew factor in X for drawing text.
725 */
726 void setTextSkewX(SkScalar skewX);
727
728 /** Describes how to interpret the text parameters that are passed to paint
729 methods like measureText() and getTextWidths().
730 */
731 enum TextEncoding {
732 kUTF8_TextEncoding, //!< the text parameters are UTF8
733 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000734 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000735 kGlyphID_TextEncoding //!< the text parameters are glyph indices
736 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000737
reedf59eab22014-07-14 14:39:15 -0700738 TextEncoding getTextEncoding() const {
739 return (TextEncoding)fBitfields.fTextEncoding;
740 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000741
742 void setTextEncoding(TextEncoding encoding);
743
744 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000745 /** Flags which indicate the confidence level of various metrics.
746 A set flag indicates that the metric may be trusted.
747 */
748 enum FontMetricsFlags {
749 kUnderlineThinknessIsValid_Flag = 1 << 0,
750 kUnderlinePositionIsValid_Flag = 1 << 1,
751 };
752
753 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000754 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
755 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
756 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
757 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
758 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000759 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
760 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000761 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
762 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000763 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
764 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000765 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
766
767 /** Underline Position - position of the top of the Underline stroke
768 relative to the baseline, this can have following values
769 - Negative - means underline should be drawn above baseline.
770 - Positive - means below baseline.
771 - Zero - mean underline should be drawn on baseline.
772 */
773 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
774
775 /** If the fontmetrics has a valid underlinethickness, return true, and set the
776 thickness param to that value. If it doesn't return false and ignore the
777 thickness param.
778 */
779 bool hasUnderlineThickness(SkScalar* thickness) const {
780 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
781 *thickness = fUnderlineThickness;
782 return true;
783 }
784 return false;
785 }
786
787 /** If the fontmetrics has a valid underlineposition, return true, and set the
788 thickness param to that value. If it doesn't return false and ignore the
789 thickness param.
790 */
791 bool hasUnderlinePosition(SkScalar* position) const {
792 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
793 *position = fUnderlinePosition;
794 return true;
795 }
796 return false;
797 }
798
reed@android.com8a1c16f2008-12-17 15:59:43 +0000799 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000800
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 /** Return the recommend spacing between lines (which will be
802 fDescent - fAscent + fLeading).
803 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000804 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805 @param metrics If not null, returns the font metrics for the
806 current typeface/pointsize/etc setting in this
807 paint.
808 @param scale If not 0, return width as if the canvas were scaled
809 by this value
810 @param return the recommended spacing between lines
811 */
812 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000813
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814 /** Return the recommend line spacing. This will be
815 fDescent - fAscent + fLeading
816 */
817 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
818
819 /** Convert the specified text into glyph IDs, returning the number of
820 glyphs ID written. If glyphs is NULL, it is ignore and only the count
821 is returned.
822 */
823 int textToGlyphs(const void* text, size_t byteLength,
halcanaryd0e95a52016-07-25 07:18:12 -0700824 SkGlyphID glyphs[]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000825
reed@android.coma5dcaf62010-02-05 17:12:32 +0000826 /** Return true if all of the specified text has a corresponding non-zero
827 glyph ID. If any of the code-points in the text are not supported in
828 the typeface (i.e. the glyph ID would be zero), then return false.
829
830 If the text encoding for the paint is kGlyph_TextEncoding, then this
831 returns true if all of the specified glyph IDs are non-zero.
832 */
833 bool containsText(const void* text, size_t byteLength) const;
834
reed@android.com9d3a9852010-01-08 14:07:42 +0000835 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
836 to zero. Note: this does not look at the text-encoding setting in the
837 paint, only at the typeface.
838 */
halcanaryd0e95a52016-07-25 07:18:12 -0700839 void glyphsToUnichars(const SkGlyphID glyphs[], int count, SkUnichar text[]) const;
reed@android.com9d3a9852010-01-08 14:07:42 +0000840
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841 /** Return the number of drawable units in the specified text buffer.
842 This looks at the current TextEncoding field of the paint. If you also
843 want to have the text converted into glyph IDs, call textToGlyphs
844 instead.
845 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000846 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847 return this->textToGlyphs(text, byteLength, NULL);
848 }
849
reed@google.com44da42e2011-11-10 20:04:47 +0000850 /** Return the width of the text. This will return the vertical measure
851 * if isVerticalText() is true, in which case the returned value should
852 * be treated has a height instead of a width.
853 *
854 * @param text The text to be measured
855 * @param length Number of bytes of text to measure
856 * @param bounds If not NULL, returns the bounds of the text,
857 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000858 * @return The advance width of the text
859 */
reed99ae8812014-08-26 11:30:01 -0700860 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000861
reed@google.com44da42e2011-11-10 20:04:47 +0000862 /** Return the width of the text. This will return the vertical measure
863 * if isVerticalText() is true, in which case the returned value should
864 * be treated has a height instead of a width.
865 *
866 * @param text Address of the text
867 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000868 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000869 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000870 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700871 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000873
reed@google.com44da42e2011-11-10 20:04:47 +0000874 /** Return the number of bytes of text that were measured. If
875 * isVerticalText() is true, then the vertical advances are used for
876 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000877 *
reed@google.com44da42e2011-11-10 20:04:47 +0000878 * @param text The text to be measured
879 * @param length Number of bytes of text to measure
880 * @param maxWidth Maximum width. Only the subset of text whose accumulated
881 * widths are <= maxWidth are measured.
882 * @param measuredWidth Optional. If non-null, this returns the actual
883 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000884 * @return The number of bytes of text that were measured. Will be
885 * <= length.
886 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700888 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000889
reed@google.com44da42e2011-11-10 20:04:47 +0000890 /** Return the advances for the text. These will be vertical advances if
891 * isVerticalText() returns true.
892 *
893 * @param text the text
894 * @param byteLength number of bytes to of text
895 * @param widths If not null, returns the array of advances for
896 * the glyphs. If not NULL, must be at least a large
897 * as the number of unichars in the specified text.
898 * @param bounds If not null, returns the bounds for each of
899 * character, relative to (0, 0)
900 * @return the number of unichars in the specified text.
901 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
903 SkRect bounds[] = NULL) const;
904
905 /** Return the path (outline) for the specified text.
caryclark0449bcf2016-02-09 13:25:45 -0800906 * Note: just like SkCanvas::drawText, this will respect the Align setting
907 * in the paint.
908 *
909 * @param text the text
910 * @param length number of bytes of text
911 * @param x The x-coordinate of the origin of the text.
912 * @param y The y-coordinate of the origin of the text.
913 * @param path The outline of the text.
914 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000915 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
916 SkPath* path) const;
917
caryclark0449bcf2016-02-09 13:25:45 -0800918 /** Return the path (outline) for the specified text.
919 * Note: just like SkCanvas::drawText, this will respect the Align setting
920 * in the paint.
921 *
922 * @param text the text
923 * @param length number of bytes of text
924 * @param pos array of positions, used to position each character
925 * @param path The outline of the text.
926 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000927 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000928 const SkPoint pos[], SkPath* path) const;
929
caryclark0449bcf2016-02-09 13:25:45 -0800930 /** Return the number of intervals that intersect the intercept along the axis of the advance.
931 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
932 * the string. The caller may pass nullptr for intervals to determine the size of the interval
933 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
934 * intervals are cached by glyph to improve performance for multiple calls.
935 * This permits constructing an underline that skips the descenders.
936 *
937 * @param text the text
938 * @param length number of bytes of text
939 * @param x The x-coordinate of the origin of the text.
940 * @param y The y-coordinate of the origin of the text.
941 * @param bounds The lower and upper line parallel to the advance.
942 * @param array If not null, the found intersections.
943 *
944 * @return The number of intersections, which may be zero.
945 */
946 int getTextIntercepts(const void* text, size_t length, SkScalar x, SkScalar y,
947 const SkScalar bounds[2], SkScalar* intervals) const;
948
949 /** Return the number of intervals that intersect the intercept along the axis of the advance.
950 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
951 * string. The caller may pass nullptr for intervals to determine the size of the interval
952 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
953 * intervals are cached by glyph to improve performance for multiple calls.
954 * This permits constructing an underline that skips the descenders.
955 *
956 * @param text the text
957 * @param length number of bytes of text
958 * @param pos array of positions, used to position each character
959 * @param bounds The lower and upper line parallel to the advance.
960 * @param array If not null, the glyph bounds contained by the advance parallel lines.
961 *
962 * @return The number of intersections, which may be zero.
963 */
964 int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[],
965 const SkScalar bounds[2], SkScalar* intervals) const;
966
reed8893e5f2014-12-15 13:27:26 -0800967 /**
968 * Return a rectangle that represents the union of the bounds of all
969 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
970 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
971 * on this paint.
972 */
973 SkRect getFontBounds() const;
974
reed@google.com632e1a22011-10-06 12:37:00 +0000975 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
976 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
977 bool nothingToDraw() const;
978
reed@google.coma584aed2012-05-16 14:06:02 +0000979 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000980 // would prefer to make these private...
981
reed@google.coma584aed2012-05-16 14:06:02 +0000982 /** Returns true if the current paint settings allow for fast computation of
983 bounds (i.e. there is nothing complex like a patheffect that would make
984 the bounds computation expensive.
985 */
senorblanco0abdf762015-08-20 11:10:41 -0700986 bool canComputeFastBounds() const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000987
reed@google.coma584aed2012-05-16 14:06:02 +0000988 /** Only call this if canComputeFastBounds() returned true. This takes a
989 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
990 effects in the paint (e.g. stroking). If needed, it uses the storage
991 rect parameter. It returns the adjusted bounds that can then be used
992 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000993
reed@google.coma584aed2012-05-16 14:06:02 +0000994 The returned rect will either be orig or storage, thus the caller
995 should not rely on storage being set to the result, but should always
996 use the retured value. It is legal for orig and storage to be the same
997 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000998
reed@google.coma584aed2012-05-16 14:06:02 +0000999 e.g.
1000 if (paint.canComputeFastBounds()) {
1001 SkRect r, storage;
1002 path.computeBounds(&r, SkPath::kFast_BoundsType);
1003 const SkRect& fastR = paint.computeFastBounds(r, &storage);
1004 if (canvas->quickReject(fastR, ...)) {
1005 // don't draw the path
1006 }
1007 }
1008 */
1009 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
1010 SkPaint::Style style = this->getStyle();
1011 // ultra fast-case: filling with no effects that affect geometry
1012 if (kFill_Style == style) {
1013 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
1014 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
1015 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +00001016 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +00001017 if (!effects) {
1018 return orig;
1019 }
1020 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001021
reed@google.coma584aed2012-05-16 14:06:02 +00001022 return this->doComputeFastBounds(orig, storage, style);
1023 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001024
reed@google.coma584aed2012-05-16 14:06:02 +00001025 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1026 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001027 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001028 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001029
reed@google.coma584aed2012-05-16 14:06:02 +00001030 // Take the style explicitly, so the caller can force us to be stroked
1031 // without having to make a copy of the paint just to change that field.
1032 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1033 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001034
reed@google.comed43dff2013-06-04 16:56:27 +00001035 /**
1036 * Return a matrix that applies the paint's text values: size, scale, skew
1037 */
1038 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1039 SkScalar scaleX, SkScalar skewX) {
1040 matrix->setScale(size * scaleX, size);
1041 if (skewX) {
1042 matrix->postSkew(skewX, 0);
1043 }
1044 return matrix;
1045 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001046
reed@google.comed43dff2013-06-04 16:56:27 +00001047 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1048 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1049 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001050
benjaminwagnerd936f632016-02-23 10:44:31 -08001051 typedef const SkGlyph& (*GlyphCacheProc)(SkGlyphCache*, const char**);
1052
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001053 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001054
reed@google.comd5f20792012-05-16 14:15:02 +00001055private:
reeda5ab9ec2016-03-06 18:10:48 -08001056 sk_sp<SkTypeface> fTypeface;
1057 sk_sp<SkPathEffect> fPathEffect;
1058 sk_sp<SkShader> fShader;
1059 sk_sp<SkXfermode> fXfermode;
1060 sk_sp<SkMaskFilter> fMaskFilter;
1061 sk_sp<SkColorFilter> fColorFilter;
1062 sk_sp<SkRasterizer> fRasterizer;
1063 sk_sp<SkDrawLooper> fLooper;
1064 sk_sp<SkImageFilter> fImageFilter;
reed@google.comd5f20792012-05-16 14:15:02 +00001065
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001066 SkScalar fTextSize;
1067 SkScalar fTextScaleX;
1068 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001069 SkColor fColor;
1070 SkScalar fWidth;
1071 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001072 union {
1073 struct {
1074 // all of these bitfields should add up to 32
1075 unsigned fFlags : 16;
1076 unsigned fTextAlign : 2;
1077 unsigned fCapType : 2;
1078 unsigned fJoinType : 2;
1079 unsigned fStyle : 2;
1080 unsigned fTextEncoding : 2; // 3 values
1081 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001082 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001083 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001084 } fBitfields;
1085 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001086 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001087
robertphillipse34f17d2016-07-19 07:59:22 -07001088 static GlyphCacheProc GetGlyphCacheProc(TextEncoding encoding,
1089 bool isDevKern,
1090 bool needFullMetrics);
reed@google.comd5f20792012-05-16 14:15:02 +00001091
1092 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1093 int* count, SkRect* bounds) const;
1094
brianosmana1e8f8d2016-04-08 06:47:54 -07001095 enum ScalerContextFlags : uint32_t {
1096 kNone_ScalerContextFlags = 0,
1097
1098 kFakeGamma_ScalerContextFlag = 1 << 0,
1099 kBoostContrast_ScalerContextFlag = 1 << 1,
1100
1101 kFakeGammaAndBoostContrast_ScalerContextFlags =
1102 kFakeGamma_ScalerContextFlag | kBoostContrast_ScalerContextFlag,
bungemanf6d1e602016-02-22 13:20:28 -08001103 };
1104
joshualittfd450792015-03-13 08:38:43 -07001105 /*
1106 * Allocs an SkDescriptor on the heap and return it to the caller as a refcnted
1107 * SkData. Caller is responsible for managing the lifetime of this object.
1108 */
reeda9322c22016-04-12 06:47:05 -07001109 void getScalerContextDescriptor(SkScalerContextEffects*, SkAutoDescriptor*,
1110 const SkSurfaceProps& surfaceProps,
brianosmana1e8f8d2016-04-08 06:47:54 -07001111 uint32_t scalerContextFlags, const SkMatrix*) const;
joshualittfd450792015-03-13 08:38:43 -07001112
brianosmana1e8f8d2016-04-08 06:47:54 -07001113 SkGlyphCache* detachCache(const SkSurfaceProps* surfaceProps, uint32_t scalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -08001114 const SkMatrix*) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001115
brianosmana1e8f8d2016-04-08 06:47:54 -07001116 void descriptorProc(const SkSurfaceProps* surfaceProps, uint32_t scalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -08001117 const SkMatrix* deviceMatrix,
reeda9322c22016-04-12 06:47:05 -07001118 void (*proc)(SkTypeface*, const SkScalerContextEffects&,
1119 const SkDescriptor*, void*),
bungemanf6d1e602016-02-22 13:20:28 -08001120 void* context) const;
reed@android.comd252db02009-04-01 18:31:44 +00001121
joshualitt9e36c1a2015-04-14 12:17:27 -07001122 /*
1123 * The luminance color is used to determine which Gamma Canonical color to map to. This is
1124 * really only used by backends which want to cache glyph masks, and need some way to know if
1125 * they need to generate new masks based off a given color.
1126 */
1127 SkColor computeLuminanceColor() const;
1128
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001130 /* This is the size we use when we ask for a glyph's path. We then
1131 * post-transform it as we draw to match the request.
1132 * This is done to try to re-use cache entries for the path.
1133 *
1134 * This value is somewhat arbitrary. In theory, it could be 1, since
1135 * we store paths as floats. However, we get the path from the font
1136 * scaler, and it may represent its paths as fixed-point (or 26.6),
1137 * so we shouldn't ask for something too big (might overflow 16.16)
1138 * or too small (underflow 26.6).
1139 *
1140 * This value could track kMaxSizeForGlyphCache, assuming the above
1141 * constraints, but since we ask for unhinted paths, the two values
1142 * need not match per-se.
1143 */
1144 kCanonicalTextSizeForPaths = 64,
1145
1146 /*
1147 * Above this size (taking into account CTM and textSize), we never use
1148 * the cache for bits or metrics (we might overflow), so we just ask
1149 * for a caononical size and post-transform that.
1150 */
1151 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001152 };
reed@google.comed43dff2013-06-04 16:56:27 +00001153
1154 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1155
reed@google.comed43dff2013-06-04 16:56:27 +00001156 // Set flags/hinting/textSize up to use for drawing text as paths.
1157 // Returns scale factor to restore the original textSize, since will will
1158 // have change it to kCanonicalTextSizeForPaths.
1159 SkScalar setupForAsPaths();
1160
1161 static SkScalar MaxCacheSize2() {
1162 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1163 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1164 return kMag2Max;
1165 }
1166
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001167 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001168 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001169 friend class SkCanvas;
1170 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001171 friend class SkPDFDevice;
joshualitte76b4bb2015-12-28 07:23:58 -08001172 friend class GrAtlasTextBlob;
joshualittdbd35932015-04-02 09:19:04 -07001173 friend class GrAtlasTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001174 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001175 friend class GrPathRendering;
joshualitt0a42e682015-12-10 13:20:58 -08001176 friend class GrTextUtils;
cdalton855d83f2014-09-18 13:51:53 -07001177 friend class GrGLPathRendering;
joshualitt9e36c1a2015-04-14 12:17:27 -07001178 friend class SkScalerContext;
caryclark0449bcf2016-02-09 13:25:45 -08001179 friend class SkTextBaseIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001180 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001181};
1182
reed@android.com8a1c16f2008-12-17 15:59:43 +00001183#endif