Debian Codegraph @

include/core/SkRRect.h

raw
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/SkSpan.h"
15 #include "include/core/SkTypes.h"
16 #include "include/private/base/SkMacros.h"
17
18 #include <cstdint>
19 #include <cstring>
20 #include <optional>
21
22 class SkMatrix;
23 class SkString;
24
25 /** \class SkRRect
26 SkRRect describes a rounded rectangle with a bounds and a pair of radii for each corner.
27 The bounds and radii can be set so that SkRRect describes: a rectangle with sharp corners;
28 a circle; an oval; or a rectangle with one or more rounded corners.
29
30 SkRRect allows implementing CSS properties that describe rounded corners.
31 SkRRect may have up to eight different radii, one for each axis on each of its four
32 corners.
33
34 SkRRect may modify the provided parameters when initializing bounds and radii.
35 If either axis radii is zero or less: radii are stored as zero; corner is square.
36 If corner curves overlap, radii are proportionally reduced to fit within bounds.
37 */
38 SK_BEGIN_REQUIRE_DENSE
39 class SK_API SkRRect {
40 public:
41
42 /** Initializes bounds at (0, 0), the origin, with zero width and height.
43 Initializes corner radii to (0, 0), and sets type of kEmpty_Type.
44
45 @return empty SkRRect
46 */
47 SkRRect() = default;
48
49 /** Initializes to copy of rrect bounds and corner radii.
50
51 @param rrect bounds and corner to copy
52 @return copy of rrect
53 */
54 SkRRect(const SkRRect& rrect) = default;
55
56 /** Copies rrect bounds and corner radii.
57
58 @param rrect bounds and corner to copy
59 @return copy of rrect
60 */
61 SkRRect& operator=(const SkRRect& rrect) = default;
62
63 /** \enum SkRRect::Type
64 Type describes possible specializations of SkRRect. Each Type is
65 exclusive; a SkRRect may only have one type.
66
67 Type members become progressively less restrictive; larger values of
68 Type have more degrees of freedom than smaller values.
69 */
70 enum Type {
71 kEmpty_Type, //!< zero width or height
72 kRect_Type, //!< non-zero width and height, and zeroed radii
73 kOval_Type, //!< non-zero width and height filled with radii
74 kSimple_Type, //!< non-zero width and height with equal radii
75 kNinePatch_Type, //!< non-zero width and height with axis-aligned radii
76 kComplex_Type, //!< non-zero width and height with arbitrary radii
77 kLastType = kComplex_Type, //!< largest Type value
78 };
79
80 Type getType() const {
81 SkASSERT(this->isValid());
82 return static_cast<Type>(fType);
83 }
84
85 Type type() const { return this->getType(); }
86
87 inline bool isEmpty() const { return kEmpty_Type == this->getType(); }
88 inline bool isRect() const { return kRect_Type == this->getType(); }
89 inline bool isOval() const { return kOval_Type == this->getType(); }
90 inline bool isSimple() const { return kSimple_Type == this->getType(); }
91 inline bool isNinePatch() const { return kNinePatch_Type == this->getType(); }
92 inline bool isComplex() const { return kComplex_Type == this->getType(); }
93
94 /** Returns span on the x-axis. This does not check if result fits in 32-bit float;
95 result may be infinity.
96
97 @return rect().fRight minus rect().fLeft
98 */
99 SkScalar width() const { return fRect.width(); }
100
101 /** Returns span on the y-axis. This does not check if result fits in 32-bit float;
102 result may be infinity.
103
104 @return rect().fBottom minus rect().fTop
105 */
106 SkScalar height() const { return fRect.height(); }
107
108 /** Returns top-left corner radii. If type() returns kEmpty_Type, kRect_Type,
109 kOval_Type, or kSimple_Type, returns a value representative of all corner radii.
110 If type() returns kNinePatch_Type or kComplex_Type, at least one of the
111 remaining three corners has a different value.
112
113 @return corner radii for simple types
114 */
115 SkVector getSimpleRadii() const {
116 return fRadii[0];
117 }
118
119 /** Sets bounds to zero width and height at (0, 0), the origin. Sets
120 corner radii to zero and sets type to kEmpty_Type.
121 */
122 void setEmpty() { *this = SkRRect(); }
123
124 /** Sets bounds to sorted rect, and sets corner radii to zero.
125 If set bounds has width and height, and sets type to kRect_Type;
126 otherwise, sets type to kEmpty_Type.
127
128 @param rect bounds to set
129 */
130 void setRect(const SkRect& rect) {
131 if (!this->initializeRect(rect)) {
132 return;
133 }
134
135 memset(fRadii, 0, sizeof(fRadii));
136 fType = kRect_Type;
137
138 SkASSERT(this->isValid());
139 }
140
141 /** Initializes bounds at (0, 0), the origin, with zero width and height.
142 Initializes corner radii to (0, 0), and sets type of kEmpty_Type.
143
144 @return empty SkRRect
145 */
146 static SkRRect MakeEmpty() { return SkRRect(); }
147
148 /** Initializes to copy of r bounds and zeroes corner radii.
149
150 @param r bounds to copy
151 @return copy of r
152 */
153 static SkRRect MakeRect(const SkRect& r) {
154 SkRRect rr;
155 rr.setRect(r);
156 return rr;
157 }
158
159 /** Initializes to oval, x-axis radii to half oval.width(), and all y-axis radii
160 to half oval.height(). If oval bounds is empty, sets to kEmpty_Type.
161 Otherwise, sets to kOval_Type.
162
163 @param oval bounds of oval
164 @return oval
165 */
166 static SkRRect MakeOval(const SkRect& oval) {
167 SkRRect rr;
168 rr.setOval(oval);
169 return rr;
170 }
171
172 /** Initializes to rounded rectangle with the same radii for all four corners.
173 If rect is empty, sets to kEmpty_Type.
174 Otherwise, if xRad and yRad are zero, sets to kRect_Type.
175 Otherwise, if xRad is at least half rect.width() and yRad is at least half
176 rect.height(), sets to kOval_Type.
177 Otherwise, sets to kSimple_Type.
178
179 @param rect bounds of rounded rectangle
180 @param xRad x-axis radius of corners
181 @param yRad y-axis radius of corners
182 @return rounded rectangle
183 */
184 static SkRRect MakeRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) {
185 SkRRect rr;
186 rr.setRectXY(rect, xRad, yRad);
187 return rr;
188 }
189
190 /** Initializes to rounded rectangle with a radii array for individual control
191 of all four corners.
192
193 If rect is empty, sets to kEmpty_Type.
194 Otherwise, if one of each corner radii are zero, sets to kRect_Type.
195 Otherwise, if all x-axis radii are equal and at least half rect.width(), and
196 all y-axis radii are equal at least half rect.height(), sets to kOval_Type.
197 Otherwise, if all x-axis radii are equal, and all y-axis radii are equal,
198 sets to kSimple_Type. Otherwise, sets to kNinePatch_Type.
199
200 @param rect bounds of rounded rectangle
201 @param radii corner x-axis and y-axis radii
202
203 example: https://fiddle.skia.org/c/@RRect_setRectRadii
204 */
205 static SkRRect MakeRectRadii(const SkRect& rect, const SkVector radii[4]) {
206 SkRRect rr;
207 rr.setRectRadii(rect, radii);
208 return rr;
209 }
210
211 /** Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii
212 to half oval.height(). If oval bounds is empty, sets to kEmpty_Type.
213 Otherwise, sets to kOval_Type.
214
215 @param oval bounds of oval
216 */
217 void setOval(const SkRect& oval);
218
219 /** Sets to rounded rectangle with the same radii for all four corners.
220 If rect is empty, sets to kEmpty_Type.
221 Otherwise, if xRad or yRad is zero, sets to kRect_Type.
222 Otherwise, if xRad is at least half rect.width() and yRad is at least half
223 rect.height(), sets to kOval_Type.
224 Otherwise, sets to kSimple_Type.
225
226 @param rect bounds of rounded rectangle
227 @param xRad x-axis radius of corners
228 @param yRad y-axis radius of corners
229
230 example: https://fiddle.skia.org/c/@RRect_setRectXY
231 */
232 void setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad);
233
234 /** Sets bounds to rect. Sets radii to (leftRad, topRad), (rightRad, topRad),
235 (rightRad, bottomRad), (leftRad, bottomRad).
236
237 If rect is empty, sets to kEmpty_Type.
238 Otherwise, if leftRad and rightRad are zero, sets to kRect_Type.
239 Otherwise, if topRad and bottomRad are zero, sets to kRect_Type.
240 Otherwise, if leftRad and rightRad are equal and at least half rect.width(), and
241 topRad and bottomRad are equal at least half rect.height(), sets to kOval_Type.
242 Otherwise, if leftRad and rightRad are equal, and topRad and bottomRad are equal,
243 sets to kSimple_Type. Otherwise, sets to kNinePatch_Type.
244
245 Nine patch refers to the nine parts defined by the radii: one center rectangle,
246 four edge patches, and four corner patches.
247
248 @param rect bounds of rounded rectangle
249 @param leftRad left-top and left-bottom x-axis radius
250 @param topRad left-top and right-top y-axis radius
251 @param rightRad right-top and right-bottom x-axis radius
252 @param bottomRad left-bottom and right-bottom y-axis radius
253 */
254 void setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
255 SkScalar rightRad, SkScalar bottomRad);
256
257 /** Sets bounds to rect. Sets radii array for individual control of all four corners.
258
259 If rect is empty, sets to kEmpty_Type.
260 Otherwise, if one of each corner radii are zero, sets to kRect_Type.
261 Otherwise, if all x-axis radii are equal and at least half rect.width(), and
262 all y-axis radii are equal at least half rect.height(), sets to kOval_Type.
263 Otherwise, if all x-axis radii are equal, and all y-axis radii are equal,
264 sets to kSimple_Type. Otherwise, sets to kNinePatch_Type.
265
266 @param rect bounds of rounded rectangle
267 @param radii corner x-axis and y-axis radii
268
269 example: https://fiddle.skia.org/c/@RRect_setRectRadii
270 */
271 void setRectRadii(const SkRect& rect, const SkVector radii[4]);
272
273 /** \enum SkRRect::Corner
274 The radii are stored: top-left, top-right, bottom-right, bottom-left.
275 */
276 enum Corner {
277 kUpperLeft_Corner, //!< index of top-left corner radii
278 kUpperRight_Corner, //!< index of top-right corner radii
279 kLowerRight_Corner, //!< index of bottom-right corner radii
280 kLowerLeft_Corner, //!< index of bottom-left corner radii
281 };
282
283 /** Returns bounds. Bounds may have zero width or zero height. Bounds right is
284 greater than or equal to left; bounds bottom is greater than or equal to top.
285 Result is identical to getBounds().
286
287 @return bounding box
288 */
289 const SkRect& rect() const { return fRect; }
290
291 /** Returns scalar pair for radius of curve on x-axis and y-axis for one corner.
292 Both radii may be zero. If not zero, both are positive and finite.
293
294 @return x-axis and y-axis radii for one corner
295 */
296 SkVector radii(Corner corner) const { return fRadii[corner]; }
297 /**
298 * Returns the corner radii for all four corners, in the same order as `Corner`.
299 */
300 SkSpan<const SkVector> radii() const { return SkSpan(fRadii, 4); }
301
302 /** Returns bounds. Bounds may have zero width or zero height. Bounds right is
303 greater than or equal to left; bounds bottom is greater than or equal to top.
304 Result is identical to rect().
305
306 @return bounding box
307 */
308 const SkRect& getBounds() const { return fRect; }
309
310 /** Returns true if bounds and radii in a are equal to bounds and radii in b.
311
312 a and b are not equal if either contain NaN. a and b are equal if members
313 contain zeroes with different signs.
314
315 @param a SkRect bounds and radii to compare
316 @param b SkRect bounds and radii to compare
317 @return true if members are equal
318 */
319 friend bool operator==(const SkRRect& a, const SkRRect& b) {
320 return a.fRect == b.fRect && SkScalarsEqual(&a.fRadii[0].fX, &b.fRadii[0].fX, 8);
321 }
322
323 /** Returns true if bounds and radii in a are not equal to bounds and radii in b.
324
325 a and b are not equal if either contain NaN. a and b are equal if members
326 contain zeroes with different signs.
327
328 @param a SkRect bounds and radii to compare
329 @param b SkRect bounds and radii to compare
330 @return true if members are not equal
331 */
332 friend bool operator!=(const SkRRect& a, const SkRRect& b) {
333 return a.fRect != b.fRect || !SkScalarsEqual(&a.fRadii[0].fX, &b.fRadii[0].fX, 8);
334 }
335
336 /** Copies SkRRect to dst, then insets dst bounds by dx and dy, and adjusts dst
337 radii by dx and dy. dx and dy may be positive, negative, or zero. dst may be
338 SkRRect.
339
340 If either corner radius is zero, the corner has no curvature and is unchanged.
341 Otherwise, if adjusted radius becomes negative, pins radius to zero.
342 If dx exceeds half dst bounds width, dst bounds left and right are set to
343 bounds x-axis center. If dy exceeds half dst bounds height, dst bounds top and
344 bottom are set to bounds y-axis center.
345
346 If dx or dy cause the bounds to become infinite, dst bounds is zeroed.
347
348 @param dx added to rect().fLeft, and subtracted from rect().fRight
349 @param dy added to rect().fTop, and subtracted from rect().fBottom
350 @param dst insets bounds and radii
351
352 example: https://fiddle.skia.org/c/@RRect_inset
353 */
354 void inset(SkScalar dx, SkScalar dy, SkRRect* dst) const;
355
356 /** Insets bounds by dx and dy, and adjusts radii by dx and dy. dx and dy may be
357 positive, negative, or zero.
358
359 If either corner radius is zero, the corner has no curvature and is unchanged.
360 Otherwise, if adjusted radius becomes negative, pins radius to zero.
361 If dx exceeds half bounds width, bounds left and right are set to
362 bounds x-axis center. If dy exceeds half bounds height, bounds top and
363 bottom are set to bounds y-axis center.
364
365 If dx or dy cause the bounds to become infinite, bounds is zeroed.
366
367 @param dx added to rect().fLeft, and subtracted from rect().fRight
368 @param dy added to rect().fTop, and subtracted from rect().fBottom
369 */
370 void inset(SkScalar dx, SkScalar dy) {
371 this->inset(dx, dy, this);
372 }
373
374 /** Outsets dst bounds by dx and dy, and adjusts radii by dx and dy. dx and dy may be
375 positive, negative, or zero.
376
377 If either corner radius is zero, the corner has no curvature and is unchanged.
378 Otherwise, if adjusted radius becomes negative, pins radius to zero.
379 If dx exceeds half dst bounds width, dst bounds left and right are set to
380 bounds x-axis center. If dy exceeds half dst bounds height, dst bounds top and
381 bottom are set to bounds y-axis center.
382
383 If dx or dy cause the bounds to become infinite, dst bounds is zeroed.
384
385 @param dx subtracted from rect().fLeft, and added to rect().fRight
386 @param dy subtracted from rect().fTop, and added to rect().fBottom
387 @param dst outset bounds and radii
388 */
389 void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
390 this->inset(-dx, -dy, dst);
391 }
392
393 /** Outsets bounds by dx and dy, and adjusts radii by dx and dy. dx and dy may be
394 positive, negative, or zero.
395
396 If either corner radius is zero, the corner has no curvature and is unchanged.
397 Otherwise, if adjusted radius becomes negative, pins radius to zero.
398 If dx exceeds half bounds width, bounds left and right are set to
399 bounds x-axis center. If dy exceeds half bounds height, bounds top and
400 bottom are set to bounds y-axis center.
401
402 If dx or dy cause the bounds to become infinite, bounds is zeroed.
403
404 @param dx subtracted from rect().fLeft, and added to rect().fRight
405 @param dy subtracted from rect().fTop, and added to rect().fBottom
406 */
407 void outset(SkScalar dx, SkScalar dy) {
408 this->inset(-dx, -dy, this);
409 }
410
411 /** Translates SkRRect by (dx, dy).
412
413 @param dx offset added to rect().fLeft and rect().fRight
414 @param dy offset added to rect().fTop and rect().fBottom
415 */
416 void offset(SkScalar dx, SkScalar dy) {
417 fRect.offset(dx, dy);
418 }
419
420 /** Returns SkRRect translated by (dx, dy).
421
422 @param dx offset added to rect().fLeft and rect().fRight
423 @param dy offset added to rect().fTop and rect().fBottom
424 @return SkRRect bounds offset by (dx, dy), with unchanged corner radii
425 */
426 [[nodiscard]] SkRRect makeOffset(SkScalar dx, SkScalar dy) const {
427 return SkRRect(fRect.makeOffset(dx, dy), fRadii, fType);
428 }
429
430 /** Returns true if rect is inside the bounds and corner radii, and if
431 SkRRect and rect are not empty.
432
433 @param rect area tested for containment
434 @return true if SkRRect contains rect
435
436 example: https://fiddle.skia.org/c/@RRect_contains
437 */
438 bool contains(const SkRect& rect) const;
439
440 /** Returns true if bounds and radii values are finite and describe a SkRRect
441 SkRRect::Type that matches getType(). All SkRRect methods construct valid types,
442 even if the input values are not valid. Invalid SkRRect data can only
443 be generated by corrupting memory.
444
445 @return true if bounds and radii match type()
446
447 example: https://fiddle.skia.org/c/@RRect_isValid
448 */
449 bool isValid() const;
450
451 static constexpr size_t kSizeInMemory = 12 * sizeof(SkScalar);
452
453 /** Writes SkRRect to buffer. Writes kSizeInMemory bytes, and returns
454 kSizeInMemory, the number of bytes written.
455
456 @param buffer storage for SkRRect
457 @return bytes written, kSizeInMemory
458
459 example: https://fiddle.skia.org/c/@RRect_writeToMemory
460 */
461 size_t writeToMemory(void* buffer) const;
462
463 /** Reads SkRRect from buffer, reading kSizeInMemory bytes.
464 Returns kSizeInMemory, bytes read if length is at least kSizeInMemory.
465 Otherwise, returns zero.
466
467 @param buffer memory to read from
468 @param length size of buffer
469 @return bytes read, or 0 if length is less than kSizeInMemory
470
471 example: https://fiddle.skia.org/c/@RRect_readFromMemory
472 */
473 size_t readFromMemory(const void* buffer, size_t length);
474
475 /** Transforms by SkRRect by matrix and return it if possible.
476 * If the matrix does not preserve axis-alignment (e.g. rotates, skews, etc.)
477 * then this returns {}.
478 */
479 std::optional<SkRRect> transform(const SkMatrix&) const;
480
481 // Deprecated: use optional form
482 bool transform(const SkMatrix& matrix, SkRRect* dst) const;
483
484 /** Writes text representation of SkRRect to standard output.
485 Set asHex true to generate exact binary representations
486 of floating point numbers.
487
488 @param asHex true if SkScalar values are written as hexadecimal
489
490 example: https://fiddle.skia.org/c/@RRect_dump
491 */
492 void dump(bool asHex) const;
493 SkString dumpToString(bool asHex) const;
494
495 /** Writes text representation of SkRRect to standard output. The representation
496 may be directly compiled as C++ code. Floating point values are written
497 with limited precision; it may not be possible to reconstruct original
498 SkRRect from output.
499 */
500 void dump() const { this->dump(false); }
501
502 /** Writes text representation of SkRRect to standard output. The representation
503 may be directly compiled as C++ code. Floating point values are written
504 in hexadecimal to preserve their exact bit pattern. The output reconstructs the
505 original SkRRect.
506 */
507 void dumpHex() const { this->dump(true); }
508
509 private:
510 static bool AreRectAndRadiiValid(const SkRect&, const SkVector[4]);
511
512 SkRRect(const SkRect& rect, const SkVector radii[4], int32_t type)
513 : fRect(rect)
514 , fRadii{radii[0], radii[1], radii[2], radii[3]}
515 , fType(type) {}
516
517 /**
518 * Initializes fRect. If the passed in rect is not finite or empty the rrect will be fully
519 * initialized and false is returned. Otherwise, just fRect is initialized and true is returned.
520 */
521 bool initializeRect(const SkRect&);
522
523 void computeType();
524 bool checkCornerContainment(SkScalar x, SkScalar y) const;
525 // Returns true if the radii had to be scaled to fit rect
526 bool scaleRadii();
527
528 SkRect fRect = SkRect::MakeEmpty();
529 // Radii order is UL, UR, LR, LL. Use Corner enum to index into fRadii[]
530 SkVector fRadii[4] = {{0, 0}, {0, 0}, {0,0}, {0,0}};
531 // use an explicitly sized type so we're sure the class is dense (no uninitialized bytes)
532 int32_t fType = kEmpty_Type;
533
534 // to access fRadii directly
535 friend class SkPath;
536 friend class SkRRectPriv;
537 };
538 SK_END_REQUIRE_DENSE
539
540 #endif