Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / layout / base / Units.h
blob465585ff1bcff3868dc67be38feb8636ddb8a9da
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 <type_traits>
12 #include "mozilla/gfx/Coord.h"
13 #include "mozilla/gfx/Point.h"
14 #include "mozilla/gfx/Rect.h"
15 #include "mozilla/gfx/RectAbsolute.h"
16 #include "mozilla/gfx/ScaleFactor.h"
17 #include "mozilla/gfx/ScaleFactors2D.h"
18 #include "nsMargin.h"
19 #include "nsRect.h"
20 #include "nsRegion.h"
21 #include "mozilla/AppUnits.h"
23 namespace mozilla {
25 template <typename T>
26 struct IsPixel : std::false_type {};
28 // See struct declaration for a description of each unit type.
29 struct CSSPixel;
30 struct OuterCSSPixel;
31 struct LayoutDevicePixel;
32 struct LayerPixel;
33 struct CSSTransformedLayerPixel;
34 struct RenderTargetPixel;
35 struct ScreenPixel;
36 struct ParentLayerPixel;
37 struct DesktopPixel;
38 struct ImagePixel;
39 struct ExternalPixel;
41 template <>
42 struct IsPixel<CSSPixel> : std::true_type {};
43 template <>
44 struct IsPixel<OuterCSSPixel> : std::true_type {};
45 template <>
46 struct IsPixel<LayoutDevicePixel> : std::true_type {};
47 template <>
48 struct IsPixel<LayerPixel> : std::true_type {};
49 template <>
50 struct IsPixel<CSSTransformedLayerPixel> : std::true_type {};
51 template <>
52 struct IsPixel<RenderTargetPixel> : std::true_type {};
53 template <>
54 struct IsPixel<ImagePixel> : std::true_type {};
55 template <>
56 struct IsPixel<ScreenPixel> : std::true_type {};
57 template <>
58 struct IsPixel<ParentLayerPixel> : std::true_type {};
59 template <>
60 struct IsPixel<DesktopPixel> : std::true_type {};
61 template <>
62 struct IsPixel<ExternalPixel> : std::true_type {};
64 typedef gfx::CoordTyped<CSSPixel> CSSCoord;
65 typedef gfx::IntCoordTyped<CSSPixel> CSSIntCoord;
66 typedef gfx::PointTyped<CSSPixel> CSSPoint;
67 typedef gfx::IntPointTyped<CSSPixel> CSSIntPoint;
68 typedef gfx::SizeTyped<CSSPixel> CSSSize;
69 typedef gfx::IntSizeTyped<CSSPixel> CSSIntSize;
70 typedef gfx::RectTyped<CSSPixel> CSSRect;
71 typedef gfx::IntRectTyped<CSSPixel> CSSIntRect;
72 typedef gfx::MarginTyped<CSSPixel> CSSMargin;
73 typedef gfx::IntMarginTyped<CSSPixel> CSSIntMargin;
74 typedef gfx::IntRegionTyped<CSSPixel> CSSIntRegion;
76 typedef gfx::CoordTyped<OuterCSSPixel> OuterCSSCoord;
77 typedef gfx::IntCoordTyped<OuterCSSPixel> OuterCSSIntCoord;
78 typedef gfx::PointTyped<OuterCSSPixel> OuterCSSPoint;
79 typedef gfx::IntPointTyped<OuterCSSPixel> OuterCSSIntPoint;
80 typedef gfx::SizeTyped<OuterCSSPixel> OuterCSSSize;
81 typedef gfx::IntSizeTyped<OuterCSSPixel> OuterCSSIntSize;
82 typedef gfx::RectTyped<OuterCSSPixel> OuterCSSRect;
83 typedef gfx::IntRectTyped<OuterCSSPixel> OuterCSSIntRect;
84 typedef gfx::MarginTyped<OuterCSSPixel> OuterCSSMargin;
85 typedef gfx::IntMarginTyped<OuterCSSPixel> OuterCSSIntMargin;
86 typedef gfx::IntRegionTyped<OuterCSSPixel> OuterCSSIntRegion;
88 typedef gfx::CoordTyped<LayoutDevicePixel> LayoutDeviceCoord;
89 typedef gfx::IntCoordTyped<LayoutDevicePixel> LayoutDeviceIntCoord;
90 typedef gfx::PointTyped<LayoutDevicePixel> LayoutDevicePoint;
91 typedef gfx::IntPointTyped<LayoutDevicePixel> LayoutDeviceIntPoint;
92 typedef gfx::SizeTyped<LayoutDevicePixel> LayoutDeviceSize;
93 typedef gfx::IntSizeTyped<LayoutDevicePixel> LayoutDeviceIntSize;
94 typedef gfx::RectTyped<LayoutDevicePixel> LayoutDeviceRect;
95 typedef gfx::IntRectTyped<LayoutDevicePixel> LayoutDeviceIntRect;
96 typedef gfx::MarginTyped<LayoutDevicePixel> LayoutDeviceMargin;
97 typedef gfx::IntMarginTyped<LayoutDevicePixel> LayoutDeviceIntMargin;
98 typedef gfx::IntRegionTyped<LayoutDevicePixel> LayoutDeviceIntRegion;
100 typedef gfx::CoordTyped<LayerPixel> LayerCoord;
101 typedef gfx::IntCoordTyped<LayerPixel> LayerIntCoord;
102 typedef gfx::PointTyped<LayerPixel> LayerPoint;
103 typedef gfx::IntPointTyped<LayerPixel> LayerIntPoint;
104 typedef gfx::SizeTyped<LayerPixel> LayerSize;
105 typedef gfx::IntSizeTyped<LayerPixel> LayerIntSize;
106 typedef gfx::RectTyped<LayerPixel> LayerRect;
107 typedef gfx::RectAbsoluteTyped<LayerPixel> LayerRectAbsolute;
108 typedef gfx::IntRectTyped<LayerPixel> LayerIntRect;
109 typedef gfx::MarginTyped<LayerPixel> LayerMargin;
110 typedef gfx::IntMarginTyped<LayerPixel> LayerIntMargin;
111 typedef gfx::IntRegionTyped<LayerPixel> LayerIntRegion;
113 typedef gfx::CoordTyped<CSSTransformedLayerPixel> CSSTransformedLayerCoord;
114 typedef gfx::IntCoordTyped<CSSTransformedLayerPixel>
115 CSSTransformedLayerIntCoord;
116 typedef gfx::PointTyped<CSSTransformedLayerPixel> CSSTransformedLayerPoint;
117 typedef gfx::IntPointTyped<CSSTransformedLayerPixel>
118 CSSTransformedLayerIntPoint;
119 typedef gfx::SizeTyped<CSSTransformedLayerPixel> CSSTransformedLayerSize;
120 typedef gfx::IntSizeTyped<CSSTransformedLayerPixel> CSSTransformedLayerIntSize;
121 typedef gfx::RectTyped<CSSTransformedLayerPixel> CSSTransformedLayerRect;
122 typedef gfx::IntRectTyped<CSSTransformedLayerPixel> CSSTransformedLayerIntRect;
123 typedef gfx::MarginTyped<CSSTransformedLayerPixel> CSSTransformedLayerMargin;
124 typedef gfx::IntMarginTyped<CSSTransformedLayerPixel>
125 CSSTransformedLayerIntMargin;
126 typedef gfx::IntRegionTyped<CSSTransformedLayerPixel>
127 CSSTransformedLayerIntRegion;
129 typedef gfx::PointTyped<RenderTargetPixel> RenderTargetPoint;
130 typedef gfx::IntPointTyped<RenderTargetPixel> RenderTargetIntPoint;
131 typedef gfx::SizeTyped<RenderTargetPixel> RenderTargetSize;
132 typedef gfx::IntSizeTyped<RenderTargetPixel> RenderTargetIntSize;
133 typedef gfx::RectTyped<RenderTargetPixel> RenderTargetRect;
134 typedef gfx::IntRectTyped<RenderTargetPixel> RenderTargetIntRect;
135 typedef gfx::MarginTyped<RenderTargetPixel> RenderTargetMargin;
136 typedef gfx::IntMarginTyped<RenderTargetPixel> RenderTargetIntMargin;
137 typedef gfx::IntRegionTyped<RenderTargetPixel> RenderTargetIntRegion;
139 typedef gfx::PointTyped<ImagePixel> ImagePoint;
140 typedef gfx::IntPointTyped<ImagePixel> ImageIntPoint;
141 typedef gfx::SizeTyped<ImagePixel> ImageSize;
142 typedef gfx::IntSizeTyped<ImagePixel> ImageIntSize;
143 typedef gfx::RectTyped<ImagePixel> ImageRect;
144 typedef gfx::IntRectTyped<ImagePixel> ImageIntRect;
146 typedef gfx::CoordTyped<ScreenPixel> ScreenCoord;
147 typedef gfx::IntCoordTyped<ScreenPixel> ScreenIntCoord;
148 typedef gfx::PointTyped<ScreenPixel> ScreenPoint;
149 typedef gfx::IntPointTyped<ScreenPixel> ScreenIntPoint;
150 typedef gfx::SizeTyped<ScreenPixel> ScreenSize;
151 typedef gfx::IntSizeTyped<ScreenPixel> ScreenIntSize;
152 typedef gfx::RectTyped<ScreenPixel> ScreenRect;
153 typedef gfx::IntRectTyped<ScreenPixel> ScreenIntRect;
154 typedef gfx::MarginTyped<ScreenPixel> ScreenMargin;
155 typedef gfx::IntMarginTyped<ScreenPixel> ScreenIntMargin;
156 typedef gfx::IntRegionTyped<ScreenPixel> ScreenIntRegion;
158 typedef gfx::CoordTyped<ParentLayerPixel> ParentLayerCoord;
159 typedef gfx::IntCoordTyped<ParentLayerPixel> ParentLayerIntCoord;
160 typedef gfx::PointTyped<ParentLayerPixel> ParentLayerPoint;
161 typedef gfx::IntPointTyped<ParentLayerPixel> ParentLayerIntPoint;
162 typedef gfx::SizeTyped<ParentLayerPixel> ParentLayerSize;
163 typedef gfx::IntSizeTyped<ParentLayerPixel> ParentLayerIntSize;
164 typedef gfx::RectTyped<ParentLayerPixel> ParentLayerRect;
165 typedef gfx::IntRectTyped<ParentLayerPixel> ParentLayerIntRect;
166 typedef gfx::MarginTyped<ParentLayerPixel> ParentLayerMargin;
167 typedef gfx::IntMarginTyped<ParentLayerPixel> ParentLayerIntMargin;
168 typedef gfx::IntRegionTyped<ParentLayerPixel> ParentLayerIntRegion;
170 typedef gfx::CoordTyped<DesktopPixel> DesktopCoord;
171 typedef gfx::IntCoordTyped<DesktopPixel> DesktopIntCoord;
172 typedef gfx::PointTyped<DesktopPixel> DesktopPoint;
173 typedef gfx::IntPointTyped<DesktopPixel> DesktopIntPoint;
174 typedef gfx::SizeTyped<DesktopPixel> DesktopSize;
175 typedef gfx::IntSizeTyped<DesktopPixel> DesktopIntSize;
176 typedef gfx::RectTyped<DesktopPixel> DesktopRect;
177 typedef gfx::IntRectTyped<DesktopPixel> DesktopIntRect;
179 typedef gfx::CoordTyped<ExternalPixel> ExternalCoord;
180 typedef gfx::IntCoordTyped<ExternalPixel> ExternalIntCoord;
181 typedef gfx::PointTyped<ExternalPixel> ExternalPoint;
182 typedef gfx::IntPointTyped<ExternalPixel> ExternalIntPoint;
183 typedef gfx::SizeTyped<ExternalPixel> ExternalSize;
184 typedef gfx::IntSizeTyped<ExternalPixel> ExternalIntSize;
185 typedef gfx::RectTyped<ExternalPixel> ExternalRect;
186 typedef gfx::IntRectTyped<ExternalPixel> ExternalIntRect;
187 typedef gfx::MarginTyped<ExternalPixel> ExternalMargin;
188 typedef gfx::IntMarginTyped<ExternalPixel> ExternalIntMargin;
189 typedef gfx::IntRegionTyped<ExternalPixel> ExternalIntRegion;
191 typedef gfx::ScaleFactor<CSSPixel, CSSPixel> CSSToCSSScale;
192 typedef gfx::ScaleFactor<CSSPixel, OuterCSSPixel> CSSToOuterCSSScale;
193 typedef gfx::ScaleFactor<CSSPixel, LayoutDevicePixel> CSSToLayoutDeviceScale;
194 typedef gfx::ScaleFactor<CSSPixel, LayerPixel> CSSToLayerScale;
195 typedef gfx::ScaleFactor<CSSPixel, ScreenPixel> CSSToScreenScale;
196 typedef gfx::ScaleFactor<CSSPixel, ParentLayerPixel> CSSToParentLayerScale;
197 typedef gfx::ScaleFactor<CSSPixel, DesktopPixel> CSSToDesktopScale;
198 typedef gfx::ScaleFactor<OuterCSSPixel, LayoutDevicePixel>
199 OuterCSSToLayoutDeviceScale;
200 typedef gfx::ScaleFactor<LayoutDevicePixel, CSSPixel> LayoutDeviceToCSSScale;
201 typedef gfx::ScaleFactor<LayoutDevicePixel, LayerPixel>
202 LayoutDeviceToLayerScale;
203 typedef gfx::ScaleFactor<LayoutDevicePixel, ScreenPixel>
204 LayoutDeviceToScreenScale;
205 typedef gfx::ScaleFactor<LayoutDevicePixel, ParentLayerPixel>
206 LayoutDeviceToParentLayerScale;
207 typedef gfx::ScaleFactor<LayerPixel, CSSPixel> LayerToCSSScale;
208 typedef gfx::ScaleFactor<LayerPixel, LayoutDevicePixel>
209 LayerToLayoutDeviceScale;
210 typedef gfx::ScaleFactor<LayerPixel, RenderTargetPixel>
211 LayerToRenderTargetScale;
212 typedef gfx::ScaleFactor<LayerPixel, ScreenPixel> LayerToScreenScale;
213 typedef gfx::ScaleFactor<LayerPixel, ParentLayerPixel> LayerToParentLayerScale;
214 typedef gfx::ScaleFactor<RenderTargetPixel, ScreenPixel>
215 RenderTargetToScreenScale;
216 typedef gfx::ScaleFactor<ScreenPixel, CSSPixel> ScreenToCSSScale;
217 typedef gfx::ScaleFactor<ScreenPixel, LayoutDevicePixel>
218 ScreenToLayoutDeviceScale;
219 typedef gfx::ScaleFactor<ScreenPixel, LayerPixel> ScreenToLayerScale;
220 typedef gfx::ScaleFactor<ScreenPixel, ParentLayerPixel>
221 ScreenToParentLayerScale;
222 typedef gfx::ScaleFactor<ParentLayerPixel, LayerPixel> ParentLayerToLayerScale;
223 typedef gfx::ScaleFactor<ParentLayerPixel, ScreenPixel>
224 ParentLayerToScreenScale;
225 typedef gfx::ScaleFactor<ParentLayerPixel, ParentLayerPixel>
226 ParentLayerToParentLayerScale;
227 typedef gfx::ScaleFactor<DesktopPixel, LayoutDevicePixel>
228 DesktopToLayoutDeviceScale;
229 typedef gfx::ScaleFactor<LayoutDevicePixel, DesktopPixel>
230 LayoutDeviceToDesktopScale;
232 typedef gfx::ScaleFactors2D<CSSPixel, LayoutDevicePixel>
233 CSSToLayoutDeviceScale2D;
234 typedef gfx::ScaleFactors2D<CSSPixel, LayerPixel> CSSToLayerScale2D;
235 typedef gfx::ScaleFactors2D<CSSPixel, ScreenPixel> CSSToScreenScale2D;
236 typedef gfx::ScaleFactors2D<CSSPixel, ParentLayerPixel> CSSToParentLayerScale2D;
237 typedef gfx::ScaleFactors2D<LayoutDevicePixel, CSSPixel>
238 LayoutDeviceToCSSScale2D;
239 typedef gfx::ScaleFactors2D<LayoutDevicePixel, LayerPixel>
240 LayoutDeviceToLayerScale2D;
241 typedef gfx::ScaleFactors2D<LayoutDevicePixel, ScreenPixel>
242 LayoutDeviceToScreenScale2D;
243 typedef gfx::ScaleFactors2D<LayoutDevicePixel, ParentLayerPixel>
244 LayoutDeviceToParentLayerScale2D;
245 typedef gfx::ScaleFactors2D<LayerPixel, CSSPixel> LayerToCSSScale2D;
246 typedef gfx::ScaleFactors2D<LayerPixel, LayoutDevicePixel>
247 LayerToLayoutDeviceScale2D;
248 typedef gfx::ScaleFactors2D<LayerPixel, RenderTargetPixel>
249 LayerToRenderTargetScale2D;
250 typedef gfx::ScaleFactors2D<LayerPixel, ScreenPixel> LayerToScreenScale2D;
251 typedef gfx::ScaleFactors2D<LayerPixel, ParentLayerPixel>
252 LayerToParentLayerScale2D;
253 typedef gfx::ScaleFactors2D<RenderTargetPixel, ScreenPixel>
254 RenderTargetToScreenScale2D;
255 typedef gfx::ScaleFactors2D<ScreenPixel, CSSPixel> ScreenToCSSScale2D;
256 typedef gfx::ScaleFactors2D<ScreenPixel, LayoutDevicePixel>
257 ScreenToLayoutDeviceScale2D;
258 typedef gfx::ScaleFactors2D<ScreenPixel, ScreenPixel> ScreenToScreenScale2D;
259 typedef gfx::ScaleFactors2D<ScreenPixel, LayerPixel> ScreenToLayerScale2D;
260 typedef gfx::ScaleFactors2D<ScreenPixel, ParentLayerPixel>
261 ScreenToParentLayerScale2D;
262 typedef gfx::ScaleFactors2D<ParentLayerPixel, LayerPixel>
263 ParentLayerToLayerScale2D;
264 typedef gfx::ScaleFactors2D<ParentLayerPixel, ScreenPixel>
265 ParentLayerToScreenScale2D;
266 typedef gfx::ScaleFactors2D<ParentLayerPixel, ParentLayerPixel>
267 ParentLayerToParentLayerScale2D;
268 typedef gfx::ScaleFactors2D<gfx::UnknownUnits, gfx::UnknownUnits> Scale2D;
270 typedef gfx::Matrix4x4Typed<CSSPixel, CSSPixel> CSSToCSSMatrix4x4;
271 typedef gfx::Matrix4x4Typed<LayoutDevicePixel, LayoutDevicePixel>
272 LayoutDeviceToLayoutDeviceMatrix4x4;
273 typedef gfx::Matrix4x4Typed<LayoutDevicePixel, ParentLayerPixel>
274 LayoutDeviceToParentLayerMatrix4x4;
275 typedef gfx::Matrix4x4Typed<LayerPixel, ParentLayerPixel>
276 LayerToParentLayerMatrix4x4;
277 typedef gfx::Matrix4x4Typed<LayerPixel, ScreenPixel> LayerToScreenMatrix4x4;
278 typedef gfx::Matrix4x4Typed<ScreenPixel, ScreenPixel> ScreenToScreenMatrix4x4;
279 typedef gfx::Matrix4x4Typed<ScreenPixel, ParentLayerPixel>
280 ScreenToParentLayerMatrix4x4;
281 typedef gfx::Matrix4x4Typed<ParentLayerPixel, LayerPixel>
282 ParentLayerToLayerMatrix4x4;
283 typedef gfx::Matrix4x4Typed<ParentLayerPixel, ScreenPixel>
284 ParentLayerToScreenMatrix4x4;
285 typedef gfx::Matrix4x4Typed<ParentLayerPixel, ParentLayerPixel>
286 ParentLayerToParentLayerMatrix4x4;
287 typedef gfx::Matrix4x4Typed<ParentLayerPixel, RenderTargetPixel>
288 ParentLayerToRenderTargetMatrix4x4;
289 typedef gfx::Matrix4x4Typed<ExternalPixel, ParentLayerPixel>
290 ExternalToParentLayerMatrix4x4;
293 * The pixels that content authors use to specify sizes in.
295 struct CSSPixel {
296 // Conversions from app units
297 static CSSCoord FromAppUnits(nscoord aCoord) {
298 return NSAppUnitsToFloatPixels(aCoord, float(AppUnitsPerCSSPixel()));
301 static CSSIntCoord FromAppUnitsRounded(nscoord aCoord) {
302 return NSAppUnitsToIntPixels(aCoord, float(AppUnitsPerCSSPixel()));
305 static CSSPoint FromAppUnits(const nsPoint& aPoint) {
306 return CSSPoint(FromAppUnits(aPoint.x), FromAppUnits(aPoint.y));
309 static CSSSize FromAppUnits(const nsSize& aSize) {
310 return CSSSize(FromAppUnits(aSize.width), FromAppUnits(aSize.height));
313 static CSSRect FromAppUnits(const nsRect& aRect) {
314 return CSSRect(FromAppUnits(aRect.x), FromAppUnits(aRect.y),
315 FromAppUnits(aRect.Width()), FromAppUnits(aRect.Height()));
318 static CSSMargin FromAppUnits(const nsMargin& aMargin) {
319 return CSSMargin(FromAppUnits(aMargin.top), FromAppUnits(aMargin.right),
320 FromAppUnits(aMargin.bottom), FromAppUnits(aMargin.left));
323 static CSSIntPoint FromAppUnitsRounded(const nsPoint& aPoint) {
324 return CSSIntPoint(FromAppUnitsRounded(aPoint.x),
325 FromAppUnitsRounded(aPoint.y));
328 static CSSIntSize FromAppUnitsRounded(const nsSize& aSize) {
329 return CSSIntSize(FromAppUnitsRounded(aSize.width),
330 FromAppUnitsRounded(aSize.height));
333 static CSSIntRect FromAppUnitsRounded(const nsRect& aRect) {
334 return CSSIntRect(FromAppUnitsRounded(aRect.x),
335 FromAppUnitsRounded(aRect.y),
336 FromAppUnitsRounded(aRect.Width()),
337 FromAppUnitsRounded(aRect.Height()));
340 static CSSIntMargin FromAppUnitsRounded(const nsMargin& aMargin) {
341 return CSSIntMargin(
342 FromAppUnitsRounded(aMargin.top), FromAppUnitsRounded(aMargin.right),
343 FromAppUnitsRounded(aMargin.bottom), FromAppUnitsRounded(aMargin.left));
346 static CSSIntRect FromAppUnitsToNearest(const nsRect& aRect) {
347 return CSSIntRect::FromUnknownRect(
348 aRect.ToNearestPixels(AppUnitsPerCSSPixel()));
351 static CSSIntRect FromAppUnitsToInside(const nsRect& aRect) {
352 return CSSIntRect::FromUnknownRect(
353 aRect.ToInsidePixels(AppUnitsPerCSSPixel()));
356 // Conversions to app units
358 // TODO: We might want an int32_t/CSSIntCoord overload which doesn't do float
359 // math but we'd need to ensure stuff is clamped to nscoord_MIN/MAX range.
360 static nscoord ToAppUnits(CSSCoord aCoord) {
361 return NSFloatPixelsToAppUnits(aCoord, AppUnitsPerCSSPixel());
364 static nsPoint ToAppUnits(const CSSPoint& aPoint) {
365 return nsPoint(ToAppUnits(aPoint.x), ToAppUnits(aPoint.y));
368 static nsPoint ToAppUnits(const CSSIntPoint& aPoint) {
369 return nsPoint(ToAppUnits(CSSCoord(aPoint.x)),
370 ToAppUnits(CSSCoord(aPoint.y)));
373 static nsSize ToAppUnits(const CSSSize& aSize) {
374 return nsSize(ToAppUnits(aSize.width), ToAppUnits(aSize.height));
377 static nsSize ToAppUnits(const CSSIntSize& aSize) {
378 return nsSize(ToAppUnits(aSize.width), ToAppUnits(aSize.height));
381 static nsRect ToAppUnits(const CSSRect& aRect) {
382 return nsRect(ToAppUnits(aRect.x), ToAppUnits(aRect.y),
383 ToAppUnits(aRect.Width()), ToAppUnits(aRect.Height()));
386 static nsRect ToAppUnits(const CSSIntRect& aRect) {
387 return nsRect(ToAppUnits(aRect.x), ToAppUnits(aRect.y),
388 ToAppUnits(aRect.Width()), ToAppUnits(aRect.Height()));
391 static nsMargin ToAppUnits(const CSSMargin& aMargin) {
392 return nsMargin(ToAppUnits(aMargin.top), ToAppUnits(aMargin.right),
393 ToAppUnits(aMargin.bottom), ToAppUnits(aMargin.left));
396 static nsMargin ToAppUnits(const CSSIntMargin& aMargin) {
397 return nsMargin(ToAppUnits(CSSCoord(aMargin.top)),
398 ToAppUnits(CSSCoord(aMargin.right)),
399 ToAppUnits(CSSCoord(aMargin.bottom)),
400 ToAppUnits(CSSCoord(aMargin.left)));
403 // Conversion from a given CSS point value.
404 static CSSCoord FromPoints(float aCoord) {
405 // One inch / 72.
406 return aCoord * 96.0f / 72.0f;
411 * In the context of a scroll frame that is zoomable, OuterCSSPixel is
412 * used to disambiguate CSS pixels of the content outside of the scroll
413 * frame (which is not subject to the zoom) from CSS pixels of the content
414 * inside the scroll frame (which is, and for which CSSPixel is used).
416 struct OuterCSSPixel {
417 static OuterCSSCoord FromAppUnits(nscoord aCoord) {
418 return NSAppUnitsToFloatPixels(aCoord, float(AppUnitsPerCSSPixel()));
423 * The pixels that are referred to as "device pixels" in layout code. In
424 * general values measured in LayoutDevicePixels are obtained by dividing a
425 * value in app units by AppUnitsPerDevPixel(). Conversion between CSS pixels
426 * and LayoutDevicePixels is affected by:
427 * 1) the "full zoom" (see nsPresContext::SetFullZoom)
428 * 2) the "widget scale" (see nsIWidget::GetDefaultScale)
430 struct LayoutDevicePixel {
431 static LayoutDeviceCoord FromAppUnits(nscoord aCoord,
432 nscoord aAppUnitsPerDevPixel) {
433 return LayoutDeviceCoord(
434 NSAppUnitsToFloatPixels(aCoord, float(aAppUnitsPerDevPixel)));
437 static LayoutDeviceIntCoord FromAppUnitsRounded(
438 nscoord aCoord, nscoord aAppUnitsPerDevPixel) {
439 return LayoutDeviceIntCoord(
440 NSAppUnitsToIntPixels(aCoord, float(aAppUnitsPerDevPixel)));
443 static LayoutDeviceRect FromAppUnits(const nsRect& aRect,
444 nscoord aAppUnitsPerDevPixel) {
445 return LayoutDeviceRect(FromAppUnits(aRect.x, aAppUnitsPerDevPixel),
446 FromAppUnits(aRect.y, aAppUnitsPerDevPixel),
447 FromAppUnits(aRect.Width(), aAppUnitsPerDevPixel),
448 FromAppUnits(aRect.Height(), aAppUnitsPerDevPixel));
451 static LayoutDeviceSize FromAppUnits(const nsSize& aSize,
452 nscoord aAppUnitsPerDevPixel) {
453 return LayoutDeviceSize(FromAppUnits(aSize.width, aAppUnitsPerDevPixel),
454 FromAppUnits(aSize.height, aAppUnitsPerDevPixel));
457 static LayoutDevicePoint FromAppUnits(const nsPoint& aPoint,
458 nscoord aAppUnitsPerDevPixel) {
459 return LayoutDevicePoint(FromAppUnits(aPoint.x, aAppUnitsPerDevPixel),
460 FromAppUnits(aPoint.y, aAppUnitsPerDevPixel));
463 static LayoutDeviceMargin FromAppUnits(const nsMargin& aMargin,
464 nscoord aAppUnitsPerDevPixel) {
465 return LayoutDeviceMargin(
466 FromAppUnits(aMargin.top, aAppUnitsPerDevPixel),
467 FromAppUnits(aMargin.right, aAppUnitsPerDevPixel),
468 FromAppUnits(aMargin.bottom, aAppUnitsPerDevPixel),
469 FromAppUnits(aMargin.left, aAppUnitsPerDevPixel));
472 static LayoutDeviceIntPoint FromAppUnitsRounded(
473 const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
474 return LayoutDeviceIntPoint(
475 FromAppUnitsRounded(aPoint.x, aAppUnitsPerDevPixel),
476 FromAppUnitsRounded(aPoint.y, aAppUnitsPerDevPixel));
479 static LayoutDeviceIntRect FromAppUnitsRounded(const nsRect& aRect,
480 nscoord aAppUnitsPerDevPixel) {
481 return LayoutDeviceIntRect(
482 FromAppUnitsRounded(aRect.x, aAppUnitsPerDevPixel),
483 FromAppUnitsRounded(aRect.y, aAppUnitsPerDevPixel),
484 FromAppUnitsRounded(aRect.Width(), aAppUnitsPerDevPixel),
485 FromAppUnitsRounded(aRect.Height(), aAppUnitsPerDevPixel));
488 static LayoutDeviceIntPoint FromAppUnitsToNearest(
489 const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
490 return LayoutDeviceIntPoint::FromUnknownPoint(
491 aPoint.ToNearestPixels(aAppUnitsPerDevPixel));
494 static LayoutDeviceIntRect FromAppUnitsToNearest(
495 const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
496 return LayoutDeviceIntRect::FromUnknownRect(
497 aRect.ToNearestPixels(aAppUnitsPerDevPixel));
500 static LayoutDeviceIntRect FromAppUnitsToInside(
501 const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
502 return LayoutDeviceIntRect::FromUnknownRect(
503 aRect.ToInsidePixels(aAppUnitsPerDevPixel));
506 static LayoutDeviceIntRect FromAppUnitsToOutside(
507 const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
508 return LayoutDeviceIntRect::FromUnknownRect(
509 aRect.ToOutsidePixels(aAppUnitsPerDevPixel));
512 static LayoutDeviceIntSize FromAppUnitsRounded(const nsSize& aSize,
513 nscoord aAppUnitsPerDevPixel) {
514 return LayoutDeviceIntSize(
515 FromAppUnitsRounded(aSize.width, aAppUnitsPerDevPixel),
516 FromAppUnitsRounded(aSize.height, aAppUnitsPerDevPixel));
519 static LayoutDeviceIntMargin FromAppUnitsRounded(
520 const nsMargin& aMargin, nscoord aAppUnitsPerDevPixel) {
521 return LayoutDeviceIntMargin(
522 FromAppUnitsRounded(aMargin.top, aAppUnitsPerDevPixel),
523 FromAppUnitsRounded(aMargin.right, aAppUnitsPerDevPixel),
524 FromAppUnitsRounded(aMargin.bottom, aAppUnitsPerDevPixel),
525 FromAppUnitsRounded(aMargin.left, aAppUnitsPerDevPixel));
528 static nscoord ToAppUnits(LayoutDeviceIntCoord aCoord,
529 nscoord aAppUnitsPerDevPixel) {
530 return aCoord * aAppUnitsPerDevPixel;
533 static nscoord ToAppUnits(int32_t aCoord, nscoord aAppUnitsPerDevPixel) {
534 return ToAppUnits(LayoutDeviceIntCoord(aCoord), aAppUnitsPerDevPixel);
537 static nscoord ToAppUnits(LayoutDeviceCoord aCoord,
538 nscoord aAppUnitsPerDevPixel) {
539 return NSFloatPixelsToAppUnits(aCoord, aAppUnitsPerDevPixel);
542 static nscoord ToAppUnits(float aCoord, nscoord aAppUnitsPerDevPixel) {
543 return ToAppUnits(LayoutDeviceCoord(aCoord), aAppUnitsPerDevPixel);
546 static nsPoint ToAppUnits(const LayoutDeviceIntPoint& aPoint,
547 nscoord aAppUnitsPerDevPixel) {
548 return nsPoint(ToAppUnits(aPoint.x, aAppUnitsPerDevPixel),
549 ToAppUnits(aPoint.y, aAppUnitsPerDevPixel));
552 static nsSize ToAppUnits(const LayoutDeviceIntSize& aSize,
553 nscoord aAppUnitsPerDevPixel) {
554 return nsSize(ToAppUnits(aSize.width, aAppUnitsPerDevPixel),
555 ToAppUnits(aSize.height, aAppUnitsPerDevPixel));
558 static nsSize ToAppUnits(const LayoutDeviceSize& aSize,
559 nscoord aAppUnitsPerDevPixel) {
560 return nsSize(ToAppUnits(aSize.width, aAppUnitsPerDevPixel),
561 ToAppUnits(aSize.height, aAppUnitsPerDevPixel));
564 static nsRect ToAppUnits(const LayoutDeviceIntRect& aRect,
565 nscoord aAppUnitsPerDevPixel) {
566 return nsRect(ToAppUnits(aRect.x, aAppUnitsPerDevPixel),
567 ToAppUnits(aRect.y, aAppUnitsPerDevPixel),
568 ToAppUnits(aRect.Width(), aAppUnitsPerDevPixel),
569 ToAppUnits(aRect.Height(), aAppUnitsPerDevPixel));
572 static nsRect ToAppUnits(const LayoutDeviceRect& aRect,
573 nscoord aAppUnitsPerDevPixel) {
574 return nsRect(ToAppUnits(aRect.x, aAppUnitsPerDevPixel),
575 ToAppUnits(aRect.y, aAppUnitsPerDevPixel),
576 ToAppUnits(aRect.Width(), aAppUnitsPerDevPixel),
577 ToAppUnits(aRect.Height(), aAppUnitsPerDevPixel));
580 static nsMargin ToAppUnits(const LayoutDeviceIntMargin& aMargin,
581 nscoord aAppUnitsPerDevPixel) {
582 return nsMargin(ToAppUnits(aMargin.top, aAppUnitsPerDevPixel),
583 ToAppUnits(aMargin.right, aAppUnitsPerDevPixel),
584 ToAppUnits(aMargin.bottom, aAppUnitsPerDevPixel),
585 ToAppUnits(aMargin.left, aAppUnitsPerDevPixel));
588 static nsMargin ToAppUnits(const LayoutDeviceMargin& aMargin,
589 nscoord aAppUnitsPerDevPixel) {
590 return nsMargin(ToAppUnits(aMargin.top, aAppUnitsPerDevPixel),
591 ToAppUnits(aMargin.right, aAppUnitsPerDevPixel),
592 ToAppUnits(aMargin.bottom, aAppUnitsPerDevPixel),
593 ToAppUnits(aMargin.left, aAppUnitsPerDevPixel));
598 * The pixels that layout rasterizes and delivers to the graphics code.
599 * These also are generally referred to as "device pixels" in layout code.
600 * Conversion between CSS pixels and LayerPixels is affected by:
601 * 1) the "display resolution" (see PresShell::SetResolution)
602 * 2) the "full zoom" (see nsPresContext::SetFullZoom)
603 * 3) the "widget scale" (see nsIWidget::GetDefaultScale)
604 * 4) rasterizing at a different scale in the presence of some CSS transforms
606 struct LayerPixel {};
609 * This is Layer coordinates with the Layer's CSS transform applied.
610 * It can be thought of as intermediate between LayerPixel and
611 * ParentLayerPixel, as further applying async transforms to this
612 * yields ParentLayerPixels.
614 struct CSSTransformedLayerPixel {};
617 * Layers are always composited to a render target. This unit
618 * represents one pixel in the render target. Note that for the
619 * root render target RenderTargetPixel == ScreenPixel. Also
620 * any ContainerLayer providing an intermediate surface will
621 * have RenderTargetPixel == LayerPixel.
623 struct RenderTargetPixel {};
626 * This unit represents one pixel in an image. Image space
627 * is largely independent of any other space.
629 struct ImagePixel {};
632 * The pixels that are displayed on the screen.
633 * On non-OMTC platforms this should be equivalent to LayerPixel units.
634 * On OMTC platforms these may diverge from LayerPixel units temporarily,
635 * while an asynchronous zoom is happening, but should eventually converge
636 * back to LayerPixel units. Some variables (such as those representing
637 * chrome UI element sizes) that are not subject to content zoom should
638 * generally be represented in ScreenPixel units.
640 struct ScreenPixel {};
642 /* The layer coordinates of the parent frame.
643 * This can be arrived at in three ways:
644 * - Start with the CSS coordinates of the parent frame, multiply by the
645 * device scale and the cumulative resolution of the parent frame.
646 * - Start with the CSS coordinates of current frame, multiply by the device
647 * scale, the cumulative resolution of the current frame, and the scales
648 * from the CSS and async transforms of the current frame.
649 * - Start with global screen coordinates and unapply all CSS and async
650 * transforms from the root down to and including the parent.
651 * It's helpful to look at
652 * https://wiki.mozilla.org/Platform/GFX/APZ#Coordinate_systems to get a picture
653 * of how the various coordinate systems relate to each other.
655 struct ParentLayerPixel {};
658 * Pixels in the coordinate space used by the host OS to manage windows on the
659 * desktop. What these mean varies between OSs:
660 * - by default (unless implemented differently in platform-specific widget
661 * code) they are the same as LayoutDevicePixels
662 * - on Mac OS X, they're "cocoa points", which correspond to device pixels
663 * on a non-Retina display, and to 2x device pixels on Retina
664 * - on Windows *without* per-monitor DPI support, they are Windows "logical
665 * pixels", i.e. device pixels scaled according to the Windows display DPI
666 * scaling factor (typically one of 1.25, 1.5, 1.75, 2.0...)
667 * - on Windows *with* per-monitor DPI support, they are physical device pixels
668 * on each screen; note that this means the scaling between CSS pixels and
669 * desktop pixels may vary across multiple displays.
671 struct DesktopPixel {};
673 struct ExternalPixel {};
675 // Operators to apply ScaleFactors directly to Coords, Points, Rects, Sizes and
676 // Margins
678 template <class Src, class Dst>
679 gfx::CoordTyped<Dst> operator*(const gfx::CoordTyped<Src>& aCoord,
680 const gfx::ScaleFactor<Src, Dst>& aScale) {
681 return gfx::CoordTyped<Dst>(aCoord.value * aScale.scale);
684 template <class Src, class Dst>
685 gfx::CoordTyped<Dst> operator/(const gfx::CoordTyped<Src>& aCoord,
686 const gfx::ScaleFactor<Dst, Src>& aScale) {
687 return gfx::CoordTyped<Dst>(aCoord.value / aScale.scale);
690 template <class Src, class Dst>
691 gfx::PointTyped<Dst> operator*(const gfx::PointTyped<Src>& aPoint,
692 const gfx::ScaleFactor<Src, Dst>& aScale) {
693 return gfx::PointTyped<Dst>(aPoint.x * aScale.scale, aPoint.y * aScale.scale);
696 template <class Src, class Dst>
697 gfx::PointTyped<Dst> operator/(const gfx::PointTyped<Src>& aPoint,
698 const gfx::ScaleFactor<Dst, Src>& aScale) {
699 return gfx::PointTyped<Dst>(aPoint.x / aScale.scale, aPoint.y / aScale.scale);
702 template <class Src, class Dst, class F>
703 gfx::PointTyped<Dst, F> operator*(
704 const gfx::PointTyped<Src, F>& aPoint,
705 const gfx::BaseScaleFactors2D<Src, Dst, F>& aScale) {
706 return gfx::PointTyped<Dst, F>(aPoint.x * aScale.xScale,
707 aPoint.y * aScale.yScale);
710 template <class Src, class Dst, class F>
711 gfx::PointTyped<Dst, F> operator/(
712 const gfx::PointTyped<Src, F>& aPoint,
713 const gfx::BaseScaleFactors2D<Dst, Src, F>& aScale) {
714 return gfx::PointTyped<Dst, F>(aPoint.x / aScale.xScale,
715 aPoint.y / aScale.yScale);
718 template <class Src, class Dst>
719 gfx::PointTyped<Dst> operator*(const gfx::IntPointTyped<Src>& aPoint,
720 const gfx::ScaleFactor<Src, Dst>& aScale) {
721 return gfx::PointTyped<Dst>(float(aPoint.x) * aScale.scale,
722 float(aPoint.y) * aScale.scale);
725 template <class Src, class Dst>
726 gfx::PointTyped<Dst> operator/(const gfx::IntPointTyped<Src>& aPoint,
727 const gfx::ScaleFactor<Dst, Src>& aScale) {
728 return gfx::PointTyped<Dst>(float(aPoint.x) / aScale.scale,
729 float(aPoint.y) / aScale.scale);
732 template <class Src, class Dst, class F>
733 gfx::PointTyped<Dst, F> operator*(
734 const gfx::IntPointTyped<Src>& aPoint,
735 const gfx::BaseScaleFactors2D<Src, Dst, F>& aScale) {
736 return gfx::PointTyped<Dst, F>(F(aPoint.x) * aScale.xScale,
737 F(aPoint.y) * aScale.yScale);
740 template <class Src, class Dst, class F>
741 gfx::PointTyped<Dst, F> operator/(
742 const gfx::IntPointTyped<Src>& aPoint,
743 const gfx::BaseScaleFactors2D<Dst, Src, F>& aScale) {
744 return gfx::PointTyped<Dst, F>(F(aPoint.x) / aScale.xScale,
745 F(aPoint.y) / aScale.yScale);
748 template <class Src, class Dst>
749 gfx::RectTyped<Dst> operator*(const gfx::RectTyped<Src>& aRect,
750 const gfx::ScaleFactor<Src, Dst>& aScale) {
751 return gfx::RectTyped<Dst>(aRect.x * aScale.scale, aRect.y * aScale.scale,
752 aRect.Width() * aScale.scale,
753 aRect.Height() * aScale.scale);
756 template <class Src, class Dst>
757 gfx::RectTyped<Dst> operator/(const gfx::RectTyped<Src>& aRect,
758 const gfx::ScaleFactor<Dst, Src>& aScale) {
759 return gfx::RectTyped<Dst>(aRect.x / aScale.scale, aRect.y / aScale.scale,
760 aRect.Width() / aScale.scale,
761 aRect.Height() / aScale.scale);
764 template <class Src, class Dst, class F>
765 gfx::RectTyped<Dst, F> operator*(
766 const gfx::RectTyped<Src, F>& aRect,
767 const gfx::BaseScaleFactors2D<Src, Dst, F>& aScale) {
768 return gfx::RectTyped<Dst, F>(
769 aRect.x * aScale.xScale, aRect.y * aScale.yScale,
770 aRect.Width() * aScale.xScale, aRect.Height() * aScale.yScale);
773 template <class Src, class Dst, class F>
774 gfx::RectTyped<Dst, F> operator/(
775 const gfx::RectTyped<Src, F>& aRect,
776 const gfx::BaseScaleFactors2D<Dst, Src, F>& aScale) {
777 return gfx::RectTyped<Dst, F>(
778 aRect.x / aScale.xScale, aRect.y / aScale.yScale,
779 aRect.Width() / aScale.xScale, aRect.Height() / aScale.yScale);
782 template <class Src, class Dst>
783 gfx::RectTyped<Dst> operator*(const gfx::IntRectTyped<Src>& aRect,
784 const gfx::ScaleFactor<Src, Dst>& aScale) {
785 return gfx::RectTyped<Dst>(float(aRect.x) * aScale.scale,
786 float(aRect.y) * aScale.scale,
787 float(aRect.Width()) * aScale.scale,
788 float(aRect.Height()) * aScale.scale);
791 template <class Src, class Dst>
792 gfx::RectTyped<Dst> operator/(const gfx::IntRectTyped<Src>& aRect,
793 const gfx::ScaleFactor<Dst, Src>& aScale) {
794 return gfx::RectTyped<Dst>(float(aRect.x) / aScale.scale,
795 float(aRect.y) / aScale.scale,
796 float(aRect.Width()) / aScale.scale,
797 float(aRect.Height()) / aScale.scale);
800 template <class Src, class Dst, class F>
801 gfx::RectTyped<Dst, F> operator*(
802 const gfx::IntRectTyped<Src>& aRect,
803 const gfx::BaseScaleFactors2D<Src, Dst, F>& aScale) {
804 return gfx::RectTyped<Dst, F>(
805 F(aRect.x) * aScale.xScale, F(aRect.y) * aScale.yScale,
806 F(aRect.Width()) * aScale.xScale, F(aRect.Height()) * aScale.yScale);
809 template <class Src, class Dst, class F>
810 gfx::RectTyped<Dst, F> operator/(
811 const gfx::IntRectTyped<Src>& aRect,
812 const gfx::BaseScaleFactors2D<Dst, Src, F>& aScale) {
813 return gfx::RectTyped<Dst, F>(
814 F(aRect.x) / aScale.xScale, F(aRect.y) / aScale.yScale,
815 F(aRect.Width()) / aScale.xScale, F(aRect.Height()) / aScale.yScale);
818 template <class Src, class Dst>
819 gfx::SizeTyped<Dst> operator*(const gfx::SizeTyped<Src>& aSize,
820 const gfx::ScaleFactor<Src, Dst>& aScale) {
821 return gfx::SizeTyped<Dst>(aSize.width * aScale.scale,
822 aSize.height * aScale.scale);
825 template <class Src, class Dst>
826 gfx::SizeTyped<Dst> operator/(const gfx::SizeTyped<Src>& aSize,
827 const gfx::ScaleFactor<Dst, Src>& aScale) {
828 return gfx::SizeTyped<Dst>(aSize.width / aScale.scale,
829 aSize.height / aScale.scale);
832 template <class Src, class Dst, class F>
833 gfx::SizeTyped<Dst, F> operator*(
834 const gfx::SizeTyped<Src, F>& aSize,
835 const gfx::BaseScaleFactors2D<Src, Dst, F>& aScale) {
836 return gfx::SizeTyped<Dst, F>(aSize.width * aScale.xScale,
837 aSize.height * aScale.yScale);
840 template <class Src, class Dst, class F>
841 gfx::SizeTyped<Dst, F> operator/(
842 const gfx::SizeTyped<Src, F>& aSize,
843 const gfx::BaseScaleFactors2D<Dst, Src, F>& aScale) {
844 return gfx::SizeTyped<Dst, F>(aSize.width / aScale.xScale,
845 aSize.height / aScale.yScale);
848 template <class Src, class Dst>
849 gfx::SizeTyped<Dst> operator*(const gfx::IntSizeTyped<Src>& aSize,
850 const gfx::ScaleFactor<Src, Dst>& aScale) {
851 return gfx::SizeTyped<Dst>(float(aSize.width) * aScale.scale,
852 float(aSize.height) * aScale.scale);
855 template <class Src, class Dst>
856 gfx::SizeTyped<Dst> operator/(const gfx::IntSizeTyped<Src>& aSize,
857 const gfx::ScaleFactor<Dst, Src>& aScale) {
858 return gfx::SizeTyped<Dst>(float(aSize.width) / aScale.scale,
859 float(aSize.height) / aScale.scale);
862 template <class Src, class Dst, class F>
863 gfx::SizeTyped<Dst, F> operator*(
864 const gfx::IntSizeTyped<Src>& aSize,
865 const gfx::BaseScaleFactors2D<Src, Dst, F>& aScale) {
866 return gfx::SizeTyped<Dst, F>(F(aSize.width) * aScale.xScale,
867 F(aSize.height) * aScale.yScale);
870 template <class Src, class Dst, class F>
871 gfx::SizeTyped<Dst, F> operator/(
872 const gfx::IntSizeTyped<Src>& aSize,
873 const gfx::BaseScaleFactors2D<Dst, Src, F>& aScale) {
874 return gfx::SizeTyped<Dst, F>(F(aSize.width) / aScale.xScale,
875 F(aSize.height) / aScale.yScale);
878 template <class Src, class Dst>
879 gfx::MarginTyped<Dst> operator*(const gfx::MarginTyped<Src>& aMargin,
880 const gfx::ScaleFactor<Src, Dst>& aScale) {
881 return gfx::MarginTyped<Dst>(
882 aMargin.top.value * aScale.scale, aMargin.right.value * aScale.scale,
883 aMargin.bottom.value * aScale.scale, aMargin.left.value * aScale.scale);
886 template <class Src, class Dst>
887 gfx::MarginTyped<Dst> operator/(const gfx::MarginTyped<Src>& aMargin,
888 const gfx::ScaleFactor<Dst, Src>& aScale) {
889 return gfx::MarginTyped<Dst>(
890 aMargin.top / aScale.scale, aMargin.right / aScale.scale,
891 aMargin.bottom / aScale.scale, aMargin.left / aScale.scale);
894 template <class Src, class Dst, class F>
895 gfx::MarginTyped<Dst, F> operator*(
896 const gfx::MarginTyped<Src, F>& aMargin,
897 const gfx::BaseScaleFactors2D<Src, Dst, F>& aScale) {
898 return gfx::MarginTyped<Dst, F>(
899 aMargin.top.value * aScale.yScale, aMargin.right.value * aScale.xScale,
900 aMargin.bottom.value * aScale.yScale, aMargin.left.value * aScale.xScale);
903 template <class Src, class Dst, class F>
904 gfx::MarginTyped<Dst, F> operator/(
905 const gfx::MarginTyped<Src, F>& aMargin,
906 const gfx::BaseScaleFactors2D<Dst, Src, F>& aScale) {
907 return gfx::MarginTyped<Dst, F>(
908 aMargin.top.value / aScale.yScale, aMargin.right.value / aScale.xScale,
909 aMargin.bottom.value / aScale.yScale, aMargin.left.value / aScale.xScale);
912 // Calculate the max or min or the ratios of the widths and heights of two
913 // sizes, returning a scale factor in the correct units.
915 template <class Src, class Dst>
916 gfx::ScaleFactor<Src, Dst> MaxScaleRatio(const gfx::SizeTyped<Dst>& aDestSize,
917 const gfx::SizeTyped<Src>& aSrcSize) {
918 MOZ_ASSERT(aSrcSize.width != 0 && aSrcSize.height != 0,
919 "Caller must verify aSrcSize has nonzero components, "
920 "to avoid division by 0 here");
921 return gfx::ScaleFactor<Src, Dst>(std::max(
922 aDestSize.width / aSrcSize.width, aDestSize.height / aSrcSize.height));
925 template <class Src, class Dst>
926 gfx::ScaleFactor<Src, Dst> MinScaleRatio(const gfx::SizeTyped<Dst>& aDestSize,
927 const gfx::SizeTyped<Src>& aSrcSize) {
928 MOZ_ASSERT(aSrcSize.width != 0 && aSrcSize.height != 0,
929 "Caller must verify aSrcSize has nonzero components, "
930 "to avoid division by 0 here");
931 return gfx::ScaleFactor<Src, Dst>(std::min(
932 aDestSize.width / aSrcSize.width, aDestSize.height / aSrcSize.height));
935 template <typename T>
936 struct CoordOfImpl;
938 template <typename Units>
939 struct CoordOfImpl<gfx::PointTyped<Units>> {
940 typedef gfx::CoordTyped<Units> Type;
943 template <typename Units>
944 struct CoordOfImpl<gfx::IntPointTyped<Units>> {
945 typedef gfx::IntCoordTyped<Units> Type;
948 template <typename Units>
949 struct CoordOfImpl<gfx::RectTyped<Units>> {
950 typedef gfx::CoordTyped<Units> Type;
953 template <typename Units>
954 struct CoordOfImpl<gfx::IntRectTyped<Units>> {
955 typedef gfx::IntCoordTyped<Units> Type;
958 template <typename Units>
959 struct CoordOfImpl<gfx::SizeTyped<Units>> {
960 typedef gfx::CoordTyped<Units> Type;
963 template <typename T>
964 using CoordOf = typename CoordOfImpl<T>::Type;
966 } // namespace mozilla
968 #endif