Line data Source code
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/. */
6 :
7 : #ifndef MOZ_UNITS_H_
8 : #define MOZ_UNITS_H_
9 :
10 : #include "mozilla/gfx/Coord.h"
11 : #include "mozilla/gfx/Point.h"
12 : #include "mozilla/gfx/Rect.h"
13 : #include "mozilla/gfx/ScaleFactor.h"
14 : #include "mozilla/gfx/ScaleFactors2D.h"
15 : #include "nsMargin.h"
16 : #include "nsRect.h"
17 : #include "nsRegion.h"
18 : #include "mozilla/AppUnits.h"
19 : #include "mozilla/TypeTraits.h"
20 :
21 : namespace mozilla {
22 :
23 : template <typename T>
24 : struct IsPixel : FalseType {};
25 :
26 : // See struct declaration for a description of each unit type.
27 : struct CSSPixel;
28 : struct LayoutDevicePixel;
29 : struct LayerPixel;
30 : struct CSSTransformedLayerPixel;
31 : struct RenderTargetPixel;
32 : struct ScreenPixel;
33 : struct ParentLayerPixel;
34 : struct DesktopPixel;
35 :
36 : template<> struct IsPixel<CSSPixel> : TrueType {};
37 : template<> struct IsPixel<LayoutDevicePixel> : TrueType {};
38 : template<> struct IsPixel<LayerPixel> : TrueType {};
39 : template<> struct IsPixel<CSSTransformedLayerPixel> : TrueType {};
40 : template<> struct IsPixel<RenderTargetPixel> : TrueType {};
41 : template<> struct IsPixel<ScreenPixel> : TrueType {};
42 : template<> struct IsPixel<ParentLayerPixel> : TrueType {};
43 : template<> struct IsPixel<DesktopPixel> : TrueType {};
44 :
45 : typedef gfx::CoordTyped<CSSPixel> CSSCoord;
46 : typedef gfx::IntCoordTyped<CSSPixel> CSSIntCoord;
47 : typedef gfx::PointTyped<CSSPixel> CSSPoint;
48 : typedef gfx::IntPointTyped<CSSPixel> CSSIntPoint;
49 : typedef gfx::SizeTyped<CSSPixel> CSSSize;
50 : typedef gfx::IntSizeTyped<CSSPixel> CSSIntSize;
51 : typedef gfx::RectTyped<CSSPixel> CSSRect;
52 : typedef gfx::IntRectTyped<CSSPixel> CSSIntRect;
53 : typedef gfx::MarginTyped<CSSPixel> CSSMargin;
54 : typedef gfx::IntMarginTyped<CSSPixel> CSSIntMargin;
55 : typedef gfx::IntRegionTyped<CSSPixel> CSSIntRegion;
56 :
57 : typedef gfx::CoordTyped<LayoutDevicePixel> LayoutDeviceCoord;
58 : typedef gfx::IntCoordTyped<LayoutDevicePixel> LayoutDeviceIntCoord;
59 : typedef gfx::PointTyped<LayoutDevicePixel> LayoutDevicePoint;
60 : typedef gfx::IntPointTyped<LayoutDevicePixel> LayoutDeviceIntPoint;
61 : typedef gfx::SizeTyped<LayoutDevicePixel> LayoutDeviceSize;
62 : typedef gfx::IntSizeTyped<LayoutDevicePixel> LayoutDeviceIntSize;
63 : typedef gfx::RectTyped<LayoutDevicePixel> LayoutDeviceRect;
64 : typedef gfx::IntRectTyped<LayoutDevicePixel> LayoutDeviceIntRect;
65 : typedef gfx::MarginTyped<LayoutDevicePixel> LayoutDeviceMargin;
66 : typedef gfx::IntMarginTyped<LayoutDevicePixel> LayoutDeviceIntMargin;
67 : typedef gfx::IntRegionTyped<LayoutDevicePixel> LayoutDeviceIntRegion;
68 :
69 : typedef gfx::CoordTyped<LayerPixel> LayerCoord;
70 : typedef gfx::IntCoordTyped<LayerPixel> LayerIntCoord;
71 : typedef gfx::PointTyped<LayerPixel> LayerPoint;
72 : typedef gfx::IntPointTyped<LayerPixel> LayerIntPoint;
73 : typedef gfx::SizeTyped<LayerPixel> LayerSize;
74 : typedef gfx::IntSizeTyped<LayerPixel> LayerIntSize;
75 : typedef gfx::RectTyped<LayerPixel> LayerRect;
76 : typedef gfx::IntRectTyped<LayerPixel> LayerIntRect;
77 : typedef gfx::MarginTyped<LayerPixel> LayerMargin;
78 : typedef gfx::IntMarginTyped<LayerPixel> LayerIntMargin;
79 : typedef gfx::IntRegionTyped<LayerPixel> LayerIntRegion;
80 :
81 : typedef gfx::CoordTyped<CSSTransformedLayerPixel> CSSTransformedLayerCoord;
82 : typedef gfx::IntCoordTyped<CSSTransformedLayerPixel> CSSTransformedLayerIntCoord;
83 : typedef gfx::PointTyped<CSSTransformedLayerPixel> CSSTransformedLayerPoint;
84 : typedef gfx::IntPointTyped<CSSTransformedLayerPixel> CSSTransformedLayerIntPoint;
85 : typedef gfx::SizeTyped<CSSTransformedLayerPixel> CSSTransformedLayerSize;
86 : typedef gfx::IntSizeTyped<CSSTransformedLayerPixel> CSSTransformedLayerIntSize;
87 : typedef gfx::RectTyped<CSSTransformedLayerPixel> CSSTransformedLayerRect;
88 : typedef gfx::IntRectTyped<CSSTransformedLayerPixel> CSSTransformedLayerIntRect;
89 : typedef gfx::MarginTyped<CSSTransformedLayerPixel> CSSTransformedLayerMargin;
90 : typedef gfx::IntMarginTyped<CSSTransformedLayerPixel> CSSTransformedLayerIntMargin;
91 : typedef gfx::IntRegionTyped<CSSTransformedLayerPixel> CSSTransformedLayerIntRegion;
92 :
93 : typedef gfx::PointTyped<RenderTargetPixel> RenderTargetPoint;
94 : typedef gfx::IntPointTyped<RenderTargetPixel> RenderTargetIntPoint;
95 : typedef gfx::SizeTyped<RenderTargetPixel> RenderTargetSize;
96 : typedef gfx::IntSizeTyped<RenderTargetPixel> RenderTargetIntSize;
97 : typedef gfx::RectTyped<RenderTargetPixel> RenderTargetRect;
98 : typedef gfx::IntRectTyped<RenderTargetPixel> RenderTargetIntRect;
99 : typedef gfx::MarginTyped<RenderTargetPixel> RenderTargetMargin;
100 : typedef gfx::IntMarginTyped<RenderTargetPixel> RenderTargetIntMargin;
101 : typedef gfx::IntRegionTyped<RenderTargetPixel> RenderTargetIntRegion;
102 :
103 : typedef gfx::CoordTyped<ScreenPixel> ScreenCoord;
104 : typedef gfx::IntCoordTyped<ScreenPixel> ScreenIntCoord;
105 : typedef gfx::PointTyped<ScreenPixel> ScreenPoint;
106 : typedef gfx::IntPointTyped<ScreenPixel> ScreenIntPoint;
107 : typedef gfx::SizeTyped<ScreenPixel> ScreenSize;
108 : typedef gfx::IntSizeTyped<ScreenPixel> ScreenIntSize;
109 : typedef gfx::RectTyped<ScreenPixel> ScreenRect;
110 : typedef gfx::IntRectTyped<ScreenPixel> ScreenIntRect;
111 : typedef gfx::MarginTyped<ScreenPixel> ScreenMargin;
112 : typedef gfx::IntMarginTyped<ScreenPixel> ScreenIntMargin;
113 : typedef gfx::IntRegionTyped<ScreenPixel> ScreenIntRegion;
114 :
115 : typedef gfx::CoordTyped<ParentLayerPixel> ParentLayerCoord;
116 : typedef gfx::IntCoordTyped<ParentLayerPixel> ParentLayerIntCoord;
117 : typedef gfx::PointTyped<ParentLayerPixel> ParentLayerPoint;
118 : typedef gfx::IntPointTyped<ParentLayerPixel> ParentLayerIntPoint;
119 : typedef gfx::SizeTyped<ParentLayerPixel> ParentLayerSize;
120 : typedef gfx::IntSizeTyped<ParentLayerPixel> ParentLayerIntSize;
121 : typedef gfx::RectTyped<ParentLayerPixel> ParentLayerRect;
122 : typedef gfx::IntRectTyped<ParentLayerPixel> ParentLayerIntRect;
123 : typedef gfx::MarginTyped<ParentLayerPixel> ParentLayerMargin;
124 : typedef gfx::IntMarginTyped<ParentLayerPixel> ParentLayerIntMargin;
125 : typedef gfx::IntRegionTyped<ParentLayerPixel> ParentLayerIntRegion;
126 :
127 : typedef gfx::CoordTyped<DesktopPixel> DesktopCoord;
128 : typedef gfx::IntCoordTyped<DesktopPixel> DesktopIntCoord;
129 : typedef gfx::PointTyped<DesktopPixel> DesktopPoint;
130 : typedef gfx::IntPointTyped<DesktopPixel> DesktopIntPoint;
131 : typedef gfx::SizeTyped<DesktopPixel> DesktopSize;
132 : typedef gfx::IntSizeTyped<DesktopPixel> DesktopIntSize;
133 : typedef gfx::RectTyped<DesktopPixel> DesktopRect;
134 : typedef gfx::IntRectTyped<DesktopPixel> DesktopIntRect;
135 :
136 : typedef gfx::ScaleFactor<CSSPixel, LayoutDevicePixel> CSSToLayoutDeviceScale;
137 : typedef gfx::ScaleFactor<CSSPixel, LayerPixel> CSSToLayerScale;
138 : typedef gfx::ScaleFactor<CSSPixel, ScreenPixel> CSSToScreenScale;
139 : typedef gfx::ScaleFactor<CSSPixel, ParentLayerPixel> CSSToParentLayerScale;
140 : typedef gfx::ScaleFactor<LayoutDevicePixel, CSSPixel> LayoutDeviceToCSSScale;
141 : typedef gfx::ScaleFactor<LayoutDevicePixel, LayerPixel> LayoutDeviceToLayerScale;
142 : typedef gfx::ScaleFactor<LayoutDevicePixel, ScreenPixel> LayoutDeviceToScreenScale;
143 : typedef gfx::ScaleFactor<LayoutDevicePixel, ParentLayerPixel> LayoutDeviceToParentLayerScale;
144 : typedef gfx::ScaleFactor<LayerPixel, CSSPixel> LayerToCSSScale;
145 : typedef gfx::ScaleFactor<LayerPixel, LayoutDevicePixel> LayerToLayoutDeviceScale;
146 : typedef gfx::ScaleFactor<LayerPixel, RenderTargetPixel> LayerToRenderTargetScale;
147 : typedef gfx::ScaleFactor<LayerPixel, ScreenPixel> LayerToScreenScale;
148 : typedef gfx::ScaleFactor<LayerPixel, ParentLayerPixel> LayerToParentLayerScale;
149 : typedef gfx::ScaleFactor<RenderTargetPixel, ScreenPixel> RenderTargetToScreenScale;
150 : typedef gfx::ScaleFactor<ScreenPixel, CSSPixel> ScreenToCSSScale;
151 : typedef gfx::ScaleFactor<ScreenPixel, LayoutDevicePixel> ScreenToLayoutDeviceScale;
152 : typedef gfx::ScaleFactor<ScreenPixel, LayerPixel> ScreenToLayerScale;
153 : typedef gfx::ScaleFactor<ScreenPixel, ParentLayerPixel> ScreenToParentLayerScale;
154 : typedef gfx::ScaleFactor<ParentLayerPixel, LayerPixel> ParentLayerToLayerScale;
155 : typedef gfx::ScaleFactor<ParentLayerPixel, ScreenPixel> ParentLayerToScreenScale;
156 : typedef gfx::ScaleFactor<ParentLayerPixel, ParentLayerPixel> ParentLayerToParentLayerScale;
157 : typedef gfx::ScaleFactor<DesktopPixel, LayoutDevicePixel> DesktopToLayoutDeviceScale;
158 :
159 : typedef gfx::ScaleFactors2D<CSSPixel, LayoutDevicePixel> CSSToLayoutDeviceScale2D;
160 : typedef gfx::ScaleFactors2D<CSSPixel, LayerPixel> CSSToLayerScale2D;
161 : typedef gfx::ScaleFactors2D<CSSPixel, ScreenPixel> CSSToScreenScale2D;
162 : typedef gfx::ScaleFactors2D<CSSPixel, ParentLayerPixel> CSSToParentLayerScale2D;
163 : typedef gfx::ScaleFactors2D<LayoutDevicePixel, CSSPixel> LayoutDeviceToCSSScale2D;
164 : typedef gfx::ScaleFactors2D<LayoutDevicePixel, LayerPixel> LayoutDeviceToLayerScale2D;
165 : typedef gfx::ScaleFactors2D<LayoutDevicePixel, ScreenPixel> LayoutDeviceToScreenScale2D;
166 : typedef gfx::ScaleFactors2D<LayoutDevicePixel, ParentLayerPixel> LayoutDeviceToParentLayerScale2D;
167 : typedef gfx::ScaleFactors2D<LayerPixel, CSSPixel> LayerToCSSScale2D;
168 : typedef gfx::ScaleFactors2D<LayerPixel, LayoutDevicePixel> LayerToLayoutDeviceScale2D;
169 : typedef gfx::ScaleFactors2D<LayerPixel, RenderTargetPixel> LayerToRenderTargetScale2D;
170 : typedef gfx::ScaleFactors2D<LayerPixel, ScreenPixel> LayerToScreenScale2D;
171 : typedef gfx::ScaleFactors2D<LayerPixel, ParentLayerPixel> LayerToParentLayerScale2D;
172 : typedef gfx::ScaleFactors2D<RenderTargetPixel, ScreenPixel> RenderTargetToScreenScale2D;
173 : typedef gfx::ScaleFactors2D<ScreenPixel, CSSPixel> ScreenToCSSScale2D;
174 : typedef gfx::ScaleFactors2D<ScreenPixel, LayoutDevicePixel> ScreenToLayoutDeviceScale2D;
175 : typedef gfx::ScaleFactors2D<ScreenPixel, LayerPixel> ScreenToLayerScale2D;
176 : typedef gfx::ScaleFactors2D<ScreenPixel, ParentLayerPixel> ScreenToParentLayerScale2D;
177 : typedef gfx::ScaleFactors2D<ParentLayerPixel, LayerPixel> ParentLayerToLayerScale2D;
178 : typedef gfx::ScaleFactors2D<ParentLayerPixel, ScreenPixel> ParentLayerToScreenScale2D;
179 : typedef gfx::ScaleFactors2D<ParentLayerPixel, ParentLayerPixel> ParentLayerToParentLayerScale2D;
180 :
181 : typedef gfx::Matrix4x4Typed<LayoutDevicePixel, LayoutDevicePixel> LayoutDeviceToLayoutDeviceMatrix4x4;
182 : typedef gfx::Matrix4x4Typed<LayerPixel, ParentLayerPixel> LayerToParentLayerMatrix4x4;
183 : typedef gfx::Matrix4x4Typed<ScreenPixel, ScreenPixel> ScreenToScreenMatrix4x4;
184 : typedef gfx::Matrix4x4Typed<ScreenPixel, ParentLayerPixel> ScreenToParentLayerMatrix4x4;
185 : typedef gfx::Matrix4x4Typed<ParentLayerPixel, LayerPixel> ParentLayerToLayerMatrix4x4;
186 : typedef gfx::Matrix4x4Typed<ParentLayerPixel, ScreenPixel> ParentLayerToScreenMatrix4x4;
187 : typedef gfx::Matrix4x4Typed<ParentLayerPixel, ParentLayerPixel> ParentLayerToParentLayerMatrix4x4;
188 : typedef gfx::Matrix4x4Typed<ParentLayerPixel, RenderTargetPixel> ParentLayerToRenderTargetMatrix4x4;
189 :
190 : /*
191 : * The pixels that content authors use to specify sizes in.
192 : */
193 2046 : struct CSSPixel {
194 :
195 : // Conversions from app units
196 :
197 : static CSSCoord FromAppUnits(nscoord aCoord) {
198 : return NSAppUnitsToFloatPixels(aCoord, float(AppUnitsPerCSSPixel()));
199 : }
200 :
201 108 : static CSSPoint FromAppUnits(const nsPoint& aPoint) {
202 324 : return CSSPoint(NSAppUnitsToFloatPixels(aPoint.x, float(AppUnitsPerCSSPixel())),
203 432 : NSAppUnitsToFloatPixels(aPoint.y, float(AppUnitsPerCSSPixel())));
204 : }
205 :
206 : static CSSSize FromAppUnits(const nsSize& aSize) {
207 : return CSSSize(NSAppUnitsToFloatPixels(aSize.width, float(AppUnitsPerCSSPixel())),
208 : NSAppUnitsToFloatPixels(aSize.height, float(AppUnitsPerCSSPixel())));
209 : }
210 :
211 81 : static CSSRect FromAppUnits(const nsRect& aRect) {
212 567 : return CSSRect(NSAppUnitsToFloatPixels(aRect.x, float(AppUnitsPerCSSPixel())),
213 162 : NSAppUnitsToFloatPixels(aRect.y, float(AppUnitsPerCSSPixel())),
214 162 : NSAppUnitsToFloatPixels(aRect.width, float(AppUnitsPerCSSPixel())),
215 324 : NSAppUnitsToFloatPixels(aRect.height, float(AppUnitsPerCSSPixel())));
216 : }
217 :
218 0 : static CSSMargin FromAppUnits(const nsMargin& aMargin) {
219 0 : return CSSMargin(NSAppUnitsToFloatPixels(aMargin.top, float(AppUnitsPerCSSPixel())),
220 0 : NSAppUnitsToFloatPixels(aMargin.right, float(AppUnitsPerCSSPixel())),
221 0 : NSAppUnitsToFloatPixels(aMargin.bottom, float(AppUnitsPerCSSPixel())),
222 0 : NSAppUnitsToFloatPixels(aMargin.left, float(AppUnitsPerCSSPixel())));
223 : }
224 :
225 48 : static CSSIntPoint FromAppUnitsRounded(const nsPoint& aPoint) {
226 144 : return CSSIntPoint(NSAppUnitsToIntPixels(aPoint.x, float(AppUnitsPerCSSPixel())),
227 192 : NSAppUnitsToIntPixels(aPoint.y, float(AppUnitsPerCSSPixel())));
228 : }
229 :
230 0 : static CSSIntSize FromAppUnitsRounded(const nsSize& aSize)
231 : {
232 0 : return CSSIntSize(NSAppUnitsToIntPixels(aSize.width, float(AppUnitsPerCSSPixel())),
233 0 : NSAppUnitsToIntPixels(aSize.height, float(AppUnitsPerCSSPixel())));
234 : }
235 :
236 0 : static CSSIntRect FromAppUnitsRounded(const nsRect& aRect) {
237 0 : return CSSIntRect(NSAppUnitsToIntPixels(aRect.x, float(AppUnitsPerCSSPixel())),
238 0 : NSAppUnitsToIntPixels(aRect.y, float(AppUnitsPerCSSPixel())),
239 0 : NSAppUnitsToIntPixels(aRect.width, float(AppUnitsPerCSSPixel())),
240 0 : NSAppUnitsToIntPixels(aRect.height, float(AppUnitsPerCSSPixel())));
241 : }
242 :
243 0 : static CSSIntRect FromAppUnitsToNearest(const nsRect& aRect) {
244 0 : return CSSIntRect::FromUnknownRect(aRect.ToNearestPixels(AppUnitsPerCSSPixel()));
245 : }
246 :
247 : // Conversions to app units
248 :
249 : static nscoord ToAppUnits(CSSCoord aCoord) {
250 : return NSToCoordRoundWithClamp(aCoord * float(AppUnitsPerCSSPixel()));
251 : }
252 :
253 0 : static nsPoint ToAppUnits(const CSSPoint& aPoint) {
254 0 : return nsPoint(NSToCoordRoundWithClamp(aPoint.x * float(AppUnitsPerCSSPixel())),
255 0 : NSToCoordRoundWithClamp(aPoint.y * float(AppUnitsPerCSSPixel())));
256 : }
257 :
258 0 : static nsPoint ToAppUnits(const CSSIntPoint& aPoint) {
259 0 : return nsPoint(NSToCoordRoundWithClamp(float(aPoint.x) * float(AppUnitsPerCSSPixel())),
260 0 : NSToCoordRoundWithClamp(float(aPoint.y) * float(AppUnitsPerCSSPixel())));
261 : }
262 :
263 0 : static nsSize ToAppUnits(const CSSSize& aSize) {
264 0 : return nsSize(NSToCoordRoundWithClamp(aSize.width * float(AppUnitsPerCSSPixel())),
265 0 : NSToCoordRoundWithClamp(aSize.height * float(AppUnitsPerCSSPixel())));
266 : }
267 :
268 72 : static nsSize ToAppUnits(const CSSIntSize& aSize) {
269 144 : return nsSize(NSToCoordRoundWithClamp(float(aSize.width) * float(AppUnitsPerCSSPixel())),
270 216 : NSToCoordRoundWithClamp(float(aSize.height) * float(AppUnitsPerCSSPixel())));
271 : }
272 :
273 0 : static nsRect ToAppUnits(const CSSRect& aRect) {
274 0 : return nsRect(NSToCoordRoundWithClamp(aRect.x * float(AppUnitsPerCSSPixel())),
275 0 : NSToCoordRoundWithClamp(aRect.y * float(AppUnitsPerCSSPixel())),
276 0 : NSToCoordRoundWithClamp(aRect.width * float(AppUnitsPerCSSPixel())),
277 0 : NSToCoordRoundWithClamp(aRect.height * float(AppUnitsPerCSSPixel())));
278 : }
279 :
280 18 : static nsRect ToAppUnits(const CSSIntRect& aRect) {
281 18 : return nsRect(NSToCoordRoundWithClamp(float(aRect.x) * float(AppUnitsPerCSSPixel())),
282 18 : NSToCoordRoundWithClamp(float(aRect.y) * float(AppUnitsPerCSSPixel())),
283 18 : NSToCoordRoundWithClamp(float(aRect.width) * float(AppUnitsPerCSSPixel())),
284 72 : NSToCoordRoundWithClamp(float(aRect.height) * float(AppUnitsPerCSSPixel())));
285 : }
286 : };
287 :
288 : /*
289 : * The pixels that are referred to as "device pixels" in layout code. In
290 : * general values measured in LayoutDevicePixels are obtained by dividing a
291 : * value in app units by AppUnitsPerDevPixel(). Conversion between CSS pixels
292 : * and LayoutDevicePixels is affected by:
293 : * 1) the "full zoom" (see nsPresContext::SetFullZoom)
294 : * 2) the "widget scale" (see nsIWidget::GetDefaultScale)
295 : */
296 9018 : struct LayoutDevicePixel {
297 567 : static LayoutDeviceRect FromAppUnits(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
298 2835 : return LayoutDeviceRect(NSAppUnitsToFloatPixels(aRect.x, float(aAppUnitsPerDevPixel)),
299 567 : NSAppUnitsToFloatPixels(aRect.y, float(aAppUnitsPerDevPixel)),
300 567 : NSAppUnitsToFloatPixels(aRect.width, float(aAppUnitsPerDevPixel)),
301 1701 : NSAppUnitsToFloatPixels(aRect.height, float(aAppUnitsPerDevPixel)));
302 : }
303 :
304 0 : static LayoutDeviceSize FromAppUnits(const nsSize& aSize, nscoord aAppUnitsPerDevPixel) {
305 0 : return LayoutDeviceSize(
306 0 : NSAppUnitsToFloatPixels(aSize.width, aAppUnitsPerDevPixel),
307 0 : NSAppUnitsToFloatPixels(aSize.height, aAppUnitsPerDevPixel));
308 : }
309 :
310 49 : static LayoutDevicePoint FromAppUnits(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
311 147 : return LayoutDevicePoint(NSAppUnitsToFloatPixels(aPoint.x, aAppUnitsPerDevPixel),
312 147 : NSAppUnitsToFloatPixels(aPoint.y, aAppUnitsPerDevPixel));
313 : }
314 :
315 51 : static LayoutDeviceMargin FromAppUnits(const nsMargin& aMargin, nscoord aAppUnitsPerDevPixel) {
316 255 : return LayoutDeviceMargin(NSAppUnitsToFloatPixels(aMargin.top, aAppUnitsPerDevPixel),
317 51 : NSAppUnitsToFloatPixels(aMargin.right, aAppUnitsPerDevPixel),
318 51 : NSAppUnitsToFloatPixels(aMargin.bottom, aAppUnitsPerDevPixel),
319 153 : NSAppUnitsToFloatPixels(aMargin.left, aAppUnitsPerDevPixel));
320 : }
321 :
322 0 : static LayoutDeviceIntPoint FromAppUnitsRounded(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
323 0 : return LayoutDeviceIntPoint(NSAppUnitsToIntPixels(aPoint.x, aAppUnitsPerDevPixel),
324 0 : NSAppUnitsToIntPixels(aPoint.y, aAppUnitsPerDevPixel));
325 : }
326 :
327 14 : static LayoutDeviceIntPoint FromAppUnitsToNearest(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
328 14 : return LayoutDeviceIntPoint::FromUnknownPoint(aPoint.ToNearestPixels(aAppUnitsPerDevPixel));
329 : }
330 :
331 11 : static LayoutDeviceIntRect FromAppUnitsToNearest(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
332 11 : return LayoutDeviceIntRect::FromUnknownRect(aRect.ToNearestPixels(aAppUnitsPerDevPixel));
333 : }
334 :
335 0 : static LayoutDeviceIntRect FromAppUnitsToInside(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
336 0 : return LayoutDeviceIntRect::FromUnknownRect(aRect.ToInsidePixels(aAppUnitsPerDevPixel));
337 : }
338 :
339 0 : static LayoutDeviceIntRect FromAppUnitsToOutside(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
340 0 : return LayoutDeviceIntRect::FromUnknownRect(aRect.ToOutsidePixels(aAppUnitsPerDevPixel));
341 : }
342 :
343 6 : static LayoutDeviceIntSize FromAppUnitsRounded(const nsSize& aSize, nscoord aAppUnitsPerDevPixel) {
344 24 : return LayoutDeviceIntSize(
345 6 : NSAppUnitsToIntPixels(aSize.width, aAppUnitsPerDevPixel),
346 18 : NSAppUnitsToIntPixels(aSize.height, aAppUnitsPerDevPixel));
347 : }
348 :
349 60 : static nsPoint ToAppUnits(const LayoutDeviceIntPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
350 60 : return nsPoint(aPoint.x * aAppUnitsPerDevPixel,
351 120 : aPoint.y * aAppUnitsPerDevPixel);
352 : }
353 :
354 : static nsSize ToAppUnits(const LayoutDeviceIntSize& aSize, nscoord aAppUnitsPerDevPixel) {
355 : return nsSize(aSize.width * aAppUnitsPerDevPixel,
356 : aSize.height * aAppUnitsPerDevPixel);
357 : }
358 :
359 0 : static nsSize ToAppUnits(const LayoutDeviceSize& aSize, nscoord aAppUnitsPerDevPixel) {
360 0 : return nsSize(NSFloatPixelsToAppUnits(aSize.width, aAppUnitsPerDevPixel),
361 0 : NSFloatPixelsToAppUnits(aSize.height, aAppUnitsPerDevPixel));
362 : }
363 :
364 0 : static nsRect ToAppUnits(const LayoutDeviceIntRect& aRect, nscoord aAppUnitsPerDevPixel) {
365 0 : return nsRect(aRect.x * aAppUnitsPerDevPixel,
366 0 : aRect.y * aAppUnitsPerDevPixel,
367 0 : aRect.width * aAppUnitsPerDevPixel,
368 0 : aRect.height * aAppUnitsPerDevPixel);
369 : }
370 :
371 39 : static nsRect ToAppUnits(const LayoutDeviceRect& aRect, nscoord aAppUnitsPerDevPixel) {
372 39 : return nsRect(NSFloatPixelsToAppUnits(aRect.x, aAppUnitsPerDevPixel),
373 39 : NSFloatPixelsToAppUnits(aRect.y, aAppUnitsPerDevPixel),
374 39 : NSFloatPixelsToAppUnits(aRect.width, aAppUnitsPerDevPixel),
375 156 : NSFloatPixelsToAppUnits(aRect.height, aAppUnitsPerDevPixel));
376 : }
377 : };
378 :
379 : /*
380 : * The pixels that layout rasterizes and delivers to the graphics code.
381 : * These also are generally referred to as "device pixels" in layout code.
382 : * Conversion between CSS pixels and LayerPixels is affected by:
383 : * 1) the "display resolution" (see nsIPresShell::SetResolution)
384 : * 2) the "full zoom" (see nsPresContext::SetFullZoom)
385 : * 3) the "widget scale" (see nsIWidget::GetDefaultScale)
386 : * 4) rasterizing at a different scale in the presence of some CSS transforms
387 : */
388 2346 : struct LayerPixel {
389 : };
390 :
391 : /*
392 : * This is Layer coordinates with the Layer's CSS transform applied.
393 : * It can be thought of as intermediate between LayerPixel and
394 : * ParentLayerPixel, as further applying async transforms to this
395 : * yields ParentLayerPixels.
396 : */
397 : struct CSSTransformedLayerPixel {
398 :
399 : };
400 :
401 : /*
402 : * Layers are always composited to a render target. This unit
403 : * represents one pixel in the render target. Note that for the
404 : * root render target RenderTargetPixel == ScreenPixel. Also
405 : * any ContainerLayer providing an intermediate surface will
406 : * have RenderTargetPixel == LayerPixel.
407 : */
408 874 : struct RenderTargetPixel {
409 : };
410 :
411 : /*
412 : * The pixels that are displayed on the screen.
413 : * On non-OMTC platforms this should be equivalent to LayerPixel units.
414 : * On OMTC platforms these may diverge from LayerPixel units temporarily,
415 : * while an asynchronous zoom is happening, but should eventually converge
416 : * back to LayerPixel units. Some variables (such as those representing
417 : * chrome UI element sizes) that are not subject to content zoom should
418 : * generally be represented in ScreenPixel units.
419 : */
420 693 : struct ScreenPixel {
421 : };
422 :
423 : /* The layer coordinates of the parent frame.
424 : * This can be arrived at in three ways:
425 : * - Start with the CSS coordinates of the parent frame, multiply by the
426 : * device scale and the cumulative resolution of the parent frame.
427 : * - Start with the CSS coordinates of current frame, multiply by the device
428 : * scale, the cumulative resolution of the current frame, and the scales
429 : * from the CSS and async transforms of the current frame.
430 : * - Start with global screen coordinates and unapply all CSS and async
431 : * transforms from the root down to and including the parent.
432 : * It's helpful to look at https://wiki.mozilla.org/Platform/GFX/APZ#Coordinate_systems
433 : * to get a picture of how the various coordinate systems relate to each other.
434 : */
435 7819 : struct ParentLayerPixel {
436 : };
437 :
438 : /*
439 : * Pixels in the coordinate space used by the host OS to manage windows on the
440 : * desktop. What these mean varies between OSs:
441 : * - by default (unless implemented differently in platform-specific widget
442 : * code) they are the same as LayoutDevicePixels
443 : * - on Mac OS X, they're "cocoa points", which correspond to device pixels
444 : * on a non-Retina display, and to 2x device pixels on Retina
445 : * - on Windows *without* per-monitor DPI support, they are Windows "logical
446 : * pixels", i.e. device pixels scaled according to the Windows display DPI
447 : * scaling factor (typically one of 1.25, 1.5, 1.75, 2.0...)
448 : * - on Windows *with* per-monitor DPI support, they are physical device pixels
449 : * on each screen; note that this means the scaling between CSS pixels and
450 : * desktop pixels may vary across multiple displays.
451 : */
452 81 : struct DesktopPixel {
453 : };
454 :
455 : // Operators to apply ScaleFactors directly to Coords, Points, Rects, Sizes and Margins
456 :
457 : template<class src, class dst>
458 0 : gfx::CoordTyped<dst> operator*(const gfx::CoordTyped<src>& aCoord, const gfx::ScaleFactor<src, dst>& aScale) {
459 0 : return gfx::CoordTyped<dst>(aCoord.value * aScale.scale);
460 : }
461 :
462 : template<class src, class dst>
463 0 : gfx::CoordTyped<dst> operator/(const gfx::CoordTyped<src>& aCoord, const gfx::ScaleFactor<dst, src>& aScale) {
464 0 : return gfx::CoordTyped<dst>(aCoord.value / aScale.scale);
465 : }
466 :
467 : template<class src, class dst>
468 16 : gfx::PointTyped<dst> operator*(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactor<src, dst>& aScale) {
469 16 : return gfx::PointTyped<dst>(aPoint.x * aScale.scale,
470 32 : aPoint.y * aScale.scale);
471 : }
472 :
473 : template<class src, class dst>
474 21 : gfx::PointTyped<dst> operator/(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactor<dst, src>& aScale) {
475 21 : return gfx::PointTyped<dst>(aPoint.x / aScale.scale,
476 42 : aPoint.y / aScale.scale);
477 : }
478 :
479 : template<class src, class dst>
480 126 : gfx::PointTyped<dst> operator*(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactors2D<src, dst>& aScale) {
481 126 : return gfx::PointTyped<dst>(aPoint.x * aScale.xScale,
482 252 : aPoint.y * aScale.yScale);
483 : }
484 :
485 : template<class src, class dst>
486 2 : gfx::PointTyped<dst> operator/(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactors2D<dst, src>& aScale) {
487 2 : return gfx::PointTyped<dst>(aPoint.x / aScale.xScale,
488 4 : aPoint.y / aScale.yScale);
489 : }
490 :
491 : template<class src, class dst>
492 12 : gfx::PointTyped<dst> operator*(const gfx::IntPointTyped<src>& aPoint, const gfx::ScaleFactor<src, dst>& aScale) {
493 24 : return gfx::PointTyped<dst>(float(aPoint.x) * aScale.scale,
494 36 : float(aPoint.y) * aScale.scale);
495 : }
496 :
497 : template<class src, class dst>
498 0 : gfx::PointTyped<dst> operator/(const gfx::IntPointTyped<src>& aPoint, const gfx::ScaleFactor<dst, src>& aScale) {
499 0 : return gfx::PointTyped<dst>(float(aPoint.x) / aScale.scale,
500 0 : float(aPoint.y) / aScale.scale);
501 : }
502 :
503 : template<class src, class dst>
504 : gfx::PointTyped<dst> operator*(const gfx::IntPointTyped<src>& aPoint, const gfx::ScaleFactors2D<src, dst>& aScale) {
505 : return gfx::PointTyped<dst>(float(aPoint.x) * aScale.xScale,
506 : float(aPoint.y) * aScale.yScale);
507 : }
508 :
509 : template<class src, class dst>
510 : gfx::PointTyped<dst> operator/(const gfx::IntPointTyped<src>& aPoint, const gfx::ScaleFactors2D<dst, src>& aScale) {
511 : return gfx::PointTyped<dst>(float(aPoint.x) / aScale.xScale,
512 : float(aPoint.y) / aScale.yScale);
513 : }
514 :
515 : template<class src, class dst>
516 33 : gfx::RectTyped<dst> operator*(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactor<src, dst>& aScale) {
517 33 : return gfx::RectTyped<dst>(aRect.x * aScale.scale,
518 33 : aRect.y * aScale.scale,
519 33 : aRect.width * aScale.scale,
520 132 : aRect.height * aScale.scale);
521 : }
522 :
523 : template<class src, class dst>
524 0 : gfx::RectTyped<dst> operator/(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactor<dst, src>& aScale) {
525 0 : return gfx::RectTyped<dst>(aRect.x / aScale.scale,
526 0 : aRect.y / aScale.scale,
527 0 : aRect.width / aScale.scale,
528 0 : aRect.height / aScale.scale);
529 : }
530 :
531 : template<class src, class dst>
532 69 : gfx::RectTyped<dst> operator*(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactors2D<src, dst>& aScale) {
533 69 : return gfx::RectTyped<dst>(aRect.x * aScale.xScale,
534 69 : aRect.y * aScale.yScale,
535 69 : aRect.width * aScale.xScale,
536 276 : aRect.height * aScale.yScale);
537 : }
538 :
539 : template<class src, class dst>
540 41 : gfx::RectTyped<dst> operator/(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactors2D<dst, src>& aScale) {
541 41 : return gfx::RectTyped<dst>(aRect.x / aScale.xScale,
542 41 : aRect.y / aScale.yScale,
543 41 : aRect.width / aScale.xScale,
544 164 : aRect.height / aScale.yScale);
545 : }
546 :
547 : template<class src, class dst>
548 2 : gfx::RectTyped<dst> operator*(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactor<src, dst>& aScale) {
549 4 : return gfx::RectTyped<dst>(float(aRect.x) * aScale.scale,
550 4 : float(aRect.y) * aScale.scale,
551 4 : float(aRect.width) * aScale.scale,
552 14 : float(aRect.height) * aScale.scale);
553 : }
554 :
555 : template<class src, class dst>
556 30 : gfx::RectTyped<dst> operator/(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactor<dst, src>& aScale) {
557 60 : return gfx::RectTyped<dst>(float(aRect.x) / aScale.scale,
558 60 : float(aRect.y) / aScale.scale,
559 60 : float(aRect.width) / aScale.scale,
560 210 : float(aRect.height) / aScale.scale);
561 : }
562 :
563 : template<class src, class dst>
564 0 : gfx::RectTyped<dst> operator*(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactors2D<src, dst>& aScale) {
565 0 : return gfx::RectTyped<dst>(float(aRect.x) * aScale.xScale,
566 0 : float(aRect.y) * aScale.yScale,
567 0 : float(aRect.width) * aScale.xScale,
568 0 : float(aRect.height) * aScale.yScale);
569 : }
570 :
571 : template<class src, class dst>
572 : gfx::RectTyped<dst> operator/(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactors2D<dst, src>& aScale) {
573 : return gfx::RectTyped<dst>(float(aRect.x) / aScale.xScale,
574 : float(aRect.y) / aScale.yScale,
575 : float(aRect.width) / aScale.xScale,
576 : float(aRect.height) / aScale.yScale);
577 : }
578 :
579 : template<class src, class dst>
580 6 : gfx::SizeTyped<dst> operator*(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
581 6 : return gfx::SizeTyped<dst>(aSize.width * aScale.scale,
582 12 : aSize.height * aScale.scale);
583 : }
584 :
585 : template<class src, class dst>
586 1 : gfx::SizeTyped<dst> operator/(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
587 1 : return gfx::SizeTyped<dst>(aSize.width / aScale.scale,
588 2 : aSize.height / aScale.scale);
589 : }
590 :
591 : template<class src, class dst>
592 0 : gfx::SizeTyped<dst> operator*(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactors2D<src, dst>& aScale) {
593 0 : return gfx::SizeTyped<dst>(aSize.width * aScale.xScale,
594 0 : aSize.height * aScale.yScale);
595 : }
596 :
597 : template<class src, class dst>
598 113 : gfx::SizeTyped<dst> operator/(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactors2D<dst, src>& aScale) {
599 113 : return gfx::SizeTyped<dst>(aSize.width / aScale.xScale,
600 226 : aSize.height / aScale.yScale);
601 : }
602 :
603 : template<class src, class dst>
604 0 : gfx::SizeTyped<dst> operator*(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
605 0 : return gfx::SizeTyped<dst>(float(aSize.width) * aScale.scale,
606 0 : float(aSize.height) * aScale.scale);
607 : }
608 :
609 : template<class src, class dst>
610 5 : gfx::SizeTyped<dst> operator/(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
611 10 : return gfx::SizeTyped<dst>(float(aSize.width) / aScale.scale,
612 15 : float(aSize.height) / aScale.scale);
613 : }
614 :
615 : template<class src, class dst>
616 : gfx::SizeTyped<dst> operator*(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactors2D<src, dst>& aScale) {
617 : return gfx::SizeTyped<dst>(float(aSize.width) * aScale.xScale,
618 : float(aSize.height) * aScale.yScale);
619 : }
620 :
621 : template<class src, class dst>
622 : gfx::SizeTyped<dst> operator/(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactors2D<dst, src>& aScale) {
623 : return gfx::SizeTyped<dst>(float(aSize.width) / aScale.xScale,
624 : float(aSize.height) / aScale.yScale);
625 : }
626 :
627 : template<class src, class dst>
628 27 : gfx::MarginTyped<dst> operator*(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactor<src, dst>& aScale) {
629 27 : return gfx::MarginTyped<dst>(aMargin.top * aScale.scale,
630 27 : aMargin.right * aScale.scale,
631 27 : aMargin.bottom * aScale.scale,
632 108 : aMargin.left * aScale.scale);
633 : }
634 :
635 : template<class src, class dst>
636 : gfx::MarginTyped<dst> operator/(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactor<dst, src>& aScale) {
637 : return gfx::MarginTyped<dst>(aMargin.top / aScale.scale,
638 : aMargin.right / aScale.scale,
639 : aMargin.bottom / aScale.scale,
640 : aMargin.left / aScale.scale);
641 : }
642 :
643 : template<class src, class dst>
644 2 : gfx::MarginTyped<dst> operator*(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactors2D<src, dst>& aScale) {
645 2 : return gfx::MarginTyped<dst>(aMargin.top * aScale.yScale,
646 2 : aMargin.right * aScale.xScale,
647 2 : aMargin.bottom * aScale.yScale,
648 8 : aMargin.left * aScale.xScale);
649 : }
650 :
651 : template<class src, class dst>
652 2 : gfx::MarginTyped<dst> operator/(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactors2D<dst, src>& aScale) {
653 2 : return gfx::MarginTyped<dst>(aMargin.top / aScale.yScale,
654 2 : aMargin.right / aScale.xScale,
655 2 : aMargin.bottom / aScale.yScale,
656 8 : aMargin.left / aScale.xScale);
657 : }
658 :
659 : // Calculate the max or min or the ratios of the widths and heights of two
660 : // sizes, returning a scale factor in the correct units.
661 :
662 : template<class src, class dst>
663 0 : gfx::ScaleFactor<src, dst> MaxScaleRatio(const gfx::SizeTyped<dst>& aDestSize, const gfx::SizeTyped<src>& aSrcSize) {
664 0 : return gfx::ScaleFactor<src, dst>(std::max(aDestSize.width / aSrcSize.width,
665 0 : aDestSize.height / aSrcSize.height));
666 : }
667 :
668 : template<class src, class dst>
669 : gfx::ScaleFactor<src, dst> MinScaleRatio(const gfx::SizeTyped<dst>& aDestSize, const gfx::SizeTyped<src>& aSrcSize) {
670 : return gfx::ScaleFactor<src, dst>(std::min(aDestSize.width / aSrcSize.width,
671 : aDestSize.height / aSrcSize.height));
672 : }
673 :
674 : template <typename T>
675 : struct CoordOfImpl;
676 :
677 : template <typename Units>
678 : struct CoordOfImpl<gfx::PointTyped<Units>> {
679 : typedef gfx::CoordTyped<Units> Type;
680 : };
681 :
682 : template <typename Units>
683 : struct CoordOfImpl<gfx::IntPointTyped<Units>> {
684 : typedef gfx::IntCoordTyped<Units> Type;
685 : };
686 :
687 : template <typename Units>
688 : struct CoordOfImpl<gfx::RectTyped<Units>> {
689 : typedef gfx::CoordTyped<Units> Type;
690 : };
691 :
692 : template <typename Units>
693 : struct CoordOfImpl<gfx::IntRectTyped<Units>> {
694 : typedef gfx::IntCoordTyped<Units> Type;
695 : };
696 :
697 : template <typename T>
698 : using CoordOf = typename CoordOfImpl<T>::Type;
699 :
700 : } // namespace mozilla
701 :
702 : #endif
|