Skia
2D Graphics Library
SkRRect.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 SkRRect_DEFINED
9 #define SkRRect_DEFINED
10 
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkRect.h"
13 
14 class SkPath;
15 class SkMatrix;
16 class SkString;
17 
31 class SK_API SkRRect {
32 public:
33 
39  SkRRect() = default;
40 
46  SkRRect(const SkRRect& rrect) = default;
47 
53  SkRRect& operator=(const SkRRect& rrect) = default;
54 
62  enum Type {
69  kLastType = kComplex_Type,
70  };
71 
72  Type getType() const {
73  SkASSERT(this->isValid());
74  return static_cast<Type>(fType);
75  }
76 
77  Type type() const { return this->getType(); }
78 
79  inline bool isEmpty() const { return kEmpty_Type == this->getType(); }
80  inline bool isRect() const { return kRect_Type == this->getType(); }
81  inline bool isOval() const { return kOval_Type == this->getType(); }
82  inline bool isSimple() const { return kSimple_Type == this->getType(); }
83  inline bool isNinePatch() const { return kNinePatch_Type == this->getType(); }
84  inline bool isComplex() const { return kComplex_Type == this->getType(); }
85 
91  SkScalar width() const { return fRect.width(); }
92 
98  SkScalar height() const { return fRect.height(); }
99 
108  return fRadii[0];
109  }
110 
114  void setEmpty() { *this = SkRRect(); }
115 
122  void setRect(const SkRect& rect) {
123  if (!this->initializeRect(rect)) {
124  return;
125  }
126 
127  memset(fRadii, 0, sizeof(fRadii));
128  fType = kRect_Type;
129 
130  SkASSERT(this->isValid());
131  }
132 
138  static SkRRect MakeEmpty() { return SkRRect(); }
139 
145  static SkRRect MakeRect(const SkRect& r) {
146  SkRRect rr;
147  rr.setRect(r);
148  return rr;
149  }
150 
158  static SkRRect MakeOval(const SkRect& oval) {
159  SkRRect rr;
160  rr.setOval(oval);
161  return rr;
162  }
163 
176  static SkRRect MakeRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) {
177  SkRRect rr;
178  rr.setRectXY(rect, xRad, yRad);
179  return rr;
180  }
181 
188  void setOval(const SkRect& oval);
189 
203  void setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad);
204 
225  void setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
226  SkScalar rightRad, SkScalar bottomRad);
227 
242  void setRectRadii(const SkRect& rect, const SkVector radii[4]);
243 
247  enum Corner {
252  };
253 
260  const SkRect& rect() const { return fRect; }
261 
267  SkVector radii(Corner corner) const { return fRadii[corner]; }
268 
275  const SkRect& getBounds() const { return fRect; }
276 
286  friend bool operator==(const SkRRect& a, const SkRRect& b) {
287  return a.fRect == b.fRect && SkScalarsEqual(&a.fRadii[0].fX, &b.fRadii[0].fX, 8);
288  }
289 
299  friend bool operator!=(const SkRRect& a, const SkRRect& b) {
300  return a.fRect != b.fRect || !SkScalarsEqual(&a.fRadii[0].fX, &b.fRadii[0].fX, 8);
301  }
302 
321  void inset(SkScalar dx, SkScalar dy, SkRRect* dst) const;
322 
337  void inset(SkScalar dx, SkScalar dy) {
338  this->inset(dx, dy, this);
339  }
340 
356  void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
357  this->inset(-dx, -dy, dst);
358  }
359 
374  void outset(SkScalar dx, SkScalar dy) {
375  this->inset(-dx, -dy, this);
376  }
377 
383  void offset(SkScalar dx, SkScalar dy) {
384  fRect.offset(dx, dy);
385  }
386 
394  return SkRRect(fRect.makeOffset(dx, dy), fRadii, fType);
395  }
396 
405  bool contains(const SkRect& rect) const;
406 
416  bool isValid() const;
417 
418  static constexpr size_t kSizeInMemory = 12 * sizeof(SkScalar);
419 
428  size_t writeToMemory(void* buffer) const;
429 
440  size_t readFromMemory(const void* buffer, size_t length);
441 
454  bool transform(const SkMatrix& matrix, SkRRect* dst) const;
455 
464  void dump(bool asHex) const;
465  SkString dumpToString(bool asHex) const;
466 
472  void dump() const { this->dump(false); }
473 
479  void dumpHex() const { this->dump(true); }
480 
481 private:
482  static bool AreRectAndRadiiValid(const SkRect&, const SkVector[4]);
483 
484  SkRRect(const SkRect& rect, const SkVector radii[4], int32_t type)
485  : fRect(rect)
486  , fRadii{radii[0], radii[1], radii[2], radii[3]}
487  , fType(type) {}
488 
493  bool initializeRect(const SkRect&);
494 
495  void computeType();
496  bool checkCornerContainment(SkScalar x, SkScalar y) const;
497  // Returns true if the radii had to be scaled to fit rect
498  bool scaleRadii();
499 
500  SkRect fRect = SkRect::MakeEmpty();
501  // Radii order is UL, UR, LR, LL. Use Corner enum to index into fRadii[]
502  SkVector fRadii[4] = {{0, 0}, {0, 0}, {0,0}, {0,0}};
503  // use an explicitly sized type so we're sure the class is dense (no uninitialized bytes)
504  int32_t fType = kEmpty_Type;
505  // TODO: add padding so we can use memcpy for flattening and not copy uninitialized data
506 
507  // to access fRadii directly
508  friend class SkPath;
509  friend class SkRRectPriv;
510 };
511 
512 #endif
SkRRect::setEmpty
void setEmpty()
Sets bounds to zero width and height at (0, 0), the origin.
Definition: SkRRect.h:114
SkRRect::inset
void inset(SkScalar dx, SkScalar dy, SkRRect *dst) const
Copies SkRRect to dst, then insets dst bounds by dx and dy, and adjusts dst radii by dx and dy.
SkRRect::offset
void offset(SkScalar dx, SkScalar dy)
Translates SkRRect by (dx, dy).
Definition: SkRRect.h:383
SkRRect::inset
void inset(SkScalar dx, SkScalar dy)
Insets bounds by dx and dy, and adjusts radii by dx and dy.
Definition: SkRRect.h:337
SkPath
Definition: SkPath.h:44
SkRRect::Type
Type
Definition: SkRRect.h:62
SkRRect::getSimpleRadii
SkVector getSimpleRadii() const
Returns top-left corner radii.
Definition: SkRRect.h:107
SkRect::MakeEmpty
static constexpr SkRect SK_WARN_UNUSED_RESULT MakeEmpty()
Returns constructed SkRect set to (0, 0, 0, 0).
Definition: SkRect.h:585
SkRRect::radii
SkVector radii(Corner corner) const
Returns scalar pair for radius of curve on x-axis and y-axis for one corner.
Definition: SkRRect.h:267
SkRRect::isEmpty
bool isEmpty() const
Definition: SkRRect.h:79
SkRRect::getType
Type getType() const
Definition: SkRRect.h:72
SkRRect::setNinePatch
void setNinePatch(const SkRect &rect, SkScalar leftRad, SkScalar topRad, SkScalar rightRad, SkScalar bottomRad)
Sets bounds to rect.
SkRRect::setOval
void setOval(const SkRect &oval)
Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii to half oval....
SkRect.h
SkRRect::kNinePatch_Type
@ kNinePatch_Type
non-zero width and height with axis-aligned radii
Definition: SkRRect.h:67
SkRRect::operator==
friend bool operator==(const SkRRect &a, const SkRRect &b)
Returns true if bounds and radii in a are equal to bounds and radii in b.
Definition: SkRRect.h:286
SkRRect::dumpHex
void dumpHex() const
Writes text representation of SkRRect to standard output.
Definition: SkRRect.h:479
SkRRect::isNinePatch
bool isNinePatch() const
Definition: SkRRect.h:83
SkRRect::height
SkScalar height() const
Returns span on the y-axis.
Definition: SkRRect.h:98
SkRRect
Definition: SkRRect.h:31
SkRRect::transform
bool transform(const SkMatrix &matrix, SkRRect *dst) const
Transforms by SkRRect by matrix, storing result in dst.
SkRRect::setRectRadii
void setRectRadii(const SkRect &rect, const SkVector radii[4])
Sets bounds to rect.
SkRRect::type
Type type() const
Definition: SkRRect.h:77
SkRRect::kOval_Type
@ kOval_Type
non-zero width and height filled with radii
Definition: SkRRect.h:65
SkRRect::SkRRect
SkRRect(const SkRRect &rrect)=default
Initializes to copy of rrect bounds and corner radii.
SkRRect::kLowerLeft_Corner
@ kLowerLeft_Corner
index of bottom-left corner radii
Definition: SkRRect.h:251
SkRRect::contains
bool contains(const SkRect &rect) const
Returns true if rect is inside the bounds and corner radii, and if SkRRect and rect are not empty.
SkPoint
Definition: SkPoint.h:160
SkRRect::SkRRect
SkRRect()=default
Initializes bounds at (0, 0), the origin, with zero width and height.
SkRRect::dump
void dump(bool asHex) const
Writes text representation of SkRRect to standard output.
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkRRect::width
SkScalar width() const
Returns span on the x-axis.
Definition: SkRRect.h:91
SkRRect::isRect
bool isRect() const
Definition: SkRRect.h:80
SkRRect::kUpperLeft_Corner
@ kUpperLeft_Corner
index of top-left corner radii
Definition: SkRRect.h:248
SkRRect::makeOffset
SkRRect SK_WARN_UNUSED_RESULT makeOffset(SkScalar dx, SkScalar dy) const
Returns SkRRect translated by (dx, dy).
Definition: SkRRect.h:393
SkRRect::MakeRectXY
static SkRRect MakeRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Sets to rounded rectangle with the same radii for all four corners.
Definition: SkRRect.h:176
SkRRect::operator=
SkRRect & operator=(const SkRRect &rrect)=default
Copies rrect bounds and corner radii.
SkRRect::isComplex
bool isComplex() const
Definition: SkRRect.h:84
SkRRect::isOval
bool isOval() const
Definition: SkRRect.h:81
SkRRect::kUpperRight_Corner
@ kUpperRight_Corner
index of top-right corner radii
Definition: SkRRect.h:249
SkPoint.h
SkRRect::setRect
void setRect(const SkRect &rect)
Sets bounds to sorted rect, and sets corner radii to zero.
Definition: SkRRect.h:122
SkScalarsEqual
static bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n)
Definition: SkScalar.h:184
SkRRect::outset
void outset(SkScalar dx, SkScalar dy)
Outsets bounds by dx and dy, and adjusts radii by dx and dy.
Definition: SkRRect.h:374
SkRRect::MakeEmpty
static SkRRect MakeEmpty()
Initializes bounds at (0, 0), the origin, with zero width and height.
Definition: SkRRect.h:138
SkRRect::MakeOval
static SkRRect MakeOval(const SkRect &oval)
Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii to half oval....
Definition: SkRRect.h:158
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkRRect::MakeRect
static SkRRect MakeRect(const SkRect &r)
Initializes to copy of r bounds and zeroes corner radii.
Definition: SkRRect.h:145
SkRRect::dump
void dump() const
Writes text representation of SkRRect to standard output.
Definition: SkRRect.h:472
SkRRect::isValid
bool isValid() const
Returns true if bounds and radii values are finite and describe a SkRRect SkRRect::Type that matches ...
SkPoint::fX
SkScalar fX
x-axis value
Definition: SkPoint.h:161
SkMatrix
Definition: SkMatrix.h:48
SkRRect::kLowerRight_Corner
@ kLowerRight_Corner
index of bottom-right corner radii
Definition: SkRRect.h:250
SkRRect::dumpToString
SkString dumpToString(bool asHex) const
SkRRect::kRect_Type
@ kRect_Type
non-zero width and height, and zeroed radii
Definition: SkRRect.h:64
SK_WARN_UNUSED_RESULT
#define SK_WARN_UNUSED_RESULT
Definition: SkTypes.h:60
SkRRect::getBounds
const SkRect & getBounds() const
Returns bounds.
Definition: SkRRect.h:275
SkString
Definition: SkString.h:116
SkRRect::rect
const SkRect & rect() const
Returns bounds.
Definition: SkRRect.h:260
SkRect
Definition: SkRect.h:572
SkRRect::Corner
Corner
Definition: SkRRect.h:247
SkRRect::readFromMemory
size_t readFromMemory(const void *buffer, size_t length)
Reads SkRRect from buffer, reading kSizeInMemory bytes.
SkRRect::isSimple
bool isSimple() const
Definition: SkRRect.h:82
SkRRect::kSimple_Type
@ kSimple_Type
non-zero width and height with equal radii
Definition: SkRRect.h:66
SkRRect::kComplex_Type
@ kComplex_Type
non-zero width and height with arbitrary radii
Definition: SkRRect.h:68
SkRRect::setRectXY
void setRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Sets to rounded rectangle with the same radii for all four corners.
SkRRect::kEmpty_Type
@ kEmpty_Type
zero width or height
Definition: SkRRect.h:63
SkRRect::outset
void outset(SkScalar dx, SkScalar dy, SkRRect *dst) const
Outsets dst bounds by dx and dy, and adjusts radii by dx and dy.
Definition: SkRRect.h:356
SkRRect::operator!=
friend bool operator!=(const SkRRect &a, const SkRRect &b)
Returns true if bounds and radii in a are not equal to bounds and radii in b.
Definition: SkRRect.h:299
SkRRect::writeToMemory
size_t writeToMemory(void *buffer) const
Writes SkRRect to buffer.