blob: fef4319477ed183dc4c8772cf6bebd45e39b4610 [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"
reed@google.com9efd9a02012-01-30 15:41:43 +000012#include "SkDrawLooper.h"
reedf803da12015-01-23 05:58:07 -080013#include "SkFilterQuality.h"
reed@google.comed43dff2013-06-04 16:56:27 +000014#include "SkMatrix.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000015#include "SkXfermode.h"
16
reedf803da12015-01-23 05:58:07 -080017// TODO: clean up Skia internals so we can remove this and only keep it for clients
18#define SK_SUPPORT_LEGACY_FILTERLEVEL_ENUM
19
reed@google.comb0a34d82012-07-11 19:57:55 +000020class SkAnnotation;
reed@android.com8a1c16f2008-12-17 15:59:43 +000021class SkAutoGlyphCache;
22class SkColorFilter;
23class SkDescriptor;
bungeman@google.com532470f2013-01-22 19:25:14 +000024struct SkDeviceProperties;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000025class SkReadBuffer;
26class SkWriteBuffer;
reed@android.com8a1c16f2008-12-17 15:59:43 +000027struct SkGlyph;
28struct SkRect;
29class SkGlyphCache;
reed@google.com15356a62011-11-03 19:29:08 +000030class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031class SkMaskFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000032class SkPath;
33class SkPathEffect;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000034struct SkPoint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000035class SkRasterizer;
36class SkShader;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
39typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
40 SkFixed x, SkFixed y);
41
42typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
43
humper@google.comb0889472013-07-09 21:37:14 +000044#define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag
45
reed@android.com8a1c16f2008-12-17 15:59:43 +000046/** \class SkPaint
47
48 The SkPaint class holds the style and color information about how to draw
49 geometries, text and bitmaps.
50*/
humper@google.comb0889472013-07-09 21:37:14 +000051
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000052class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053public:
54 SkPaint();
55 SkPaint(const SkPaint& paint);
56 ~SkPaint();
57
58 SkPaint& operator=(const SkPaint&);
59
mtkleinbc97ef42014-08-25 10:10:47 -070060 /** operator== may give false negatives: two paints that draw equivalently
61 may return false. It will never give false positives: two paints that
62 are not equivalent always return false.
63 */
robertphillips@google.comb2657412013-08-07 22:36:29 +000064 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
65 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
66 return !(a == b);
67 }
68
mtkleinfb1fe4f2014-10-07 09:26:10 -070069 /** getHash() is a shallow hash, with the same limitations as operator==.
70 * If operator== returns true for two paints, getHash() returns the same value for each.
71 */
72 uint32_t getHash() const;
73
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000074 void flatten(SkWriteBuffer&) const;
75 void unflatten(SkReadBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076
77 /** Restores the paint to its initial settings.
78 */
79 void reset();
80
agl@chromium.org309485b2009-07-21 17:41:32 +000081 /** Specifies the level of hinting to be performed. These names are taken
82 from the Gnome/Cairo names for the same. They are translated into
83 Freetype concepts the same as in cairo-ft-font.c:
84 kNo_Hinting -> FT_LOAD_NO_HINTING
85 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
86 kNormal_Hinting -> <default, no option>
87 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
88 subpixel glyphs, in which case TARGET_LCD or
89 TARGET_LCD_V is used>
90 */
91 enum Hinting {
92 kNo_Hinting = 0,
93 kSlight_Hinting = 1,
94 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000095 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000096 };
97
reed@google.com9d07fec2011-03-16 20:02:59 +000098 Hinting getHinting() const {
reedf59eab22014-07-14 14:39:15 -070099 return static_cast<Hinting>(fBitfields.fHinting);
agl@chromium.org309485b2009-07-21 17:41:32 +0000100 }
101
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000102 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 /** Specifies the bit values that are stored in the paint's flags.
105 */
106 enum Flags {
107 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 kDither_Flag = 0x04, //!< mask to enable dithering
109 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
110 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
111 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
112 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000113 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000115 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000116 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000117 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000118 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000119 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000120 kDistanceFieldTextTEMP_Flag = 0x4000, //!< TEMPORARY mask to enable distance fields
121 // currently overrides LCD and subpixel rendering
agl@chromium.org309485b2009-07-21 17:41:32 +0000122 // when adding extra flags, note that the fFlags member is specified
123 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
humper@google.com387db0a2013-07-09 14:13:04 +0000125 kAllFlags = 0xFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 };
127
128 /** Return the paint's flags. Use the Flag enum to test flag values.
129 @return the paint's flags (see enums ending in _Flag for bit masks)
130 */
reedf59eab22014-07-14 14:39:15 -0700131 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132
133 /** Set the paint's flags. Use the Flag enum to specific flag values.
134 @param flags The new flag bits for the paint (see Flags enum)
135 */
136 void setFlags(uint32_t flags);
137
138 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
139 @return true if the antialias bit is set in the paint's flags.
140 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000141 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 return SkToBool(this->getFlags() & kAntiAlias_Flag);
143 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
146 @param aa true to enable antialiasing, false to disable it
147 */
148 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 /** Helper for getFlags(), returning true if kDither_Flag bit is set
151 @return true if the dithering bit is set in the paint's flags.
152 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000153 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 return SkToBool(this->getFlags() & kDither_Flag);
155 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
158 @param dither true to enable dithering, false to disable it
159 */
160 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
163 @return true if the lineartext bit is set in the paint's flags
164 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000165 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 return SkToBool(this->getFlags() & kLinearText_Flag);
167 }
168
169 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
170 @param linearText true to set the linearText bit in the paint's flags,
171 false to clear it.
172 */
173 void setLinearText(bool linearText);
174
175 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
176 @return true if the lineartext bit is set in the paint's flags
177 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000178 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 return SkToBool(this->getFlags() & kSubpixelText_Flag);
180 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000181
reed@google.com84b437e2011-08-01 12:45:35 +0000182 /**
183 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
184 * @param subpixelText true to set the subpixelText bit in the paint's
185 * flags, false to clear it.
186 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000188
reed@google.com9d07fec2011-03-16 20:02:59 +0000189 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000190 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
191 }
192
reed@google.com84b437e2011-08-01 12:45:35 +0000193 /**
194 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
195 * Note: antialiasing must also be on for lcd rendering
196 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
197 * false to clear it.
198 */
199 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000200
reed@google.com9d07fec2011-03-16 20:02:59 +0000201 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000202 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
203 }
204
205 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
206 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
207 false to clear it.
208 */
209 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
210
reed@google.com9d07fec2011-03-16 20:02:59 +0000211 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000212 return SkToBool(this->getFlags() & kAutoHinting_Flag);
213 }
214
215 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
216 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
217 paint's flags,
218 false to clear it.
219 */
220 void setAutohinted(bool useAutohinter);
221
reed@google.com830a23e2011-11-10 15:20:49 +0000222 bool isVerticalText() const {
223 return SkToBool(this->getFlags() & kVerticalText_Flag);
224 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000225
reed@google.com830a23e2011-11-10 15:20:49 +0000226 /**
227 * Helper for setting or clearing the kVerticalText_Flag bit in
228 * setFlags(...).
229 *
230 * If this bit is set, then advances are treated as Y values rather than
231 * X values, and drawText will places its glyphs vertically rather than
232 * horizontally.
233 */
234 void setVerticalText(bool);
235
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
237 @return true if the underlineText bit is set in the paint's flags.
238 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000239 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 return SkToBool(this->getFlags() & kUnderlineText_Flag);
241 }
242
243 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
244 @param underlineText true to set the underlineText bit in the paint's
245 flags, false to clear it.
246 */
247 void setUnderlineText(bool underlineText);
248
249 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
250 @return true if the strikeThruText bit is set in the paint's flags.
251 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000252 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
254 }
255
256 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
257 @param strikeThruText true to set the strikeThruText bit in the
258 paint's flags, false to clear it.
259 */
260 void setStrikeThruText(bool strikeThruText);
261
262 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
263 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
264 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000265 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
267 }
268
269 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
270 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
271 flags, false to clear it.
272 */
273 void setFakeBoldText(bool fakeBoldText);
274
275 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
276 @return true if the kernText bit is set in the paint's flags.
277 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000278 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 return SkToBool(this->getFlags() & kDevKernText_Flag);
280 }
281
282 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
283 @param kernText true to set the kKernText_Flag bit in the paint's
284 flags, false to clear it.
285 */
286 void setDevKernText(bool devKernText);
287
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000288 /** Helper for getFlags(), returns true if kDistanceFieldTextTEMP_Flag bit is set
289 @return true if the distanceFieldText bit is set in the paint's flags.
290 */
291 bool isDistanceFieldTextTEMP() const {
292 return SkToBool(this->getFlags() & kDistanceFieldTextTEMP_Flag);
293 }
294
295 /** Helper for setFlags(), setting or clearing the kDistanceFieldTextTEMP_Flag bit
296 @param distanceFieldText true to set the kDistanceFieldTextTEMP_Flag bit in the paint's
297 flags, false to clear it.
298 */
299 void setDistanceFieldTextTEMP(bool distanceFieldText);
300
reedf803da12015-01-23 05:58:07 -0800301#ifdef SK_SUPPORT_LEGACY_FILTERLEVEL_ENUM
reed@google.comc9683152013-07-18 13:47:01 +0000302 enum FilterLevel {
reedf803da12015-01-23 05:58:07 -0800303 kNone_FilterLevel = kNone_SkFilterQuality,
304 kLow_FilterLevel = kLow_SkFilterQuality,
305 kMedium_FilterLevel = kMedium_SkFilterQuality,
306 kHigh_FilterLevel = kHigh_SkFilterQuality
reed@google.comc9683152013-07-18 13:47:01 +0000307 };
308
309 /**
310 * Return the filter level. This affects the quality (and performance) of
311 * drawing scaled images.
312 */
reedf59eab22014-07-14 14:39:15 -0700313 FilterLevel getFilterLevel() const {
reedf803da12015-01-23 05:58:07 -0800314 return (FilterLevel)this->getFilterQuality();
reedf59eab22014-07-14 14:39:15 -0700315 }
reed@google.comc9683152013-07-18 13:47:01 +0000316
317 /**
318 * Set the filter level. This affects the quality (and performance) of
319 * drawing scaled images.
320 */
reedf803da12015-01-23 05:58:07 -0800321 void setFilterLevel(FilterLevel level) {
322 this->setFilterQuality((SkFilterQuality)level);
323 }
324#endif
325
326 /**
327 * Return the filter level. This affects the quality (and performance) of
328 * drawing scaled images.
329 */
330 SkFilterQuality getFilterQuality() const {
331 return (SkFilterQuality)fBitfields.fFilterQuality;
332 }
333
334 /**
335 * Set the filter quality. This affects the quality (and performance) of
336 * drawing scaled images.
337 */
338 void setFilterQuality(SkFilterQuality quality);
reed@google.comc9683152013-07-18 13:47:01 +0000339
340 /**
reed@google.comc9683152013-07-18 13:47:01 +0000341 * If the predicate is true, set the filterLevel to Low, else set it to
342 * None.
343 */
reed@google.com44699382013-10-31 17:28:30 +0000344 SK_ATTR_DEPRECATED("use setFilterLevel")
reed@google.comc9683152013-07-18 13:47:01 +0000345 void setFilterBitmap(bool doFilter) {
346 this->setFilterLevel(doFilter ? kLow_FilterLevel : kNone_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000348
reed@google.comc9683152013-07-18 13:47:01 +0000349 /**
reed@google.comc9683152013-07-18 13:47:01 +0000350 * Returns true if getFilterLevel() returns anything other than None.
351 */
reed@google.com44699382013-10-31 17:28:30 +0000352 SK_ATTR_DEPRECATED("use getFilterLevel")
reed@google.comc9683152013-07-18 13:47:01 +0000353 bool isFilterBitmap() const {
354 return kNone_FilterLevel != this->getFilterLevel();
355 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356
357 /** Styles apply to rect, oval, path, and text.
358 Bitmaps are always drawn in "fill", and lines are always drawn in
359 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000360
reed@android.comed881c22009-09-15 14:10:42 +0000361 Note: strokeandfill implicitly draws the result with
362 SkPath::kWinding_FillType, so if the original path is even-odd, the
363 results may not appear the same as if it was drawn twice, filled and
364 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 */
366 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000367 kFill_Style, //!< fill the geometry
368 kStroke_Style, //!< stroke the geometry
369 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000370 };
371 enum {
372 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 };
374
375 /** Return the paint's style, used for controlling how primitives'
376 geometries are interpreted (except for drawBitmap, which always assumes
377 kFill_Style).
378 @return the paint's Style
379 */
reedf59eab22014-07-14 14:39:15 -0700380 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381
382 /** Set the paint's style, used for controlling how primitives'
383 geometries are interpreted (except for drawBitmap, which always assumes
384 Fill).
385 @param style The new style to set in the paint
386 */
387 void setStyle(Style style);
388
389 /** Return the paint's color. Note that the color is a 32bit value
390 containing alpha as well as r,g,b. This 32bit value is not
391 premultiplied, meaning that its alpha can be any value, regardless of
392 the values of r,g,b.
393 @return the paint's color (and alpha).
394 */
395 SkColor getColor() const { return fColor; }
396
397 /** Set the paint's color. Note that the color is a 32bit value containing
398 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
399 that its alpha can be any value, regardless of the values of r,g,b.
400 @param color The new color (including alpha) to set in the paint.
401 */
402 void setColor(SkColor color);
403
404 /** Helper to getColor() that just returns the color's alpha value.
405 @return the alpha component of the paint's color.
406 */
407 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000408
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 /** Helper to setColor(), that only assigns the color's alpha value,
410 leaving its r,g,b values unchanged.
411 @param a set the alpha component (0..255) of the paint's color.
412 */
413 void setAlpha(U8CPU a);
414
415 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
416 using SkColorSetARGB()
417 @param a The new alpha component (0..255) of the paint's color.
418 @param r The new red component (0..255) of the paint's color.
419 @param g The new green component (0..255) of the paint's color.
420 @param b The new blue component (0..255) of the paint's color.
421 */
422 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
423
reed@google.com9d07fec2011-03-16 20:02:59 +0000424 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 <p />
426 A value of 0 strokes in hairline mode.
427 Hairlines always draw 1-pixel wide, regardless of the matrix.
428 @return the paint's stroke width, used whenever the paint's style is
429 Stroke or StrokeAndFill.
430 */
431 SkScalar getStrokeWidth() const { return fWidth; }
432
reed@google.com9d07fec2011-03-16 20:02:59 +0000433 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 Pass 0 to stroke in hairline mode.
435 Hairlines always draw 1-pixel wide, regardless of the matrix.
436 @param width set the paint's stroke width, used whenever the paint's
437 style is Stroke or StrokeAndFill.
438 */
439 void setStrokeWidth(SkScalar width);
440
441 /** Return the paint's stroke miter value. This is used to control the
442 behavior of miter joins when the joins angle is sharp.
443 @return the paint's miter limit, used whenever the paint's style is
444 Stroke or StrokeAndFill.
445 */
446 SkScalar getStrokeMiter() const { return fMiterLimit; }
447
448 /** Set the paint's stroke miter value. This is used to control the
449 behavior of miter joins when the joins angle is sharp. This value must
450 be >= 0.
451 @param miter set the miter limit on the paint, used whenever the
452 paint's style is Stroke or StrokeAndFill.
453 */
454 void setStrokeMiter(SkScalar miter);
455
456 /** Cap enum specifies the settings for the paint's strokecap. This is the
457 treatment that is applied to the beginning and end of each non-closed
458 contour (e.g. lines).
459 */
460 enum Cap {
461 kButt_Cap, //!< begin/end contours with no extension
462 kRound_Cap, //!< begin/end contours with a semi-circle extension
463 kSquare_Cap, //!< begin/end contours with a half square extension
464
465 kCapCount,
466 kDefault_Cap = kButt_Cap
467 };
468
469 /** Join enum specifies the settings for the paint's strokejoin. This is
470 the treatment that is applied to corners in paths and rectangles.
471 */
472 enum Join {
473 kMiter_Join, //!< connect path segments with a sharp join
474 kRound_Join, //!< connect path segments with a round join
475 kBevel_Join, //!< connect path segments with a flat bevel join
476
477 kJoinCount,
478 kDefault_Join = kMiter_Join
479 };
480
481 /** Return the paint's stroke cap type, controlling how the start and end
482 of stroked lines and paths are treated.
483 @return the line cap style for the paint, used whenever the paint's
484 style is Stroke or StrokeAndFill.
485 */
reedf59eab22014-07-14 14:39:15 -0700486 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000487
488 /** Set the paint's stroke cap type.
489 @param cap set the paint's line cap style, used whenever the paint's
490 style is Stroke or StrokeAndFill.
491 */
492 void setStrokeCap(Cap cap);
493
494 /** Return the paint's stroke join type.
495 @return the paint's line join style, used whenever the paint's style is
496 Stroke or StrokeAndFill.
497 */
reedf59eab22014-07-14 14:39:15 -0700498 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499
500 /** Set the paint's stroke join type.
501 @param join set the paint's line join style, used whenever the paint's
502 style is Stroke or StrokeAndFill.
503 */
504 void setStrokeJoin(Join join);
505
reed@google.com4bbdeac2013-01-24 21:03:11 +0000506 /**
507 * Applies any/all effects (patheffect, stroking) to src, returning the
508 * result in dst. The result is that drawing src with this paint will be
509 * the same as drawing dst with a default paint (at least from the
510 * geometric perspective).
511 *
512 * @param src input path
513 * @param dst output path (may be the same as src)
514 * @param cullRect If not null, the dst path may be culled to this rect.
515 * @return true if the path should be filled, or false if it should be
516 * drawn with a hairline (width == 0)
517 */
518 bool getFillPath(const SkPath& src, SkPath* dst,
519 const SkRect* cullRect = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 /** Get the paint's shader object.
522 <p />
523 The shader's reference count is not affected.
524 @return the paint's shader (or NULL)
525 */
526 SkShader* getShader() const { return fShader; }
527
528 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000529 * Shaders specify the source color(s) for what is being drawn. If a paint
530 * has no shader, then the paint's color is used. If the paint has a
531 * shader, then the shader's color(s) are use instead, but they are
532 * modulated by the paint's alpha. This makes it easy to create a shader
533 * once (e.g. bitmap tiling or gradient) and then change its transparency
534 * w/o having to modify the original shader... only the paint's alpha needs
535 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000536 *
537 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
538 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
539 * then the shader will use the paint's entire color to "colorize" its output (modulating the
540 * bitmap's alpha with the paint's color+alpha).
541 *
reed@google.com880dc472012-05-11 14:47:03 +0000542 * Pass NULL to clear any previous shader.
543 * As a convenience, the parameter passed is also returned.
544 * If a previous shader exists, its reference count is decremented.
545 * If shader is not NULL, its reference count is incremented.
546 * @param shader May be NULL. The shader to be installed in the paint
547 * @return shader
548 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000549 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000550
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551 /** Get the paint's colorfilter. If there is a colorfilter, its reference
552 count is not changed.
553 @return the paint's colorfilter (or NULL)
554 */
555 SkColorFilter* getColorFilter() const { return fColorFilter; }
556
557 /** Set or clear the paint's colorfilter, returning the parameter.
558 <p />
559 If the paint already has a filter, its reference count is decremented.
560 If filter is not NULL, its reference count is incremented.
561 @param filter May be NULL. The filter to be installed in the paint
562 @return filter
563 */
564 SkColorFilter* setColorFilter(SkColorFilter* filter);
565
566 /** Get the paint's xfermode object.
567 <p />
568 The xfermode's reference count is not affected.
569 @return the paint's xfermode (or NULL)
570 */
571 SkXfermode* getXfermode() const { return fXfermode; }
572
573 /** Set or clear the xfermode object.
574 <p />
575 Pass NULL to clear any previous xfermode.
576 As a convenience, the parameter passed is also returned.
577 If a previous xfermode exists, its reference count is decremented.
578 If xfermode is not NULL, its reference count is incremented.
579 @param xfermode May be NULL. The new xfermode to be installed in the
580 paint
581 @return xfermode
582 */
583 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000584
585 /** Create an xfermode based on the specified Mode, and assign it into the
586 paint, returning the mode that was set. If the Mode is SrcOver, then
587 the paint's xfermode is set to null.
588 */
reed@android.com0baf1932009-06-24 12:41:42 +0000589 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000590
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591 /** Get the paint's patheffect object.
592 <p />
593 The patheffect reference count is not affected.
594 @return the paint's patheffect (or NULL)
595 */
596 SkPathEffect* getPathEffect() const { return fPathEffect; }
597
598 /** Set or clear the patheffect object.
599 <p />
600 Pass NULL to clear any previous patheffect.
601 As a convenience, the parameter passed is also returned.
602 If a previous patheffect exists, its reference count is decremented.
603 If patheffect is not NULL, its reference count is incremented.
604 @param effect May be NULL. The new patheffect to be installed in the
605 paint
606 @return effect
607 */
608 SkPathEffect* setPathEffect(SkPathEffect* effect);
609
610 /** Get the paint's maskfilter object.
611 <p />
612 The maskfilter reference count is not affected.
613 @return the paint's maskfilter (or NULL)
614 */
615 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
616
617 /** Set or clear the maskfilter object.
618 <p />
619 Pass NULL to clear any previous maskfilter.
620 As a convenience, the parameter passed is also returned.
621 If a previous maskfilter exists, its reference count is decremented.
622 If maskfilter is not NULL, its reference count is incremented.
623 @param maskfilter May be NULL. The new maskfilter to be installed in
624 the paint
625 @return maskfilter
626 */
627 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
628
629 // These attributes are for text/fonts
630
631 /** Get the paint's typeface object.
632 <p />
633 The typeface object identifies which font to use when drawing or
634 measuring text. The typeface reference count is not affected.
635 @return the paint's typeface (or NULL)
636 */
637 SkTypeface* getTypeface() const { return fTypeface; }
638
639 /** Set or clear the typeface object.
640 <p />
641 Pass NULL to clear any previous typeface.
642 As a convenience, the parameter passed is also returned.
643 If a previous typeface exists, its reference count is decremented.
644 If typeface is not NULL, its reference count is incremented.
645 @param typeface May be NULL. The new typeface to be installed in the
646 paint
647 @return typeface
648 */
649 SkTypeface* setTypeface(SkTypeface* typeface);
650
651 /** Get the paint's rasterizer (or NULL).
652 <p />
653 The raster controls how paths/text are turned into alpha masks.
654 @return the paint's rasterizer (or NULL)
655 */
656 SkRasterizer* getRasterizer() const { return fRasterizer; }
657
658 /** Set or clear the rasterizer object.
659 <p />
660 Pass NULL to clear any previous rasterizer.
661 As a convenience, the parameter passed is also returned.
662 If a previous rasterizer exists in the paint, its reference count is
663 decremented. If rasterizer is not NULL, its reference count is
664 incremented.
665 @param rasterizer May be NULL. The new rasterizer to be installed in
666 the paint.
667 @return rasterizer
668 */
669 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
670
reed@google.com15356a62011-11-03 19:29:08 +0000671 SkImageFilter* getImageFilter() const { return fImageFilter; }
672 SkImageFilter* setImageFilter(SkImageFilter*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000673
reed@google.comb0a34d82012-07-11 19:57:55 +0000674 SkAnnotation* getAnnotation() const { return fAnnotation; }
675 SkAnnotation* setAnnotation(SkAnnotation*);
676
677 /**
678 * Returns true if there is an annotation installed on this paint, and
679 * the annotation specifics no-drawing.
680 */
reed@google.com44699382013-10-31 17:28:30 +0000681 SK_ATTR_DEPRECATED("use getAnnotation and check for non-null")
commit-bot@chromium.org950923b2013-10-29 20:44:39 +0000682 bool isNoDrawAnnotation() const { return this->getAnnotation() != NULL; }
reed@google.com15356a62011-11-03 19:29:08 +0000683
reed@google.com9d07fec2011-03-16 20:02:59 +0000684 /**
685 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
686 * reference count.
687 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000689
690 /**
691 * Set or clear the looper object.
692 * <p />
693 * Pass NULL to clear any previous looper.
694 * As a convenience, the parameter passed is also returned.
695 * If a previous looper exists in the paint, its reference count is
696 * decremented. If looper is not NULL, its reference count is
697 * incremented.
698 * @param looper May be NULL. The new looper to be installed in the paint.
699 * @return looper
700 */
701 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000702
703 enum Align {
704 kLeft_Align,
705 kCenter_Align,
706 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000707 };
708 enum {
709 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000710 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000711
reed@android.com8a1c16f2008-12-17 15:59:43 +0000712 /** Return the paint's Align value for drawing text.
713 @return the paint's Align value for drawing text.
714 */
reedf59eab22014-07-14 14:39:15 -0700715 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000716
reed@android.com8a1c16f2008-12-17 15:59:43 +0000717 /** Set the paint's text alignment.
718 @param align set the paint's Align value for drawing text.
719 */
720 void setTextAlign(Align align);
721
722 /** Return the paint's text size.
723 @return the paint's text size.
724 */
725 SkScalar getTextSize() const { return fTextSize; }
726
727 /** Set the paint's text size. This value must be > 0
728 @param textSize set the paint's text size.
729 */
730 void setTextSize(SkScalar textSize);
731
732 /** Return the paint's horizontal scale factor for text. The default value
733 is 1.0.
734 @return the paint's scale factor in X for drawing/measuring text
735 */
736 SkScalar getTextScaleX() const { return fTextScaleX; }
737
738 /** Set the paint's horizontal scale factor for text. The default value
739 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
740 stretch the text narrower.
741 @param scaleX set the paint's scale factor in X for drawing/measuring
742 text.
743 */
744 void setTextScaleX(SkScalar scaleX);
745
746 /** Return the paint's horizontal skew factor for text. The default value
747 is 0.
748 @return the paint's skew factor in X for drawing text.
749 */
750 SkScalar getTextSkewX() const { return fTextSkewX; }
751
752 /** Set the paint's horizontal skew factor for text. The default value
753 is 0. For approximating oblique text, use values around -0.25.
754 @param skewX set the paint's skew factor in X for drawing text.
755 */
756 void setTextSkewX(SkScalar skewX);
757
758 /** Describes how to interpret the text parameters that are passed to paint
759 methods like measureText() and getTextWidths().
760 */
761 enum TextEncoding {
762 kUTF8_TextEncoding, //!< the text parameters are UTF8
763 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000764 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765 kGlyphID_TextEncoding //!< the text parameters are glyph indices
766 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000767
reedf59eab22014-07-14 14:39:15 -0700768 TextEncoding getTextEncoding() const {
769 return (TextEncoding)fBitfields.fTextEncoding;
770 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771
772 void setTextEncoding(TextEncoding encoding);
773
774 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000775 /** Flags which indicate the confidence level of various metrics.
776 A set flag indicates that the metric may be trusted.
777 */
778 enum FontMetricsFlags {
779 kUnderlineThinknessIsValid_Flag = 1 << 0,
780 kUnderlinePositionIsValid_Flag = 1 << 1,
781 };
782
783 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000784 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
785 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
786 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
787 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
788 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000789 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
790 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000791 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
792 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000793 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
794 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000795 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
796
797 /** Underline Position - position of the top of the Underline stroke
798 relative to the baseline, this can have following values
799 - Negative - means underline should be drawn above baseline.
800 - Positive - means below baseline.
801 - Zero - mean underline should be drawn on baseline.
802 */
803 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
804
805 /** If the fontmetrics has a valid underlinethickness, return true, and set the
806 thickness param to that value. If it doesn't return false and ignore the
807 thickness param.
808 */
809 bool hasUnderlineThickness(SkScalar* thickness) const {
810 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
811 *thickness = fUnderlineThickness;
812 return true;
813 }
814 return false;
815 }
816
817 /** If the fontmetrics has a valid underlineposition, return true, and set the
818 thickness param to that value. If it doesn't return false and ignore the
819 thickness param.
820 */
821 bool hasUnderlinePosition(SkScalar* position) const {
822 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
823 *position = fUnderlinePosition;
824 return true;
825 }
826 return false;
827 }
828
reed@android.com8a1c16f2008-12-17 15:59:43 +0000829 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000830
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 /** Return the recommend spacing between lines (which will be
832 fDescent - fAscent + fLeading).
833 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000834 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835 @param metrics If not null, returns the font metrics for the
836 current typeface/pointsize/etc setting in this
837 paint.
838 @param scale If not 0, return width as if the canvas were scaled
839 by this value
840 @param return the recommended spacing between lines
841 */
842 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000843
reed@android.com8a1c16f2008-12-17 15:59:43 +0000844 /** Return the recommend line spacing. This will be
845 fDescent - fAscent + fLeading
846 */
847 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
848
849 /** Convert the specified text into glyph IDs, returning the number of
850 glyphs ID written. If glyphs is NULL, it is ignore and only the count
851 is returned.
852 */
853 int textToGlyphs(const void* text, size_t byteLength,
854 uint16_t glyphs[]) const;
855
reed@android.coma5dcaf62010-02-05 17:12:32 +0000856 /** Return true if all of the specified text has a corresponding non-zero
857 glyph ID. If any of the code-points in the text are not supported in
858 the typeface (i.e. the glyph ID would be zero), then return false.
859
860 If the text encoding for the paint is kGlyph_TextEncoding, then this
861 returns true if all of the specified glyph IDs are non-zero.
862 */
863 bool containsText(const void* text, size_t byteLength) const;
864
reed@android.com9d3a9852010-01-08 14:07:42 +0000865 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
866 to zero. Note: this does not look at the text-encoding setting in the
867 paint, only at the typeface.
868 */
869 void glyphsToUnichars(const uint16_t glyphs[], int count,
870 SkUnichar text[]) const;
871
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 /** Return the number of drawable units in the specified text buffer.
873 This looks at the current TextEncoding field of the paint. If you also
874 want to have the text converted into glyph IDs, call textToGlyphs
875 instead.
876 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000877 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000878 return this->textToGlyphs(text, byteLength, NULL);
879 }
880
reed@google.com44da42e2011-11-10 20:04:47 +0000881 /** Return the width of the text. This will return the vertical measure
882 * if isVerticalText() is true, in which case the returned value should
883 * be treated has a height instead of a width.
884 *
885 * @param text The text to be measured
886 * @param length Number of bytes of text to measure
887 * @param bounds If not NULL, returns the bounds of the text,
888 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000889 * @return The advance width of the text
890 */
reed99ae8812014-08-26 11:30:01 -0700891 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892
reed@google.com44da42e2011-11-10 20:04:47 +0000893 /** Return the width of the text. This will return the vertical measure
894 * if isVerticalText() is true, in which case the returned value should
895 * be treated has a height instead of a width.
896 *
897 * @param text Address of the text
898 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000899 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000900 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000901 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700902 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000904
reed@google.com44da42e2011-11-10 20:04:47 +0000905 /** Return the number of bytes of text that were measured. If
906 * isVerticalText() is true, then the vertical advances are used for
907 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000908 *
reed@google.com44da42e2011-11-10 20:04:47 +0000909 * @param text The text to be measured
910 * @param length Number of bytes of text to measure
911 * @param maxWidth Maximum width. Only the subset of text whose accumulated
912 * widths are <= maxWidth are measured.
913 * @param measuredWidth Optional. If non-null, this returns the actual
914 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000915 * @return The number of bytes of text that were measured. Will be
916 * <= length.
917 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000918 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700919 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000920
reed@google.com44da42e2011-11-10 20:04:47 +0000921 /** Return the advances for the text. These will be vertical advances if
922 * isVerticalText() returns true.
923 *
924 * @param text the text
925 * @param byteLength number of bytes to of text
926 * @param widths If not null, returns the array of advances for
927 * the glyphs. If not NULL, must be at least a large
928 * as the number of unichars in the specified text.
929 * @param bounds If not null, returns the bounds for each of
930 * character, relative to (0, 0)
931 * @return the number of unichars in the specified text.
932 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000933 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
934 SkRect bounds[] = NULL) const;
935
936 /** Return the path (outline) for the specified text.
937 Note: just like SkCanvas::drawText, this will respect the Align setting
938 in the paint.
939 */
940 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
941 SkPath* path) const;
942
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000943 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000944 const SkPoint pos[], SkPath* path) const;
945
reed8893e5f2014-12-15 13:27:26 -0800946 /**
947 * Return a rectangle that represents the union of the bounds of all
948 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
949 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
950 * on this paint.
951 */
952 SkRect getFontBounds() const;
953
reed@google.com632e1a22011-10-06 12:37:00 +0000954 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
955 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
956 bool nothingToDraw() const;
957
reed@google.coma584aed2012-05-16 14:06:02 +0000958 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000959 // would prefer to make these private...
960
reed@google.coma584aed2012-05-16 14:06:02 +0000961 /** Returns true if the current paint settings allow for fast computation of
962 bounds (i.e. there is nothing complex like a patheffect that would make
963 the bounds computation expensive.
964 */
965 bool canComputeFastBounds() const {
966 if (this->getLooper()) {
967 return this->getLooper()->canComputeFastBounds(*this);
968 }
969 return !this->getRasterizer();
970 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000971
reed@google.coma584aed2012-05-16 14:06:02 +0000972 /** Only call this if canComputeFastBounds() returned true. This takes a
973 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
974 effects in the paint (e.g. stroking). If needed, it uses the storage
975 rect parameter. It returns the adjusted bounds that can then be used
976 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000977
reed@google.coma584aed2012-05-16 14:06:02 +0000978 The returned rect will either be orig or storage, thus the caller
979 should not rely on storage being set to the result, but should always
980 use the retured value. It is legal for orig and storage to be the same
981 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000982
reed@google.coma584aed2012-05-16 14:06:02 +0000983 e.g.
984 if (paint.canComputeFastBounds()) {
985 SkRect r, storage;
986 path.computeBounds(&r, SkPath::kFast_BoundsType);
987 const SkRect& fastR = paint.computeFastBounds(r, &storage);
988 if (canvas->quickReject(fastR, ...)) {
989 // don't draw the path
990 }
991 }
992 */
993 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
994 SkPaint::Style style = this->getStyle();
995 // ultra fast-case: filling with no effects that affect geometry
996 if (kFill_Style == style) {
997 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
998 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
999 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +00001000 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +00001001 if (!effects) {
1002 return orig;
1003 }
1004 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001005
reed@google.coma584aed2012-05-16 14:06:02 +00001006 return this->doComputeFastBounds(orig, storage, style);
1007 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001008
reed@google.coma584aed2012-05-16 14:06:02 +00001009 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1010 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001011 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001012 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001013
reed@google.coma584aed2012-05-16 14:06:02 +00001014 // Take the style explicitly, so the caller can force us to be stroked
1015 // without having to make a copy of the paint just to change that field.
1016 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1017 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001018
reed@google.comed43dff2013-06-04 16:56:27 +00001019 /**
1020 * Return a matrix that applies the paint's text values: size, scale, skew
1021 */
1022 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1023 SkScalar scaleX, SkScalar skewX) {
1024 matrix->setScale(size * scaleX, size);
1025 if (skewX) {
1026 matrix->postSkew(skewX, 0);
1027 }
1028 return matrix;
1029 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001030
reed@google.comed43dff2013-06-04 16:56:27 +00001031 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1032 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1033 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001034
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001035 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001036
reed@google.comd5f20792012-05-16 14:15:02 +00001037private:
1038 SkTypeface* fTypeface;
reed@google.comd5f20792012-05-16 14:15:02 +00001039 SkPathEffect* fPathEffect;
1040 SkShader* fShader;
1041 SkXfermode* fXfermode;
1042 SkMaskFilter* fMaskFilter;
1043 SkColorFilter* fColorFilter;
1044 SkRasterizer* fRasterizer;
1045 SkDrawLooper* fLooper;
1046 SkImageFilter* fImageFilter;
reed@google.comb0a34d82012-07-11 19:57:55 +00001047 SkAnnotation* fAnnotation;
reed@google.comd5f20792012-05-16 14:15:02 +00001048
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001049 SkScalar fTextSize;
1050 SkScalar fTextScaleX;
1051 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001052 SkColor fColor;
1053 SkScalar fWidth;
1054 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001055 union {
1056 struct {
1057 // all of these bitfields should add up to 32
1058 unsigned fFlags : 16;
1059 unsigned fTextAlign : 2;
1060 unsigned fCapType : 2;
1061 unsigned fJoinType : 2;
1062 unsigned fStyle : 2;
1063 unsigned fTextEncoding : 2; // 3 values
1064 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001065 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001066 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001067 } fBitfields;
1068 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001069 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001070
reed@google.comd5f20792012-05-16 14:15:02 +00001071 SkDrawCacheProc getDrawCacheProc() const;
reed9e96aa02014-10-03 12:44:37 -07001072 SkMeasureCacheProc getMeasureCacheProc(bool needFullMetrics) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001073
1074 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1075 int* count, SkRect* bounds) const;
1076
jvanverth2d2a68c2014-06-10 06:42:56 -07001077 SkGlyphCache* detachCache(const SkDeviceProperties* deviceProperties, const SkMatrix*,
1078 bool ignoreGamma) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001079
bungeman@google.com532470f2013-01-22 19:25:14 +00001080 void descriptorProc(const SkDeviceProperties* deviceProperties, const SkMatrix* deviceMatrix,
reed@google.com90808e82013-03-19 14:44:54 +00001081 void (*proc)(SkTypeface*, const SkDescriptor*, void*),
reed@google.comd5f20792012-05-16 14:15:02 +00001082 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +00001083
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001084 static void Term();
1085
reed@android.com8a1c16f2008-12-17 15:59:43 +00001086 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001087 /* This is the size we use when we ask for a glyph's path. We then
1088 * post-transform it as we draw to match the request.
1089 * This is done to try to re-use cache entries for the path.
1090 *
1091 * This value is somewhat arbitrary. In theory, it could be 1, since
1092 * we store paths as floats. However, we get the path from the font
1093 * scaler, and it may represent its paths as fixed-point (or 26.6),
1094 * so we shouldn't ask for something too big (might overflow 16.16)
1095 * or too small (underflow 26.6).
1096 *
1097 * This value could track kMaxSizeForGlyphCache, assuming the above
1098 * constraints, but since we ask for unhinted paths, the two values
1099 * need not match per-se.
1100 */
1101 kCanonicalTextSizeForPaths = 64,
1102
1103 /*
1104 * Above this size (taking into account CTM and textSize), we never use
1105 * the cache for bits or metrics (we might overflow), so we just ask
1106 * for a caononical size and post-transform that.
1107 */
1108 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001109 };
reed@google.comed43dff2013-06-04 16:56:27 +00001110
1111 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1112
reed@google.comed43dff2013-06-04 16:56:27 +00001113 // Set flags/hinting/textSize up to use for drawing text as paths.
1114 // Returns scale factor to restore the original textSize, since will will
1115 // have change it to kCanonicalTextSizeForPaths.
1116 SkScalar setupForAsPaths();
1117
1118 static SkScalar MaxCacheSize2() {
1119 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1120 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1121 return kMag2Max;
1122 }
1123
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001124 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001125 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001126 friend class SkCanvas;
1127 friend class SkDraw;
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001128 friend class SkGraphics; // So Term() can be called.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001129 friend class SkPDFDevice;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00001130 friend class GrBitmapTextContext;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001131 friend class GrDistanceFieldTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001132 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001133 friend class GrPathRendering;
1134 friend class GrGLPathRendering;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001135 friend class SkTextToPathIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001136 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001137};
1138
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139#endif