SkPathEffect.h revision fd4be26c4202ae91f0f7cf2c03e44b5169d885eb
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() { fWidth = 0; } 52 53 void setStrokeStyle(SkScalar width, bool strokeAndFill = false) { 54 fWidth = width; 55 fStrokeAndFill = strokeAndFill; 56 } 57 58 void setStrokeParams(SkPaint::Cap cap, SkPaint::Join join, SkScalar miterLimit) { 59 fCap = cap; 60 fJoin = join; 61 fMiterLimit = miterLimit; 62 } 63 64 /** 65 * Returns true if this specifes any thick stroking, i.e. applyToPath() 66 * will return true. 67 */ 68 bool needToApply() const { 69 Style style = this->getStyle(); 70 return (kStroke_Style == style) || (kStrokeAndFill_Style == style); 71 } 72 73 /** 74 * Apply these stroke parameters to the src path, returning the result 75 * in dst. 76 * 77 * If there was no change (i.e. style == hairline or fill) this returns 78 * false and dst is unchanged. Otherwise returns true and the result is 79 * stored in dst. 80 * 81 * src and dst may be the same path. 82 */ 83 bool applyToPath(SkPath* dst, const SkPath& src) const; 84 85private: 86 SkScalar fWidth; 87 SkScalar fMiterLimit; 88 SkPaint::Cap fCap; 89 SkPaint::Join fJoin; 90 bool fStrokeAndFill; 91}; 92 93/** \class SkPathEffect 94 95 SkPathEffect is the base class for objects in the SkPaint that affect 96 the geometry of a drawing primitive before it is transformed by the 97 canvas' matrix and drawn. 98 99 Dashing is implemented as a subclass of SkPathEffect. 100*/ 101class SK_API SkPathEffect : public SkFlattenable { 102public: 103 SkPathEffect() {} 104 105 /** 106 * Given a src path (input) and a stroke-rec (input and output), apply 107 * this effect to the src path, returning the new path in dst, and return 108 * true. If this effect cannot be applied, return false and ignore dst 109 * and stroke-rec. 110 * 111 * The stroke-rec specifies the initial request for stroking (if any). 112 * The effect can treat this as input only, or it can choose to change 113 * the rec as well. For example, the effect can decide to change the 114 * stroke's width or join, or the effect can change the rec from stroke 115 * to fill (or fill to stroke) in addition to returning a new (dst) path. 116 * 117 * If this method returns true, the caller will apply (as needed) the 118 * resulting stroke-rec to dst and then draw. 119 */ 120 virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) = 0; 121 122 /** 123 * Compute a conservative bounds for its effect, given the src bounds. 124 * The baseline implementation just assigns src to dst. 125 */ 126 virtual void computeFastBounds(SkRect* dst, const SkRect& src); 127 128protected: 129 SkPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} 130 131private: 132 // illegal 133 SkPathEffect(const SkPathEffect&); 134 SkPathEffect& operator=(const SkPathEffect&); 135 136 typedef SkFlattenable INHERITED; 137}; 138 139/** \class SkPairPathEffect 140 141 Common baseclass for Compose and Sum. This subclass manages two pathEffects, 142 including flattening them. It does nothing in filterPath, and is only useful 143 for managing the lifetimes of its two arguments. 144*/ 145class SkPairPathEffect : public SkPathEffect { 146public: 147 SkPairPathEffect(SkPathEffect* pe0, SkPathEffect* pe1); 148 virtual ~SkPairPathEffect(); 149 150protected: 151 SkPairPathEffect(SkFlattenableReadBuffer&); 152 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; 153 154 // these are visible to our subclasses 155 SkPathEffect* fPE0, *fPE1; 156 157private: 158 typedef SkPathEffect INHERITED; 159}; 160 161/** \class SkComposePathEffect 162 163 This subclass of SkPathEffect composes its two arguments, to create 164 a compound pathEffect. 165*/ 166class SkComposePathEffect : public SkPairPathEffect { 167public: 168 /** Construct a pathEffect whose effect is to apply first the inner pathEffect 169 and the the outer pathEffect (e.g. outer(inner(path))) 170 The reference counts for outer and inner are both incremented in the constructor, 171 and decremented in the destructor. 172 */ 173 SkComposePathEffect(SkPathEffect* outer, SkPathEffect* inner) 174 : INHERITED(outer, inner) {} 175 176 virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) SK_OVERRIDE; 177 178 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposePathEffect) 179 180protected: 181 SkComposePathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} 182 183private: 184 // illegal 185 SkComposePathEffect(const SkComposePathEffect&); 186 SkComposePathEffect& operator=(const SkComposePathEffect&); 187 188 typedef SkPairPathEffect INHERITED; 189}; 190 191/** \class SkSumPathEffect 192 193 This subclass of SkPathEffect applies two pathEffects, one after the other. 194 Its filterPath() returns true if either of the effects succeeded. 195*/ 196class SkSumPathEffect : public SkPairPathEffect { 197public: 198 /** Construct a pathEffect whose effect is to apply two effects, in sequence. 199 (e.g. first(path) + second(path)) 200 The reference counts for first and second are both incremented in the constructor, 201 and decremented in the destructor. 202 */ 203 SkSumPathEffect(SkPathEffect* first, SkPathEffect* second) 204 : INHERITED(first, second) {} 205 206 virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) SK_OVERRIDE; 207 208 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSumPathEffect) 209 210protected: 211 SkSumPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} 212 213private: 214 // illegal 215 SkSumPathEffect(const SkSumPathEffect&); 216 SkSumPathEffect& operator=(const SkSumPathEffect&); 217 218 typedef SkPairPathEffect INHERITED; 219}; 220 221#endif 222 223