SkRRect.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. /* Generated by tools/bookmaker from include/core/SkRRect.h and docs/SkRRect_Reference.bmh
  8. on 2018-08-10 12:59:44. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkRRect_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkRRect_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkRRect.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkRRect_DEFINED
  16. #define SkRRect_DEFINED
  17. #include "SkRect.h"
  18. #include "SkPoint.h"
  19. class SkPath;
  20. class SkMatrix;
  21. /** \class SkRRect
  22. SkRRect describes a rounded rectangle with a bounds and a pair of radii for each corner.
  23. The bounds and radii can be set so that SkRRect describes: a rectangle with sharp corners;
  24. a circle; an oval; or a rectangle with one or more rounded corners.
  25. SkRRect allows implementing CSS properties that describe rounded corners.
  26. SkRRect may have up to eight different radii, one for each axis on each of its four
  27. corners.
  28. SkRRect may modify the provided parameters when initializing bounds and radii.
  29. If either axis radii is zero or less: radii are stored as zero; corner is square.
  30. If corner curves overlap, radii are proportionally reduced to fit within bounds.
  31. */
  32. class SK_API SkRRect {
  33. public:
  34. /** Initializes bounds at (0, 0), the origin, with zero width and height.
  35. Initializes corner radii to (0, 0), and sets type of kEmpty_Type.
  36. @return empty SkRRect
  37. */
  38. SkRRect() = default;
  39. /** Initializes to copy of rrect bounds and corner radii.
  40. @param rrect bounds and corner to copy
  41. @return copy of rrect
  42. */
  43. SkRRect(const SkRRect& rrect) = default;
  44. /** Copies rrect bounds and corner radii.
  45. @param rrect bounds and corner to copy
  46. @return copy of rrect
  47. */
  48. SkRRect& operator=(const SkRRect& rrect) = default;
  49. /** \enum SkRRect::Type
  50. Type describes possible specializations of SkRRect. Each Type is
  51. exclusive; a SkRRect may only have one type.
  52. Type members become progressively less restrictive; larger values of
  53. Type have more degrees of freedom than smaller values.
  54. */
  55. enum Type {
  56. kEmpty_Type, //!< zero width or height
  57. kRect_Type, //!< non-zero width and height, and zeroed radii
  58. kOval_Type, //!< non-zero width and height filled with radii
  59. kSimple_Type, //!< non-zero width and height with equal radii
  60. kNinePatch_Type, //!< non-zero width and height with axis-aligned radii
  61. kComplex_Type, //!< non-zero width and height with arbitrary radii
  62. kLastType = kComplex_Type, //!< largest Type value
  63. };
  64. /** Returns SkRRect::Type, one of:
  65. kEmpty_Type, kRect_Type, kOval_Type, kSimple_Type, kNinePatch_Type,
  66. kComplex_Type.
  67. @return SkRRect::Type
  68. */
  69. Type getType() const {
  70. SkASSERT(this->isValid());
  71. return static_cast<Type>(fType);
  72. }
  73. /** Returns SkRRect::Type, one of:
  74. kEmpty_Type, kRect_Type, kOval_Type, kSimple_Type, kNinePatch_Type,
  75. kComplex_Type.
  76. @return SkRRect::Type
  77. */
  78. Type type() const { return this->getType(); }
  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. /** Returns span on the x-axis. This does not check if result fits in 32-bit float;
  86. result may be infinity.
  87. @return rect().fRight minus rect().fLeft
  88. */
  89. SkScalar width() const { return fRect.width(); }
  90. /** Returns span on the y-axis. This does not check if result fits in 32-bit float;
  91. result may be infinity.
  92. @return rect().fBottom minus rect().fTop
  93. */
  94. SkScalar height() const { return fRect.height(); }
  95. /** Returns top-left corner radii. If type() returns kEmpty_Type, kRect_Type,
  96. kOval_Type, or kSimple_Type, returns a value representative of all corner radii.
  97. If type() returns kNinePatch_Type or kComplex_Type, at least one of the
  98. remaining three corners has a different value.
  99. @return corner radii for simple types
  100. */
  101. SkVector getSimpleRadii() const {
  102. return fRadii[0];
  103. }
  104. /** Sets bounds to zero width and height at (0, 0), the origin. Sets
  105. corner radii to zero and sets type to kEmpty_Type.
  106. */
  107. void setEmpty() { *this = SkRRect(); }
  108. /** Sets bounds to sorted rect, and sets corner radii to zero.
  109. If set bounds has width and height, and sets type to kRect_Type;
  110. otherwise, sets type to kEmpty_Type.
  111. @param rect bounds to set
  112. */
  113. void setRect(const SkRect& rect) {
  114. if (!this->initializeRect(rect)) {
  115. return;
  116. }
  117. memset(fRadii, 0, sizeof(fRadii));
  118. fType = kRect_Type;
  119. SkASSERT(this->isValid());
  120. }
  121. /** Initializes bounds at (0, 0), the origin, with zero width and height.
  122. Initializes corner radii to (0, 0), and sets type of kEmpty_Type.
  123. @return empty SkRRect
  124. */
  125. static SkRRect MakeEmpty() { return SkRRect(); }
  126. /** Initializes to copy of r bounds and zeroes corner radii.
  127. @param r bounds to copy
  128. @return copy of r
  129. */
  130. static SkRRect MakeRect(const SkRect& r) {
  131. SkRRect rr;
  132. rr.setRect(r);
  133. return rr;
  134. }
  135. /** Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii
  136. to half oval.height(). If oval bounds is empty, sets to kEmpty_Type.
  137. Otherwise, sets to kOval_Type.
  138. @param oval bounds of oval
  139. @return oval
  140. */
  141. static SkRRect MakeOval(const SkRect& oval) {
  142. SkRRect rr;
  143. rr.setOval(oval);
  144. return rr;
  145. }
  146. /** Sets to rounded rectangle with the same radii for all four corners.
  147. If rect is empty, sets to kEmpty_Type.
  148. Otherwise, if xRad and yRad are zero, sets to kRect_Type.
  149. Otherwise, if xRad is at least half rect.width() and yRad is at least half
  150. rect.height(), sets to kOval_Type.
  151. Otherwise, sets to kSimple_Type.
  152. @param rect bounds of rounded rectangle
  153. @param xRad x-axis radius of corners
  154. @param yRad y-axis radius of corners
  155. @return rounded rectangle
  156. */
  157. static SkRRect MakeRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) {
  158. SkRRect rr;
  159. rr.setRectXY(rect, xRad, yRad);
  160. return rr;
  161. }
  162. /** Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii
  163. to half oval.height(). If oval bounds is empty, sets to kEmpty_Type.
  164. Otherwise, sets to kOval_Type.
  165. @param oval bounds of oval
  166. */
  167. void setOval(const SkRect& oval) {
  168. if (!this->initializeRect(oval)) {
  169. return;
  170. }
  171. SkScalar xRad = SkScalarHalf(fRect.width());
  172. SkScalar yRad = SkScalarHalf(fRect.height());
  173. for (int i = 0; i < 4; ++i) {
  174. fRadii[i].set(xRad, yRad);
  175. }
  176. fType = kOval_Type;
  177. SkASSERT(this->isValid());
  178. }
  179. /** Sets to rounded rectangle with the same radii for all four corners.
  180. If rect is empty, sets to kEmpty_Type.
  181. Otherwise, if xRad or yRad is zero, sets to kRect_Type.
  182. Otherwise, if xRad is at least half rect.width() and yRad is at least half
  183. rect.height(), sets to kOval_Type.
  184. Otherwise, sets to kSimple_Type.
  185. @param rect bounds of rounded rectangle
  186. @param xRad x-axis radius of corners
  187. @param yRad y-axis radius of corners
  188. */
  189. void setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad);
  190. /** Sets bounds to rect. Sets radii to (leftRad, topRad), (rightRad, topRad),
  191. (rightRad, bottomRad), (leftRad, bottomRad).
  192. If rect is empty, sets to kEmpty_Type.
  193. Otherwise, if leftRad and rightRad are zero, sets to kRect_Type.
  194. Otherwise, if topRad and bottomRad are zero, sets to kRect_Type.
  195. Otherwise, if leftRad and rightRad are equal and at least half rect.width(), and
  196. topRad and bottomRad are equal at least half rect.height(), sets to kOval_Type.
  197. Otherwise, if leftRad and rightRad are equal, and topRad and bottomRad are equal,
  198. sets to kSimple_Type. Otherwise, sets to kNinePatch_Type.
  199. Nine patch refers to the nine parts defined by the radii: one center rectangle,
  200. four edge patches, and four corner patches.
  201. @param rect bounds of rounded rectangle
  202. @param leftRad left-top and left-bottom x-axis radius
  203. @param topRad left-top and right-top y-axis radius
  204. @param rightRad right-top and right-bottom x-axis radius
  205. @param bottomRad left-bottom and right-bottom y-axis radius
  206. */
  207. void setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
  208. SkScalar rightRad, SkScalar bottomRad);
  209. /** Sets bounds to rect. Sets radii array for individual control of all for corners.
  210. If rect is empty, sets to kEmpty_Type.
  211. Otherwise, if one of each corner radii are zero, sets to kRect_Type.
  212. Otherwise, if all x-axis radii are equal and at least half rect.width(), and
  213. all y-axis radii are equal at least half rect.height(), sets to kOval_Type.
  214. Otherwise, if all x-axis radii are equal, and all y-axis radii are equal,
  215. sets to kSimple_Type. Otherwise, sets to kNinePatch_Type.
  216. @param rect bounds of rounded rectangle
  217. @param radii corner x-axis and y-axis radii
  218. */
  219. void setRectRadii(const SkRect& rect, const SkVector radii[4]);
  220. /** \enum SkRRect::Corner
  221. The radii are stored: top-left, top-right, bottom-right, bottom-left.
  222. */
  223. enum Corner {
  224. kUpperLeft_Corner, //!< index of top-left corner radii
  225. kUpperRight_Corner, //!< index of top-right corner radii
  226. kLowerRight_Corner, //!< index of bottom-right corner radii
  227. kLowerLeft_Corner, //!< index of bottom-left corner radii
  228. };
  229. /** Returns bounds. Bounds may have zero width or zero height. Bounds right is
  230. greater than or equal to left; bounds bottom is greater than or equal to top.
  231. Result is identical to getBounds().
  232. @return bounding box
  233. */
  234. const SkRect& rect() const { return fRect; }
  235. /** Returns scalar pair for radius of curve on x-axis and y-axis for one corner.
  236. Both radii may be zero. If not zero, both are positive and finite.
  237. @param corner one of: kUpperLeft_Corner, kUpperRight_Corner,
  238. kLowerRight_Corner, kLowerLeft_Corner
  239. @return x-axis and y-axis radii for one corner
  240. */
  241. SkVector radii(Corner corner) const { return fRadii[corner]; }
  242. /** Returns bounds. Bounds may have zero width or zero height. Bounds right is
  243. greater than or equal to left; bounds bottom is greater than or equal to top.
  244. Result is identical to rect().
  245. @return bounding box
  246. */
  247. const SkRect& getBounds() const { return fRect; }
  248. /** Returns true if bounds and radii in a are equal to bounds and radii in b.
  249. a and b are not equal if either contain NaN. a and b are equal if members
  250. contain zeroes with different signs.
  251. @param a SkRect bounds and radii to compare
  252. @param b SkRect bounds and radii to compare
  253. @return true if members are equal
  254. */
  255. friend bool operator==(const SkRRect& a, const SkRRect& b) {
  256. return a.fRect == b.fRect && SkScalarsEqual(&a.fRadii[0].fX, &b.fRadii[0].fX, 8);
  257. }
  258. /** Returns true if bounds and radii in a are not equal to bounds and radii in b.
  259. a and b are not equal if either contain NaN. a and b are equal if members
  260. contain zeroes with different signs.
  261. @param a SkRect bounds and radii to compare
  262. @param b SkRect bounds and radii to compare
  263. @return true if members are not equal
  264. */
  265. friend bool operator!=(const SkRRect& a, const SkRRect& b) {
  266. return a.fRect != b.fRect || !SkScalarsEqual(&a.fRadii[0].fX, &b.fRadii[0].fX, 8);
  267. }
  268. /** Copies SkRRect to dst, then insets dst bounds by dx and dy, and adjusts dst
  269. radii by dx and dy. dx and dy may be positive, negative, or zero. dst may be
  270. SkRRect.
  271. If either corner radius is zero, the corner has no curvature and is unchanged.
  272. Otherwise, if adjusted radius becomes negative, pins radius to zero.
  273. If dx exceeds half dst bounds width, dst bounds left and right are set to
  274. bounds x-axis center. If dy exceeds half dst bounds height, dst bounds top and
  275. bottom are set to bounds y-axis center.
  276. If dx or dy cause the bounds to become infinite, dst bounds is zeroed.
  277. @param dx added to rect().fLeft, and subtracted from rect().fRight
  278. @param dy added to rect().fTop, and subtracted from rect().fBottom
  279. @param dst insets bounds and radii
  280. */
  281. void inset(SkScalar dx, SkScalar dy, SkRRect* dst) const;
  282. /** Insets bounds by dx and dy, and adjusts radii by dx and dy. dx and dy may be
  283. positive, negative, or zero.
  284. If either corner radius is zero, the corner has no curvature and is unchanged.
  285. Otherwise, if adjusted radius becomes negative, pins radius to zero.
  286. If dx exceeds half bounds width, bounds left and right are set to
  287. bounds x-axis center. If dy exceeds half bounds height, bounds top and
  288. bottom are set to bounds y-axis center.
  289. If dx or dy cause the bounds to become infinite, bounds is zeroed.
  290. @param dx added to rect().fLeft, and subtracted from rect().fRight
  291. @param dy added to rect().fTop, and subtracted from rect().fBottom
  292. */
  293. void inset(SkScalar dx, SkScalar dy) {
  294. this->inset(dx, dy, this);
  295. }
  296. /** Outsets dst bounds by dx and dy, and adjusts radii by dx and dy. dx and dy may be
  297. positive, negative, or zero.
  298. If either corner radius is zero, the corner has no curvature and is unchanged.
  299. Otherwise, if adjusted radius becomes negative, pins radius to zero.
  300. If dx exceeds half dst bounds width, dst bounds left and right are set to
  301. bounds x-axis center. If dy exceeds half dst bounds height, dst bounds top and
  302. bottom are set to bounds y-axis center.
  303. If dx or dy cause the bounds to become infinite, dst bounds is zeroed.
  304. @param dx subtracted from rect().fLeft, and added to rect().fRight
  305. @param dy subtracted from rect().fTop, and added to rect().fBottom
  306. @param dst outset bounds and radii
  307. */
  308. void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
  309. this->inset(-dx, -dy, dst);
  310. }
  311. /** Outsets bounds by dx and dy, and adjusts radii by dx and dy. dx and dy may be
  312. positive, negative, or zero.
  313. If either corner radius is zero, the corner has no curvature and is unchanged.
  314. Otherwise, if adjusted radius becomes negative, pins radius to zero.
  315. If dx exceeds half bounds width, bounds left and right are set to
  316. bounds x-axis center. If dy exceeds half bounds height, bounds top and
  317. bottom are set to bounds y-axis center.
  318. If dx or dy cause the bounds to become infinite, bounds is zeroed.
  319. @param dx subtracted from rect().fLeft, and added to rect().fRight
  320. @param dy subtracted from rect().fTop, and added to rect().fBottom
  321. */
  322. void outset(SkScalar dx, SkScalar dy) {
  323. this->inset(-dx, -dy, this);
  324. }
  325. /** Translates SkRRect by (dx, dy).
  326. @param dx offset added to rect().fLeft and rect().fRight
  327. @param dy offset added to rect().fTop and rect().fBottom
  328. */
  329. void offset(SkScalar dx, SkScalar dy) {
  330. fRect.offset(dx, dy);
  331. }
  332. /** Returns SkRRect translated by (dx, dy).
  333. @param dx offset added to rect().fLeft and rect().fRight
  334. @param dy offset added to rect().fTop and rect().fBottom
  335. @return SkRRect bounds offset by (dx, dy), with unchanged corner radii
  336. */
  337. SkRRect SK_WARN_UNUSED_RESULT makeOffset(SkScalar dx, SkScalar dy) const {
  338. return SkRRect(fRect.makeOffset(dx, dy), fRadii, fType);
  339. }
  340. /** Returns true if rect is inside the bounds and corner radii, and if
  341. SkRRect and rect are not empty.
  342. @param rect area tested for containment
  343. @return true if SkRRect contains rect
  344. */
  345. bool contains(const SkRect& rect) const;
  346. /** Returns true if bounds and radii values are finite and describe a SkRRect
  347. SkRRect::Type that matches getType(). All SkRRect methods construct valid types,
  348. even if the input values are not valid. Invalid SkRRect data can only
  349. be generated by corrupting memory.
  350. @return true if bounds and radii match type()
  351. */
  352. bool isValid() const;
  353. static constexpr size_t kSizeInMemory = 12 * sizeof(SkScalar);
  354. /** Writes SkRRect to buffer. Writes kSizeInMemory bytes, and returns
  355. kSizeInMemory, the number of bytes written.
  356. @param buffer storage for SkRRect
  357. @return bytes written, kSizeInMemory
  358. */
  359. size_t writeToMemory(void* buffer) const;
  360. /** Reads SkRRect from buffer, reading kSizeInMemory bytes.
  361. Returns kSizeInMemory, bytes read if length is at least kSizeInMemory.
  362. Otherwise, returns zero.
  363. @param buffer memory to read from
  364. @param length size of buffer
  365. @return bytes read, or 0 if length is less than kSizeInMemory
  366. */
  367. size_t readFromMemory(const void* buffer, size_t length);
  368. /** Transforms by SkRRect by matrix, storing result in dst.
  369. Returns true if SkRRect transformed can be represented by another SkRRect.
  370. Returns false if matrix contains transformations other than scale and translate.
  371. Asserts in debug builds if SkRRect equals dst.
  372. @param matrix SkMatrix specifying the transform
  373. @param dst SkRRect to store the result
  374. @return true if transformation succeeded.
  375. */
  376. bool transform(const SkMatrix& matrix, SkRRect* dst) const;
  377. /** Writes text representation of SkRRect to standard output.
  378. Set asHex true to generate exact binary representations
  379. of floating point numbers.
  380. @param asHex true if SkScalar values are written as hexadecimal
  381. */
  382. void dump(bool asHex) const;
  383. /** Writes text representation of SkRRect to standard output. The representation
  384. may be directly compiled as C++ code. Floating point values are written
  385. with limited precision; it may not be possible to reconstruct original
  386. SkRRect from output.
  387. */
  388. void dump() const { this->dump(false); }
  389. /** Writes text representation of SkRRect to standard output. The representation
  390. may be directly compiled as C++ code. Floating point values are written
  391. in hexadecimal to preserve their exact bit pattern. The output reconstructs the
  392. original SkRRect.
  393. */
  394. void dumpHex() const { this->dump(true); }
  395. private:
  396. static bool AreRectAndRadiiValid(const SkRect&, const SkVector[4]);
  397. SkRRect(const SkRect& rect, const SkVector radii[4], int32_t type)
  398. : fRect(rect)
  399. , fRadii{radii[0], radii[1], radii[2], radii[3]}
  400. , fType(type) {}
  401. /**
  402. * Initializes fRect. If the passed in rect is not finite or empty the rrect will be fully
  403. * initialized and false is returned. Otherwise, just fRect is initialized and true is returned.
  404. */
  405. bool initializeRect(const SkRect&);
  406. void computeType();
  407. bool checkCornerContainment(SkScalar x, SkScalar y) const;
  408. void scaleRadii(const SkRect& rect);
  409. SkRect fRect = SkRect::MakeEmpty();
  410. // Radii order is UL, UR, LR, LL. Use Corner enum to index into fRadii[]
  411. SkVector fRadii[4] = {{0, 0}, {0, 0}, {0,0}, {0,0}};
  412. // use an explicitly sized type so we're sure the class is dense (no uninitialized bytes)
  413. int32_t fType = kEmpty_Type;
  414. // TODO: add padding so we can use memcpy for flattening and not copy uninitialized data
  415. // to access fRadii directly
  416. friend class SkPath;
  417. friend class SkRRectPriv;
  418. };
  419. #endif