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