michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZ_UNIT_TRANSFORMS_H_ michael@0: #define MOZ_UNIT_TRANSFORMS_H_ michael@0: michael@0: #include "Units.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: // Convenience functions for converting an entity from one strongly-typed michael@0: // coordinate system to another without changing the values it stores (this michael@0: // can be thought of as a cast). michael@0: // To use these functions, you must provide a justification for each use! michael@0: // Feel free to add more justifications to PixelCastJustification, along with michael@0: // a comment that explains under what circumstances it is appropriate to use. michael@0: michael@0: MOZ_BEGIN_ENUM_CLASS(PixelCastJustification, uint8_t) michael@0: // For the root layer, Screen Pixel = Parent Layer Pixel. michael@0: ScreenToParentLayerForRoot, michael@0: // For the root composition size we want to view it as layer pixels in any layer michael@0: ParentLayerToLayerForRootComposition michael@0: MOZ_END_ENUM_CLASS(PixelCastJustification) michael@0: michael@0: template michael@0: gfx::SizeTyped ViewAs(const gfx::SizeTyped& aSize, PixelCastJustification) { michael@0: return gfx::SizeTyped(aSize.width, aSize.height); michael@0: } michael@0: template michael@0: gfx::IntSizeTyped ViewAs(const gfx::IntSizeTyped& aSize, PixelCastJustification) { michael@0: return gfx::IntSizeTyped(aSize.width, aSize.height); michael@0: } michael@0: michael@0: // Convenience functions for casting untyped entities to typed entities. michael@0: // Using these functions does not require a justification, but once we convert michael@0: // all code to use strongly typed units they should not be needed any longer. michael@0: template michael@0: gfx::PointTyped ViewAs(const gfxPoint& aPoint) { michael@0: return gfx::PointTyped(aPoint.x, aPoint.y); michael@0: } michael@0: template michael@0: gfx::RectTyped ViewAs(const gfxRect& aRect) { michael@0: return gfx::RectTyped(aRect.x, aRect.y, aRect.width, aRect.height); michael@0: } michael@0: template michael@0: gfx::IntSizeTyped ViewAs(const nsIntSize& aSize) { michael@0: return gfx::IntSizeTyped(aSize.width, aSize.height); michael@0: } michael@0: template michael@0: gfx::IntPointTyped ViewAs(const nsIntPoint& aPoint) { michael@0: return gfx::IntPointTyped(aPoint.x, aPoint.y); michael@0: } michael@0: template michael@0: gfx::IntRectTyped ViewAs(const nsIntRect& aRect) { michael@0: return gfx::IntRectTyped(aRect.x, aRect.y, aRect.width, aRect.height); michael@0: } michael@0: michael@0: // Convenience functions for casting typed entities to untyped entities. michael@0: // Using these functions does not require a justification, but once we convert michael@0: // all code to use strongly typed units they should not be needed any longer. michael@0: template michael@0: gfxPoint ViewAsUntyped(const gfx::PointTyped& aPoint) { michael@0: return gfxPoint(aPoint.x, aPoint.y); michael@0: } michael@0: template michael@0: gfxRect ViewAsUntyped(const gfx::RectTyped& aRect) { michael@0: return gfxRect(aRect.x, aRect.y, aRect.width, aRect.height); michael@0: } michael@0: michael@0: // Convenience functions for transforming an entity from one strongly-typed michael@0: // coordinate system to another using the provided transformation matrix. michael@0: template michael@0: static gfx::PointTyped TransformTo(const gfx3DMatrix& aTransform, michael@0: const gfx::PointTyped& aPoint) michael@0: { michael@0: return ViewAs(aTransform.Transform(ViewAsUntyped(aPoint))); michael@0: } michael@0: template michael@0: static gfx::RectTyped TransformTo(const gfx3DMatrix& aTransform, michael@0: const gfx::RectTyped& aRect) michael@0: { michael@0: return ViewAs(aTransform.TransformBounds(ViewAsUntyped(aRect))); michael@0: } michael@0: michael@0: michael@0: } michael@0: michael@0: #endif