Bug 1496194 - Add a RectAbsolute implementation for MoveInsideAndClamp. r=botond

Depends on D15135

Differential Revision: https://phabricator.services.mozilla.com/D15136

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Kartikaya Gupta 2018-12-21 22:31:55 +00:00
Родитель 04689eb8ea
Коммит b184eedbf0
2 изменённых файлов: 57 добавлений и 0 удалений

Просмотреть файл

@ -239,6 +239,30 @@ struct BaseRectAbsolute {
left = static_cast<T>(ceil(double(left) / aXScale));
top = static_cast<T>(ceil(double(top) / aYScale));
}
/**
* 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.
*/
MOZ_MUST_USE Sub MoveInsideAndClamp(const Sub& aRect) const {
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) {
return stream << '(' << aRect.left << ',' << aRect.top << ',' << aRect.right
<< ',' << aRect.bottom << ')';
}
};
template <class Units>

Просмотреть файл

@ -599,3 +599,36 @@ TEST(Gfx, gfxRect) {
TestSetWH<gfxRect>();
TestSwap<gfxRect>();
}
static void TestMoveInsideAndClamp(IntRect aSrc, IntRect aTarget,
IntRect aExpected) {
// Test the implementation in BaseRect (x/y/width/height representation)
IntRect result = aSrc.MoveInsideAndClamp(aTarget);
EXPECT_TRUE(result.IsEqualEdges(aExpected))
<< "Source " << aSrc << " Target " << aTarget << " Expected " << aExpected
<< " Actual " << result;
// Also test the implementation in RectAbsolute (left/top/right/bottom
// representation)
IntRectAbsolute absSrc = IntRectAbsolute::FromRect(aSrc);
IntRectAbsolute absTarget = IntRectAbsolute::FromRect(aTarget);
IntRectAbsolute absExpected = IntRectAbsolute::FromRect(aExpected);
IntRectAbsolute absResult = absSrc.MoveInsideAndClamp(absTarget);
EXPECT_TRUE(absResult.IsEqualEdges(absExpected))
<< "AbsSource " << absSrc << " AbsTarget " << absTarget << " AbsExpected "
<< absExpected << " AbsActual " << absResult;
}
TEST(Gfx, MoveInsideAndClamp) {
TestMoveInsideAndClamp(IntRect(0, 0, 10, 10), IntRect(1, -1, 10, 10),
IntRect(1, -1, 10, 10));
TestMoveInsideAndClamp(IntRect(0, 0, 10, 10), IntRect(-1, -1, 12, 5),
IntRect(0, -1, 10, 5));
TestMoveInsideAndClamp(IntRect(0, 0, 10, 10), IntRect(10, 11, 10, 0),
IntRect(10, 11, 10, 0));
TestMoveInsideAndClamp(IntRect(0, 0, 10, 10), IntRect(-10, -1, 10, 0),
IntRect(-10, -1, 10, 0));
TestMoveInsideAndClamp(IntRect(0, 0, 0, 0), IntRect(10, -10, 10, 10),
IntRect(10, 0, 0, 0));
}