blob: 86f174a524f8b1c2239a6ca0870fc10689ae4d4a [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkPaint_DEFINED
18#define SkPaint_DEFINED
19
20#include "SkColor.h"
21#include "SkMath.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000022#include "SkXfermode.h"
23
reed@android.com8a1c16f2008-12-17 15:59:43 +000024class SkAutoGlyphCache;
25class SkColorFilter;
26class SkDescriptor;
27class SkFlattenableReadBuffer;
28class SkFlattenableWriteBuffer;
29struct SkGlyph;
30struct SkRect;
31class SkGlyphCache;
32class SkMaskFilter;
33class SkMatrix;
34class SkPath;
35class SkPathEffect;
36class SkRasterizer;
37class SkShader;
38class SkDrawLooper;
39class SkTypeface;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
41typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
42 SkFixed x, SkFixed y);
43
44typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
45
46/** \class SkPaint
47
48 The SkPaint class holds the style and color information about how to draw
49 geometries, text and bitmaps.
50*/
51class SkPaint {
52public:
53 SkPaint();
54 SkPaint(const SkPaint& paint);
55 ~SkPaint();
56
57 SkPaint& operator=(const SkPaint&);
58
59 friend int operator==(const SkPaint& a, const SkPaint& b);
60 friend int operator!=(const SkPaint& a, const SkPaint& b)
61 {
62 return !(a == b);
63 }
64
65 void flatten(SkFlattenableWriteBuffer&) const;
66 void unflatten(SkFlattenableReadBuffer&);
67
68 /** Restores the paint to its initial settings.
69 */
70 void reset();
71
agl@chromium.org309485b2009-07-21 17:41:32 +000072 /** Specifies the level of hinting to be performed. These names are taken
73 from the Gnome/Cairo names for the same. They are translated into
74 Freetype concepts the same as in cairo-ft-font.c:
75 kNo_Hinting -> FT_LOAD_NO_HINTING
76 kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
77 kNormal_Hinting -> <default, no option>
78 kFull_Hinting -> <same as kNormalHinting, unless we are rendering
79 subpixel glyphs, in which case TARGET_LCD or
80 TARGET_LCD_V is used>
81 */
82 enum Hinting {
83 kNo_Hinting = 0,
84 kSlight_Hinting = 1,
85 kNormal_Hinting = 2, //!< this is the default
86 kFull_Hinting = 3,
87 };
88
89 Hinting getHinting() const
90 {
91 return static_cast<Hinting>(fHinting);
92 }
93
94 void setHinting(Hinting hintingLevel)
95 {
96 fHinting = hintingLevel;
97 }
98
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 /** Specifies the bit values that are stored in the paint's flags.
100 */
101 enum Flags {
102 kAntiAlias_Flag = 0x01, //!< mask to enable antialiasing
103 kFilterBitmap_Flag = 0x02, //!< mask to enable bitmap filtering
104 kDither_Flag = 0x04, //!< mask to enable dithering
105 kUnderlineText_Flag = 0x08, //!< mask to enable underline text
106 kStrikeThruText_Flag = 0x10, //!< mask to enable strike-thru text
107 kFakeBoldText_Flag = 0x20, //!< mask to enable fake-bold text
108 kLinearText_Flag = 0x40, //!< mask to enable linear-text
agl@chromium.org309485b2009-07-21 17:41:32 +0000109 kSubpixelText_Flag = 0x80, //!< mask to enable subpixel text positioning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 kDevKernText_Flag = 0x100, //!< mask to enable device kerning text
agl@chromium.org309485b2009-07-21 17:41:32 +0000111 kLCDRenderText_Flag = 0x200, //!< mask to enable subpixel glyph renderering
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000112 kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
agl@chromium.org309485b2009-07-21 17:41:32 +0000113 // when adding extra flags, note that the fFlags member is specified
114 // with a bit-width and you'll have to expand it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000116 kAllFlags = 0x7FF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 };
118
119 /** Return the paint's flags. Use the Flag enum to test flag values.
120 @return the paint's flags (see enums ending in _Flag for bit masks)
121 */
122 uint32_t getFlags() const { return fFlags; }
123
124 /** Set the paint's flags. Use the Flag enum to specific flag values.
125 @param flags The new flag bits for the paint (see Flags enum)
126 */
127 void setFlags(uint32_t flags);
128
129 /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
130 @return true if the antialias bit is set in the paint's flags.
131 */
132 bool isAntiAlias() const
133 {
134 return SkToBool(this->getFlags() & kAntiAlias_Flag);
135 }
136
137 /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
138 @param aa true to enable antialiasing, false to disable it
139 */
140 void setAntiAlias(bool aa);
141
142 /** Helper for getFlags(), returning true if kDither_Flag bit is set
143 @return true if the dithering bit is set in the paint's flags.
144 */
145 bool isDither() const
146 {
147 return SkToBool(this->getFlags() & kDither_Flag);
148 }
149
150 /** Helper for setFlags(), setting or clearing the kDither_Flag bit
151 @param dither true to enable dithering, false to disable it
152 */
153 void setDither(bool dither);
154
155 /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
156 @return true if the lineartext bit is set in the paint's flags
157 */
158 bool isLinearText() const
159 {
160 return SkToBool(this->getFlags() & kLinearText_Flag);
161 }
162
163 /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
164 @param linearText true to set the linearText bit in the paint's flags,
165 false to clear it.
166 */
167 void setLinearText(bool linearText);
168
169 /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
170 @return true if the lineartext bit is set in the paint's flags
171 */
172 bool isSubpixelText() const
173 {
174 return SkToBool(this->getFlags() & kSubpixelText_Flag);
175 }
176
agl@chromium.org309485b2009-07-21 17:41:32 +0000177 /** Helper for setFlags(), setting or clearing the kSubpixelText_Flag
178 bit @param subpixelText true to set the subpixelText bit in the paint's flags,
179 false to clear it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 */
181 void setSubpixelText(bool subpixelText);
agl@chromium.org309485b2009-07-21 17:41:32 +0000182
183 bool isLCDRenderText() const
184 {
185 return SkToBool(this->getFlags() & kLCDRenderText_Flag);
186 }
187
188 /** Helper for setFlags(), setting or clearing the kLCDRenderText_Flag bit
189 @param subpixelRender true to set the subpixelRenderText bit in the paint's flags,
190 false to clear it.
191 */
192 void setLCDRenderText(bool subpixelRender);
193
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000194 bool isEmbeddedBitmapText() const
195 {
196 return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
197 }
198
199 /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
200 @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
201 false to clear it.
202 */
203 void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 /** Helper for getFlags(), returning true if kUnderlineText_Flag bit is set
206 @return true if the underlineText bit is set in the paint's flags.
207 */
208 bool isUnderlineText() const
209 {
210 return SkToBool(this->getFlags() & kUnderlineText_Flag);
211 }
212
213 /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
214 @param underlineText true to set the underlineText bit in the paint's
215 flags, false to clear it.
216 */
217 void setUnderlineText(bool underlineText);
218
219 /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
220 @return true if the strikeThruText bit is set in the paint's flags.
221 */
222 bool isStrikeThruText() const
223 {
224 return SkToBool(this->getFlags() & kStrikeThruText_Flag);
225 }
226
227 /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
228 @param strikeThruText true to set the strikeThruText bit in the
229 paint's flags, false to clear it.
230 */
231 void setStrikeThruText(bool strikeThruText);
232
233 /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
234 @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
235 */
236 bool isFakeBoldText() const
237 {
238 return SkToBool(this->getFlags() & kFakeBoldText_Flag);
239 }
240
241 /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
242 @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
243 flags, false to clear it.
244 */
245 void setFakeBoldText(bool fakeBoldText);
246
247 /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
248 @return true if the kernText bit is set in the paint's flags.
249 */
250 bool isDevKernText() const
251 {
252 return SkToBool(this->getFlags() & kDevKernText_Flag);
253 }
254
255 /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
256 @param kernText true to set the kKernText_Flag bit in the paint's
257 flags, false to clear it.
258 */
259 void setDevKernText(bool devKernText);
260
261 bool isFilterBitmap() const
262 {
263 return SkToBool(this->getFlags() & kFilterBitmap_Flag);
264 }
265
266 void setFilterBitmap(bool filterBitmap);
267
268 /** Styles apply to rect, oval, path, and text.
269 Bitmaps are always drawn in "fill", and lines are always drawn in
270 "stroke".
reed@android.comed881c22009-09-15 14:10:42 +0000271
272 Note: strokeandfill implicitly draws the result with
273 SkPath::kWinding_FillType, so if the original path is even-odd, the
274 results may not appear the same as if it was drawn twice, filled and
275 then stroked.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 */
277 enum Style {
reed@android.comed881c22009-09-15 14:10:42 +0000278 kFill_Style, //!< fill the geometry
279 kStroke_Style, //!< stroke the geometry
280 kStrokeAndFill_Style, //!< fill and stroke the geometry
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281
282 kStyleCount,
283 };
284
285 /** Return the paint's style, used for controlling how primitives'
286 geometries are interpreted (except for drawBitmap, which always assumes
287 kFill_Style).
288 @return the paint's Style
289 */
290 Style getStyle() const { return (Style)fStyle; }
291
292 /** Set the paint's style, used for controlling how primitives'
293 geometries are interpreted (except for drawBitmap, which always assumes
294 Fill).
295 @param style The new style to set in the paint
296 */
297 void setStyle(Style style);
298
299 /** Return the paint's color. Note that the color is a 32bit value
300 containing alpha as well as r,g,b. This 32bit value is not
301 premultiplied, meaning that its alpha can be any value, regardless of
302 the values of r,g,b.
303 @return the paint's color (and alpha).
304 */
305 SkColor getColor() const { return fColor; }
306
307 /** Set the paint's color. Note that the color is a 32bit value containing
308 alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
309 that its alpha can be any value, regardless of the values of r,g,b.
310 @param color The new color (including alpha) to set in the paint.
311 */
312 void setColor(SkColor color);
313
314 /** Helper to getColor() that just returns the color's alpha value.
315 @return the alpha component of the paint's color.
316 */
317 uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
318
319 /** Helper to setColor(), that only assigns the color's alpha value,
320 leaving its r,g,b values unchanged.
321 @param a set the alpha component (0..255) of the paint's color.
322 */
323 void setAlpha(U8CPU a);
324
325 /** Helper to setColor(), that takes a,r,g,b and constructs the color value
326 using SkColorSetARGB()
327 @param a The new alpha component (0..255) of the paint's color.
328 @param r The new red component (0..255) of the paint's color.
329 @param g The new green component (0..255) of the paint's color.
330 @param b The new blue component (0..255) of the paint's color.
331 */
332 void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
333
334 /** Return the width for stroking.
335 <p />
336 A value of 0 strokes in hairline mode.
337 Hairlines always draw 1-pixel wide, regardless of the matrix.
338 @return the paint's stroke width, used whenever the paint's style is
339 Stroke or StrokeAndFill.
340 */
341 SkScalar getStrokeWidth() const { return fWidth; }
342
343 /** Set the width for stroking.
344 Pass 0 to stroke in hairline mode.
345 Hairlines always draw 1-pixel wide, regardless of the matrix.
346 @param width set the paint's stroke width, used whenever the paint's
347 style is Stroke or StrokeAndFill.
348 */
349 void setStrokeWidth(SkScalar width);
350
351 /** Return the paint's stroke miter value. This is used to control the
352 behavior of miter joins when the joins angle is sharp.
353 @return the paint's miter limit, used whenever the paint's style is
354 Stroke or StrokeAndFill.
355 */
356 SkScalar getStrokeMiter() const { return fMiterLimit; }
357
358 /** Set the paint's stroke miter value. This is used to control the
359 behavior of miter joins when the joins angle is sharp. This value must
360 be >= 0.
361 @param miter set the miter limit on the paint, used whenever the
362 paint's style is Stroke or StrokeAndFill.
363 */
364 void setStrokeMiter(SkScalar miter);
365
366 /** Cap enum specifies the settings for the paint's strokecap. This is the
367 treatment that is applied to the beginning and end of each non-closed
368 contour (e.g. lines).
369 */
370 enum Cap {
371 kButt_Cap, //!< begin/end contours with no extension
372 kRound_Cap, //!< begin/end contours with a semi-circle extension
373 kSquare_Cap, //!< begin/end contours with a half square extension
374
375 kCapCount,
376 kDefault_Cap = kButt_Cap
377 };
378
379 /** Join enum specifies the settings for the paint's strokejoin. This is
380 the treatment that is applied to corners in paths and rectangles.
381 */
382 enum Join {
383 kMiter_Join, //!< connect path segments with a sharp join
384 kRound_Join, //!< connect path segments with a round join
385 kBevel_Join, //!< connect path segments with a flat bevel join
386
387 kJoinCount,
388 kDefault_Join = kMiter_Join
389 };
390
391 /** Return the paint's stroke cap type, controlling how the start and end
392 of stroked lines and paths are treated.
393 @return the line cap style for the paint, used whenever the paint's
394 style is Stroke or StrokeAndFill.
395 */
396 Cap getStrokeCap() const { return (Cap)fCapType; }
397
398 /** Set the paint's stroke cap type.
399 @param cap set the paint's line cap style, used whenever the paint's
400 style is Stroke or StrokeAndFill.
401 */
402 void setStrokeCap(Cap cap);
403
404 /** Return the paint's stroke join type.
405 @return the paint's line join style, used whenever the paint's style is
406 Stroke or StrokeAndFill.
407 */
408 Join getStrokeJoin() const { return (Join)fJoinType; }
409
410 /** Set the paint's stroke join type.
411 @param join set the paint's line join style, used whenever the paint's
412 style is Stroke or StrokeAndFill.
413 */
414 void setStrokeJoin(Join join);
415
416 /** Applies any/all effects (patheffect, stroking) to src, returning the
417 result in dst. The result is that drawing src with this paint will be
418 the same as drawing dst with a default paint (at least from the
419 geometric perspective).
420 @param src input path
421 @param dst output path (may be the same as src)
422 @return true if the path should be filled, or false if it should be
423 drawn with a hairline (width == 0)
424 */
425 bool getFillPath(const SkPath& src, SkPath* dst) const;
426
427 /** Returns true if the current paint settings allow for fast computation of
428 bounds (i.e. there is nothing complex like a patheffect that would make
429 the bounds computation expensive.
430 */
reed@android.comd252db02009-04-01 18:31:44 +0000431 bool canComputeFastBounds() const {
432 // use bit-or since no need for early exit
433 return (reinterpret_cast<uintptr_t>(this->getMaskFilter()) |
434 reinterpret_cast<uintptr_t>(this->getLooper()) |
435 reinterpret_cast<uintptr_t>(this->getRasterizer()) |
436 reinterpret_cast<uintptr_t>(this->getPathEffect())) == 0;
437 }
438
reed@android.com8a1c16f2008-12-17 15:59:43 +0000439 /** Only call this if canComputeFastBounds() returned true. This takes a
440 raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
441 effects in the paint (e.g. stroking). If needed, it uses the storage
442 rect parameter. It returns the adjusted bounds that can then be used
443 for quickReject tests.
444
445 The returned rect will either be orig or storage, thus the caller
446 should not rely on storage being set to the result, but should always
447 use the retured value. It is legal for orig and storage to be the same
448 rect.
449
450 e.g.
451 if (paint.canComputeFastBounds()) {
452 SkRect r, storage;
453 path.computeBounds(&r, SkPath::kFast_BoundsType);
454 const SkRect& fastR = paint.computeFastBounds(r, &storage);
455 if (canvas->quickReject(fastR, ...)) {
456 // don't draw the path
457 }
458 }
459 */
reed@android.comd252db02009-04-01 18:31:44 +0000460 const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
461 return this->getStyle() == kFill_Style ? orig :
462 this->computeStrokeFastBounds(orig, storage);
463 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464
465 /** Get the paint's shader object.
466 <p />
467 The shader's reference count is not affected.
468 @return the paint's shader (or NULL)
469 */
470 SkShader* getShader() const { return fShader; }
471
472 /** Set or clear the shader object.
473 <p />
474 Pass NULL to clear any previous shader.
475 As a convenience, the parameter passed is also returned.
476 If a previous shader exists, its reference count is decremented.
477 If shader is not NULL, its reference count is incremented.
478 @param shader May be NULL. The shader to be installed in the paint
479 @return shader
480 */
481 SkShader* setShader(SkShader* shader);
482
483 /** Get the paint's colorfilter. If there is a colorfilter, its reference
484 count is not changed.
485 @return the paint's colorfilter (or NULL)
486 */
487 SkColorFilter* getColorFilter() const { return fColorFilter; }
488
489 /** Set or clear the paint's colorfilter, returning the parameter.
490 <p />
491 If the paint already has a filter, its reference count is decremented.
492 If filter is not NULL, its reference count is incremented.
493 @param filter May be NULL. The filter to be installed in the paint
494 @return filter
495 */
496 SkColorFilter* setColorFilter(SkColorFilter* filter);
497
498 /** Get the paint's xfermode object.
499 <p />
500 The xfermode's reference count is not affected.
501 @return the paint's xfermode (or NULL)
502 */
503 SkXfermode* getXfermode() const { return fXfermode; }
504
505 /** Set or clear the xfermode object.
506 <p />
507 Pass NULL to clear any previous xfermode.
508 As a convenience, the parameter passed is also returned.
509 If a previous xfermode exists, its reference count is decremented.
510 If xfermode is not NULL, its reference count is incremented.
511 @param xfermode May be NULL. The new xfermode to be installed in the
512 paint
513 @return xfermode
514 */
515 SkXfermode* setXfermode(SkXfermode* xfermode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000516
517 /** Create an xfermode based on the specified Mode, and assign it into the
518 paint, returning the mode that was set. If the Mode is SrcOver, then
519 the paint's xfermode is set to null.
520 */
reed@android.com0baf1932009-06-24 12:41:42 +0000521 SkXfermode* setXfermodeMode(SkXfermode::Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000522
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 /** Get the paint's patheffect object.
524 <p />
525 The patheffect reference count is not affected.
526 @return the paint's patheffect (or NULL)
527 */
528 SkPathEffect* getPathEffect() const { return fPathEffect; }
529
530 /** Set or clear the patheffect object.
531 <p />
532 Pass NULL to clear any previous patheffect.
533 As a convenience, the parameter passed is also returned.
534 If a previous patheffect exists, its reference count is decremented.
535 If patheffect is not NULL, its reference count is incremented.
536 @param effect May be NULL. The new patheffect to be installed in the
537 paint
538 @return effect
539 */
540 SkPathEffect* setPathEffect(SkPathEffect* effect);
541
542 /** Get the paint's maskfilter object.
543 <p />
544 The maskfilter reference count is not affected.
545 @return the paint's maskfilter (or NULL)
546 */
547 SkMaskFilter* getMaskFilter() const { return fMaskFilter; }
548
549 /** Set or clear the maskfilter object.
550 <p />
551 Pass NULL to clear any previous maskfilter.
552 As a convenience, the parameter passed is also returned.
553 If a previous maskfilter exists, its reference count is decremented.
554 If maskfilter is not NULL, its reference count is incremented.
555 @param maskfilter May be NULL. The new maskfilter to be installed in
556 the paint
557 @return maskfilter
558 */
559 SkMaskFilter* setMaskFilter(SkMaskFilter* maskfilter);
560
561 // These attributes are for text/fonts
562
563 /** Get the paint's typeface object.
564 <p />
565 The typeface object identifies which font to use when drawing or
566 measuring text. The typeface reference count is not affected.
567 @return the paint's typeface (or NULL)
568 */
569 SkTypeface* getTypeface() const { return fTypeface; }
570
571 /** Set or clear the typeface object.
572 <p />
573 Pass NULL to clear any previous typeface.
574 As a convenience, the parameter passed is also returned.
575 If a previous typeface exists, its reference count is decremented.
576 If typeface is not NULL, its reference count is incremented.
577 @param typeface May be NULL. The new typeface to be installed in the
578 paint
579 @return typeface
580 */
581 SkTypeface* setTypeface(SkTypeface* typeface);
582
583 /** Get the paint's rasterizer (or NULL).
584 <p />
585 The raster controls how paths/text are turned into alpha masks.
586 @return the paint's rasterizer (or NULL)
587 */
588 SkRasterizer* getRasterizer() const { return fRasterizer; }
589
590 /** Set or clear the rasterizer object.
591 <p />
592 Pass NULL to clear any previous rasterizer.
593 As a convenience, the parameter passed is also returned.
594 If a previous rasterizer exists in the paint, its reference count is
595 decremented. If rasterizer is not NULL, its reference count is
596 incremented.
597 @param rasterizer May be NULL. The new rasterizer to be installed in
598 the paint.
599 @return rasterizer
600 */
601 SkRasterizer* setRasterizer(SkRasterizer* rasterizer);
602
603 SkDrawLooper* getLooper() const { return fLooper; }
604 SkDrawLooper* setLooper(SkDrawLooper*);
605
606 enum Align {
607 kLeft_Align,
608 kCenter_Align,
609 kRight_Align,
610
611 kAlignCount
612 };
613 /** Return the paint's Align value for drawing text.
614 @return the paint's Align value for drawing text.
615 */
616 Align getTextAlign() const { return (Align)fTextAlign; }
617 /** Set the paint's text alignment.
618 @param align set the paint's Align value for drawing text.
619 */
620 void setTextAlign(Align align);
621
622 /** Return the paint's text size.
623 @return the paint's text size.
624 */
625 SkScalar getTextSize() const { return fTextSize; }
626
627 /** Set the paint's text size. This value must be > 0
628 @param textSize set the paint's text size.
629 */
630 void setTextSize(SkScalar textSize);
631
632 /** Return the paint's horizontal scale factor for text. The default value
633 is 1.0.
634 @return the paint's scale factor in X for drawing/measuring text
635 */
636 SkScalar getTextScaleX() const { return fTextScaleX; }
637
638 /** Set the paint's horizontal scale factor for text. The default value
639 is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
640 stretch the text narrower.
641 @param scaleX set the paint's scale factor in X for drawing/measuring
642 text.
643 */
644 void setTextScaleX(SkScalar scaleX);
645
646 /** Return the paint's horizontal skew factor for text. The default value
647 is 0.
648 @return the paint's skew factor in X for drawing text.
649 */
650 SkScalar getTextSkewX() const { return fTextSkewX; }
651
652 /** Set the paint's horizontal skew factor for text. The default value
653 is 0. For approximating oblique text, use values around -0.25.
654 @param skewX set the paint's skew factor in X for drawing text.
655 */
656 void setTextSkewX(SkScalar skewX);
657
658 /** Describes how to interpret the text parameters that are passed to paint
659 methods like measureText() and getTextWidths().
660 */
661 enum TextEncoding {
662 kUTF8_TextEncoding, //!< the text parameters are UTF8
663 kUTF16_TextEncoding, //!< the text parameters are UTF16
664 kGlyphID_TextEncoding //!< the text parameters are glyph indices
665 };
666
667 TextEncoding getTextEncoding() const
668 {
669 return (TextEncoding)fTextEncoding;
670 }
671
672 void setTextEncoding(TextEncoding encoding);
673
674 struct FontMetrics {
675 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
676 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
677 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
678 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
679 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000680 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
681 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
682 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
683 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
reed@android.com8a1c16f2008-12-17 15:59:43 +0000684 };
685
686 /** Return the recommend spacing between lines (which will be
687 fDescent - fAscent + fLeading).
688 If metrics is not null, return in it the font metrics for the
689 typeface/pointsize/etc. currently set in the paint.
690 @param metrics If not null, returns the font metrics for the
691 current typeface/pointsize/etc setting in this
692 paint.
693 @param scale If not 0, return width as if the canvas were scaled
694 by this value
695 @param return the recommended spacing between lines
696 */
697 SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
698
699 /** Return the recommend line spacing. This will be
700 fDescent - fAscent + fLeading
701 */
702 SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
703
704 /** Convert the specified text into glyph IDs, returning the number of
705 glyphs ID written. If glyphs is NULL, it is ignore and only the count
706 is returned.
707 */
708 int textToGlyphs(const void* text, size_t byteLength,
709 uint16_t glyphs[]) const;
710
reed@android.com9d3a9852010-01-08 14:07:42 +0000711 /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
712 to zero. Note: this does not look at the text-encoding setting in the
713 paint, only at the typeface.
714 */
715 void glyphsToUnichars(const uint16_t glyphs[], int count,
716 SkUnichar text[]) const;
717
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718 /** Return the number of drawable units in the specified text buffer.
719 This looks at the current TextEncoding field of the paint. If you also
720 want to have the text converted into glyph IDs, call textToGlyphs
721 instead.
722 */
723 int countText(const void* text, size_t byteLength) const
724 {
725 return this->textToGlyphs(text, byteLength, NULL);
726 }
727
728 /** Return the width of the text.
729 @param text The text to be measured
730 @param length Number of bytes of text to measure
731 @param bounds If not NULL, returns the bounds of the text,
732 relative to (0, 0).
733 @param scale If not 0, return width as if the canvas were scaled
734 by this value
735 @return The advance width of the text
736 */
737 SkScalar measureText(const void* text, size_t length,
738 SkRect* bounds, SkScalar scale = 0) const;
739
740 /** Return the width of the text.
741 @param text Address of the text
742 @param length Number of bytes of text to measure
743 @return The width of the text
744 */
745 SkScalar measureText(const void* text, size_t length) const
746 {
747 return this->measureText(text, length, NULL, 0);
748 }
749
750 /** Specify the direction the text buffer should be processed in breakText()
751 */
752 enum TextBufferDirection {
753 /** When measuring text for breakText(), begin at the start of the text
754 buffer and proceed forward through the data. This is the default.
755 */
756 kForward_TextBufferDirection,
757 /** When measuring text for breakText(), begin at the end of the text
758 buffer and proceed backwards through the data.
759 */
760 kBackward_TextBufferDirection
761 };
762
763 /** Return the width of the text.
764 @param text The text to be measured
765 @param length Number of bytes of text to measure
766 @param maxWidth Maximum width. Only the subset of text whose accumulated
767 widths are <= maxWidth are measured.
768 @param measuredWidth Optional. If non-null, this returns the actual
769 width of the measured text.
770 @param tbd Optional. The direction the text buffer should be
771 traversed during measuring.
772 @return The number of bytes of text that were measured. Will be
773 <= length.
774 */
775 size_t breakText(const void* text, size_t length, SkScalar maxWidth,
776 SkScalar* measuredWidth = NULL,
777 TextBufferDirection tbd = kForward_TextBufferDirection)
778 const;
779
780 /** Return the advance widths for the characters in the string.
781 @param text the text
782 @param byteLength number of bytes to of text
783 @param widths If not null, returns the array of advance widths of
784 the glyphs. If not NULL, must be at least a large
785 as the number of unichars in the specified text.
786 @param bounds If not null, returns the bounds for each of
787 character, relative to (0, 0)
788 @return the number of unichars in the specified text.
789 */
790 int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
791 SkRect bounds[] = NULL) const;
792
793 /** Return the path (outline) for the specified text.
794 Note: just like SkCanvas::drawText, this will respect the Align setting
795 in the paint.
796 */
797 void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
798 SkPath* path) const;
799
800private:
801 SkTypeface* fTypeface;
802 SkScalar fTextSize;
803 SkScalar fTextScaleX;
804 SkScalar fTextSkewX;
805
806 SkPathEffect* fPathEffect;
807 SkShader* fShader;
808 SkXfermode* fXfermode;
809 SkMaskFilter* fMaskFilter;
810 SkColorFilter* fColorFilter;
811 SkRasterizer* fRasterizer;
812 SkDrawLooper* fLooper;
813
814 SkColor fColor;
815 SkScalar fWidth;
816 SkScalar fMiterLimit;
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000817 unsigned fFlags : 11;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 unsigned fTextAlign : 2;
819 unsigned fCapType : 2;
820 unsigned fJoinType : 2;
821 unsigned fStyle : 2;
822 unsigned fTextEncoding : 2; // 3 values
agl@chromium.org309485b2009-07-21 17:41:32 +0000823 unsigned fHinting : 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824
825 SkDrawCacheProc getDrawCacheProc() const;
826 SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
827 bool needFullMetrics) const;
828
829 SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
830 int* count, SkRect* bounds) const;
831
832 SkGlyphCache* detachCache(const SkMatrix*) const;
833
834 void descriptorProc(const SkMatrix* deviceMatrix,
835 void (*proc)(const SkDescriptor*, void*),
836 void* context) const;
reed@android.comd252db02009-04-01 18:31:44 +0000837
838 const SkRect& computeStrokeFastBounds(const SkRect& orig,
839 SkRect* storage) const;
840
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841 enum {
842 kCanonicalTextSizeForPaths = 64
843 };
844 friend class SkCanvas;
845 friend class SkDraw;
846 friend class SkAutoGlyphCache;
847 friend class SkTextToPathIter;
848};
849
850//////////////////////////////////////////////////////////////////////////
851
852#include "SkPathEffect.h"
853
854/** \class SkStrokePathEffect
855
856 SkStrokePathEffect simulates stroking inside a patheffect, allowing the
857 caller to have explicit control of when to stroke a path. Typically this is
858 used if the caller wants to stroke before another patheffect is applied
859 (using SkComposePathEffect or SkSumPathEffect).
860*/
861class SkStrokePathEffect : public SkPathEffect {
862public:
863 SkStrokePathEffect(const SkPaint&);
864 SkStrokePathEffect(SkScalar width, SkPaint::Style, SkPaint::Join,
865 SkPaint::Cap, SkScalar miterLimit = -1);
866
867 // overrides
868 // This method is not exported to java.
869 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width);
870
871 // overrides for SkFlattenable
872 // This method is not exported to java.
873 virtual void flatten(SkFlattenableWriteBuffer&);
874 // This method is not exported to java.
875 virtual Factory getFactory();
876
877private:
878 SkScalar fWidth, fMiter;
879 uint8_t fStyle, fJoin, fCap;
880
881 static SkFlattenable* CreateProc(SkFlattenableReadBuffer&);
882 SkStrokePathEffect(SkFlattenableReadBuffer&);
883
884 typedef SkPathEffect INHERITED;
885
886 // illegal
887 SkStrokePathEffect(const SkStrokePathEffect&);
888 SkStrokePathEffect& operator=(const SkStrokePathEffect&);
889};
890
891#endif
892