2018-02-09 23:40:07 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2018-03-06 00:35:35 +03:00
|
|
|
#ifndef MOZILLA_GFX_RECT_ABSOLUTE_H_
|
|
|
|
#define MOZILLA_GFX_RECT_ABSOLUTE_H_
|
2018-02-09 23:40:07 +03:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
2018-12-22 01:11:26 +03:00
|
|
|
#include "Point.h"
|
2018-02-09 23:40:07 +03:00
|
|
|
#include "Rect.h"
|
|
|
|
#include "Types.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <typename>
|
|
|
|
struct IsPixel;
|
|
|
|
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
/**
|
2018-03-06 00:35:35 +03:00
|
|
|
* A RectAbsolute is similar to a Rect (see BaseRect.h), but represented as
|
2018-02-09 23:40:07 +03:00
|
|
|
* (x1, y1, x2, y2) instead of (x, y, width, height).
|
|
|
|
*
|
|
|
|
* Unless otherwise indicated, methods on this class correspond
|
|
|
|
* to methods on BaseRect.
|
|
|
|
*
|
|
|
|
* The API is currently very bare-bones; it may be extended as needed.
|
|
|
|
*
|
|
|
|
* Do not use this class directly. Subclass it, pass that subclass as the
|
|
|
|
* Sub parameter, and only use that subclass.
|
|
|
|
*/
|
2018-12-22 01:11:26 +03:00
|
|
|
template <class T, class Sub, class Point, class Rect>
|
2018-03-06 00:35:35 +03:00
|
|
|
struct BaseRectAbsolute {
|
2018-02-09 23:40:07 +03:00
|
|
|
protected:
|
2018-07-09 19:39:50 +03:00
|
|
|
T left, top, right, bottom;
|
2018-02-09 23:40:07 +03:00
|
|
|
|
|
|
|
public:
|
2018-07-09 19:39:50 +03:00
|
|
|
BaseRectAbsolute() : left(0), top(0), right(0), bottom(0) {}
|
|
|
|
BaseRectAbsolute(T aLeft, T aTop, T aRight, T aBottom)
|
|
|
|
: left(aLeft), top(aTop), right(aRight), bottom(aBottom) {}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE T X() const { return left; }
|
|
|
|
MOZ_ALWAYS_INLINE T Y() const { return top; }
|
|
|
|
MOZ_ALWAYS_INLINE T Width() const { return right - left; }
|
|
|
|
MOZ_ALWAYS_INLINE T Height() const { return bottom - top; }
|
|
|
|
MOZ_ALWAYS_INLINE T XMost() const { return right; }
|
|
|
|
MOZ_ALWAYS_INLINE T YMost() const { return bottom; }
|
|
|
|
MOZ_ALWAYS_INLINE const T& Left() const { return left; }
|
|
|
|
MOZ_ALWAYS_INLINE const T& Right() const { return right; }
|
|
|
|
MOZ_ALWAYS_INLINE const T& Top() const { return top; }
|
|
|
|
MOZ_ALWAYS_INLINE const T& Bottom() const { return bottom; }
|
|
|
|
MOZ_ALWAYS_INLINE T& Left() { return left; }
|
|
|
|
MOZ_ALWAYS_INLINE T& Right() { return right; }
|
|
|
|
MOZ_ALWAYS_INLINE T& Top() { return top; }
|
|
|
|
MOZ_ALWAYS_INLINE T& Bottom() { return bottom; }
|
2018-08-07 18:54:32 +03:00
|
|
|
T Area() const { return Width() * Height(); }
|
|
|
|
|
|
|
|
void Inflate(T aD) { Inflate(aD, aD); }
|
|
|
|
void Inflate(T aDx, T aDy) {
|
|
|
|
left -= aDx;
|
|
|
|
top -= aDy;
|
|
|
|
right += aDx;
|
|
|
|
bottom += aDy;
|
|
|
|
}
|
2018-07-09 19:39:50 +03:00
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE void SetBox(T aLeft, T aTop, T aRight, T aBottom) {
|
|
|
|
left = aLeft;
|
|
|
|
top = aTop;
|
|
|
|
right = aRight;
|
|
|
|
bottom = aBottom;
|
2018-02-09 23:40:07 +03:00
|
|
|
}
|
2018-08-07 18:54:32 +03:00
|
|
|
void SetLeftEdge(T aLeft) { left = aLeft; }
|
|
|
|
void SetRightEdge(T aRight) { right = aRight; }
|
|
|
|
void SetTopEdge(T aTop) { top = aTop; }
|
2018-07-09 19:39:50 +03:00
|
|
|
void SetBottomEdge(T aBottom) { bottom = aBottom; }
|
2018-02-09 23:40:07 +03:00
|
|
|
|
|
|
|
static Sub FromRect(const Rect& aRect) {
|
2018-08-07 18:54:32 +03:00
|
|
|
if (aRect.Overflows()) {
|
|
|
|
return Sub();
|
|
|
|
}
|
2018-02-09 23:40:07 +03:00
|
|
|
return Sub(aRect.x, aRect.y, aRect.XMost(), aRect.YMost());
|
|
|
|
}
|
|
|
|
|
2020-03-25 13:35:04 +03:00
|
|
|
[[nodiscard]] Sub Intersect(const Sub& aOther) const {
|
2018-02-09 23:40:07 +03:00
|
|
|
Sub result;
|
2018-07-09 19:39:50 +03:00
|
|
|
result.left = std::max<T>(left, aOther.left);
|
|
|
|
result.top = std::max<T>(top, aOther.top);
|
|
|
|
result.right = std::min<T>(right, aOther.right);
|
|
|
|
result.bottom = std::min<T>(bottom, aOther.bottom);
|
2018-08-07 18:54:32 +03:00
|
|
|
if (result.right < result.left || result.bottom < result.top) {
|
|
|
|
result.SizeTo(0, 0);
|
|
|
|
}
|
2018-02-09 23:40:07 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-03 01:09:17 +03:00
|
|
|
bool IsEmpty() const { return right <= left || bottom <= top; }
|
|
|
|
|
2018-03-06 00:35:35 +03:00
|
|
|
bool IsEqualEdges(const Sub& aOther) const {
|
2018-07-09 19:39:50 +03:00
|
|
|
return left == aOther.left && top == aOther.top && right == aOther.right &&
|
|
|
|
bottom == aOther.bottom;
|
2018-02-09 23:40:07 +03:00
|
|
|
}
|
2018-08-07 18:54:32 +03:00
|
|
|
|
|
|
|
bool IsEqualInterior(const Sub& aRect) const {
|
|
|
|
return IsEqualEdges(aRect) || (IsEmpty() && aRect.IsEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE void MoveBy(T aDx, T aDy) {
|
|
|
|
left += aDx;
|
|
|
|
right += aDx;
|
|
|
|
top += aDy;
|
|
|
|
bottom += aDy;
|
|
|
|
}
|
|
|
|
MOZ_ALWAYS_INLINE void MoveBy(const Point& aPoint) {
|
|
|
|
left += aPoint.x;
|
|
|
|
right += aPoint.x;
|
|
|
|
top += aPoint.y;
|
|
|
|
bottom += aPoint.y;
|
|
|
|
}
|
|
|
|
MOZ_ALWAYS_INLINE void SizeTo(T aWidth, T aHeight) {
|
|
|
|
right = left + aWidth;
|
|
|
|
bottom = top + aHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Contains(const Sub& aRect) const {
|
|
|
|
return aRect.IsEmpty() || (left <= aRect.left && aRect.right <= right &&
|
|
|
|
top <= aRect.top && aRect.bottom <= bottom);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-08-07 18:54:32 +03:00
|
|
|
bool Contains(T aX, T aY) const {
|
|
|
|
return (left <= aX && aX < right && top <= aY && aY < bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Intersects(const Sub& aRect) const {
|
|
|
|
return !IsEmpty() && !aRect.IsEmpty() && left < aRect.right &&
|
|
|
|
aRect.left < right && top < aRect.bottom && aRect.top < bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetEmpty() { left = right = top = bottom = 0; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-08-07 18:54:32 +03:00
|
|
|
// Returns the smallest rectangle that contains both the area of both
|
2021-11-22 06:26:19 +03:00
|
|
|
// this and aRect. Thus, empty input rectangles are ignored.
|
|
|
|
// Note: if both rectangles are empty, returns aRect.
|
2018-08-07 18:54:32 +03:00
|
|
|
// WARNING! This is not safe against overflow, prefer using SafeUnion instead
|
|
|
|
// when dealing with int-based rects.
|
2020-03-25 13:35:04 +03:00
|
|
|
[[nodiscard]] Sub Union(const Sub& aRect) const {
|
2018-08-07 18:54:32 +03:00
|
|
|
if (IsEmpty()) {
|
|
|
|
return aRect;
|
|
|
|
} else if (aRect.IsEmpty()) {
|
|
|
|
return *static_cast<const Sub*>(this);
|
|
|
|
} else {
|
|
|
|
return UnionEdges(aRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns the smallest rectangle that contains both the points (including
|
|
|
|
// edges) of both aRect1 and aRect2.
|
|
|
|
// Thus, empty input rectangles are allowed to affect the result.
|
|
|
|
// WARNING! This is not safe against overflow, prefer using SafeUnionEdges
|
|
|
|
// instead when dealing with int-based rects.
|
2020-03-25 13:35:04 +03:00
|
|
|
[[nodiscard]] Sub UnionEdges(const Sub& aRect) const {
|
2018-08-07 18:54:32 +03:00
|
|
|
Sub result;
|
|
|
|
result.left = std::min(left, aRect.left);
|
|
|
|
result.top = std::min(top, aRect.top);
|
|
|
|
result.right = std::max(XMost(), aRect.XMost());
|
|
|
|
result.bottom = std::max(YMost(), aRect.YMost());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale 'this' by aScale without doing any rounding.
|
|
|
|
void Scale(T aScale) { Scale(aScale, aScale); }
|
|
|
|
// Scale 'this' by aXScale and aYScale, without doing any rounding.
|
|
|
|
void Scale(T aXScale, T aYScale) {
|
|
|
|
right = XMost() * aXScale;
|
|
|
|
bottom = YMost() * aYScale;
|
|
|
|
left = left * aXScale;
|
|
|
|
top = top * aYScale;
|
|
|
|
}
|
|
|
|
// Scale 'this' by aScale, converting coordinates to integers so that the
|
|
|
|
// result is the smallest integer-coordinate rectangle containing the
|
|
|
|
// unrounded result. Note: this can turn an empty rectangle into a non-empty
|
|
|
|
// rectangle
|
|
|
|
void ScaleRoundOut(double aScale) { ScaleRoundOut(aScale, aScale); }
|
|
|
|
// Scale 'this' by aXScale and aYScale, converting coordinates to integers so
|
|
|
|
// that the result is the smallest integer-coordinate rectangle containing the
|
|
|
|
// unrounded result.
|
|
|
|
// Note: this can turn an empty rectangle into a non-empty rectangle
|
|
|
|
void ScaleRoundOut(double aXScale, double aYScale) {
|
|
|
|
right = static_cast<T>(ceil(double(XMost()) * aXScale));
|
|
|
|
bottom = static_cast<T>(ceil(double(YMost()) * aYScale));
|
|
|
|
left = static_cast<T>(floor(double(left) * aXScale));
|
|
|
|
top = static_cast<T>(floor(double(top) * aYScale));
|
|
|
|
}
|
|
|
|
// Scale 'this' by aScale, converting coordinates to integers so that the
|
|
|
|
// result is the largest integer-coordinate rectangle contained by the
|
|
|
|
// unrounded result.
|
|
|
|
void ScaleRoundIn(double aScale) { ScaleRoundIn(aScale, aScale); }
|
|
|
|
// Scale 'this' by aXScale and aYScale, converting coordinates to integers so
|
|
|
|
// that the result is the largest integer-coordinate rectangle contained by
|
|
|
|
// the unrounded result.
|
|
|
|
void ScaleRoundIn(double aXScale, double aYScale) {
|
|
|
|
right = static_cast<T>(floor(double(XMost()) * aXScale));
|
|
|
|
bottom = static_cast<T>(floor(double(YMost()) * aYScale));
|
|
|
|
left = static_cast<T>(ceil(double(left) * aXScale));
|
|
|
|
top = static_cast<T>(ceil(double(top) * aYScale));
|
|
|
|
}
|
|
|
|
// Scale 'this' by 1/aScale, converting coordinates to integers so that the
|
|
|
|
// result is the smallest integer-coordinate rectangle containing the
|
|
|
|
// unrounded result. Note: this can turn an empty rectangle into a non-empty
|
|
|
|
// rectangle
|
|
|
|
void ScaleInverseRoundOut(double aScale) {
|
|
|
|
ScaleInverseRoundOut(aScale, aScale);
|
|
|
|
}
|
|
|
|
// Scale 'this' by 1/aXScale and 1/aYScale, converting coordinates to integers
|
|
|
|
// so that the result is the smallest integer-coordinate rectangle containing
|
|
|
|
// the unrounded result. Note: this can turn an empty rectangle into a
|
|
|
|
// non-empty rectangle
|
|
|
|
void ScaleInverseRoundOut(double aXScale, double aYScale) {
|
|
|
|
right = static_cast<T>(ceil(double(XMost()) / aXScale));
|
|
|
|
bottom = static_cast<T>(ceil(double(YMost()) / aYScale));
|
|
|
|
left = static_cast<T>(floor(double(left) / aXScale));
|
|
|
|
top = static_cast<T>(floor(double(top) / aYScale));
|
|
|
|
}
|
|
|
|
// Scale 'this' by 1/aScale, converting coordinates to integers so that the
|
|
|
|
// result is the largest integer-coordinate rectangle contained by the
|
|
|
|
// unrounded result.
|
|
|
|
void ScaleInverseRoundIn(double aScale) {
|
|
|
|
ScaleInverseRoundIn(aScale, aScale);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-08-07 18:54:32 +03:00
|
|
|
// Scale 'this' by 1/aXScale and 1/aYScale, converting coordinates to integers
|
|
|
|
// so that the result is the largest integer-coordinate rectangle contained by
|
|
|
|
// the unrounded result.
|
|
|
|
void ScaleInverseRoundIn(double aXScale, double aYScale) {
|
|
|
|
right = static_cast<T>(floor(double(XMost()) / aXScale));
|
|
|
|
bottom = static_cast<T>(floor(double(YMost()) / aYScale));
|
|
|
|
left = static_cast<T>(ceil(double(left) / aXScale));
|
|
|
|
top = static_cast<T>(ceil(double(top) / aYScale));
|
|
|
|
}
|
2018-12-22 01:31:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate this rectangle to be inside aRect. If it doesn't fit inside
|
|
|
|
* aRect then the dimensions that don't fit will be shrunk so that they
|
|
|
|
* do fit. The resulting rect is returned.
|
|
|
|
*/
|
2020-03-25 13:35:04 +03:00
|
|
|
[[nodiscard]] Sub MoveInsideAndClamp(const Sub& aRect) const {
|
2018-12-22 01:31:55 +03:00
|
|
|
T newLeft = std::max(aRect.left, left);
|
|
|
|
T newTop = std::max(aRect.top, top);
|
|
|
|
T width = std::min(aRect.Width(), Width());
|
|
|
|
T height = std::min(aRect.Height(), Height());
|
|
|
|
Sub rect(newLeft, newTop, newLeft + width, newTop + height);
|
|
|
|
newLeft = std::min(rect.right, aRect.right) - width;
|
|
|
|
newTop = std::min(rect.bottom, aRect.bottom) - height;
|
|
|
|
rect.MoveBy(newLeft - rect.left, newTop - rect.top);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream& operator<<(
|
|
|
|
std::ostream& stream,
|
|
|
|
const BaseRectAbsolute<T, Sub, Point, Rect>& aRect) {
|
2020-09-25 11:57:25 +03:00
|
|
|
return stream << "(l=" << aRect.left << ", t=" << aRect.top
|
|
|
|
<< ", r=" << aRect.right << ", b=" << aRect.bottom << ')';
|
2018-12-22 01:31:55 +03:00
|
|
|
}
|
2018-02-09 23:40:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Units>
|
2018-03-06 00:35:35 +03:00
|
|
|
struct IntRectAbsoluteTyped
|
|
|
|
: public BaseRectAbsolute<int32_t, IntRectAbsoluteTyped<Units>,
|
2018-12-22 01:11:26 +03:00
|
|
|
IntPointTyped<Units>, IntRectTyped<Units>>,
|
2018-02-09 23:40:07 +03:00
|
|
|
public Units {
|
|
|
|
static_assert(IsPixel<Units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
2018-03-06 00:35:35 +03:00
|
|
|
typedef BaseRectAbsolute<int32_t, IntRectAbsoluteTyped<Units>,
|
2018-12-22 01:11:26 +03:00
|
|
|
IntPointTyped<Units>, IntRectTyped<Units>>
|
2018-03-06 00:35:35 +03:00
|
|
|
Super;
|
2018-02-09 23:40:07 +03:00
|
|
|
typedef IntParam<int32_t> ToInt;
|
|
|
|
|
2018-03-06 00:35:35 +03:00
|
|
|
IntRectAbsoluteTyped() : Super() {}
|
2018-07-09 19:39:50 +03:00
|
|
|
IntRectAbsoluteTyped(ToInt aLeft, ToInt aTop, ToInt aRight, ToInt aBottom)
|
|
|
|
: Super(aLeft.value, aTop.value, aRight.value, aBottom.value) {}
|
2018-02-09 23:40:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Units>
|
2018-03-06 00:35:35 +03:00
|
|
|
struct RectAbsoluteTyped
|
|
|
|
: public BaseRectAbsolute<Float, RectAbsoluteTyped<Units>,
|
2018-12-22 01:11:26 +03:00
|
|
|
PointTyped<Units>, RectTyped<Units>>,
|
2018-02-09 23:40:07 +03:00
|
|
|
public Units {
|
|
|
|
static_assert(IsPixel<Units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
2018-12-22 01:11:26 +03:00
|
|
|
typedef BaseRectAbsolute<Float, RectAbsoluteTyped<Units>, PointTyped<Units>,
|
|
|
|
RectTyped<Units>>
|
2018-03-06 00:35:35 +03:00
|
|
|
Super;
|
2018-02-09 23:40:07 +03:00
|
|
|
|
2018-03-06 00:35:35 +03:00
|
|
|
RectAbsoluteTyped() : Super() {}
|
2018-07-09 19:39:50 +03:00
|
|
|
RectAbsoluteTyped(Float aLeft, Float aTop, Float aRight, Float aBottom)
|
|
|
|
: Super(aLeft, aTop, aRight, aBottom) {}
|
2018-02-09 23:40:07 +03:00
|
|
|
};
|
|
|
|
|
2018-07-09 19:39:50 +03:00
|
|
|
typedef IntRectAbsoluteTyped<UnknownUnits> IntRectAbsolute;
|
|
|
|
|
2018-02-09 23:40:07 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2018-03-06 00:35:35 +03:00
|
|
|
#endif /* MOZILLA_GFX_RECT_ABSOLUTE_H_ */
|