Skia
2D Graphics Library
SkStrokeRec.h
Go to the documentation of this file.
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/private/SkMacros.h"
13 
14 class SkPath;
15 
16 SK_BEGIN_REQUIRE_DENSE
18 public:
19  enum InitStyle {
21  kFill_InitStyle
22  };
24  SkStrokeRec(const SkPaint&, SkPaint::Style, SkScalar resScale = 1);
25  explicit SkStrokeRec(const SkPaint&, SkScalar resScale = 1);
26 
27  enum Style {
31  kStrokeAndFill_Style
32  };
33 
34  static constexpr int kStyleCount = kStrokeAndFill_Style + 1;
35 
36  Style getStyle() const;
37  SkScalar getWidth() const { return fWidth; }
38  SkScalar getMiter() const { return fMiterLimit; }
39  SkPaint::Cap getCap() const { return (SkPaint::Cap)fCap; }
40  SkPaint::Join getJoin() const { return (SkPaint::Join)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();
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 
67  return fResScale;
68  }
69 
70  void setResScale(SkScalar rs) {
71  SkASSERT(rs > 0 && SkScalarIsFinite(rs));
72  fResScale = rs;
73  }
74 
79  bool needToApply() const {
80  Style style = this->getStyle();
81  return (kStroke_Style == style) || (kStrokeAndFill_Style == style);
82  }
83 
94  bool applyToPath(SkPath* dst, const SkPath& src) const;
95 
99  void applyToPaint(SkPaint* paint) const;
100 
107 
116 
118  SkScalar strokeWidth);
119 
125  bool hasEqualEffect(const SkStrokeRec& other) const {
126  if (!this->needToApply()) {
127  return this->getStyle() == other.getStyle();
128  }
129  return fWidth == other.fWidth &&
130  (fJoin != SkPaint::kMiter_Join || fMiterLimit == other.fMiterLimit) &&
131  fCap == other.fCap &&
132  fJoin == other.fJoin &&
133  fStrokeAndFill == other.fStrokeAndFill;
134  }
135 
136 private:
137  void init(const SkPaint&, SkPaint::Style, SkScalar resScale);
138 
139  SkScalar fResScale;
140  SkScalar fWidth;
141  SkScalar fMiterLimit;
142  // The following three members are packed together into a single u32.
143  // This is to avoid unnecessary padding and ensure binary equality for
144  // hashing (because the padded areas might contain garbage values).
145  //
146  // fCap and fJoin are larger than needed to avoid having to initialize
147  // any pad values
148  uint32_t fCap : 16; // SkPaint::Cap
149  uint32_t fJoin : 15; // SkPaint::Join
150  uint32_t fStrokeAndFill : 1; // bool
151 };
152 SK_END_REQUIRE_DENSE
153 
154 #endif
SkStrokeRec::kFill_Style
@ kFill_Style
Definition: SkStrokeRec.h:29
SkPath
Definition: SkPath.h:44
SkPaint::kMiter_Join
@ kMiter_Join
extends to miter limit
Definition: SkPaint.h:390
SkStrokeRec::getCap
SkPaint::Cap getCap() const
Definition: SkStrokeRec.h:39
SkPaint::Cap
Cap
Definition: SkPaint.h:364
SkStrokeRec::getResScale
SkScalar getResScale() const
Definition: SkStrokeRec.h:66
SkStrokeRec::SkStrokeRec
SkStrokeRec(const SkPaint &, SkPaint::Style, SkScalar resScale=1)
SkPaint::Join
Join
Definition: SkPaint.h:389
SkStrokeRec::setResScale
void setResScale(SkScalar rs)
Definition: SkStrokeRec.h:70
SkStrokeRec::GetInflationRadius
static SkScalar GetInflationRadius(SkPaint::Join, SkScalar miterLimit, SkPaint::Cap, SkScalar strokeWidth)
SkPaint
Definition: SkPaint.h:39
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkStrokeRec
Definition: SkStrokeRec.h:17
SkStrokeRec::hasEqualEffect
bool hasEqualEffect(const SkStrokeRec &other) const
Compare if two SkStrokeRecs have an equal effect on a path.
Definition: SkStrokeRec.h:125
SkStrokeRec::applyToPath
bool applyToPath(SkPath *dst, const SkPath &src) const
Apply these stroke parameters to the src path, returning the result in dst.
SkStrokeRec::SkStrokeRec
SkStrokeRec(InitStyle style)
SkStrokeRec::needToApply
bool needToApply() const
Returns true if this specifes any thick stroking, i.e.
Definition: SkStrokeRec.h:79
SkStrokeRec::applyToPaint
void applyToPaint(SkPaint *paint) const
Apply these stroke parameters to a paint.
SkStrokeRec::setStrokeStyle
void setStrokeStyle(SkScalar width, bool strokeAndFill=false)
Specify the strokewidth, and optionally if you want stroke + fill.
SkPaint.h
SkStrokeRec::setFillStyle
void setFillStyle()
SkStrokeRec::getWidth
SkScalar getWidth() const
Definition: SkStrokeRec.h:37
SkStrokeRec::isHairlineStyle
bool isHairlineStyle() const
Definition: SkStrokeRec.h:42
SkStrokeRec::setStrokeParams
void setStrokeParams(SkPaint::Cap cap, SkPaint::Join join, SkScalar miterLimit)
Definition: SkStrokeRec.h:60
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkStrokeRec::GetInflationRadius
static SkScalar GetInflationRadius(const SkPaint &, SkPaint::Style)
Equivalent to: SkStrokeRec rec(paint, style); rec.getInflationRadius(); This does not account for oth...
SkStrokeRec::kHairline_Style
@ kHairline_Style
Definition: SkStrokeRec.h:28
SkStrokeRec::getInflationRadius
SkScalar getInflationRadius() const
Gives a conservative value for the outset that should applied to a geometries bounds to account for a...
SkStrokeRec::getJoin
SkPaint::Join getJoin() const
Definition: SkStrokeRec.h:40
SkStrokeRec::Style
Style
Definition: SkStrokeRec.h:27
SkStrokeRec::setHairlineStyle
void setHairlineStyle()
SkStrokeRec::getMiter
SkScalar getMiter() const
Definition: SkStrokeRec.h:38
SkStrokeRec::SkStrokeRec
SkStrokeRec(const SkPaint &, SkScalar resScale=1)
SkStrokeRec::kHairline_InitStyle
@ kHairline_InitStyle
Definition: SkStrokeRec.h:20
SkStrokeRec::getStyle
Style getStyle() const
SkPaint::Style
Style
Definition: SkPaint.h:225
SkStrokeRec::isFillStyle
bool isFillStyle() const
Definition: SkStrokeRec.h:46
SkStrokeRec::kStroke_Style
@ kStroke_Style
Definition: SkStrokeRec.h:30
SkStrokeRec::InitStyle
InitStyle
Definition: SkStrokeRec.h:19
SkScalarIsFinite
static bool SkScalarIsFinite(SkScalar x)
Returns true if x is not NaN and not infinite.
Definition: SkScalar.h:71