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"
|
2016-04-21 00:16:09 +03:00
|
|
|
#include "mozilla/Maybe.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
|
|
|
|
2016-09-16 18:49:39 +03:00
|
|
|
template<class units, class F> struct RectTyped;
|
|
|
|
|
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) {}
|
2015-11-12 10:05:04 +03:00
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert
|
|
|
|
// to and from unknown types should be removed.
|
|
|
|
|
|
|
|
static IntMarginTyped<units> FromUnknownMargin(const IntMarginTyped<UnknownUnits>& aMargin) {
|
|
|
|
return IntMarginTyped<units>(aMargin.top, aMargin.right,
|
|
|
|
aMargin.bottom, aMargin.left);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntMarginTyped<UnknownUnits> ToUnknownMargin() const {
|
|
|
|
return IntMarginTyped<UnknownUnits>(this->top, this->right,
|
|
|
|
this->bottom, this->left);
|
|
|
|
}
|
2013-08-22 20:41:51 +04:00
|
|
|
};
|
|
|
|
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;
|
2016-07-20 04:50:09 +03:00
|
|
|
typedef IntRectTyped<units> Self;
|
2016-09-16 18:49:39 +03:00
|
|
|
typedef IntParam<int32_t> ToInt;
|
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) {}
|
2016-09-16 18:49:39 +03:00
|
|
|
|
|
|
|
IntRectTyped(ToInt aX, ToInt aY, ToInt aWidth, ToInt aHeight) :
|
|
|
|
Super(aX.value, aY.value, aWidth.value, aHeight.value) {}
|
|
|
|
|
|
|
|
static IntRectTyped<units> RoundIn(float aX, float aY, float aW, float aH) {
|
|
|
|
return IntRectTyped<units>::RoundIn(RectTyped<units, float>(aX, aY, aW, aH));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntRectTyped<units> RoundOut(float aX, float aY, float aW, float aH) {
|
|
|
|
return IntRectTyped<units>::RoundOut(RectTyped<units, float>(aX, aY, aW, aH));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntRectTyped<units> Round(float aX, float aY, float aW, float aH) {
|
|
|
|
return IntRectTyped<units>::Round(RectTyped<units, float>(aX, aY, aW, aH));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntRectTyped<units> Truncate(float aX, float aY, float aW, float aH) {
|
|
|
|
return IntRectTyped<units>(IntPointTyped<units>::Truncate(aX, aY),
|
|
|
|
IntSizeTyped<units>::Truncate(aW, aH));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntRectTyped<units> RoundIn(const RectTyped<units, float>& aRect) {
|
|
|
|
auto tmp(aRect);
|
|
|
|
tmp.RoundIn();
|
|
|
|
return IntRectTyped(int32_t(tmp.x), int32_t(tmp.y),
|
|
|
|
int32_t(tmp.width), int32_t(tmp.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntRectTyped<units> RoundOut(const RectTyped<units, float>& aRect) {
|
|
|
|
auto tmp(aRect);
|
|
|
|
tmp.RoundOut();
|
|
|
|
return IntRectTyped(int32_t(tmp.x), int32_t(tmp.y),
|
|
|
|
int32_t(tmp.width), int32_t(tmp.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntRectTyped<units> Round(const RectTyped<units, float>& aRect) {
|
|
|
|
auto tmp(aRect);
|
|
|
|
tmp.Round();
|
|
|
|
return IntRectTyped(int32_t(tmp.x), int32_t(tmp.y),
|
|
|
|
int32_t(tmp.width), int32_t(tmp.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntRectTyped<units> Truncate(const RectTyped<units, float>& aRect) {
|
|
|
|
return IntRectTyped::Truncate(aRect.x, aRect.y, aRect.width, aRect.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
|
|
|
|
2015-11-12 10:05:04 +03:00
|
|
|
// XXX When all of the code is ported, the following functions to convert
|
|
|
|
// to and from unknown types should be removed.
|
2013-06-03 17:52:44 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-07-20 04:50:09 +03:00
|
|
|
// Same as Union(), but in the cases where aRect is non-empty, the union is
|
|
|
|
// done while guarding against overflow. If an overflow is detected, Nothing
|
|
|
|
// is returned.
|
|
|
|
MOZ_MUST_USE Maybe<Self> SafeUnion(const Self& aRect) const
|
|
|
|
{
|
|
|
|
if (this->IsEmpty()) {
|
|
|
|
return aRect.Overflows() ? Nothing() : Some(aRect);
|
|
|
|
} else if (aRect.IsEmpty()) {
|
|
|
|
return Some(*static_cast<const Self*>(this));
|
|
|
|
} else {
|
|
|
|
return this->SafeUnionEdges(aRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as UnionEdges, but guards against overflow. If an overflow is detected,
|
|
|
|
// Nothing is returned.
|
|
|
|
MOZ_MUST_USE Maybe<Self> SafeUnionEdges(const Self& aRect) const
|
|
|
|
{
|
|
|
|
if (this->Overflows() || aRect.Overflows()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
// If neither |this| nor |aRect| overflow, then their XMost/YMost values
|
|
|
|
// should be safe to use.
|
|
|
|
CheckedInt<int32_t> newX = std::min(this->x, aRect.x);
|
|
|
|
CheckedInt<int32_t> newY = std::min(this->y, aRect.y);
|
|
|
|
CheckedInt<int32_t> newXMost = std::max(this->XMost(), aRect.XMost());
|
|
|
|
CheckedInt<int32_t> newYMost = std::max(this->YMost(), aRect.YMost());
|
|
|
|
CheckedInt<int32_t> newW = newXMost - newX;
|
|
|
|
CheckedInt<int32_t> newH = newYMost - newY;
|
|
|
|
if (!newW.isValid() || !newH.isValid()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
return Some(Self(newX.value(), newY.value(), newW.value(), newH.value()));
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-03-29 20:40:07 +03:00
|
|
|
if (this->IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-21 18:04:57 +03:00
|
|
|
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
|
|
|
|
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()));
|
2016-02-02 03:27:49 +03:00
|
|
|
return RectTyped<units, F>(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)
|
|
|
|
{
|
2016-09-16 18:49:39 +03:00
|
|
|
return IntRectTyped<units>::RoundIn(aRect);
|
2013-06-15 00:11:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-27 21:33:27 +04:00
|
|
|
template<class units>
|
|
|
|
IntRectTyped<units> RoundedOut(const RectTyped<units>& aRect)
|
|
|
|
{
|
2016-09-16 18:49:39 +03:00
|
|
|
return IntRectTyped<units>::RoundOut(aRect);
|
2013-11-27 21:33:27 +04:00
|
|
|
}
|
|
|
|
|
2015-11-17 07:34:00 +03:00
|
|
|
template<class units>
|
|
|
|
IntRectTyped<units> TruncatedToInt(const RectTyped<units>& aRect) {
|
2016-09-16 18:49:39 +03:00
|
|
|
return IntRectTyped<units>::Truncate(aRect);
|
2015-11-17 07:34:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-12 01:23:14 +03:00
|
|
|
template<class units>
|
|
|
|
RectTyped<units> IntRectToRect(const IntRectTyped<units>& aRect)
|
|
|
|
{
|
|
|
|
return RectTyped<units>(aRect.x, aRect.y, aRect.width, aRect.height);
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:40:58 +03:00
|
|
|
// Convenience function for intersecting two rectangles wrapped in Maybes.
|
|
|
|
template <typename T>
|
|
|
|
Maybe<T>
|
|
|
|
IntersectMaybeRects(const Maybe<T>& a, const Maybe<T>& b)
|
2016-04-21 00:16:09 +03:00
|
|
|
{
|
|
|
|
if (!a) {
|
|
|
|
return b;
|
|
|
|
} else if (!b) {
|
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
return Some(a->Intersect(*b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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_ */
|