Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef MOZILLA_GFX_SCALEFACTOR_H_
7 : #define MOZILLA_GFX_SCALEFACTOR_H_
8 :
9 : #include "mozilla/Attributes.h"
10 :
11 : #include "gfxPoint.h"
12 :
13 : namespace mozilla {
14 : namespace gfx {
15 :
16 : /*
17 : * This class represents a scaling factor between two different pixel unit
18 : * systems. This is effectively a type-safe float, intended to be used in
19 : * combination with the known-type instances of gfx::Point, gfx::Rect, etc.
20 : *
21 : * This class is meant to be used in cases where a single scale applies to
22 : * both the x and y axes. For cases where two diferent scales apply, use
23 : * ScaleFactors2D.
24 : */
25 : template<class src, class dst>
26 : struct ScaleFactor {
27 : float scale;
28 :
29 246 : constexpr ScaleFactor() : scale(1.0) {}
30 179 : constexpr ScaleFactor(const ScaleFactor<src, dst>& aCopy) : scale(aCopy.scale) {}
31 487 : explicit constexpr ScaleFactor(float aScale) : scale(aScale) {}
32 :
33 : ScaleFactor<dst, src> Inverse() {
34 : return ScaleFactor<dst, src>(1 / scale);
35 : }
36 :
37 65 : bool operator==(const ScaleFactor<src, dst>& aOther) const {
38 65 : return scale == aOther.scale;
39 : }
40 :
41 : bool operator!=(const ScaleFactor<src, dst>& aOther) const {
42 : return !(*this == aOther);
43 : }
44 :
45 3 : bool operator<(const ScaleFactor<src, dst>& aOther) const {
46 3 : return scale < aOther.scale;
47 : }
48 :
49 0 : bool operator<=(const ScaleFactor<src, dst>& aOther) const {
50 0 : return scale <= aOther.scale;
51 : }
52 :
53 3 : bool operator>(const ScaleFactor<src, dst>& aOther) const {
54 3 : return scale > aOther.scale;
55 : }
56 :
57 0 : bool operator>=(const ScaleFactor<src, dst>& aOther) const {
58 0 : return scale >= aOther.scale;
59 : }
60 :
61 : template<class other>
62 0 : ScaleFactor<other, dst> operator/(const ScaleFactor<src, other>& aOther) const {
63 0 : return ScaleFactor<other, dst>(scale / aOther.scale);
64 : }
65 :
66 : template<class other>
67 6 : ScaleFactor<src, other> operator/(const ScaleFactor<other, dst>& aOther) const {
68 6 : return ScaleFactor<src, other>(scale / aOther.scale);
69 : }
70 :
71 : template<class other>
72 9 : ScaleFactor<src, other> operator*(const ScaleFactor<dst, other>& aOther) const {
73 9 : return ScaleFactor<src, other>(scale * aOther.scale);
74 : }
75 :
76 : template<class other>
77 0 : ScaleFactor<other, dst> operator*(const ScaleFactor<other, src>& aOther) const {
78 0 : return ScaleFactor<other, dst>(scale * aOther.scale);
79 : }
80 : };
81 :
82 : } // namespace gfx
83 : } // namespace mozilla
84 :
85 : #endif /* MOZILLA_GFX_SCALEFACTOR_H_ */
|