1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #ifndef _FXCRT_COORDINATES_
8 #define _FXCRT_COORDINATES_
12 template<class baseType> class CFX_PSVTemplate;
13 template<class baseType> class CFX_VTemplate;
14 template<class baseType> class CFX_PRLTemplate;
15 template<class baseType> class CFX_RTemplate;
16 template<class baseType> class CFX_ETemplate;
17 template<class baseType> class CFX_ATemplate;
18 template<class baseType> class CFX_RRTemplate;
20 template<class baseType>
21 class CFX_PSVTemplate : public CFX_Object
24 typedef CFX_PSVTemplate<baseType> FXT_PSV;
25 typedef CFX_PSVTemplate<baseType> FXT_POINT;
26 typedef CFX_PSVTemplate<baseType> FXT_SIZE;
27 void Set(baseType x, baseType y)
29 FXT_PSV::x = x, FXT_PSV::y = y;
31 void Set(const FXT_PSV &psv)
33 FXT_PSV::x = psv.x, FXT_PSV::y = psv.y;
35 void Add(baseType x, baseType y)
37 FXT_PSV::x += x, FXT_PSV::y += y;
39 void Subtract(baseType x, baseType y)
41 FXT_PSV::x -= x, FXT_PSV::y -= y;
45 FXT_PSV::x = FXT_PSV::y = 0;
47 FXT_PSV& operator += (const FXT_PSV &obj)
53 FXT_PSV& operator -= (const FXT_PSV &obj)
59 FXT_PSV& operator *= (baseType lamda)
65 FXT_PSV& operator /= (baseType lamda)
71 friend FX_BOOL operator == (const FXT_PSV &obj1, const FXT_PSV &obj2)
73 return obj1.x == obj2.x && obj1.y == obj2.y;
75 friend FX_BOOL operator != (const FXT_PSV &obj1, const FXT_PSV &obj2)
77 return obj1.x != obj2.x || obj1.y != obj2.y;
79 friend FXT_PSV operator + (const FXT_PSV &obj1, const FXT_PSV &obj2)
82 obj.x = obj1.x + obj2.x;
83 obj.y = obj1.y + obj2.y;
86 friend FXT_PSV operator - (const FXT_PSV &obj1, const FXT_PSV &obj2)
89 obj.x = obj1.x - obj2.x;
90 obj.y = obj1.y - obj2.y;
93 friend FXT_PSV operator * (const FXT_PSV &obj, baseType lamda)
100 friend FXT_PSV operator * (baseType lamda, const FXT_PSV &obj)
107 friend FXT_PSV operator / (const FXT_PSV &obj, baseType lamda)
116 typedef CFX_PSVTemplate<FX_INT32> CFX_Point;
117 typedef CFX_PSVTemplate<FX_FLOAT> CFX_PointF;
118 typedef CFX_PSVTemplate<FX_INT32> CFX_Size;
119 typedef CFX_PSVTemplate<FX_FLOAT> CFX_SizeF;
120 typedef CFX_ArrayTemplate<CFX_Point> CFX_Points;
121 typedef CFX_ArrayTemplate<CFX_PointF> CFX_PointsF;
122 typedef CFX_PSVTemplate<FX_INT32> * FX_LPPOINT;
123 typedef CFX_PSVTemplate<FX_FLOAT> * FX_LPPOINTF;
124 typedef CFX_PSVTemplate<FX_INT32> const * FX_LPCPOINT;
125 typedef CFX_PSVTemplate<FX_FLOAT> const * FX_LPCPOINTF;
126 #define CFX_FloatPoint CFX_PointF
127 template<class baseType>
128 class CFX_VTemplate: public CFX_PSVTemplate<baseType>
131 typedef CFX_PSVTemplate<baseType> FXT_PSV;
132 typedef CFX_PSVTemplate<baseType> FXT_POINT;
133 typedef CFX_PSVTemplate<baseType> FXT_SIZE;
134 typedef CFX_VTemplate<baseType> FXT_VECTOR;
135 void Set(baseType x, baseType y)
137 FXT_PSV::x = x, FXT_PSV::y = y;
139 void Set(const FXT_PSV &psv)
141 FXT_PSV::x = psv.x, FXT_PSV::y = psv.y;
143 void Set(const FXT_POINT &p1, const FXT_POINT &p2)
145 FXT_PSV::x = p2.x - p1.x, FXT_PSV::y = p2.y - p1.y;
149 FXT_PSV::x = FXT_PSV::y = 0;
151 baseType SquareLength() const
153 return FXT_PSV::x * FXT_PSV::x + FXT_PSV::y * FXT_PSV::y;
155 baseType Length() const
157 return FXSYS_sqrt(FXT_PSV::x * FXT_PSV::x + FXT_PSV::y * FXT_PSV::y);
161 FX_FLOAT fLen = FXSYS_sqrt(FXT_PSV::x * FXT_PSV::x + FXT_PSV::y * FXT_PSV::y);
162 if (fLen < 0.0001f) {
165 FXT_PSV::x = ((baseType)FXT_PSV::x) / fLen;
166 FXT_PSV::y = ((baseType)FXT_PSV::y) / fLen;
168 baseType DotProduct(baseType x, baseType y) const
170 return FXT_PSV::x * x + FXT_PSV::y * y;
172 baseType DotProduct(const FXT_VECTOR &v) const
174 return FXT_PSV::x * v.x + FXT_PSV::y * v.y;
176 FX_BOOL IsParallel(baseType x, baseType y) const
178 baseType t = FXT_PSV::x * y - FXT_PSV::y * x;
179 return FXSYS_fabs(t) < 0x0001f;
181 FX_BOOL IsParallel(const FXT_VECTOR &v) const
183 return IsParallel(v.x, v.y);
185 FX_BOOL IsPerpendicular(baseType x, baseType y) const
187 baseType t = DotProduct(x, y);
188 return FXSYS_fabs(t) < 0x0001f;
190 FX_BOOL IsPerpendicular(const FXT_VECTOR &v) const
192 return IsPerpendicular(v.x, v.y);
194 void Translate(baseType dx, baseType dy)
196 FXT_PSV::x += dx, FXT_PSV::y += dy;
198 void Scale(baseType sx, baseType sy)
200 FXT_PSV::x *= sx, FXT_PSV::y *= sy;
202 void Rotate(FX_FLOAT fRadian)
204 FX_FLOAT xx = (FX_FLOAT)FXT_PSV::x;
205 FX_FLOAT yy = (FX_FLOAT)FXT_PSV::y;
206 FX_FLOAT cosValue = FXSYS_cos(fRadian);
207 FX_FLOAT sinValue = FXSYS_sin(fRadian);
208 FXT_PSV::x = xx * cosValue - yy * sinValue;
209 FXT_PSV::y = xx * sinValue + yy * cosValue;
211 friend FX_FLOAT Cosine(const FXT_VECTOR &v1, const FXT_VECTOR &v2)
213 FXSYS_assert(v1.SquareLength() != 0 && v2.SquareLength() != 0);
214 FX_FLOAT dotProduct = v1.DotProduct(v2);
215 return dotProduct / (FX_FLOAT)FXSYS_sqrt(v1.SquareLength() * v2.SquareLength());
217 friend FX_FLOAT ArcCosine(const FXT_VECTOR &v1, const FXT_VECTOR &v2)
219 return (FX_FLOAT)FXSYS_acos(Cosine(v1, v2));
221 friend FX_FLOAT SlopeAngle(const FXT_VECTOR &v)
225 FX_FLOAT fSlope = ArcCosine(v, vx);
226 return v.y < 0 ? -fSlope : fSlope;
229 typedef CFX_VTemplate<FX_INT32> CFX_Vector;
230 typedef CFX_VTemplate<FX_FLOAT> CFX_VectorF;
231 template<class baseType>
232 class CFX_RTemplate: public CFX_Object
235 typedef CFX_PSVTemplate<baseType> FXT_POINT;
236 typedef CFX_PSVTemplate<baseType> FXT_SIZE;
237 typedef CFX_VTemplate<baseType> FXT_VECTOR;
238 typedef CFX_PRLTemplate<baseType> FXT_PARAL;
239 typedef CFX_RTemplate<baseType> FXT_RECT;
240 void Set(baseType left, baseType top, baseType width, baseType height)
242 FXT_RECT::left = left, FXT_RECT::top = top, FXT_RECT::width = width, FXT_RECT::height = height;
244 void Set(baseType left, baseType top, const FXT_SIZE &size)
246 FXT_RECT::left = left, FXT_RECT::top = top, FXT_RECT::Size(size);
248 void Set(const FXT_POINT &p, baseType width, baseType height)
250 TopLeft(p), FXT_RECT::width = width, FXT_RECT::height = height;
252 void Set(const FXT_POINT &p1, const FXT_POINT &p2)
254 TopLeft(p1), FXT_RECT::width = p2.x - p1.x, FXT_RECT::height = p2.y - p1.y, FXT_RECT::Normalize();
256 void Set(const FXT_POINT &p, const FXT_VECTOR &v)
258 TopLeft(p), FXT_RECT::width = v.x, FXT_RECT::height = v.y, FXT_RECT::Normalize();
262 FXT_RECT::left = FXT_RECT::top = FXT_RECT::width = FXT_RECT::height = 0;
264 FXT_RECT& operator += (const FXT_POINT &p)
266 left += p.x, top += p.y;
269 FXT_RECT& operator -= (const FXT_POINT &p)
271 left -= p.x, top -= p.y;
274 baseType right() const
278 baseType bottom() const
293 void Offset(baseType dx, baseType dy)
298 void Inflate(baseType x, baseType y)
305 void Inflate(const FXT_POINT &p)
309 void Inflate(baseType left, baseType top, baseType right, baseType bottom)
311 FXT_RECT::left -= left;
312 FXT_RECT::top -= top;
313 FXT_RECT::width += left + right;
314 FXT_RECT::height += top + bottom;
316 void Inflate(const FXT_RECT &rt)
318 Inflate(rt.left, rt.top, rt.left + rt.width, rt.top + rt.height);
320 void Deflate(baseType x, baseType y)
327 void Deflate(const FXT_POINT &p)
331 void Deflate(baseType left, baseType top, baseType right, baseType bottom)
333 FXT_RECT::left += left;
334 FXT_RECT::top += top;
335 FXT_RECT::width -= left + right;
336 FXT_RECT::height -= top + bottom;
338 void Deflate(const FXT_RECT &rt)
340 Deflate(rt.left, rt.top, rt.top + rt.width, rt.top + rt.height);
342 FX_BOOL IsEmpty() const
344 return width <= 0 || height <= 0;
346 FX_BOOL IsEmpty(FX_FLOAT fEpsilon) const
348 return width <= fEpsilon || height <= fEpsilon;
354 FX_BOOL Contains(baseType x, baseType y) const
356 return x >= left && x < left + width && y >= top && y < top + height;
358 FX_BOOL Contains(const FXT_POINT &p) const
360 return Contains(p.x, p.y);
362 FX_BOOL Contains(const FXT_RECT &rt) const
364 return rt.left >= left && rt.right() <= right() && rt.top >= top && rt.bottom() <= bottom();
366 baseType Width() const
370 baseType Height() const
374 FXT_SIZE Size() const
377 size.Set(width, height);
380 void Size(FXT_SIZE s)
382 width = s.x, height = s.y;
384 FXT_POINT TopLeft() const
391 FXT_POINT TopRight() const
398 FXT_POINT BottomLeft() const
405 FXT_POINT BottomRight() const
412 void TopLeft(FXT_POINT tl)
417 void TopRight(FXT_POINT tr)
422 void BottomLeft(FXT_POINT bl)
427 void BottomRight(FXT_POINT br)
432 FXT_POINT Center() const
435 p.x = left + width / 2;
436 p.y = top + height / 2;
439 void GetParallelogram(FXT_PARAL &pg) const
441 pg.x = left, pg.y = top;
442 pg.x1 = width, pg.y1 = 0;
443 pg.x2 = 0, pg.y2 = height;
445 void Union(baseType x, baseType y)
447 baseType r = right(), b = bottom();
463 void Union(const FXT_POINT &p)
467 void Union(const FXT_RECT &rt)
469 baseType r = right(), b = bottom();
470 if (left > rt.left) {
473 if (r < rt.right()) {
479 if (b < rt.bottom()) {
485 void Intersect(const FXT_RECT &rt)
487 baseType r = right(), b = bottom();
488 if (left < rt.left) {
491 if (r > rt.right()) {
497 if (b > rt.bottom()) {
503 FX_BOOL IntersectWith(const FXT_RECT &rt) const
506 rect.Intersect(*this);
507 return !rect.IsEmpty();
509 FX_BOOL IntersectWith(const FXT_RECT &rt, FX_FLOAT fEpsilon) const
512 rect.Intersect(*this);
513 return !rect.IsEmpty(fEpsilon);
515 friend FX_BOOL operator == (const FXT_RECT &rc1, const FXT_RECT &rc2)
517 return rc1.left == rc2.left && rc1.top == rc2.top && rc1.width == rc2.width && rc1.height == rc2.height;
519 friend FX_BOOL operator != (const FXT_RECT &rc1, const FXT_RECT &rc2)
521 return rc1.left != rc2.left || rc1.top != rc2.top || rc1.width != rc2.width || rc1.height != rc2.height;
524 baseType width, height;
526 typedef CFX_RTemplate<FX_INT32> CFX_Rect;
527 typedef CFX_RTemplate<FX_FLOAT> CFX_RectF;
528 typedef CFX_RTemplate<FX_INT32> * FX_LPRECT;
529 typedef CFX_RTemplate<FX_FLOAT> * FX_LPRECTF;
530 typedef CFX_RTemplate<FX_INT32> const * FX_LPCRECT;
531 typedef CFX_RTemplate<FX_FLOAT> const * FX_LPCRECTF;
532 typedef CFX_ArrayTemplate<CFX_RectF> CFX_RectFArray;
543 FX_RECT(): left(0), top(0), right(0), bottom(0) { }
545 FX_RECT(int left1, int top1, int right1, int bottom1)
563 FX_BOOL IsEmpty() const
565 return right <= left || bottom <= top;
570 void Intersect(const FX_RECT& src);
572 void Intersect(int left1, int top1, int right1, int bottom1)
574 Intersect(FX_RECT(left1, top1, right1, bottom1));
577 void Union(const FX_RECT& other_rect);
579 FX_BOOL operator == (const FX_RECT& src) const
581 return left == src.left && right == src.right && top == src.top && bottom == src.bottom;
584 void Offset(int dx, int dy)
592 FX_BOOL Contains(const FX_RECT& other_rect) const
594 return other_rect.left >= left && other_rect.right <= right && other_rect.top >= top && other_rect.bottom <= bottom;
597 FX_BOOL Contains(int x, int y) const
599 return x >= left && x < right && y >= top && y < bottom;
602 struct FX_SMALL_RECT {
612 class CFX_FloatRect : public CFX_Object
618 left = right = bottom = top = 0;
621 CFX_FloatRect(FX_FLOAT left1, FX_FLOAT bottom1, FX_FLOAT right1, FX_FLOAT top1)
629 CFX_FloatRect(const FX_FLOAT* pArray)
637 CFX_FloatRect(const FX_RECT& rect);
639 FX_BOOL IsEmpty() const
641 return left >= right || bottom >= top;
648 left = right = bottom = top = 0;
651 FX_BOOL Contains(const CFX_FloatRect& other_rect) const;
653 FX_BOOL Contains(FX_FLOAT x, FX_FLOAT y) const;
655 void Transform(const CFX_Matrix* pMatrix);
657 void Intersect(const CFX_FloatRect& other_rect);
659 void Union(const CFX_FloatRect& other_rect);
661 FX_RECT GetInnerRect() const;
663 FX_RECT GetOutterRect() const;
665 FX_RECT GetClosestRect() const;
667 int Substract4(CFX_FloatRect& substract_rect, CFX_FloatRect* pRects);
669 void InitRect(FX_FLOAT x, FX_FLOAT y)
675 void UpdateRect(FX_FLOAT x, FX_FLOAT y);
677 FX_FLOAT Width() const
682 FX_FLOAT Height() const
687 void Inflate(FX_FLOAT x, FX_FLOAT y)
696 void Inflate(FX_FLOAT left, FX_FLOAT bottom, FX_FLOAT right, FX_FLOAT top)
700 this->bottom -= bottom;
701 this->right += right;
705 void Inflate(const CFX_FloatRect &rt)
707 Inflate(rt.left, rt.bottom, rt.right, rt.top);
710 void Deflate(FX_FLOAT x, FX_FLOAT y)
719 void Deflate(FX_FLOAT left, FX_FLOAT bottom, FX_FLOAT right, FX_FLOAT top)
723 this->bottom += bottom;
724 this->right -= right;
728 void Deflate(const CFX_FloatRect &rt)
730 Deflate(rt.left, rt.bottom, rt.right, rt.top);
733 void Translate(FX_FLOAT e, FX_FLOAT f)
741 static CFX_FloatRect GetBBox(const CFX_FloatPoint* pPoints, int nPoints);
751 class CFX_Matrix : public CFX_Object
761 CFX_Matrix(FX_FLOAT a1, FX_FLOAT b1, FX_FLOAT c1, FX_FLOAT d1, FX_FLOAT e1, FX_FLOAT f1)
771 void Set(FX_FLOAT a, FX_FLOAT b, FX_FLOAT c, FX_FLOAT d, FX_FLOAT e, FX_FLOAT f);
772 void Set(const FX_FLOAT n[6]);
780 void SetReverse(const CFX_Matrix &m);
782 void Concat(FX_FLOAT a, FX_FLOAT b, FX_FLOAT c, FX_FLOAT d, FX_FLOAT e, FX_FLOAT f, FX_BOOL bPrepended = FALSE);
784 void Concat(const CFX_Matrix &m, FX_BOOL bPrepended = FALSE);
786 void ConcatInverse(const CFX_Matrix& m, FX_BOOL bPrepended = FALSE);
792 void Copy(const CFX_Matrix& m)
797 FX_BOOL IsIdentity() const
799 return a == 1 && b == 0 && c == 0 && d == 1 && e == 0 && f == 0;
801 FX_BOOL IsInvertible() const;
803 FX_BOOL Is90Rotated() const;
805 FX_BOOL IsScaled() const;
807 void Translate(FX_FLOAT x, FX_FLOAT y, FX_BOOL bPrepended = FALSE);
809 void TranslateI(FX_INT32 x, FX_INT32 y, FX_BOOL bPrepended = FALSE)
811 Translate((FX_FLOAT)x, (FX_FLOAT)y, bPrepended);
814 void Scale(FX_FLOAT sx, FX_FLOAT sy, FX_BOOL bPrepended = FALSE);
816 void Rotate(FX_FLOAT fRadian, FX_BOOL bPrepended = FALSE);
818 void RotateAt(FX_FLOAT fRadian, FX_FLOAT x, FX_FLOAT y, FX_BOOL bPrepended = FALSE);
820 void Shear(FX_FLOAT fAlphaRadian, FX_FLOAT fBetaRadian, FX_BOOL bPrepended = FALSE);
822 void MatchRect(const CFX_FloatRect &dest, const CFX_FloatRect &src);
824 FX_FLOAT GetXUnit() const;
826 FX_FLOAT GetYUnit() const;
827 void GetUnitRect(CFX_RectF &rect) const;
829 CFX_FloatRect GetUnitRect() const;
831 FX_FLOAT GetUnitArea() const;
832 FX_FLOAT TransformXDistance(FX_FLOAT dx) const;
833 FX_INT32 TransformXDistance(FX_INT32 dx) const;
834 FX_FLOAT TransformYDistance(FX_FLOAT dy) const;
835 FX_INT32 TransformYDistance(FX_INT32 dy) const;
836 FX_FLOAT TransformDistance(FX_FLOAT dx, FX_FLOAT dy) const;
837 FX_INT32 TransformDistance(FX_INT32 dx, FX_INT32 dy) const;
839 FX_FLOAT TransformDistance(FX_FLOAT distance) const;
840 void TransformPoint(FX_FLOAT &x, FX_FLOAT &y) const;
841 void TransformPoint(FX_INT32 &x, FX_INT32 &y) const;
842 void TransformPoints(CFX_PointF *points, FX_INT32 iCount) const;
843 void TransformPoints(CFX_Point *points, FX_INT32 iCount) const;
845 void Transform(FX_FLOAT& x, FX_FLOAT& y) const
847 TransformPoint(x, y);
850 void Transform(FX_FLOAT x, FX_FLOAT y, FX_FLOAT& x1, FX_FLOAT& y1) const
853 TransformPoint(x1, y1);
855 void TransformVector(CFX_VectorF &v) const;
856 void TransformVector(CFX_Vector &v) const;
857 void TransformRect(CFX_RectF &rect) const;
858 void TransformRect(CFX_Rect &rect) const;
860 void TransformRect(FX_FLOAT& left, FX_FLOAT& right, FX_FLOAT& top, FX_FLOAT& bottom) const;
862 void TransformRect(CFX_FloatRect& rect) const
864 TransformRect(rect.left, rect.right, rect.top, rect.bottom);
867 FX_FLOAT GetA() const
872 FX_FLOAT GetB() const
877 FX_FLOAT GetC() const
882 FX_FLOAT GetD() const
887 FX_FLOAT GetE() const
892 FX_FLOAT GetF() const
904 #define CFX_AffineMatrix CFX_Matrix