SkPathEffect.h revision fbfcd5602128ec010c82cb733c9cdc0a3254f9f3
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkPathEffect_DEFINED
11#define SkPathEffect_DEFINED
12
13#include "SkFlattenable.h"
14#include "SkPaint.h"
15
16class SkPath;
17
18class SkStrokeRec {
19public:
20    enum InitStyle {
21        kHairline_InitStyle,
22        kFill_InitStyle
23    };
24    SkStrokeRec(InitStyle style);
25
26    SkStrokeRec(const SkStrokeRec&);
27    explicit SkStrokeRec(const SkPaint&);
28
29    enum Style {
30        kHairline_Style,
31        kFill_Style,
32        kStroke_Style,
33        kStrokeAndFill_Style
34    };
35
36    Style getStyle() const;
37    SkScalar getWidth() const { return fWidth; }
38    SkScalar getMiter() const { return fMiterLimit; }
39    SkPaint::Cap getCap() const { return fCap; }
40    SkPaint::Join getJoin() const { return fJoin; }
41
42    bool isHairlineStyle() const {
43        return kHairline_Style == this->getStyle();
44    }
45
46    bool isFillStyle() const {
47        return kFill_Style == this->getStyle();
48    }
49
50    void setFillStyle();
51    void setHairlineStyle();
52    /**
53     *  Specify the strokewidth, and optionally if you want stroke + fill.
54     *  Note, if width==0, then this request is taken to mean:
55     *      strokeAndFill==true -> new style will be Fill
56     *      strokeAndFill==false -> new style will be Hairline
57     */
58    void setStrokeStyle(SkScalar width, bool strokeAndFill = false);
59
60    void setStrokeParams(SkPaint::Cap cap, SkPaint::Join join, SkScalar miterLimit) {
61        fCap = cap;
62        fJoin = join;
63        fMiterLimit = miterLimit;
64    }
65
66    /**
67     *  Returns true if this specifes any thick stroking, i.e. applyToPath()
68     *  will return true.
69     */
70    bool needToApply() const {
71        Style style = this->getStyle();
72        return (kStroke_Style == style) || (kStrokeAndFill_Style == style);
73    }
74
75    /**
76     *  Apply these stroke parameters to the src path, returning the result
77     *  in dst.
78     *
79     *  If there was no change (i.e. style == hairline or fill) this returns
80     *  false and dst is unchanged. Otherwise returns true and the result is
81     *  stored in dst.
82     *
83     *  src and dst may be the same path.
84     */
85    bool applyToPath(SkPath* dst, const SkPath& src) const;
86
87private:
88    SkScalar        fWidth;
89    SkScalar        fMiterLimit;
90    SkPaint::Cap    fCap;
91    SkPaint::Join   fJoin;
92    bool            fStrokeAndFill;
93};
94
95/** \class SkPathEffect
96
97    SkPathEffect is the base class for objects in the SkPaint that affect
98    the geometry of a drawing primitive before it is transformed by the
99    canvas' matrix and drawn.
100
101    Dashing is implemented as a subclass of SkPathEffect.
102*/
103class SK_API SkPathEffect : public SkFlattenable {
104public:
105    SK_DECLARE_INST_COUNT(SkPathEffect)
106
107    SkPathEffect() {}
108
109    /**
110     *  Given a src path (input) and a stroke-rec (input and output), apply
111     *  this effect to the src path, returning the new path in dst, and return
112     *  true. If this effect cannot be applied, return false and ignore dst
113     *  and stroke-rec.
114     *
115     *  The stroke-rec specifies the initial request for stroking (if any).
116     *  The effect can treat this as input only, or it can choose to change
117     *  the rec as well. For example, the effect can decide to change the
118     *  stroke's width or join, or the effect can change the rec from stroke
119     *  to fill (or fill to stroke) in addition to returning a new (dst) path.
120     *
121     *  If this method returns true, the caller will apply (as needed) the
122     *  resulting stroke-rec to dst and then draw.
123     */
124    virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) = 0;
125
126    /**
127     *  Compute a conservative bounds for its effect, given the src bounds.
128     *  The baseline implementation just assigns src to dst.
129     */
130    virtual void computeFastBounds(SkRect* dst, const SkRect& src);
131
132protected:
133    SkPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
134
135private:
136    // illegal
137    SkPathEffect(const SkPathEffect&);
138    SkPathEffect& operator=(const SkPathEffect&);
139
140    typedef SkFlattenable INHERITED;
141};
142
143/** \class SkPairPathEffect
144
145    Common baseclass for Compose and Sum. This subclass manages two pathEffects,
146    including flattening them. It does nothing in filterPath, and is only useful
147    for managing the lifetimes of its two arguments.
148*/
149class SkPairPathEffect : public SkPathEffect {
150public:
151    SkPairPathEffect(SkPathEffect* pe0, SkPathEffect* pe1);
152    virtual ~SkPairPathEffect();
153
154protected:
155    SkPairPathEffect(SkFlattenableReadBuffer&);
156    virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
157
158    // these are visible to our subclasses
159    SkPathEffect* fPE0, *fPE1;
160
161private:
162    typedef SkPathEffect INHERITED;
163};
164
165/** \class SkComposePathEffect
166
167    This subclass of SkPathEffect composes its two arguments, to create
168    a compound pathEffect.
169*/
170class SkComposePathEffect : public SkPairPathEffect {
171public:
172    /** Construct a pathEffect whose effect is to apply first the inner pathEffect
173        and the the outer pathEffect (e.g. outer(inner(path)))
174        The reference counts for outer and inner are both incremented in the constructor,
175        and decremented in the destructor.
176    */
177    SkComposePathEffect(SkPathEffect* outer, SkPathEffect* inner)
178        : INHERITED(outer, inner) {}
179
180    virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) SK_OVERRIDE;
181
182    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposePathEffect)
183
184protected:
185    SkComposePathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
186
187private:
188    // illegal
189    SkComposePathEffect(const SkComposePathEffect&);
190    SkComposePathEffect& operator=(const SkComposePathEffect&);
191
192    typedef SkPairPathEffect INHERITED;
193};
194
195/** \class SkSumPathEffect
196
197    This subclass of SkPathEffect applies two pathEffects, one after the other.
198    Its filterPath() returns true if either of the effects succeeded.
199*/
200class SkSumPathEffect : public SkPairPathEffect {
201public:
202    /** Construct a pathEffect whose effect is to apply two effects, in sequence.
203        (e.g. first(path) + second(path))
204        The reference counts for first and second are both incremented in the constructor,
205        and decremented in the destructor.
206    */
207    SkSumPathEffect(SkPathEffect* first, SkPathEffect* second)
208        : INHERITED(first, second) {}
209
210    virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) SK_OVERRIDE;
211
212    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSumPathEffect)
213
214protected:
215    SkSumPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
216
217private:
218    // illegal
219    SkSumPathEffect(const SkSumPathEffect&);
220    SkSumPathEffect& operator=(const SkSumPathEffect&);
221
222    typedef SkPairPathEffect INHERITED;
223};
224
225#endif
226
227