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 #include "include/core/SkScalar.h"
14 #include "include/core/SkTypes.h"
15 
16 #include <cstdint>
17 #include <cstring>
18 
19 class SkMatrix;
20 class SkString;
21 
35 class SK_API SkRRect {
36 public:
37 
43  SkRRect() = default;
44 
50  SkRRect(const SkRRect& rrect) = default;
51 
57  SkRRect& operator=(const SkRRect& rrect) = default;
58 
66  enum Type {
73  kLastType = kComplex_Type,
74  };
75 
76  Type getType() const {
77  SkASSERT(this->isValid());
78  return static_cast<Type>(fType);
79  }
80 
81  Type type() const { return this->getType(); }
82 
83  inline bool isEmpty() const { return kEmpty_Type == this->getType(); }
84  inline bool isRect() const { return kRect_Type == this->getType(); }
85  inline bool isOval() const { return kOval_Type == this->getType(); }
86  inline bool isSimple() const { return kSimple_Type == this->getType(); }
87  inline bool isNinePatch() const { return kNinePatch_Type == this->getType(); }
88  inline bool isComplex() const { return kComplex_Type == this->getType(); }
89 
95  SkScalar width() const { return fRect.width(); }
96 
102  SkScalar height() const { return fRect.height(); }
103 
111  SkVector getSimpleRadii() const {
112  return fRadii[0];
113  }
114 
118  void setEmpty() { *this = SkRRect(); }
119 
126  void setRect(const SkRect& rect) {
127  if (!this->initializeRect(rect)) {
128  return;
129  }
130 
131  memset(fRadii, 0, sizeof(fRadii));
132  fType = kRect_Type;
133 
134  SkASSERT(this->isValid());
135  }
136 
142  static SkRRect MakeEmpty() { return SkRRect(); }
143 
149  static SkRRect MakeRect(const SkRect& r) {
150  SkRRect rr;
151  rr.setRect(r);
152  return rr;
153  }
154 
162  static SkRRect MakeOval(const SkRect& oval) {
163  SkRRect rr;
164  rr.setOval(oval);
165  return rr;
166  }
167 
180  static SkRRect MakeRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) {
181  SkRRect rr;
182  rr.setRectXY(rect, xRad, yRad);
183  return rr;
184  }
185 
192  void setOval(const SkRect& oval);
193 
207  void setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad);
208 
229  void setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
230  SkScalar rightRad, SkScalar bottomRad);
231 
246  void setRectRadii(const SkRect& rect, const SkVector radii[4]);
247 
251  enum Corner {
256  };
257 
264  const SkRect& rect() const { return fRect; }
265 
271  SkVector radii(Corner corner) const { return fRadii[corner]; }
272 
279  const SkRect& getBounds() const { return fRect; }
280 
290  friend bool operator==(const SkRRect& a, const SkRRect& b) {
291  return a.fRect == b.fRect && SkScalarsEqual(&a.fRadii[0].fX, &b.fRadii[0].fX, 8);
292  }
293 
303  friend bool operator!=(const SkRRect& a, const SkRRect& b) {
304  return a.fRect != b.fRect || !SkScalarsEqual(&a.fRadii[0].fX, &b.fRadii[0].fX, 8);
305  }
306 
325  void inset(SkScalar dx, SkScalar dy, SkRRect* dst) const;
326 
341  void inset(SkScalar dx, SkScalar dy) {
342  this->inset(dx, dy, this);
343  }
344 
360  void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
361  this->inset(-dx, -dy, dst);
362  }
363 
378  void outset(SkScalar dx, SkScalar dy) {
379  this->inset(-dx, -dy, this);
380  }
381 
387  void offset(SkScalar dx, SkScalar dy) {
388  fRect.offset(dx, dy);
389  }
390 
397  [[nodiscard]] SkRRect makeOffset(SkScalar dx, SkScalar dy) const {
398  return SkRRect(fRect.makeOffset(dx, dy), fRadii, fType);
399  }
400 
409  bool contains(const SkRect& rect) const;
410 
420  bool isValid() const;
421 
422  static constexpr size_t kSizeInMemory = 12 * sizeof(SkScalar);
423 
432  size_t writeToMemory(void* buffer) const;
433 
444  size_t readFromMemory(const void* buffer, size_t length);
445 
458  bool transform(const SkMatrix& matrix, SkRRect* dst) const;
459 
468  void dump(bool asHex) const;
469  SkString dumpToString(bool asHex) const;
470 
476  void dump() const { this->dump(false); }
477 
483  void dumpHex() const { this->dump(true); }
484 
485 private:
486  static bool AreRectAndRadiiValid(const SkRect&, const SkVector[4]);
487 
488  SkRRect(const SkRect& rect, const SkVector radii[4], int32_t type)
489  : fRect(rect)
490  , fRadii{radii[0], radii[1], radii[2], radii[3]}
491  , fType(type) {}
492 
497  bool initializeRect(const SkRect&);
498 
499  void computeType();
500  bool checkCornerContainment(SkScalar x, SkScalar y) const;
501  // Returns true if the radii had to be scaled to fit rect
502  bool scaleRadii();
503 
504  SkRect fRect = SkRect::MakeEmpty();
505  // Radii order is UL, UR, LR, LL. Use Corner enum to index into fRadii[]
506  SkVector fRadii[4] = {{0, 0}, {0, 0}, {0,0}, {0,0}};
507  // use an explicitly sized type so we're sure the class is dense (no uninitialized bytes)
508  int32_t fType = kEmpty_Type;
509  // TODO: add padding so we can use memcpy for flattening and not copy uninitialized data
510 
511  // to access fRadii directly
512  friend class SkPath;
513  friend class SkRRectPriv;
514 };
515 
516 #endif
float SkScalar
Definition: SkScalar.h:14
static bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n)
Definition: SkScalar.h:163
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
SkPath contain geometry.
Definition: SkPath.h:58
SkRRect describes a rounded rectangle with a bounds and a pair of radii for each corner.
Definition: SkRRect.h:35
void dump() const
Writes text representation of SkRRect to standard output.
Definition: SkRRect.h:476
SkVector getSimpleRadii() const
Returns top-left corner radii.
Definition: SkRRect.h:111
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:360
bool isOval() const
Definition: SkRRect.h:85
Type getType() const
Definition: SkRRect.h:76
void dumpHex() const
Writes text representation of SkRRect to standard output.
Definition: SkRRect.h:483
SkVector radii(Corner corner) const
Returns scalar pair for radius of curve on x-axis and y-axis for one corner.
Definition: SkRRect.h:271
Type
Definition: SkRRect.h:66
@ kOval_Type
non-zero width and height filled with radii
Definition: SkRRect.h:69
@ kSimple_Type
non-zero width and height with equal radii
Definition: SkRRect.h:70
@ kEmpty_Type
zero width or height
Definition: SkRRect.h:67
@ kNinePatch_Type
non-zero width and height with axis-aligned radii
Definition: SkRRect.h:71
@ kRect_Type
non-zero width and height, and zeroed radii
Definition: SkRRect.h:68
@ kComplex_Type
non-zero width and height with arbitrary radii
Definition: SkRRect.h:72
SkRRect()=default
Initializes bounds at (0, 0), the origin, with zero width and height.
SkRRect & operator=(const SkRRect &rrect)=default
Copies rrect bounds and corner radii.
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:290
SkString dumpToString(bool asHex) const
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:162
const SkRect & getBounds() const
Returns bounds.
Definition: SkRRect.h:279
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.
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:303
SkRRect(const SkRRect &rrect)=default
Initializes to copy of rrect bounds and corner radii.
void setEmpty()
Sets bounds to zero width and height at (0, 0), the origin.
Definition: SkRRect.h:118
size_t readFromMemory(const void *buffer, size_t length)
Reads SkRRect from buffer, reading kSizeInMemory bytes.
Corner
Definition: SkRRect.h:251
@ kUpperLeft_Corner
index of top-left corner radii
Definition: SkRRect.h:252
@ kLowerRight_Corner
index of bottom-right corner radii
Definition: SkRRect.h:254
@ kUpperRight_Corner
index of top-right corner radii
Definition: SkRRect.h:253
@ kLowerLeft_Corner
index of bottom-left corner radii
Definition: SkRRect.h:255
const SkRect & rect() const
Returns bounds.
Definition: SkRRect.h:264
bool transform(const SkMatrix &matrix, SkRRect *dst) const
Transforms by SkRRect by matrix, storing result in dst.
static SkRRect MakeRect(const SkRect &r)
Initializes to copy of r bounds and zeroes corner radii.
Definition: SkRRect.h:149
bool isNinePatch() const
Definition: SkRRect.h:87
size_t writeToMemory(void *buffer) const
Writes SkRRect to buffer.
void dump(bool asHex) const
Writes text representation of SkRRect to standard output.
void setOval(const SkRect &oval)
Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii to half oval....
SkScalar width() const
Returns span on the x-axis.
Definition: SkRRect.h:95
void offset(SkScalar dx, SkScalar dy)
Translates SkRRect by (dx, dy).
Definition: SkRRect.h:387
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:180
void outset(SkScalar dx, SkScalar dy)
Outsets bounds by dx and dy, and adjusts radii by dx and dy.
Definition: SkRRect.h:378
bool isRect() const
Definition: SkRRect.h:84
void setRectRadii(const SkRect &rect, const SkVector radii[4])
Sets bounds to rect.
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.
Type type() const
Definition: SkRRect.h:81
bool isEmpty() const
Definition: SkRRect.h:83
SkRRect makeOffset(SkScalar dx, SkScalar dy) const
Returns SkRRect translated by (dx, dy).
Definition: SkRRect.h:397
void setRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Sets to rounded rectangle with the same radii for all four corners.
SkScalar height() const
Returns span on the y-axis.
Definition: SkRRect.h:102
void setNinePatch(const SkRect &rect, SkScalar leftRad, SkScalar topRad, SkScalar rightRad, SkScalar bottomRad)
Sets bounds to rect.
void inset(SkScalar dx, SkScalar dy)
Insets bounds by dx and dy, and adjusts radii by dx and dy.
Definition: SkRRect.h:341
bool isSimple() const
Definition: SkRRect.h:86
bool isComplex() const
Definition: SkRRect.h:88
bool isValid() const
Returns true if bounds and radii values are finite and describe a SkRRect SkRRect::Type that matches ...
void setRect(const SkRect &rect)
Sets bounds to sorted rect, and sets corner radii to zero.
Definition: SkRRect.h:126
static SkRRect MakeEmpty()
Initializes bounds at (0, 0), the origin, with zero width and height.
Definition: SkRRect.h:142
Light weight class for managing strings.
Definition: SkString.h:118
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
static constexpr SkRect MakeEmpty()
Returns constructed SkRect set to (0, 0, 0, 0).
Definition: SkRect.h:595