blob: cd170bc94f9f8fb209305e1232c1cfd449554ed9 [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
reed@google.comb0a34d82012-07-11 19:57:55 +000016class SkAnnotation;
joshualitt2b6acb42015-04-01 11:30:27 -070017class SkAutoDescriptor;
reed@android.com8a1c16f2008-12-17 15:59:43 +000018class SkAutoGlyphCache;
19class SkColorFilter;
joshualittfd450792015-03-13 08:38:43 -070020class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000021class SkDescriptor;
senorblanco0abdf762015-08-20 11:10:41 -070022class SkDrawLooper;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000023class SkReadBuffer;
24class SkWriteBuffer;
herbb69d0e02015-02-25 06:47:06 -080025class SkGlyph;
reed@android.com8a1c16f2008-12-17 15:59:43 +000026struct SkRect;
27class SkGlyphCache;
reed@google.com15356a62011-11-03 19:29:08 +000028class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029class SkMaskFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000030class SkPath;
31class SkPathEffect;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000032struct SkPoint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkRasterizer;
34class SkShader;
robertphillipsfcf78292015-06-19 11:49:52 -070035class SkSurfaceProps;
reed@android.com8a1c16f2008-12-17 15:59:43 +000036class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037
38typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
39 SkFixed x, SkFixed y);
40
41typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
42
humper@google.comb0889472013-07-09 21:37:14 +000043#define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag
44
reed@android.com8a1c16f2008-12-17 15:59:43 +000045/** \class SkPaint
46
47 The SkPaint class holds the style and color information about how to draw
48 geometries, text and bitmaps.
49*/
humper@google.comb0889472013-07-09 21:37:14 +000050
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000051class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052public:
53 SkPaint();
54 SkPaint(const SkPaint& paint);
bungemanccce0e02016-02-07 14:37:23 -080055 SkPaint(SkPaint&& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 ~SkPaint();
57
58 SkPaint& operator=(const SkPaint&);
bungemanccce0e02016-02-07 14:37:23 -080059 SkPaint& operator=(SkPaint&&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060
mtkleinbc97ef42014-08-25 10:10:47 -070061 /** operator== may give false negatives: two paints that draw equivalently
62 may return false. It will never give false positives: two paints that
63 are not equivalent always return false.
64 */
robertphillips@google.comb2657412013-08-07 22:36:29 +000065 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
66 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
67 return !(a == b);
68 }
69
mtkleinfb1fe4f2014-10-07 09:26:10 -070070 /** getHash() is a shallow hash, with the same limitations as operator==.
71 * If operator== returns true for two paints, getHash() returns the same value for each.
72 */
73 uint32_t getHash() const;
74
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000075 void flatten(SkWriteBuffer&) const;
76 void unflatten(SkReadBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
78 /** Restores the paint to its initial settings.
79 */
80 void reset();
81
agl@chromium.org309485b2009-07-21 17:41:32 +000082 /** Specifies the level of hinting to be performed. These names are taken
83 from the Gnome/Cairo names for the same. They are translated into
84 Freetype concepts the same as in cairo-ft-font.c:
85 kNo_Hinting -> FT_LOAD_NO_HINTING
86 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
87 kNormal_Hinting -> <default, no option>
88 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
89 subpixel glyphs, in which case TARGET_LCD or
90 TARGET_LCD_V is used>
91 */
92 enum Hinting {
93 kNo_Hinting = 0,
94 kSlight_Hinting = 1,
95 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000096 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000097 };
98
reed@google.com9d07fec2011-03-16 20:02:59 +000099 Hinting getHinting() const {
reedf59eab22014-07-14 14:39:15 -0700100 return static_cast<Hinting>(fBitfields.fHinting);
agl@chromium.org309485b2009-07-21 17:41:32 +0000101 }
102
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000103 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 /** Specifies the bit values that are stored in the paint's flags.
106 */
107 enum Flags {
108 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 kDither_Flag = 0x04, //!< mask to enable dithering
110 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
111 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
112 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
113 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000114 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000116 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000117 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000118 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000119 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000120 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
agl@chromium.org309485b2009-07-21 17:41:32 +0000121 // when adding extra flags, note that the fFlags member is specified
122 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123
humper@google.com387db0a2013-07-09 14:13:04 +0000124 kAllFlags = 0xFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 };
126
127 /** Return the paint's flags. Use the Flag enum to test flag values.
128 @return the paint's flags (see enums ending in _Flag for bit masks)
129 */
reedf59eab22014-07-14 14:39:15 -0700130 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131
132 /** Set the paint's flags. Use the Flag enum to specific flag values.
133 @param flags The new flag bits for the paint (see Flags enum)
134 */
135 void setFlags(uint32_t flags);
136
137 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
138 @return true if the antialias bit is set in the paint's flags.
139 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000140 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 return SkToBool(this->getFlags() & kAntiAlias_Flag);
142 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
145 @param aa true to enable antialiasing, false to disable it
146 */
147 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 /** Helper for getFlags(), returning true if kDither_Flag bit is set
150 @return true if the dithering bit is set in the paint's flags.
151 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000152 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 return SkToBool(this->getFlags() & kDither_Flag);
154 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
157 @param dither true to enable dithering, false to disable it
158 */
159 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
162 @return true if the lineartext bit is set in the paint's flags
163 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000164 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 return SkToBool(this->getFlags() & kLinearText_Flag);
166 }
167
168 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
169 @param linearText true to set the linearText bit in the paint's flags,
170 false to clear it.
171 */
172 void setLinearText(bool linearText);
173
174 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
175 @return true if the lineartext bit is set in the paint's flags
176 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000177 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 return SkToBool(this->getFlags() & kSubpixelText_Flag);
179 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000180
reed@google.com84b437e2011-08-01 12:45:35 +0000181 /**
182 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
183 * @param subpixelText true to set the subpixelText bit in the paint's
184 * flags, false to clear it.
185 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000187
reed@google.com9d07fec2011-03-16 20:02:59 +0000188 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000189 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
190 }
191
reed@google.com84b437e2011-08-01 12:45:35 +0000192 /**
193 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
194 * Note: antialiasing must also be on for lcd rendering
195 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
196 * false to clear it.
197 */
198 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000199
reed@google.com9d07fec2011-03-16 20:02:59 +0000200 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000201 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
202 }
203
204 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
205 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
206 false to clear it.
207 */
208 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
209
reed@google.com9d07fec2011-03-16 20:02:59 +0000210 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000211 return SkToBool(this->getFlags() & kAutoHinting_Flag);
212 }
213
214 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
215 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
216 paint's flags,
217 false to clear it.
218 */
219 void setAutohinted(bool useAutohinter);
220
reed@google.com830a23e2011-11-10 15:20:49 +0000221 bool isVerticalText() const {
222 return SkToBool(this->getFlags() & kVerticalText_Flag);
223 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000224
reed@google.com830a23e2011-11-10 15:20:49 +0000225 /**
226 * Helper for setting or clearing the kVerticalText_Flag bit in
227 * setFlags(...).
228 *
229 * If this bit is set, then advances are treated as Y values rather than
230 * X values, and drawText will places its glyphs vertically rather than
231 * horizontally.
232 */
233 void setVerticalText(bool);
234
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
236 @return true if the underlineText bit is set in the paint's flags.
237 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000238 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 return SkToBool(this->getFlags() & kUnderlineText_Flag);
240 }
241
242 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
243 @param underlineText true to set the underlineText bit in the paint's
244 flags, false to clear it.
245 */
246 void setUnderlineText(bool underlineText);
247
248 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
249 @return true if the strikeThruText bit is set in the paint's flags.
250 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000251 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
253 }
254
255 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
256 @param strikeThruText true to set the strikeThruText bit in the
257 paint's flags, false to clear it.
258 */
259 void setStrikeThruText(bool strikeThruText);
260
261 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
262 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
263 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000264 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
266 }
267
268 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
269 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
270 flags, false to clear it.
271 */
272 void setFakeBoldText(bool fakeBoldText);
273
274 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
275 @return true if the kernText bit is set in the paint's flags.
276 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000277 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 return SkToBool(this->getFlags() & kDevKernText_Flag);
279 }
280
281 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
282 @param kernText true to set the kKernText_Flag bit in the paint's
283 flags, false to clear it.
284 */
285 void setDevKernText(bool devKernText);
286
reedf803da12015-01-23 05:58:07 -0800287 /**
288 * Return the filter level. This affects the quality (and performance) of
289 * drawing scaled images.
290 */
291 SkFilterQuality getFilterQuality() const {
292 return (SkFilterQuality)fBitfields.fFilterQuality;
293 }
mtkleinfe81e2d2015-09-09 07:35:42 -0700294
reedf803da12015-01-23 05:58:07 -0800295 /**
296 * Set the filter quality. This affects the quality (and performance) of
297 * drawing scaled images.
298 */
299 void setFilterQuality(SkFilterQuality quality);
reed@google.comc9683152013-07-18 13:47:01 +0000300
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 /** Styles apply to rect, oval, path, and text.
302 Bitmaps are always drawn in "fill", and lines are always drawn in
303 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000304
reed@android.comed881c22009-09-15 14:10:42 +0000305 Note: strokeandfill implicitly draws the result with
306 SkPath::kWinding_FillType, so if the original path is even-odd, the
307 results may not appear the same as if it was drawn twice, filled and
308 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309 */
310 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000311 kFill_Style, //!< fill the geometry
312 kStroke_Style, //!< stroke the geometry
313 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000314 };
315 enum {
316 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 };
318
319 /** Return the paint's style, used for controlling how primitives'
320 geometries are interpreted (except for drawBitmap, which always assumes
321 kFill_Style).
322 @return the paint's Style
323 */
reedf59eab22014-07-14 14:39:15 -0700324 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325
326 /** Set the paint's style, used for controlling how primitives'
327 geometries are interpreted (except for drawBitmap, which always assumes
328 Fill).
329 @param style The new style to set in the paint
330 */
331 void setStyle(Style style);
332
333 /** Return the paint's color. Note that the color is a 32bit value
334 containing alpha as well as r,g,b. This 32bit value is not
335 premultiplied, meaning that its alpha can be any value, regardless of
336 the values of r,g,b.
337 @return the paint's color (and alpha).
338 */
339 SkColor getColor() const { return fColor; }
340
341 /** Set the paint's color. Note that the color is a 32bit value containing
342 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
343 that its alpha can be any value, regardless of the values of r,g,b.
344 @param color The new color (including alpha) to set in the paint.
345 */
346 void setColor(SkColor color);
347
348 /** Helper to getColor() that just returns the color's alpha value.
349 @return the alpha component of the paint's color.
350 */
351 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000352
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 /** Helper to setColor(), that only assigns the color's alpha value,
354 leaving its r,g,b values unchanged.
355 @param a set the alpha component (0..255) of the paint's color.
356 */
357 void setAlpha(U8CPU a);
358
359 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
360 using SkColorSetARGB()
361 @param a The new alpha component (0..255) of the paint's color.
362 @param r The new red component (0..255) of the paint's color.
363 @param g The new green component (0..255) of the paint's color.
364 @param b The new blue component (0..255) of the paint's color.
365 */
366 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
367
reed@google.com9d07fec2011-03-16 20:02:59 +0000368 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369 <p />
370 A value of 0 strokes in hairline mode.
371 Hairlines always draw 1-pixel wide, regardless of the matrix.
372 @return the paint's stroke width, used whenever the paint's style is
373 Stroke or StrokeAndFill.
374 */
375 SkScalar getStrokeWidth() const { return fWidth; }
376
reed@google.com9d07fec2011-03-16 20:02:59 +0000377 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000378 Pass 0 to stroke in hairline mode.
379 Hairlines always draw 1-pixel wide, regardless of the matrix.
380 @param width set the paint's stroke width, used whenever the paint's
381 style is Stroke or StrokeAndFill.
382 */
383 void setStrokeWidth(SkScalar width);
384
385 /** Return the paint's stroke miter value. This is used to control the
386 behavior of miter joins when the joins angle is sharp.
387 @return the paint's miter limit, used whenever the paint's style is
388 Stroke or StrokeAndFill.
389 */
390 SkScalar getStrokeMiter() const { return fMiterLimit; }
391
392 /** Set the paint's stroke miter value. This is used to control the
393 behavior of miter joins when the joins angle is sharp. This value must
394 be >= 0.
395 @param miter set the miter limit on the paint, used whenever the
396 paint's style is Stroke or StrokeAndFill.
397 */
398 void setStrokeMiter(SkScalar miter);
399
400 /** Cap enum specifies the settings for the paint's strokecap. This is the
401 treatment that is applied to the beginning and end of each non-closed
402 contour (e.g. lines).
caryclark5cb00a92015-08-26 09:04:55 -0700403
404 If the cap is round or square, the caps are drawn when the contour has
405 a zero length. Zero length contours can be created by following moveTo
406 with a lineTo at the same point, or a moveTo followed by a close.
407
408 A dash with an on interval of zero also creates a zero length contour.
409
410 The zero length contour draws the square cap without rotation, since
411 the no direction can be inferred.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 */
413 enum Cap {
414 kButt_Cap, //!< begin/end contours with no extension
415 kRound_Cap, //!< begin/end contours with a semi-circle extension
416 kSquare_Cap, //!< begin/end contours with a half square extension
417
418 kCapCount,
419 kDefault_Cap = kButt_Cap
420 };
421
422 /** Join enum specifies the settings for the paint's strokejoin. This is
423 the treatment that is applied to corners in paths and rectangles.
424 */
425 enum Join {
426 kMiter_Join, //!< connect path segments with a sharp join
427 kRound_Join, //!< connect path segments with a round join
428 kBevel_Join, //!< connect path segments with a flat bevel join
429
430 kJoinCount,
431 kDefault_Join = kMiter_Join
432 };
433
434 /** Return the paint's stroke cap type, controlling how the start and end
435 of stroked lines and paths are treated.
436 @return the line cap style for the paint, used whenever the paint's
437 style is Stroke or StrokeAndFill.
438 */
reedf59eab22014-07-14 14:39:15 -0700439 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440
441 /** Set the paint's stroke cap type.
442 @param cap set the paint's line cap style, used whenever the paint's
443 style is Stroke or StrokeAndFill.
444 */
445 void setStrokeCap(Cap cap);
446
447 /** Return the paint's stroke join type.
448 @return the paint's line join style, used whenever the paint's style is
449 Stroke or StrokeAndFill.
450 */
reedf59eab22014-07-14 14:39:15 -0700451 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452
453 /** Set the paint's stroke join type.
454 @param join set the paint's line join style, used whenever the paint's
455 style is Stroke or StrokeAndFill.
456 */
457 void setStrokeJoin(Join join);
458
reed@google.com4bbdeac2013-01-24 21:03:11 +0000459 /**
460 * Applies any/all effects (patheffect, stroking) to src, returning the
461 * result in dst. The result is that drawing src with this paint will be
462 * the same as drawing dst with a default paint (at least from the
463 * geometric perspective).
464 *
465 * @param src input path
466 * @param dst output path (may be the same as src)
467 * @param cullRect If not null, the dst path may be culled to this rect.
reed05d90442015-02-12 13:35:52 -0800468 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision
469 * in favor of speed/size.
reed@google.com4bbdeac2013-01-24 21:03:11 +0000470 * @return true if the path should be filled, or false if it should be
471 * drawn with a hairline (width == 0)
472 */
reed05d90442015-02-12 13:35:52 -0800473 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
474 SkScalar resScale = 1) const;
475
476 bool getFillPath(const SkPath& src, SkPath* dst) const {
477 return this->getFillPath(src, dst, NULL, 1);
478 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000479
reed@android.com8a1c16f2008-12-17 15:59:43 +0000480 /** Get the paint's shader object.
481 <p />
482 The shader's reference count is not affected.
483 @return the paint's shader (or NULL)
484 */
485 SkShader* getShader() const { return fShader; }
486
487 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000488 * Shaders specify the source color(s) for what is being drawn. If a paint
489 * has no shader, then the paint's color is used. If the paint has a
490 * shader, then the shader's color(s) are use instead, but they are
491 * modulated by the paint's alpha. This makes it easy to create a shader
492 * once (e.g. bitmap tiling or gradient) and then change its transparency
493 * w/o having to modify the original shader... only the paint's alpha needs
494 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000495 *
496 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
497 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
498 * then the shader will use the paint's entire color to "colorize" its output (modulating the
499 * bitmap's alpha with the paint's color+alpha).
500 *
reed@google.com880dc472012-05-11 14:47:03 +0000501 * Pass NULL to clear any previous shader.
502 * As a convenience, the parameter passed is also returned.
503 * If a previous shader exists, its reference count is decremented.
504 * If shader is not NULL, its reference count is incremented.
505 * @param shader May be NULL. The shader to be installed in the paint
506 * @return shader
507 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508 SkShader* setShader(SkShader* shader);
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 */
514 SkColorFilter* getColorFilter() const { return fColorFilter; }
515
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 */
523 SkColorFilter* setColorFilter(SkColorFilter* filter);
524
525 /** Get the paint's xfermode object.
526 <p />
527 The xfermode's reference count is not affected.
528 @return the paint's xfermode (or NULL)
529 */
530 SkXfermode* getXfermode() const { return fXfermode; }
531
532 /** Set or clear the xfermode object.
533 <p />
534 Pass NULL to clear any previous xfermode.
535 As a convenience, the parameter passed is also returned.
536 If a previous xfermode exists, its reference count is decremented.
537 If xfermode is not NULL, its reference count is incremented.
538 @param xfermode May be NULL. The new xfermode to be installed in the
539 paint
540 @return xfermode
541 */
542 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000543
544 /** Create an xfermode based on the specified Mode, and assign it into the
545 paint, returning the mode that was set. If the Mode is SrcOver, then
546 the paint's xfermode is set to null.
547 */
reed@android.com0baf1932009-06-24 12:41:42 +0000548 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000549
reed@android.com8a1c16f2008-12-17 15:59:43 +0000550 /** Get the paint's patheffect object.
551 <p />
552 The patheffect reference count is not affected.
553 @return the paint's patheffect (or NULL)
554 */
555 SkPathEffect* getPathEffect() const { return fPathEffect; }
556
557 /** Set or clear the patheffect object.
558 <p />
559 Pass NULL to clear any previous patheffect.
560 As a convenience, the parameter passed is also returned.
561 If a previous patheffect exists, its reference count is decremented.
562 If patheffect is not NULL, its reference count is incremented.
563 @param effect May be NULL. The new patheffect to be installed in the
564 paint
565 @return effect
566 */
567 SkPathEffect* setPathEffect(SkPathEffect* effect);
568
569 /** Get the paint's maskfilter object.
570 <p />
571 The maskfilter reference count is not affected.
572 @return the paint's maskfilter (or NULL)
573 */
574 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
575
576 /** Set or clear the maskfilter object.
577 <p />
578 Pass NULL to clear any previous maskfilter.
579 As a convenience, the parameter passed is also returned.
580 If a previous maskfilter exists, its reference count is decremented.
581 If maskfilter is not NULL, its reference count is incremented.
582 @param maskfilter May be NULL. The new maskfilter to be installed in
583 the paint
584 @return maskfilter
585 */
586 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
587
588 // These attributes are for text/fonts
589
590 /** Get the paint's typeface object.
591 <p />
592 The typeface object identifies which font to use when drawing or
593 measuring text. The typeface reference count is not affected.
594 @return the paint's typeface (or NULL)
595 */
596 SkTypeface* getTypeface() const { return fTypeface; }
597
598 /** Set or clear the typeface object.
599 <p />
600 Pass NULL to clear any previous typeface.
601 As a convenience, the parameter passed is also returned.
602 If a previous typeface exists, its reference count is decremented.
603 If typeface is not NULL, its reference count is incremented.
604 @param typeface May be NULL. The new typeface to be installed in the
605 paint
606 @return typeface
607 */
608 SkTypeface* setTypeface(SkTypeface* typeface);
609
610 /** Get the paint's rasterizer (or NULL).
611 <p />
612 The raster controls how paths/text are turned into alpha masks.
613 @return the paint's rasterizer (or NULL)
614 */
615 SkRasterizer* getRasterizer() const { return fRasterizer; }
616
617 /** Set or clear the rasterizer object.
618 <p />
619 Pass NULL to clear any previous rasterizer.
620 As a convenience, the parameter passed is also returned.
621 If a previous rasterizer exists in the paint, its reference count is
622 decremented. If rasterizer is not NULL, its reference count is
623 incremented.
624 @param rasterizer May be NULL. The new rasterizer to be installed in
625 the paint.
626 @return rasterizer
627 */
628 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
629
reed@google.com15356a62011-11-03 19:29:08 +0000630 SkImageFilter* getImageFilter() const { return fImageFilter; }
631 SkImageFilter* setImageFilter(SkImageFilter*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000632
reed@google.comb0a34d82012-07-11 19:57:55 +0000633 SkAnnotation* getAnnotation() const { return fAnnotation; }
634 SkAnnotation* setAnnotation(SkAnnotation*);
635
636 /**
reed@google.com9d07fec2011-03-16 20:02:59 +0000637 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
638 * reference count.
639 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000641
642 /**
643 * Set or clear the looper object.
644 * <p />
645 * Pass NULL to clear any previous looper.
646 * As a convenience, the parameter passed is also returned.
647 * If a previous looper exists in the paint, its reference count is
648 * decremented. If looper is not NULL, its reference count is
649 * incremented.
650 * @param looper May be NULL. The new looper to be installed in the paint.
651 * @return looper
652 */
653 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000654
655 enum Align {
656 kLeft_Align,
657 kCenter_Align,
658 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000659 };
660 enum {
661 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000662 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000663
reed@android.com8a1c16f2008-12-17 15:59:43 +0000664 /** Return the paint's Align value for drawing text.
665 @return the paint's Align value for drawing text.
666 */
reedf59eab22014-07-14 14:39:15 -0700667 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000668
reed@android.com8a1c16f2008-12-17 15:59:43 +0000669 /** Set the paint's text alignment.
670 @param align set the paint's Align value for drawing text.
671 */
672 void setTextAlign(Align align);
673
674 /** Return the paint's text size.
675 @return the paint's text size.
676 */
677 SkScalar getTextSize() const { return fTextSize; }
678
679 /** Set the paint's text size. This value must be > 0
680 @param textSize set the paint's text size.
681 */
682 void setTextSize(SkScalar textSize);
683
684 /** Return the paint's horizontal scale factor for text. The default value
685 is 1.0.
686 @return the paint's scale factor in X for drawing/measuring text
687 */
688 SkScalar getTextScaleX() const { return fTextScaleX; }
689
690 /** Set the paint's horizontal scale factor for text. The default value
691 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
692 stretch the text narrower.
693 @param scaleX set the paint's scale factor in X for drawing/measuring
694 text.
695 */
696 void setTextScaleX(SkScalar scaleX);
697
698 /** Return the paint's horizontal skew factor for text. The default value
699 is 0.
700 @return the paint's skew factor in X for drawing text.
701 */
702 SkScalar getTextSkewX() const { return fTextSkewX; }
703
704 /** Set the paint's horizontal skew factor for text. The default value
705 is 0. For approximating oblique text, use values around -0.25.
706 @param skewX set the paint's skew factor in X for drawing text.
707 */
708 void setTextSkewX(SkScalar skewX);
709
710 /** Describes how to interpret the text parameters that are passed to paint
711 methods like measureText() and getTextWidths().
712 */
713 enum TextEncoding {
714 kUTF8_TextEncoding, //!< the text parameters are UTF8
715 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000716 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000717 kGlyphID_TextEncoding //!< the text parameters are glyph indices
718 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000719
reedf59eab22014-07-14 14:39:15 -0700720 TextEncoding getTextEncoding() const {
721 return (TextEncoding)fBitfields.fTextEncoding;
722 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723
724 void setTextEncoding(TextEncoding encoding);
725
726 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000727 /** Flags which indicate the confidence level of various metrics.
728 A set flag indicates that the metric may be trusted.
729 */
730 enum FontMetricsFlags {
731 kUnderlineThinknessIsValid_Flag = 1 << 0,
732 kUnderlinePositionIsValid_Flag = 1 << 1,
733 };
734
735 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000736 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
737 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
738 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
739 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
740 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000741 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
742 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000743 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
744 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000745 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
746 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000747 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
748
749 /** Underline Position - position of the top of the Underline stroke
750 relative to the baseline, this can have following values
751 - Negative - means underline should be drawn above baseline.
752 - Positive - means below baseline.
753 - Zero - mean underline should be drawn on baseline.
754 */
755 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
756
757 /** If the fontmetrics has a valid underlinethickness, return true, and set the
758 thickness param to that value. If it doesn't return false and ignore the
759 thickness param.
760 */
761 bool hasUnderlineThickness(SkScalar* thickness) const {
762 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
763 *thickness = fUnderlineThickness;
764 return true;
765 }
766 return false;
767 }
768
769 /** If the fontmetrics has a valid underlineposition, return true, and set the
770 thickness param to that value. If it doesn't return false and ignore the
771 thickness param.
772 */
773 bool hasUnderlinePosition(SkScalar* position) const {
774 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
775 *position = fUnderlinePosition;
776 return true;
777 }
778 return false;
779 }
780
reed@android.com8a1c16f2008-12-17 15:59:43 +0000781 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000782
reed@android.com8a1c16f2008-12-17 15:59:43 +0000783 /** Return the recommend spacing between lines (which will be
784 fDescent - fAscent + fLeading).
785 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000786 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000787 @param metrics If not null, returns the font metrics for the
788 current typeface/pointsize/etc setting in this
789 paint.
790 @param scale If not 0, return width as if the canvas were scaled
791 by this value
792 @param return the recommended spacing between lines
793 */
794 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000795
reed@android.com8a1c16f2008-12-17 15:59:43 +0000796 /** Return the recommend line spacing. This will be
797 fDescent - fAscent + fLeading
798 */
799 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
800
801 /** Convert the specified text into glyph IDs, returning the number of
802 glyphs ID written. If glyphs is NULL, it is ignore and only the count
803 is returned.
804 */
805 int textToGlyphs(const void* text, size_t byteLength,
806 uint16_t glyphs[]) const;
807
reed@android.coma5dcaf62010-02-05 17:12:32 +0000808 /** Return true if all of the specified text has a corresponding non-zero
809 glyph ID. If any of the code-points in the text are not supported in
810 the typeface (i.e. the glyph ID would be zero), then return false.
811
812 If the text encoding for the paint is kGlyph_TextEncoding, then this
813 returns true if all of the specified glyph IDs are non-zero.
814 */
815 bool containsText(const void* text, size_t byteLength) const;
816
reed@android.com9d3a9852010-01-08 14:07:42 +0000817 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
818 to zero. Note: this does not look at the text-encoding setting in the
819 paint, only at the typeface.
820 */
robertphillipsd24955a2015-06-26 12:17:59 -0700821 void glyphsToUnichars(const uint16_t glyphs[], int count, SkUnichar text[]) const;
reed@android.com9d3a9852010-01-08 14:07:42 +0000822
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 /** Return the number of drawable units in the specified text buffer.
824 This looks at the current TextEncoding field of the paint. If you also
825 want to have the text converted into glyph IDs, call textToGlyphs
826 instead.
827 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000828 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000829 return this->textToGlyphs(text, byteLength, NULL);
830 }
831
reed@google.com44da42e2011-11-10 20:04:47 +0000832 /** Return the width of the text. This will return the vertical measure
833 * if isVerticalText() is true, in which case the returned value should
834 * be treated has a height instead of a width.
835 *
836 * @param text The text to be measured
837 * @param length Number of bytes of text to measure
838 * @param bounds If not NULL, returns the bounds of the text,
839 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000840 * @return The advance width of the text
841 */
reed99ae8812014-08-26 11:30:01 -0700842 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000843
reed@google.com44da42e2011-11-10 20:04:47 +0000844 /** Return the width of the text. This will return the vertical measure
845 * if isVerticalText() is true, in which case the returned value should
846 * be treated has a height instead of a width.
847 *
848 * @param text Address of the text
849 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000850 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000851 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000852 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700853 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000854 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000855
reed@google.com44da42e2011-11-10 20:04:47 +0000856 /** Return the number of bytes of text that were measured. If
857 * isVerticalText() is true, then the vertical advances are used for
858 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000859 *
reed@google.com44da42e2011-11-10 20:04:47 +0000860 * @param text The text to be measured
861 * @param length Number of bytes of text to measure
862 * @param maxWidth Maximum width. Only the subset of text whose accumulated
863 * widths are <= maxWidth are measured.
864 * @param measuredWidth Optional. If non-null, this returns the actual
865 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000866 * @return The number of bytes of text that were measured. Will be
867 * <= length.
868 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000869 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700870 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000871
reed@google.com44da42e2011-11-10 20:04:47 +0000872 /** Return the advances for the text. These will be vertical advances if
873 * isVerticalText() returns true.
874 *
875 * @param text the text
876 * @param byteLength number of bytes to of text
877 * @param widths If not null, returns the array of advances for
878 * the glyphs. If not NULL, must be at least a large
879 * as the number of unichars in the specified text.
880 * @param bounds If not null, returns the bounds for each of
881 * character, relative to (0, 0)
882 * @return the number of unichars in the specified text.
883 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000884 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
885 SkRect bounds[] = NULL) const;
886
887 /** Return the path (outline) for the specified text.
caryclark0449bcf2016-02-09 13:25:45 -0800888 * Note: just like SkCanvas::drawText, this will respect the Align setting
889 * in the paint.
890 *
891 * @param text the text
892 * @param length number of bytes of text
893 * @param x The x-coordinate of the origin of the text.
894 * @param y The y-coordinate of the origin of the text.
895 * @param path The outline of the text.
896 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000897 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
898 SkPath* path) const;
899
caryclark0449bcf2016-02-09 13:25:45 -0800900 /** Return the path (outline) for the specified text.
901 * Note: just like SkCanvas::drawText, this will respect the Align setting
902 * in the paint.
903 *
904 * @param text the text
905 * @param length number of bytes of text
906 * @param pos array of positions, used to position each character
907 * @param path The outline of the text.
908 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000909 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000910 const SkPoint pos[], SkPath* path) const;
911
caryclark0449bcf2016-02-09 13:25:45 -0800912 /** Return the number of intervals that intersect the intercept along the axis of the advance.
913 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
914 * the string. The caller may pass nullptr for intervals to determine the size of the interval
915 * array, or may conservatively pre-allocate an array with length * 2 entries. The computed
916 * intervals are cached by glyph to improve performance for multiple calls.
917 * This permits constructing an underline that skips the descenders.
918 *
919 * @param text the text
920 * @param length number of bytes of text
921 * @param x The x-coordinate of the origin of the text.
922 * @param y The y-coordinate of the origin of the text.
923 * @param bounds The lower and upper line parallel to the advance.
924 * @param array If not null, the found intersections.
925 *
926 * @return The number of intersections, which may be zero.
927 */
928 int getTextIntercepts(const void* text, size_t length, SkScalar x, SkScalar y,
929 const SkScalar bounds[2], SkScalar* intervals) const;
930
931 /** Return the number of intervals that intersect the intercept along the axis of the advance.
932 * The return count is zero or a multiple of two, and is at most the number of glyphs * 2 in
933 * 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 pos array of positions, used to position each character
941 * @param bounds The lower and upper line parallel to the advance.
942 * @param array If not null, the glyph bounds contained by the advance parallel lines.
943 *
944 * @return The number of intersections, which may be zero.
945 */
946 int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[],
947 const SkScalar bounds[2], SkScalar* intervals) const;
948
reed8893e5f2014-12-15 13:27:26 -0800949 /**
950 * Return a rectangle that represents the union of the bounds of all
951 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
952 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
953 * on this paint.
954 */
955 SkRect getFontBounds() const;
956
reed@google.com632e1a22011-10-06 12:37:00 +0000957 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
958 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
959 bool nothingToDraw() const;
960
reed@google.coma584aed2012-05-16 14:06:02 +0000961 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000962 // would prefer to make these private...
963
reed@google.coma584aed2012-05-16 14:06:02 +0000964 /** Returns true if the current paint settings allow for fast computation of
965 bounds (i.e. there is nothing complex like a patheffect that would make
966 the bounds computation expensive.
967 */
senorblanco0abdf762015-08-20 11:10:41 -0700968 bool canComputeFastBounds() const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000969
reed@google.coma584aed2012-05-16 14:06:02 +0000970 /** Only call this if canComputeFastBounds() returned true. This takes a
971 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
972 effects in the paint (e.g. stroking). If needed, it uses the storage
973 rect parameter. It returns the adjusted bounds that can then be used
974 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000975
reed@google.coma584aed2012-05-16 14:06:02 +0000976 The returned rect will either be orig or storage, thus the caller
977 should not rely on storage being set to the result, but should always
978 use the retured value. It is legal for orig and storage to be the same
979 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000980
reed@google.coma584aed2012-05-16 14:06:02 +0000981 e.g.
982 if (paint.canComputeFastBounds()) {
983 SkRect r, storage;
984 path.computeBounds(&r, SkPath::kFast_BoundsType);
985 const SkRect& fastR = paint.computeFastBounds(r, &storage);
986 if (canvas->quickReject(fastR, ...)) {
987 // don't draw the path
988 }
989 }
990 */
991 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
992 SkPaint::Style style = this->getStyle();
993 // ultra fast-case: filling with no effects that affect geometry
994 if (kFill_Style == style) {
995 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
996 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
997 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000998 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +0000999 if (!effects) {
1000 return orig;
1001 }
1002 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001003
reed@google.coma584aed2012-05-16 14:06:02 +00001004 return this->doComputeFastBounds(orig, storage, style);
1005 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001006
reed@google.coma584aed2012-05-16 14:06:02 +00001007 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1008 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001009 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001010 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001011
reed@google.coma584aed2012-05-16 14:06:02 +00001012 // Take the style explicitly, so the caller can force us to be stroked
1013 // without having to make a copy of the paint just to change that field.
1014 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1015 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001016
reed@google.comed43dff2013-06-04 16:56:27 +00001017 /**
1018 * Return a matrix that applies the paint's text values: size, scale, skew
1019 */
1020 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1021 SkScalar scaleX, SkScalar skewX) {
1022 matrix->setScale(size * scaleX, size);
1023 if (skewX) {
1024 matrix->postSkew(skewX, 0);
1025 }
1026 return matrix;
1027 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001028
reed@google.comed43dff2013-06-04 16:56:27 +00001029 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1030 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1031 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001032
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001033 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001034
reed@google.comd5f20792012-05-16 14:15:02 +00001035private:
1036 SkTypeface* fTypeface;
reed@google.comd5f20792012-05-16 14:15:02 +00001037 SkPathEffect* fPathEffect;
1038 SkShader* fShader;
1039 SkXfermode* fXfermode;
1040 SkMaskFilter* fMaskFilter;
1041 SkColorFilter* fColorFilter;
1042 SkRasterizer* fRasterizer;
1043 SkDrawLooper* fLooper;
1044 SkImageFilter* fImageFilter;
reed@google.comb0a34d82012-07-11 19:57:55 +00001045 SkAnnotation* fAnnotation;
reed@google.comd5f20792012-05-16 14:15:02 +00001046
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001047 SkScalar fTextSize;
1048 SkScalar fTextScaleX;
1049 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001050 SkColor fColor;
1051 SkScalar fWidth;
1052 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001053 union {
1054 struct {
1055 // all of these bitfields should add up to 32
1056 unsigned fFlags : 16;
1057 unsigned fTextAlign : 2;
1058 unsigned fCapType : 2;
1059 unsigned fJoinType : 2;
1060 unsigned fStyle : 2;
1061 unsigned fTextEncoding : 2; // 3 values
1062 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001063 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001064 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001065 } fBitfields;
1066 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001067 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001068
reed@google.comd5f20792012-05-16 14:15:02 +00001069 SkDrawCacheProc getDrawCacheProc() const;
reed9e96aa02014-10-03 12:44:37 -07001070 SkMeasureCacheProc getMeasureCacheProc(bool needFullMetrics) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001071
1072 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1073 int* count, SkRect* bounds) const;
1074
joshualittfd450792015-03-13 08:38:43 -07001075 /*
1076 * Allocs an SkDescriptor on the heap and return it to the caller as a refcnted
1077 * SkData. Caller is responsible for managing the lifetime of this object.
1078 */
robertphillipsfcf78292015-06-19 11:49:52 -07001079 void getScalerContextDescriptor(SkAutoDescriptor*, const SkSurfaceProps& surfaceProps,
joshualitt2b6acb42015-04-01 11:30:27 -07001080 const SkMatrix*, bool ignoreGamma) const;
joshualittfd450792015-03-13 08:38:43 -07001081
robertphillipsfcf78292015-06-19 11:49:52 -07001082 SkGlyphCache* detachCache(const SkSurfaceProps* surfaceProps, const SkMatrix*,
jvanverth2d2a68c2014-06-10 06:42:56 -07001083 bool ignoreGamma) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001084
robertphillipsfcf78292015-06-19 11:49:52 -07001085 void descriptorProc(const SkSurfaceProps* surfaceProps, const SkMatrix* deviceMatrix,
reed@google.com90808e82013-03-19 14:44:54 +00001086 void (*proc)(SkTypeface*, const SkDescriptor*, void*),
robertphillipsfcf78292015-06-19 11:49:52 -07001087 void* context, bool ignoreGamma) const;
reed@android.comd252db02009-04-01 18:31:44 +00001088
joshualitt9e36c1a2015-04-14 12:17:27 -07001089 /*
1090 * The luminance color is used to determine which Gamma Canonical color to map to. This is
1091 * really only used by backends which want to cache glyph masks, and need some way to know if
1092 * they need to generate new masks based off a given color.
1093 */
1094 SkColor computeLuminanceColor() const;
1095
reed@android.com8a1c16f2008-12-17 15:59:43 +00001096 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001097 /* This is the size we use when we ask for a glyph's path. We then
1098 * post-transform it as we draw to match the request.
1099 * This is done to try to re-use cache entries for the path.
1100 *
1101 * This value is somewhat arbitrary. In theory, it could be 1, since
1102 * we store paths as floats. However, we get the path from the font
1103 * scaler, and it may represent its paths as fixed-point (or 26.6),
1104 * so we shouldn't ask for something too big (might overflow 16.16)
1105 * or too small (underflow 26.6).
1106 *
1107 * This value could track kMaxSizeForGlyphCache, assuming the above
1108 * constraints, but since we ask for unhinted paths, the two values
1109 * need not match per-se.
1110 */
1111 kCanonicalTextSizeForPaths = 64,
1112
1113 /*
1114 * Above this size (taking into account CTM and textSize), we never use
1115 * the cache for bits or metrics (we might overflow), so we just ask
1116 * for a caononical size and post-transform that.
1117 */
1118 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001119 };
reed@google.comed43dff2013-06-04 16:56:27 +00001120
1121 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1122
reed@google.comed43dff2013-06-04 16:56:27 +00001123 // Set flags/hinting/textSize up to use for drawing text as paths.
1124 // Returns scale factor to restore the original textSize, since will will
1125 // have change it to kCanonicalTextSizeForPaths.
1126 SkScalar setupForAsPaths();
1127
1128 static SkScalar MaxCacheSize2() {
1129 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1130 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1131 return kMag2Max;
1132 }
1133
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001134 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001135 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136 friend class SkCanvas;
1137 friend class SkDraw;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001138 friend class SkPDFDevice;
joshualitte76b4bb2015-12-28 07:23:58 -08001139 friend class GrAtlasTextBlob;
joshualittdbd35932015-04-02 09:19:04 -07001140 friend class GrAtlasTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001141 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001142 friend class GrPathRendering;
joshualitt0a42e682015-12-10 13:20:58 -08001143 friend class GrTextUtils;
cdalton855d83f2014-09-18 13:51:53 -07001144 friend class GrGLPathRendering;
joshualitt9e36c1a2015-04-14 12:17:27 -07001145 friend class SkScalerContext;
caryclark0449bcf2016-02-09 13:25:45 -08001146 friend class SkTextBaseIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001147 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148};
1149
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150#endif