• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
22 #include "SkXfermode.h"
23 
24 class SkAutoGlyphCache;
25 class SkColorFilter;
26 class SkDescriptor;
27 class SkFlattenableReadBuffer;
28 class SkFlattenableWriteBuffer;
29 struct SkGlyph;
30 struct SkRect;
31 class SkGlyphCache;
32 class SkMaskFilter;
33 class SkMatrix;
34 class SkPath;
35 class SkPathEffect;
36 class SkRasterizer;
37 class SkShader;
38 class SkDrawLooper;
39 class SkTypeface;
40 
41 typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
42                                            SkFixed x, SkFixed y);
43 
44 typedef 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 */
51 class SkPaint {
52 public:
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 
72     /** 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 
getHinting()89     Hinting getHinting() const
90     {
91         return static_cast<Hinting>(fHinting);
92     }
93 
setHinting(Hinting hintingLevel)94     void setHinting(Hinting hintingLevel)
95     {
96         fHinting = hintingLevel;
97     }
98 
99     /** 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
109         kSubpixelText_Flag    = 0x80,   //!< mask to enable subpixel text positioning
110         kDevKernText_Flag     = 0x100,  //!< mask to enable device kerning text
111         kLCDRenderText_Flag   = 0x200,  //!< mask to enable subpixel glyph renderering
112         kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
113         // when adding extra flags, note that the fFlags member is specified
114         // with a bit-width and you'll have to expand it.
115 
116         kAllFlags = 0x7FF
117     };
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     */
getFlags()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         */
isAntiAlias()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         */
isDither()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     */
isLinearText()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     */
isSubpixelText()172     bool isSubpixelText() const
173     {
174         return SkToBool(this->getFlags() & kSubpixelText_Flag);
175     }
176 
177     /** 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.
180     */
181     void setSubpixelText(bool subpixelText);
182 
isLCDRenderText()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 
isEmbeddedBitmapText()194     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 
205     /** 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     */
isUnderlineText()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     */
isStrikeThruText()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     */
isFakeBoldText()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     */
isDevKernText()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 
isFilterBitmap()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".
271 
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.
276     */
277     enum Style {
278         kFill_Style,            //!< fill the geometry
279         kStroke_Style,          //!< stroke the geometry
280         kStrokeAndFill_Style,   //!< fill and stroke the geometry
281 
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     */
getStyle()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     */
getColor()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         */
getAlpha()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     */
getStrokeWidth()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     */
getStrokeMiter()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     */
getStrokeCap()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     */
getStrokeJoin()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     */
canComputeFastBounds()431     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 
439     /** 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     */
computeFastBounds(const SkRect & orig,SkRect * storage)460     const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
461         return this->getStyle() == kFill_Style ? orig :
462                     this->computeStrokeFastBounds(orig, storage);
463     }
464 
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     */
getShader()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     */
getColorFilter()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     */
getXfermode()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);
516 
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      */
521     SkXfermode* setXfermodeMode(SkXfermode::Mode);
522 
523     /** 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     */
getPathEffect()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     */
getMaskFilter()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     */
getTypeface()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     */
getRasterizer()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 
getLooper()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     */
getTextAlign()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     */
getTextSize()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     */
getTextScaleX()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     */
getTextSkewX()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 
getTextEncoding()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)
680         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
684     };
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     */
getFontSpacing()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 
711     /** Return true if all of the specified text has a corresponding non-zero
712         glyph ID. If any of the code-points in the text are not supported in
713         the typeface (i.e. the glyph ID would be zero), then return false.
714 
715         If the text encoding for the paint is kGlyph_TextEncoding, then this
716         returns true if all of the specified glyph IDs are non-zero.
717      */
718     bool containsText(const void* text, size_t byteLength) const;
719 
720     /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
721         to zero. Note: this does not look at the text-encoding setting in the
722         paint, only at the typeface.
723     */
724     void glyphsToUnichars(const uint16_t glyphs[], int count,
725                           SkUnichar text[]) const;
726 
727     /** Return the number of drawable units in the specified text buffer.
728         This looks at the current TextEncoding field of the paint. If you also
729         want to have the text converted into glyph IDs, call textToGlyphs
730         instead.
731     */
countText(const void * text,size_t byteLength)732     int countText(const void* text, size_t byteLength) const
733     {
734         return this->textToGlyphs(text, byteLength, NULL);
735     }
736 
737     /** Return the width of the text.
738         @param text         The text to be measured
739         @param length       Number of bytes of text to measure
740         @param bounds       If not NULL, returns the bounds of the text,
741                             relative to (0, 0).
742         @param scale        If not 0, return width as if the canvas were scaled
743                             by this value
744         @return             The advance width of the text
745     */
746     SkScalar    measureText(const void* text, size_t length,
747                             SkRect* bounds, SkScalar scale = 0) const;
748 
749     /** Return the width of the text.
750         @param text         Address of the text
751         @param length       Number of bytes of text to measure
752         @return The width of the text
753     */
measureText(const void * text,size_t length)754     SkScalar measureText(const void* text, size_t length) const
755     {
756         return this->measureText(text, length, NULL, 0);
757     }
758 
759     /** Specify the direction the text buffer should be processed in breakText()
760     */
761     enum TextBufferDirection {
762         /** When measuring text for breakText(), begin at the start of the text
763             buffer and proceed forward through the data. This is the default.
764         */
765         kForward_TextBufferDirection,
766         /** When measuring text for breakText(), begin at the end of the text
767             buffer and proceed backwards through the data.
768         */
769         kBackward_TextBufferDirection
770     };
771 
772     /** Return the width of the text.
773         @param text     The text to be measured
774         @param length   Number of bytes of text to measure
775         @param maxWidth Maximum width. Only the subset of text whose accumulated
776                         widths are <= maxWidth are measured.
777         @param measuredWidth Optional. If non-null, this returns the actual
778                         width of the measured text.
779         @param tbd      Optional. The direction the text buffer should be
780                         traversed during measuring.
781         @return         The number of bytes of text that were measured. Will be
782                         <= length.
783     */
784     size_t  breakText(const void* text, size_t length, SkScalar maxWidth,
785                       SkScalar* measuredWidth = NULL,
786                       TextBufferDirection tbd = kForward_TextBufferDirection)
787                       const;
788 
789     /** Return the advance widths for the characters in the string.
790         @param text         the text
791         @param byteLength   number of bytes to of text
792         @param widths       If not null, returns the array of advance widths of
793                             the glyphs. If not NULL, must be at least a large
794                             as the number of unichars in the specified text.
795         @param bounds       If not null, returns the bounds for each of
796                             character, relative to (0, 0)
797         @return the number of unichars in the specified text.
798     */
799     int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
800                       SkRect bounds[] = NULL) const;
801 
802     /** Return the path (outline) for the specified text.
803         Note: just like SkCanvas::drawText, this will respect the Align setting
804               in the paint.
805     */
806     void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
807                      SkPath* path) const;
808 
809 private:
810     SkTypeface*     fTypeface;
811     SkScalar        fTextSize;
812     SkScalar        fTextScaleX;
813     SkScalar        fTextSkewX;
814 
815     SkPathEffect*   fPathEffect;
816     SkShader*       fShader;
817     SkXfermode*     fXfermode;
818     SkMaskFilter*   fMaskFilter;
819     SkColorFilter*  fColorFilter;
820     SkRasterizer*   fRasterizer;
821     SkDrawLooper*   fLooper;
822 
823     SkColor         fColor;
824     SkScalar        fWidth;
825     SkScalar        fMiterLimit;
826     unsigned        fFlags : 11;
827     unsigned        fTextAlign : 2;
828     unsigned        fCapType : 2;
829     unsigned        fJoinType : 2;
830     unsigned        fStyle : 2;
831     unsigned        fTextEncoding : 2;  // 3 values
832     unsigned        fHinting : 2;
833 
834     SkDrawCacheProc    getDrawCacheProc() const;
835     SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
836                                            bool needFullMetrics) const;
837 
838     SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
839                           int* count, SkRect* bounds) const;
840 
841     SkGlyphCache*   detachCache(const SkMatrix*) const;
842 
843     void descriptorProc(const SkMatrix* deviceMatrix,
844                         void (*proc)(const SkDescriptor*, void*),
845                         void* context) const;
846 
847     const SkRect& computeStrokeFastBounds(const SkRect& orig,
848                                           SkRect* storage) const;
849 
850     enum {
851         kCanonicalTextSizeForPaths = 64
852     };
853     friend class SkCanvas;
854     friend class SkDraw;
855     friend class SkAutoGlyphCache;
856     friend class SkTextToPathIter;
857 };
858 
859 //////////////////////////////////////////////////////////////////////////
860 
861 #include "SkPathEffect.h"
862 
863 /** \class SkStrokePathEffect
864 
865     SkStrokePathEffect simulates stroking inside a patheffect, allowing the
866     caller to have explicit control of when to stroke a path. Typically this is
867     used if the caller wants to stroke before another patheffect is applied
868     (using SkComposePathEffect or SkSumPathEffect).
869 */
870 class SkStrokePathEffect : public SkPathEffect {
871 public:
872     SkStrokePathEffect(const SkPaint&);
873     SkStrokePathEffect(SkScalar width, SkPaint::Style, SkPaint::Join,
874                        SkPaint::Cap, SkScalar miterLimit = -1);
875 
876     // overrides
877     // This method is not exported to java.
878     virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width);
879 
880     // overrides for SkFlattenable
881     // This method is not exported to java.
882     virtual void flatten(SkFlattenableWriteBuffer&);
883     // This method is not exported to java.
884     virtual Factory getFactory();
885 
886 private:
887     SkScalar    fWidth, fMiter;
888     uint8_t     fStyle, fJoin, fCap;
889 
890     static SkFlattenable* CreateProc(SkFlattenableReadBuffer&);
891     SkStrokePathEffect(SkFlattenableReadBuffer&);
892 
893     typedef SkPathEffect INHERITED;
894 
895     // illegal
896     SkStrokePathEffect(const SkStrokePathEffect&);
897     SkStrokePathEffect& operator=(const SkStrokePathEffect&);
898 };
899 
900 #endif
901 
902