2011-06-24 10:41:16 -07:00
|
|
|
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07: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 12:41:33 -07:00
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
#ifndef MOZILLA_GFX_RECT_H_
|
|
|
|
#define MOZILLA_GFX_RECT_H_
|
|
|
|
|
|
|
|
#include "BaseRect.h"
|
|
|
|
#include "BaseMargin.h"
|
|
|
|
#include "Point.h"
|
2013-05-29 06:32:30 -07:00
|
|
|
#include "Tools.h"
|
2013-08-22 02:10:35 -07:00
|
|
|
|
|
|
|
#include <cmath>
|
2011-05-26 12:41:33 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-06-10 09:43:52 -07:00
|
|
|
|
|
|
|
template <typename> struct IsPixel;
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
namespace gfx {
|
2011-05-26 12:41:33 -07:00
|
|
|
|
2013-08-22 09:41:51 -07:00
|
|
|
template<class units>
|
|
|
|
struct IntMarginTyped:
|
|
|
|
public BaseMargin<int32_t, IntMarginTyped<units> >,
|
|
|
|
public units {
|
2014-06-10 09:43:52 -07:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-08-22 09:41:51 -07: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;
|
|
|
|
|
2013-07-22 01:50:11 -07:00
|
|
|
template<class units>
|
|
|
|
struct MarginTyped:
|
|
|
|
public BaseMargin<Float, MarginTyped<units> >,
|
|
|
|
public units {
|
2014-06-10 09:43:52 -07:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-07-22 01:50:11 -07:00
|
|
|
typedef BaseMargin<Float, MarginTyped<units> > Super;
|
2011-05-26 12:41:33 -07:00
|
|
|
|
2013-08-22 09:41:51 -07:00
|
|
|
MarginTyped() : Super() {}
|
2013-07-22 01:50:11 -07:00
|
|
|
MarginTyped(Float aTop, Float aRight, Float aBottom, Float aLeft) :
|
|
|
|
Super(aTop, aRight, aBottom, aLeft) {}
|
2013-08-22 09:41:51 -07:00
|
|
|
explicit MarginTyped(const IntMarginTyped<units>& aMargin) :
|
|
|
|
Super(float(aMargin.top), float(aMargin.right),
|
|
|
|
float(aMargin.bottom), float(aMargin.left)) {}
|
2011-06-24 10:41:16 -07:00
|
|
|
};
|
2013-07-22 01:50:11 -07:00
|
|
|
typedef MarginTyped<UnknownUnits> Margin;
|
2011-05-26 12:41:33 -07:00
|
|
|
|
2013-09-11 10:20:43 -07: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 06:32:30 -07:00
|
|
|
template<class units>
|
|
|
|
struct IntRectTyped :
|
2013-08-22 09:41:51 -07:00
|
|
|
public BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> >,
|
2013-05-29 06:32:30 -07:00
|
|
|
public units {
|
2014-06-10 09:43:52 -07:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-08-22 09:41:51 -07:00
|
|
|
typedef BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> > Super;
|
2011-05-26 12:41:33 -07:00
|
|
|
|
2013-05-29 06:32:30 -07:00
|
|
|
IntRectTyped() : Super() {}
|
2013-11-13 10:22:15 -08:00
|
|
|
IntRectTyped(const IntPointTyped<units>& aPos, const IntSizeTyped<units>& aSize) :
|
2011-06-24 10:41:16 -07:00
|
|
|
Super(aPos, aSize) {}
|
2013-05-29 06:32:30 -07:00
|
|
|
IntRectTyped(int32_t _x, int32_t _y, int32_t _width, int32_t _height) :
|
2011-06-24 10:41:16 -07:00
|
|
|
Super(_x, _y, _width, _height) {}
|
2011-11-13 20:29:14 -08:00
|
|
|
|
|
|
|
// Rounding isn't meaningful on an integer rectangle.
|
|
|
|
void Round() {}
|
|
|
|
void RoundIn() {}
|
|
|
|
void RoundOut() {}
|
2013-06-03 06:52:44 -07: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);
|
|
|
|
}
|
2011-05-26 12:41:33 -07:00
|
|
|
};
|
2013-05-29 06:32:30 -07:00
|
|
|
typedef IntRectTyped<UnknownUnits> IntRect;
|
2011-05-26 12:41:33 -07:00
|
|
|
|
2013-05-29 06:32:30 -07:00
|
|
|
template<class units>
|
|
|
|
struct RectTyped :
|
2013-08-22 09:41:51 -07:00
|
|
|
public BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> >,
|
2013-05-29 06:32:30 -07:00
|
|
|
public units {
|
2014-06-10 09:43:52 -07:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2013-08-22 09:41:51 -07:00
|
|
|
typedef BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> > Super;
|
2011-11-13 20:29:01 -08:00
|
|
|
|
2013-05-29 06:32:30 -07:00
|
|
|
RectTyped() : Super() {}
|
2013-11-13 10:22:15 -08:00
|
|
|
RectTyped(const PointTyped<units>& aPos, const SizeTyped<units>& aSize) :
|
2011-11-13 20:29:01 -08:00
|
|
|
Super(aPos, aSize) {}
|
2013-05-29 06:32:30 -07:00
|
|
|
RectTyped(Float _x, Float _y, Float _width, Float _height) :
|
2011-11-13 20:29:01 -08:00
|
|
|
Super(_x, _y, _width, _height) {}
|
2013-05-29 06:32:30 -07:00
|
|
|
explicit RectTyped(const IntRectTyped<units>& rect) :
|
2013-03-04 06:38:11 -08:00
|
|
|
Super(float(rect.x), float(rect.y),
|
|
|
|
float(rect.width), float(rect.height)) {}
|
2012-05-03 14:41:37 -07:00
|
|
|
|
2014-02-18 18:59:34 -08:00
|
|
|
void NudgeToIntegers()
|
2013-05-29 06:32:30 -07:00
|
|
|
{
|
|
|
|
NudgeToInteger(&(this->x));
|
|
|
|
NudgeToInteger(&(this->y));
|
|
|
|
NudgeToInteger(&(this->width));
|
|
|
|
NudgeToInteger(&(this->height));
|
|
|
|
}
|
2012-09-11 22:24:09 -07:00
|
|
|
|
2013-05-29 06:32:30 -07:00
|
|
|
bool ToIntRect(IntRectTyped<units> *aOut) const
|
2012-05-03 14:41:37 -07:00
|
|
|
{
|
2013-05-29 06:32:30 -07:00
|
|
|
*aOut = IntRectTyped<units>(int32_t(this->X()), int32_t(this->Y()),
|
|
|
|
int32_t(this->Width()), int32_t(this->Height()));
|
|
|
|
return RectTyped<units>(Float(aOut->x), Float(aOut->y),
|
|
|
|
Float(aOut->width), Float(aOut->height))
|
|
|
|
.IsEqualEdges(*this);
|
2012-05-03 14:41:37 -07:00
|
|
|
}
|
2013-06-03 06:52:44 -07:00
|
|
|
|
|
|
|
// XXX When all of the code is ported, the following functions to convert to and from
|
|
|
|
// unknown types should be removed.
|
|
|
|
|
|
|
|
static RectTyped<units> FromUnknownRect(const RectTyped<UnknownUnits>& rect) {
|
|
|
|
return RectTyped<units>(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
RectTyped<UnknownUnits> ToUnknownRect() const {
|
|
|
|
return RectTyped<UnknownUnits>(this->x, this->y, this->width, this->height);
|
|
|
|
}
|
2013-09-11 21:09:43 -07:00
|
|
|
|
|
|
|
// This is here only to keep IPDL-generated code happy. DO NOT USE.
|
|
|
|
bool operator==(const RectTyped<units>& aRect) const
|
|
|
|
{
|
|
|
|
return RectTyped<units>::IsEqualEdges(aRect);
|
|
|
|
}
|
2011-11-13 20:29:01 -08:00
|
|
|
};
|
2013-05-29 06:32:30 -07:00
|
|
|
typedef RectTyped<UnknownUnits> Rect;
|
2011-11-13 20:29:01 -08:00
|
|
|
|
2013-06-14 13:11:31 -07:00
|
|
|
template<class units>
|
|
|
|
IntRectTyped<units> RoundedToInt(const RectTyped<units>& aRect)
|
|
|
|
{
|
2013-08-22 02:10:35 -07:00
|
|
|
return IntRectTyped<units>(int32_t(floorf(aRect.x + 0.5f)),
|
|
|
|
int32_t(floorf(aRect.y + 0.5f)),
|
|
|
|
int32_t(floorf(aRect.width + 0.5f)),
|
|
|
|
int32_t(floorf(aRect.height + 0.5f)));
|
2013-06-14 13:11:31 -07:00
|
|
|
}
|
|
|
|
|
2013-06-14 13:11:44 -07: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 09:33:27 -08: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));
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
2011-05-26 12:41:33 -07:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
#endif /* MOZILLA_GFX_RECT_H_ */
|