Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 MOZ_UNITS_H_
8 #define MOZ_UNITS_H_
10 #include "mozilla/gfx/Point.h"
11 #include "mozilla/gfx/Rect.h"
12 #include "mozilla/gfx/ScaleFactor.h"
13 #include "nsRect.h"
14 #include "nsMargin.h"
15 #include "mozilla/AppUnits.h"
17 namespace mozilla {
19 struct CSSPixel;
20 struct LayoutDevicePixel;
21 struct LayerPixel;
22 struct ScreenPixel;
24 typedef gfx::PointTyped<CSSPixel> CSSPoint;
25 typedef gfx::IntPointTyped<CSSPixel> CSSIntPoint;
26 typedef gfx::SizeTyped<CSSPixel> CSSSize;
27 typedef gfx::IntSizeTyped<CSSPixel> CSSIntSize;
28 typedef gfx::RectTyped<CSSPixel> CSSRect;
29 typedef gfx::IntRectTyped<CSSPixel> CSSIntRect;
30 typedef gfx::MarginTyped<CSSPixel> CSSMargin;
31 typedef gfx::IntMarginTyped<CSSPixel> CSSIntMargin;
33 typedef gfx::PointTyped<LayoutDevicePixel> LayoutDevicePoint;
34 typedef gfx::IntPointTyped<LayoutDevicePixel> LayoutDeviceIntPoint;
35 typedef gfx::SizeTyped<LayoutDevicePixel> LayoutDeviceSize;
36 typedef gfx::IntSizeTyped<LayoutDevicePixel> LayoutDeviceIntSize;
37 typedef gfx::RectTyped<LayoutDevicePixel> LayoutDeviceRect;
38 typedef gfx::IntRectTyped<LayoutDevicePixel> LayoutDeviceIntRect;
39 typedef gfx::MarginTyped<LayoutDevicePixel> LayoutDeviceMargin;
40 typedef gfx::IntMarginTyped<LayoutDevicePixel> LayoutDeviceIntMargin;
42 typedef gfx::PointTyped<LayerPixel> LayerPoint;
43 typedef gfx::IntPointTyped<LayerPixel> LayerIntPoint;
44 typedef gfx::SizeTyped<LayerPixel> LayerSize;
45 typedef gfx::IntSizeTyped<LayerPixel> LayerIntSize;
46 typedef gfx::RectTyped<LayerPixel> LayerRect;
47 typedef gfx::IntRectTyped<LayerPixel> LayerIntRect;
48 typedef gfx::MarginTyped<LayerPixel> LayerMargin;
49 typedef gfx::IntMarginTyped<LayerPixel> LayerIntMargin;
51 typedef gfx::PointTyped<ScreenPixel> ScreenPoint;
52 typedef gfx::IntPointTyped<ScreenPixel> ScreenIntPoint;
53 typedef gfx::SizeTyped<ScreenPixel> ScreenSize;
54 typedef gfx::IntSizeTyped<ScreenPixel> ScreenIntSize;
55 typedef gfx::RectTyped<ScreenPixel> ScreenRect;
56 typedef gfx::IntRectTyped<ScreenPixel> ScreenIntRect;
57 typedef gfx::MarginTyped<ScreenPixel> ScreenMargin;
58 typedef gfx::IntMarginTyped<ScreenPixel> ScreenIntMargin;
60 typedef gfx::ScaleFactor<CSSPixel, LayoutDevicePixel> CSSToLayoutDeviceScale;
61 typedef gfx::ScaleFactor<LayoutDevicePixel, CSSPixel> LayoutDeviceToCSSScale;
62 typedef gfx::ScaleFactor<CSSPixel, LayerPixel> CSSToLayerScale;
63 typedef gfx::ScaleFactor<LayerPixel, CSSPixel> LayerToCSSScale;
64 typedef gfx::ScaleFactor<CSSPixel, ScreenPixel> CSSToScreenScale;
65 typedef gfx::ScaleFactor<ScreenPixel, CSSPixel> ScreenToCSSScale;
66 typedef gfx::ScaleFactor<LayoutDevicePixel, LayerPixel> LayoutDeviceToLayerScale;
67 typedef gfx::ScaleFactor<LayerPixel, LayoutDevicePixel> LayerToLayoutDeviceScale;
68 typedef gfx::ScaleFactor<LayoutDevicePixel, ScreenPixel> LayoutDeviceToScreenScale;
69 typedef gfx::ScaleFactor<ScreenPixel, LayoutDevicePixel> ScreenToLayoutDeviceScale;
70 typedef gfx::ScaleFactor<LayerPixel, ScreenPixel> LayerToScreenScale;
71 typedef gfx::ScaleFactor<ScreenPixel, LayerPixel> ScreenToLayerScale;
73 /*
74 * The pixels that content authors use to specify sizes in.
75 */
76 struct CSSPixel {
78 // Conversions from app units
80 static CSSPoint FromAppUnits(const nsPoint& aPoint) {
81 return CSSPoint(NSAppUnitsToFloatPixels(aPoint.x, float(AppUnitsPerCSSPixel())),
82 NSAppUnitsToFloatPixels(aPoint.y, float(AppUnitsPerCSSPixel())));
83 }
85 static CSSRect FromAppUnits(const nsRect& aRect) {
86 return CSSRect(NSAppUnitsToFloatPixels(aRect.x, float(AppUnitsPerCSSPixel())),
87 NSAppUnitsToFloatPixels(aRect.y, float(AppUnitsPerCSSPixel())),
88 NSAppUnitsToFloatPixels(aRect.width, float(AppUnitsPerCSSPixel())),
89 NSAppUnitsToFloatPixels(aRect.height, float(AppUnitsPerCSSPixel())));
90 }
92 static CSSMargin FromAppUnits(const nsMargin& aMargin) {
93 return CSSMargin(NSAppUnitsToFloatPixels(aMargin.top, float(AppUnitsPerCSSPixel())),
94 NSAppUnitsToFloatPixels(aMargin.right, float(AppUnitsPerCSSPixel())),
95 NSAppUnitsToFloatPixels(aMargin.bottom, float(AppUnitsPerCSSPixel())),
96 NSAppUnitsToFloatPixels(aMargin.left, float(AppUnitsPerCSSPixel())));
97 }
99 static CSSIntPoint FromAppUnitsRounded(const nsPoint& aPoint) {
100 return CSSIntPoint(NSAppUnitsToIntPixels(aPoint.x, float(AppUnitsPerCSSPixel())),
101 NSAppUnitsToIntPixels(aPoint.y, float(AppUnitsPerCSSPixel())));
102 }
104 static CSSIntSize FromAppUnitsRounded(const nsSize& aSize)
105 {
106 return CSSIntSize(NSAppUnitsToIntPixels(aSize.width, float(AppUnitsPerCSSPixel())),
107 NSAppUnitsToIntPixels(aSize.height, float(AppUnitsPerCSSPixel())));
108 }
110 static CSSIntRect FromAppUnitsRounded(const nsRect& aRect) {
111 return CSSIntRect(NSAppUnitsToIntPixels(aRect.x, float(AppUnitsPerCSSPixel())),
112 NSAppUnitsToIntPixels(aRect.y, float(AppUnitsPerCSSPixel())),
113 NSAppUnitsToIntPixels(aRect.width, float(AppUnitsPerCSSPixel())),
114 NSAppUnitsToIntPixels(aRect.height, float(AppUnitsPerCSSPixel())));
115 }
117 // Conversions to app units
119 static nsPoint ToAppUnits(const CSSPoint& aPoint) {
120 return nsPoint(NSToCoordRoundWithClamp(aPoint.x * float(AppUnitsPerCSSPixel())),
121 NSToCoordRoundWithClamp(aPoint.y * float(AppUnitsPerCSSPixel())));
122 }
124 static nsPoint ToAppUnits(const CSSIntPoint& aPoint) {
125 return nsPoint(NSToCoordRoundWithClamp(float(aPoint.x) * float(AppUnitsPerCSSPixel())),
126 NSToCoordRoundWithClamp(float(aPoint.y) * float(AppUnitsPerCSSPixel())));
127 }
129 static nsRect ToAppUnits(const CSSRect& aRect) {
130 return nsRect(NSToCoordRoundWithClamp(aRect.x * float(AppUnitsPerCSSPixel())),
131 NSToCoordRoundWithClamp(aRect.y * float(AppUnitsPerCSSPixel())),
132 NSToCoordRoundWithClamp(aRect.width * float(AppUnitsPerCSSPixel())),
133 NSToCoordRoundWithClamp(aRect.height * float(AppUnitsPerCSSPixel())));
134 }
135 };
137 /*
138 * The pixels that are referred to as "device pixels" in layout code. In
139 * general this is obtained by converting a value in app units value by the
140 * AppUnitsPerDevPixel() value. The size of these pixels
141 * are affected by:
142 * 1) the "full zoom" (see nsPresContext::SetFullZoom)
143 * 2) the "widget scale" (see nsIWidget::GetDefaultScale)
144 */
145 struct LayoutDevicePixel {
146 static LayoutDeviceIntPoint FromUntyped(const nsIntPoint& aPoint) {
147 return LayoutDeviceIntPoint(aPoint.x, aPoint.y);
148 }
150 static nsIntPoint ToUntyped(const LayoutDeviceIntPoint& aPoint) {
151 return nsIntPoint(aPoint.x, aPoint.y);
152 }
154 static LayoutDeviceIntRect FromUntyped(const nsIntRect& aRect) {
155 return LayoutDeviceIntRect(aRect.x, aRect.y, aRect.width, aRect.height);
156 }
158 static nsIntRect ToUntyped(const LayoutDeviceIntRect& aRect) {
159 return nsIntRect(aRect.x, aRect.y, aRect.width, aRect.height);
160 }
162 static LayoutDeviceRect FromAppUnits(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
163 return LayoutDeviceRect(NSAppUnitsToFloatPixels(aRect.x, float(aAppUnitsPerDevPixel)),
164 NSAppUnitsToFloatPixels(aRect.y, float(aAppUnitsPerDevPixel)),
165 NSAppUnitsToFloatPixels(aRect.width, float(aAppUnitsPerDevPixel)),
166 NSAppUnitsToFloatPixels(aRect.height, float(aAppUnitsPerDevPixel)));
167 }
169 static LayoutDeviceIntPoint FromAppUnitsRounded(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
170 return LayoutDeviceIntPoint(NSAppUnitsToIntPixels(aPoint.x, aAppUnitsPerDevPixel),
171 NSAppUnitsToIntPixels(aPoint.y, aAppUnitsPerDevPixel));
172 }
174 static LayoutDeviceIntPoint FromAppUnitsToNearest(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
175 return FromUntyped(aPoint.ToNearestPixels(aAppUnitsPerDevPixel));
176 }
178 static LayoutDeviceIntRect FromAppUnitsToNearest(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
179 return FromUntyped(aRect.ToNearestPixels(aAppUnitsPerDevPixel));
180 }
181 };
183 /*
184 * The pixels that layout rasterizes and delivers to the graphics code.
185 * These are generally referred to as "device pixels" in layout code. Layer
186 * pixels are affected by:
187 * 1) the "display resolution" (see nsIPresShell::SetResolution)
188 * 2) the "full zoom" (see nsPresContext::SetFullZoom)
189 * 3) the "widget scale" (see nsIWidget::GetDefaultScale)
190 */
191 struct LayerPixel {
192 };
194 /*
195 * The pixels that are displayed on the screen.
196 * On non-OMTC platforms this should be equivalent to LayerPixel units.
197 * On OMTC platforms these may diverge from LayerPixel units temporarily,
198 * while an asynchronous zoom is happening, but should eventually converge
199 * back to LayerPixel units. Some variables (such as those representing
200 * chrome UI element sizes) that are not subject to content zoom should
201 * generally be represented in ScreenPixel units.
202 */
203 struct ScreenPixel {
204 };
206 // Operators to apply ScaleFactors directly to Points, Rects, Sizes and Margins
208 template<class src, class dst>
209 gfx::PointTyped<dst> operator*(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactor<src, dst>& aScale) {
210 return gfx::PointTyped<dst>(aPoint.x * aScale.scale,
211 aPoint.y * aScale.scale);
212 }
214 template<class src, class dst>
215 gfx::PointTyped<dst> operator/(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactor<dst, src>& aScale) {
216 return gfx::PointTyped<dst>(aPoint.x / aScale.scale,
217 aPoint.y / aScale.scale);
218 }
220 template<class src, class dst>
221 gfx::RectTyped<dst> operator*(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactor<src, dst>& aScale) {
222 return gfx::RectTyped<dst>(aRect.x * aScale.scale,
223 aRect.y * aScale.scale,
224 aRect.width * aScale.scale,
225 aRect.height * aScale.scale);
226 }
228 template<class src, class dst>
229 gfx::RectTyped<dst> operator/(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactor<dst, src>& aScale) {
230 return gfx::RectTyped<dst>(aRect.x / aScale.scale,
231 aRect.y / aScale.scale,
232 aRect.width / aScale.scale,
233 aRect.height / aScale.scale);
234 }
236 template<class src, class dst>
237 gfx::RectTyped<dst> operator*(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactor<src, dst>& aScale) {
238 return gfx::RectTyped<dst>(float(aRect.x) * aScale.scale,
239 float(aRect.y) * aScale.scale,
240 float(aRect.width) * aScale.scale,
241 float(aRect.height) * aScale.scale);
242 }
244 template<class src, class dst>
245 gfx::RectTyped<dst> operator/(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactor<dst, src>& aScale) {
246 return gfx::RectTyped<dst>(float(aRect.x) / aScale.scale,
247 float(aRect.y) / aScale.scale,
248 float(aRect.width) / aScale.scale,
249 float(aRect.height) / aScale.scale);
250 }
252 template<class src, class dst>
253 gfx::SizeTyped<dst> operator*(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
254 return gfx::SizeTyped<dst>(aSize.width * aScale.scale,
255 aSize.height * aScale.scale);
256 }
258 template<class src, class dst>
259 gfx::SizeTyped<dst> operator/(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
260 return gfx::SizeTyped<dst>(aSize.width / aScale.scale,
261 aSize.height / aScale.scale);
262 }
264 template<class src, class dst>
265 gfx::SizeTyped<dst> operator*(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
266 return gfx::SizeTyped<dst>(float(aSize.width) * aScale.scale,
267 float(aSize.height) * aScale.scale);
268 }
270 template<class src, class dst>
271 gfx::SizeTyped<dst> operator/(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
272 return gfx::SizeTyped<dst>(float(aSize.width) / aScale.scale,
273 float(aSize.height) / aScale.scale);
274 }
276 template<class src, class dst>
277 gfx::MarginTyped<dst> operator*(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactor<src, dst>& aScale) {
278 return gfx::MarginTyped<dst>(aMargin.top * aScale.scale,
279 aMargin.right * aScale.scale,
280 aMargin.bottom * aScale.scale,
281 aMargin.left * aScale.scale);
282 }
284 template<class src, class dst>
285 gfx::MarginTyped<dst> operator/(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactor<dst, src>& aScale) {
286 return gfx::MarginTyped<dst>(aMargin.top / aScale.scale,
287 aMargin.right / aScale.scale,
288 aMargin.bottom / aScale.scale,
289 aMargin.left / aScale.scale);
290 }
292 }
294 #endif