|
1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef MOZILLA_GFX_RECT_H_ |
|
7 #define MOZILLA_GFX_RECT_H_ |
|
8 |
|
9 #include "BaseRect.h" |
|
10 #include "BaseMargin.h" |
|
11 #include "Point.h" |
|
12 #include "Tools.h" |
|
13 |
|
14 #include <cmath> |
|
15 |
|
16 namespace mozilla { |
|
17 namespace gfx { |
|
18 |
|
19 template<class units> |
|
20 struct IntMarginTyped: |
|
21 public BaseMargin<int32_t, IntMarginTyped<units> >, |
|
22 public units { |
|
23 typedef BaseMargin<int32_t, IntMarginTyped<units> > Super; |
|
24 |
|
25 IntMarginTyped() : Super() {} |
|
26 IntMarginTyped(int32_t aTop, int32_t aRight, int32_t aBottom, int32_t aLeft) : |
|
27 Super(aTop, aRight, aBottom, aLeft) {} |
|
28 }; |
|
29 typedef IntMarginTyped<UnknownUnits> IntMargin; |
|
30 |
|
31 template<class units> |
|
32 struct MarginTyped: |
|
33 public BaseMargin<Float, MarginTyped<units> >, |
|
34 public units { |
|
35 typedef BaseMargin<Float, MarginTyped<units> > Super; |
|
36 |
|
37 MarginTyped() : Super() {} |
|
38 MarginTyped(Float aTop, Float aRight, Float aBottom, Float aLeft) : |
|
39 Super(aTop, aRight, aBottom, aLeft) {} |
|
40 explicit MarginTyped(const IntMarginTyped<units>& aMargin) : |
|
41 Super(float(aMargin.top), float(aMargin.right), |
|
42 float(aMargin.bottom), float(aMargin.left)) {} |
|
43 }; |
|
44 typedef MarginTyped<UnknownUnits> Margin; |
|
45 |
|
46 template<class units> |
|
47 IntMarginTyped<units> RoundedToInt(const MarginTyped<units>& aMargin) |
|
48 { |
|
49 return IntMarginTyped<units>(int32_t(floorf(aMargin.top + 0.5f)), |
|
50 int32_t(floorf(aMargin.right + 0.5f)), |
|
51 int32_t(floorf(aMargin.bottom + 0.5f)), |
|
52 int32_t(floorf(aMargin.left + 0.5f))); |
|
53 } |
|
54 |
|
55 template<class units> |
|
56 struct IntRectTyped : |
|
57 public BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> >, |
|
58 public units { |
|
59 typedef BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> > Super; |
|
60 |
|
61 IntRectTyped() : Super() {} |
|
62 IntRectTyped(const IntPointTyped<units>& aPos, const IntSizeTyped<units>& aSize) : |
|
63 Super(aPos, aSize) {} |
|
64 IntRectTyped(int32_t _x, int32_t _y, int32_t _width, int32_t _height) : |
|
65 Super(_x, _y, _width, _height) {} |
|
66 |
|
67 // Rounding isn't meaningful on an integer rectangle. |
|
68 void Round() {} |
|
69 void RoundIn() {} |
|
70 void RoundOut() {} |
|
71 |
|
72 // XXX When all of the code is ported, the following functions to convert to and from |
|
73 // unknown types should be removed. |
|
74 |
|
75 static IntRectTyped<units> FromUnknownRect(const IntRectTyped<UnknownUnits>& rect) { |
|
76 return IntRectTyped<units>(rect.x, rect.y, rect.width, rect.height); |
|
77 } |
|
78 |
|
79 IntRectTyped<UnknownUnits> ToUnknownRect() const { |
|
80 return IntRectTyped<UnknownUnits>(this->x, this->y, this->width, this->height); |
|
81 } |
|
82 }; |
|
83 typedef IntRectTyped<UnknownUnits> IntRect; |
|
84 |
|
85 template<class units> |
|
86 struct RectTyped : |
|
87 public BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> >, |
|
88 public units { |
|
89 typedef BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> > Super; |
|
90 |
|
91 RectTyped() : Super() {} |
|
92 RectTyped(const PointTyped<units>& aPos, const SizeTyped<units>& aSize) : |
|
93 Super(aPos, aSize) {} |
|
94 RectTyped(Float _x, Float _y, Float _width, Float _height) : |
|
95 Super(_x, _y, _width, _height) {} |
|
96 explicit RectTyped(const IntRectTyped<units>& rect) : |
|
97 Super(float(rect.x), float(rect.y), |
|
98 float(rect.width), float(rect.height)) {} |
|
99 |
|
100 void NudgeToIntegers() |
|
101 { |
|
102 NudgeToInteger(&(this->x)); |
|
103 NudgeToInteger(&(this->y)); |
|
104 NudgeToInteger(&(this->width)); |
|
105 NudgeToInteger(&(this->height)); |
|
106 } |
|
107 |
|
108 bool ToIntRect(IntRectTyped<units> *aOut) const |
|
109 { |
|
110 *aOut = IntRectTyped<units>(int32_t(this->X()), int32_t(this->Y()), |
|
111 int32_t(this->Width()), int32_t(this->Height())); |
|
112 return RectTyped<units>(Float(aOut->x), Float(aOut->y), |
|
113 Float(aOut->width), Float(aOut->height)) |
|
114 .IsEqualEdges(*this); |
|
115 } |
|
116 |
|
117 // XXX When all of the code is ported, the following functions to convert to and from |
|
118 // unknown types should be removed. |
|
119 |
|
120 static RectTyped<units> FromUnknownRect(const RectTyped<UnknownUnits>& rect) { |
|
121 return RectTyped<units>(rect.x, rect.y, rect.width, rect.height); |
|
122 } |
|
123 |
|
124 RectTyped<UnknownUnits> ToUnknownRect() const { |
|
125 return RectTyped<UnknownUnits>(this->x, this->y, this->width, this->height); |
|
126 } |
|
127 |
|
128 // This is here only to keep IPDL-generated code happy. DO NOT USE. |
|
129 bool operator==(const RectTyped<units>& aRect) const |
|
130 { |
|
131 return RectTyped<units>::IsEqualEdges(aRect); |
|
132 } |
|
133 }; |
|
134 typedef RectTyped<UnknownUnits> Rect; |
|
135 |
|
136 template<class units> |
|
137 IntRectTyped<units> RoundedToInt(const RectTyped<units>& aRect) |
|
138 { |
|
139 return IntRectTyped<units>(int32_t(floorf(aRect.x + 0.5f)), |
|
140 int32_t(floorf(aRect.y + 0.5f)), |
|
141 int32_t(floorf(aRect.width + 0.5f)), |
|
142 int32_t(floorf(aRect.height + 0.5f))); |
|
143 } |
|
144 |
|
145 template<class units> |
|
146 IntRectTyped<units> RoundedIn(const RectTyped<units>& aRect) |
|
147 { |
|
148 RectTyped<units> copy(aRect); |
|
149 copy.RoundIn(); |
|
150 return IntRectTyped<units>(int32_t(copy.x), |
|
151 int32_t(copy.y), |
|
152 int32_t(copy.width), |
|
153 int32_t(copy.height)); |
|
154 } |
|
155 |
|
156 template<class units> |
|
157 IntRectTyped<units> RoundedOut(const RectTyped<units>& aRect) |
|
158 { |
|
159 RectTyped<units> copy(aRect); |
|
160 copy.RoundOut(); |
|
161 return IntRectTyped<units>(int32_t(copy.x), |
|
162 int32_t(copy.y), |
|
163 int32_t(copy.width), |
|
164 int32_t(copy.height)); |
|
165 } |
|
166 |
|
167 } |
|
168 } |
|
169 |
|
170 #endif /* MOZILLA_GFX_RECT_H_ */ |