SkRect.h 57 KB

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