1 // Copyright 2014 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FXCRT_FX_COORDINATES_H_ 8 #define CORE_FXCRT_FX_COORDINATES_H_ 9 10 #include <stdint.h> 11 12 #ifndef NDEBUG 13 #include <iosfwd> 14 #endif 15 16 #include "third_party/base/span.h" 17 18 template <class BaseType> 19 class CFX_PTemplate { 20 public: CFX_PTemplate()21 CFX_PTemplate() : x(0), y(0) {} CFX_PTemplate(BaseType new_x,BaseType new_y)22 CFX_PTemplate(BaseType new_x, BaseType new_y) : x(new_x), y(new_y) {} CFX_PTemplate(const CFX_PTemplate & other)23 CFX_PTemplate(const CFX_PTemplate& other) : x(other.x), y(other.y) {} 24 25 CFX_PTemplate& operator=(const CFX_PTemplate& other) { 26 if (this != &other) { 27 x = other.x; 28 y = other.y; 29 } 30 return *this; 31 } 32 bool operator==(const CFX_PTemplate& other) const { 33 return x == other.x && y == other.y; 34 } 35 bool operator!=(const CFX_PTemplate& other) const { 36 return !(*this == other); 37 } 38 CFX_PTemplate& operator+=(const CFX_PTemplate<BaseType>& obj) { 39 x += obj.x; 40 y += obj.y; 41 return *this; 42 } 43 CFX_PTemplate& operator-=(const CFX_PTemplate<BaseType>& obj) { 44 x -= obj.x; 45 y -= obj.y; 46 return *this; 47 } 48 CFX_PTemplate operator+(const CFX_PTemplate& other) const { 49 return CFX_PTemplate(x + other.x, y + other.y); 50 } 51 CFX_PTemplate operator-(const CFX_PTemplate& other) const { 52 return CFX_PTemplate(x - other.x, y - other.y); 53 } 54 55 BaseType x; 56 BaseType y; 57 }; 58 using CFX_Point16 = CFX_PTemplate<int16_t>; 59 using CFX_Point = CFX_PTemplate<int32_t>; 60 using CFX_PointF = CFX_PTemplate<float>; 61 62 template <class BaseType> 63 class CFX_STemplate { 64 public: CFX_STemplate()65 CFX_STemplate() : width(0), height(0) {} 66 CFX_STemplate(BaseType new_width,BaseType new_height)67 CFX_STemplate(BaseType new_width, BaseType new_height) 68 : width(new_width), height(new_height) {} 69 CFX_STemplate(const CFX_STemplate & other)70 CFX_STemplate(const CFX_STemplate& other) 71 : width(other.width), height(other.height) {} 72 73 template <typename OtherType> As()74 CFX_STemplate<OtherType> As() const { 75 return CFX_STemplate<OtherType>(static_cast<OtherType>(width), 76 static_cast<OtherType>(height)); 77 } 78 clear()79 void clear() { 80 width = 0; 81 height = 0; 82 } 83 CFX_STemplate& operator=(const CFX_STemplate& other) { 84 if (this != &other) { 85 width = other.width; 86 height = other.height; 87 } 88 return *this; 89 } 90 bool operator==(const CFX_STemplate& other) const { 91 return width == other.width && height == other.height; 92 } 93 bool operator!=(const CFX_STemplate& other) const { 94 return !(*this == other); 95 } 96 CFX_STemplate& operator+=(const CFX_STemplate<BaseType>& obj) { 97 width += obj.width; 98 height += obj.height; 99 return *this; 100 } 101 CFX_STemplate& operator-=(const CFX_STemplate<BaseType>& obj) { 102 width -= obj.width; 103 height -= obj.height; 104 return *this; 105 } 106 CFX_STemplate& operator*=(BaseType factor) { 107 width *= factor; 108 height *= factor; 109 return *this; 110 } 111 CFX_STemplate& operator/=(BaseType divisor) { 112 width /= divisor; 113 height /= divisor; 114 return *this; 115 } 116 CFX_STemplate operator+(const CFX_STemplate& other) const { 117 return CFX_STemplate(width + other.width, height + other.height); 118 } 119 CFX_STemplate operator-(const CFX_STemplate& other) const { 120 return CFX_STemplate(width - other.width, height - other.height); 121 } 122 CFX_STemplate operator*(BaseType factor) const { 123 return CFX_STemplate(width * factor, height * factor); 124 } 125 CFX_STemplate operator/(BaseType divisor) const { 126 return CFX_STemplate(width / divisor, height / divisor); 127 } 128 129 BaseType width; 130 BaseType height; 131 }; 132 using CFX_Size = CFX_STemplate<int32_t>; 133 using CFX_SizeF = CFX_STemplate<float>; 134 135 template <class BaseType> 136 class CFX_VTemplate final : public CFX_PTemplate<BaseType> { 137 public: 138 using CFX_PTemplate<BaseType>::x; 139 using CFX_PTemplate<BaseType>::y; 140 CFX_VTemplate()141 CFX_VTemplate() : CFX_PTemplate<BaseType>() {} CFX_VTemplate(BaseType new_x,BaseType new_y)142 CFX_VTemplate(BaseType new_x, BaseType new_y) 143 : CFX_PTemplate<BaseType>(new_x, new_y) {} 144 CFX_VTemplate(const CFX_VTemplate & other)145 CFX_VTemplate(const CFX_VTemplate& other) : CFX_PTemplate<BaseType>(other) {} 146 CFX_VTemplate(const CFX_PTemplate<BaseType> & point1,const CFX_PTemplate<BaseType> & point2)147 CFX_VTemplate(const CFX_PTemplate<BaseType>& point1, 148 const CFX_PTemplate<BaseType>& point2) 149 : CFX_PTemplate<BaseType>(point2.x - point1.x, point2.y - point1.y) {} 150 151 float Length() const; 152 void Normalize(); 153 }; 154 using CFX_Vector = CFX_VTemplate<int32_t>; 155 using CFX_VectorF = CFX_VTemplate<float>; 156 157 // Rectangles. 158 // TODO(tsepez): Consolidate all these different rectangle classes. 159 160 // LTRB rectangles (y-axis runs downwards). 161 // Struct layout is compatible with win32 RECT. 162 struct FX_RECT { 163 FX_RECT() = default; FX_RECTFX_RECT164 FX_RECT(int l, int t, int r, int b) : left(l), top(t), right(r), bottom(b) {} 165 WidthFX_RECT166 int Width() const { return right - left; } HeightFX_RECT167 int Height() const { return bottom - top; } IsEmptyFX_RECT168 bool IsEmpty() const { return right <= left || bottom <= top; } 169 170 bool Valid() const; 171 172 void Normalize(); 173 void Intersect(const FX_RECT& src); IntersectFX_RECT174 void Intersect(int l, int t, int r, int b) { Intersect(FX_RECT(l, t, r, b)); } 175 FX_RECT SwappedClipBox(int width, int height, bool bFlipX, bool bFlipY) const; 176 OffsetFX_RECT177 void Offset(int dx, int dy) { 178 left += dx; 179 right += dx; 180 top += dy; 181 bottom += dy; 182 } 183 184 bool operator==(const FX_RECT& src) const { 185 return left == src.left && right == src.right && top == src.top && 186 bottom == src.bottom; 187 } 188 ContainsFX_RECT189 bool Contains(int x, int y) const { 190 return x >= left && x < right && y >= top && y < bottom; 191 } 192 193 int32_t left = 0; 194 int32_t top = 0; 195 int32_t right = 0; 196 int32_t bottom = 0; 197 }; 198 199 // LTRB rectangles (y-axis runs upwards). 200 class CFX_FloatRect { 201 public: 202 constexpr CFX_FloatRect() = default; CFX_FloatRect(float l,float b,float r,float t)203 constexpr CFX_FloatRect(float l, float b, float r, float t) 204 : left(l), bottom(b), right(r), top(t) {} 205 206 explicit CFX_FloatRect(const FX_RECT& rect); 207 explicit CFX_FloatRect(const CFX_PointF& point); 208 209 static CFX_FloatRect GetBBox(pdfium::span<const CFX_PointF> pPoints); 210 211 void Normalize(); 212 IsEmpty()213 bool IsEmpty() const { return left >= right || bottom >= top; } 214 bool Contains(const CFX_PointF& point) const; 215 bool Contains(const CFX_FloatRect& other_rect) const; 216 217 void Intersect(const CFX_FloatRect& other_rect); 218 void Union(const CFX_FloatRect& other_rect); 219 220 // These may be better at rounding than ToFxRect() and friends. 221 // 222 // Returned rect has bounds rounded up/down such that it is contained in the 223 // original. 224 FX_RECT GetInnerRect() const; 225 226 // Returned rect has bounds rounded up/down such that the original is 227 // contained in it. 228 FX_RECT GetOuterRect() const; 229 230 // Returned rect has bounds rounded up/down such that the dimensions are 231 // rounded up and the sum of the error in the bounds is minimized. 232 FX_RECT GetClosestRect() const; 233 234 CFX_FloatRect GetCenterSquare() const; 235 236 void UpdateRect(const CFX_PointF& point); 237 Width()238 float Width() const { return right - left; } Height()239 float Height() const { return top - bottom; } Left()240 float Left() const { return left; } Bottom()241 float Bottom() const { return bottom; } Right()242 float Right() const { return right; } Top()243 float Top() const { return top; } 244 245 void Inflate(float x, float y); 246 void Inflate(float other_left, 247 float other_bottom, 248 float other_right, 249 float other_top); 250 void Inflate(const CFX_FloatRect& rt); 251 252 void Deflate(float x, float y); 253 void Deflate(float other_left, 254 float other_bottom, 255 float other_right, 256 float other_top); 257 void Deflate(const CFX_FloatRect& rt); 258 259 CFX_FloatRect GetDeflated(float x, float y) const; 260 261 void Translate(float e, float f); 262 263 void Scale(float fScale); 264 void ScaleFromCenterPoint(float fScale); 265 266 // GetInnerRect() and friends may be better at rounding than these methods. 267 // Unlike the methods above, these two blindly floor / round the LBRT values. 268 // Doing so may introduce rounding errors that are visible to users as 269 // off-by-one pixels/lines. 270 // 271 // Floors LBRT values. 272 FX_RECT ToFxRect() const; 273 274 // Rounds LBRT values. 275 FX_RECT ToRoundedFxRect() const; 276 277 bool operator==(const CFX_FloatRect& other) const { 278 return left == other.left && right == other.right && top == other.top && 279 bottom == other.bottom; 280 } 281 282 float left = 0.0f; 283 float bottom = 0.0f; 284 float right = 0.0f; 285 float top = 0.0f; 286 }; 287 288 #ifndef NDEBUG 289 std::ostream& operator<<(std::ostream& os, const CFX_FloatRect& rect); 290 #endif 291 292 // LTWH rectangles (y-axis runs downwards). 293 class CFX_RectF { 294 public: 295 using PointType = CFX_PointF; 296 using SizeType = CFX_SizeF; 297 298 CFX_RectF() = default; CFX_RectF(float dst_left,float dst_top,float dst_width,float dst_height)299 CFX_RectF(float dst_left, float dst_top, float dst_width, float dst_height) 300 : left(dst_left), top(dst_top), width(dst_width), height(dst_height) {} CFX_RectF(float dst_left,float dst_top,const SizeType & dst_size)301 CFX_RectF(float dst_left, float dst_top, const SizeType& dst_size) 302 : left(dst_left), 303 top(dst_top), 304 width(dst_size.width), 305 height(dst_size.height) {} CFX_RectF(const PointType & p,float dst_width,float dst_height)306 CFX_RectF(const PointType& p, float dst_width, float dst_height) 307 : left(p.x), top(p.y), width(dst_width), height(dst_height) {} CFX_RectF(const PointType & p1,const SizeType & s2)308 CFX_RectF(const PointType& p1, const SizeType& s2) 309 : left(p1.x), top(p1.y), width(s2.width), height(s2.height) {} CFX_RectF(const FX_RECT & that)310 explicit CFX_RectF(const FX_RECT& that) 311 : left(static_cast<float>(that.left)), 312 top(static_cast<float>(that.top)), 313 width(static_cast<float>(that.Width())), 314 height(static_cast<float>(that.Height())) {} 315 316 // NOLINTNEXTLINE(runtime/explicit) 317 CFX_RectF(const CFX_RectF& other) = default; 318 319 CFX_RectF& operator=(const CFX_RectF& other) = default; 320 321 CFX_RectF& operator+=(const PointType& p) { 322 left += p.x; 323 top += p.y; 324 return *this; 325 } 326 CFX_RectF& operator-=(const PointType& p) { 327 left -= p.x; 328 top -= p.y; 329 return *this; 330 } right()331 float right() const { return left + width; } bottom()332 float bottom() const { return top + height; } Normalize()333 void Normalize() { 334 if (width < 0) { 335 left += width; 336 width = -width; 337 } 338 if (height < 0) { 339 top += height; 340 height = -height; 341 } 342 } Offset(float dx,float dy)343 void Offset(float dx, float dy) { 344 left += dx; 345 top += dy; 346 } Inflate(float x,float y)347 void Inflate(float x, float y) { 348 left -= x; 349 width += x * 2; 350 top -= y; 351 height += y * 2; 352 } Inflate(const PointType & p)353 void Inflate(const PointType& p) { Inflate(p.x, p.y); } Inflate(float off_left,float off_top,float off_right,float off_bottom)354 void Inflate(float off_left, 355 float off_top, 356 float off_right, 357 float off_bottom) { 358 left -= off_left; 359 top -= off_top; 360 width += off_left + off_right; 361 height += off_top + off_bottom; 362 } Inflate(const CFX_RectF & rt)363 void Inflate(const CFX_RectF& rt) { 364 Inflate(rt.left, rt.top, rt.left + rt.width, rt.top + rt.height); 365 } Deflate(float x,float y)366 void Deflate(float x, float y) { 367 left += x; 368 width -= x * 2; 369 top += y; 370 height -= y * 2; 371 } Deflate(const PointType & p)372 void Deflate(const PointType& p) { Deflate(p.x, p.y); } Deflate(float off_left,float off_top,float off_right,float off_bottom)373 void Deflate(float off_left, 374 float off_top, 375 float off_right, 376 float off_bottom) { 377 left += off_left; 378 top += off_top; 379 width -= off_left + off_right; 380 height -= off_top + off_bottom; 381 } Deflate(const CFX_RectF & rt)382 void Deflate(const CFX_RectF& rt) { 383 Deflate(rt.left, rt.top, rt.top + rt.width, rt.top + rt.height); 384 } IsEmpty()385 bool IsEmpty() const { return width <= 0 || height <= 0; } IsEmpty(float fEpsilon)386 bool IsEmpty(float fEpsilon) const { 387 return width <= fEpsilon || height <= fEpsilon; 388 } Empty()389 void Empty() { width = height = 0; } Contains(const PointType & p)390 bool Contains(const PointType& p) const { 391 return p.x >= left && p.x < left + width && p.y >= top && 392 p.y < top + height; 393 } Contains(const CFX_RectF & rt)394 bool Contains(const CFX_RectF& rt) const { 395 return rt.left >= left && rt.right() <= right() && rt.top >= top && 396 rt.bottom() <= bottom(); 397 } Left()398 float Left() const { return left; } Top()399 float Top() const { return top; } Width()400 float Width() const { return width; } Height()401 float Height() const { return height; } Size()402 SizeType Size() const { return SizeType(width, height); } TopLeft()403 PointType TopLeft() const { return PointType(left, top); } TopRight()404 PointType TopRight() const { return PointType(left + width, top); } BottomLeft()405 PointType BottomLeft() const { return PointType(left, top + height); } BottomRight()406 PointType BottomRight() const { 407 return PointType(left + width, top + height); 408 } Center()409 PointType Center() const { 410 return PointType(left + width / 2, top + height / 2); 411 } 412 void Union(float x, float y); Union(const PointType & p)413 void Union(const PointType& p) { Union(p.x, p.y); } 414 void Union(const CFX_RectF& rt); 415 void Intersect(const CFX_RectF& rt); IntersectWith(const CFX_RectF & rt)416 bool IntersectWith(const CFX_RectF& rt) const { 417 CFX_RectF rect = rt; 418 rect.Intersect(*this); 419 return !rect.IsEmpty(); 420 } IntersectWith(const CFX_RectF & rt,float fEpsilon)421 bool IntersectWith(const CFX_RectF& rt, float fEpsilon) const { 422 CFX_RectF rect = rt; 423 rect.Intersect(*this); 424 return !rect.IsEmpty(fEpsilon); 425 } 426 friend bool operator==(const CFX_RectF& rc1, const CFX_RectF& rc2) { 427 return rc1.left == rc2.left && rc1.top == rc2.top && 428 rc1.width == rc2.width && rc1.height == rc2.height; 429 } 430 friend bool operator!=(const CFX_RectF& rc1, const CFX_RectF& rc2) { 431 return !(rc1 == rc2); 432 } 433 ToFloatRect()434 CFX_FloatRect ToFloatRect() const { 435 // Note, we flip top/bottom here because the CFX_FloatRect has the 436 // y-axis running in the opposite direction. 437 return CFX_FloatRect(left, top, right(), bottom()); 438 } 439 440 // Returned rect has bounds rounded up/down such that the original is 441 // contained in it. 442 FX_RECT GetOuterRect() const; 443 444 float left = 0.0f; 445 float top = 0.0f; 446 float width = 0.0f; 447 float height = 0.0f; 448 }; 449 450 #ifndef NDEBUG 451 std::ostream& operator<<(std::ostream& os, const CFX_RectF& rect); 452 #endif // NDEBUG 453 454 // The matrix is of the form: 455 // | a b 0 | 456 // | c d 0 | 457 // | e f 1 | 458 // See PDF spec 1.7 Section 4.2.3. 459 // 460 class CFX_Matrix { 461 public: 462 CFX_Matrix() = default; 463 CFX_Matrix(const float n[6])464 explicit CFX_Matrix(const float n[6]) 465 : a(n[0]), b(n[1]), c(n[2]), d(n[3]), e(n[4]), f(n[5]) {} 466 CFX_Matrix(float a1,float b1,float c1,float d1,float e1,float f1)467 CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1) 468 : a(a1), b(b1), c(c1), d(d1), e(e1), f(f1) {} 469 470 CFX_Matrix(const CFX_Matrix& other) = default; 471 472 CFX_Matrix& operator=(const CFX_Matrix& other) = default; 473 474 bool operator==(const CFX_Matrix& other) const { 475 return a == other.a && b == other.b && c == other.c && d == other.d && 476 e == other.e && f == other.f; 477 } 478 bool operator!=(const CFX_Matrix& other) const { return !(*this == other); } 479 480 CFX_Matrix operator*(const CFX_Matrix& right) const { 481 return CFX_Matrix(a * right.a + b * right.c, a * right.b + b * right.d, 482 c * right.a + d * right.c, c * right.b + d * right.d, 483 e * right.a + f * right.c + right.e, 484 e * right.b + f * right.d + right.f); 485 } 486 CFX_Matrix& operator*=(const CFX_Matrix& other) { 487 *this = *this * other; 488 return *this; 489 } 490 IsIdentity()491 bool IsIdentity() const { return *this == CFX_Matrix(); } 492 CFX_Matrix GetInverse() const; 493 494 bool Is90Rotated() const; 495 bool IsScaled() const; WillScale()496 bool WillScale() const { return a != 1.0f || b != 0 || c != 0 || d != 1.0f; } 497 Concat(const CFX_Matrix & right)498 void Concat(const CFX_Matrix& right) { *this *= right; } 499 void Translate(float x, float y); 500 void TranslatePrepend(float x, float y); Translate(int32_t x,int32_t y)501 void Translate(int32_t x, int32_t y) { 502 Translate(static_cast<float>(x), static_cast<float>(y)); 503 } TranslatePrepend(int32_t x,int32_t y)504 void TranslatePrepend(int32_t x, int32_t y) { 505 TranslatePrepend(static_cast<float>(x), static_cast<float>(y)); 506 } 507 508 void Scale(float sx, float sy); 509 void Rotate(float fRadian); 510 511 void MatchRect(const CFX_FloatRect& dest, const CFX_FloatRect& src); 512 513 float GetXUnit() const; 514 float GetYUnit() const; 515 CFX_FloatRect GetUnitRect() const; 516 517 float TransformXDistance(float dx) const; 518 float TransformDistance(float distance) const; 519 520 CFX_PointF Transform(const CFX_PointF& point) const; 521 522 CFX_RectF TransformRect(const CFX_RectF& rect) const; 523 CFX_FloatRect TransformRect(const CFX_FloatRect& rect) const; 524 525 float a = 1.0f; 526 float b = 0.0f; 527 float c = 0.0f; 528 float d = 1.0f; 529 float e = 0.0f; 530 float f = 0.0f; 531 }; 532 533 #endif // CORE_FXCRT_FX_COORDINATES_H_ 534