diff -r 000000000000 -r 6474c204b198 gfx/2d/Rect.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gfx/2d/Rect.h Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,170 @@ +/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * 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/. */ + +#ifndef MOZILLA_GFX_RECT_H_ +#define MOZILLA_GFX_RECT_H_ + +#include "BaseRect.h" +#include "BaseMargin.h" +#include "Point.h" +#include "Tools.h" + +#include + +namespace mozilla { +namespace gfx { + +template +struct IntMarginTyped: + public BaseMargin >, + public units { + typedef BaseMargin > Super; + + IntMarginTyped() : Super() {} + IntMarginTyped(int32_t aTop, int32_t aRight, int32_t aBottom, int32_t aLeft) : + Super(aTop, aRight, aBottom, aLeft) {} +}; +typedef IntMarginTyped IntMargin; + +template +struct MarginTyped: + public BaseMargin >, + public units { + typedef BaseMargin > Super; + + MarginTyped() : Super() {} + MarginTyped(Float aTop, Float aRight, Float aBottom, Float aLeft) : + Super(aTop, aRight, aBottom, aLeft) {} + explicit MarginTyped(const IntMarginTyped& aMargin) : + Super(float(aMargin.top), float(aMargin.right), + float(aMargin.bottom), float(aMargin.left)) {} +}; +typedef MarginTyped Margin; + +template +IntMarginTyped RoundedToInt(const MarginTyped& aMargin) +{ + return IntMarginTyped(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))); +} + +template +struct IntRectTyped : + public BaseRect, IntPointTyped, IntSizeTyped, IntMarginTyped >, + public units { + typedef BaseRect, IntPointTyped, IntSizeTyped, IntMarginTyped > Super; + + IntRectTyped() : Super() {} + IntRectTyped(const IntPointTyped& aPos, const IntSizeTyped& aSize) : + Super(aPos, aSize) {} + IntRectTyped(int32_t _x, int32_t _y, int32_t _width, int32_t _height) : + Super(_x, _y, _width, _height) {} + + // Rounding isn't meaningful on an integer rectangle. + void Round() {} + void RoundIn() {} + void RoundOut() {} + + // XXX When all of the code is ported, the following functions to convert to and from + // unknown types should be removed. + + static IntRectTyped FromUnknownRect(const IntRectTyped& rect) { + return IntRectTyped(rect.x, rect.y, rect.width, rect.height); + } + + IntRectTyped ToUnknownRect() const { + return IntRectTyped(this->x, this->y, this->width, this->height); + } +}; +typedef IntRectTyped IntRect; + +template +struct RectTyped : + public BaseRect, PointTyped, SizeTyped, MarginTyped >, + public units { + typedef BaseRect, PointTyped, SizeTyped, MarginTyped > Super; + + RectTyped() : Super() {} + RectTyped(const PointTyped& aPos, const SizeTyped& aSize) : + Super(aPos, aSize) {} + RectTyped(Float _x, Float _y, Float _width, Float _height) : + Super(_x, _y, _width, _height) {} + explicit RectTyped(const IntRectTyped& rect) : + Super(float(rect.x), float(rect.y), + float(rect.width), float(rect.height)) {} + + void NudgeToIntegers() + { + NudgeToInteger(&(this->x)); + NudgeToInteger(&(this->y)); + NudgeToInteger(&(this->width)); + NudgeToInteger(&(this->height)); + } + + bool ToIntRect(IntRectTyped *aOut) const + { + *aOut = IntRectTyped(int32_t(this->X()), int32_t(this->Y()), + int32_t(this->Width()), int32_t(this->Height())); + return RectTyped(Float(aOut->x), Float(aOut->y), + Float(aOut->width), Float(aOut->height)) + .IsEqualEdges(*this); + } + + // XXX When all of the code is ported, the following functions to convert to and from + // unknown types should be removed. + + static RectTyped FromUnknownRect(const RectTyped& rect) { + return RectTyped(rect.x, rect.y, rect.width, rect.height); + } + + RectTyped ToUnknownRect() const { + return RectTyped(this->x, this->y, this->width, this->height); + } + + // This is here only to keep IPDL-generated code happy. DO NOT USE. + bool operator==(const RectTyped& aRect) const + { + return RectTyped::IsEqualEdges(aRect); + } +}; +typedef RectTyped Rect; + +template +IntRectTyped RoundedToInt(const RectTyped& aRect) +{ + return IntRectTyped(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))); +} + +template +IntRectTyped RoundedIn(const RectTyped& aRect) +{ + RectTyped copy(aRect); + copy.RoundIn(); + return IntRectTyped(int32_t(copy.x), + int32_t(copy.y), + int32_t(copy.width), + int32_t(copy.height)); +} + +template +IntRectTyped RoundedOut(const RectTyped& aRect) +{ + RectTyped copy(aRect); + copy.RoundOut(); + return IntRectTyped(int32_t(copy.x), + int32_t(copy.y), + int32_t(copy.width), + int32_t(copy.height)); +} + +} +} + +#endif /* MOZILLA_GFX_RECT_H_ */