1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_GFX_SCALEFACTOR_H_
8 #define MOZILLA_GFX_SCALEFACTOR_H_
12 #include "mozilla/Attributes.h"
20 * This class represents a scaling factor between two different pixel unit
21 * systems. This is effectively a type-safe float, intended to be used in
22 * combination with the known-type instances of gfx::Point, gfx::Rect, etc.
24 * This class is meant to be used in cases where a single scale applies to
25 * both the x and y axes. For cases where two diferent scales apply, use
28 template <class Src
, class Dst
>
32 constexpr ScaleFactor() : scale(1.0) {}
33 constexpr ScaleFactor(const ScaleFactor
<Src
, Dst
>& aCopy
)
34 : scale(aCopy
.scale
) {}
35 explicit constexpr ScaleFactor(float aScale
) : scale(aScale
) {}
37 ScaleFactor
<Dst
, Src
> Inverse() { return ScaleFactor
<Dst
, Src
>(1 / scale
); }
39 ScaleFactor
<Src
, Dst
>& operator=(const ScaleFactor
<Src
, Dst
>&) = default;
41 bool operator==(const ScaleFactor
<Src
, Dst
>& aOther
) const {
42 return scale
== aOther
.scale
;
45 bool operator!=(const ScaleFactor
<Src
, Dst
>& aOther
) const {
46 return !(*this == aOther
);
49 bool operator<(const ScaleFactor
<Src
, Dst
>& aOther
) const {
50 return scale
< aOther
.scale
;
53 bool operator<=(const ScaleFactor
<Src
, Dst
>& aOther
) const {
54 return scale
<= aOther
.scale
;
57 bool operator>(const ScaleFactor
<Src
, Dst
>& aOther
) const {
58 return scale
> aOther
.scale
;
61 bool operator>=(const ScaleFactor
<Src
, Dst
>& aOther
) const {
62 return scale
>= aOther
.scale
;
65 template <class Other
>
66 ScaleFactor
<Other
, Dst
> operator/(
67 const ScaleFactor
<Src
, Other
>& aOther
) const {
68 return ScaleFactor
<Other
, Dst
>(scale
/ aOther
.scale
);
71 template <class Other
>
72 ScaleFactor
<Src
, Other
> operator/(
73 const ScaleFactor
<Other
, Dst
>& aOther
) const {
74 return ScaleFactor
<Src
, Other
>(scale
/ aOther
.scale
);
77 template <class Other
>
78 ScaleFactor
<Src
, Other
> operator*(
79 const ScaleFactor
<Dst
, Other
>& aOther
) const {
80 return ScaleFactor
<Src
, Other
>(scale
* aOther
.scale
);
83 template <class Other
>
84 ScaleFactor
<Other
, Dst
> operator*(
85 const ScaleFactor
<Other
, Src
>& aOther
) const {
86 return ScaleFactor
<Other
, Dst
>(scale
* aOther
.scale
);
89 friend std::ostream
& operator<<(std::ostream
& aStream
,
90 const ScaleFactor
<Src
, Dst
>& aSF
) {
91 return aStream
<< aSF
.scale
;
96 } // namespace mozilla
98 #endif /* MOZILLA_GFX_SCALEFACTOR_H_ */