Home | History | Annotate | Download | only in core
      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 "SkDrawLooper.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 SkTypeface;
     39 
     40 typedef const SkGlyph& (*SkDrawCacheProc)(SkGlyphCache*, const char**,
     41                                            SkFixed x, SkFixed y);
     42 
     43 typedef const SkGlyph& (*SkMeasureCacheProc)(SkGlyphCache*, const char**);
     44 
     45 /** \class SkPaint
     46 
     47     The SkPaint class holds the style and color information about how to draw
     48     geometries, text and bitmaps.
     49 */
     50 class SK_API SkPaint {
     51 public:
     52     SkPaint();
     53     SkPaint(const SkPaint& paint);
     54     ~SkPaint();
     55 
     56     SkPaint& operator=(const SkPaint&);
     57 
     58     SK_API friend int operator==(const SkPaint& a, const SkPaint& b);
     59     friend int operator!=(const SkPaint& a, const SkPaint& b) {
     60         return !(a == b);
     61     }
     62 
     63     void flatten(SkFlattenableWriteBuffer&) const;
     64     void unflatten(SkFlattenableReadBuffer&);
     65 
     66     /** Restores the paint to its initial settings.
     67     */
     68     void reset();
     69 
     70     /** Specifies the level of hinting to be performed. These names are taken
     71         from the Gnome/Cairo names for the same. They are translated into
     72         Freetype concepts the same as in cairo-ft-font.c:
     73            kNo_Hinting     -> FT_LOAD_NO_HINTING
     74            kSlight_Hinting -> FT_LOAD_TARGET_LIGHT
     75            kNormal_Hinting -> <default, no option>
     76            kFull_Hinting   -> <same as kNormalHinting, unless we are rendering
     77                               subpixel glyphs, in which case TARGET_LCD or
     78                               TARGET_LCD_V is used>
     79     */
     80     enum Hinting {
     81         kNo_Hinting            = 0,
     82         kSlight_Hinting        = 1,
     83         kNormal_Hinting        = 2,     //!< this is the default
     84         kFull_Hinting          = 3,
     85     };
     86 
     87     Hinting getHinting() const {
     88         return static_cast<Hinting>(fHinting);
     89     }
     90 
     91     void setHinting(Hinting hintingLevel);
     92 
     93     /** Specifies the bit values that are stored in the paint's flags.
     94     */
     95     enum Flags {
     96         kAntiAlias_Flag       = 0x01,   //!< mask to enable antialiasing
     97         kFilterBitmap_Flag    = 0x02,   //!< mask to enable bitmap filtering
     98         kDither_Flag          = 0x04,   //!< mask to enable dithering
     99         kUnderlineText_Flag   = 0x08,   //!< mask to enable underline text
    100         kStrikeThruText_Flag  = 0x10,   //!< mask to enable strike-thru text
    101         kFakeBoldText_Flag    = 0x20,   //!< mask to enable fake-bold text
    102         kLinearText_Flag      = 0x40,   //!< mask to enable linear-text
    103         kSubpixelText_Flag    = 0x80,   //!< mask to enable subpixel text positioning
    104         kDevKernText_Flag     = 0x100,  //!< mask to enable device kerning text
    105         kLCDRenderText_Flag   = 0x200,  //!< mask to enable subpixel glyph renderering
    106         kEmbeddedBitmapText_Flag = 0x400, //!< mask to enable embedded bitmap strikes
    107         kAutoHinting_Flag     = 0x800,  //!< mask to force Freetype's autohinter
    108         // when adding extra flags, note that the fFlags member is specified
    109         // with a bit-width and you'll have to expand it.
    110 
    111         kAllFlags = 0xFFF
    112     };
    113 
    114     /** Return the paint's flags. Use the Flag enum to test flag values.
    115         @return the paint's flags (see enums ending in _Flag for bit masks)
    116     */
    117     uint32_t getFlags() const { return fFlags; }
    118 
    119     /** Set the paint's flags. Use the Flag enum to specific flag values.
    120         @param flags    The new flag bits for the paint (see Flags enum)
    121     */
    122     void setFlags(uint32_t flags);
    123 
    124     /** Helper for getFlags(), returning true if kAntiAlias_Flag bit is set
    125         @return true if the antialias bit is set in the paint's flags.
    126         */
    127     bool isAntiAlias() const {
    128         return SkToBool(this->getFlags() & kAntiAlias_Flag);
    129     }
    130 
    131     /** Helper for setFlags(), setting or clearing the kAntiAlias_Flag bit
    132         @param aa   true to enable antialiasing, false to disable it
    133         */
    134     void setAntiAlias(bool aa);
    135 
    136     /** Helper for getFlags(), returning true if kDither_Flag bit is set
    137         @return true if the dithering bit is set in the paint's flags.
    138         */
    139     bool isDither() const {
    140         return SkToBool(this->getFlags() & kDither_Flag);
    141     }
    142 
    143     /** Helper for setFlags(), setting or clearing the kDither_Flag bit
    144         @param dither   true to enable dithering, false to disable it
    145         */
    146     void setDither(bool dither);
    147 
    148     /** Helper for getFlags(), returning true if kLinearText_Flag bit is set
    149         @return true if the lineartext bit is set in the paint's flags
    150     */
    151     bool isLinearText() const {
    152         return SkToBool(this->getFlags() & kLinearText_Flag);
    153     }
    154 
    155     /** Helper for setFlags(), setting or clearing the kLinearText_Flag bit
    156         @param linearText true to set the linearText bit in the paint's flags,
    157                           false to clear it.
    158     */
    159     void setLinearText(bool linearText);
    160 
    161     /** Helper for getFlags(), returning true if kSubpixelText_Flag bit is set
    162         @return true if the lineartext bit is set in the paint's flags
    163     */
    164     bool isSubpixelText() const {
    165         return SkToBool(this->getFlags() & kSubpixelText_Flag);
    166     }
    167 
    168     /** Helper for setFlags(), setting or clearing the kSubpixelText_Flag
    169        bit @param subpixelText true to set the subpixelText bit in the paint's flags,
    170                                false to clear it.
    171     */
    172     void setSubpixelText(bool subpixelText);
    173 
    174     bool isLCDRenderText() const {
    175         return SkToBool(this->getFlags() & kLCDRenderText_Flag);
    176     }
    177 
    178     /** Helper for setFlags(), setting or clearing the kLCDRenderText_Flag bit
    179         @param subpixelRender true to set the subpixelRenderText bit in the paint's flags,
    180                               false to clear it.
    181     */
    182     void setLCDRenderText(bool subpixelRender);
    183 
    184     bool isEmbeddedBitmapText() const {
    185         return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
    186     }
    187 
    188     /** Helper for setFlags(), setting or clearing the kEmbeddedBitmapText_Flag bit
    189         @param useEmbeddedBitmapText true to set the kEmbeddedBitmapText bit in the paint's flags,
    190                                      false to clear it.
    191     */
    192     void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
    193 
    194     bool isAutohinted() const {
    195         return SkToBool(this->getFlags() & kAutoHinting_Flag);
    196     }
    197 
    198     /** Helper for setFlags(), setting or clearing the kAutoHinting_Flag bit
    199         @param useAutohinter true to set the kEmbeddedBitmapText bit in the
    200                                   paint's flags,
    201                              false to clear it.
    202     */
    203     void setAutohinted(bool useAutohinter);
    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     */
    208     bool isUnderlineText() const {
    209         return SkToBool(this->getFlags() & kUnderlineText_Flag);
    210     }
    211 
    212     /** Helper for setFlags(), setting or clearing the kUnderlineText_Flag bit
    213         @param underlineText true to set the underlineText bit in the paint's
    214                              flags, false to clear it.
    215     */
    216     void setUnderlineText(bool underlineText);
    217 
    218     /** Helper for getFlags(), returns true if kStrikeThruText_Flag bit is set
    219         @return true if the strikeThruText bit is set in the paint's flags.
    220     */
    221     bool isStrikeThruText() const {
    222         return SkToBool(this->getFlags() & kStrikeThruText_Flag);
    223     }
    224 
    225     /** Helper for setFlags(), setting or clearing the kStrikeThruText_Flag bit
    226         @param strikeThruText   true to set the strikeThruText bit in the
    227                                 paint's flags, false to clear it.
    228     */
    229     void setStrikeThruText(bool strikeThruText);
    230 
    231     /** Helper for getFlags(), returns true if kFakeBoldText_Flag bit is set
    232         @return true if the kFakeBoldText_Flag bit is set in the paint's flags.
    233     */
    234     bool isFakeBoldText() const {
    235         return SkToBool(this->getFlags() & kFakeBoldText_Flag);
    236     }
    237 
    238     /** Helper for setFlags(), setting or clearing the kFakeBoldText_Flag bit
    239         @param fakeBoldText true to set the kFakeBoldText_Flag bit in the paint's
    240                             flags, false to clear it.
    241     */
    242     void setFakeBoldText(bool fakeBoldText);
    243 
    244     /** Helper for getFlags(), returns true if kDevKernText_Flag bit is set
    245         @return true if the kernText bit is set in the paint's flags.
    246     */
    247     bool isDevKernText() const {
    248         return SkToBool(this->getFlags() & kDevKernText_Flag);
    249     }
    250 
    251     /** Helper for setFlags(), setting or clearing the kKernText_Flag bit
    252         @param kernText true to set the kKernText_Flag bit in the paint's
    253                             flags, false to clear it.
    254     */
    255     void setDevKernText(bool devKernText);
    256 
    257     bool isFilterBitmap() const {
    258         return SkToBool(this->getFlags() & kFilterBitmap_Flag);
    259     }
    260 
    261     void setFilterBitmap(bool filterBitmap);
    262 
    263     /** Styles apply to rect, oval, path, and text.
    264         Bitmaps are always drawn in "fill", and lines are always drawn in
    265         "stroke".
    266 
    267         Note: strokeandfill implicitly draws the result with
    268         SkPath::kWinding_FillType, so if the original path is even-odd, the
    269         results may not appear the same as if it was drawn twice, filled and
    270         then stroked.
    271     */
    272     enum Style {
    273         kFill_Style,            //!< fill the geometry
    274         kStroke_Style,          //!< stroke the geometry
    275         kStrokeAndFill_Style,   //!< fill and stroke the geometry
    276 
    277         kStyleCount,
    278     };
    279 
    280     /** Return the paint's style, used for controlling how primitives'
    281         geometries are interpreted (except for drawBitmap, which always assumes
    282         kFill_Style).
    283         @return the paint's Style
    284     */
    285     Style getStyle() const { return (Style)fStyle; }
    286 
    287     /** Set the paint's style, used for controlling how primitives'
    288         geometries are interpreted (except for drawBitmap, which always assumes
    289         Fill).
    290         @param style    The new style to set in the paint
    291     */
    292     void setStyle(Style style);
    293 
    294     /** Return the paint's color. Note that the color is a 32bit value
    295         containing alpha as well as r,g,b. This 32bit value is not
    296         premultiplied, meaning that its alpha can be any value, regardless of
    297         the values of r,g,b.
    298         @return the paint's color (and alpha).
    299     */
    300     SkColor getColor() const { return fColor; }
    301 
    302     /** Set the paint's color. Note that the color is a 32bit value containing
    303         alpha as well as r,g,b. This 32bit value is not premultiplied, meaning
    304         that its alpha can be any value, regardless of the values of r,g,b.
    305         @param color    The new color (including alpha) to set in the paint.
    306     */
    307     void setColor(SkColor color);
    308 
    309     /** Helper to getColor() that just returns the color's alpha value.
    310         @return the alpha component of the paint's color.
    311         */
    312     uint8_t getAlpha() const { return SkToU8(SkColorGetA(fColor)); }
    313 
    314     /** Helper to setColor(), that only assigns the color's alpha value,
    315         leaving its r,g,b values unchanged.
    316         @param a    set the alpha component (0..255) of the paint's color.
    317     */
    318     void setAlpha(U8CPU a);
    319 
    320     /** Helper to setColor(), that takes a,r,g,b and constructs the color value
    321         using SkColorSetARGB()
    322         @param a    The new alpha component (0..255) of the paint's color.
    323         @param r    The new red component (0..255) of the paint's color.
    324         @param g    The new green component (0..255) of the paint's color.
    325         @param b    The new blue component (0..255) of the paint's color.
    326     */
    327     void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
    328 
    329     /** Return the width for stroking.
    330         <p />
    331         A value of 0 strokes in hairline mode.
    332         Hairlines always draw 1-pixel wide, regardless of the matrix.
    333         @return the paint's stroke width, used whenever the paint's style is
    334                 Stroke or StrokeAndFill.
    335     */
    336     SkScalar getStrokeWidth() const { return fWidth; }
    337 
    338     /** Set the width for stroking.
    339         Pass 0 to stroke in hairline mode.
    340         Hairlines always draw 1-pixel wide, regardless of the matrix.
    341         @param width set the paint's stroke width, used whenever the paint's
    342                      style is Stroke or StrokeAndFill.
    343     */
    344     void setStrokeWidth(SkScalar width);
    345 
    346     /** Return the paint's stroke miter value. This is used to control the
    347         behavior of miter joins when the joins angle is sharp.
    348         @return the paint's miter limit, used whenever the paint's style is
    349                 Stroke or StrokeAndFill.
    350     */
    351     SkScalar getStrokeMiter() const { return fMiterLimit; }
    352 
    353     /** Set the paint's stroke miter value. This is used to control the
    354         behavior of miter joins when the joins angle is sharp. This value must
    355         be >= 0.
    356         @param miter    set the miter limit on the paint, used whenever the
    357                         paint's style is Stroke or StrokeAndFill.
    358     */
    359     void setStrokeMiter(SkScalar miter);
    360 
    361     /** Cap enum specifies the settings for the paint's strokecap. This is the
    362         treatment that is applied to the beginning and end of each non-closed
    363         contour (e.g. lines).
    364     */
    365     enum Cap {
    366         kButt_Cap,      //!< begin/end contours with no extension
    367         kRound_Cap,     //!< begin/end contours with a semi-circle extension
    368         kSquare_Cap,    //!< begin/end contours with a half square extension
    369 
    370         kCapCount,
    371         kDefault_Cap = kButt_Cap
    372     };
    373 
    374     /** Join enum specifies the settings for the paint's strokejoin. This is
    375         the treatment that is applied to corners in paths and rectangles.
    376     */
    377     enum Join {
    378         kMiter_Join,    //!< connect path segments with a sharp join
    379         kRound_Join,    //!< connect path segments with a round join
    380         kBevel_Join,    //!< connect path segments with a flat bevel join
    381 
    382         kJoinCount,
    383         kDefault_Join = kMiter_Join
    384     };
    385 
    386     /** Return the paint's stroke cap type, controlling how the start and end
    387         of stroked lines and paths are treated.
    388         @return the line cap style for the paint, used whenever the paint's
    389                 style is Stroke or StrokeAndFill.
    390     */
    391     Cap getStrokeCap() const { return (Cap)fCapType; }
    392 
    393     /** Set the paint's stroke cap type.
    394         @param cap  set the paint's line cap style, used whenever the paint's
    395                     style is Stroke or StrokeAndFill.
    396     */
    397     void setStrokeCap(Cap cap);
    398 
    399     /** Return the paint's stroke join type.
    400         @return the paint's line join style, used whenever the paint's style is
    401                 Stroke or StrokeAndFill.
    402     */
    403     Join getStrokeJoin() const { return (Join)fJoinType; }
    404 
    405     /** Set the paint's stroke join type.
    406         @param join set the paint's line join style, used whenever the paint's
    407                     style is Stroke or StrokeAndFill.
    408     */
    409     void setStrokeJoin(Join join);
    410 
    411     /** Applies any/all effects (patheffect, stroking) to src, returning the
    412         result in dst. The result is that drawing src with this paint will be
    413         the same as drawing dst with a default paint (at least from the
    414         geometric perspective).
    415         @param src  input path
    416         @param dst  output path (may be the same as src)
    417         @return     true if the path should be filled, or false if it should be
    418                     drawn with a hairline (width == 0)
    419     */
    420     bool getFillPath(const SkPath& src, SkPath* dst) const;
    421 
    422     /** Returns true if the current paint settings allow for fast computation of
    423         bounds (i.e. there is nothing complex like a patheffect that would make
    424         the bounds computation expensive.
    425     */
    426     bool canComputeFastBounds() const {
    427         if (this->getLooper()) {
    428             return this->getLooper()->canComputeFastBounds(*this);
    429         }
    430         // use bit-or since no need for early exit
    431         return (reinterpret_cast<uintptr_t>(this->getRasterizer()) |
    432                 reinterpret_cast<uintptr_t>(this->getPathEffect())) == 0;
    433     }
    434 
    435     /** Only call this if canComputeFastBounds() returned true. This takes a
    436         raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
    437         effects in the paint (e.g. stroking). If needed, it uses the storage
    438         rect parameter. It returns the adjusted bounds that can then be used
    439         for quickReject tests.
    440 
    441         The returned rect will either be orig or storage, thus the caller
    442         should not rely on storage being set to the result, but should always
    443         use the retured value. It is legal for orig and storage to be the same
    444         rect.
    445 
    446         e.g.
    447         if (paint.canComputeFastBounds()) {
    448             SkRect r, storage;
    449             path.computeBounds(&r, SkPath::kFast_BoundsType);
    450             const SkRect& fastR = paint.computeFastBounds(r, &storage);
    451             if (canvas->quickReject(fastR, ...)) {
    452                 // don't draw the path
    453             }
    454         }
    455     */
    456     const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
    457         if (this->getStyle() == kFill_Style &&
    458                 !this->getLooper() && !this->getMaskFilter()) {
    459             return orig;
    460         }
    461 
    462         return this->doComputeFastBounds(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     */
    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);
    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     */
    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     /**
    604      *  Return the paint's SkDrawLooper (if any). Does not affect the looper's
    605      *  reference count.
    606      */
    607     SkDrawLooper* getLooper() const { return fLooper; }
    608 
    609     /**
    610      *  Set or clear the looper object.
    611      *  <p />
    612      *  Pass NULL to clear any previous looper.
    613      *  As a convenience, the parameter passed is also returned.
    614      *  If a previous looper exists in the paint, its reference count is
    615      *  decremented. If looper is not NULL, its reference count is
    616      *  incremented.
    617      *  @param looper May be NULL. The new looper to be installed in the paint.
    618      *  @return looper
    619      */
    620     SkDrawLooper* setLooper(SkDrawLooper* looper);
    621 
    622     enum Align {
    623         kLeft_Align,
    624         kCenter_Align,
    625         kRight_Align,
    626 
    627         kAlignCount
    628     };
    629 
    630     /** Return the paint's Align value for drawing text.
    631         @return the paint's Align value for drawing text.
    632     */
    633     Align   getTextAlign() const { return (Align)fTextAlign; }
    634 
    635     /** Set the paint's text alignment.
    636         @param align set the paint's Align value for drawing text.
    637     */
    638     void    setTextAlign(Align align);
    639 
    640     /** Return the paint's text size.
    641         @return the paint's text size.
    642     */
    643     SkScalar getTextSize() const { return fTextSize; }
    644 
    645     /** Set the paint's text size. This value must be > 0
    646         @param textSize set the paint's text size.
    647     */
    648     void setTextSize(SkScalar textSize);
    649 
    650     /** Return the paint's horizontal scale factor for text. The default value
    651         is 1.0.
    652         @return the paint's scale factor in X for drawing/measuring text
    653     */
    654     SkScalar getTextScaleX() const { return fTextScaleX; }
    655 
    656     /** Set the paint's horizontal scale factor for text. The default value
    657         is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
    658         stretch the text narrower.
    659         @param scaleX   set the paint's scale factor in X for drawing/measuring
    660                         text.
    661     */
    662     void setTextScaleX(SkScalar scaleX);
    663 
    664     /** Return the paint's horizontal skew factor for text. The default value
    665         is 0.
    666         @return the paint's skew factor in X for drawing text.
    667     */
    668     SkScalar getTextSkewX() const { return fTextSkewX; }
    669 
    670     /** Set the paint's horizontal skew factor for text. The default value
    671         is 0. For approximating oblique text, use values around -0.25.
    672         @param skewX set the paint's skew factor in X for drawing text.
    673     */
    674     void setTextSkewX(SkScalar skewX);
    675 
    676     /** Describes how to interpret the text parameters that are passed to paint
    677         methods like measureText() and getTextWidths().
    678     */
    679     enum TextEncoding {
    680         kUTF8_TextEncoding,     //!< the text parameters are UTF8
    681         kUTF16_TextEncoding,    //!< the text parameters are UTF16
    682         kGlyphID_TextEncoding   //!< the text parameters are glyph indices
    683     };
    684 
    685     TextEncoding getTextEncoding() const { return (TextEncoding)fTextEncoding; }
    686 
    687     void setTextEncoding(TextEncoding encoding);
    688 
    689     struct FontMetrics {
    690         SkScalar    fTop;       //!< The greatest distance above the baseline for any glyph (will be <= 0)
    691         SkScalar    fAscent;    //!< The recommended distance above the baseline (will be <= 0)
    692         SkScalar    fDescent;   //!< The recommended distance below the baseline (will be >= 0)
    693         SkScalar    fBottom;    //!< The greatest distance below the baseline for any glyph (will be >= 0)
    694         SkScalar    fLeading;   //!< The recommended distance to add between lines of text (will be >= 0)
    695         SkScalar    fAvgCharWidth;  //!< the average charactor width (>= 0)
    696         SkScalar    fXMin;      //!< The minimum bounding box x value for all glyphs
    697         SkScalar    fXMax;      //!< The maximum bounding box x value for all glyphs
    698         SkScalar    fXHeight;   //!< the height of an 'x' in px, or 0 if no 'x' in face
    699     };
    700 
    701     /** Return the recommend spacing between lines (which will be
    702         fDescent - fAscent + fLeading).
    703         If metrics is not null, return in it the font metrics for the
    704         typeface/pointsize/etc. currently set in the paint.
    705         @param metrics      If not null, returns the font metrics for the
    706                             current typeface/pointsize/etc setting in this
    707                             paint.
    708         @param scale        If not 0, return width as if the canvas were scaled
    709                             by this value
    710         @param return the recommended spacing between lines
    711     */
    712     SkScalar getFontMetrics(FontMetrics* metrics, SkScalar scale = 0) const;
    713 
    714     /** Return the recommend line spacing. This will be
    715         fDescent - fAscent + fLeading
    716     */
    717     SkScalar getFontSpacing() const { return this->getFontMetrics(NULL, 0); }
    718 
    719     /** Convert the specified text into glyph IDs, returning the number of
    720         glyphs ID written. If glyphs is NULL, it is ignore and only the count
    721         is returned.
    722     */
    723     int textToGlyphs(const void* text, size_t byteLength,
    724                      uint16_t glyphs[]) const;
    725 
    726     /** Return true if all of the specified text has a corresponding non-zero
    727         glyph ID. If any of the code-points in the text are not supported in
    728         the typeface (i.e. the glyph ID would be zero), then return false.
    729 
    730         If the text encoding for the paint is kGlyph_TextEncoding, then this
    731         returns true if all of the specified glyph IDs are non-zero.
    732      */
    733     bool containsText(const void* text, size_t byteLength) const;
    734 
    735     /** Convert the glyph array into Unichars. Unconvertable glyphs are mapped
    736         to zero. Note: this does not look at the text-encoding setting in the
    737         paint, only at the typeface.
    738     */
    739     void glyphsToUnichars(const uint16_t glyphs[], int count,
    740                           SkUnichar text[]) const;
    741 
    742     /** Return the number of drawable units in the specified text buffer.
    743         This looks at the current TextEncoding field of the paint. If you also
    744         want to have the text converted into glyph IDs, call textToGlyphs
    745         instead.
    746     */
    747     int countText(const void* text, size_t byteLength) const {
    748         return this->textToGlyphs(text, byteLength, NULL);
    749     }
    750 
    751     /** Return the width of the text.
    752         @param text         The text to be measured
    753         @param length       Number of bytes of text to measure
    754         @param bounds       If not NULL, returns the bounds of the text,
    755                             relative to (0, 0).
    756         @param scale        If not 0, return width as if the canvas were scaled
    757                             by this value
    758         @return             The advance width of the text
    759     */
    760     SkScalar measureText(const void* text, size_t length,
    761                          SkRect* bounds, SkScalar scale = 0) const;
    762 
    763     /** Return the width of the text.
    764         @param text         Address of the text
    765         @param length       Number of bytes of text to measure
    766         @return The width of the text
    767     */
    768     SkScalar measureText(const void* text, size_t length) const {
    769         return this->measureText(text, length, NULL, 0);
    770     }
    771 
    772     /** Specify the direction the text buffer should be processed in breakText()
    773     */
    774     enum TextBufferDirection {
    775         /** When measuring text for breakText(), begin at the start of the text
    776             buffer and proceed forward through the data. This is the default.
    777         */
    778         kForward_TextBufferDirection,
    779         /** When measuring text for breakText(), begin at the end of the text
    780             buffer and proceed backwards through the data.
    781         */
    782         kBackward_TextBufferDirection
    783     };
    784 
    785     /** Return the width of the text.
    786         @param text     The text to be measured
    787         @param length   Number of bytes of text to measure
    788         @param maxWidth Maximum width. Only the subset of text whose accumulated
    789                         widths are <= maxWidth are measured.
    790         @param measuredWidth Optional. If non-null, this returns the actual
    791                         width of the measured text.
    792         @param tbd      Optional. The direction the text buffer should be
    793                         traversed during measuring.
    794         @return         The number of bytes of text that were measured. Will be
    795                         <= length.
    796     */
    797     size_t  breakText(const void* text, size_t length, SkScalar maxWidth,
    798                       SkScalar* measuredWidth = NULL,
    799                       TextBufferDirection tbd = kForward_TextBufferDirection)
    800                       const;
    801 
    802     /** Return the advance widths for the characters in the string.
    803         @param text         the text
    804         @param byteLength   number of bytes to of text
    805         @param widths       If not null, returns the array of advance widths of
    806                             the glyphs. If not NULL, must be at least a large
    807                             as the number of unichars in the specified text.
    808         @param bounds       If not null, returns the bounds for each of
    809                             character, relative to (0, 0)
    810         @return the number of unichars in the specified text.
    811     */
    812     int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
    813                       SkRect bounds[] = NULL) const;
    814 
    815     /** Return the path (outline) for the specified text.
    816         Note: just like SkCanvas::drawText, this will respect the Align setting
    817               in the paint.
    818     */
    819     void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
    820                      SkPath* path) const;
    821 
    822 #ifdef ANDROID
    823     const SkGlyph& getUnicharMetrics(SkUnichar);
    824     const SkGlyph& getGlyphMetrics(uint16_t);
    825     const void* findImage(const SkGlyph&);
    826 
    827     uint32_t getGenerationID() const;
    828 #endif
    829 
    830 private:
    831     SkTypeface*     fTypeface;
    832     SkScalar        fTextSize;
    833     SkScalar        fTextScaleX;
    834     SkScalar        fTextSkewX;
    835 
    836     SkPathEffect*   fPathEffect;
    837     SkShader*       fShader;
    838     SkXfermode*     fXfermode;
    839     SkMaskFilter*   fMaskFilter;
    840     SkColorFilter*  fColorFilter;
    841     SkRasterizer*   fRasterizer;
    842     SkDrawLooper*   fLooper;
    843 
    844     SkColor         fColor;
    845     SkScalar        fWidth;
    846     SkScalar        fMiterLimit;
    847     unsigned        fFlags : 12;
    848     unsigned        fTextAlign : 2;
    849     unsigned        fCapType : 2;
    850     unsigned        fJoinType : 2;
    851     unsigned        fStyle : 2;
    852     unsigned        fTextEncoding : 2;  // 3 values
    853     unsigned        fHinting : 2;
    854 #ifdef ANDROID
    855     uint32_t        fGenerationID;
    856 #endif
    857 
    858     SkDrawCacheProc    getDrawCacheProc() const;
    859     SkMeasureCacheProc getMeasureCacheProc(TextBufferDirection dir,
    860                                            bool needFullMetrics) const;
    861 
    862     SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
    863                           int* count, SkRect* bounds) const;
    864 
    865     SkGlyphCache*   detachCache(const SkMatrix*) const;
    866 
    867     void descriptorProc(const SkMatrix* deviceMatrix,
    868                         void (*proc)(const SkDescriptor*, void*),
    869                         void* context, bool ignoreGamma = false) const;
    870 
    871     const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage) const;
    872 
    873     enum {
    874         kCanonicalTextSizeForPaths = 64
    875     };
    876     friend class SkAutoGlyphCache;
    877     friend class SkCanvas;
    878     friend class SkDraw;
    879     friend class SkPDFDevice;
    880     friend class SkTextToPathIter;
    881 };
    882 
    883 //////////////////////////////////////////////////////////////////////////
    884 
    885 #include "SkPathEffect.h"
    886 
    887 /** \class SkStrokePathEffect
    888 
    889     SkStrokePathEffect simulates stroking inside a patheffect, allowing the
    890     caller to have explicit control of when to stroke a path. Typically this is
    891     used if the caller wants to stroke before another patheffect is applied
    892     (using SkComposePathEffect or SkSumPathEffect).
    893 */
    894 class SkStrokePathEffect : public SkPathEffect {
    895 public:
    896     SkStrokePathEffect(const SkPaint&);
    897     SkStrokePathEffect(SkScalar width, SkPaint::Style, SkPaint::Join,
    898                        SkPaint::Cap, SkScalar miterLimit = -1);
    899 
    900     // overrides
    901     // This method is not exported to java.
    902     virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width);
    903 
    904     // overrides for SkFlattenable
    905     // This method is not exported to java.
    906     virtual void flatten(SkFlattenableWriteBuffer&);
    907     // This method is not exported to java.
    908     virtual Factory getFactory();
    909 
    910 private:
    911     SkScalar    fWidth, fMiter;
    912     uint8_t     fStyle, fJoin, fCap;
    913 
    914     static SkFlattenable* CreateProc(SkFlattenableReadBuffer&);
    915     SkStrokePathEffect(SkFlattenableReadBuffer&);
    916 
    917     typedef SkPathEffect INHERITED;
    918 
    919     // illegal
    920     SkStrokePathEffect(const SkStrokePathEffect&);
    921     SkStrokePathEffect& operator=(const SkStrokePathEffect&);
    922 };
    923 
    924 #endif
    925 
    926