| 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 "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkScalar.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "include/private/base/SkMacros.h" |
| 15 |
|
| 16 |
#include <cmath> |
| 17 |
#include <cstdint> |
| 18 |
|
| 19 |
class SkPath; |
| 20 |
class SkPathBuilder; |
| 21 |
|
| 22 |
SK_BEGIN_REQUIRE_DENSE |
| 23 |
class SK_API SkStrokeRec { |
| 24 |
public: |
| 25 |
enum InitStyle { |
| 26 |
kHairline_InitStyle, |
| 27 |
kFill_InitStyle |
| 28 |
}; |
| 29 |
explicit SkStrokeRec(InitStyle style); |
| 30 |
SkStrokeRec(const SkPaint&, SkPaint::Style, SkScalar resScale = 1); |
| 31 |
explicit SkStrokeRec(const SkPaint&, SkScalar resScale = 1); |
| 32 |
|
| 33 |
enum Style { |
| 34 |
kHairline_Style, |
| 35 |
kFill_Style, |
| 36 |
kStroke_Style, |
| 37 |
kStrokeAndFill_Style |
| 38 |
}; |
| 39 |
|
| 40 |
static constexpr int kStyleCount = kStrokeAndFill_Style + 1; |
| 41 |
|
| 42 |
Style getStyle() const; |
| 43 |
SkScalar getWidth() const { return fWidth; } |
| 44 |
SkScalar getMiter() const { return fMiterLimit; } |
| 45 |
SkPaint::Cap getCap() const { return (SkPaint::Cap)fCap; } |
| 46 |
SkPaint::Join getJoin() const { return (SkPaint::Join)fJoin; } |
| 47 |
|
| 48 |
bool isHairlineStyle() const { |
| 49 |
return kHairline_Style == this->getStyle(); |
| 50 |
} |
| 51 |
|
| 52 |
bool isFillStyle() const { |
| 53 |
return kFill_Style == this->getStyle(); |
| 54 |
} |
| 55 |
|
| 56 |
void setFillStyle(); |
| 57 |
void setHairlineStyle(); |
| 58 |
/** |
| 59 |
* Specify the strokewidth, and optionally if you want stroke + fill. |
| 60 |
* Note, if width==0, then this request is taken to mean: |
| 61 |
* strokeAndFill==true -> new style will be Fill |
| 62 |
* strokeAndFill==false -> new style will be Hairline |
| 63 |
*/ |
| 64 |
void setStrokeStyle(SkScalar width, bool strokeAndFill = false); |
| 65 |
|
| 66 |
void setStrokeParams(SkPaint::Cap cap, SkPaint::Join join, SkScalar miterLimit) { |
| 67 |
fCap = cap; |
| 68 |
fJoin = join; |
| 69 |
fMiterLimit = miterLimit; |
| 70 |
} |
| 71 |
|
| 72 |
SkScalar getResScale() const { |
| 73 |
return fResScale; |
| 74 |
} |
| 75 |
|
| 76 |
void setResScale(SkScalar rs) { |
| 77 |
SkASSERT(rs > 0 && std::isfinite(rs)); |
| 78 |
fResScale = rs; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns true if this specifes any thick stroking, i.e. applyToPath() |
| 83 |
* will return true. |
| 84 |
*/ |
| 85 |
bool needToApply() const { |
| 86 |
Style style = this->getStyle(); |
| 87 |
return (kStroke_Style == style) || (kStrokeAndFill_Style == style); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Apply these stroke parameters to the src path, returning the result |
| 92 |
* in dst. |
| 93 |
* |
| 94 |
* If there was no change (i.e. style == hairline or fill) this returns |
| 95 |
* false and dst is unchanged. Otherwise returns true and the result is |
| 96 |
* stored in dst. |
| 97 |
* |
| 98 |
* src and dst may be the same path. |
| 99 |
*/ |
| 100 |
bool applyToPath(SkPathBuilder* dst, const SkPath& src) const; |
| 101 |
|
| 102 |
/** |
| 103 |
* Apply these stroke parameters to a paint. |
| 104 |
*/ |
| 105 |
void applyToPaint(SkPaint* paint) const; |
| 106 |
|
| 107 |
/** |
| 108 |
* Gives a conservative value for the outset that should applied to a |
| 109 |
* geometries bounds to account for any inflation due to applying this |
| 110 |
* strokeRec to the geometry. |
| 111 |
*/ |
| 112 |
SkScalar getInflationRadius() const; |
| 113 |
|
| 114 |
/** |
| 115 |
* Equivalent to: |
| 116 |
* SkStrokeRec rec(paint, style); |
| 117 |
* rec.getInflationRadius(); |
| 118 |
* This does not account for other effects on the paint (i.e. path |
| 119 |
* effect). |
| 120 |
*/ |
| 121 |
static SkScalar GetInflationRadius(const SkPaint&, SkPaint::Style); |
| 122 |
|
| 123 |
static SkScalar GetInflationRadius(SkPaint::Join, SkScalar miterLimit, SkPaint::Cap, |
| 124 |
SkScalar strokeWidth); |
| 125 |
|
| 126 |
/** |
| 127 |
* Compare if two SkStrokeRecs have an equal effect on a path. |
| 128 |
* Equal SkStrokeRecs produce equal paths. Equality of produced |
| 129 |
* paths does not take the ResScale parameter into account. |
| 130 |
*/ |
| 131 |
bool hasEqualEffect(const SkStrokeRec& other) const { |
| 132 |
if (!this->needToApply()) { |
| 133 |
return this->getStyle() == other.getStyle(); |
| 134 |
} |
| 135 |
return fWidth == other.fWidth && |
| 136 |
(fJoin != SkPaint::kMiter_Join || fMiterLimit == other.fMiterLimit) && |
| 137 |
fCap == other.fCap && |
| 138 |
fJoin == other.fJoin && |
| 139 |
fStrokeAndFill == other.fStrokeAndFill; |
| 140 |
} |
| 141 |
|
| 142 |
private: |
| 143 |
void init(const SkPaint&, SkPaint::Style, SkScalar resScale); |
| 144 |
|
| 145 |
SkScalar fResScale; |
| 146 |
SkScalar fWidth; |
| 147 |
SkScalar fMiterLimit; |
| 148 |
// The following three members are packed together into a single u32. |
| 149 |
// This is to avoid unnecessary padding and ensure binary equality for |
| 150 |
// hashing (because the padded areas might contain garbage values). |
| 151 |
// |
| 152 |
// fCap and fJoin are larger than needed to avoid having to initialize |
| 153 |
// any pad values |
| 154 |
uint32_t fCap : 16; // SkPaint::Cap |
| 155 |
uint32_t fJoin : 15; // SkPaint::Join |
| 156 |
uint32_t fStrokeAndFill : 1; // bool |
| 157 |
}; |
| 158 |
SK_END_REQUIRE_DENSE |
| 159 |
|
| 160 |
#endif |