Update configs. IGNORE BROKEN CHANGESETS CLOSED TREE NO BUG a=release ba=release
[gecko.git] / gfx / 2d / ScaleFactor.h
blob4d7346d2009347da228ae7b8b38c9e29afb436c3
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_
10 #include <ostream>
12 #include "mozilla/Attributes.h"
14 #include "gfxPoint.h"
16 namespace mozilla {
17 namespace gfx {
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
26 * ScaleFactors2D.
28 template <class Src, class Dst>
29 struct ScaleFactor {
30 float scale;
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;
95 } // namespace gfx
96 } // namespace mozilla
98 #endif /* MOZILLA_GFX_SCALEFACTOR_H_ */