Bumping manifests a=b2g-bump
[gecko.git] / gfx / 2d / Rect.h
blobdb0f45d373b67e908e3b411c7bc10f099a51b7bb
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_
9 #include "BaseRect.h"
10 #include "BaseMargin.h"
11 #include "Point.h"
12 #include "Tools.h"
14 #include <cmath>
16 namespace mozilla {
18 template <typename> struct IsPixel;
20 namespace gfx {
22 template<class units>
23 struct IntMarginTyped:
24 public BaseMargin<int32_t, IntMarginTyped<units> >,
25 public 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;
37 template<class units>
38 struct MarginTyped:
39 public BaseMargin<Float, MarginTyped<units> >,
40 public 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;
55 template<class units>
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)));
64 template<class units>
65 struct IntRectTyped :
66 public BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> >,
67 public 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) :
75 Super(aPos, 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.
80 void Round() {}
81 void RoundIn() {}
82 void RoundOut() {}
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 bool Overflows() const {
96 CheckedInt<int32_t> xMost = this->x;
97 xMost += this->width;
98 CheckedInt<int32_t> yMost = this->y;
99 yMost += this->height;
100 return !xMost.isValid() || !yMost.isValid();
103 typedef IntRectTyped<UnknownUnits> IntRect;
105 template<class units>
106 struct RectTyped :
107 public BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> >,
108 public units {
109 static_assert(IsPixel<units>::value,
110 "'units' must be a coordinate system tag");
112 typedef BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> > Super;
114 RectTyped() : Super() {}
115 RectTyped(const PointTyped<units>& aPos, const SizeTyped<units>& aSize) :
116 Super(aPos, aSize) {}
117 RectTyped(Float _x, Float _y, Float _width, Float _height) :
118 Super(_x, _y, _width, _height) {}
119 explicit RectTyped(const IntRectTyped<units>& rect) :
120 Super(float(rect.x), float(rect.y),
121 float(rect.width), float(rect.height)) {}
123 void NudgeToIntegers()
125 NudgeToInteger(&(this->x));
126 NudgeToInteger(&(this->y));
127 NudgeToInteger(&(this->width));
128 NudgeToInteger(&(this->height));
131 bool ToIntRect(IntRectTyped<units> *aOut) const
133 *aOut = IntRectTyped<units>(int32_t(this->X()), int32_t(this->Y()),
134 int32_t(this->Width()), int32_t(this->Height()));
135 return RectTyped<units>(Float(aOut->x), Float(aOut->y),
136 Float(aOut->width), Float(aOut->height))
137 .IsEqualEdges(*this);
140 // XXX When all of the code is ported, the following functions to convert to and from
141 // unknown types should be removed.
143 static RectTyped<units> FromUnknownRect(const RectTyped<UnknownUnits>& rect) {
144 return RectTyped<units>(rect.x, rect.y, rect.width, rect.height);
147 RectTyped<UnknownUnits> ToUnknownRect() const {
148 return RectTyped<UnknownUnits>(this->x, this->y, this->width, this->height);
151 // This is here only to keep IPDL-generated code happy. DO NOT USE.
152 bool operator==(const RectTyped<units>& aRect) const
154 return RectTyped<units>::IsEqualEdges(aRect);
157 typedef RectTyped<UnknownUnits> Rect;
159 template<class units>
160 IntRectTyped<units> RoundedToInt(const RectTyped<units>& aRect)
162 return IntRectTyped<units>(int32_t(floorf(aRect.x + 0.5f)),
163 int32_t(floorf(aRect.y + 0.5f)),
164 int32_t(floorf(aRect.width + 0.5f)),
165 int32_t(floorf(aRect.height + 0.5f)));
168 template<class units>
169 IntRectTyped<units> RoundedIn(const RectTyped<units>& aRect)
171 RectTyped<units> copy(aRect);
172 copy.RoundIn();
173 return IntRectTyped<units>(int32_t(copy.x),
174 int32_t(copy.y),
175 int32_t(copy.width),
176 int32_t(copy.height));
179 template<class units>
180 IntRectTyped<units> RoundedOut(const RectTyped<units>& aRect)
182 RectTyped<units> copy(aRect);
183 copy.RoundOut();
184 return IntRectTyped<units>(int32_t(copy.x),
185 int32_t(copy.y),
186 int32_t(copy.width),
187 int32_t(copy.height));
193 #endif /* MOZILLA_GFX_RECT_H_ */