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