2011-06-24 21:41:16 +04:00
|
|
|
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#ifndef MOZILLA_GFX_RECT_H_
|
|
|
|
#define MOZILLA_GFX_RECT_H_
|
|
|
|
|
|
|
|
#include "BaseRect.h"
|
|
|
|
#include "BaseMargin.h"
|
2015-04-21 18:04:57 +03:00
|
|
|
#include "NumericTools.h"
|
2011-06-24 21:41:16 +04:00
|
|
|
#include "Point.h"
|
2013-05-29 17:32:30 +04:00
|
|
|
#include "Tools.h"
|
2013-08-22 13:10:35 +04:00
|
|
|
|
|
|
|
#include <cmath>
|
2011-05-26 23:41:33 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-06-10 20:43:52 +04:00
|
|
|
|
|
|
|
template <typename> struct IsPixel;
|
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
namespace gfx {
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2013-08-22 20:41:51 +04:00
|
|
|
template<class units>
|
|
|
|
struct IntMarginTyped:
|
|
|
|
public BaseMargin<int32_t, IntMarginTyped<units> >,
|
|
|
|
public units {
|
2014-06-10 20:43:52 +04:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-08-22 20:41:51 +04:00
|
|
|
typedef BaseMargin<int32_t, IntMarginTyped<units> > Super;
|
|
|
|
|
|
|
|
IntMarginTyped() : Super() {}
|
|
|
|
IntMarginTyped(int32_t aTop, int32_t aRight, int32_t aBottom, int32_t aLeft) :
|
|
|
|
Super(aTop, aRight, aBottom, aLeft) {}
|
|
|
|
};
|
|
|
|
typedef IntMarginTyped<UnknownUnits> IntMargin;
|
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
template<class units, class F = Float>
|
2013-07-22 12:50:11 +04:00
|
|
|
struct MarginTyped:
|
2015-06-11 23:43:40 +03:00
|
|
|
public BaseMargin<F, MarginTyped<units> >,
|
2013-07-22 12:50:11 +04:00
|
|
|
public units {
|
2014-06-10 20:43:52 +04:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
typedef BaseMargin<F, MarginTyped<units, F> > Super;
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2013-08-22 20:41:51 +04:00
|
|
|
MarginTyped() : Super() {}
|
2015-06-11 23:43:40 +03:00
|
|
|
MarginTyped(F aTop, F aRight, F aBottom, F aLeft) :
|
2013-07-22 12:50:11 +04:00
|
|
|
Super(aTop, aRight, aBottom, aLeft) {}
|
2013-08-22 20:41:51 +04:00
|
|
|
explicit MarginTyped(const IntMarginTyped<units>& aMargin) :
|
2015-06-11 23:43:40 +03:00
|
|
|
Super(F(aMargin.top), F(aMargin.right),
|
|
|
|
F(aMargin.bottom), F(aMargin.left)) {}
|
2011-06-24 21:41:16 +04:00
|
|
|
};
|
2013-07-22 12:50:11 +04:00
|
|
|
typedef MarginTyped<UnknownUnits> Margin;
|
2015-06-11 23:43:40 +03:00
|
|
|
typedef MarginTyped<UnknownUnits, double> MarginDouble;
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2013-09-11 21:20:43 +04:00
|
|
|
template<class units>
|
|
|
|
IntMarginTyped<units> RoundedToInt(const MarginTyped<units>& aMargin)
|
|
|
|
{
|
|
|
|
return IntMarginTyped<units>(int32_t(floorf(aMargin.top + 0.5f)),
|
|
|
|
int32_t(floorf(aMargin.right + 0.5f)),
|
|
|
|
int32_t(floorf(aMargin.bottom + 0.5f)),
|
|
|
|
int32_t(floorf(aMargin.left + 0.5f)));
|
|
|
|
}
|
|
|
|
|
2013-05-29 17:32:30 +04:00
|
|
|
template<class units>
|
|
|
|
struct IntRectTyped :
|
2013-08-22 20:41:51 +04:00
|
|
|
public BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> >,
|
2013-05-29 17:32:30 +04:00
|
|
|
public units {
|
2014-06-10 20:43:52 +04:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-08-22 20:41:51 +04:00
|
|
|
typedef BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> > Super;
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2013-05-29 17:32:30 +04:00
|
|
|
IntRectTyped() : Super() {}
|
2013-11-13 22:22:15 +04:00
|
|
|
IntRectTyped(const IntPointTyped<units>& aPos, const IntSizeTyped<units>& aSize) :
|
2011-06-24 21:41:16 +04:00
|
|
|
Super(aPos, aSize) {}
|
2013-05-29 17:32:30 +04:00
|
|
|
IntRectTyped(int32_t _x, int32_t _y, int32_t _width, int32_t _height) :
|
2011-06-24 21:41:16 +04:00
|
|
|
Super(_x, _y, _width, _height) {}
|
2011-11-14 08:29:14 +04:00
|
|
|
|
|
|
|
// Rounding isn't meaningful on an integer rectangle.
|
|
|
|
void Round() {}
|
|
|
|
void RoundIn() {}
|
|
|
|
void RoundOut() {}
|
2013-06-03 17:52:44 +04:00
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert to and from
|
|
|
|
// unknown types should be removed.
|
|
|
|
|
|
|
|
static IntRectTyped<units> FromUnknownRect(const IntRectTyped<UnknownUnits>& rect) {
|
|
|
|
return IntRectTyped<units>(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntRectTyped<UnknownUnits> ToUnknownRect() const {
|
|
|
|
return IntRectTyped<UnknownUnits>(this->x, this->y, this->width, this->height);
|
|
|
|
}
|
2014-11-29 02:11:03 +03:00
|
|
|
|
|
|
|
bool Overflows() const {
|
|
|
|
CheckedInt<int32_t> xMost = this->x;
|
|
|
|
xMost += this->width;
|
|
|
|
CheckedInt<int32_t> yMost = this->y;
|
|
|
|
yMost += this->height;
|
|
|
|
return !xMost.isValid() || !yMost.isValid();
|
|
|
|
}
|
2015-04-12 05:03:00 +03:00
|
|
|
|
|
|
|
// This is here only to keep IPDL-generated code happy. DO NOT USE.
|
|
|
|
bool operator==(const IntRectTyped<units>& aRect) const
|
|
|
|
{
|
|
|
|
return IntRectTyped<units>::IsEqualEdges(aRect);
|
|
|
|
}
|
2015-04-21 18:04:57 +03:00
|
|
|
|
|
|
|
void InflateToMultiple(const IntSizeTyped<units>& aTileSize)
|
|
|
|
{
|
|
|
|
int32_t yMost = this->YMost();
|
|
|
|
int32_t xMost = this->XMost();
|
|
|
|
|
2015-04-21 18:22:30 +03:00
|
|
|
this->x = mozilla::RoundDownToMultiple(this->x, aTileSize.width);
|
|
|
|
this->y = mozilla::RoundDownToMultiple(this->y, aTileSize.height);
|
|
|
|
xMost = mozilla::RoundUpToMultiple(xMost, aTileSize.width);
|
|
|
|
yMost = mozilla::RoundUpToMultiple(yMost, aTileSize.height);
|
2015-04-21 18:04:57 +03:00
|
|
|
|
|
|
|
this->width = xMost - this->x;
|
|
|
|
this->height = yMost - this->y;
|
|
|
|
}
|
|
|
|
|
2011-05-26 23:41:33 +04:00
|
|
|
};
|
2013-05-29 17:32:30 +04:00
|
|
|
typedef IntRectTyped<UnknownUnits> IntRect;
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
template<class units, class F = Float>
|
2013-05-29 17:32:30 +04:00
|
|
|
struct RectTyped :
|
2015-06-11 23:43:40 +03:00
|
|
|
public BaseRect<F, RectTyped<units, F>, PointTyped<units, F>, SizeTyped<units, F>, MarginTyped<units, F> >,
|
2013-05-29 17:32:30 +04:00
|
|
|
public units {
|
2014-06-10 20:43:52 +04:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
typedef BaseRect<F, RectTyped<units, F>, PointTyped<units, F>, SizeTyped<units, F>, MarginTyped<units, F> > Super;
|
2011-11-14 08:29:01 +04:00
|
|
|
|
2013-05-29 17:32:30 +04:00
|
|
|
RectTyped() : Super() {}
|
2015-06-11 23:43:40 +03:00
|
|
|
RectTyped(const PointTyped<units, F>& aPos, const SizeTyped<units, F>& aSize) :
|
2011-11-14 08:29:01 +04:00
|
|
|
Super(aPos, aSize) {}
|
2015-06-11 23:43:40 +03:00
|
|
|
RectTyped(F _x, F _y, F _width, F _height) :
|
2011-11-14 08:29:01 +04:00
|
|
|
Super(_x, _y, _width, _height) {}
|
2013-05-29 17:32:30 +04:00
|
|
|
explicit RectTyped(const IntRectTyped<units>& rect) :
|
2015-06-11 23:43:40 +03:00
|
|
|
Super(F(rect.x), F(rect.y),
|
|
|
|
F(rect.width), F(rect.height)) {}
|
2012-05-04 01:41:37 +04:00
|
|
|
|
2015-08-07 03:26:09 +03:00
|
|
|
// Returns the largest rectangle that can be represented with 32-bit
|
|
|
|
// signed integers, centered around a point at 0,0. As BaseRect's represent
|
|
|
|
// the dimensions as a top-left point with a width and height, the width
|
|
|
|
// and height will be the largest positive 32-bit value. The top-left
|
|
|
|
// position coordinate is divided by two to center the rectangle around a
|
|
|
|
// point at 0,0.
|
|
|
|
static RectTyped<units, F> MaxIntRect()
|
|
|
|
{
|
|
|
|
return RectTyped<units, F>(
|
|
|
|
-std::numeric_limits<int32_t>::max() * 0.5,
|
|
|
|
-std::numeric_limits<int32_t>::max() * 0.5,
|
|
|
|
std::numeric_limits<int32_t>::max(),
|
|
|
|
std::numeric_limits<int32_t>::max()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-02-19 06:59:34 +04:00
|
|
|
void NudgeToIntegers()
|
2013-05-29 17:32:30 +04:00
|
|
|
{
|
|
|
|
NudgeToInteger(&(this->x));
|
|
|
|
NudgeToInteger(&(this->y));
|
|
|
|
NudgeToInteger(&(this->width));
|
|
|
|
NudgeToInteger(&(this->height));
|
|
|
|
}
|
2012-09-12 09:24:09 +04:00
|
|
|
|
2013-05-29 17:32:30 +04:00
|
|
|
bool ToIntRect(IntRectTyped<units> *aOut) const
|
2012-05-04 01:41:37 +04:00
|
|
|
{
|
2013-05-29 17:32:30 +04:00
|
|
|
*aOut = IntRectTyped<units>(int32_t(this->X()), int32_t(this->Y()),
|
|
|
|
int32_t(this->Width()), int32_t(this->Height()));
|
2015-06-11 23:43:40 +03:00
|
|
|
return RectTyped<units>(F(aOut->x), F(aOut->y),
|
|
|
|
F(aOut->width), F(aOut->height))
|
2013-05-29 17:32:30 +04:00
|
|
|
.IsEqualEdges(*this);
|
2012-05-04 01:41:37 +04:00
|
|
|
}
|
2013-06-03 17:52:44 +04:00
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert to and from
|
|
|
|
// unknown types should be removed.
|
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
static RectTyped<units, F> FromUnknownRect(const RectTyped<UnknownUnits, F>& rect) {
|
|
|
|
return RectTyped<units, F>(rect.x, rect.y, rect.width, rect.height);
|
2013-06-03 17:52:44 +04:00
|
|
|
}
|
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
RectTyped<UnknownUnits, F> ToUnknownRect() const {
|
|
|
|
return RectTyped<UnknownUnits, F>(this->x, this->y, this->width, this->height);
|
2013-06-03 17:52:44 +04:00
|
|
|
}
|
2013-09-12 08:09:43 +04:00
|
|
|
|
|
|
|
// This is here only to keep IPDL-generated code happy. DO NOT USE.
|
2015-06-11 23:43:40 +03:00
|
|
|
bool operator==(const RectTyped<units, F>& aRect) const
|
2013-09-12 08:09:43 +04:00
|
|
|
{
|
2015-06-11 23:43:40 +03:00
|
|
|
return RectTyped<units, F>::IsEqualEdges(aRect);
|
2013-09-12 08:09:43 +04:00
|
|
|
}
|
2011-11-14 08:29:01 +04:00
|
|
|
};
|
2013-05-29 17:32:30 +04:00
|
|
|
typedef RectTyped<UnknownUnits> Rect;
|
2015-06-11 23:43:40 +03:00
|
|
|
typedef RectTyped<UnknownUnits, double> RectDouble;
|
2011-11-14 08:29:01 +04:00
|
|
|
|
2013-06-15 00:11:31 +04:00
|
|
|
template<class units>
|
|
|
|
IntRectTyped<units> RoundedToInt(const RectTyped<units>& aRect)
|
|
|
|
{
|
2015-05-07 21:25:06 +03:00
|
|
|
RectTyped<units> copy(aRect);
|
|
|
|
copy.Round();
|
|
|
|
return IntRectTyped<units>(int32_t(copy.x),
|
|
|
|
int32_t(copy.y),
|
|
|
|
int32_t(copy.width),
|
|
|
|
int32_t(copy.height));
|
2013-06-15 00:11:31 +04:00
|
|
|
}
|
|
|
|
|
2013-06-15 00:11:44 +04:00
|
|
|
template<class units>
|
|
|
|
IntRectTyped<units> RoundedIn(const RectTyped<units>& aRect)
|
|
|
|
{
|
|
|
|
RectTyped<units> copy(aRect);
|
|
|
|
copy.RoundIn();
|
|
|
|
return IntRectTyped<units>(int32_t(copy.x),
|
|
|
|
int32_t(copy.y),
|
|
|
|
int32_t(copy.width),
|
|
|
|
int32_t(copy.height));
|
|
|
|
}
|
|
|
|
|
2013-11-27 21:33:27 +04:00
|
|
|
template<class units>
|
|
|
|
IntRectTyped<units> RoundedOut(const RectTyped<units>& aRect)
|
|
|
|
{
|
|
|
|
RectTyped<units> copy(aRect);
|
|
|
|
copy.RoundOut();
|
|
|
|
return IntRectTyped<units>(int32_t(copy.x),
|
|
|
|
int32_t(copy.y),
|
|
|
|
int32_t(copy.width),
|
|
|
|
int32_t(copy.height));
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#endif /* MOZILLA_GFX_RECT_H_ */
|