Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef MOZ_UNIT_TRANSFORMS_H_ |
michael@0 | 8 | #define MOZ_UNIT_TRANSFORMS_H_ |
michael@0 | 9 | |
michael@0 | 10 | #include "Units.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace mozilla { |
michael@0 | 13 | |
michael@0 | 14 | // Convenience functions for converting an entity from one strongly-typed |
michael@0 | 15 | // coordinate system to another without changing the values it stores (this |
michael@0 | 16 | // can be thought of as a cast). |
michael@0 | 17 | // To use these functions, you must provide a justification for each use! |
michael@0 | 18 | // Feel free to add more justifications to PixelCastJustification, along with |
michael@0 | 19 | // a comment that explains under what circumstances it is appropriate to use. |
michael@0 | 20 | |
michael@0 | 21 | MOZ_BEGIN_ENUM_CLASS(PixelCastJustification, uint8_t) |
michael@0 | 22 | // For the root layer, Screen Pixel = Parent Layer Pixel. |
michael@0 | 23 | ScreenToParentLayerForRoot, |
michael@0 | 24 | // For the root composition size we want to view it as layer pixels in any layer |
michael@0 | 25 | ParentLayerToLayerForRootComposition |
michael@0 | 26 | MOZ_END_ENUM_CLASS(PixelCastJustification) |
michael@0 | 27 | |
michael@0 | 28 | template <class TargetUnits, class SourceUnits> |
michael@0 | 29 | gfx::SizeTyped<TargetUnits> ViewAs(const gfx::SizeTyped<SourceUnits>& aSize, PixelCastJustification) { |
michael@0 | 30 | return gfx::SizeTyped<TargetUnits>(aSize.width, aSize.height); |
michael@0 | 31 | } |
michael@0 | 32 | template <class TargetUnits, class SourceUnits> |
michael@0 | 33 | gfx::IntSizeTyped<TargetUnits> ViewAs(const gfx::IntSizeTyped<SourceUnits>& aSize, PixelCastJustification) { |
michael@0 | 34 | return gfx::IntSizeTyped<TargetUnits>(aSize.width, aSize.height); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | // Convenience functions for casting untyped entities to typed entities. |
michael@0 | 38 | // Using these functions does not require a justification, but once we convert |
michael@0 | 39 | // all code to use strongly typed units they should not be needed any longer. |
michael@0 | 40 | template <class TargetUnits> |
michael@0 | 41 | gfx::PointTyped<TargetUnits> ViewAs(const gfxPoint& aPoint) { |
michael@0 | 42 | return gfx::PointTyped<TargetUnits>(aPoint.x, aPoint.y); |
michael@0 | 43 | } |
michael@0 | 44 | template <class TargetUnits> |
michael@0 | 45 | gfx::RectTyped<TargetUnits> ViewAs(const gfxRect& aRect) { |
michael@0 | 46 | return gfx::RectTyped<TargetUnits>(aRect.x, aRect.y, aRect.width, aRect.height); |
michael@0 | 47 | } |
michael@0 | 48 | template <class TargetUnits> |
michael@0 | 49 | gfx::IntSizeTyped<TargetUnits> ViewAs(const nsIntSize& aSize) { |
michael@0 | 50 | return gfx::IntSizeTyped<TargetUnits>(aSize.width, aSize.height); |
michael@0 | 51 | } |
michael@0 | 52 | template <class TargetUnits> |
michael@0 | 53 | gfx::IntPointTyped<TargetUnits> ViewAs(const nsIntPoint& aPoint) { |
michael@0 | 54 | return gfx::IntPointTyped<TargetUnits>(aPoint.x, aPoint.y); |
michael@0 | 55 | } |
michael@0 | 56 | template <class TargetUnits> |
michael@0 | 57 | gfx::IntRectTyped<TargetUnits> ViewAs(const nsIntRect& aRect) { |
michael@0 | 58 | return gfx::IntRectTyped<TargetUnits>(aRect.x, aRect.y, aRect.width, aRect.height); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | // Convenience functions for casting typed entities to untyped entities. |
michael@0 | 62 | // Using these functions does not require a justification, but once we convert |
michael@0 | 63 | // all code to use strongly typed units they should not be needed any longer. |
michael@0 | 64 | template <class SourceUnits> |
michael@0 | 65 | gfxPoint ViewAsUntyped(const gfx::PointTyped<SourceUnits>& aPoint) { |
michael@0 | 66 | return gfxPoint(aPoint.x, aPoint.y); |
michael@0 | 67 | } |
michael@0 | 68 | template <class SourceUnits> |
michael@0 | 69 | gfxRect ViewAsUntyped(const gfx::RectTyped<SourceUnits>& aRect) { |
michael@0 | 70 | return gfxRect(aRect.x, aRect.y, aRect.width, aRect.height); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // Convenience functions for transforming an entity from one strongly-typed |
michael@0 | 74 | // coordinate system to another using the provided transformation matrix. |
michael@0 | 75 | template <typename TargetUnits, typename SourceUnits> |
michael@0 | 76 | static gfx::PointTyped<TargetUnits> TransformTo(const gfx3DMatrix& aTransform, |
michael@0 | 77 | const gfx::PointTyped<SourceUnits>& aPoint) |
michael@0 | 78 | { |
michael@0 | 79 | return ViewAs<TargetUnits>(aTransform.Transform(ViewAsUntyped(aPoint))); |
michael@0 | 80 | } |
michael@0 | 81 | template <typename TargetUnits, typename SourceUnits> |
michael@0 | 82 | static gfx::RectTyped<TargetUnits> TransformTo(const gfx3DMatrix& aTransform, |
michael@0 | 83 | const gfx::RectTyped<SourceUnits>& aRect) |
michael@0 | 84 | { |
michael@0 | 85 | return ViewAs<TargetUnits>(aTransform.TransformBounds(ViewAsUntyped(aRect))); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | #endif |