layout/base/UnitTransforms.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:2fca1890828c
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_UNIT_TRANSFORMS_H_
8 #define MOZ_UNIT_TRANSFORMS_H_
9
10 #include "Units.h"
11
12 namespace mozilla {
13
14 // Convenience functions for converting an entity from one strongly-typed
15 // coordinate system to another without changing the values it stores (this
16 // can be thought of as a cast).
17 // To use these functions, you must provide a justification for each use!
18 // Feel free to add more justifications to PixelCastJustification, along with
19 // a comment that explains under what circumstances it is appropriate to use.
20
21 MOZ_BEGIN_ENUM_CLASS(PixelCastJustification, uint8_t)
22 // For the root layer, Screen Pixel = Parent Layer Pixel.
23 ScreenToParentLayerForRoot,
24 // For the root composition size we want to view it as layer pixels in any layer
25 ParentLayerToLayerForRootComposition
26 MOZ_END_ENUM_CLASS(PixelCastJustification)
27
28 template <class TargetUnits, class SourceUnits>
29 gfx::SizeTyped<TargetUnits> ViewAs(const gfx::SizeTyped<SourceUnits>& aSize, PixelCastJustification) {
30 return gfx::SizeTyped<TargetUnits>(aSize.width, aSize.height);
31 }
32 template <class TargetUnits, class SourceUnits>
33 gfx::IntSizeTyped<TargetUnits> ViewAs(const gfx::IntSizeTyped<SourceUnits>& aSize, PixelCastJustification) {
34 return gfx::IntSizeTyped<TargetUnits>(aSize.width, aSize.height);
35 }
36
37 // Convenience functions for casting untyped entities to typed entities.
38 // Using these functions does not require a justification, but once we convert
39 // all code to use strongly typed units they should not be needed any longer.
40 template <class TargetUnits>
41 gfx::PointTyped<TargetUnits> ViewAs(const gfxPoint& aPoint) {
42 return gfx::PointTyped<TargetUnits>(aPoint.x, aPoint.y);
43 }
44 template <class TargetUnits>
45 gfx::RectTyped<TargetUnits> ViewAs(const gfxRect& aRect) {
46 return gfx::RectTyped<TargetUnits>(aRect.x, aRect.y, aRect.width, aRect.height);
47 }
48 template <class TargetUnits>
49 gfx::IntSizeTyped<TargetUnits> ViewAs(const nsIntSize& aSize) {
50 return gfx::IntSizeTyped<TargetUnits>(aSize.width, aSize.height);
51 }
52 template <class TargetUnits>
53 gfx::IntPointTyped<TargetUnits> ViewAs(const nsIntPoint& aPoint) {
54 return gfx::IntPointTyped<TargetUnits>(aPoint.x, aPoint.y);
55 }
56 template <class TargetUnits>
57 gfx::IntRectTyped<TargetUnits> ViewAs(const nsIntRect& aRect) {
58 return gfx::IntRectTyped<TargetUnits>(aRect.x, aRect.y, aRect.width, aRect.height);
59 }
60
61 // Convenience functions for casting typed entities to untyped entities.
62 // Using these functions does not require a justification, but once we convert
63 // all code to use strongly typed units they should not be needed any longer.
64 template <class SourceUnits>
65 gfxPoint ViewAsUntyped(const gfx::PointTyped<SourceUnits>& aPoint) {
66 return gfxPoint(aPoint.x, aPoint.y);
67 }
68 template <class SourceUnits>
69 gfxRect ViewAsUntyped(const gfx::RectTyped<SourceUnits>& aRect) {
70 return gfxRect(aRect.x, aRect.y, aRect.width, aRect.height);
71 }
72
73 // Convenience functions for transforming an entity from one strongly-typed
74 // coordinate system to another using the provided transformation matrix.
75 template <typename TargetUnits, typename SourceUnits>
76 static gfx::PointTyped<TargetUnits> TransformTo(const gfx3DMatrix& aTransform,
77 const gfx::PointTyped<SourceUnits>& aPoint)
78 {
79 return ViewAs<TargetUnits>(aTransform.Transform(ViewAsUntyped(aPoint)));
80 }
81 template <typename TargetUnits, typename SourceUnits>
82 static gfx::RectTyped<TargetUnits> TransformTo(const gfx3DMatrix& aTransform,
83 const gfx::RectTyped<SourceUnits>& aRect)
84 {
85 return ViewAs<TargetUnits>(aTransform.TransformBounds(ViewAsUntyped(aRect)));
86 }
87
88
89 }
90
91 #endif

mercurial