layout/base/Units.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/base/Units.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,294 @@
     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_UNITS_H_
    1.11 +#define MOZ_UNITS_H_
    1.12 +
    1.13 +#include "mozilla/gfx/Point.h"
    1.14 +#include "mozilla/gfx/Rect.h"
    1.15 +#include "mozilla/gfx/ScaleFactor.h"
    1.16 +#include "nsRect.h"
    1.17 +#include "nsMargin.h"
    1.18 +#include "mozilla/AppUnits.h"
    1.19 +
    1.20 +namespace mozilla {
    1.21 +
    1.22 +struct CSSPixel;
    1.23 +struct LayoutDevicePixel;
    1.24 +struct LayerPixel;
    1.25 +struct ScreenPixel;
    1.26 +
    1.27 +typedef gfx::PointTyped<CSSPixel> CSSPoint;
    1.28 +typedef gfx::IntPointTyped<CSSPixel> CSSIntPoint;
    1.29 +typedef gfx::SizeTyped<CSSPixel> CSSSize;
    1.30 +typedef gfx::IntSizeTyped<CSSPixel> CSSIntSize;
    1.31 +typedef gfx::RectTyped<CSSPixel> CSSRect;
    1.32 +typedef gfx::IntRectTyped<CSSPixel> CSSIntRect;
    1.33 +typedef gfx::MarginTyped<CSSPixel> CSSMargin;
    1.34 +typedef gfx::IntMarginTyped<CSSPixel> CSSIntMargin;
    1.35 +
    1.36 +typedef gfx::PointTyped<LayoutDevicePixel> LayoutDevicePoint;
    1.37 +typedef gfx::IntPointTyped<LayoutDevicePixel> LayoutDeviceIntPoint;
    1.38 +typedef gfx::SizeTyped<LayoutDevicePixel> LayoutDeviceSize;
    1.39 +typedef gfx::IntSizeTyped<LayoutDevicePixel> LayoutDeviceIntSize;
    1.40 +typedef gfx::RectTyped<LayoutDevicePixel> LayoutDeviceRect;
    1.41 +typedef gfx::IntRectTyped<LayoutDevicePixel> LayoutDeviceIntRect;
    1.42 +typedef gfx::MarginTyped<LayoutDevicePixel> LayoutDeviceMargin;
    1.43 +typedef gfx::IntMarginTyped<LayoutDevicePixel> LayoutDeviceIntMargin;
    1.44 +
    1.45 +typedef gfx::PointTyped<LayerPixel> LayerPoint;
    1.46 +typedef gfx::IntPointTyped<LayerPixel> LayerIntPoint;
    1.47 +typedef gfx::SizeTyped<LayerPixel> LayerSize;
    1.48 +typedef gfx::IntSizeTyped<LayerPixel> LayerIntSize;
    1.49 +typedef gfx::RectTyped<LayerPixel> LayerRect;
    1.50 +typedef gfx::IntRectTyped<LayerPixel> LayerIntRect;
    1.51 +typedef gfx::MarginTyped<LayerPixel> LayerMargin;
    1.52 +typedef gfx::IntMarginTyped<LayerPixel> LayerIntMargin;
    1.53 +
    1.54 +typedef gfx::PointTyped<ScreenPixel> ScreenPoint;
    1.55 +typedef gfx::IntPointTyped<ScreenPixel> ScreenIntPoint;
    1.56 +typedef gfx::SizeTyped<ScreenPixel> ScreenSize;
    1.57 +typedef gfx::IntSizeTyped<ScreenPixel> ScreenIntSize;
    1.58 +typedef gfx::RectTyped<ScreenPixel> ScreenRect;
    1.59 +typedef gfx::IntRectTyped<ScreenPixel> ScreenIntRect;
    1.60 +typedef gfx::MarginTyped<ScreenPixel> ScreenMargin;
    1.61 +typedef gfx::IntMarginTyped<ScreenPixel> ScreenIntMargin;
    1.62 +
    1.63 +typedef gfx::ScaleFactor<CSSPixel, LayoutDevicePixel> CSSToLayoutDeviceScale;
    1.64 +typedef gfx::ScaleFactor<LayoutDevicePixel, CSSPixel> LayoutDeviceToCSSScale;
    1.65 +typedef gfx::ScaleFactor<CSSPixel, LayerPixel> CSSToLayerScale;
    1.66 +typedef gfx::ScaleFactor<LayerPixel, CSSPixel> LayerToCSSScale;
    1.67 +typedef gfx::ScaleFactor<CSSPixel, ScreenPixel> CSSToScreenScale;
    1.68 +typedef gfx::ScaleFactor<ScreenPixel, CSSPixel> ScreenToCSSScale;
    1.69 +typedef gfx::ScaleFactor<LayoutDevicePixel, LayerPixel> LayoutDeviceToLayerScale;
    1.70 +typedef gfx::ScaleFactor<LayerPixel, LayoutDevicePixel> LayerToLayoutDeviceScale;
    1.71 +typedef gfx::ScaleFactor<LayoutDevicePixel, ScreenPixel> LayoutDeviceToScreenScale;
    1.72 +typedef gfx::ScaleFactor<ScreenPixel, LayoutDevicePixel> ScreenToLayoutDeviceScale;
    1.73 +typedef gfx::ScaleFactor<LayerPixel, ScreenPixel> LayerToScreenScale;
    1.74 +typedef gfx::ScaleFactor<ScreenPixel, LayerPixel> ScreenToLayerScale;
    1.75 +
    1.76 +/*
    1.77 + * The pixels that content authors use to specify sizes in.
    1.78 + */
    1.79 +struct CSSPixel {
    1.80 +
    1.81 +  // Conversions from app units
    1.82 +
    1.83 +  static CSSPoint FromAppUnits(const nsPoint& aPoint) {
    1.84 +    return CSSPoint(NSAppUnitsToFloatPixels(aPoint.x, float(AppUnitsPerCSSPixel())),
    1.85 +                    NSAppUnitsToFloatPixels(aPoint.y, float(AppUnitsPerCSSPixel())));
    1.86 +  }
    1.87 +
    1.88 +  static CSSRect FromAppUnits(const nsRect& aRect) {
    1.89 +    return CSSRect(NSAppUnitsToFloatPixels(aRect.x, float(AppUnitsPerCSSPixel())),
    1.90 +                   NSAppUnitsToFloatPixels(aRect.y, float(AppUnitsPerCSSPixel())),
    1.91 +                   NSAppUnitsToFloatPixels(aRect.width, float(AppUnitsPerCSSPixel())),
    1.92 +                   NSAppUnitsToFloatPixels(aRect.height, float(AppUnitsPerCSSPixel())));
    1.93 +  }
    1.94 +
    1.95 +  static CSSMargin FromAppUnits(const nsMargin& aMargin) {
    1.96 +    return CSSMargin(NSAppUnitsToFloatPixels(aMargin.top, float(AppUnitsPerCSSPixel())),
    1.97 +                     NSAppUnitsToFloatPixels(aMargin.right, float(AppUnitsPerCSSPixel())),
    1.98 +                     NSAppUnitsToFloatPixels(aMargin.bottom, float(AppUnitsPerCSSPixel())),
    1.99 +                     NSAppUnitsToFloatPixels(aMargin.left, float(AppUnitsPerCSSPixel())));
   1.100 +  }
   1.101 +
   1.102 +  static CSSIntPoint FromAppUnitsRounded(const nsPoint& aPoint) {
   1.103 +    return CSSIntPoint(NSAppUnitsToIntPixels(aPoint.x, float(AppUnitsPerCSSPixel())),
   1.104 +                       NSAppUnitsToIntPixels(aPoint.y, float(AppUnitsPerCSSPixel())));
   1.105 +  }
   1.106 +
   1.107 +  static CSSIntSize FromAppUnitsRounded(const nsSize& aSize)
   1.108 +  {
   1.109 +    return CSSIntSize(NSAppUnitsToIntPixels(aSize.width, float(AppUnitsPerCSSPixel())),
   1.110 +                      NSAppUnitsToIntPixels(aSize.height, float(AppUnitsPerCSSPixel())));
   1.111 +  }
   1.112 +
   1.113 +  static CSSIntRect FromAppUnitsRounded(const nsRect& aRect) {
   1.114 +    return CSSIntRect(NSAppUnitsToIntPixels(aRect.x, float(AppUnitsPerCSSPixel())),
   1.115 +                      NSAppUnitsToIntPixels(aRect.y, float(AppUnitsPerCSSPixel())),
   1.116 +                      NSAppUnitsToIntPixels(aRect.width, float(AppUnitsPerCSSPixel())),
   1.117 +                      NSAppUnitsToIntPixels(aRect.height, float(AppUnitsPerCSSPixel())));
   1.118 +  }
   1.119 +
   1.120 +  // Conversions to app units
   1.121 +
   1.122 +  static nsPoint ToAppUnits(const CSSPoint& aPoint) {
   1.123 +    return nsPoint(NSToCoordRoundWithClamp(aPoint.x * float(AppUnitsPerCSSPixel())),
   1.124 +                   NSToCoordRoundWithClamp(aPoint.y * float(AppUnitsPerCSSPixel())));
   1.125 +  }
   1.126 +
   1.127 +  static nsPoint ToAppUnits(const CSSIntPoint& aPoint) {
   1.128 +    return nsPoint(NSToCoordRoundWithClamp(float(aPoint.x) * float(AppUnitsPerCSSPixel())),
   1.129 +                   NSToCoordRoundWithClamp(float(aPoint.y) * float(AppUnitsPerCSSPixel())));
   1.130 +  }
   1.131 +
   1.132 +  static nsRect ToAppUnits(const CSSRect& aRect) {
   1.133 +    return nsRect(NSToCoordRoundWithClamp(aRect.x * float(AppUnitsPerCSSPixel())),
   1.134 +                  NSToCoordRoundWithClamp(aRect.y * float(AppUnitsPerCSSPixel())),
   1.135 +                  NSToCoordRoundWithClamp(aRect.width * float(AppUnitsPerCSSPixel())),
   1.136 +                  NSToCoordRoundWithClamp(aRect.height * float(AppUnitsPerCSSPixel())));
   1.137 +  }
   1.138 +};
   1.139 +
   1.140 +/*
   1.141 + * The pixels that are referred to as "device pixels" in layout code. In
   1.142 + * general this is obtained by converting a value in app units value by the
   1.143 + * AppUnitsPerDevPixel() value. The size of these pixels
   1.144 + * are affected by:
   1.145 + * 1) the "full zoom" (see nsPresContext::SetFullZoom)
   1.146 + * 2) the "widget scale" (see nsIWidget::GetDefaultScale)
   1.147 + */
   1.148 +struct LayoutDevicePixel {
   1.149 +  static LayoutDeviceIntPoint FromUntyped(const nsIntPoint& aPoint) {
   1.150 +    return LayoutDeviceIntPoint(aPoint.x, aPoint.y);
   1.151 +  }
   1.152 +
   1.153 +  static nsIntPoint ToUntyped(const LayoutDeviceIntPoint& aPoint) {
   1.154 +    return nsIntPoint(aPoint.x, aPoint.y);
   1.155 +  }
   1.156 +
   1.157 +  static LayoutDeviceIntRect FromUntyped(const nsIntRect& aRect) {
   1.158 +    return LayoutDeviceIntRect(aRect.x, aRect.y, aRect.width, aRect.height);
   1.159 +  }
   1.160 +
   1.161 +  static nsIntRect ToUntyped(const LayoutDeviceIntRect& aRect) {
   1.162 +    return nsIntRect(aRect.x, aRect.y, aRect.width, aRect.height);
   1.163 +  }
   1.164 +
   1.165 +  static LayoutDeviceRect FromAppUnits(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
   1.166 +    return LayoutDeviceRect(NSAppUnitsToFloatPixels(aRect.x, float(aAppUnitsPerDevPixel)),
   1.167 +                            NSAppUnitsToFloatPixels(aRect.y, float(aAppUnitsPerDevPixel)),
   1.168 +                            NSAppUnitsToFloatPixels(aRect.width, float(aAppUnitsPerDevPixel)),
   1.169 +                            NSAppUnitsToFloatPixels(aRect.height, float(aAppUnitsPerDevPixel)));
   1.170 +  }
   1.171 +
   1.172 +  static LayoutDeviceIntPoint FromAppUnitsRounded(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
   1.173 +    return LayoutDeviceIntPoint(NSAppUnitsToIntPixels(aPoint.x, aAppUnitsPerDevPixel),
   1.174 +                                NSAppUnitsToIntPixels(aPoint.y, aAppUnitsPerDevPixel));
   1.175 +  }
   1.176 +
   1.177 +  static LayoutDeviceIntPoint FromAppUnitsToNearest(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
   1.178 +    return FromUntyped(aPoint.ToNearestPixels(aAppUnitsPerDevPixel));
   1.179 +  }
   1.180 +
   1.181 +  static LayoutDeviceIntRect FromAppUnitsToNearest(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
   1.182 +    return FromUntyped(aRect.ToNearestPixels(aAppUnitsPerDevPixel));
   1.183 +  }
   1.184 +};
   1.185 +
   1.186 +/*
   1.187 + * The pixels that layout rasterizes and delivers to the graphics code.
   1.188 + * These are generally referred to as "device pixels" in layout code. Layer
   1.189 + * pixels are affected by:
   1.190 + * 1) the "display resolution" (see nsIPresShell::SetResolution)
   1.191 + * 2) the "full zoom" (see nsPresContext::SetFullZoom)
   1.192 + * 3) the "widget scale" (see nsIWidget::GetDefaultScale)
   1.193 + */
   1.194 +struct LayerPixel {
   1.195 +};
   1.196 +
   1.197 +/*
   1.198 + * The pixels that are displayed on the screen.
   1.199 + * On non-OMTC platforms this should be equivalent to LayerPixel units.
   1.200 + * On OMTC platforms these may diverge from LayerPixel units temporarily,
   1.201 + * while an asynchronous zoom is happening, but should eventually converge
   1.202 + * back to LayerPixel units. Some variables (such as those representing
   1.203 + * chrome UI element sizes) that are not subject to content zoom should
   1.204 + * generally be represented in ScreenPixel units.
   1.205 + */
   1.206 +struct ScreenPixel {
   1.207 +};
   1.208 +
   1.209 +// Operators to apply ScaleFactors directly to Points, Rects, Sizes and Margins
   1.210 +
   1.211 +template<class src, class dst>
   1.212 +gfx::PointTyped<dst> operator*(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactor<src, dst>& aScale) {
   1.213 +  return gfx::PointTyped<dst>(aPoint.x * aScale.scale,
   1.214 +                              aPoint.y * aScale.scale);
   1.215 +}
   1.216 +
   1.217 +template<class src, class dst>
   1.218 +gfx::PointTyped<dst> operator/(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactor<dst, src>& aScale) {
   1.219 +  return gfx::PointTyped<dst>(aPoint.x / aScale.scale,
   1.220 +                              aPoint.y / aScale.scale);
   1.221 +}
   1.222 +
   1.223 +template<class src, class dst>
   1.224 +gfx::RectTyped<dst> operator*(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactor<src, dst>& aScale) {
   1.225 +  return gfx::RectTyped<dst>(aRect.x * aScale.scale,
   1.226 +                             aRect.y * aScale.scale,
   1.227 +                             aRect.width * aScale.scale,
   1.228 +                             aRect.height * aScale.scale);
   1.229 +}
   1.230 +
   1.231 +template<class src, class dst>
   1.232 +gfx::RectTyped<dst> operator/(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactor<dst, src>& aScale) {
   1.233 +  return gfx::RectTyped<dst>(aRect.x / aScale.scale,
   1.234 +                             aRect.y / aScale.scale,
   1.235 +                             aRect.width / aScale.scale,
   1.236 +                             aRect.height / aScale.scale);
   1.237 +}
   1.238 +
   1.239 +template<class src, class dst>
   1.240 +gfx::RectTyped<dst> operator*(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactor<src, dst>& aScale) {
   1.241 +  return gfx::RectTyped<dst>(float(aRect.x) * aScale.scale,
   1.242 +                             float(aRect.y) * aScale.scale,
   1.243 +                             float(aRect.width) * aScale.scale,
   1.244 +                             float(aRect.height) * aScale.scale);
   1.245 +}
   1.246 +
   1.247 +template<class src, class dst>
   1.248 +gfx::RectTyped<dst> operator/(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactor<dst, src>& aScale) {
   1.249 +  return gfx::RectTyped<dst>(float(aRect.x) / aScale.scale,
   1.250 +                             float(aRect.y) / aScale.scale,
   1.251 +                             float(aRect.width) / aScale.scale,
   1.252 +                             float(aRect.height) / aScale.scale);
   1.253 +}
   1.254 +
   1.255 +template<class src, class dst>
   1.256 +gfx::SizeTyped<dst> operator*(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
   1.257 +  return gfx::SizeTyped<dst>(aSize.width * aScale.scale,
   1.258 +                             aSize.height * aScale.scale);
   1.259 +}
   1.260 +
   1.261 +template<class src, class dst>
   1.262 +gfx::SizeTyped<dst> operator/(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
   1.263 +  return gfx::SizeTyped<dst>(aSize.width / aScale.scale,
   1.264 +                              aSize.height / aScale.scale);
   1.265 +}
   1.266 +
   1.267 +template<class src, class dst>
   1.268 +gfx::SizeTyped<dst> operator*(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
   1.269 +  return gfx::SizeTyped<dst>(float(aSize.width) * aScale.scale,
   1.270 +                             float(aSize.height) * aScale.scale);
   1.271 +}
   1.272 +
   1.273 +template<class src, class dst>
   1.274 +gfx::SizeTyped<dst> operator/(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
   1.275 +  return gfx::SizeTyped<dst>(float(aSize.width) / aScale.scale,
   1.276 +                             float(aSize.height) / aScale.scale);
   1.277 +}
   1.278 +
   1.279 +template<class src, class dst>
   1.280 +gfx::MarginTyped<dst> operator*(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactor<src, dst>& aScale) {
   1.281 +  return gfx::MarginTyped<dst>(aMargin.top * aScale.scale,
   1.282 +                               aMargin.right * aScale.scale,
   1.283 +                               aMargin.bottom * aScale.scale,
   1.284 +                               aMargin.left * aScale.scale);
   1.285 +}
   1.286 +
   1.287 +template<class src, class dst>
   1.288 +gfx::MarginTyped<dst> operator/(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactor<dst, src>& aScale) {
   1.289 +  return gfx::MarginTyped<dst>(aMargin.top / aScale.scale,
   1.290 +                               aMargin.right / aScale.scale,
   1.291 +                               aMargin.bottom / aScale.scale,
   1.292 +                               aMargin.left / aScale.scale);
   1.293 +}
   1.294 +
   1.295 +}
   1.296 +
   1.297 +#endif

mercurial