From 9e3c08db40b8916968b9f30096c7be3f00ce9647 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:44:51 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- gfx/2d/Coord.h | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 gfx/2d/Coord.h (limited to 'gfx/2d/Coord.h') diff --git a/gfx/2d/Coord.h b/gfx/2d/Coord.h new file mode 100644 index 0000000000..643cf4969a --- /dev/null +++ b/gfx/2d/Coord.h @@ -0,0 +1,176 @@ +/* -*- 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 { + +template +struct IsPixel; + +namespace gfx { + +// Should only be used to define generic typedefs like Coord, Point, etc. +struct UnknownUnits {}; + +template +struct IntCoordTyped; +template +struct CoordTyped; + +// CommonType is a metafunction that returns the type of the +// result of an arithmetic operation on the underlying type of a strongly-typed +// coordinate type 'Coord', and a primitive type 'Primitive'. C++ rules for +// arithmetic conversions are designed to avoid losing information - for +// example, the result of adding an int and a float is a float - and we want +// the same behaviour when mixing our coordinate types with primitive types. +// We get C++ to compute the desired result type using 'decltype'. + +template +struct CommonType; + +template +struct CommonType, Primitive> { + using type = decltype(Rep() + Primitive()); +}; + +template +struct CommonType, Primitive> { + using type = decltype(F() + Primitive()); +}; + +// 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; } + + using result_type = typename CommonType::type; + + friend result_type operator+(Coord aA, Primitive aB) { return aA.value + aB; } + friend result_type operator+(Primitive aA, Coord aB) { return aA + aB.value; } + friend result_type operator-(Coord aA, Primitive aB) { return aA.value - aB; } + friend result_type operator-(Primitive aA, Coord aB) { return aA - aB.value; } + friend result_type operator*(Coord aCoord, Primitive aScale) { + return aCoord.value * aScale; + } + friend result_type operator*(Primitive aScale, Coord aCoord) { + return aScale * aCoord.value; + } + friend result_type operator/(Coord aCoord, Primitive aScale) { + return 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!"); + } + constexpr MOZ_IMPLICIT IntCoordTyped(Rep 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_ */ -- cgit v1.2.3