michael@0: /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_GFX_RECT_H_ michael@0: #define MOZILLA_GFX_RECT_H_ michael@0: michael@0: #include "BaseRect.h" michael@0: #include "BaseMargin.h" michael@0: #include "Point.h" michael@0: #include "Tools.h" michael@0: michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: template michael@0: struct IntMarginTyped: michael@0: public BaseMargin >, michael@0: public units { michael@0: typedef BaseMargin > Super; michael@0: michael@0: IntMarginTyped() : Super() {} michael@0: IntMarginTyped(int32_t aTop, int32_t aRight, int32_t aBottom, int32_t aLeft) : michael@0: Super(aTop, aRight, aBottom, aLeft) {} michael@0: }; michael@0: typedef IntMarginTyped IntMargin; michael@0: michael@0: template michael@0: struct MarginTyped: michael@0: public BaseMargin >, michael@0: public units { michael@0: typedef BaseMargin > Super; michael@0: michael@0: MarginTyped() : Super() {} michael@0: MarginTyped(Float aTop, Float aRight, Float aBottom, Float aLeft) : michael@0: Super(aTop, aRight, aBottom, aLeft) {} michael@0: explicit MarginTyped(const IntMarginTyped& aMargin) : michael@0: Super(float(aMargin.top), float(aMargin.right), michael@0: float(aMargin.bottom), float(aMargin.left)) {} michael@0: }; michael@0: typedef MarginTyped Margin; michael@0: michael@0: template michael@0: IntMarginTyped RoundedToInt(const MarginTyped& aMargin) michael@0: { michael@0: return IntMarginTyped(int32_t(floorf(aMargin.top + 0.5f)), michael@0: int32_t(floorf(aMargin.right + 0.5f)), michael@0: int32_t(floorf(aMargin.bottom + 0.5f)), michael@0: int32_t(floorf(aMargin.left + 0.5f))); michael@0: } michael@0: michael@0: template michael@0: struct IntRectTyped : michael@0: public BaseRect, IntPointTyped, IntSizeTyped, IntMarginTyped >, michael@0: public units { michael@0: typedef BaseRect, IntPointTyped, IntSizeTyped, IntMarginTyped > Super; michael@0: michael@0: IntRectTyped() : Super() {} michael@0: IntRectTyped(const IntPointTyped& aPos, const IntSizeTyped& aSize) : michael@0: Super(aPos, aSize) {} michael@0: IntRectTyped(int32_t _x, int32_t _y, int32_t _width, int32_t _height) : michael@0: Super(_x, _y, _width, _height) {} michael@0: michael@0: // Rounding isn't meaningful on an integer rectangle. michael@0: void Round() {} michael@0: void RoundIn() {} michael@0: void RoundOut() {} michael@0: michael@0: // XXX When all of the code is ported, the following functions to convert to and from michael@0: // unknown types should be removed. michael@0: michael@0: static IntRectTyped FromUnknownRect(const IntRectTyped& rect) { michael@0: return IntRectTyped(rect.x, rect.y, rect.width, rect.height); michael@0: } michael@0: michael@0: IntRectTyped ToUnknownRect() const { michael@0: return IntRectTyped(this->x, this->y, this->width, this->height); michael@0: } michael@0: }; michael@0: typedef IntRectTyped IntRect; michael@0: michael@0: template michael@0: struct RectTyped : michael@0: public BaseRect, PointTyped, SizeTyped, MarginTyped >, michael@0: public units { michael@0: typedef BaseRect, PointTyped, SizeTyped, MarginTyped > Super; michael@0: michael@0: RectTyped() : Super() {} michael@0: RectTyped(const PointTyped& aPos, const SizeTyped& aSize) : michael@0: Super(aPos, aSize) {} michael@0: RectTyped(Float _x, Float _y, Float _width, Float _height) : michael@0: Super(_x, _y, _width, _height) {} michael@0: explicit RectTyped(const IntRectTyped& rect) : michael@0: Super(float(rect.x), float(rect.y), michael@0: float(rect.width), float(rect.height)) {} michael@0: michael@0: void NudgeToIntegers() michael@0: { michael@0: NudgeToInteger(&(this->x)); michael@0: NudgeToInteger(&(this->y)); michael@0: NudgeToInteger(&(this->width)); michael@0: NudgeToInteger(&(this->height)); michael@0: } michael@0: michael@0: bool ToIntRect(IntRectTyped *aOut) const michael@0: { michael@0: *aOut = IntRectTyped(int32_t(this->X()), int32_t(this->Y()), michael@0: int32_t(this->Width()), int32_t(this->Height())); michael@0: return RectTyped(Float(aOut->x), Float(aOut->y), michael@0: Float(aOut->width), Float(aOut->height)) michael@0: .IsEqualEdges(*this); michael@0: } michael@0: michael@0: // XXX When all of the code is ported, the following functions to convert to and from michael@0: // unknown types should be removed. michael@0: michael@0: static RectTyped FromUnknownRect(const RectTyped& rect) { michael@0: return RectTyped(rect.x, rect.y, rect.width, rect.height); michael@0: } michael@0: michael@0: RectTyped ToUnknownRect() const { michael@0: return RectTyped(this->x, this->y, this->width, this->height); michael@0: } michael@0: michael@0: // This is here only to keep IPDL-generated code happy. DO NOT USE. michael@0: bool operator==(const RectTyped& aRect) const michael@0: { michael@0: return RectTyped::IsEqualEdges(aRect); michael@0: } michael@0: }; michael@0: typedef RectTyped Rect; michael@0: michael@0: template michael@0: IntRectTyped RoundedToInt(const RectTyped& aRect) michael@0: { michael@0: return IntRectTyped(int32_t(floorf(aRect.x + 0.5f)), michael@0: int32_t(floorf(aRect.y + 0.5f)), michael@0: int32_t(floorf(aRect.width + 0.5f)), michael@0: int32_t(floorf(aRect.height + 0.5f))); michael@0: } michael@0: michael@0: template michael@0: IntRectTyped RoundedIn(const RectTyped& aRect) michael@0: { michael@0: RectTyped copy(aRect); michael@0: copy.RoundIn(); michael@0: return IntRectTyped(int32_t(copy.x), michael@0: int32_t(copy.y), michael@0: int32_t(copy.width), michael@0: int32_t(copy.height)); michael@0: } michael@0: michael@0: template michael@0: IntRectTyped RoundedOut(const RectTyped& aRect) michael@0: { michael@0: RectTyped copy(aRect); michael@0: copy.RoundOut(); michael@0: return IntRectTyped(int32_t(copy.x), michael@0: int32_t(copy.y), michael@0: int32_t(copy.width), michael@0: int32_t(copy.height)); michael@0: } michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif /* MOZILLA_GFX_RECT_H_ */