SkStrokeRec.h revision 5c8ee2539b9316b22416a991a1f560ef5cec7957
1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkStrokeRec_DEFINED
9#define SkStrokeRec_DEFINED
10
11#include "SkPaint.h"
12
13class SkPath;
14
15class SkStrokeRec {
16public:
17    enum InitStyle {
18        kHairline_InitStyle,
19        kFill_InitStyle
20    };
21    SkStrokeRec(InitStyle style);
22
23    SkStrokeRec(const SkStrokeRec&);
24    explicit SkStrokeRec(const SkPaint&);
25
26    enum Style {
27        kHairline_Style,
28        kFill_Style,
29        kStroke_Style,
30        kStrokeAndFill_Style
31    };
32
33    Style getStyle() const;
34    SkScalar getWidth() const { return fWidth; }
35    SkScalar getMiter() const { return fMiterLimit; }
36    SkPaint::Cap getCap() const { return fCap; }
37    SkPaint::Join getJoin() const { return fJoin; }
38
39    bool isHairlineStyle() const {
40        return kHairline_Style == this->getStyle();
41    }
42
43    bool isFillStyle() const {
44        return kFill_Style == this->getStyle();
45    }
46
47    void setFillStyle();
48    void setHairlineStyle();
49    /**
50     *  Specify the strokewidth, and optionally if you want stroke + fill.
51     *  Note, if width==0, then this request is taken to mean:
52     *      strokeAndFill==true -> new style will be Fill
53     *      strokeAndFill==false -> new style will be Hairline
54     */
55    void setStrokeStyle(SkScalar width, bool strokeAndFill = false);
56
57    void setStrokeParams(SkPaint::Cap cap, SkPaint::Join join, SkScalar miterLimit) {
58        fCap = cap;
59        fJoin = join;
60        fMiterLimit = miterLimit;
61    }
62
63    /**
64     *  Returns true if this specifes any thick stroking, i.e. applyToPath()
65     *  will return true.
66     */
67    bool needToApply() const {
68        Style style = this->getStyle();
69        return (kStroke_Style == style) || (kStrokeAndFill_Style == style);
70    }
71
72    /**
73     *  Apply these stroke parameters to the src path, returning the result
74     *  in dst.
75     *
76     *  If there was no change (i.e. style == hairline or fill) this returns
77     *  false and dst is unchanged. Otherwise returns true and the result is
78     *  stored in dst.
79     *
80     *  src and dst may be the same path.
81     */
82    bool applyToPath(SkPath* dst, const SkPath& src) const;
83
84    bool operator==(const SkStrokeRec& other) const {
85            return fWidth == other.fWidth &&
86                   fMiterLimit == other.fMiterLimit &&
87                   fCap == other.fCap &&
88                   fJoin == other.fJoin &&
89                   fStrokeAndFill == other.fStrokeAndFill;
90    }
91
92private:
93    SkScalar        fWidth;
94    SkScalar        fMiterLimit;
95    SkPaint::Cap    fCap;
96    SkPaint::Join   fJoin;
97    bool            fStrokeAndFill;
98};
99
100#endif
101