blob: bb5eff4d91cc47084e3e92ad5145e7c6a1d3e367 [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;
joshualittfd450792015-03-13 08:38:43 -070023class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000024class SkDescriptor;
bungeman@google.com532470f2013-01-22 19:25:14 +000025struct SkDeviceProperties;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000026class SkReadBuffer;
27class SkWriteBuffer;
herbb69d0e02015-02-25 06:47:06 -080028class SkGlyph;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029struct SkRect;
30class SkGlyphCache;
reed@google.com15356a62011-11-03 19:29:08 +000031class SkImageFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000032class SkMaskFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkPath;
34class SkPathEffect;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000035struct SkPoint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000036class SkRasterizer;
37class SkShader;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
40typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
41 SkFixed x, SkFixed y);
42
43typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
44
humper@google.comb0889472013-07-09 21:37:14 +000045#define kBicubicFilterBitmap_Flag kHighQualityFilterBitmap_Flag
46
reed@android.com8a1c16f2008-12-17 15:59:43 +000047/** \class SkPaint
48
49 The SkPaint class holds the style and color information about how to draw
50 geometries, text and bitmaps.
51*/
humper@google.comb0889472013-07-09 21:37:14 +000052
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000053class SK_API SkPaint {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054public:
55 SkPaint();
56 SkPaint(const SkPaint& paint);
57 ~SkPaint();
58
59 SkPaint& operator=(const SkPaint&);
60
mtkleinbc97ef42014-08-25 10:10:47 -070061 /** operator== may give false negatives: two paints that draw equivalently
62 may return false. It will never give false positives: two paints that
63 are not equivalent always return false.
64 */
robertphillips@google.comb2657412013-08-07 22:36:29 +000065 SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
66 friend bool operator!=(const SkPaint& a, const SkPaint& b) {
67 return !(a == b);
68 }
69
mtkleinfb1fe4f2014-10-07 09:26:10 -070070 /** getHash() is a shallow hash, with the same limitations as operator==.
71 * If operator== returns true for two paints, getHash() returns the same value for each.
72 */
73 uint32_t getHash() const;
74
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000075 void flatten(SkWriteBuffer&) const;
76 void unflatten(SkReadBuffer&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
78 /** Restores the paint to its initial settings.
79 */
80 void reset();
81
agl@chromium.org309485b2009-07-21 17:41:32 +000082 /** Specifies the level of hinting to be performed. These names are taken
83 from the Gnome/Cairo names for the same. They are translated into
84 Freetype concepts the same as in cairo-ft-font.c:
85 kNo_Hinting -> FT_LOAD_NO_HINTING
86 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
87 kNormal_Hinting -> <default, no option>
88 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
89 subpixel glyphs, in which case TARGET_LCD or
90 TARGET_LCD_V is used>
91 */
92 enum Hinting {
93 kNo_Hinting = 0,
94 kSlight_Hinting = 1,
95 kNormal_Hinting = 2, //!< this is the default
tomhudson@google.com1f902872012-06-01 13:15:47 +000096 kFull_Hinting = 3
agl@chromium.org309485b2009-07-21 17:41:32 +000097 };
98
reed@google.com9d07fec2011-03-16 20:02:59 +000099 Hinting getHinting() const {
reedf59eab22014-07-14 14:39:15 -0700100 return static_cast<Hinting>(fBitfields.fHinting);
agl@chromium.org309485b2009-07-21 17:41:32 +0000101 }
102
djsollen@google.comf5dbe2f2011-04-15 13:41:26 +0000103 void setHinting(Hinting hintingLevel);
agl@chromium.org309485b2009-07-21 17:41:32 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 /** Specifies the bit values that are stored in the paint's flags.
106 */
107 enum Flags {
108 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 kDither_Flag = 0x04, //!< mask to enable dithering
110 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
111 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
112 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
113 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000114 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000116 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000117 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000118 kAutoHinting_Flag = 0x800, //!< mask to force Freetype's autohinter
reed@google.com830a23e2011-11-10 15:20:49 +0000119 kVerticalText_Flag = 0x1000,
reed@google.com8351aab2012-01-18 17:06:35 +0000120 kGenA8FromLCD_Flag = 0x2000, // hack for GDI -- do not use if you can help it
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000121 kDistanceFieldTextTEMP_Flag = 0x4000, //!< TEMPORARY mask to enable distance fields
122 // currently overrides LCD and subpixel rendering
agl@chromium.org309485b2009-07-21 17:41:32 +0000123 // when adding extra flags, note that the fFlags member is specified
124 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125
humper@google.com387db0a2013-07-09 14:13:04 +0000126 kAllFlags = 0xFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 };
128
129 /** Return the paint's flags. Use the Flag enum to test flag values.
130 @return the paint's flags (see enums ending in _Flag for bit masks)
131 */
reedf59eab22014-07-14 14:39:15 -0700132 uint32_t getFlags() const { return fBitfields.fFlags; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133
134 /** Set the paint's flags. Use the Flag enum to specific flag values.
135 @param flags The new flag bits for the paint (see Flags enum)
136 */
137 void setFlags(uint32_t flags);
138
139 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
140 @return true if the antialias bit is set in the paint's flags.
141 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000142 bool isAntiAlias() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 return SkToBool(this->getFlags() & kAntiAlias_Flag);
144 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
147 @param aa true to enable antialiasing, false to disable it
148 */
149 void setAntiAlias(bool aa);
reed@google.com9d07fec2011-03-16 20:02:59 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 /** Helper for getFlags(), returning true if kDither_Flag bit is set
152 @return true if the dithering bit is set in the paint's flags.
153 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000154 bool isDither() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 return SkToBool(this->getFlags() & kDither_Flag);
156 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
159 @param dither true to enable dithering, false to disable it
160 */
161 void setDither(bool dither);
reed@google.com9d07fec2011-03-16 20:02:59 +0000162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
164 @return true if the lineartext bit is set in the paint's flags
165 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000166 bool isLinearText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 return SkToBool(this->getFlags() & kLinearText_Flag);
168 }
169
170 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
171 @param linearText true to set the linearText bit in the paint's flags,
172 false to clear it.
173 */
174 void setLinearText(bool linearText);
175
176 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
177 @return true if the lineartext bit is set in the paint's flags
178 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000179 bool isSubpixelText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 return SkToBool(this->getFlags() & kSubpixelText_Flag);
181 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000182
reed@google.com84b437e2011-08-01 12:45:35 +0000183 /**
184 * Helper for setFlags(), setting or clearing the kSubpixelText_Flag.
185 * @param subpixelText true to set the subpixelText bit in the paint's
186 * flags, false to clear it.
187 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000189
reed@google.com9d07fec2011-03-16 20:02:59 +0000190 bool isLCDRenderText() const {
agl@chromium.org309485b2009-07-21 17:41:32 +0000191 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
192 }
193
reed@google.com84b437e2011-08-01 12:45:35 +0000194 /**
195 * Helper for setFlags(), setting or clearing the kLCDRenderText_Flag.
196 * Note: antialiasing must also be on for lcd rendering
197 * @param lcdText true to set the LCDRenderText bit in the paint's flags,
198 * false to clear it.
199 */
200 void setLCDRenderText(bool lcdText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000201
reed@google.com9d07fec2011-03-16 20:02:59 +0000202 bool isEmbeddedBitmapText() const {
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000203 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
204 }
205
206 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
207 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
208 false to clear it.
209 */
210 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
211
reed@google.com9d07fec2011-03-16 20:02:59 +0000212 bool isAutohinted() const {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000213 return SkToBool(this->getFlags() & kAutoHinting_Flag);
214 }
215
216 /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
217 @param useAutohinter true to set the kEmbeddedBitmapText bit in the
218 paint's flags,
219 false to clear it.
220 */
221 void setAutohinted(bool useAutohinter);
222
reed@google.com830a23e2011-11-10 15:20:49 +0000223 bool isVerticalText() const {
224 return SkToBool(this->getFlags() & kVerticalText_Flag);
225 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000226
reed@google.com830a23e2011-11-10 15:20:49 +0000227 /**
228 * Helper for setting or clearing the kVerticalText_Flag bit in
229 * setFlags(...).
230 *
231 * If this bit is set, then advances are treated as Y values rather than
232 * X values, and drawText will places its glyphs vertically rather than
233 * horizontally.
234 */
235 void setVerticalText(bool);
236
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
238 @return true if the underlineText bit is set in the paint's flags.
239 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000240 bool isUnderlineText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 return SkToBool(this->getFlags() & kUnderlineText_Flag);
242 }
243
244 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
245 @param underlineText true to set the underlineText bit in the paint's
246 flags, false to clear it.
247 */
248 void setUnderlineText(bool underlineText);
249
250 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
251 @return true if the strikeThruText bit is set in the paint's flags.
252 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000253 bool isStrikeThruText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
255 }
256
257 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
258 @param strikeThruText true to set the strikeThruText bit in the
259 paint's flags, false to clear it.
260 */
261 void setStrikeThruText(bool strikeThruText);
262
263 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
264 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
265 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000266 bool isFakeBoldText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
268 }
269
270 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
271 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
272 flags, false to clear it.
273 */
274 void setFakeBoldText(bool fakeBoldText);
275
276 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
277 @return true if the kernText bit is set in the paint's flags.
278 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000279 bool isDevKernText() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 return SkToBool(this->getFlags() & kDevKernText_Flag);
281 }
282
283 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
284 @param kernText true to set the kKernText_Flag bit in the paint's
285 flags, false to clear it.
286 */
287 void setDevKernText(bool devKernText);
288
commit-bot@chromium.orgb97c3ff2014-03-11 17:07:15 +0000289 /** Helper for getFlags(), returns true if kDistanceFieldTextTEMP_Flag bit is set
290 @return true if the distanceFieldText bit is set in the paint's flags.
291 */
292 bool isDistanceFieldTextTEMP() const {
293 return SkToBool(this->getFlags() & kDistanceFieldTextTEMP_Flag);
294 }
295
296 /** Helper for setFlags(), setting or clearing the kDistanceFieldTextTEMP_Flag bit
297 @param distanceFieldText true to set the kDistanceFieldTextTEMP_Flag bit in the paint's
298 flags, false to clear it.
299 */
300 void setDistanceFieldTextTEMP(bool distanceFieldText);
301
reedf803da12015-01-23 05:58:07 -0800302#ifdef SK_SUPPORT_LEGACY_FILTERLEVEL_ENUM
reed@google.comc9683152013-07-18 13:47:01 +0000303 enum FilterLevel {
reedf803da12015-01-23 05:58:07 -0800304 kNone_FilterLevel = kNone_SkFilterQuality,
305 kLow_FilterLevel = kLow_SkFilterQuality,
306 kMedium_FilterLevel = kMedium_SkFilterQuality,
307 kHigh_FilterLevel = kHigh_SkFilterQuality
reed@google.comc9683152013-07-18 13:47:01 +0000308 };
309
310 /**
311 * Return the filter level. This affects the quality (and performance) of
312 * drawing scaled images.
313 */
reedf59eab22014-07-14 14:39:15 -0700314 FilterLevel getFilterLevel() const {
reedf803da12015-01-23 05:58:07 -0800315 return (FilterLevel)this->getFilterQuality();
reedf59eab22014-07-14 14:39:15 -0700316 }
reed@google.comc9683152013-07-18 13:47:01 +0000317
318 /**
319 * Set the filter level. This affects the quality (and performance) of
320 * drawing scaled images.
321 */
reedf803da12015-01-23 05:58:07 -0800322 void setFilterLevel(FilterLevel level) {
323 this->setFilterQuality((SkFilterQuality)level);
324 }
325#endif
326
327 /**
328 * Return the filter level. This affects the quality (and performance) of
329 * drawing scaled images.
330 */
331 SkFilterQuality getFilterQuality() const {
332 return (SkFilterQuality)fBitfields.fFilterQuality;
333 }
334
335 /**
336 * Set the filter quality. This affects the quality (and performance) of
337 * drawing scaled images.
338 */
339 void setFilterQuality(SkFilterQuality quality);
reed@google.comc9683152013-07-18 13:47:01 +0000340
341 /**
reed@google.comc9683152013-07-18 13:47:01 +0000342 * If the predicate is true, set the filterLevel to Low, else set it to
343 * None.
344 */
reed@google.com44699382013-10-31 17:28:30 +0000345 SK_ATTR_DEPRECATED("use setFilterLevel")
reed@google.comc9683152013-07-18 13:47:01 +0000346 void setFilterBitmap(bool doFilter) {
347 this->setFilterLevel(doFilter ? kLow_FilterLevel : kNone_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000349
reed@google.comc9683152013-07-18 13:47:01 +0000350 /**
reed@google.comc9683152013-07-18 13:47:01 +0000351 * Returns true if getFilterLevel() returns anything other than None.
352 */
reed@google.com44699382013-10-31 17:28:30 +0000353 SK_ATTR_DEPRECATED("use getFilterLevel")
reed@google.comc9683152013-07-18 13:47:01 +0000354 bool isFilterBitmap() const {
355 return kNone_FilterLevel != this->getFilterLevel();
356 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357
358 /** Styles apply to rect, oval, path, and text.
359 Bitmaps are always drawn in "fill", and lines are always drawn in
360 "stroke".
reed@google.com9d07fec2011-03-16 20:02:59 +0000361
reed@android.comed881c22009-09-15 14:10:42 +0000362 Note: strokeandfill implicitly draws the result with
363 SkPath::kWinding_FillType, so if the original path is even-odd, the
364 results may not appear the same as if it was drawn twice, filled and
365 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 */
367 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000368 kFill_Style, //!< fill the geometry
369 kStroke_Style, //!< stroke the geometry
370 kStrokeAndFill_Style, //!< fill and stroke the geometry
mike@reedtribe.orgaac2fb82013-02-04 05:17:10 +0000371 };
372 enum {
373 kStyleCount = kStrokeAndFill_Style + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374 };
375
376 /** Return the paint's style, used for controlling how primitives'
377 geometries are interpreted (except for drawBitmap, which always assumes
378 kFill_Style).
379 @return the paint's Style
380 */
reedf59eab22014-07-14 14:39:15 -0700381 Style getStyle() const { return (Style)fBitfields.fStyle; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382
383 /** Set the paint's style, used for controlling how primitives'
384 geometries are interpreted (except for drawBitmap, which always assumes
385 Fill).
386 @param style The new style to set in the paint
387 */
388 void setStyle(Style style);
389
390 /** Return the paint's color. Note that the color is a 32bit value
391 containing alpha as well as r,g,b. This 32bit value is not
392 premultiplied, meaning that its alpha can be any value, regardless of
393 the values of r,g,b.
394 @return the paint's color (and alpha).
395 */
396 SkColor getColor() const { return fColor; }
397
398 /** Set the paint's color. Note that the color is a 32bit value containing
399 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
400 that its alpha can be any value, regardless of the values of r,g,b.
401 @param color The new color (including alpha) to set in the paint.
402 */
403 void setColor(SkColor color);
404
405 /** Helper to getColor() that just returns the color's alpha value.
406 @return the alpha component of the paint's color.
407 */
408 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
reed@google.com9d07fec2011-03-16 20:02:59 +0000409
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 /** Helper to setColor(), that only assigns the color's alpha value,
411 leaving its r,g,b values unchanged.
412 @param a set the alpha component (0..255) of the paint's color.
413 */
414 void setAlpha(U8CPU a);
415
416 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
417 using SkColorSetARGB()
418 @param a The new alpha component (0..255) of the paint's color.
419 @param r The new red component (0..255) of the paint's color.
420 @param g The new green component (0..255) of the paint's color.
421 @param b The new blue component (0..255) of the paint's color.
422 */
423 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
424
reed@google.com9d07fec2011-03-16 20:02:59 +0000425 /** Return the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 <p />
427 A value of 0 strokes in hairline mode.
428 Hairlines always draw 1-pixel wide, regardless of the matrix.
429 @return the paint's stroke width, used whenever the paint's style is
430 Stroke or StrokeAndFill.
431 */
432 SkScalar getStrokeWidth() const { return fWidth; }
433
reed@google.com9d07fec2011-03-16 20:02:59 +0000434 /** Set the width for stroking.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 Pass 0 to stroke in hairline mode.
436 Hairlines always draw 1-pixel wide, regardless of the matrix.
437 @param width set the paint's stroke width, used whenever the paint's
438 style is Stroke or StrokeAndFill.
439 */
440 void setStrokeWidth(SkScalar width);
441
442 /** Return the paint's stroke miter value. This is used to control the
443 behavior of miter joins when the joins angle is sharp.
444 @return the paint's miter limit, used whenever the paint's style is
445 Stroke or StrokeAndFill.
446 */
447 SkScalar getStrokeMiter() const { return fMiterLimit; }
448
449 /** Set the paint's stroke miter value. This is used to control the
450 behavior of miter joins when the joins angle is sharp. This value must
451 be >= 0.
452 @param miter set the miter limit on the paint, used whenever the
453 paint's style is Stroke or StrokeAndFill.
454 */
455 void setStrokeMiter(SkScalar miter);
456
457 /** Cap enum specifies the settings for the paint's strokecap. This is the
458 treatment that is applied to the beginning and end of each non-closed
459 contour (e.g. lines).
460 */
461 enum Cap {
462 kButt_Cap, //!< begin/end contours with no extension
463 kRound_Cap, //!< begin/end contours with a semi-circle extension
464 kSquare_Cap, //!< begin/end contours with a half square extension
465
466 kCapCount,
467 kDefault_Cap = kButt_Cap
468 };
469
470 /** Join enum specifies the settings for the paint's strokejoin. This is
471 the treatment that is applied to corners in paths and rectangles.
472 */
473 enum Join {
474 kMiter_Join, //!< connect path segments with a sharp join
475 kRound_Join, //!< connect path segments with a round join
476 kBevel_Join, //!< connect path segments with a flat bevel join
477
478 kJoinCount,
479 kDefault_Join = kMiter_Join
480 };
481
482 /** Return the paint's stroke cap type, controlling how the start and end
483 of stroked lines and paths are treated.
484 @return the line cap style for the paint, used whenever the paint's
485 style is Stroke or StrokeAndFill.
486 */
reedf59eab22014-07-14 14:39:15 -0700487 Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000488
489 /** Set the paint's stroke cap type.
490 @param cap set the paint's line cap style, used whenever the paint's
491 style is Stroke or StrokeAndFill.
492 */
493 void setStrokeCap(Cap cap);
494
495 /** Return the paint's stroke join type.
496 @return the paint's line join style, used whenever the paint's style is
497 Stroke or StrokeAndFill.
498 */
reedf59eab22014-07-14 14:39:15 -0700499 Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000500
501 /** Set the paint's stroke join type.
502 @param join set the paint's line join style, used whenever the paint's
503 style is Stroke or StrokeAndFill.
504 */
505 void setStrokeJoin(Join join);
506
reed@google.com4bbdeac2013-01-24 21:03:11 +0000507 /**
508 * Applies any/all effects (patheffect, stroking) to src, returning the
509 * result in dst. The result is that drawing src with this paint will be
510 * the same as drawing dst with a default paint (at least from the
511 * geometric perspective).
512 *
513 * @param src input path
514 * @param dst output path (may be the same as src)
515 * @param cullRect If not null, the dst path may be culled to this rect.
reed05d90442015-02-12 13:35:52 -0800516 * @param resScale If > 1, increase precision, else if (0 < res < 1) reduce precision
517 * in favor of speed/size.
reed@google.com4bbdeac2013-01-24 21:03:11 +0000518 * @return true if the path should be filled, or false if it should be
519 * drawn with a hairline (width == 0)
520 */
reed05d90442015-02-12 13:35:52 -0800521 bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
522 SkScalar resScale = 1) const;
523
524 bool getFillPath(const SkPath& src, SkPath* dst) const {
525 return this->getFillPath(src, dst, NULL, 1);
526 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 /** Get the paint's shader object.
529 <p />
530 The shader's reference count is not affected.
531 @return the paint's shader (or NULL)
532 */
533 SkShader* getShader() const { return fShader; }
534
535 /** Set or clear the shader object.
reed@google.com880dc472012-05-11 14:47:03 +0000536 * Shaders specify the source color(s) for what is being drawn. If a paint
537 * has no shader, then the paint's color is used. If the paint has a
538 * shader, then the shader's color(s) are use instead, but they are
539 * modulated by the paint's alpha. This makes it easy to create a shader
540 * once (e.g. bitmap tiling or gradient) and then change its transparency
541 * w/o having to modify the original shader... only the paint's alpha needs
542 * to be modified.
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +0000543 *
544 * There is an exception to this only-respect-paint's-alpha rule: If the shader only generates
545 * alpha (e.g. SkShader::CreateBitmapShader(bitmap, ...) where bitmap's colortype is kAlpha_8)
546 * then the shader will use the paint's entire color to "colorize" its output (modulating the
547 * bitmap's alpha with the paint's color+alpha).
548 *
reed@google.com880dc472012-05-11 14:47:03 +0000549 * Pass NULL to clear any previous shader.
550 * As a convenience, the parameter passed is also returned.
551 * If a previous shader exists, its reference count is decremented.
552 * If shader is not NULL, its reference count is incremented.
553 * @param shader May be NULL. The shader to be installed in the paint
554 * @return shader
555 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000556 SkShader* setShader(SkShader* shader);
reed@google.com9d07fec2011-03-16 20:02:59 +0000557
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 /** Get the paint's colorfilter. If there is a colorfilter, its reference
559 count is not changed.
560 @return the paint's colorfilter (or NULL)
561 */
562 SkColorFilter* getColorFilter() const { return fColorFilter; }
563
564 /** Set or clear the paint's colorfilter, returning the parameter.
565 <p />
566 If the paint already has a filter, its reference count is decremented.
567 If filter is not NULL, its reference count is incremented.
568 @param filter May be NULL. The filter to be installed in the paint
569 @return filter
570 */
571 SkColorFilter* setColorFilter(SkColorFilter* filter);
572
573 /** Get the paint's xfermode object.
574 <p />
575 The xfermode's reference count is not affected.
576 @return the paint's xfermode (or NULL)
577 */
578 SkXfermode* getXfermode() const { return fXfermode; }
579
580 /** Set or clear the xfermode object.
581 <p />
582 Pass NULL to clear any previous xfermode.
583 As a convenience, the parameter passed is also returned.
584 If a previous xfermode exists, its reference count is decremented.
585 If xfermode is not NULL, its reference count is incremented.
586 @param xfermode May be NULL. The new xfermode to be installed in the
587 paint
588 @return xfermode
589 */
590 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000591
592 /** Create an xfermode based on the specified Mode, and assign it into the
593 paint, returning the mode that was set. If the Mode is SrcOver, then
594 the paint's xfermode is set to null.
595 */
reed@android.com0baf1932009-06-24 12:41:42 +0000596 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000597
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598 /** Get the paint's patheffect object.
599 <p />
600 The patheffect reference count is not affected.
601 @return the paint's patheffect (or NULL)
602 */
603 SkPathEffect* getPathEffect() const { return fPathEffect; }
604
605 /** Set or clear the patheffect object.
606 <p />
607 Pass NULL to clear any previous patheffect.
608 As a convenience, the parameter passed is also returned.
609 If a previous patheffect exists, its reference count is decremented.
610 If patheffect is not NULL, its reference count is incremented.
611 @param effect May be NULL. The new patheffect to be installed in the
612 paint
613 @return effect
614 */
615 SkPathEffect* setPathEffect(SkPathEffect* effect);
616
617 /** Get the paint's maskfilter object.
618 <p />
619 The maskfilter reference count is not affected.
620 @return the paint's maskfilter (or NULL)
621 */
622 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
623
624 /** Set or clear the maskfilter object.
625 <p />
626 Pass NULL to clear any previous maskfilter.
627 As a convenience, the parameter passed is also returned.
628 If a previous maskfilter exists, its reference count is decremented.
629 If maskfilter is not NULL, its reference count is incremented.
630 @param maskfilter May be NULL. The new maskfilter to be installed in
631 the paint
632 @return maskfilter
633 */
634 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
635
636 // These attributes are for text/fonts
637
638 /** Get the paint's typeface object.
639 <p />
640 The typeface object identifies which font to use when drawing or
641 measuring text. The typeface reference count is not affected.
642 @return the paint's typeface (or NULL)
643 */
644 SkTypeface* getTypeface() const { return fTypeface; }
645
646 /** Set or clear the typeface object.
647 <p />
648 Pass NULL to clear any previous typeface.
649 As a convenience, the parameter passed is also returned.
650 If a previous typeface exists, its reference count is decremented.
651 If typeface is not NULL, its reference count is incremented.
652 @param typeface May be NULL. The new typeface to be installed in the
653 paint
654 @return typeface
655 */
656 SkTypeface* setTypeface(SkTypeface* typeface);
657
658 /** Get the paint's rasterizer (or NULL).
659 <p />
660 The raster controls how paths/text are turned into alpha masks.
661 @return the paint's rasterizer (or NULL)
662 */
663 SkRasterizer* getRasterizer() const { return fRasterizer; }
664
665 /** Set or clear the rasterizer object.
666 <p />
667 Pass NULL to clear any previous rasterizer.
668 As a convenience, the parameter passed is also returned.
669 If a previous rasterizer exists in the paint, its reference count is
670 decremented. If rasterizer is not NULL, its reference count is
671 incremented.
672 @param rasterizer May be NULL. The new rasterizer to be installed in
673 the paint.
674 @return rasterizer
675 */
676 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
677
reed@google.com15356a62011-11-03 19:29:08 +0000678 SkImageFilter* getImageFilter() const { return fImageFilter; }
679 SkImageFilter* setImageFilter(SkImageFilter*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000680
reed@google.comb0a34d82012-07-11 19:57:55 +0000681 SkAnnotation* getAnnotation() const { return fAnnotation; }
682 SkAnnotation* setAnnotation(SkAnnotation*);
683
684 /**
685 * Returns true if there is an annotation installed on this paint, and
686 * the annotation specifics no-drawing.
687 */
reed@google.com44699382013-10-31 17:28:30 +0000688 SK_ATTR_DEPRECATED("use getAnnotation and check for non-null")
commit-bot@chromium.org950923b2013-10-29 20:44:39 +0000689 bool isNoDrawAnnotation() const { return this->getAnnotation() != NULL; }
reed@google.com15356a62011-11-03 19:29:08 +0000690
reed@google.com9d07fec2011-03-16 20:02:59 +0000691 /**
692 * Return the paint's SkDrawLooper (if any). Does not affect the looper's
693 * reference count.
694 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000695 SkDrawLooper* getLooper() const { return fLooper; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000696
697 /**
698 * Set or clear the looper object.
699 * <p />
700 * Pass NULL to clear any previous looper.
701 * As a convenience, the parameter passed is also returned.
702 * If a previous looper exists in the paint, its reference count is
703 * decremented. If looper is not NULL, its reference count is
704 * incremented.
705 * @param looper May be NULL. The new looper to be installed in the paint.
706 * @return looper
707 */
708 SkDrawLooper* setLooper(SkDrawLooper* looper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000709
710 enum Align {
711 kLeft_Align,
712 kCenter_Align,
713 kRight_Align,
mike@reedtribe.orgddc813b2013-06-08 12:58:19 +0000714 };
715 enum {
716 kAlignCount = 3
reed@android.com8a1c16f2008-12-17 15:59:43 +0000717 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000718
reed@android.com8a1c16f2008-12-17 15:59:43 +0000719 /** Return the paint's Align value for drawing text.
720 @return the paint's Align value for drawing text.
721 */
reedf59eab22014-07-14 14:39:15 -0700722 Align getTextAlign() const { return (Align)fBitfields.fTextAlign; }
reed@google.com9d07fec2011-03-16 20:02:59 +0000723
reed@android.com8a1c16f2008-12-17 15:59:43 +0000724 /** Set the paint's text alignment.
725 @param align set the paint's Align value for drawing text.
726 */
727 void setTextAlign(Align align);
728
729 /** Return the paint's text size.
730 @return the paint's text size.
731 */
732 SkScalar getTextSize() const { return fTextSize; }
733
734 /** Set the paint's text size. This value must be > 0
735 @param textSize set the paint's text size.
736 */
737 void setTextSize(SkScalar textSize);
738
739 /** Return the paint's horizontal scale factor for text. The default value
740 is 1.0.
741 @return the paint's scale factor in X for drawing/measuring text
742 */
743 SkScalar getTextScaleX() const { return fTextScaleX; }
744
745 /** Set the paint's horizontal scale factor for text. The default value
746 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
747 stretch the text narrower.
748 @param scaleX set the paint's scale factor in X for drawing/measuring
749 text.
750 */
751 void setTextScaleX(SkScalar scaleX);
752
753 /** Return the paint's horizontal skew factor for text. The default value
754 is 0.
755 @return the paint's skew factor in X for drawing text.
756 */
757 SkScalar getTextSkewX() const { return fTextSkewX; }
758
759 /** Set the paint's horizontal skew factor for text. The default value
760 is 0. For approximating oblique text, use values around -0.25.
761 @param skewX set the paint's skew factor in X for drawing text.
762 */
763 void setTextSkewX(SkScalar skewX);
764
765 /** Describes how to interpret the text parameters that are passed to paint
766 methods like measureText() and getTextWidths().
767 */
768 enum TextEncoding {
769 kUTF8_TextEncoding, //!< the text parameters are UTF8
770 kUTF16_TextEncoding, //!< the text parameters are UTF16
robertphillips@google.com69705572012-03-21 19:46:50 +0000771 kUTF32_TextEncoding, //!< the text parameters are UTF32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000772 kGlyphID_TextEncoding //!< the text parameters are glyph indices
773 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000774
reedf59eab22014-07-14 14:39:15 -0700775 TextEncoding getTextEncoding() const {
776 return (TextEncoding)fBitfields.fTextEncoding;
777 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000778
779 void setTextEncoding(TextEncoding encoding);
780
781 struct FontMetrics {
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000782 /** Flags which indicate the confidence level of various metrics.
783 A set flag indicates that the metric may be trusted.
784 */
785 enum FontMetricsFlags {
786 kUnderlineThinknessIsValid_Flag = 1 << 0,
787 kUnderlinePositionIsValid_Flag = 1 << 1,
788 };
789
790 uint32_t fFlags; //!< Bit field to identify which values are unknown
reed@android.com8a1c16f2008-12-17 15:59:43 +0000791 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
792 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
793 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
794 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
795 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000796 SkScalar fAvgCharWidth; //!< the average character width (>= 0)
797 SkScalar fMaxCharWidth; //!< the max character width (>= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000798 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
799 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000800 SkScalar fXHeight; //!< The height of an 'x' in px, or 0 if no 'x' in face
801 SkScalar fCapHeight; //!< The cap height (> 0), or 0 if cannot be determined.
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +0000802 SkScalar fUnderlineThickness; //!< underline thickness, or 0 if cannot be determined
803
804 /** Underline Position - position of the top of the Underline stroke
805 relative to the baseline, this can have following values
806 - Negative - means underline should be drawn above baseline.
807 - Positive - means below baseline.
808 - Zero - mean underline should be drawn on baseline.
809 */
810 SkScalar fUnderlinePosition; //!< underline position, or 0 if cannot be determined
811
812 /** If the fontmetrics has a valid underlinethickness, return true, and set the
813 thickness param to that value. If it doesn't return false and ignore the
814 thickness param.
815 */
816 bool hasUnderlineThickness(SkScalar* thickness) const {
817 if (SkToBool(fFlags & kUnderlineThinknessIsValid_Flag)) {
818 *thickness = fUnderlineThickness;
819 return true;
820 }
821 return false;
822 }
823
824 /** If the fontmetrics has a valid underlineposition, return true, and set the
825 thickness param to that value. If it doesn't return false and ignore the
826 thickness param.
827 */
828 bool hasUnderlinePosition(SkScalar* position) const {
829 if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
830 *position = fUnderlinePosition;
831 return true;
832 }
833 return false;
834 }
835
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836 };
reed@google.com9d07fec2011-03-16 20:02:59 +0000837
reed@android.com8a1c16f2008-12-17 15:59:43 +0000838 /** Return the recommend spacing between lines (which will be
839 fDescent - fAscent + fLeading).
840 If metrics is not null, return in it the font metrics for the
reed@google.com9d07fec2011-03-16 20:02:59 +0000841 typeface/pointsize/etc. currently set in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000842 @param metrics If not null, returns the font metrics for the
843 current typeface/pointsize/etc setting in this
844 paint.
845 @param scale If not 0, return width as if the canvas were scaled
846 by this value
847 @param return the recommended spacing between lines
848 */
849 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
reed@google.com9d07fec2011-03-16 20:02:59 +0000850
reed@android.com8a1c16f2008-12-17 15:59:43 +0000851 /** Return the recommend line spacing. This will be
852 fDescent - fAscent + fLeading
853 */
854 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
855
856 /** Convert the specified text into glyph IDs, returning the number of
857 glyphs ID written. If glyphs is NULL, it is ignore and only the count
858 is returned.
859 */
860 int textToGlyphs(const void* text, size_t byteLength,
861 uint16_t glyphs[]) const;
862
reed@android.coma5dcaf62010-02-05 17:12:32 +0000863 /** Return true if all of the specified text has a corresponding non-zero
864 glyph ID. If any of the code-points in the text are not supported in
865 the typeface (i.e. the glyph ID would be zero), then return false.
866
867 If the text encoding for the paint is kGlyph_TextEncoding, then this
868 returns true if all of the specified glyph IDs are non-zero.
869 */
870 bool containsText(const void* text, size_t byteLength) const;
871
reed@android.com9d3a9852010-01-08 14:07:42 +0000872 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
873 to zero. Note: this does not look at the text-encoding setting in the
874 paint, only at the typeface.
875 */
876 void glyphsToUnichars(const uint16_t glyphs[], int count,
877 SkUnichar text[]) const;
878
reed@android.com8a1c16f2008-12-17 15:59:43 +0000879 /** Return the number of drawable units in the specified text buffer.
880 This looks at the current TextEncoding field of the paint. If you also
881 want to have the text converted into glyph IDs, call textToGlyphs
882 instead.
883 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000884 int countText(const void* text, size_t byteLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000885 return this->textToGlyphs(text, byteLength, NULL);
886 }
887
reed@google.com44da42e2011-11-10 20:04:47 +0000888 /** Return the width of the text. This will return the vertical measure
889 * if isVerticalText() is true, in which case the returned value should
890 * be treated has a height instead of a width.
891 *
892 * @param text The text to be measured
893 * @param length Number of bytes of text to measure
894 * @param bounds If not NULL, returns the bounds of the text,
895 * relative to (0, 0).
reed@google.com44da42e2011-11-10 20:04:47 +0000896 * @return The advance width of the text
897 */
reed99ae8812014-08-26 11:30:01 -0700898 SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899
reed@google.com44da42e2011-11-10 20:04:47 +0000900 /** Return the width of the text. This will return the vertical measure
901 * if isVerticalText() is true, in which case the returned value should
902 * be treated has a height instead of a width.
903 *
904 * @param text Address of the text
905 * @param length Number of bytes of text to measure
reed@google.com97ecd1d2012-05-15 19:25:17 +0000906 * @return The advance width of the text
reed@google.com44da42e2011-11-10 20:04:47 +0000907 */
reed@google.com9d07fec2011-03-16 20:02:59 +0000908 SkScalar measureText(const void* text, size_t length) const {
reed99ae8812014-08-26 11:30:01 -0700909 return this->measureText(text, length, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000910 }
reed@google.com9d07fec2011-03-16 20:02:59 +0000911
reed@google.com44da42e2011-11-10 20:04:47 +0000912 /** Return the number of bytes of text that were measured. If
913 * isVerticalText() is true, then the vertical advances are used for
914 * the measurement.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000915 *
reed@google.com44da42e2011-11-10 20:04:47 +0000916 * @param text The text to be measured
917 * @param length Number of bytes of text to measure
918 * @param maxWidth Maximum width. Only the subset of text whose accumulated
919 * widths are <= maxWidth are measured.
920 * @param measuredWidth Optional. If non-null, this returns the actual
921 * width of the measured text.
reed@google.com44da42e2011-11-10 20:04:47 +0000922 * @return The number of bytes of text that were measured. Will be
923 * <= length.
924 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
reed9e96aa02014-10-03 12:44:37 -0700926 SkScalar* measuredWidth = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000927
reed@google.com44da42e2011-11-10 20:04:47 +0000928 /** Return the advances for the text. These will be vertical advances if
929 * isVerticalText() returns true.
930 *
931 * @param text the text
932 * @param byteLength number of bytes to of text
933 * @param widths If not null, returns the array of advances for
934 * the glyphs. If not NULL, must be at least a large
935 * as the number of unichars in the specified text.
936 * @param bounds If not null, returns the bounds for each of
937 * character, relative to (0, 0)
938 * @return the number of unichars in the specified text.
939 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
941 SkRect bounds[] = NULL) const;
942
943 /** Return the path (outline) for the specified text.
944 Note: just like SkCanvas::drawText, this will respect the Align setting
945 in the paint.
946 */
947 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
948 SkPath* path) const;
949
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000950 void getPosTextPath(const void* text, size_t length,
reed@google.comca0062e2012-07-20 11:20:32 +0000951 const SkPoint pos[], SkPath* path) const;
952
reed8893e5f2014-12-15 13:27:26 -0800953 /**
954 * Return a rectangle that represents the union of the bounds of all
955 * of the glyphs, but each one positioned at (0,0). This may be conservatively large, and
956 * will not take into account any hinting, but will respect any text-scale-x or text-skew-x
957 * on this paint.
958 */
959 SkRect getFontBounds() const;
960
reed@google.com632e1a22011-10-06 12:37:00 +0000961 // returns true if the paint's settings (e.g. xfermode + alpha) resolve to
962 // mean that we need not draw at all (e.g. SrcOver + 0-alpha)
963 bool nothingToDraw() const;
964
reed@google.coma584aed2012-05-16 14:06:02 +0000965 ///////////////////////////////////////////////////////////////////////////
reed@google.comd5f20792012-05-16 14:15:02 +0000966 // would prefer to make these private...
967
reed@google.coma584aed2012-05-16 14:06:02 +0000968 /** Returns true if the current paint settings allow for fast computation of
969 bounds (i.e. there is nothing complex like a patheffect that would make
970 the bounds computation expensive.
971 */
972 bool canComputeFastBounds() const {
973 if (this->getLooper()) {
974 return this->getLooper()->canComputeFastBounds(*this);
975 }
976 return !this->getRasterizer();
977 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000978
reed@google.coma584aed2012-05-16 14:06:02 +0000979 /** Only call this if canComputeFastBounds() returned true. This takes a
980 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
981 effects in the paint (e.g. stroking). If needed, it uses the storage
982 rect parameter. It returns the adjusted bounds that can then be used
983 for quickReject tests.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000984
reed@google.coma584aed2012-05-16 14:06:02 +0000985 The returned rect will either be orig or storage, thus the caller
986 should not rely on storage being set to the result, but should always
987 use the retured value. It is legal for orig and storage to be the same
988 rect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000989
reed@google.coma584aed2012-05-16 14:06:02 +0000990 e.g.
991 if (paint.canComputeFastBounds()) {
992 SkRect r, storage;
993 path.computeBounds(&r, SkPath::kFast_BoundsType);
994 const SkRect& fastR = paint.computeFastBounds(r, &storage);
995 if (canvas->quickReject(fastR, ...)) {
996 // don't draw the path
997 }
998 }
999 */
1000 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
1001 SkPaint::Style style = this->getStyle();
1002 // ultra fast-case: filling with no effects that affect geometry
1003 if (kFill_Style == style) {
1004 uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
1005 effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
1006 effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +00001007 effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
reed@google.coma584aed2012-05-16 14:06:02 +00001008 if (!effects) {
1009 return orig;
1010 }
1011 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001012
reed@google.coma584aed2012-05-16 14:06:02 +00001013 return this->doComputeFastBounds(orig, storage, style);
1014 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001015
reed@google.coma584aed2012-05-16 14:06:02 +00001016 const SkRect& computeFastStrokeBounds(const SkRect& orig,
1017 SkRect* storage) const {
reed@google.com73a02582012-05-16 19:21:12 +00001018 return this->doComputeFastBounds(orig, storage, kStroke_Style);
reed@google.coma584aed2012-05-16 14:06:02 +00001019 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001020
reed@google.coma584aed2012-05-16 14:06:02 +00001021 // Take the style explicitly, so the caller can force us to be stroked
1022 // without having to make a copy of the paint just to change that field.
1023 const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
1024 Style) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001025
reed@google.comed43dff2013-06-04 16:56:27 +00001026 /**
1027 * Return a matrix that applies the paint's text values: size, scale, skew
1028 */
1029 static SkMatrix* SetTextMatrix(SkMatrix* matrix, SkScalar size,
1030 SkScalar scaleX, SkScalar skewX) {
1031 matrix->setScale(size * scaleX, size);
1032 if (skewX) {
1033 matrix->postSkew(skewX, 0);
1034 }
1035 return matrix;
1036 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001037
reed@google.comed43dff2013-06-04 16:56:27 +00001038 SkMatrix* setTextMatrix(SkMatrix* matrix) const {
1039 return SetTextMatrix(matrix, fTextSize, fTextScaleX, fTextSkewX);
1040 }
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001041
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001042 SK_TO_STRING_NONVIRT()
robertphillips@google.com791f12e2013-02-14 13:53:53 +00001043
reed@google.comd5f20792012-05-16 14:15:02 +00001044private:
1045 SkTypeface* fTypeface;
reed@google.comd5f20792012-05-16 14:15:02 +00001046 SkPathEffect* fPathEffect;
1047 SkShader* fShader;
1048 SkXfermode* fXfermode;
1049 SkMaskFilter* fMaskFilter;
1050 SkColorFilter* fColorFilter;
1051 SkRasterizer* fRasterizer;
1052 SkDrawLooper* fLooper;
1053 SkImageFilter* fImageFilter;
reed@google.comb0a34d82012-07-11 19:57:55 +00001054 SkAnnotation* fAnnotation;
reed@google.comd5f20792012-05-16 14:15:02 +00001055
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001056 SkScalar fTextSize;
1057 SkScalar fTextScaleX;
1058 SkScalar fTextSkewX;
reed@google.comd5f20792012-05-16 14:15:02 +00001059 SkColor fColor;
1060 SkScalar fWidth;
1061 SkScalar fMiterLimit;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001062 union {
1063 struct {
1064 // all of these bitfields should add up to 32
1065 unsigned fFlags : 16;
1066 unsigned fTextAlign : 2;
1067 unsigned fCapType : 2;
1068 unsigned fJoinType : 2;
1069 unsigned fStyle : 2;
1070 unsigned fTextEncoding : 2; // 3 values
1071 unsigned fHinting : 2;
reedf803da12015-01-23 05:58:07 -08001072 unsigned fFilterQuality : 2;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001073 //unsigned fFreeBits : 2;
reedf59eab22014-07-14 14:39:15 -07001074 } fBitfields;
1075 uint32_t fBitfieldsUInt;
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +00001076 };
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +00001077
reed@google.comd5f20792012-05-16 14:15:02 +00001078 SkDrawCacheProc getDrawCacheProc() const;
reed9e96aa02014-10-03 12:44:37 -07001079 SkMeasureCacheProc getMeasureCacheProc(bool needFullMetrics) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001080
1081 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
1082 int* count, SkRect* bounds) const;
1083
joshualittfd450792015-03-13 08:38:43 -07001084 /*
1085 * Allocs an SkDescriptor on the heap and return it to the caller as a refcnted
1086 * SkData. Caller is responsible for managing the lifetime of this object.
1087 */
1088 const SkData* getScalerContextDescriptor(const SkDeviceProperties* deviceProperties,
1089 const SkMatrix*, bool ignoreGamma) const;
1090
jvanverth2d2a68c2014-06-10 06:42:56 -07001091 SkGlyphCache* detachCache(const SkDeviceProperties* deviceProperties, const SkMatrix*,
1092 bool ignoreGamma) const;
reed@google.comd5f20792012-05-16 14:15:02 +00001093
bungeman@google.com532470f2013-01-22 19:25:14 +00001094 void descriptorProc(const SkDeviceProperties* deviceProperties, const SkMatrix* deviceMatrix,
reed@google.com90808e82013-03-19 14:44:54 +00001095 void (*proc)(SkTypeface*, const SkDescriptor*, void*),
reed@google.comd5f20792012-05-16 14:15:02 +00001096 void* context, bool ignoreGamma = false) const;
reed@android.comd252db02009-04-01 18:31:44 +00001097
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001098 static void Term();
1099
reed@android.com8a1c16f2008-12-17 15:59:43 +00001100 enum {
reed@google.comed43dff2013-06-04 16:56:27 +00001101 /* This is the size we use when we ask for a glyph's path. We then
1102 * post-transform it as we draw to match the request.
1103 * This is done to try to re-use cache entries for the path.
1104 *
1105 * This value is somewhat arbitrary. In theory, it could be 1, since
1106 * we store paths as floats. However, we get the path from the font
1107 * scaler, and it may represent its paths as fixed-point (or 26.6),
1108 * so we shouldn't ask for something too big (might overflow 16.16)
1109 * or too small (underflow 26.6).
1110 *
1111 * This value could track kMaxSizeForGlyphCache, assuming the above
1112 * constraints, but since we ask for unhinted paths, the two values
1113 * need not match per-se.
1114 */
1115 kCanonicalTextSizeForPaths = 64,
1116
1117 /*
1118 * Above this size (taking into account CTM and textSize), we never use
1119 * the cache for bits or metrics (we might overflow), so we just ask
1120 * for a caononical size and post-transform that.
1121 */
1122 kMaxSizeForGlyphCache = 256,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 };
reed@google.comed43dff2013-06-04 16:56:27 +00001124
1125 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM);
1126
reed@google.comed43dff2013-06-04 16:56:27 +00001127 // Set flags/hinting/textSize up to use for drawing text as paths.
1128 // Returns scale factor to restore the original textSize, since will will
1129 // have change it to kCanonicalTextSizeForPaths.
1130 SkScalar setupForAsPaths();
1131
1132 static SkScalar MaxCacheSize2() {
1133 static const SkScalar kMaxSize = SkIntToScalar(kMaxSizeForGlyphCache);
1134 static const SkScalar kMag2Max = kMaxSize * kMaxSize;
1135 return kMag2Max;
1136 }
1137
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001138 friend class SkAutoGlyphCache;
jvanverth2d2a68c2014-06-10 06:42:56 -07001139 friend class SkAutoGlyphCacheNoGamma;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001140 friend class SkCanvas;
1141 friend class SkDraw;
bungeman@google.comb24b4fa2012-09-04 13:49:59 +00001142 friend class SkGraphics; // So Term() can be called.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001143 friend class SkPDFDevice;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00001144 friend class GrBitmapTextContext;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001145 friend class GrDistanceFieldTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001146 friend class GrStencilAndCoverTextContext;
cdalton855d83f2014-09-18 13:51:53 -07001147 friend class GrPathRendering;
1148 friend class GrGLPathRendering;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149 friend class SkTextToPathIter;
reed@google.comed43dff2013-06-04 16:56:27 +00001150 friend class SkCanonicalizePaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001151};
1152
reed@android.com8a1c16f2008-12-17 15:59:43 +00001153#endif