diff options
Diffstat (limited to 'gfx/2d/Point.h')
-rw-r--r-- | gfx/2d/Point.h | 135 |
1 files changed, 120 insertions, 15 deletions
diff --git a/gfx/2d/Point.h b/gfx/2d/Point.h index 026b44604..0e271ccc2 100644 --- a/gfx/2d/Point.h +++ b/gfx/2d/Point.h @@ -6,24 +6,50 @@ #ifndef MOZILLA_GFX_POINT_H_ #define MOZILLA_GFX_POINT_H_ +#include "mozilla/Attributes.h" #include "Types.h" +#include "Coord.h" +#include "BaseCoord.h" #include "BasePoint.h" +#include "BasePoint3D.h" +#include "BasePoint4D.h" #include "BaseSize.h" +#include "mozilla/TypeTraits.h" + +#include <cmath> namespace mozilla { + +template <typename> struct IsPixel; + namespace gfx { // This should only be used by the typedefs below. struct UnknownUnits {}; +} // close namespace 'gfx' because IsPixel specialization must be in 'mozilla' + +template<> struct IsPixel<gfx::UnknownUnits> : TrueType {}; + +namespace gfx { + template<class units> struct IntPointTyped : - public BasePoint< int32_t, IntPointTyped<units> >, + public BasePoint< int32_t, IntPointTyped<units>, IntCoordTyped<units> >, public units { - typedef BasePoint< int32_t, IntPointTyped<units> > Super; + static_assert(IsPixel<units>::value, + "'units' must be a coordinate system tag"); + + typedef IntCoordTyped<units> Coord; + typedef BasePoint< int32_t, IntPointTyped<units>, IntCoordTyped<units> > Super; - IntPointTyped() : Super() {} - IntPointTyped(int32_t aX, int32_t aY) : Super(aX, aY) {} + MOZ_CONSTEXPR IntPointTyped() : Super() {} + MOZ_CONSTEXPR IntPointTyped(int32_t aX, int32_t aY) : Super(Coord(aX), Coord(aY)) {} + // The mixed-type constructors (int, Coord) and (Coord, int) are needed to + // avoid ambiguities because Coord is implicitly convertible to int. + MOZ_CONSTEXPR IntPointTyped(int32_t aX, Coord aY) : Super(Coord(aX), aY) {} + MOZ_CONSTEXPR IntPointTyped(Coord aX, int32_t aY) : Super(aX, Coord(aY)) {} + MOZ_CONSTEXPR IntPointTyped(Coord aX, Coord aY) : Super(aX, aY) {} // XXX When all of the code is ported, the following functions to convert to and from // unknown types should be removed. @@ -40,13 +66,22 @@ typedef IntPointTyped<UnknownUnits> IntPoint; template<class units> struct PointTyped : - public BasePoint< Float, PointTyped<units> >, + public BasePoint< Float, PointTyped<units>, CoordTyped<units> >, public units { - typedef BasePoint< Float, PointTyped<units> > Super; + static_assert(IsPixel<units>::value, + "'units' must be a coordinate system tag"); + + typedef CoordTyped<units> Coord; + typedef BasePoint< Float, PointTyped<units>, CoordTyped<units> > Super; - PointTyped() : Super() {} - PointTyped(Float aX, Float aY) : Super(aX, aY) {} - PointTyped(const IntPointTyped<units>& point) : Super(float(point.x), float(point.y)) {} + MOZ_CONSTEXPR PointTyped() : Super() {} + MOZ_CONSTEXPR PointTyped(Float aX, Float aY) : Super(Coord(aX), Coord(aY)) {} + // The mixed-type constructors (Float, Coord) and (Coord, Float) are needed to + // avoid ambiguities because Coord is implicitly convertible to Float. + MOZ_CONSTEXPR PointTyped(Float aX, Coord aY) : Super(Coord(aX), aY) {} + MOZ_CONSTEXPR PointTyped(Coord aX, Float aY) : Super(aX, Coord(aY)) {} + MOZ_CONSTEXPR PointTyped(Coord aX, Coord aY) : Super(aX.value, aY.value) {} + MOZ_CONSTEXPR MOZ_IMPLICIT PointTyped(const IntPointTyped<units>& point) : Super(float(point.x), float(point.y)) {} // XXX When all of the code is ported, the following functions to convert to and from // unknown types should be removed. @@ -63,18 +98,79 @@ typedef PointTyped<UnknownUnits> Point; template<class units> IntPointTyped<units> RoundedToInt(const PointTyped<units>& aPoint) { - return IntPointTyped<units>(NS_lround(aPoint.x), - NS_lround(aPoint.y)); + return IntPointTyped<units>(int32_t(floorf(aPoint.x + 0.5f)), + int32_t(floorf(aPoint.y + 0.5f))); +} + +template<class units> +IntPointTyped<units> TruncatedToInt(const PointTyped<units>& aPoint) { + return IntPointTyped<units>(int32_t(aPoint.x), + int32_t(aPoint.y)); } template<class units> +struct Point3DTyped : + public BasePoint3D< Float, Point3DTyped<units> > { + static_assert(IsPixel<units>::value, + "'units' must be a coordinate system tag"); + + typedef BasePoint3D< Float, Point3DTyped<units> > Super; + + Point3DTyped() : Super() {} + Point3DTyped(Float aX, Float aY, Float aZ) : Super(aX, aY, aZ) {} + + // XXX When all of the code is ported, the following functions to convert to and from + // unknown types should be removed. + + static Point3DTyped<units> FromUnknownPoint(const Point3DTyped<UnknownUnits>& aPoint) { + return Point3DTyped<units>(aPoint.x, aPoint.y, aPoint.z); + } + + Point3DTyped<UnknownUnits> ToUnknownPoint() const { + return Point3DTyped<UnknownUnits>(this->x, this->y, this->z); + } +}; +typedef Point3DTyped<UnknownUnits> Point3D; + +template<class units> +struct Point4DTyped : + public BasePoint4D< Float, Point4DTyped<units> > { + static_assert(IsPixel<units>::value, + "'units' must be a coordinate system tag"); + + typedef BasePoint4D< Float, Point4DTyped<units> > Super; + + Point4DTyped() : Super() {} + Point4DTyped(Float aX, Float aY, Float aZ, Float aW) : Super(aX, aY, aZ, aW) {} + + // XXX When all of the code is ported, the following functions to convert to and from + // unknown types should be removed. + + static Point4DTyped<units> FromUnknownPoint(const Point4DTyped<UnknownUnits>& aPoint) { + return Point4DTyped<units>(aPoint.x, aPoint.y, aPoint.z, aPoint.w); + } + + Point4DTyped<UnknownUnits> ToUnknownPoint() const { + return Point4DTyped<UnknownUnits>(this->x, this->y, this->z, this->w); + } + + PointTyped<units> As2DPoint() { + return PointTyped<units>(this->x / this->w, this->y / this->w); + } +}; +typedef Point4DTyped<UnknownUnits> Point4D; + +template<class units> struct IntSizeTyped : public BaseSize< int32_t, IntSizeTyped<units> >, public units { + static_assert(IsPixel<units>::value, + "'units' must be a coordinate system tag"); + typedef BaseSize< int32_t, IntSizeTyped<units> > Super; - IntSizeTyped() : Super() {} - IntSizeTyped(int32_t aWidth, int32_t aHeight) : Super(aWidth, aHeight) {} + MOZ_CONSTEXPR IntSizeTyped() : Super() {} + MOZ_CONSTEXPR IntSizeTyped(int32_t aWidth, int32_t aHeight) : Super(aWidth, aHeight) {} // XXX When all of the code is ported, the following functions to convert to and from // unknown types should be removed. @@ -93,10 +189,13 @@ template<class units> struct SizeTyped : public BaseSize< Float, SizeTyped<units> >, public units { + static_assert(IsPixel<units>::value, + "'units' must be a coordinate system tag"); + typedef BaseSize< Float, SizeTyped<units> > Super; - SizeTyped() : Super() {} - SizeTyped(Float aWidth, Float aHeight) : Super(aWidth, aHeight) {} + MOZ_CONSTEXPR SizeTyped() : Super() {} + MOZ_CONSTEXPR SizeTyped(Float aWidth, Float aHeight) : Super(aWidth, aHeight) {} explicit SizeTyped(const IntSizeTyped<units>& size) : Super(float(size.width), float(size.height)) {} @@ -113,6 +212,12 @@ struct SizeTyped : }; typedef SizeTyped<UnknownUnits> Size; +template<class units> +IntSizeTyped<units> RoundedToInt(const SizeTyped<units>& aSize) { + return IntSizeTyped<units>(int32_t(floorf(aSize.width + 0.5f)), + int32_t(floorf(aSize.height + 0.5f))); +} + } } |