Bug 1073336 part 14a - Update animation generation when changing animations via the...
[gecko.git] / gfx / 2d / Rect.h
blobb38f2e94fb67311d1d8da48aecd36f26dae91d66
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 typedef IntRectTyped<UnknownUnits> IntRect;
97 template<class units>
98 struct RectTyped :
99 public BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> >,
100 public 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);
164 copy.RoundIn();
165 return IntRectTyped<units>(int32_t(copy.x),
166 int32_t(copy.y),
167 int32_t(copy.width),
168 int32_t(copy.height));
171 template<class units>
172 IntRectTyped<units> RoundedOut(const RectTyped<units>& aRect)
174 RectTyped<units> copy(aRect);
175 copy.RoundOut();
176 return IntRectTyped<units>(int32_t(copy.x),
177 int32_t(copy.y),
178 int32_t(copy.width),
179 int32_t(copy.height));
185 #endif /* MOZILLA_GFX_RECT_H_ */