blob: 50fe1f4c1df20399760fa26c2aedfd665a9b8ce4 [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.
reed05d90442015-02-12 13:35:52 -0800515 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision
516 * in favor of speed/size.
reed@google.com4bbdeac2013-01-24 21:03:11 +0000517 * @return true if the path should be filled, or false if it should be
518 * drawn with a hairline (width == 0)
519 */
reed05d90442015-02-12 13:35:52 -0800520 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
521 SkScalar resScale = 1) const;
522
523 bool getFillPath(const SkPath& src, SkPath* dst) const {
524 return this->getFillPath(src, dst, NULL, 1);
525 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527 /** Get the paint's shader object.
528 <p />
529 The shader's reference count is not affected.
530 @return the paint's shader (or NULL)
531 */
532 SkShader* getShader() const { return fShader; }
533
534 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000535 * Shaders specify the source color(s) for what is being drawn. If a paint
536 * has no shader, then the paint's color is used. If the paint has a
537 * shader, then the shader's color(s) are use instead, but they are
538 * modulated by the paint's alpha. This makes it easy to create a shader
539 * once (e.g. bitmap tiling or gradient) and then change its transparency
540 * w/o having to modify the original shader... only the paint's alpha needs
541 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000542 *
543 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
544 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
545 * then the shader will use the paint's entire color to "colorize" its output (modulating the
546 * bitmap's alpha with the paint's color+alpha).
547 *
reed@google.com880dc472012-05-11 14:47:03 +0000548 * Pass NULL to clear any previous shader.
549 * As a convenience, the parameter passed is also returned.
550 * If a previous shader exists, its reference count is decremented.
551 * If shader is not NULL, its reference count is incremented.
552 * @param shader May be NULL. The shader to be installed in the paint
553 * @return shader
554 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000556
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557 /** Get the paint's colorfilter. If there is a colorfilter, its reference
558 count is not changed.
559 @return the paint's colorfilter (or NULL)
560 */
561 SkColorFilter* getColorFilter() const { return fColorFilter; }
562
563 /** Set or clear the paint's colorfilter, returning the parameter.
564 <p />
565 If the paint already has a filter, its reference count is decremented.
566 If filter is not NULL, its reference count is incremented.
567 @param filter May be NULL. The filter to be installed in the paint
568 @return filter
569 */
570 SkColorFilter* setColorFilter(SkColorFilter* filter);
571
572 /** Get the paint's xfermode object.
573 <p />
574 The xfermode's reference count is not affected.
575 @return the paint's xfermode (or NULL)
576 */
577 SkXfermode* getXfermode() const { return fXfermode; }
578
579 /** Set or clear the xfermode object.
580 <p />
581 Pass NULL to clear any previous xfermode.
582 As a convenience, the parameter passed is also returned.
583 If a previous xfermode exists, its reference count is decremented.
584 If xfermode is not NULL, its reference count is incremented.
585 @param xfermode May be NULL. The new xfermode to be installed in the
586 paint
587 @return xfermode
588 */
589 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000590
591 /** Create an xfermode based on the specified Mode, and assign it into the
592 paint, returning the mode that was set. If the Mode is SrcOver, then
593 the paint's xfermode is set to null.
594 */
reed@android.com0baf1932009-06-24 12:41:42 +0000595 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000596
reed@android.com8a1c16f2008-12-17 15:59:43 +0000597 /** Get the paint's patheffect object.
598 <p />
599 The patheffect reference count is not affected.
600 @return the paint's patheffect (or NULL)
601 */
602 SkPathEffect* getPathEffect() const { return fPathEffect; }
603
604 /** Set or clear the patheffect object.
605 <p />
606 Pass NULL to clear any previous patheffect.
607 As a convenience, the parameter passed is also returned.
608 If a previous patheffect exists, its reference count is decremented.
609 If patheffect is not NULL, its reference count is incremented.
610 @param effect May be NULL. The new patheffect to be installed in the
611 paint
612 @return effect
613 */
614 SkPathEffect* setPathEffect(SkPathEffect* effect);
615
616 /** Get the paint's maskfilter object.
617 <p />
618 The maskfilter reference count is not affected.
619 @return the paint's maskfilter (or NULL)
620 */
621 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
622
623 /** Set or clear the maskfilter object.
624 <p />
625 Pass NULL to clear any previous maskfilter.
626 As a convenience, the parameter passed is also returned.
627 If a previous maskfilter exists, its reference count is decremented.
628 If maskfilter is not NULL, its reference count is incremented.
629 @param maskfilter May be NULL. The new maskfilter to be installed in
630 the paint
631 @return maskfilter
632 */
633 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
634
635 // These attributes are for text/fonts
636
637 /** Get the paint's typeface object.
638 <p />
639 The typeface object identifies which font to use when drawing or
640 measuring text. The typeface reference count is not affected.
641 @return the paint's typeface (or NULL)
642 */
643 SkTypeface* getTypeface() const { return fTypeface; }
644
645 /** Set or clear the typeface object.
646 <p />
647 Pass NULL to clear any previous typeface.
648 As a convenience, the parameter passed is also returned.
649 If a previous typeface exists, its reference count is decremented.
650 If typeface is not NULL, its reference count is incremented.
651 @param typeface May be NULL. The new typeface to be installed in the
652 paint
653 @return typeface
654 */
655 SkTypeface* setTypeface(SkTypeface* typeface);
656
657 /** Get the paint's rasterizer (or NULL).
658 <p />
659 The raster controls how paths/text are turned into alpha masks.
660 @return the paint's rasterizer (or NULL)
661 */
662 SkRasterizer* getRasterizer() const { return fRasterizer; }
663
664 /** Set or clear the rasterizer object.
665 <p />
666 Pass NULL to clear any previous rasterizer.
667 As a convenience, the parameter passed is also returned.
668 If a previous rasterizer exists in the paint, its reference count is
669 decremented. If rasterizer is not NULL, its reference count is
670 incremented.
671 @param rasterizer May be NULL. The new rasterizer to be installed in
672 the paint.
673 @return rasterizer
674 */
675 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
676
reed@google.com15356a62011-11-03 19:29:08 +0000677 SkImageFilter* getImageFilter() const { return fImageFilter; }
678 SkImageFilter* setImageFilter(SkImageFilter*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000679
reed@google.comb0a34d82012-07-11 19:57:55 +0000680 SkAnnotation* getAnnotation() const { return fAnnotation; }
681 SkAnnotation* setAnnotation(SkAnnotation*);
682
683 /**
684 * Returns true if there is an annotation installed on this paint, and
685 * the annotation specifics no-drawing.
686 */
reed@google.com44699382013-10-31 17:28:30 +0000687 SK_ATTR_DEPRECATED("use getAnnotation and check for non-null")
commit-bot@chromium.org950923b2013-10-29 20:44:39 +0000688 bool isNoDrawAnnotation() const { return this->getAnnotation() != NULL; }
reed@google.com15356a62011-11-03 19:29:08 +0000689
reed@google.com9d07fec2011-03-16 20:02:59 +0000690 /**
691 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
692 * reference count.
693 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000694 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000695
696 /**
697 * Set or clear the looper object.
698 * <p />
699 * Pass NULL to clear any previous looper.
700 * As a convenience, the parameter passed is also returned.
701 * If a previous looper exists in the paint, its reference count is
702 * decremented. If looper is not NULL, its reference count is
703 * incremented.
704 * @param looper May be NULL. The new looper to be installed in the paint.
705 * @return looper
706 */
707 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000708
709 enum Align {
710 kLeft_Align,
711 kCenter_Align,
712 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000713 };
714 enum {
715 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000717
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718 /** Return the paint's Align value for drawing text.
719 @return the paint's Align value for drawing text.
720 */
reedf59eab22014-07-14 14:39:15 -0700721 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000722
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723 /** Set the paint's text alignment.
724 @param align set the paint's Align value for drawing text.
725 */
726 void setTextAlign(Align align);
727
728 /** Return the paint's text size.
729 @return the paint's text size.
730 */
731 SkScalar getTextSize() const { return fTextSize; }
732
733 /** Set the paint's text size. This value must be > 0
734 @param textSize set the paint's text size.
735 */
736 void setTextSize(SkScalar textSize);
737
738 /** Return the paint's horizontal scale factor for text. The default value
739 is 1.0.
740 @return the paint's scale factor in X for drawing/measuring text
741 */
742 SkScalar getTextScaleX() const { return fTextScaleX; }
743
744 /** Set the paint's horizontal scale factor for text. The default value
745 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
746 stretch the text narrower.
747 @param scaleX set the paint's scale factor in X for drawing/measuring
748 text.
749 */
750 void setTextScaleX(SkScalar scaleX);
751
752 /** Return the paint's horizontal skew factor for text. The default value
753 is 0.
754 @return the paint's skew factor in X for drawing text.
755 */
756 SkScalar getTextSkewX() const { return fTextSkewX; }
757
758 /** Set the paint's horizontal skew factor for text. The default value
759 is 0. For approximating oblique text, use values around -0.25.
760 @param skewX set the paint's skew factor in X for drawing text.
761 */
762 void setTextSkewX(SkScalar skewX);
763
764 /** Describes how to interpret the text parameters that are passed to paint
765 methods like measureText() and getTextWidths().
766 */
767 enum TextEncoding {
768 kUTF8_TextEncoding, //!< the text parameters are UTF8
769 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000770 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771 kGlyphID_TextEncoding //!< the text parameters are glyph indices
772 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000773
reedf59eab22014-07-14 14:39:15 -0700774 TextEncoding getTextEncoding() const {
775 return (TextEncoding)fBitfields.fTextEncoding;
776 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000777
778 void setTextEncoding(TextEncoding encoding);
779
780 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000781 /** Flags which indicate the confidence level of various metrics.
782 A set flag indicates that the metric may be trusted.
783 */
784 enum FontMetricsFlags {
785 kUnderlineThinknessIsValid_Flag = 1 << 0,
786 kUnderlinePositionIsValid_Flag = 1 << 1,
787 };
788
789 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000790 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
791 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
792 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
793 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
794 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000795 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
796 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000797 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
798 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000799 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
800 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000801 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
802
803 /** Underline Position - position of the top of the Underline stroke
804 relative to the baseline, this can have following values
805 - Negative - means underline should be drawn above baseline.
806 - Positive - means below baseline.
807 - Zero - mean underline should be drawn on baseline.
808 */
809 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
810
811 /** If the fontmetrics has a valid underlinethickness, return true, and set the
812 thickness param to that value. If it doesn't return false and ignore the
813 thickness param.
814 */
815 bool hasUnderlineThickness(SkScalar* thickness) const {
816 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
817 *thickness = fUnderlineThickness;
818 return true;
819 }
820 return false;
821 }
822
823 /** If the fontmetrics has a valid underlineposition, return true, and set the
824 thickness param to that value. If it doesn't return false and ignore the
825 thickness param.
826 */
827 bool hasUnderlinePosition(SkScalar* position) const {
828 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
829 *position = fUnderlinePosition;
830 return true;
831 }
832 return false;
833 }
834
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000836
reed@android.com8a1c16f2008-12-17 15:59:43 +0000837 /** Return the recommend spacing between lines (which will be
838 fDescent - fAscent + fLeading).
839 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000840 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841 @param metrics If not null, returns the font metrics for the
842 current typeface/pointsize/etc setting in this
843 paint.
844 @param scale If not 0, return width as if the canvas were scaled
845 by this value
846 @param return the recommended spacing between lines
847 */
848 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000849
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850 /** Return the recommend line spacing. This will be
851 fDescent - fAscent + fLeading
852 */
853 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
854
855 /** Convert the specified text into glyph IDs, returning the number of
856 glyphs ID written. If glyphs is NULL, it is ignore and only the count
857 is returned.
858 */
859 int textToGlyphs(const void* text, size_t byteLength,
860 uint16_t glyphs[]) const;
861
reed@android.coma5dcaf62010-02-05 17:12:32 +0000862 /** Return true if all of the specified text has a corresponding non-zero
863 glyph ID. If any of the code-points in the text are not supported in
864 the typeface (i.e. the glyph ID would be zero), then return false.
865
866 If the text encoding for the paint is kGlyph_TextEncoding, then this
867 returns true if all of the specified glyph IDs are non-zero.
868 */
869 bool containsText(const void* text, size_t byteLength) const;
870
reed@android.com9d3a9852010-01-08 14:07:42 +0000871 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
872 to zero. Note: this does not look at the text-encoding setting in the
873 paint, only at the typeface.
874 */
875 void glyphsToUnichars(const uint16_t glyphs[], int count,
876 SkUnichar text[]) const;
877
reed@android.com8a1c16f2008-12-17 15:59:43 +0000878 /** Return the number of drawable units in the specified text buffer.
879 This looks at the current TextEncoding field of the paint. If you also
880 want to have the text converted into glyph IDs, call textToGlyphs
881 instead.
882 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000883 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000884 return this->textToGlyphs(text, byteLength, NULL);
885 }
886
reed@google.com44da42e2011-11-10 20:04:47 +0000887 /** Return the width of the text. This will return the vertical measure
888 * if isVerticalText() is true, in which case the returned value should
889 * be treated has a height instead of a width.
890 *
891 * @param text The text to be measured
892 * @param length Number of bytes of text to measure
893 * @param bounds If not NULL, returns the bounds of the text,
894 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000895 * @return The advance width of the text
896 */
reed99ae8812014-08-26 11:30:01 -0700897 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898
reed@google.com44da42e2011-11-10 20:04:47 +0000899 /** Return the width of the text. This will return the vertical measure
900 * if isVerticalText() is true, in which case the returned value should
901 * be treated has a height instead of a width.
902 *
903 * @param text Address of the text
904 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000905 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000906 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000907 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700908 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000909 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000910
reed@google.com44da42e2011-11-10 20:04:47 +0000911 /** Return the number of bytes of text that were measured. If
912 * isVerticalText() is true, then the vertical advances are used for
913 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000914 *
reed@google.com44da42e2011-11-10 20:04:47 +0000915 * @param text The text to be measured
916 * @param length Number of bytes of text to measure
917 * @param maxWidth Maximum width. Only the subset of text whose accumulated
918 * widths are <= maxWidth are measured.
919 * @param measuredWidth Optional. If non-null, this returns the actual
920 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000921 * @return The number of bytes of text that were measured. Will be
922 * <= length.
923 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000924 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700925 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000926
reed@google.com44da42e2011-11-10 20:04:47 +0000927 /** Return the advances for the text. These will be vertical advances if
928 * isVerticalText() returns true.
929 *
930 * @param text the text
931 * @param byteLength number of bytes to of text
932 * @param widths If not null, returns the array of advances for
933 * the glyphs. If not NULL, must be at least a large
934 * as the number of unichars in the specified text.
935 * @param bounds If not null, returns the bounds for each of
936 * character, relative to (0, 0)
937 * @return the number of unichars in the specified text.
938 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000939 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
940 SkRect bounds[] = NULL) const;
941
942 /** Return the path (outline) for the specified text.
943 Note: just like SkCanvas::drawText, this will respect the Align setting
944 in the paint.
945 */
946 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
947 SkPath* path) const;
948
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000949 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000950 const SkPoint pos[], SkPath* path) const;
951
reed8893e5f2014-12-15 13:27:26 -0800952 /**
953 * Return a rectangle that represents the union of the bounds of all
954 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
955 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
956 * on this paint.
957 */
958 SkRect getFontBounds() const;
959
reed@google.com632e1a22011-10-06 12:37:00 +0000960 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
961 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
962 bool nothingToDraw() const;
963
reed@google.coma584aed2012-05-16 14:06:02 +0000964 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000965 // would prefer to make these private...
966
reed@google.coma584aed2012-05-16 14:06:02 +0000967 /** Returns true if the current paint settings allow for fast computation of
968 bounds (i.e. there is nothing complex like a patheffect that would make
969 the bounds computation expensive.
970 */
971 bool canComputeFastBounds() const {
972 if (this->getLooper()) {
973 return this->getLooper()->canComputeFastBounds(*this);
974 }
975 return !this->getRasterizer();
976 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000977
reed@google.coma584aed2012-05-16 14:06:02 +0000978 /** Only call this if canComputeFastBounds() returned true. This takes a
979 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
980 effects in the paint (e.g. stroking). If needed, it uses the storage
981 rect parameter. It returns the adjusted bounds that can then be used
982 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000983
reed@google.coma584aed2012-05-16 14:06:02 +0000984 The returned rect will either be orig or storage, thus the caller
985 should not rely on storage being set to the result, but should always
986 use the retured value. It is legal for orig and storage to be the same
987 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000988
reed@google.coma584aed2012-05-16 14:06:02 +0000989 e.g.
990 if (paint.canComputeFastBounds()) {
991 SkRect r, storage;
992 path.computeBounds(&r, SkPath::kFast_BoundsType);
993 const SkRect& fastR = paint.computeFastBounds(r, &storage);
994 if (canvas->quickReject(fastR, ...)) {
995 // don't draw the path
996 }
997 }
998 */
999 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
1000 SkPaint::Style style = this->getStyle();
1001 // ultra fast-case: filling with no effects that affect geometry
1002 if (kFill_Style == style) {
1003 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
1004 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
1005 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +00001006 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +00001007 if (!effects) {
1008 return orig;
1009 }
1010 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001011
reed@google.coma584aed2012-05-16 14:06:02 +00001012 return this->doComputeFastBounds(orig, storage, style);
1013 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001014
reed@google.coma584aed2012-05-16 14:06:02 +00001015 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1016 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001017 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001018 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001019
reed@google.coma584aed2012-05-16 14:06:02 +00001020 // Take the style explicitly, so the caller can force us to be stroked
1021 // without having to make a copy of the paint just to change that field.
1022 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1023 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001024
reed@google.comed43dff2013-06-04 16:56:27 +00001025 /**
1026 * Return a matrix that applies the paint's text values: size, scale, skew
1027 */
1028 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1029 SkScalar scaleX, SkScalar skewX) {
1030 matrix->setScale(size * scaleX, size);
1031 if (skewX) {
1032 matrix->postSkew(skewX, 0);
1033 }
1034 return matrix;
1035 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001036
reed@google.comed43dff2013-06-04 16:56:27 +00001037 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1038 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1039 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001040
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001041 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001042
reed@google.comd5f20792012-05-16 14:15:02 +00001043private:
1044 SkTypeface* fTypeface;
reed@google.comd5f20792012-05-16 14:15:02 +00001045 SkPathEffect* fPathEffect;
1046 SkShader* fShader;
1047 SkXfermode* fXfermode;
1048 SkMaskFilter* fMaskFilter;
1049 SkColorFilter* fColorFilter;
1050 SkRasterizer* fRasterizer;
1051 SkDrawLooper* fLooper;
1052 SkImageFilter* fImageFilter;
reed@google.comb0a34d82012-07-11 19:57:55 +00001053 SkAnnotation* fAnnotation;
reed@google.comd5f20792012-05-16 14:15:02 +00001054
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001055 SkScalar fTextSize;
1056 SkScalar fTextScaleX;
1057 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001058 SkColor fColor;
1059 SkScalar fWidth;
1060 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001061 union {
1062 struct {
1063 // all of these bitfields should add up to 32
1064 unsigned fFlags : 16;
1065 unsigned fTextAlign : 2;
1066 unsigned fCapType : 2;
1067 unsigned fJoinType : 2;
1068 unsigned fStyle : 2;
1069 unsigned fTextEncoding : 2; // 3 values
1070 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001071 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001072 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001073 } fBitfields;
1074 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001075 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001076
reed@google.comd5f20792012-05-16 14:15:02 +00001077 SkDrawCacheProc getDrawCacheProc() const;
reed9e96aa02014-10-03 12:44:37 -07001078 SkMeasureCacheProc getMeasureCacheProc(bool needFullMetrics) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001079
1080 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1081 int* count, SkRect* bounds) const;
1082
jvanverth2d2a68c2014-06-10 06:42:56 -07001083 SkGlyphCache* detachCache(const SkDeviceProperties* deviceProperties, const SkMatrix*,
1084 bool ignoreGamma) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001085
bungeman@google.com532470f2013-01-22 19:25:14 +00001086 void descriptorProc(const SkDeviceProperties* deviceProperties, const SkMatrix* deviceMatrix,
reed@google.com90808e82013-03-19 14:44:54 +00001087 void (*proc)(SkTypeface*, const SkDescriptor*, void*),
reed@google.comd5f20792012-05-16 14:15:02 +00001088 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +00001089
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001090 static void Term();
1091
reed@android.com8a1c16f2008-12-17 15:59:43 +00001092 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001093 /* This is the size we use when we ask for a glyph's path. We then
1094 * post-transform it as we draw to match the request.
1095 * This is done to try to re-use cache entries for the path.
1096 *
1097 * This value is somewhat arbitrary. In theory, it could be 1, since
1098 * we store paths as floats. However, we get the path from the font
1099 * scaler, and it may represent its paths as fixed-point (or 26.6),
1100 * so we shouldn't ask for something too big (might overflow 16.16)
1101 * or too small (underflow 26.6).
1102 *
1103 * This value could track kMaxSizeForGlyphCache, assuming the above
1104 * constraints, but since we ask for unhinted paths, the two values
1105 * need not match per-se.
1106 */
1107 kCanonicalTextSizeForPaths = 64,
1108
1109 /*
1110 * Above this size (taking into account CTM and textSize), we never use
1111 * the cache for bits or metrics (we might overflow), so we just ask
1112 * for a caononical size and post-transform that.
1113 */
1114 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115 };
reed@google.comed43dff2013-06-04 16:56:27 +00001116
1117 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1118
reed@google.comed43dff2013-06-04 16:56:27 +00001119 // Set flags/hinting/textSize up to use for drawing text as paths.
1120 // Returns scale factor to restore the original textSize, since will will
1121 // have change it to kCanonicalTextSizeForPaths.
1122 SkScalar setupForAsPaths();
1123
1124 static SkScalar MaxCacheSize2() {
1125 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1126 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1127 return kMag2Max;
1128 }
1129
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001130 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001131 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132 friend class SkCanvas;
1133 friend class SkDraw;
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001134 friend class SkGraphics; // So Term() can be called.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001135 friend class SkPDFDevice;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00001136 friend class GrBitmapTextContext;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001137 friend class GrDistanceFieldTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001138 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001139 friend class GrPathRendering;
1140 friend class GrGLPathRendering;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141 friend class SkTextToPathIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001142 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001143};
1144
reed@android.com8a1c16f2008-12-17 15:59:43 +00001145#endif