SkRect.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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/SkRect.h and docs/SkRect_Reference.bmh
  8. on 2018-09-13 13:59:55. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkRect_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkRect_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkRect.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkRect_DEFINED
  16. #define SkRect_DEFINED
  17. #include "SkPoint.h"
  18. #include "SkSize.h"
  19. #include "../private/SkSafe32.h"
  20. #include "../private/SkTFitsIn.h"
  21. #include <utility>
  22. struct SkRect;
  23. /** \struct SkIRect
  24. SkIRect holds four 32-bit integer coordinates describing the upper and
  25. lower bounds of a rectangle. SkIRect may be created from outer bounds or
  26. from position, width, and height. SkIRect describes an area; if its right
  27. is less than or equal to its left, or if its bottom is less than or equal to
  28. its top, it is considered empty.
  29. */
  30. struct SK_API SkIRect {
  31. int32_t fLeft; //!< smaller x-axis bounds
  32. int32_t fTop; //!< smaller y-axis bounds
  33. int32_t fRight; //!< larger x-axis bounds
  34. int32_t fBottom; //!< larger y-axis bounds
  35. /** Returns constructed SkIRect set to (0, 0, 0, 0).
  36. Many other rectangles are empty; if left is equal to or greater than right,
  37. or if top is equal to or greater than bottom. Setting all members to zero
  38. is a convenience, but does not designate a special empty rectangle.
  39. @return bounds (0, 0, 0, 0)
  40. */
  41. static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeEmpty() {
  42. return SkIRect{0, 0, 0, 0};
  43. }
  44. /** Returns constructed SkIRect set to (0, 0, w, h). Does not validate input; w or h
  45. may be negative.
  46. @param w width of constructed SkIRect
  47. @param h height of constructed SkIRect
  48. @return bounds (0, 0, w, h)
  49. */
  50. static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeWH(int32_t w, int32_t h) {
  51. return SkIRect{0, 0, w, h};
  52. }
  53. /** Returns constructed SkIRect set to (0, 0, size.width(), size.height()).
  54. Does not validate input; size.width() or size.height() may be negative.
  55. @param size values for SkIRect width and height
  56. @return bounds (0, 0, size.width(), size.height())
  57. */
  58. static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeSize(const SkISize& size) {
  59. return SkIRect{0, 0, size.fWidth, size.fHeight};
  60. }
  61. /** Returns constructed SkIRect set to (l, t, r, b). Does not sort input; SkIRect may
  62. result in fLeft greater than fRight, or fTop greater than fBottom.
  63. @param l integer stored in fLeft
  64. @param t integer stored in fTop
  65. @param r integer stored in fRight
  66. @param b integer stored in fBottom
  67. @return bounds (l, t, r, b)
  68. */
  69. static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeLTRB(int32_t l, int32_t t,
  70. int32_t r, int32_t b) {
  71. return SkIRect{l, t, r, b};
  72. }
  73. /** Returns constructed SkIRect set to: (x, y, x + w, y + h).
  74. Does not validate input; w or h may be negative.
  75. @param x stored in fLeft
  76. @param y stored in fTop
  77. @param w added to x and stored in fRight
  78. @param h added to y and stored in fBottom
  79. @return bounds at (x, y) with width w and height h
  80. */
  81. static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeXYWH(int32_t x, int32_t y,
  82. int32_t w, int32_t h) {
  83. return { x, y, Sk32_sat_add(x, w), Sk32_sat_add(y, h) };
  84. }
  85. /** Returns left edge of SkIRect, if sorted.
  86. Call sort() to reverse fLeft and fRight if needed.
  87. @return fLeft
  88. */
  89. int32_t left() const { return fLeft; }
  90. /** Returns top edge of SkIRect, if sorted. Call isEmpty() to see if SkIRect may be invalid,
  91. and sort() to reverse fTop and fBottom if needed.
  92. @return fTop
  93. */
  94. int32_t top() const { return fTop; }
  95. /** Returns right edge of SkIRect, if sorted.
  96. Call sort() to reverse fLeft and fRight if needed.
  97. @return fRight
  98. */
  99. int32_t right() const { return fRight; }
  100. /** Returns bottom edge of SkIRect, if sorted. Call isEmpty() to see if SkIRect may be invalid,
  101. and sort() to reverse fTop and fBottom if needed.
  102. @return fBottom
  103. */
  104. int32_t bottom() const { return fBottom; }
  105. /** Returns left edge of SkIRect, if sorted. Call isEmpty() to see if SkIRect may be invalid,
  106. and sort() to reverse fLeft and fRight if needed.
  107. @return fLeft
  108. */
  109. int32_t x() const { return fLeft; }
  110. /** Returns top edge of SkIRect, if sorted. Call isEmpty() to see if SkIRect may be invalid,
  111. and sort() to reverse fTop and fBottom if needed.
  112. @return fTop
  113. */
  114. int32_t y() const { return fTop; }
  115. /** Returns span on the x-axis. This does not check if SkIRect is sorted, or if
  116. result fits in 32-bit signed integer; result may be negative.
  117. @return fRight minus fLeft
  118. */
  119. int32_t width() const { return Sk32_can_overflow_sub(fRight, fLeft); }
  120. /** Returns span on the y-axis. This does not check if SkIRect is sorted, or if
  121. result fits in 32-bit signed integer; result may be negative.
  122. @return fBottom minus fTop
  123. */
  124. int32_t height() const { return Sk32_can_overflow_sub(fBottom, fTop); }
  125. /** Returns spans on the x-axis and y-axis. This does not check if SkIRect is sorted,
  126. or if result fits in 32-bit signed integer; result may be negative.
  127. @return SkISize (width, height)
  128. */
  129. SkISize size() const { return SkISize::Make(this->width(), this->height()); }
  130. /** Returns span on the x-axis. This does not check if SkIRect is sorted, so the
  131. result may be negative. This is safer than calling width() since width() might
  132. overflow in its calculation.
  133. @return fRight minus fLeft cast to int64_t
  134. */
  135. int64_t width64() const { return (int64_t)fRight - (int64_t)fLeft; }
  136. /** Returns span on the y-axis. This does not check if SkIRect is sorted, so the
  137. result may be negative. This is safer than calling height() since height() might
  138. overflow in its calculation.
  139. @return fBottom minus fTop cast to int64_t
  140. */
  141. int64_t height64() const { return (int64_t)fBottom - (int64_t)fTop; }
  142. /** Returns true if fLeft is equal to or greater than fRight, or if fTop is equal
  143. to or greater than fBottom. Call sort() to reverse rectangles with negative
  144. width64() or height64().
  145. @return true if width64() or height64() are zero or negative
  146. */
  147. bool isEmpty64() const { return fRight <= fLeft || fBottom <= fTop; }
  148. /** Returns true if width() or height() are zero or negative.
  149. @return true if width() or height() are zero or negative
  150. */
  151. bool isEmpty() const {
  152. int64_t w = this->width64();
  153. int64_t h = this->height64();
  154. if (w <= 0 || h <= 0) {
  155. return true;
  156. }
  157. // Return true if either exceeds int32_t
  158. return !SkTFitsIn<int32_t>(w | h);
  159. }
  160. /** Returns true if all members in a: fLeft, fTop, fRight, and fBottom; are
  161. identical to corresponding members in b.
  162. @param a SkIRect to compare
  163. @param b SkIRect to compare
  164. @return true if members are equal
  165. */
  166. friend bool operator==(const SkIRect& a, const SkIRect& b) {
  167. return !memcmp(&a, &b, sizeof(a));
  168. }
  169. /** Returns true if any member in a: fLeft, fTop, fRight, and fBottom; is not
  170. identical to the corresponding member in b.
  171. @param a SkIRect to compare
  172. @param b SkIRect to compare
  173. @return true if members are not equal
  174. */
  175. friend bool operator!=(const SkIRect& a, const SkIRect& b) {
  176. return !(a == b);
  177. }
  178. /** Sets SkIRect to (0, 0, 0, 0).
  179. Many other rectangles are empty; if left is equal to or greater than right,
  180. or if top is equal to or greater than bottom. Setting all members to zero
  181. is a convenience, but does not designate a special empty rectangle.
  182. */
  183. void setEmpty() { memset(this, 0, sizeof(*this)); }
  184. /** Sets SkIRect to (left, top, right, bottom).
  185. left and right are not sorted; left is not necessarily less than right.
  186. top and bottom are not sorted; top is not necessarily less than bottom.
  187. @param left assigned to fLeft
  188. @param top assigned to fTop
  189. @param right assigned to fRight
  190. @param bottom assigned to fBottom
  191. */
  192. void set(int32_t left, int32_t top, int32_t right, int32_t bottom) {
  193. fLeft = left;
  194. fTop = top;
  195. fRight = right;
  196. fBottom = bottom;
  197. }
  198. /** Sets SkIRect to (left, top, right, bottom).
  199. left and right are not sorted; left is not necessarily less than right.
  200. top and bottom are not sorted; top is not necessarily less than bottom.
  201. @param left stored in fLeft
  202. @param top stored in fTop
  203. @param right stored in fRight
  204. @param bottom stored in fBottom
  205. */
  206. void setLTRB(int32_t left, int32_t top, int32_t right, int32_t bottom) {
  207. this->set(left, top, right, bottom);
  208. }
  209. /** Sets SkIRect to: (x, y, x + width, y + height).
  210. Does not validate input; width or height may be negative.
  211. @param x stored in fLeft
  212. @param y stored in fTop
  213. @param width added to x and stored in fRight
  214. @param height added to y and stored in fBottom
  215. */
  216. void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height) {
  217. fLeft = x;
  218. fTop = y;
  219. fRight = Sk32_sat_add(x, width);
  220. fBottom = Sk32_sat_add(y, height);
  221. }
  222. /** Returns SkIRect offset by (dx, dy).
  223. If dx is negative, SkIRect returned is moved to the left.
  224. If dx is positive, SkIRect returned is moved to the right.
  225. If dy is negative, SkIRect returned is moved upward.
  226. If dy is positive, SkIRect returned is moved downward.
  227. @param dx offset added to fLeft and fRight
  228. @param dy offset added to fTop and fBottom
  229. @return SkIRect offset by dx and dy, with original width and height
  230. */
  231. SkIRect makeOffset(int32_t dx, int32_t dy) const {
  232. return {
  233. Sk32_sat_add(fLeft, dx), Sk32_sat_add(fTop, dy),
  234. Sk32_sat_add(fRight, dx), Sk32_sat_add(fBottom, dy),
  235. };
  236. }
  237. /** Returns SkIRect, inset by (dx, dy).
  238. If dx is negative, SkIRect returned is wider.
  239. If dx is positive, SkIRect returned is narrower.
  240. If dy is negative, SkIRect returned is taller.
  241. If dy is positive, SkIRect returned is shorter.
  242. @param dx offset added to fLeft and subtracted from fRight
  243. @param dy offset added to fTop and subtracted from fBottom
  244. @return SkIRect inset symmetrically left and right, top and bottom
  245. */
  246. SkIRect makeInset(int32_t dx, int32_t dy) const {
  247. return {
  248. Sk32_sat_add(fLeft, dx), Sk32_sat_add(fTop, dy),
  249. Sk32_sat_sub(fRight, dx), Sk32_sat_sub(fBottom, dy),
  250. };
  251. }
  252. /** Returns SkIRect, outset by (dx, dy).
  253. If dx is negative, SkIRect returned is narrower.
  254. If dx is positive, SkIRect returned is wider.
  255. If dy is negative, SkIRect returned is shorter.
  256. If dy is positive, SkIRect returned is taller.
  257. @param dx offset subtracted to fLeft and added from fRight
  258. @param dy offset subtracted to fTop and added from fBottom
  259. @return SkIRect outset symmetrically left and right, top and bottom
  260. */
  261. SkIRect makeOutset(int32_t dx, int32_t dy) const {
  262. return {
  263. Sk32_sat_sub(fLeft, dx), Sk32_sat_sub(fTop, dy),
  264. Sk32_sat_add(fRight, dx), Sk32_sat_add(fBottom, dy),
  265. };
  266. }
  267. /** Offsets SkIRect by adding dx to fLeft, fRight; and by adding dy to fTop, fBottom.
  268. If dx is negative, moves SkIRect returned to the left.
  269. If dx is positive, moves SkIRect returned to the right.
  270. If dy is negative, moves SkIRect returned upward.
  271. If dy is positive, moves SkIRect returned downward.
  272. @param dx offset added to fLeft and fRight
  273. @param dy offset added to fTop and fBottom
  274. */
  275. void offset(int32_t dx, int32_t dy) {
  276. fLeft = Sk32_sat_add(fLeft, dx);
  277. fTop = Sk32_sat_add(fTop, dy);
  278. fRight = Sk32_sat_add(fRight, dx);
  279. fBottom = Sk32_sat_add(fBottom, dy);
  280. }
  281. /** Offsets SkIRect by adding delta.fX to fLeft, fRight; and by adding delta.fY to
  282. fTop, fBottom.
  283. If delta.fX is negative, moves SkIRect returned to the left.
  284. If delta.fX is positive, moves SkIRect returned to the right.
  285. If delta.fY is negative, moves SkIRect returned upward.
  286. If delta.fY is positive, moves SkIRect returned downward.
  287. @param delta offset added to SkIRect
  288. */
  289. void offset(const SkIPoint& delta) {
  290. this->offset(delta.fX, delta.fY);
  291. }
  292. /** Offsets SkIRect so that fLeft equals newX, and fTop equals newY. width and height
  293. are unchanged.
  294. @param newX stored in fLeft, preserving width()
  295. @param newY stored in fTop, preserving height()
  296. */
  297. void offsetTo(int32_t newX, int32_t newY) {
  298. fRight = Sk64_pin_to_s32((int64_t)fRight + newX - fLeft);
  299. fBottom = Sk64_pin_to_s32((int64_t)fBottom + newY - fTop);
  300. fLeft = newX;
  301. fTop = newY;
  302. }
  303. /** Insets SkIRect by (dx,dy).
  304. If dx is positive, makes SkIRect narrower.
  305. If dx is negative, makes SkIRect wider.
  306. If dy is positive, makes SkIRect shorter.
  307. If dy is negative, makes SkIRect taller.
  308. @param dx offset added to fLeft and subtracted from fRight
  309. @param dy offset added to fTop and subtracted from fBottom
  310. */
  311. void inset(int32_t dx, int32_t dy) {
  312. fLeft = Sk32_sat_add(fLeft, dx);
  313. fTop = Sk32_sat_add(fTop, dy);
  314. fRight = Sk32_sat_sub(fRight, dx);
  315. fBottom = Sk32_sat_sub(fBottom, dy);
  316. }
  317. /** Outsets SkIRect by (dx, dy).
  318. If dx is positive, makes SkIRect wider.
  319. If dx is negative, makes SkIRect narrower.
  320. If dy is positive, makes SkIRect taller.
  321. If dy is negative, makes SkIRect shorter.
  322. @param dx subtracted to fLeft and added from fRight
  323. @param dy subtracted to fTop and added from fBottom
  324. */
  325. void outset(int32_t dx, int32_t dy) { this->inset(-dx, -dy); }
  326. /** Adjusts SkIRect by adding dL to fLeft, dT to fTop, dR to fRight, and dB to fBottom.
  327. If dL is positive, narrows SkIRect on the left. If negative, widens it on the left.
  328. If dT is positive, shrinks SkIRect on the top. If negative, lengthens it on the top.
  329. If dR is positive, narrows SkIRect on the right. If negative, widens it on the right.
  330. If dB is positive, shrinks SkIRect on the bottom. If negative, lengthens it on the bottom.
  331. The resulting SkIRect is not checked for validity. Thus, if the resulting SkIRect left is
  332. greater than right, the SkIRect will be considered empty. Call sort() after this call
  333. if that is not the desired behavior.
  334. @param dL offset added to fLeft
  335. @param dT offset added to fTop
  336. @param dR offset added to fRight
  337. @param dB offset added to fBottom
  338. */
  339. void adjust(int32_t dL, int32_t dT, int32_t dR, int32_t dB) {
  340. fLeft = Sk32_sat_add(fLeft, dL);
  341. fTop = Sk32_sat_add(fTop, dT);
  342. fRight = Sk32_sat_add(fRight, dR);
  343. fBottom = Sk32_sat_add(fBottom, dB);
  344. }
  345. /** Returns true if: fLeft <= x < fRight && fTop <= y < fBottom.
  346. Returns false if SkIRect is empty.
  347. Considers input to describe constructed SkIRect: (x, y, x + 1, y + 1) and
  348. returns true if constructed area is completely enclosed by SkIRect area.
  349. @param x test SkIPoint x-coordinate
  350. @param y test SkIPoint y-coordinate
  351. @return true if (x, y) is inside SkIRect
  352. */
  353. bool contains(int32_t x, int32_t y) const {
  354. return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
  355. }
  356. /** Constructs SkIRect to intersect from (left, top, right, bottom). Does not sort
  357. construction.
  358. Returns true if SkIRect contains construction.
  359. Returns false if SkIRect is empty or construction is empty.
  360. @param left x-axis minimum of constructed SkIRect
  361. @param top y-axis minimum of constructed SkIRect
  362. @param right x-axis maximum of constructed SkIRect
  363. @param bottom y-axis maximum of constructed SkIRect
  364. @return true if all sides of SkIRect are outside construction
  365. */
  366. bool contains(int32_t left, int32_t top, int32_t right, int32_t bottom) const {
  367. return left < right && top < bottom && !this->isEmpty() && // check for empties
  368. fLeft <= left && fTop <= top &&
  369. fRight >= right && fBottom >= bottom;
  370. }
  371. /** Returns true if SkIRect contains r.
  372. Returns false if SkIRect is empty or r is empty.
  373. SkIRect contains r when SkIRect area completely includes r area.
  374. @param r SkIRect contained
  375. @return true if all sides of SkIRect are outside r
  376. */
  377. bool contains(const SkIRect& r) const {
  378. return !r.isEmpty() && !this->isEmpty() && // check for empties
  379. fLeft <= r.fLeft && fTop <= r.fTop &&
  380. fRight >= r.fRight && fBottom >= r.fBottom;
  381. }
  382. /** Returns true if SkIRect contains r.
  383. Returns false if SkIRect is empty or r is empty.
  384. SkIRect contains r when SkIRect area completely includes r area.
  385. @param r SkRect contained
  386. @return true if all sides of SkIRect are outside r
  387. */
  388. bool contains(const SkRect& r) const;
  389. /** Constructs SkIRect from (left, top, right, bottom). Does not sort
  390. construction.
  391. Returns true if SkIRect contains construction.
  392. Asserts if SkIRect is empty or construction is empty, and if SK_DEBUG is defined.
  393. Return is undefined if SkIRect is empty or construction is empty.
  394. @param left x-axis minimum of constructed SkIRect
  395. @param top y-axis minimum of constructed SkIRect
  396. @param right x-axis maximum of constructed SkIRect
  397. @param bottom y-axis maximum of constructed SkIRect
  398. @return true if all sides of SkIRect are outside construction
  399. */
  400. bool containsNoEmptyCheck(int32_t left, int32_t top,
  401. int32_t right, int32_t bottom) const {
  402. SkASSERT(fLeft < fRight && fTop < fBottom);
  403. SkASSERT(left < right && top < bottom);
  404. return fLeft <= left && fTop <= top &&
  405. fRight >= right && fBottom >= bottom;
  406. }
  407. /** Returns true if SkIRect contains construction.
  408. Asserts if SkIRect is empty or construction is empty, and if SK_DEBUG is defined.
  409. Return is undefined if SkIRect is empty or construction is empty.
  410. @param r SkIRect contained
  411. @return true if all sides of SkIRect are outside r
  412. */
  413. bool containsNoEmptyCheck(const SkIRect& r) const {
  414. return containsNoEmptyCheck(r.fLeft, r.fTop, r.fRight, r.fBottom);
  415. }
  416. /** Returns true if SkIRect intersects r, and sets SkIRect to intersection.
  417. Returns false if SkIRect does not intersect r, and leaves SkIRect unchanged.
  418. Returns false if either r or SkIRect is empty, leaving SkIRect unchanged.
  419. @param r limit of result
  420. @return true if r and SkIRect have area in common
  421. */
  422. bool intersect(const SkIRect& r) {
  423. return this->intersect(*this, r);
  424. }
  425. /** Returns true if a intersects b, and sets SkIRect to intersection.
  426. Returns false if a does not intersect b, and leaves SkIRect unchanged.
  427. Asserts if either a or b is empty, and if SK_DEBUG is defined.
  428. @param a SkIRect to intersect
  429. @param b SkIRect to intersect
  430. @return true if a and b have area in common
  431. */
  432. bool SK_WARN_UNUSED_RESULT intersectNoEmptyCheck(const SkIRect& a, const SkIRect& b) {
  433. SkASSERT(!a.isEmpty64() && !b.isEmpty64());
  434. SkIRect r = {
  435. SkMax32(a.fLeft, b.fLeft),
  436. SkMax32(a.fTop, b.fTop),
  437. SkMin32(a.fRight, b.fRight),
  438. SkMin32(a.fBottom, b.fBottom)
  439. };
  440. if (r.isEmpty()) {
  441. return false;
  442. }
  443. *this = r;
  444. return true;
  445. }
  446. /** Returns true if a intersects b, and sets SkIRect to intersection.
  447. Returns false if a does not intersect b, and leaves SkIRect unchanged.
  448. Returns false if either a or b is empty, leaving SkIRect unchanged.
  449. @param a SkIRect to intersect
  450. @param b SkIRect to intersect
  451. @return true if a and b have area in common
  452. */
  453. bool SK_WARN_UNUSED_RESULT intersect(const SkIRect& a, const SkIRect& b) {
  454. if (a.isEmpty64() || b.isEmpty64()) {
  455. return false;
  456. }
  457. return this->intersectNoEmptyCheck(a, b);
  458. }
  459. /** Constructs SkIRect to intersect from (left, top, right, bottom). Does not sort
  460. construction.
  461. Returns true if SkIRect intersects construction, and sets SkIRect to intersection.
  462. Returns false if SkIRect does not intersect construction, and leaves SkIRect unchanged.
  463. Returns false if either construction or SkIRect is empty, leaving SkIRect unchanged.
  464. @param left x-axis minimum of constructed SkIRect
  465. @param top y-axis minimum of constructed SkIRect
  466. @param right x-axis maximum of constructed SkIRect
  467. @param bottom y-axis maximum of constructed SkIRect
  468. @return true if construction and SkIRect have area in common
  469. */
  470. bool intersect(int32_t left, int32_t top, int32_t right, int32_t bottom) {
  471. return this->intersect(*this, {left, top, right, bottom});
  472. }
  473. /** Returns true if a intersects b.
  474. Returns false if either a or b is empty, or do not intersect.
  475. @param a SkIRect to intersect
  476. @param b SkIRect to intersect
  477. @return true if a and b have area in common
  478. */
  479. static bool Intersects(const SkIRect& a, const SkIRect& b) {
  480. SkIRect dummy;
  481. return dummy.intersect(a, b);
  482. }
  483. /** Returns true if a intersects b.
  484. Asserts if either a or b is empty, and if SK_DEBUG is defined.
  485. @param a SkIRect to intersect
  486. @param b SkIRect to intersect
  487. @return true if a and b have area in common
  488. */
  489. static bool IntersectsNoEmptyCheck(const SkIRect& a, const SkIRect& b) {
  490. SkIRect dummy;
  491. return dummy.intersectNoEmptyCheck(a, b);
  492. }
  493. /** Constructs SkIRect to intersect from (left, top, right, bottom). Does not sort
  494. construction.
  495. Sets SkIRect to the union of itself and the construction.
  496. Has no effect if construction is empty. Otherwise, if SkIRect is empty, sets
  497. SkIRect to construction.
  498. @param left x-axis minimum of constructed SkIRect
  499. @param top y-axis minimum of constructed SkIRect
  500. @param right x-axis maximum of constructed SkIRect
  501. @param bottom y-axis maximum of constructed SkIRect
  502. */
  503. void join(int32_t left, int32_t top, int32_t right, int32_t bottom);
  504. /** Sets SkIRect to the union of itself and r.
  505. Has no effect if r is empty. Otherwise, if SkIRect is empty, sets SkIRect to r.
  506. @param r expansion SkIRect
  507. */
  508. void join(const SkIRect& r) {
  509. this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
  510. }
  511. /** Swaps fLeft and fRight if fLeft is greater than fRight; and swaps
  512. fTop and fBottom if fTop is greater than fBottom. Result may be empty,
  513. and width() and height() will be zero or positive.
  514. */
  515. void sort() {
  516. using std::swap;
  517. if (fLeft > fRight) {
  518. swap(fLeft, fRight);
  519. }
  520. if (fTop > fBottom) {
  521. swap(fTop, fBottom);
  522. }
  523. }
  524. /** Returns SkIRect with fLeft and fRight swapped if fLeft is greater than fRight; and
  525. with fTop and fBottom swapped if fTop is greater than fBottom. Result may be empty;
  526. and width() and height() will be zero or positive.
  527. @return sorted SkIRect
  528. */
  529. SkIRect makeSorted() const {
  530. return MakeLTRB(SkMin32(fLeft, fRight), SkMin32(fTop, fBottom),
  531. SkMax32(fLeft, fRight), SkMax32(fTop, fBottom));
  532. }
  533. /** Returns a reference to immutable empty SkIRect, set to (0, 0, 0, 0).
  534. @return global SkIRect set to all zeroes
  535. */
  536. static const SkIRect& SK_WARN_UNUSED_RESULT EmptyIRect() {
  537. static const SkIRect gEmpty = { 0, 0, 0, 0 };
  538. return gEmpty;
  539. }
  540. };
  541. /** \struct SkRect
  542. SkRect holds four SkScalar coordinates describing the upper and
  543. lower bounds of a rectangle. SkRect may be created from outer bounds or
  544. from position, width, and height. SkRect describes an area; if its right
  545. is less than or equal to its left, or if its bottom is less than or equal to
  546. its top, it is considered empty.
  547. */
  548. struct SK_API SkRect {
  549. SkScalar fLeft; //!< smaller x-axis bounds
  550. SkScalar fTop; //!< smaller y-axis bounds
  551. SkScalar fRight; //!< larger x-axis bounds
  552. SkScalar fBottom; //!< larger y-axis bounds
  553. /** Returns constructed SkRect set to (0, 0, 0, 0).
  554. Many other rectangles are empty; if left is equal to or greater than right,
  555. or if top is equal to or greater than bottom. Setting all members to zero
  556. is a convenience, but does not designate a special empty rectangle.
  557. @return bounds (0, 0, 0, 0)
  558. */
  559. static constexpr SkRect SK_WARN_UNUSED_RESULT MakeEmpty() {
  560. return SkRect{0, 0, 0, 0};
  561. }
  562. /** Returns constructed SkRect set to SkScalar values (0, 0, w, h). Does not
  563. validate input; w or h may be negative.
  564. Passing integer values may generate a compiler warning since SkRect cannot
  565. represent 32-bit integers exactly. Use SkIRect for an exact integer rectangle.
  566. @param w SkScalar width of constructed SkRect
  567. @param h SkScalar height of constructed SkRect
  568. @return bounds (0, 0, w, h)
  569. */
  570. static constexpr SkRect SK_WARN_UNUSED_RESULT MakeWH(SkScalar w, SkScalar h) {
  571. return SkRect{0, 0, w, h};
  572. }
  573. /** Returns constructed SkRect set to integer values (0, 0, w, h). Does not validate
  574. input; w or h may be negative.
  575. Use to avoid a compiler warning that input may lose precision when stored.
  576. Use SkIRect for an exact integer rectangle.
  577. @param w integer width of constructed SkRect
  578. @param h integer height of constructed SkRect
  579. @return bounds (0, 0, w, h)
  580. */
  581. static SkRect SK_WARN_UNUSED_RESULT MakeIWH(int w, int h) {
  582. SkRect r;
  583. r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
  584. return r;
  585. }
  586. /** Returns constructed SkRect set to (0, 0, size.width(), size.height()). Does not
  587. validate input; size.width() or size.height() may be negative.
  588. @param size SkScalar values for SkRect width and height
  589. @return bounds (0, 0, size.width(), size.height())
  590. */
  591. static constexpr SkRect SK_WARN_UNUSED_RESULT MakeSize(const SkSize& size) {
  592. return SkRect{0, 0, size.fWidth, size.fHeight};
  593. }
  594. /** Returns constructed SkRect set to (l, t, r, b). Does not sort input; SkRect may
  595. result in fLeft greater than fRight, or fTop greater than fBottom.
  596. @param l SkScalar stored in fLeft
  597. @param t SkScalar stored in fTop
  598. @param r SkScalar stored in fRight
  599. @param b SkScalar stored in fBottom
  600. @return bounds (l, t, r, b)
  601. */
  602. static constexpr SkRect SK_WARN_UNUSED_RESULT MakeLTRB(SkScalar l, SkScalar t, SkScalar r,
  603. SkScalar b) {
  604. return SkRect {l, t, r, b};
  605. }
  606. /** Returns constructed SkRect set to (x, y, x + w, y + h).
  607. Does not validate input; w or h may be negative.
  608. @param x stored in fLeft
  609. @param y stored in fTop
  610. @param w added to x and stored in fRight
  611. @param h added to y and stored in fBottom
  612. @return bounds at (x, y) with width w and height h
  613. */
  614. static constexpr SkRect SK_WARN_UNUSED_RESULT MakeXYWH(SkScalar x, SkScalar y, SkScalar w,
  615. SkScalar h) {
  616. return SkRect {x, y, x + w, y + h};
  617. }
  618. /** Returns constructed SkIRect set to (0, 0, size.width(), size.height()).
  619. Does not validate input; size.width() or size.height() may be negative.
  620. @param size integer values for SkRect width and height
  621. @return bounds (0, 0, size.width(), size.height())
  622. */
  623. static SkRect Make(const SkISize& size) {
  624. return MakeIWH(size.width(), size.height());
  625. }
  626. /** Returns constructed SkIRect set to irect, promoting integers to scalar.
  627. Does not validate input; fLeft may be greater than fRight, fTop may be greater
  628. than fBottom.
  629. @param irect integer unsorted bounds
  630. @return irect members converted to SkScalar
  631. */
  632. static SkRect SK_WARN_UNUSED_RESULT Make(const SkIRect& irect) {
  633. SkRect r;
  634. r.set(SkIntToScalar(irect.fLeft),
  635. SkIntToScalar(irect.fTop),
  636. SkIntToScalar(irect.fRight),
  637. SkIntToScalar(irect.fBottom));
  638. return r;
  639. }
  640. /** Returns true if fLeft is equal to or greater than fRight, or if fTop is equal
  641. to or greater than fBottom. Call sort() to reverse rectangles with negative
  642. width() or height().
  643. @return true if width() or height() are zero or negative
  644. */
  645. bool isEmpty() const {
  646. // We write it as the NOT of a non-empty rect, so we will return true if any values
  647. // are NaN.
  648. return !(fLeft < fRight && fTop < fBottom);
  649. }
  650. /** Returns true if fLeft is equal to or less than fRight, or if fTop is equal
  651. to or less than fBottom. Call sort() to reverse rectangles with negative
  652. width() or height().
  653. @return true if width() or height() are zero or positive
  654. */
  655. bool isSorted() const { return fLeft <= fRight && fTop <= fBottom; }
  656. /** Returns true if all values in the rectangle are finite: SK_ScalarMin or larger,
  657. and SK_ScalarMax or smaller.
  658. @return true if no member is infinite or NaN
  659. */
  660. bool isFinite() const {
  661. float accum = 0;
  662. accum *= fLeft;
  663. accum *= fTop;
  664. accum *= fRight;
  665. accum *= fBottom;
  666. // accum is either NaN or it is finite (zero).
  667. SkASSERT(0 == accum || SkScalarIsNaN(accum));
  668. // value==value will be true iff value is not NaN
  669. // TODO: is it faster to say !accum or accum==accum?
  670. return !SkScalarIsNaN(accum);
  671. }
  672. /** Returns left edge of SkRect, if sorted. Call isSorted() to see if SkRect is valid.
  673. Call sort() to reverse fLeft and fRight if needed.
  674. @return fLeft
  675. */
  676. SkScalar x() const { return fLeft; }
  677. /** Returns top edge of SkRect, if sorted. Call isEmpty() to see if SkRect may be invalid,
  678. and sort() to reverse fTop and fBottom if needed.
  679. @return fTop
  680. */
  681. SkScalar y() const { return fTop; }
  682. /** Returns left edge of SkRect, if sorted. Call isSorted() to see if SkRect is valid.
  683. Call sort() to reverse fLeft and fRight if needed.
  684. @return fLeft
  685. */
  686. SkScalar left() const { return fLeft; }
  687. /** Returns top edge of SkRect, if sorted. Call isEmpty() to see if SkRect may be invalid,
  688. and sort() to reverse fTop and fBottom if needed.
  689. @return fTop
  690. */
  691. SkScalar top() const { return fTop; }
  692. /** Returns right edge of SkRect, if sorted. Call isSorted() to see if SkRect is valid.
  693. Call sort() to reverse fLeft and fRight if needed.
  694. @return fRight
  695. */
  696. SkScalar right() const { return fRight; }
  697. /** Returns bottom edge of SkRect, if sorted. Call isEmpty() to see if SkRect may be invalid,
  698. and sort() to reverse fTop and fBottom if needed.
  699. @return fBottom
  700. */
  701. SkScalar bottom() const { return fBottom; }
  702. /** Returns span on the x-axis. This does not check if SkRect is sorted, or if
  703. result fits in 32-bit float; result may be negative or infinity.
  704. @return fRight minus fLeft
  705. */
  706. SkScalar width() const { return fRight - fLeft; }
  707. /** Returns span on the y-axis. This does not check if SkRect is sorted, or if
  708. result fits in 32-bit float; result may be negative or infinity.
  709. @return fBottom minus fTop
  710. */
  711. SkScalar height() const { return fBottom - fTop; }
  712. /** Returns average of left edge and right edge. Result does not change if SkRect
  713. is sorted. Result may overflow to infinity if SkRect is far from the origin.
  714. @return midpoint on x-axis
  715. */
  716. SkScalar centerX() const {
  717. // don't use SkScalarHalf(fLeft + fBottom) as that might overflow before the 0.5
  718. return SkScalarHalf(fLeft) + SkScalarHalf(fRight);
  719. }
  720. /** Returns average of top edge and bottom edge. Result does not change if SkRect
  721. is sorted.
  722. @return midpoint on y-axis
  723. */
  724. SkScalar centerY() const {
  725. // don't use SkScalarHalf(fTop + fBottom) as that might overflow before the 0.5
  726. return SkScalarHalf(fTop) + SkScalarHalf(fBottom);
  727. }
  728. /** Returns true if all members in a: fLeft, fTop, fRight, and fBottom; are
  729. equal to the corresponding members in b.
  730. a and b are not equal if either contain NaN. a and b are equal if members
  731. contain zeroes with different signs.
  732. @param a SkRect to compare
  733. @param b SkRect to compare
  734. @return true if members are equal
  735. */
  736. friend bool operator==(const SkRect& a, const SkRect& b) {
  737. return SkScalarsEqual((const SkScalar*)&a, (const SkScalar*)&b, 4);
  738. }
  739. /** Returns true if any in a: fLeft, fTop, fRight, and fBottom; does not
  740. equal the corresponding members in b.
  741. a and b are not equal if either contain NaN. a and b are equal if members
  742. contain zeroes with different signs.
  743. @param a SkRect to compare
  744. @param b SkRect to compare
  745. @return true if members are not equal
  746. */
  747. friend bool operator!=(const SkRect& a, const SkRect& b) {
  748. return !SkScalarsEqual((const SkScalar*)&a, (const SkScalar*)&b, 4);
  749. }
  750. /** Returns four points in quad that enclose SkRect ordered as: top-left, top-right,
  751. bottom-right, bottom-left.
  752. TODO: Consider adding parameter to control whether quad is clockwise or counterclockwise.
  753. @param quad storage for corners of SkRect
  754. */
  755. void toQuad(SkPoint quad[4]) const;
  756. /** Sets SkRect to (0, 0, 0, 0).
  757. Many other rectangles are empty; if left is equal to or greater than right,
  758. or if top is equal to or greater than bottom. Setting all members to zero
  759. is a convenience, but does not designate a special empty rectangle.
  760. */
  761. void setEmpty() { *this = MakeEmpty(); }
  762. /** Sets SkRect to src, promoting src members from integer to scalar.
  763. Very large values in src may lose precision.
  764. @param src integer SkRect
  765. */
  766. void set(const SkIRect& src) {
  767. fLeft = SkIntToScalar(src.fLeft);
  768. fTop = SkIntToScalar(src.fTop);
  769. fRight = SkIntToScalar(src.fRight);
  770. fBottom = SkIntToScalar(src.fBottom);
  771. }
  772. /** Sets SkRect to (left, top, right, bottom).
  773. left and right are not sorted; left is not necessarily less than right.
  774. top and bottom are not sorted; top is not necessarily less than bottom.
  775. @param left stored in fLeft
  776. @param top stored in fTop
  777. @param right stored in fRight
  778. @param bottom stored in fBottom
  779. */
  780. void set(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
  781. fLeft = left;
  782. fTop = top;
  783. fRight = right;
  784. fBottom = bottom;
  785. }
  786. /** Sets SkRect to (left, top, right, bottom).
  787. left and right are not sorted; left is not necessarily less than right.
  788. top and bottom are not sorted; top is not necessarily less than bottom.
  789. @param left stored in fLeft
  790. @param top stored in fTop
  791. @param right stored in fRight
  792. @param bottom stored in fBottom
  793. */
  794. void setLTRB(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
  795. this->set(left, top, right, bottom);
  796. }
  797. /** Sets SkRect to (left, top, right, bottom).
  798. All parameters are promoted from integer to scalar.
  799. left and right are not sorted; left is not necessarily less than right.
  800. top and bottom are not sorted; top is not necessarily less than bottom.
  801. @param left promoted to SkScalar and stored in fLeft
  802. @param top promoted to SkScalar and stored in fTop
  803. @param right promoted to SkScalar and stored in fRight
  804. @param bottom promoted to SkScalar and stored in fBottom
  805. */
  806. void iset(int left, int top, int right, int bottom) {
  807. fLeft = SkIntToScalar(left);
  808. fTop = SkIntToScalar(top);
  809. fRight = SkIntToScalar(right);
  810. fBottom = SkIntToScalar(bottom);
  811. }
  812. /** Sets SkRect to (0, 0, width, height).
  813. width and height may be zero or negative. width and height are promoted from
  814. integer to SkScalar, large values may lose precision.
  815. @param width promoted to SkScalar and stored in fRight
  816. @param height promoted to SkScalar and stored in fBottom
  817. */
  818. void isetWH(int width, int height) {
  819. fLeft = fTop = 0;
  820. fRight = SkIntToScalar(width);
  821. fBottom = SkIntToScalar(height);
  822. }
  823. /** Sets to bounds of SkPoint array with count entries. If count is zero or smaller,
  824. or if SkPoint array contains an infinity or NaN, sets SkRect to (0, 0, 0, 0).
  825. Result is either empty or sorted: fLeft is less than or equal to fRight, and
  826. fTop is less than or equal to fBottom.
  827. @param pts SkPoint array
  828. @param count entries in array
  829. */
  830. void set(const SkPoint pts[], int count) {
  831. // set() had been checking for non-finite values, so keep that behavior
  832. // for now. Now that we have setBoundsCheck(), we may decide to make
  833. // set() be simpler/faster, and not check for those.
  834. (void)this->setBoundsCheck(pts, count);
  835. }
  836. /** Sets to bounds of SkPoint array with count entries. If count is zero or smaller,
  837. or if SkPoint array contains an infinity or NaN, sets to (0, 0, 0, 0).
  838. Result is either empty or sorted: fLeft is less than or equal to fRight, and
  839. fTop is less than or equal to fBottom.
  840. @param pts SkPoint array
  841. @param count entries in array
  842. */
  843. void setBounds(const SkPoint pts[], int count) {
  844. (void)this->setBoundsCheck(pts, count);
  845. }
  846. /** Sets to bounds of SkPoint array with count entries. Returns false if count is
  847. zero or smaller, or if SkPoint array contains an infinity or NaN; in these cases
  848. sets SkRect to (0, 0, 0, 0).
  849. Result is either empty or sorted: fLeft is less than or equal to fRight, and
  850. fTop is less than or equal to fBottom.
  851. @param pts SkPoint array
  852. @param count entries in array
  853. @return true if all SkPoint values are finite
  854. */
  855. bool setBoundsCheck(const SkPoint pts[], int count);
  856. /** Sets to bounds of SkPoint pts array with count entries. If any SkPoint in pts
  857. contains infinity or NaN, all SkRect dimensions are set to NaN.
  858. @param pts SkPoint array
  859. @param count entries in array
  860. */
  861. void setBoundsNoCheck(const SkPoint pts[], int count);
  862. /** Sets bounds to the smallest SkRect enclosing SkPoint p0 and p1. The result is
  863. sorted and may be empty. Does not check to see if values are finite.
  864. @param p0 corner to include
  865. @param p1 corner to include
  866. */
  867. void set(const SkPoint& p0, const SkPoint& p1) {
  868. fLeft = SkMinScalar(p0.fX, p1.fX);
  869. fRight = SkMaxScalar(p0.fX, p1.fX);
  870. fTop = SkMinScalar(p0.fY, p1.fY);
  871. fBottom = SkMaxScalar(p0.fY, p1.fY);
  872. }
  873. /** Sets SkRect to (x, y, x + width, y + height).
  874. Does not validate input; width or height may be negative.
  875. @param x stored in fLeft
  876. @param y stored in fTop
  877. @param width added to x and stored in fRight
  878. @param height added to y and stored in fBottom
  879. */
  880. void setXYWH(SkScalar x, SkScalar y, SkScalar width, SkScalar height) {
  881. fLeft = x;
  882. fTop = y;
  883. fRight = x + width;
  884. fBottom = y + height;
  885. }
  886. /** Sets SkRect to (0, 0, width, height). Does not validate input;
  887. width or height may be negative.
  888. @param width stored in fRight
  889. @param height stored in fBottom
  890. */
  891. void setWH(SkScalar width, SkScalar height) {
  892. fLeft = 0;
  893. fTop = 0;
  894. fRight = width;
  895. fBottom = height;
  896. }
  897. /** Returns SkRect offset by (dx, dy).
  898. If dx is negative, SkRect returned is moved to the left.
  899. If dx is positive, SkRect returned is moved to the right.
  900. If dy is negative, SkRect returned is moved upward.
  901. If dy is positive, SkRect returned is moved downward.
  902. @param dx added to fLeft and fRight
  903. @param dy added to fTop and fBottom
  904. @return SkRect offset on axes, with original width and height
  905. */
  906. SkRect makeOffset(SkScalar dx, SkScalar dy) const {
  907. return MakeLTRB(fLeft + dx, fTop + dy, fRight + dx, fBottom + dy);
  908. }
  909. /** Returns SkRect, inset by (dx, dy).
  910. If dx is negative, SkRect returned is wider.
  911. If dx is positive, SkRect returned is narrower.
  912. If dy is negative, SkRect returned is taller.
  913. If dy is positive, SkRect returned is shorter.
  914. @param dx added to fLeft and subtracted from fRight
  915. @param dy added to fTop and subtracted from fBottom
  916. @return SkRect inset symmetrically left and right, top and bottom
  917. */
  918. SkRect makeInset(SkScalar dx, SkScalar dy) const {
  919. return MakeLTRB(fLeft + dx, fTop + dy, fRight - dx, fBottom - dy);
  920. }
  921. /** Returns SkRect, outset by (dx, dy).
  922. If dx is negative, SkRect returned is narrower.
  923. If dx is positive, SkRect returned is wider.
  924. If dy is negative, SkRect returned is shorter.
  925. If dy is positive, SkRect returned is taller.
  926. @param dx subtracted to fLeft and added from fRight
  927. @param dy subtracted to fTop and added from fBottom
  928. @return SkRect outset symmetrically left and right, top and bottom
  929. */
  930. SkRect makeOutset(SkScalar dx, SkScalar dy) const {
  931. return MakeLTRB(fLeft - dx, fTop - dy, fRight + dx, fBottom + dy);
  932. }
  933. /** Offsets SkRect by adding dx to fLeft, fRight; and by adding dy to fTop, fBottom.
  934. If dx is negative, moves SkRect to the left.
  935. If dx is positive, moves SkRect to the right.
  936. If dy is negative, moves SkRect upward.
  937. If dy is positive, moves SkRect downward.
  938. @param dx offset added to fLeft and fRight
  939. @param dy offset added to fTop and fBottom
  940. */
  941. void offset(SkScalar dx, SkScalar dy) {
  942. fLeft += dx;
  943. fTop += dy;
  944. fRight += dx;
  945. fBottom += dy;
  946. }
  947. /** Offsets SkRect by adding delta.fX to fLeft, fRight; and by adding delta.fY to
  948. fTop, fBottom.
  949. If delta.fX is negative, moves SkRect to the left.
  950. If delta.fX is positive, moves SkRect to the right.
  951. If delta.fY is negative, moves SkRect upward.
  952. If delta.fY is positive, moves SkRect downward.
  953. @param delta added to SkRect
  954. */
  955. void offset(const SkPoint& delta) {
  956. this->offset(delta.fX, delta.fY);
  957. }
  958. /** Offsets SkRect so that fLeft equals newX, and fTop equals newY. width and height
  959. are unchanged.
  960. @param newX stored in fLeft, preserving width()
  961. @param newY stored in fTop, preserving height()
  962. */
  963. void offsetTo(SkScalar newX, SkScalar newY) {
  964. fRight += newX - fLeft;
  965. fBottom += newY - fTop;
  966. fLeft = newX;
  967. fTop = newY;
  968. }
  969. /** Insets SkRect by (dx, dy).
  970. If dx is positive, makes SkRect narrower.
  971. If dx is negative, makes SkRect wider.
  972. If dy is positive, makes SkRect shorter.
  973. If dy is negative, makes SkRect taller.
  974. @param dx added to fLeft and subtracted from fRight
  975. @param dy added to fTop and subtracted from fBottom
  976. */
  977. void inset(SkScalar dx, SkScalar dy) {
  978. fLeft += dx;
  979. fTop += dy;
  980. fRight -= dx;
  981. fBottom -= dy;
  982. }
  983. /** Outsets SkRect by (dx, dy).
  984. If dx is positive, makes SkRect wider.
  985. If dx is negative, makes SkRect narrower.
  986. If dy is positive, makes SkRect taller.
  987. If dy is negative, makes SkRect shorter.
  988. @param dx subtracted to fLeft and added from fRight
  989. @param dy subtracted to fTop and added from fBottom
  990. */
  991. void outset(SkScalar dx, SkScalar dy) { this->inset(-dx, -dy); }
  992. /** Returns true if SkRect intersects r, and sets SkRect to intersection.
  993. Returns false if SkRect does not intersect r, and leaves SkRect unchanged.
  994. Returns false if either r or SkRect is empty, leaving SkRect unchanged.
  995. @param r limit of result
  996. @return true if r and SkRect have area in common
  997. */
  998. bool intersect(const SkRect& r);
  999. /** Constructs SkRect to intersect from (left, top, right, bottom). Does not sort
  1000. construction.
  1001. Returns true if SkRect intersects construction, and sets SkRect to intersection.
  1002. Returns false if SkRect does not intersect construction, and leaves SkRect unchanged.
  1003. Returns false if either construction or SkRect is empty, leaving SkRect unchanged.
  1004. @param left x-axis minimum of constructed SkRect
  1005. @param top y-axis minimum of constructed SkRect
  1006. @param right x-axis maximum of constructed SkRect
  1007. @param bottom y-axis maximum of constructed SkRect
  1008. @return true if construction and SkRect have area in common
  1009. */
  1010. bool intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
  1011. /** Returns true if a intersects b, and sets SkRect to intersection.
  1012. Returns false if a does not intersect b, and leaves SkRect unchanged.
  1013. Returns false if either a or b is empty, leaving SkRect unchanged.
  1014. @param a SkRect to intersect
  1015. @param b SkRect to intersect
  1016. @return true if a and b have area in common
  1017. */
  1018. bool SK_WARN_UNUSED_RESULT intersect(const SkRect& a, const SkRect& b);
  1019. private:
  1020. static bool Intersects(SkScalar al, SkScalar at, SkScalar ar, SkScalar ab,
  1021. SkScalar bl, SkScalar bt, SkScalar br, SkScalar bb) {
  1022. SkScalar L = SkMaxScalar(al, bl);
  1023. SkScalar R = SkMinScalar(ar, br);
  1024. SkScalar T = SkMaxScalar(at, bt);
  1025. SkScalar B = SkMinScalar(ab, bb);
  1026. return L < R && T < B;
  1027. }
  1028. public:
  1029. /** Constructs SkRect to intersect from (left, top, right, bottom). Does not sort
  1030. construction.
  1031. Returns true if SkRect intersects construction.
  1032. Returns false if either construction or SkRect is empty, or do not intersect.
  1033. @param left x-axis minimum of constructed SkRect
  1034. @param top y-axis minimum of constructed SkRect
  1035. @param right x-axis maximum of constructed SkRect
  1036. @param bottom y-axis maximum of constructed SkRect
  1037. @return true if construction and SkRect have area in common
  1038. */
  1039. bool intersects(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) const {
  1040. return Intersects(fLeft, fTop, fRight, fBottom, left, top, right, bottom);
  1041. }
  1042. /** Returns true if SkRect intersects r.
  1043. Returns false if either r or SkRect is empty, or do not intersect.
  1044. @param r SkRect to intersect
  1045. @return true if r and SkRect have area in common
  1046. */
  1047. bool intersects(const SkRect& r) const {
  1048. return Intersects(fLeft, fTop, fRight, fBottom,
  1049. r.fLeft, r.fTop, r.fRight, r.fBottom);
  1050. }
  1051. /** Returns true if a intersects b.
  1052. Returns false if either a or b is empty, or do not intersect.
  1053. @param a SkRect to intersect
  1054. @param b SkRect to intersect
  1055. @return true if a and b have area in common
  1056. */
  1057. static bool Intersects(const SkRect& a, const SkRect& b) {
  1058. return Intersects(a.fLeft, a.fTop, a.fRight, a.fBottom,
  1059. b.fLeft, b.fTop, b.fRight, b.fBottom);
  1060. }
  1061. /** Constructs SkRect to intersect from (left, top, right, bottom). Does not sort
  1062. construction.
  1063. Sets SkRect to the union of itself and the construction.
  1064. Has no effect if construction is empty. Otherwise, if SkRect is empty, sets
  1065. SkRect to construction.
  1066. @param left x-axis minimum of constructed SkRect
  1067. @param top y-axis minimum of constructed SkRect
  1068. @param right x-axis maximum of constructed SkRect
  1069. @param bottom y-axis maximum of constructed SkRect
  1070. */
  1071. void join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
  1072. /** Sets SkRect to the union of itself and r.
  1073. Has no effect if r is empty. Otherwise, if SkRect is empty, sets
  1074. SkRect to r.
  1075. @param r expansion SkRect
  1076. */
  1077. void join(const SkRect& r) {
  1078. this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
  1079. }
  1080. /** Sets SkRect to the union of itself and r.
  1081. Asserts if r is empty and SK_DEBUG is defined.
  1082. If SkRect is empty, sets SkRect to r.
  1083. May produce incorrect results if r is empty.
  1084. @param r expansion SkRect
  1085. */
  1086. void joinNonEmptyArg(const SkRect& r) {
  1087. SkASSERT(!r.isEmpty());
  1088. // if we are empty, just assign
  1089. if (fLeft >= fRight || fTop >= fBottom) {
  1090. *this = r;
  1091. } else {
  1092. this->joinPossiblyEmptyRect(r);
  1093. }
  1094. }
  1095. /** Sets SkRect to the union of itself and the construction.
  1096. May produce incorrect results if SkRect or r is empty.
  1097. @param r expansion SkRect
  1098. */
  1099. void joinPossiblyEmptyRect(const SkRect& r) {
  1100. fLeft = SkMinScalar(fLeft, r.left());
  1101. fTop = SkMinScalar(fTop, r.top());
  1102. fRight = SkMaxScalar(fRight, r.right());
  1103. fBottom = SkMaxScalar(fBottom, r.bottom());
  1104. }
  1105. /** Returns true if: fLeft <= x < fRight && fTop <= y < fBottom.
  1106. Returns false if SkRect is empty.
  1107. @param x test SkPoint x-coordinate
  1108. @param y test SkPoint y-coordinate
  1109. @return true if (x, y) is inside SkRect
  1110. */
  1111. bool contains(SkScalar x, SkScalar y) const {
  1112. return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
  1113. }
  1114. /** Returns true if SkRect contains r.
  1115. Returns false if SkRect is empty or r is empty.
  1116. SkRect contains r when SkRect area completely includes r area.
  1117. @param r SkRect contained
  1118. @return true if all sides of SkRect are outside r
  1119. */
  1120. bool contains(const SkRect& r) const {
  1121. // todo: can we eliminate the this->isEmpty check?
  1122. return !r.isEmpty() && !this->isEmpty() &&
  1123. fLeft <= r.fLeft && fTop <= r.fTop &&
  1124. fRight >= r.fRight && fBottom >= r.fBottom;
  1125. }
  1126. /** Returns true if SkRect contains r.
  1127. Returns false if SkRect is empty or r is empty.
  1128. SkRect contains r when SkRect area completely includes r area.
  1129. @param r SkIRect contained
  1130. @return true if all sides of SkRect are outside r
  1131. */
  1132. bool contains(const SkIRect& r) const {
  1133. // todo: can we eliminate the this->isEmpty check?
  1134. return !r.isEmpty() && !this->isEmpty() &&
  1135. fLeft <= SkIntToScalar(r.fLeft) && fTop <= SkIntToScalar(r.fTop) &&
  1136. fRight >= SkIntToScalar(r.fRight) && fBottom >= SkIntToScalar(r.fBottom);
  1137. }
  1138. /** Sets SkIRect by adding 0.5 and discarding the fractional portion of SkRect
  1139. members, using (SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
  1140. SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom)).
  1141. @param dst storage for SkIRect
  1142. */
  1143. void round(SkIRect* dst) const {
  1144. SkASSERT(dst);
  1145. dst->set(SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
  1146. SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom));
  1147. }
  1148. /** Sets SkIRect by discarding the fractional portion of fLeft and fTop; and rounding
  1149. up fRight and fBottom, using
  1150. (SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
  1151. SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)).
  1152. @param dst storage for SkIRect
  1153. */
  1154. void roundOut(SkIRect* dst) const {
  1155. SkASSERT(dst);
  1156. dst->set(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
  1157. SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom));
  1158. }
  1159. /** Sets SkRect by discarding the fractional portion of fLeft and fTop; and rounding
  1160. up fRight and fBottom, using
  1161. (SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
  1162. SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)).
  1163. @param dst storage for SkRect
  1164. */
  1165. void roundOut(SkRect* dst) const {
  1166. dst->set(SkScalarFloorToScalar(fLeft),
  1167. SkScalarFloorToScalar(fTop),
  1168. SkScalarCeilToScalar(fRight),
  1169. SkScalarCeilToScalar(fBottom));
  1170. }
  1171. /** Sets SkRect by rounding up fLeft and fTop; and discarding the fractional portion
  1172. of fRight and fBottom, using
  1173. (SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop),
  1174. SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom)).
  1175. @param dst storage for SkIRect
  1176. */
  1177. void roundIn(SkIRect* dst) const {
  1178. SkASSERT(dst);
  1179. dst->set(SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop),
  1180. SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom));
  1181. }
  1182. /** Returns SkIRect by adding 0.5 and discarding the fractional portion of SkRect
  1183. members, using (SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
  1184. SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom)).
  1185. @return rounded SkIRect
  1186. */
  1187. SkIRect round() const {
  1188. SkIRect ir;
  1189. this->round(&ir);
  1190. return ir;
  1191. }
  1192. /** Sets SkIRect by discarding the fractional portion of fLeft and fTop; and rounding
  1193. up fRight and fBottom, using
  1194. (SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
  1195. SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)).
  1196. @return rounded SkIRect
  1197. */
  1198. SkIRect roundOut() const {
  1199. SkIRect ir;
  1200. this->roundOut(&ir);
  1201. return ir;
  1202. }
  1203. /** Swaps fLeft and fRight if fLeft is greater than fRight; and swaps
  1204. fTop and fBottom if fTop is greater than fBottom. Result may be empty;
  1205. and width() and height() will be zero or positive.
  1206. */
  1207. void sort() {
  1208. using std::swap;
  1209. if (fLeft > fRight) {
  1210. swap(fLeft, fRight);
  1211. }
  1212. if (fTop > fBottom) {
  1213. swap(fTop, fBottom);
  1214. }
  1215. }
  1216. /** Returns SkRect with fLeft and fRight swapped if fLeft is greater than fRight; and
  1217. with fTop and fBottom swapped if fTop is greater than fBottom. Result may be empty;
  1218. and width() and height() will be zero or positive.
  1219. @return sorted SkRect
  1220. */
  1221. SkRect makeSorted() const {
  1222. return MakeLTRB(SkMinScalar(fLeft, fRight), SkMinScalar(fTop, fBottom),
  1223. SkMaxScalar(fLeft, fRight), SkMaxScalar(fTop, fBottom));
  1224. }
  1225. /** Returns pointer to first scalar in SkRect, to treat it as an array with four
  1226. entries.
  1227. @return pointer to fLeft
  1228. */
  1229. const SkScalar* asScalars() const { return &fLeft; }
  1230. /** Writes text representation of SkRect to standard output. Set asHex to true to
  1231. generate exact binary representations of floating point numbers.
  1232. @param asHex true if SkScalar values are written as hexadecimal
  1233. */
  1234. void dump(bool asHex) const;
  1235. /** Writes text representation of SkRect to standard output. The representation may be
  1236. directly compiled as C++ code. Floating point values are written
  1237. with limited precision; it may not be possible to reconstruct original SkRect
  1238. from output.
  1239. */
  1240. void dump() const { this->dump(false); }
  1241. /** Writes text representation of SkRect to standard output. The representation may be
  1242. directly compiled as C++ code. Floating point values are written
  1243. in hexadecimal to preserve their exact bit pattern. The output reconstructs the
  1244. original SkRect.
  1245. Use instead of dump() when submitting
  1246. */
  1247. void dumpHex() const { this->dump(true); }
  1248. };
  1249. inline bool SkIRect::contains(const SkRect& r) const {
  1250. return !r.isEmpty() && !this->isEmpty() && // check for empties
  1251. (SkScalar)fLeft <= r.fLeft && (SkScalar)fTop <= r.fTop &&
  1252. (SkScalar)fRight >= r.fRight && (SkScalar)fBottom >= r.fBottom;
  1253. }
  1254. #endif