/* -*- 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/. */ #ifndef MOZILLA_GFX_COORD_H_ #define MOZILLA_GFX_COORD_H_ #include "mozilla/Attributes.h" #include "mozilla/FloatingPoint.h" #include "Types.h" #include "BaseCoord.h" #include #include namespace mozilla { namespace gfx { template struct IntCoordTyped; template struct CoordTyped; } // namespace gfx } // namespace mozilla namespace std { template struct common_type, float> { using type = mozilla::gfx::CoordTyped>; }; template struct common_type, double> { using type = mozilla::gfx::CoordTyped>; }; template struct common_type, int32_t> { using type = mozilla::gfx::IntCoordTyped>; }; template struct common_type, uint32_t> { using type = mozilla::gfx::IntCoordTyped>; }; template struct common_type, T> { using type = mozilla::gfx::CoordTyped>; }; // With a few exceptions, we use CoordTyped values with a float representation. // These are the types for which we have short typedefs like // CSSCoord, and the types expected in most interfaces. // So, for float inputs, keep the results as float even if the other // operand is a double, accepting a slight loss of precision. template struct common_type, T> { using type = mozilla::gfx::CoordTyped; }; } // namespace std namespace mozilla { template struct IsPixel; namespace gfx { // Should only be used to define generic typedefs like Coord, Point, etc. struct UnknownUnits {}; // This is a base class that provides mixed-type operator overloads between // a strongly-typed Coord and a Primitive value. It is needed to avoid // ambiguities at mixed-type call sites, because Coord classes are implicitly // convertible to their underlying value type. As we transition more of our code // to strongly-typed classes, we may be able to remove some or all of these // overloads. template struct CoordOperatorsHelper { // Using SFINAE (Substitution Failure Is Not An Error) to suppress redundant // operators }; template struct CoordOperatorsHelper { friend bool operator==(Coord aA, Primitive aB) { return aA.value == aB; } friend bool operator==(Primitive aA, Coord aB) { return aA == aB.value; } friend bool operator!=(Coord aA, Primitive aB) { return aA.value != aB; } friend bool operator!=(Primitive aA, Coord aB) { return aA != aB.value; } friend auto operator+(Coord aA, Primitive aB) { return aA.value + aB; } friend auto operator+(Primitive aA, Coord aB) { return aA + aB.value; } friend auto operator-(Coord aA, Primitive aB) { return aA.value - aB; } friend auto operator-(Primitive aA, Coord aB) { return aA - aB.value; } friend auto operator*(Coord aCoord, Primitive aScale) { return std::common_type_t(aCoord.value * aScale); } friend auto operator*(Primitive aScale, Coord aCoord) { return aCoord * aScale; } friend auto operator/(Coord aCoord, Primitive aScale) { return std::common_type_t(aCoord.value / aScale); } // 'scale / coord' is intentionally omitted because it doesn't make sense. }; template struct MOZ_EMPTY_BASES IntCoordTyped : public BaseCoord>, public CoordOperatorsHelper, float>, public CoordOperatorsHelper, double> { static_assert(IsPixel::value, "'Units' must be a coordinate system tag"); using Super = BaseCoord>; constexpr IntCoordTyped() : Super() { static_assert(sizeof(IntCoordTyped) == sizeof(Rep), "Would be unfortunate otherwise!"); } template || std::is_enum_v>> constexpr MOZ_IMPLICIT IntCoordTyped(T aValue) : Super(aValue) { static_assert(sizeof(IntCoordTyped) == sizeof(Rep), "Would be unfortunate otherwise!"); } }; template struct MOZ_EMPTY_BASES CoordTyped : public BaseCoord>, public CoordOperatorsHelper, CoordTyped, int32_t>, public CoordOperatorsHelper, CoordTyped, uint32_t>, public CoordOperatorsHelper, CoordTyped, double>, public CoordOperatorsHelper, CoordTyped, float> { static_assert(IsPixel::value, "'Units' must be a coordinate system tag"); using Super = BaseCoord>; constexpr CoordTyped() : Super() { static_assert(sizeof(CoordTyped) == sizeof(F), "Would be unfortunate otherwise!"); } constexpr MOZ_IMPLICIT CoordTyped(F aValue) : Super(aValue) { static_assert(sizeof(CoordTyped) == sizeof(F), "Would be unfortunate otherwise!"); } explicit constexpr CoordTyped(const IntCoordTyped& aCoord) : Super(F(aCoord.value)) { static_assert(sizeof(CoordTyped) == sizeof(F), "Would be unfortunate otherwise!"); } void Round() { this->value = floor(this->value + 0.5); } void Truncate() { this->value = int32_t(this->value); } IntCoordTyped Rounded() const { return IntCoordTyped(int32_t(floor(this->value + 0.5))); } IntCoordTyped Truncated() const { return IntCoordTyped(int32_t(this->value)); } }; typedef CoordTyped Coord; } // namespace gfx template static MOZ_ALWAYS_INLINE bool FuzzyEqualsAdditive( gfx::CoordTyped aValue1, gfx::CoordTyped aValue2, gfx::CoordTyped aEpsilon = detail::FuzzyEqualsEpsilon::value()) { return FuzzyEqualsAdditive(aValue1.value, aValue2.value, aEpsilon.value); } template static MOZ_ALWAYS_INLINE bool FuzzyEqualsMultiplicative( gfx::CoordTyped aValue1, gfx::CoordTyped aValue2, gfx::CoordTyped aEpsilon = detail::FuzzyEqualsEpsilon::value()) { return FuzzyEqualsMultiplicative(aValue1.value, aValue2.value, aEpsilon.value); } } // namespace mozilla #endif /* MOZILLA_GFX_COORD_H_ */