layout/base/UnitTransforms.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/base/UnitTransforms.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef MOZ_UNIT_TRANSFORMS_H_
    1.11 +#define MOZ_UNIT_TRANSFORMS_H_
    1.12 +
    1.13 +#include "Units.h"
    1.14 +
    1.15 +namespace mozilla {
    1.16 +
    1.17 +// Convenience functions for converting an entity from one strongly-typed
    1.18 +// coordinate system to another without changing the values it stores (this
    1.19 +// can be thought of as a cast).
    1.20 +// To use these functions, you must provide a justification for each use!
    1.21 +// Feel free to add more justifications to PixelCastJustification, along with
    1.22 +// a comment that explains under what circumstances it is appropriate to use.
    1.23 +
    1.24 +MOZ_BEGIN_ENUM_CLASS(PixelCastJustification, uint8_t)
    1.25 +  // For the root layer, Screen Pixel = Parent Layer Pixel.
    1.26 +  ScreenToParentLayerForRoot,
    1.27 +  // For the root composition size we want to view it as layer pixels in any layer
    1.28 +  ParentLayerToLayerForRootComposition
    1.29 +MOZ_END_ENUM_CLASS(PixelCastJustification)
    1.30 +
    1.31 +template <class TargetUnits, class SourceUnits>
    1.32 +gfx::SizeTyped<TargetUnits> ViewAs(const gfx::SizeTyped<SourceUnits>& aSize, PixelCastJustification) {
    1.33 +  return gfx::SizeTyped<TargetUnits>(aSize.width, aSize.height);
    1.34 +}
    1.35 +template <class TargetUnits, class SourceUnits>
    1.36 +gfx::IntSizeTyped<TargetUnits> ViewAs(const gfx::IntSizeTyped<SourceUnits>& aSize, PixelCastJustification) {
    1.37 +  return gfx::IntSizeTyped<TargetUnits>(aSize.width, aSize.height);
    1.38 +}
    1.39 +
    1.40 +// Convenience functions for casting untyped entities to typed entities.
    1.41 +// Using these functions does not require a justification, but once we convert
    1.42 +// all code to use strongly typed units they should not be needed any longer.
    1.43 +template <class TargetUnits>
    1.44 +gfx::PointTyped<TargetUnits> ViewAs(const gfxPoint& aPoint) {
    1.45 +  return gfx::PointTyped<TargetUnits>(aPoint.x, aPoint.y);
    1.46 +}
    1.47 +template <class TargetUnits>
    1.48 +gfx::RectTyped<TargetUnits> ViewAs(const gfxRect& aRect) {
    1.49 +  return gfx::RectTyped<TargetUnits>(aRect.x, aRect.y, aRect.width, aRect.height);
    1.50 +}
    1.51 +template <class TargetUnits>
    1.52 +gfx::IntSizeTyped<TargetUnits> ViewAs(const nsIntSize& aSize) {
    1.53 +  return gfx::IntSizeTyped<TargetUnits>(aSize.width, aSize.height);
    1.54 +}
    1.55 +template <class TargetUnits>
    1.56 +gfx::IntPointTyped<TargetUnits> ViewAs(const nsIntPoint& aPoint) {
    1.57 +  return gfx::IntPointTyped<TargetUnits>(aPoint.x, aPoint.y);
    1.58 +}
    1.59 +template <class TargetUnits>
    1.60 +gfx::IntRectTyped<TargetUnits> ViewAs(const nsIntRect& aRect) {
    1.61 +  return gfx::IntRectTyped<TargetUnits>(aRect.x, aRect.y, aRect.width, aRect.height);
    1.62 +}
    1.63 +
    1.64 +// Convenience functions for casting typed entities to untyped entities.
    1.65 +// Using these functions does not require a justification, but once we convert
    1.66 +// all code to use strongly typed units they should not be needed any longer.
    1.67 +template <class SourceUnits>
    1.68 +gfxPoint ViewAsUntyped(const gfx::PointTyped<SourceUnits>& aPoint) {
    1.69 +  return gfxPoint(aPoint.x, aPoint.y);
    1.70 +}
    1.71 +template <class SourceUnits>
    1.72 +gfxRect ViewAsUntyped(const gfx::RectTyped<SourceUnits>& aRect) {
    1.73 +  return gfxRect(aRect.x, aRect.y, aRect.width, aRect.height);
    1.74 +}
    1.75 +
    1.76 +// Convenience functions for transforming an entity from one strongly-typed
    1.77 +// coordinate system to another using the provided transformation matrix.
    1.78 +template <typename TargetUnits, typename SourceUnits>
    1.79 +static gfx::PointTyped<TargetUnits> TransformTo(const gfx3DMatrix& aTransform,
    1.80 +                                                const gfx::PointTyped<SourceUnits>& aPoint)
    1.81 +{
    1.82 +  return ViewAs<TargetUnits>(aTransform.Transform(ViewAsUntyped(aPoint)));
    1.83 +}
    1.84 +template <typename TargetUnits, typename SourceUnits>
    1.85 +static gfx::RectTyped<TargetUnits> TransformTo(const gfx3DMatrix& aTransform,
    1.86 +                                               const gfx::RectTyped<SourceUnits>& aRect)
    1.87 +{
    1.88 +  return ViewAs<TargetUnits>(aTransform.TransformBounds(ViewAsUntyped(aRect)));
    1.89 +}
    1.90 +
    1.91 +
    1.92 +}
    1.93 +
    1.94 +#endif

mercurial