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