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/. */
6 #ifndef MOZILLA_GFX_RECT_H_
7 #define MOZILLA_GFX_RECT_H_
10 #include "BaseMargin.h"
18 template <typename
> struct IsPixel
;
23 struct IntMarginTyped
:
24 public BaseMargin
<int32_t, IntMarginTyped
<units
> >,
26 static_assert(IsPixel
<units
>::value
,
27 "'units' must be a coordinate system tag");
29 typedef BaseMargin
<int32_t, IntMarginTyped
<units
> > Super
;
31 IntMarginTyped() : Super() {}
32 IntMarginTyped(int32_t aTop
, int32_t aRight
, int32_t aBottom
, int32_t aLeft
) :
33 Super(aTop
, aRight
, aBottom
, aLeft
) {}
35 typedef IntMarginTyped
<UnknownUnits
> IntMargin
;
39 public BaseMargin
<Float
, MarginTyped
<units
> >,
41 static_assert(IsPixel
<units
>::value
,
42 "'units' must be a coordinate system tag");
44 typedef BaseMargin
<Float
, MarginTyped
<units
> > Super
;
46 MarginTyped() : Super() {}
47 MarginTyped(Float aTop
, Float aRight
, Float aBottom
, Float aLeft
) :
48 Super(aTop
, aRight
, aBottom
, aLeft
) {}
49 explicit MarginTyped(const IntMarginTyped
<units
>& aMargin
) :
50 Super(float(aMargin
.top
), float(aMargin
.right
),
51 float(aMargin
.bottom
), float(aMargin
.left
)) {}
53 typedef MarginTyped
<UnknownUnits
> Margin
;
56 IntMarginTyped
<units
> RoundedToInt(const MarginTyped
<units
>& aMargin
)
58 return IntMarginTyped
<units
>(int32_t(floorf(aMargin
.top
+ 0.5f
)),
59 int32_t(floorf(aMargin
.right
+ 0.5f
)),
60 int32_t(floorf(aMargin
.bottom
+ 0.5f
)),
61 int32_t(floorf(aMargin
.left
+ 0.5f
)));
66 public BaseRect
<int32_t, IntRectTyped
<units
>, IntPointTyped
<units
>, IntSizeTyped
<units
>, IntMarginTyped
<units
> >,
68 static_assert(IsPixel
<units
>::value
,
69 "'units' must be a coordinate system tag");
71 typedef BaseRect
<int32_t, IntRectTyped
<units
>, IntPointTyped
<units
>, IntSizeTyped
<units
>, IntMarginTyped
<units
> > Super
;
73 IntRectTyped() : Super() {}
74 IntRectTyped(const IntPointTyped
<units
>& aPos
, const IntSizeTyped
<units
>& aSize
) :
76 IntRectTyped(int32_t _x
, int32_t _y
, int32_t _width
, int32_t _height
) :
77 Super(_x
, _y
, _width
, _height
) {}
79 // Rounding isn't meaningful on an integer rectangle.
84 // XXX When all of the code is ported, the following functions to convert to and from
85 // unknown types should be removed.
87 static IntRectTyped
<units
> FromUnknownRect(const IntRectTyped
<UnknownUnits
>& rect
) {
88 return IntRectTyped
<units
>(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
91 IntRectTyped
<UnknownUnits
> ToUnknownRect() const {
92 return IntRectTyped
<UnknownUnits
>(this->x
, this->y
, this->width
, this->height
);
95 typedef IntRectTyped
<UnknownUnits
> IntRect
;
99 public BaseRect
<Float
, RectTyped
<units
>, PointTyped
<units
>, SizeTyped
<units
>, MarginTyped
<units
> >,
101 static_assert(IsPixel
<units
>::value
,
102 "'units' must be a coordinate system tag");
104 typedef BaseRect
<Float
, RectTyped
<units
>, PointTyped
<units
>, SizeTyped
<units
>, MarginTyped
<units
> > Super
;
106 RectTyped() : Super() {}
107 RectTyped(const PointTyped
<units
>& aPos
, const SizeTyped
<units
>& aSize
) :
108 Super(aPos
, aSize
) {}
109 RectTyped(Float _x
, Float _y
, Float _width
, Float _height
) :
110 Super(_x
, _y
, _width
, _height
) {}
111 explicit RectTyped(const IntRectTyped
<units
>& rect
) :
112 Super(float(rect
.x
), float(rect
.y
),
113 float(rect
.width
), float(rect
.height
)) {}
115 void NudgeToIntegers()
117 NudgeToInteger(&(this->x
));
118 NudgeToInteger(&(this->y
));
119 NudgeToInteger(&(this->width
));
120 NudgeToInteger(&(this->height
));
123 bool ToIntRect(IntRectTyped
<units
> *aOut
) const
125 *aOut
= IntRectTyped
<units
>(int32_t(this->X()), int32_t(this->Y()),
126 int32_t(this->Width()), int32_t(this->Height()));
127 return RectTyped
<units
>(Float(aOut
->x
), Float(aOut
->y
),
128 Float(aOut
->width
), Float(aOut
->height
))
129 .IsEqualEdges(*this);
132 // XXX When all of the code is ported, the following functions to convert to and from
133 // unknown types should be removed.
135 static RectTyped
<units
> FromUnknownRect(const RectTyped
<UnknownUnits
>& rect
) {
136 return RectTyped
<units
>(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
139 RectTyped
<UnknownUnits
> ToUnknownRect() const {
140 return RectTyped
<UnknownUnits
>(this->x
, this->y
, this->width
, this->height
);
143 // This is here only to keep IPDL-generated code happy. DO NOT USE.
144 bool operator==(const RectTyped
<units
>& aRect
) const
146 return RectTyped
<units
>::IsEqualEdges(aRect
);
149 typedef RectTyped
<UnknownUnits
> Rect
;
151 template<class units
>
152 IntRectTyped
<units
> RoundedToInt(const RectTyped
<units
>& aRect
)
154 return IntRectTyped
<units
>(int32_t(floorf(aRect
.x
+ 0.5f
)),
155 int32_t(floorf(aRect
.y
+ 0.5f
)),
156 int32_t(floorf(aRect
.width
+ 0.5f
)),
157 int32_t(floorf(aRect
.height
+ 0.5f
)));
160 template<class units
>
161 IntRectTyped
<units
> RoundedIn(const RectTyped
<units
>& aRect
)
163 RectTyped
<units
> copy(aRect
);
165 return IntRectTyped
<units
>(int32_t(copy
.x
),
168 int32_t(copy
.height
));
171 template<class units
>
172 IntRectTyped
<units
> RoundedOut(const RectTyped
<units
>& aRect
)
174 RectTyped
<units
> copy(aRect
);
176 return IntRectTyped
<units
>(int32_t(copy
.x
),
179 int32_t(copy
.height
));
185 #endif /* MOZILLA_GFX_RECT_H_ */