blob: ea7eedd65f0917764a6c9ceb39e1c3e3167bd70e [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;
fmalitaeae6a912016-07-28 09:47:24 -070036class SkTextBlob;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
humper@google.comb0889472013-07-09 21:37:14 +000039#define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag
40
reed@android.com8a1c16f2008-12-17 15:59:43 +000041/** \class SkPaint
42
43 The SkPaint class holds the style and color information about how to draw
44 geometries, text and bitmaps.
45*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000046class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047public:
48 SkPaint();
49 SkPaint(const SkPaint& paint);
bungemanccce0e02016-02-07 14:37:23 -080050 SkPaint(SkPaint&& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 ~SkPaint();
52
53 SkPaint& operator=(const SkPaint&);
bungemanccce0e02016-02-07 14:37:23 -080054 SkPaint& operator=(SkPaint&&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
mtkleinbc97ef42014-08-25 10:10:47 -070056 /** operator== may give false negatives: two paints that draw equivalently
57 may return false. It will never give false positives: two paints that
58 are not equivalent always return false.
59 */
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
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000070 void flatten(SkWriteBuffer&) const;
71 void unflatten(SkReadBuffer&);
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 kDither_Flag = 0x04, //!< mask to enable dithering
105 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
106 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
107 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
108 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000109 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000111 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000112 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000113 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000114 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000115 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
agl@chromium.org309485b2009-07-21 17:41:32 +0000116 // when adding extra flags, note that the fFlags member is specified
117 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
humper@google.com387db0a2013-07-09 14:13:04 +0000119 kAllFlags = 0xFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 };
121
122 /** Return the paint's flags. Use the Flag enum to test flag values.
123 @return the paint's flags (see enums ending in _Flag for bit masks)
124 */
reedf59eab22014-07-14 14:39:15 -0700125 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126
127 /** Set the paint's flags. Use the Flag enum to specific flag values.
128 @param flags The new flag bits for the paint (see Flags enum)
129 */
130 void setFlags(uint32_t flags);
131
132 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
133 @return true if the antialias bit is set in the paint's flags.
134 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000135 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 return SkToBool(this->getFlags() & kAntiAlias_Flag);
137 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
140 @param aa true to enable antialiasing, false to disable it
141 */
142 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 /** Helper for getFlags(), returning true if kDither_Flag bit is set
145 @return true if the dithering bit is set in the paint's flags.
146 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000147 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 return SkToBool(this->getFlags() & kDither_Flag);
149 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
152 @param dither true to enable dithering, false to disable it
153 */
154 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
157 @return true if the lineartext bit is set in the paint's flags
158 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000159 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 return SkToBool(this->getFlags() & kLinearText_Flag);
161 }
162
163 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
164 @param linearText true to set the linearText bit in the paint's flags,
165 false to clear it.
166 */
167 void setLinearText(bool linearText);
168
169 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
170 @return true if the lineartext bit is set in the paint's flags
171 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000172 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 return SkToBool(this->getFlags() & kSubpixelText_Flag);
174 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000175
reed@google.com84b437e2011-08-01 12:45:35 +0000176 /**
177 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
178 * @param subpixelText true to set the subpixelText bit in the paint's
179 * flags, false to clear it.
180 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000182
reed@google.com9d07fec2011-03-16 20:02:59 +0000183 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000184 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
185 }
186
reed@google.com84b437e2011-08-01 12:45:35 +0000187 /**
188 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
189 * Note: antialiasing must also be on for lcd rendering
190 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
191 * false to clear it.
192 */
193 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000194
reed@google.com9d07fec2011-03-16 20:02:59 +0000195 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000196 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
197 }
198
199 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
200 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
201 false to clear it.
202 */
203 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
204
reed@google.com9d07fec2011-03-16 20:02:59 +0000205 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000206 return SkToBool(this->getFlags() & kAutoHinting_Flag);
207 }
208
209 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
210 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
211 paint's flags,
212 false to clear it.
213 */
214 void setAutohinted(bool useAutohinter);
215
reed@google.com830a23e2011-11-10 15:20:49 +0000216 bool isVerticalText() const {
217 return SkToBool(this->getFlags() & kVerticalText_Flag);
218 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000219
reed@google.com830a23e2011-11-10 15:20:49 +0000220 /**
221 * Helper for setting or clearing the kVerticalText_Flag bit in
222 * setFlags(...).
223 *
224 * If this bit is set, then advances are treated as Y values rather than
225 * X values, and drawText will places its glyphs vertically rather than
226 * horizontally.
227 */
228 void setVerticalText(bool);
229
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
231 @return true if the underlineText bit is set in the paint's flags.
232 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000233 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 return SkToBool(this->getFlags() & kUnderlineText_Flag);
235 }
236
237 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
238 @param underlineText true to set the underlineText bit in the paint's
239 flags, false to clear it.
240 */
241 void setUnderlineText(bool underlineText);
242
243 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
244 @return true if the strikeThruText bit is set in the paint's flags.
245 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000246 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
248 }
249
250 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
251 @param strikeThruText true to set the strikeThruText bit in the
252 paint's flags, false to clear it.
253 */
254 void setStrikeThruText(bool strikeThruText);
255
256 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
257 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
258 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000259 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
261 }
262
263 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
264 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
265 flags, false to clear it.
266 */
267 void setFakeBoldText(bool fakeBoldText);
268
269 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
270 @return true if the kernText bit is set in the paint's flags.
271 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000272 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 return SkToBool(this->getFlags() & kDevKernText_Flag);
274 }
275
276 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
277 @param kernText true to set the kKernText_Flag bit in the paint's
278 flags, false to clear it.
279 */
280 void setDevKernText(bool devKernText);
281
reedf803da12015-01-23 05:58:07 -0800282 /**
283 * Return the filter level. This affects the quality (and performance) of
284 * drawing scaled images.
285 */
286 SkFilterQuality getFilterQuality() const {
287 return (SkFilterQuality)fBitfields.fFilterQuality;
288 }
mtkleinfe81e2d2015-09-09 07:35:42 -0700289
reedf803da12015-01-23 05:58:07 -0800290 /**
291 * Set the filter quality. This affects the quality (and performance) of
292 * drawing scaled images.
293 */
294 void setFilterQuality(SkFilterQuality quality);
reed@google.comc9683152013-07-18 13:47:01 +0000295
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 /** Styles apply to rect, oval, path, and text.
297 Bitmaps are always drawn in "fill", and lines are always drawn in
298 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000299
reed@android.comed881c22009-09-15 14:10:42 +0000300 Note: strokeandfill implicitly draws the result with
301 SkPath::kWinding_FillType, so if the original path is even-odd, the
302 results may not appear the same as if it was drawn twice, filled and
303 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 */
305 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000306 kFill_Style, //!< fill the geometry
307 kStroke_Style, //!< stroke the geometry
308 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000309 };
310 enum {
311 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 };
313
314 /** Return the paint's style, used for controlling how primitives'
315 geometries are interpreted (except for drawBitmap, which always assumes
316 kFill_Style).
317 @return the paint's Style
318 */
reedf59eab22014-07-14 14:39:15 -0700319 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320
321 /** Set the paint's style, used for controlling how primitives'
322 geometries are interpreted (except for drawBitmap, which always assumes
323 Fill).
324 @param style The new style to set in the paint
325 */
326 void setStyle(Style style);
327
328 /** Return the paint's color. Note that the color is a 32bit value
329 containing alpha as well as r,g,b. This 32bit value is not
330 premultiplied, meaning that its alpha can be any value, regardless of
331 the values of r,g,b.
332 @return the paint's color (and alpha).
333 */
334 SkColor getColor() const { return fColor; }
335
336 /** Set the paint's color. Note that the color is a 32bit value containing
337 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
338 that its alpha can be any value, regardless of the values of r,g,b.
339 @param color The new color (including alpha) to set in the paint.
340 */
341 void setColor(SkColor color);
342
343 /** Helper to getColor() that just returns the color's alpha value.
344 @return the alpha component of the paint's color.
345 */
346 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000347
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 /** Helper to setColor(), that only assigns the color's alpha value,
349 leaving its r,g,b values unchanged.
350 @param a set the alpha component (0..255) of the paint's color.
351 */
352 void setAlpha(U8CPU a);
353
354 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
355 using SkColorSetARGB()
356 @param a The new alpha component (0..255) of the paint's color.
357 @param r The new red component (0..255) of the paint's color.
358 @param g The new green component (0..255) of the paint's color.
359 @param b The new blue component (0..255) of the paint's color.
360 */
361 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
362
reed@google.com9d07fec2011-03-16 20:02:59 +0000363 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 <p />
365 A value of 0 strokes in hairline mode.
366 Hairlines always draw 1-pixel wide, regardless of the matrix.
367 @return the paint's stroke width, used whenever the paint's style is
368 Stroke or StrokeAndFill.
369 */
370 SkScalar getStrokeWidth() const { return fWidth; }
371
reed@google.com9d07fec2011-03-16 20:02:59 +0000372 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 Pass 0 to stroke in hairline mode.
374 Hairlines always draw 1-pixel wide, regardless of the matrix.
375 @param width set the paint's stroke width, used whenever the paint's
376 style is Stroke or StrokeAndFill.
377 */
378 void setStrokeWidth(SkScalar width);
379
380 /** Return the paint's stroke miter value. This is used to control the
381 behavior of miter joins when the joins angle is sharp.
382 @return the paint's miter limit, used whenever the paint's style is
383 Stroke or StrokeAndFill.
384 */
385 SkScalar getStrokeMiter() const { return fMiterLimit; }
386
387 /** Set the paint's stroke miter value. This is used to control the
388 behavior of miter joins when the joins angle is sharp. This value must
389 be >= 0.
390 @param miter set the miter limit on the paint, used whenever the
391 paint's style is Stroke or StrokeAndFill.
392 */
393 void setStrokeMiter(SkScalar miter);
394
395 /** Cap enum specifies the settings for the paint's strokecap. This is the
396 treatment that is applied to the beginning and end of each non-closed
397 contour (e.g. lines).
caryclark5cb00a92015-08-26 09:04:55 -0700398
399 If the cap is round or square, the caps are drawn when the contour has
400 a zero length. Zero length contours can be created by following moveTo
401 with a lineTo at the same point, or a moveTo followed by a close.
402
403 A dash with an on interval of zero also creates a zero length contour.
404
405 The zero length contour draws the square cap without rotation, since
406 the no direction can be inferred.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 */
408 enum Cap {
409 kButt_Cap, //!< begin/end contours with no extension
410 kRound_Cap, //!< begin/end contours with a semi-circle extension
411 kSquare_Cap, //!< begin/end contours with a half square extension
412
bsalomona7d85ba2016-07-06 11:54:59 -0700413 kLast_Cap = kSquare_Cap,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 kDefault_Cap = kButt_Cap
415 };
bsalomona7d85ba2016-07-06 11:54:59 -0700416 static constexpr int kCapCount = kLast_Cap + 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417
418 /** Join enum specifies the settings for the paint's strokejoin. This is
419 the treatment that is applied to corners in paths and rectangles.
420 */
421 enum Join {
422 kMiter_Join, //!< connect path segments with a sharp join
423 kRound_Join, //!< connect path segments with a round join
424 kBevel_Join, //!< connect path segments with a flat bevel join
425
bsalomona7d85ba2016-07-06 11:54:59 -0700426 kLast_Join = kBevel_Join,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 kDefault_Join = kMiter_Join
428 };
bsalomona7d85ba2016-07-06 11:54:59 -0700429 static constexpr int kJoinCount = kLast_Join + 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430
431 /** Return the paint's stroke cap type, controlling how the start and end
432 of stroked lines and paths are treated.
433 @return the line cap style for the paint, used whenever the paint's
434 style is Stroke or StrokeAndFill.
435 */
reedf59eab22014-07-14 14:39:15 -0700436 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437
438 /** Set the paint's stroke cap type.
439 @param cap set the paint's line cap style, used whenever the paint's
440 style is Stroke or StrokeAndFill.
441 */
442 void setStrokeCap(Cap cap);
443
444 /** Return the paint's stroke join type.
445 @return the paint's line join style, used whenever the paint's style is
446 Stroke or StrokeAndFill.
447 */
reedf59eab22014-07-14 14:39:15 -0700448 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449
450 /** Set the paint's stroke join type.
451 @param join set the paint's line join style, used whenever the paint's
452 style is Stroke or StrokeAndFill.
453 */
454 void setStrokeJoin(Join join);
455
reed@google.com4bbdeac2013-01-24 21:03:11 +0000456 /**
457 * Applies any/all effects (patheffect, stroking) to src, returning the
458 * result in dst. The result is that drawing src with this paint will be
459 * the same as drawing dst with a default paint (at least from the
460 * geometric perspective).
461 *
462 * @param src input path
463 * @param dst output path (may be the same as src)
464 * @param cullRect If not null, the dst path may be culled to this rect.
reed05d90442015-02-12 13:35:52 -0800465 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision
466 * in favor of speed/size.
reed@google.com4bbdeac2013-01-24 21:03:11 +0000467 * @return true if the path should be filled, or false if it should be
468 * drawn with a hairline (width == 0)
469 */
reed05d90442015-02-12 13:35:52 -0800470 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
471 SkScalar resScale = 1) const;
472
473 bool getFillPath(const SkPath& src, SkPath* dst) const {
474 return this->getFillPath(src, dst, NULL, 1);
475 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477 /** Get the paint's shader object.
478 <p />
479 The shader's reference count is not affected.
480 @return the paint's shader (or NULL)
481 */
reeda5ab9ec2016-03-06 18:10:48 -0800482 SkShader* getShader() const { return fShader.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483
484 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000485 * Shaders specify the source color(s) for what is being drawn. If a paint
486 * has no shader, then the paint's color is used. If the paint has a
487 * shader, then the shader's color(s) are use instead, but they are
488 * modulated by the paint's alpha. This makes it easy to create a shader
489 * once (e.g. bitmap tiling or gradient) and then change its transparency
490 * w/o having to modify the original shader... only the paint's alpha needs
491 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000492 *
493 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
494 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
495 * then the shader will use the paint's entire color to "colorize" its output (modulating the
496 * bitmap's alpha with the paint's color+alpha).
497 *
reed@google.com880dc472012-05-11 14:47:03 +0000498 * Pass NULL to clear any previous shader.
499 * As a convenience, the parameter passed is also returned.
500 * If a previous shader exists, its reference count is decremented.
501 * If shader is not NULL, its reference count is incremented.
502 * @param shader May be NULL. The shader to be installed in the paint
503 * @return shader
504 */
reeda5ab9ec2016-03-06 18:10:48 -0800505 void setShader(sk_sp<SkShader>);
reedfe630452016-03-25 09:08:00 -0700506#ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR
507 SkShader* setShader(SkShader* shader);
508#endif
reed@google.com9d07fec2011-03-16 20:02:59 +0000509
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 /** Get the paint's colorfilter. If there is a colorfilter, its reference
511 count is not changed.
512 @return the paint's colorfilter (or NULL)
513 */
reeda5ab9ec2016-03-06 18:10:48 -0800514 SkColorFilter* getColorFilter() const { return fColorFilter.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515
516 /** Set or clear the paint's colorfilter, returning the parameter.
517 <p />
518 If the paint already has a filter, its reference count is decremented.
519 If filter is not NULL, its reference count is incremented.
520 @param filter May be NULL. The filter to be installed in the paint
521 @return filter
522 */
reedd053ce92016-03-22 10:17:23 -0700523#ifdef SK_SUPPORT_LEGACY_COLORFILTER_PTR
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524 SkColorFilter* setColorFilter(SkColorFilter* filter);
reedd053ce92016-03-22 10:17:23 -0700525#endif
reeda5ab9ec2016-03-06 18:10:48 -0800526 void setColorFilter(sk_sp<SkColorFilter>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527
528 /** Get the paint's xfermode object.
529 <p />
530 The xfermode's reference count is not affected.
531 @return the paint's xfermode (or NULL)
532 */
reeda5ab9ec2016-03-06 18:10:48 -0800533 SkXfermode* getXfermode() const { return fXfermode.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000534
535 /** Set or clear the xfermode object.
536 <p />
537 Pass NULL to clear any previous xfermode.
538 As a convenience, the parameter passed is also returned.
539 If a previous xfermode exists, its reference count is decremented.
540 If xfermode is not NULL, its reference count is incremented.
541 @param xfermode May be NULL. The new xfermode to be installed in the
542 paint
543 @return xfermode
544 */
reeda5ab9ec2016-03-06 18:10:48 -0800545 void setXfermode(sk_sp<SkXfermode>);
reedcfb6bdf2016-03-29 11:32:50 -0700546#ifdef SK_SUPPORT_LEGACY_XFERMODE_PTR
547 SkXfermode* setXfermode(SkXfermode* xfermode);
548#endif
reed@android.coma0f5d152009-06-22 17:38:10 +0000549
550 /** Create an xfermode based on the specified Mode, and assign it into the
551 paint, returning the mode that was set. If the Mode is SrcOver, then
552 the paint's xfermode is set to null.
553 */
reed@android.com0baf1932009-06-24 12:41:42 +0000554 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000555
reed@android.com8a1c16f2008-12-17 15:59:43 +0000556 /** Get the paint's patheffect object.
557 <p />
558 The patheffect reference count is not affected.
559 @return the paint's patheffect (or NULL)
560 */
reeda5ab9ec2016-03-06 18:10:48 -0800561 SkPathEffect* getPathEffect() const { return fPathEffect.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562
563 /** Set or clear the patheffect object.
564 <p />
565 Pass NULL to clear any previous patheffect.
566 As a convenience, the parameter passed is also returned.
567 If a previous patheffect exists, its reference count is decremented.
568 If patheffect is not NULL, its reference count is incremented.
569 @param effect May be NULL. The new patheffect to be installed in the
570 paint
571 @return effect
572 */
reeda5ab9ec2016-03-06 18:10:48 -0800573 void setPathEffect(sk_sp<SkPathEffect>);
reeda4393342016-03-18 11:22:57 -0700574#ifdef SK_SUPPORT_LEGACY_PATHEFFECT_PTR
575 SkPathEffect* setPathEffect(SkPathEffect* effect);
576#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000577
578 /** Get the paint's maskfilter object.
579 <p />
580 The maskfilter reference count is not affected.
581 @return the paint's maskfilter (or NULL)
582 */
reeda5ab9ec2016-03-06 18:10:48 -0800583 SkMaskFilter* getMaskFilter() const { return fMaskFilter.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000584
585 /** Set or clear the maskfilter object.
586 <p />
587 Pass NULL to clear any previous maskfilter.
588 As a convenience, the parameter passed is also returned.
589 If a previous maskfilter exists, its reference count is decremented.
590 If maskfilter is not NULL, its reference count is incremented.
591 @param maskfilter May be NULL. The new maskfilter to be installed in
592 the paint
593 @return maskfilter
594 */
reedefdfd512016-04-04 10:02:58 -0700595#ifdef SK_SUPPORT_LEGACY_MASKFILTER_PTR
reed@android.com8a1c16f2008-12-17 15:59:43 +0000596 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
reedefdfd512016-04-04 10:02:58 -0700597#endif
reeda5ab9ec2016-03-06 18:10:48 -0800598 void setMaskFilter(sk_sp<SkMaskFilter>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599
600 // These attributes are for text/fonts
601
602 /** Get the paint's typeface object.
603 <p />
604 The typeface object identifies which font to use when drawing or
605 measuring text. The typeface reference count is not affected.
606 @return the paint's typeface (or NULL)
607 */
reeda5ab9ec2016-03-06 18:10:48 -0800608 SkTypeface* getTypeface() const { return fTypeface.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000609
610 /** Set or clear the typeface object.
611 <p />
612 Pass NULL to clear any previous typeface.
613 As a convenience, the parameter passed is also returned.
614 If a previous typeface exists, its reference count is decremented.
615 If typeface is not NULL, its reference count is incremented.
616 @param typeface May be NULL. The new typeface to be installed in the
617 paint
618 @return typeface
619 */
scroggo9a9a7b22016-05-12 06:22:30 -0700620 void setTypeface(sk_sp<SkTypeface>);
bungeman13b9c952016-05-12 10:09:30 -0700621#ifdef SK_SUPPORT_LEGACY_TYPEFACE_PTR
622 SkTypeface* setTypeface(SkTypeface* typeface);
623#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000624
625 /** Get the paint's rasterizer (or NULL).
626 <p />
627 The raster controls how paths/text are turned into alpha masks.
628 @return the paint's rasterizer (or NULL)
629 */
reeda5ab9ec2016-03-06 18:10:48 -0800630 SkRasterizer* getRasterizer() const { return fRasterizer.get(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000631
632 /** Set or clear the rasterizer object.
633 <p />
634 Pass NULL to clear any previous rasterizer.
635 As a convenience, the parameter passed is also returned.
636 If a previous rasterizer exists in the paint, its reference count is
637 decremented. If rasterizer is not NULL, its reference count is
638 incremented.
639 @param rasterizer May be NULL. The new rasterizer to be installed in
640 the paint.
641 @return rasterizer
642 */
reed7b380d02016-03-21 13:25:16 -0700643#ifdef SK_SUPPORT_LEGACY_MINOR_EFFECT_PTR
reed@android.com8a1c16f2008-12-17 15:59:43 +0000644 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
reed7b380d02016-03-21 13:25:16 -0700645#endif
reeda5ab9ec2016-03-06 18:10:48 -0800646 void setRasterizer(sk_sp<SkRasterizer>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647
reeda5ab9ec2016-03-06 18:10:48 -0800648 SkImageFilter* getImageFilter() const { return fImageFilter.get(); }
reed@google.com15356a62011-11-03 19:29:08 +0000649 SkImageFilter* setImageFilter(SkImageFilter*);
reeda5ab9ec2016-03-06 18:10:48 -0800650 void setImageFilter(sk_sp<SkImageFilter>);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000651
reed@google.comb0a34d82012-07-11 19:57:55 +0000652 /**
reed@google.com9d07fec2011-03-16 20:02:59 +0000653 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
654 * reference count.
655 */
reeda5ab9ec2016-03-06 18:10:48 -0800656 SkDrawLooper* getLooper() const { return fLooper.get(); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000657
658 /**
659 * Set or clear the looper object.
660 * <p />
661 * Pass NULL to clear any previous looper.
662 * As a convenience, the parameter passed is also returned.
663 * If a previous looper exists in the paint, its reference count is
664 * decremented. If looper is not NULL, its reference count is
665 * incremented.
666 * @param looper May be NULL. The new looper to be installed in the paint.
667 * @return looper
668 */
reed7b380d02016-03-21 13:25:16 -0700669#ifdef SK_SUPPORT_LEGACY_MINOR_EFFECT_PTR
reed@google.com9d07fec2011-03-16 20:02:59 +0000670 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed7b380d02016-03-21 13:25:16 -0700671#endif
reeda5ab9ec2016-03-06 18:10:48 -0800672 void setLooper(sk_sp<SkDrawLooper>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000673
674 enum Align {
675 kLeft_Align,
676 kCenter_Align,
677 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000678 };
679 enum {
680 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000681 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000682
reed@android.com8a1c16f2008-12-17 15:59:43 +0000683 /** Return the paint's Align value for drawing text.
684 @return the paint's Align value for drawing text.
685 */
reedf59eab22014-07-14 14:39:15 -0700686 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000687
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688 /** Set the paint's text alignment.
689 @param align set the paint's Align value for drawing text.
690 */
691 void setTextAlign(Align align);
692
693 /** Return the paint's text size.
694 @return the paint's text size.
695 */
696 SkScalar getTextSize() const { return fTextSize; }
697
698 /** Set the paint's text size. This value must be > 0
699 @param textSize set the paint's text size.
700 */
701 void setTextSize(SkScalar textSize);
702
703 /** Return the paint's horizontal scale factor for text. The default value
704 is 1.0.
705 @return the paint's scale factor in X for drawing/measuring text
706 */
707 SkScalar getTextScaleX() const { return fTextScaleX; }
708
709 /** Set the paint's horizontal scale factor for text. The default value
710 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
711 stretch the text narrower.
712 @param scaleX set the paint's scale factor in X for drawing/measuring
713 text.
714 */
715 void setTextScaleX(SkScalar scaleX);
716
717 /** Return the paint's horizontal skew factor for text. The default value
718 is 0.
719 @return the paint's skew factor in X for drawing text.
720 */
721 SkScalar getTextSkewX() const { return fTextSkewX; }
722
723 /** Set the paint's horizontal skew factor for text. The default value
724 is 0. For approximating oblique text, use values around -0.25.
725 @param skewX set the paint's skew factor in X for drawing text.
726 */
727 void setTextSkewX(SkScalar skewX);
728
729 /** Describes how to interpret the text parameters that are passed to paint
730 methods like measureText() and getTextWidths().
731 */
732 enum TextEncoding {
733 kUTF8_TextEncoding, //!< the text parameters are UTF8
734 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000735 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000736 kGlyphID_TextEncoding //!< the text parameters are glyph indices
737 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000738
reedf59eab22014-07-14 14:39:15 -0700739 TextEncoding getTextEncoding() const {
740 return (TextEncoding)fBitfields.fTextEncoding;
741 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000742
743 void setTextEncoding(TextEncoding encoding);
744
745 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000746 /** Flags which indicate the confidence level of various metrics.
747 A set flag indicates that the metric may be trusted.
748 */
749 enum FontMetricsFlags {
750 kUnderlineThinknessIsValid_Flag = 1 << 0,
751 kUnderlinePositionIsValid_Flag = 1 << 1,
752 };
753
754 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000755 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
756 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
757 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
758 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
759 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000760 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
761 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000762 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
763 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000764 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
765 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000766 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
767
768 /** Underline Position - position of the top of the Underline stroke
769 relative to the baseline, this can have following values
770 - Negative - means underline should be drawn above baseline.
771 - Positive - means below baseline.
772 - Zero - mean underline should be drawn on baseline.
773 */
774 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
775
776 /** If the fontmetrics has a valid underlinethickness, return true, and set the
777 thickness param to that value. If it doesn't return false and ignore the
778 thickness param.
779 */
780 bool hasUnderlineThickness(SkScalar* thickness) const {
781 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
782 *thickness = fUnderlineThickness;
783 return true;
784 }
785 return false;
786 }
787
788 /** If the fontmetrics has a valid underlineposition, return true, and set the
789 thickness param to that value. If it doesn't return false and ignore the
790 thickness param.
791 */
792 bool hasUnderlinePosition(SkScalar* position) const {
793 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
794 *position = fUnderlinePosition;
795 return true;
796 }
797 return false;
798 }
799
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000801
reed@android.com8a1c16f2008-12-17 15:59:43 +0000802 /** Return the recommend spacing between lines (which will be
803 fDescent - fAscent + fLeading).
804 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000805 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000806 @param metrics If not null, returns the font metrics for the
807 current typeface/pointsize/etc setting in this
808 paint.
809 @param scale If not 0, return width as if the canvas were scaled
810 by this value
811 @param return the recommended spacing between lines
812 */
813 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000814
reed@android.com8a1c16f2008-12-17 15:59:43 +0000815 /** Return the recommend line spacing. This will be
816 fDescent - fAscent + fLeading
817 */
818 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
819
820 /** Convert the specified text into glyph IDs, returning the number of
821 glyphs ID written. If glyphs is NULL, it is ignore and only the count
822 is returned.
823 */
824 int textToGlyphs(const void* text, size_t byteLength,
halcanaryd0e95a52016-07-25 07:18:12 -0700825 SkGlyphID glyphs[]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000826
reed@android.coma5dcaf62010-02-05 17:12:32 +0000827 /** Return true if all of the specified text has a corresponding non-zero
828 glyph ID. If any of the code-points in the text are not supported in
829 the typeface (i.e. the glyph ID would be zero), then return false.
830
831 If the text encoding for the paint is kGlyph_TextEncoding, then this
832 returns true if all of the specified glyph IDs are non-zero.
833 */
834 bool containsText(const void* text, size_t byteLength) const;
835
reed@android.com9d3a9852010-01-08 14:07:42 +0000836 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
837 to zero. Note: this does not look at the text-encoding setting in the
838 paint, only at the typeface.
839 */
halcanaryd0e95a52016-07-25 07:18:12 -0700840 void glyphsToUnichars(const SkGlyphID glyphs[], int count, SkUnichar text[]) const;
reed@android.com9d3a9852010-01-08 14:07:42 +0000841
reed@android.com8a1c16f2008-12-17 15:59:43 +0000842 /** Return the number of drawable units in the specified text buffer.
843 This looks at the current TextEncoding field of the paint. If you also
844 want to have the text converted into glyph IDs, call textToGlyphs
845 instead.
846 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000847 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848 return this->textToGlyphs(text, byteLength, NULL);
849 }
850
reed@google.com44da42e2011-11-10 20:04:47 +0000851 /** Return the width of the text. This will return the vertical measure
852 * if isVerticalText() is true, in which case the returned value should
853 * be treated has a height instead of a width.
854 *
855 * @param text The text to be measured
856 * @param length Number of bytes of text to measure
857 * @param bounds If not NULL, returns the bounds of the text,
858 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000859 * @return The advance width of the text
860 */
reed99ae8812014-08-26 11:30:01 -0700861 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862
reed@google.com44da42e2011-11-10 20:04:47 +0000863 /** Return the width of the text. This will return the vertical measure
864 * if isVerticalText() is true, in which case the returned value should
865 * be treated has a height instead of a width.
866 *
867 * @param text Address of the text
868 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000869 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000870 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000871 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700872 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000873 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000874
reed@google.com44da42e2011-11-10 20:04:47 +0000875 /** Return the number of bytes of text that were measured. If
876 * isVerticalText() is true, then the vertical advances are used for
877 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000878 *
reed@google.com44da42e2011-11-10 20:04:47 +0000879 * @param text The text to be measured
880 * @param length Number of bytes of text to measure
881 * @param maxWidth Maximum width. Only the subset of text whose accumulated
882 * widths are <= maxWidth are measured.
883 * @param measuredWidth Optional. If non-null, this returns the actual
884 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000885 * @return The number of bytes of text that were measured. Will be
886 * <= length.
887 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000888 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700889 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890
reed@google.com44da42e2011-11-10 20:04:47 +0000891 /** Return the advances for the text. These will be vertical advances if
892 * isVerticalText() returns true.
893 *
894 * @param text the text
895 * @param byteLength number of bytes to of text
896 * @param widths If not null, returns the array of advances for
897 * the glyphs. If not NULL, must be at least a large
898 * as the number of unichars in the specified text.
899 * @param bounds If not null, returns the bounds for each of
900 * character, relative to (0, 0)
901 * @return the number of unichars in the specified text.
902 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
904 SkRect bounds[] = NULL) const;
905
906 /** Return the path (outline) for the specified text.
caryclark0449bcf2016-02-09 13:25:45 -0800907 * Note: just like SkCanvas::drawText, this will respect the Align setting
908 * in the paint.
909 *
910 * @param text the text
911 * @param length number of bytes of text
912 * @param x The x-coordinate of the origin of the text.
913 * @param y The y-coordinate of the origin of the text.
914 * @param path The outline of the text.
915 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000916 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
917 SkPath* path) const;
918
caryclark0449bcf2016-02-09 13:25:45 -0800919 /** Return the path (outline) for the specified text.
920 * Note: just like SkCanvas::drawText, this will respect the Align setting
921 * in the paint.
922 *
923 * @param text the text
924 * @param length number of bytes of text
925 * @param pos array of positions, used to position each character
926 * @param path The outline of the text.
927 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000928 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000929 const SkPoint pos[], SkPath* path) const;
930
caryclark0449bcf2016-02-09 13:25:45 -0800931 /** Return the number of intervals that intersect the intercept along the axis of the advance.
932 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
933 * the string. The caller may pass nullptr for intervals to determine the size of the interval
934 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
935 * intervals are cached by glyph to improve performance for multiple calls.
936 * This permits constructing an underline that skips the descenders.
937 *
938 * @param text the text
939 * @param length number of bytes of text
940 * @param x The x-coordinate of the origin of the text.
941 * @param y The y-coordinate of the origin of the text.
942 * @param bounds The lower and upper line parallel to the advance.
943 * @param array If not null, the found intersections.
944 *
945 * @return The number of intersections, which may be zero.
946 */
947 int getTextIntercepts(const void* text, size_t length, SkScalar x, SkScalar y,
948 const SkScalar bounds[2], SkScalar* intervals) const;
949
950 /** Return the number of intervals that intersect the intercept along the axis of the advance.
951 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
952 * string. The caller may pass nullptr for intervals to determine the size of the interval
953 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
954 * intervals are cached by glyph to improve performance for multiple calls.
955 * This permits constructing an underline that skips the descenders.
956 *
957 * @param text the text
958 * @param length number of bytes of text
959 * @param pos array of positions, used to position each character
960 * @param bounds The lower and upper line parallel to the advance.
961 * @param array If not null, the glyph bounds contained by the advance parallel lines.
962 *
963 * @return The number of intersections, which may be zero.
964 */
965 int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[],
966 const SkScalar bounds[2], SkScalar* intervals) const;
967
fmalitaeae6a912016-07-28 09:47:24 -0700968 /** Return the number of intervals that intersect the intercept along the axis of the advance.
969 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
970 * string. The caller may pass nullptr for intervals to determine the size of the interval
971 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
972 * intervals are cached by glyph to improve performance for multiple calls.
973 * This permits constructing an underline that skips the descenders.
974 *
975 * @param text The text.
976 * @param length Number of bytes of text.
977 * @param xpos Array of x-positions, used to position each character.
978 * @param constY The shared Y coordinate for all of the positions.
979 * @param bounds The lower and upper line parallel to the advance.
980 * @param array If not null, the glyph bounds contained by the advance parallel lines.
981 *
982 * @return The number of intersections, which may be zero.
983 */
984 int getPosTextHIntercepts(const void* text, size_t length, const SkScalar xpos[],
985 SkScalar constY, const SkScalar bounds[2], SkScalar* intervals) const;
986
987 /** Return the number of intervals that intersect the intercept along the axis of the advance.
988 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
989 * text blob. The caller may pass nullptr for intervals to determine the size of the interval
990 * array. The computed intervals are cached by glyph to improve performance for multiple calls.
991 * This permits constructing an underline that skips the descenders.
992 *
993 * @param blob The text blob.
994 * @param bounds The lower and upper line parallel to the advance.
995 * @param array If not null, the glyph bounds contained by the advance parallel lines.
996 *
997 * @return The number of intersections, which may be zero.
998 */
999 int getTextBlobIntercepts(const SkTextBlob* blob, const SkScalar bounds[2],
1000 SkScalar* intervals) const;
1001
reed8893e5f2014-12-15 13:27:26 -08001002 /**
1003 * Return a rectangle that represents the union of the bounds of all
1004 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
1005 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
1006 * on this paint.
1007 */
1008 SkRect getFontBounds() const;
1009
reed@google.com632e1a22011-10-06 12:37:00 +00001010 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
1011 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
1012 bool nothingToDraw() const;
1013
reed@google.coma584aed2012-05-16 14:06:02 +00001014 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +00001015 // would prefer to make these private...
1016
reed@google.coma584aed2012-05-16 14:06:02 +00001017 /** Returns true if the current paint settings allow for fast computation of
1018 bounds (i.e. there is nothing complex like a patheffect that would make
1019 the bounds computation expensive.
1020 */
senorblanco0abdf762015-08-20 11:10:41 -07001021 bool canComputeFastBounds() const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001022
reed@google.coma584aed2012-05-16 14:06:02 +00001023 /** Only call this if canComputeFastBounds() returned true. This takes a
1024 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
1025 effects in the paint (e.g. stroking). If needed, it uses the storage
1026 rect parameter. It returns the adjusted bounds that can then be used
1027 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001028
reed@google.coma584aed2012-05-16 14:06:02 +00001029 The returned rect will either be orig or storage, thus the caller
1030 should not rely on storage being set to the result, but should always
1031 use the retured value. It is legal for orig and storage to be the same
1032 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001033
reed@google.coma584aed2012-05-16 14:06:02 +00001034 e.g.
1035 if (paint.canComputeFastBounds()) {
1036 SkRect r, storage;
1037 path.computeBounds(&r, SkPath::kFast_BoundsType);
1038 const SkRect& fastR = paint.computeFastBounds(r, &storage);
1039 if (canvas->quickReject(fastR, ...)) {
1040 // don't draw the path
1041 }
1042 }
1043 */
1044 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
1045 SkPaint::Style style = this->getStyle();
1046 // ultra fast-case: filling with no effects that affect geometry
1047 if (kFill_Style == style) {
1048 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
1049 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
1050 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +00001051 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +00001052 if (!effects) {
1053 return orig;
1054 }
1055 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001056
reed@google.coma584aed2012-05-16 14:06:02 +00001057 return this->doComputeFastBounds(orig, storage, style);
1058 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001059
reed@google.coma584aed2012-05-16 14:06:02 +00001060 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1061 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001062 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001063 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001064
reed@google.coma584aed2012-05-16 14:06:02 +00001065 // Take the style explicitly, so the caller can force us to be stroked
1066 // without having to make a copy of the paint just to change that field.
1067 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1068 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001069
reed@google.comed43dff2013-06-04 16:56:27 +00001070 /**
1071 * Return a matrix that applies the paint's text values: size, scale, skew
1072 */
1073 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1074 SkScalar scaleX, SkScalar skewX) {
1075 matrix->setScale(size * scaleX, size);
1076 if (skewX) {
1077 matrix->postSkew(skewX, 0);
1078 }
1079 return matrix;
1080 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001081
reed@google.comed43dff2013-06-04 16:56:27 +00001082 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1083 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1084 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001085
benjaminwagnerd936f632016-02-23 10:44:31 -08001086 typedef const SkGlyph& (*GlyphCacheProc)(SkGlyphCache*, const char**);
1087
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001088 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001089
reed@google.comd5f20792012-05-16 14:15:02 +00001090private:
reeda5ab9ec2016-03-06 18:10:48 -08001091 sk_sp<SkTypeface> fTypeface;
1092 sk_sp<SkPathEffect> fPathEffect;
1093 sk_sp<SkShader> fShader;
1094 sk_sp<SkXfermode> fXfermode;
1095 sk_sp<SkMaskFilter> fMaskFilter;
1096 sk_sp<SkColorFilter> fColorFilter;
1097 sk_sp<SkRasterizer> fRasterizer;
1098 sk_sp<SkDrawLooper> fLooper;
1099 sk_sp<SkImageFilter> fImageFilter;
reed@google.comd5f20792012-05-16 14:15:02 +00001100
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001101 SkScalar fTextSize;
1102 SkScalar fTextScaleX;
1103 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001104 SkColor fColor;
1105 SkScalar fWidth;
1106 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001107 union {
1108 struct {
1109 // all of these bitfields should add up to 32
1110 unsigned fFlags : 16;
1111 unsigned fTextAlign : 2;
1112 unsigned fCapType : 2;
1113 unsigned fJoinType : 2;
1114 unsigned fStyle : 2;
1115 unsigned fTextEncoding : 2; // 3 values
1116 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001117 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001118 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001119 } fBitfields;
1120 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001121 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001122
robertphillipse34f17d2016-07-19 07:59:22 -07001123 static GlyphCacheProc GetGlyphCacheProc(TextEncoding encoding,
1124 bool isDevKern,
1125 bool needFullMetrics);
reed@google.comd5f20792012-05-16 14:15:02 +00001126
1127 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1128 int* count, SkRect* bounds) const;
1129
brianosmana1e8f8d2016-04-08 06:47:54 -07001130 enum ScalerContextFlags : uint32_t {
1131 kNone_ScalerContextFlags = 0,
1132
1133 kFakeGamma_ScalerContextFlag = 1 << 0,
1134 kBoostContrast_ScalerContextFlag = 1 << 1,
1135
1136 kFakeGammaAndBoostContrast_ScalerContextFlags =
1137 kFakeGamma_ScalerContextFlag | kBoostContrast_ScalerContextFlag,
bungemanf6d1e602016-02-22 13:20:28 -08001138 };
1139
joshualittfd450792015-03-13 08:38:43 -07001140 /*
1141 * Allocs an SkDescriptor on the heap and return it to the caller as a refcnted
1142 * SkData. Caller is responsible for managing the lifetime of this object.
1143 */
reeda9322c22016-04-12 06:47:05 -07001144 void getScalerContextDescriptor(SkScalerContextEffects*, SkAutoDescriptor*,
1145 const SkSurfaceProps& surfaceProps,
brianosmana1e8f8d2016-04-08 06:47:54 -07001146 uint32_t scalerContextFlags, const SkMatrix*) const;
joshualittfd450792015-03-13 08:38:43 -07001147
brianosmana1e8f8d2016-04-08 06:47:54 -07001148 SkGlyphCache* detachCache(const SkSurfaceProps* surfaceProps, uint32_t scalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -08001149 const SkMatrix*) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001150
brianosmana1e8f8d2016-04-08 06:47:54 -07001151 void descriptorProc(const SkSurfaceProps* surfaceProps, uint32_t scalerContextFlags,
bungemanf6d1e602016-02-22 13:20:28 -08001152 const SkMatrix* deviceMatrix,
reeda9322c22016-04-12 06:47:05 -07001153 void (*proc)(SkTypeface*, const SkScalerContextEffects&,
1154 const SkDescriptor*, void*),
bungemanf6d1e602016-02-22 13:20:28 -08001155 void* context) const;
reed@android.comd252db02009-04-01 18:31:44 +00001156
joshualitt9e36c1a2015-04-14 12:17:27 -07001157 /*
1158 * The luminance color is used to determine which Gamma Canonical color to map to. This is
1159 * really only used by backends which want to cache glyph masks, and need some way to know if
1160 * they need to generate new masks based off a given color.
1161 */
1162 SkColor computeLuminanceColor() const;
1163
reed@android.com8a1c16f2008-12-17 15:59:43 +00001164 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001165 /* This is the size we use when we ask for a glyph's path. We then
1166 * post-transform it as we draw to match the request.
1167 * This is done to try to re-use cache entries for the path.
1168 *
1169 * This value is somewhat arbitrary. In theory, it could be 1, since
1170 * we store paths as floats. However, we get the path from the font
1171 * scaler, and it may represent its paths as fixed-point (or 26.6),
1172 * so we shouldn't ask for something too big (might overflow 16.16)
1173 * or too small (underflow 26.6).
1174 *
1175 * This value could track kMaxSizeForGlyphCache, assuming the above
1176 * constraints, but since we ask for unhinted paths, the two values
1177 * need not match per-se.
1178 */
1179 kCanonicalTextSizeForPaths = 64,
1180
1181 /*
1182 * Above this size (taking into account CTM and textSize), we never use
1183 * the cache for bits or metrics (we might overflow), so we just ask
1184 * for a caononical size and post-transform that.
1185 */
1186 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001187 };
reed@google.comed43dff2013-06-04 16:56:27 +00001188
1189 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1190
reed@google.comed43dff2013-06-04 16:56:27 +00001191 // Set flags/hinting/textSize up to use for drawing text as paths.
1192 // Returns scale factor to restore the original textSize, since will will
1193 // have change it to kCanonicalTextSizeForPaths.
1194 SkScalar setupForAsPaths();
1195
1196 static SkScalar MaxCacheSize2() {
1197 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1198 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1199 return kMag2Max;
1200 }
1201
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001202 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001203 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 friend class SkCanvas;
1205 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001206 friend class SkPDFDevice;
joshualitte76b4bb2015-12-28 07:23:58 -08001207 friend class GrAtlasTextBlob;
joshualittdbd35932015-04-02 09:19:04 -07001208 friend class GrAtlasTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001209 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001210 friend class GrPathRendering;
joshualitt0a42e682015-12-10 13:20:58 -08001211 friend class GrTextUtils;
cdalton855d83f2014-09-18 13:51:53 -07001212 friend class GrGLPathRendering;
joshualitt9e36c1a2015-04-14 12:17:27 -07001213 friend class SkScalerContext;
caryclark0449bcf2016-02-09 13:25:45 -08001214 friend class SkTextBaseIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001215 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001216};
1217
reed@android.com8a1c16f2008-12-17 15:59:43 +00001218#endif