|
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 * Note that some parts of the code that pre-date this class used separate |
|
22 * scaling factors for the x and y axes. However, at runtime these values |
|
23 * were always expected to be the same, so this class uses only one scale |
|
24 * factor for both axes. The two constructors that take two-axis scaling |
|
25 * factors check to ensure that this assertion holds. |
|
26 */ |
|
27 template<class src, class dst> |
|
28 struct ScaleFactor { |
|
29 float scale; |
|
30 |
|
31 MOZ_CONSTEXPR ScaleFactor() : scale(1.0) {} |
|
32 MOZ_CONSTEXPR ScaleFactor(const ScaleFactor<src, dst>& aCopy) : scale(aCopy.scale) {} |
|
33 explicit MOZ_CONSTEXPR ScaleFactor(float aScale) : scale(aScale) {} |
|
34 |
|
35 explicit ScaleFactor(float aX, float aY) : scale(aX) { |
|
36 MOZ_ASSERT(fabs(aX - aY) < 1e-6); |
|
37 } |
|
38 |
|
39 ScaleFactor<dst, src> Inverse() { |
|
40 return ScaleFactor<dst, src>(1 / scale); |
|
41 } |
|
42 |
|
43 bool operator==(const ScaleFactor<src, dst>& aOther) const { |
|
44 return scale == aOther.scale; |
|
45 } |
|
46 |
|
47 bool operator!=(const ScaleFactor<src, dst>& aOther) const { |
|
48 return !(*this == aOther); |
|
49 } |
|
50 |
|
51 bool operator<(const ScaleFactor<src, dst>& aOther) const { |
|
52 return scale < aOther.scale; |
|
53 } |
|
54 |
|
55 bool operator<=(const ScaleFactor<src, dst>& aOther) const { |
|
56 return scale <= aOther.scale; |
|
57 } |
|
58 |
|
59 bool operator>(const ScaleFactor<src, dst>& aOther) const { |
|
60 return scale > aOther.scale; |
|
61 } |
|
62 |
|
63 bool operator>=(const ScaleFactor<src, dst>& aOther) const { |
|
64 return scale >= aOther.scale; |
|
65 } |
|
66 |
|
67 template<class other> |
|
68 ScaleFactor<other, dst> operator/(const ScaleFactor<src, other>& aOther) const { |
|
69 return ScaleFactor<other, dst>(scale / aOther.scale); |
|
70 } |
|
71 |
|
72 template<class other> |
|
73 ScaleFactor<src, other> operator/(const ScaleFactor<other, dst>& aOther) const { |
|
74 return ScaleFactor<src, other>(scale / aOther.scale); |
|
75 } |
|
76 |
|
77 template<class other> |
|
78 ScaleFactor<src, other> operator*(const ScaleFactor<dst, other>& aOther) const { |
|
79 return ScaleFactor<src, other>(scale * aOther.scale); |
|
80 } |
|
81 |
|
82 template<class other> |
|
83 ScaleFactor<other, dst> operator*(const ScaleFactor<other, src>& aOther) const { |
|
84 return ScaleFactor<other, dst>(scale * aOther.scale); |
|
85 } |
|
86 }; |
|
87 |
|
88 } |
|
89 } |
|
90 |
|
91 #endif /* MOZILLA_GFX_SCALEFACTOR_H_ */ |