2017-10-28 02:10:06 +03:00
|
|
|
/* -*- 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
|
2015-03-06 21:53:47 +03:00
|
|
|
* 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_SCALEFACTORS2D_H_
|
|
|
|
#define MOZILLA_GFX_SCALEFACTORS2D_H_
|
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
#include "mozilla/gfx/ScaleFactor.h"
|
2022-04-30 04:35:24 +03:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2015-03-06 21:53:47 +03:00
|
|
|
|
|
|
|
#include "gfxPoint.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This class is like ScaleFactor, but allows different scales on the x and
|
|
|
|
* y axes.
|
|
|
|
*/
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Src, class Dst, class T>
|
2022-04-30 04:35:23 +03:00
|
|
|
struct BaseScaleFactors2D {
|
|
|
|
T xScale;
|
|
|
|
T yScale;
|
2015-03-06 21:53:47 +03:00
|
|
|
|
2022-04-30 04:35:23 +03:00
|
|
|
constexpr BaseScaleFactors2D() : xScale(1.0), yScale(1.0) {}
|
|
|
|
constexpr BaseScaleFactors2D(const BaseScaleFactors2D& aCopy)
|
2015-03-06 21:53:47 +03:00
|
|
|
: xScale(aCopy.xScale), yScale(aCopy.yScale) {}
|
2022-04-30 04:35:23 +03:00
|
|
|
constexpr BaseScaleFactors2D(T aXScale, T aYScale)
|
2015-03-06 21:53:47 +03:00
|
|
|
: xScale(aXScale), yScale(aYScale) {}
|
|
|
|
// Layout code often uses gfxSize to represent a pair of x/y scales.
|
2022-04-30 04:35:23 +03:00
|
|
|
explicit constexpr BaseScaleFactors2D(const gfxSize& aSize)
|
2015-03-06 21:53:47 +03:00
|
|
|
: xScale(aSize.width), yScale(aSize.height) {}
|
|
|
|
|
|
|
|
// "Upgrade" from a ScaleFactor.
|
|
|
|
// This is deliberately 'explicit' so that the treatment of a single scale
|
|
|
|
// number as both the x- and y-scale in a context where they are allowed to
|
|
|
|
// be different, is more visible.
|
2022-05-11 20:23:15 +03:00
|
|
|
explicit constexpr BaseScaleFactors2D(const ScaleFactor<Src, Dst>& aScale)
|
2015-03-06 21:53:47 +03:00
|
|
|
: xScale(aScale.scale), yScale(aScale.scale) {}
|
|
|
|
|
|
|
|
bool AreScalesSame() const {
|
|
|
|
return FuzzyEqualsMultiplicative(xScale, yScale);
|
|
|
|
}
|
|
|
|
|
2022-04-30 04:35:23 +03:00
|
|
|
// Convert the underlying floating point type storing the scale factors
|
|
|
|
// to that of NewT.
|
|
|
|
template <typename NewT>
|
2022-05-11 20:23:15 +03:00
|
|
|
BaseScaleFactors2D<Src, Dst, NewT> ConvertTo() const {
|
|
|
|
return BaseScaleFactors2D<Src, Dst, NewT>(NewT(xScale), NewT(yScale));
|
2022-04-30 04:35:23 +03:00
|
|
|
}
|
|
|
|
|
2015-03-06 21:53:47 +03:00
|
|
|
// Convert to a ScaleFactor. Asserts that the scales are, in fact, equal.
|
2022-05-11 20:23:15 +03:00
|
|
|
ScaleFactor<Src, Dst> ToScaleFactor() const {
|
2022-04-30 04:35:23 +03:00
|
|
|
// Avoid implicit narrowing from double to float. An explicit conversion
|
|
|
|
// may be done with `scales.ConvertTo<float>().ToScaleFactor()` if desired.
|
|
|
|
static_assert(std::is_same_v<T, float>);
|
2015-03-06 21:53:47 +03:00
|
|
|
MOZ_ASSERT(AreScalesSame());
|
2022-05-11 20:23:15 +03:00
|
|
|
return ScaleFactor<Src, Dst>(xScale);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-04-30 04:35:24 +03:00
|
|
|
// Convert to a SizeTyped. Eventually, we should replace all uses of SizeTyped
|
|
|
|
// to represent scales with ScaleFactors2D, and remove this function.
|
|
|
|
SizeTyped<UnknownUnits, T> ToSize() const {
|
|
|
|
return SizeTyped<UnknownUnits, T>(xScale, yScale);
|
|
|
|
}
|
|
|
|
|
2022-04-30 04:35:23 +03:00
|
|
|
BaseScaleFactors2D& operator=(const BaseScaleFactors2D&) = default;
|
2019-12-13 20:58:55 +03:00
|
|
|
|
2022-04-30 04:35:23 +03:00
|
|
|
bool operator==(const BaseScaleFactors2D& aOther) const {
|
2015-03-06 21:53:47 +03:00
|
|
|
return xScale == aOther.xScale && yScale == aOther.yScale;
|
|
|
|
}
|
|
|
|
|
2022-04-30 04:35:23 +03:00
|
|
|
bool operator!=(const BaseScaleFactors2D& aOther) const {
|
2015-03-06 21:53:47 +03:00
|
|
|
return !(*this == aOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
2022-04-30 04:35:23 +03:00
|
|
|
const BaseScaleFactors2D& aScale) {
|
2015-03-06 21:53:47 +03:00
|
|
|
if (aScale.AreScalesSame()) {
|
|
|
|
return aStream << aScale.xScale;
|
|
|
|
} else {
|
|
|
|
return aStream << '(' << aScale.xScale << ',' << aScale.yScale << ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
BaseScaleFactors2D<Other, Dst, T> operator/(
|
|
|
|
const BaseScaleFactors2D<Src, Other, T>& aOther) const {
|
|
|
|
return BaseScaleFactors2D<Other, Dst, T>(xScale / aOther.xScale,
|
2022-04-30 04:35:23 +03:00
|
|
|
yScale / aOther.yScale);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
BaseScaleFactors2D<Src, Other, T> operator/(
|
|
|
|
const BaseScaleFactors2D<Other, Dst, T>& aOther) const {
|
|
|
|
return BaseScaleFactors2D<Src, Other, T>(xScale / aOther.xScale,
|
2022-04-30 04:35:23 +03:00
|
|
|
yScale / aOther.yScale);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
BaseScaleFactors2D<Src, Other, T> operator*(
|
|
|
|
const BaseScaleFactors2D<Dst, Other, T>& aOther) const {
|
|
|
|
return BaseScaleFactors2D<Src, Other, T>(xScale * aOther.xScale,
|
2022-04-30 04:35:23 +03:00
|
|
|
yScale * aOther.yScale);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
BaseScaleFactors2D<Other, Dst, T> operator*(
|
|
|
|
const BaseScaleFactors2D<Other, Src, T>& aOther) const {
|
|
|
|
return BaseScaleFactors2D<Other, Dst, T>(xScale * aOther.xScale,
|
2022-04-30 04:35:23 +03:00
|
|
|
yScale * aOther.yScale);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
BaseScaleFactors2D<Src, Src, T> operator*(
|
|
|
|
const BaseScaleFactors2D<Dst, Src, T>& aOther) const {
|
|
|
|
return BaseScaleFactors2D<Src, Src, T>(xScale * aOther.xScale,
|
2022-05-06 02:19:18 +03:00
|
|
|
yScale * aOther.yScale);
|
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
BaseScaleFactors2D<Src, Other, T> operator*(
|
|
|
|
const ScaleFactor<Dst, Other>& aOther) const {
|
|
|
|
return *this * BaseScaleFactors2D<Dst, Other, T>(aOther);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
BaseScaleFactors2D<Other, Dst, T> operator*(
|
|
|
|
const ScaleFactor<Other, Src>& aOther) const {
|
|
|
|
return *this * BaseScaleFactors2D<Other, Src, T>(aOther);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
BaseScaleFactors2D<Src, Src, T> operator*(
|
|
|
|
const ScaleFactor<Dst, Src>& aOther) const {
|
|
|
|
return *this * BaseScaleFactors2D<Dst, Src, T>(aOther);
|
2022-05-06 02:19:18 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
BaseScaleFactors2D<Src, Other, T> operator/(
|
|
|
|
const ScaleFactor<Other, Dst>& aOther) const {
|
|
|
|
return *this / BaseScaleFactors2D<Other, Dst, T>(aOther);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
BaseScaleFactors2D<Other, Dst, T> operator/(
|
|
|
|
const ScaleFactor<Src, Other>& aOther) const {
|
|
|
|
return *this / BaseScaleFactors2D<Src, Other, T>(aOther);
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
friend BaseScaleFactors2D<Other, Dst, T> operator*(
|
|
|
|
const ScaleFactor<Other, Src>& aA, const BaseScaleFactors2D& aB) {
|
|
|
|
return BaseScaleFactors2D<Other, Src, T>(aA) * aB;
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Other>
|
|
|
|
friend BaseScaleFactors2D<Other, Src, T> operator/(
|
|
|
|
const ScaleFactor<Other, Dst>& aA, const BaseScaleFactors2D& aB) {
|
|
|
|
return BaseScaleFactors2D<Other, Src, T>(aA) / aB;
|
2015-03-06 21:53:47 +03:00
|
|
|
}
|
2022-05-19 04:45:52 +03:00
|
|
|
|
|
|
|
static BaseScaleFactors2D<Src, Dst, T> FromUnknownScale(
|
|
|
|
const BaseScaleFactors2D<UnknownUnits, UnknownUnits, T>& scale) {
|
|
|
|
return BaseScaleFactors2D<Src, Dst, T>(scale.xScale, scale.yScale);
|
|
|
|
}
|
2022-06-02 02:19:11 +03:00
|
|
|
|
|
|
|
friend BaseScaleFactors2D Min(const BaseScaleFactors2D& aA,
|
|
|
|
const BaseScaleFactors2D& aB) {
|
|
|
|
return BaseScaleFactors2D(std::min(aA.xScale, aB.xScale),
|
|
|
|
std::min(aA.yScale, aB.yScale));
|
|
|
|
}
|
|
|
|
|
|
|
|
friend BaseScaleFactors2D Max(const BaseScaleFactors2D& aA,
|
|
|
|
const BaseScaleFactors2D& aB) {
|
|
|
|
return BaseScaleFactors2D(std::max(aA.xScale, aB.xScale),
|
|
|
|
std::max(aA.yScale, aB.yScale));
|
|
|
|
}
|
2015-03-06 21:53:47 +03:00
|
|
|
};
|
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Src, class Dst>
|
|
|
|
using ScaleFactors2D = BaseScaleFactors2D<Src, Dst, float>;
|
2022-04-30 04:35:23 +03:00
|
|
|
|
2022-05-11 20:23:15 +03:00
|
|
|
template <class Src, class Dst>
|
|
|
|
using ScaleFactors2DDouble = BaseScaleFactors2D<Src, Dst, double>;
|
2022-04-30 04:35:23 +03:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2015-03-06 21:53:47 +03:00
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_SCALEFACTORS2D_H_ */
|