layout/base/Units.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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_UNITS_H_
michael@0 8 #define MOZ_UNITS_H_
michael@0 9
michael@0 10 #include "mozilla/gfx/Point.h"
michael@0 11 #include "mozilla/gfx/Rect.h"
michael@0 12 #include "mozilla/gfx/ScaleFactor.h"
michael@0 13 #include "nsRect.h"
michael@0 14 #include "nsMargin.h"
michael@0 15 #include "mozilla/AppUnits.h"
michael@0 16
michael@0 17 namespace mozilla {
michael@0 18
michael@0 19 struct CSSPixel;
michael@0 20 struct LayoutDevicePixel;
michael@0 21 struct LayerPixel;
michael@0 22 struct ScreenPixel;
michael@0 23
michael@0 24 typedef gfx::PointTyped<CSSPixel> CSSPoint;
michael@0 25 typedef gfx::IntPointTyped<CSSPixel> CSSIntPoint;
michael@0 26 typedef gfx::SizeTyped<CSSPixel> CSSSize;
michael@0 27 typedef gfx::IntSizeTyped<CSSPixel> CSSIntSize;
michael@0 28 typedef gfx::RectTyped<CSSPixel> CSSRect;
michael@0 29 typedef gfx::IntRectTyped<CSSPixel> CSSIntRect;
michael@0 30 typedef gfx::MarginTyped<CSSPixel> CSSMargin;
michael@0 31 typedef gfx::IntMarginTyped<CSSPixel> CSSIntMargin;
michael@0 32
michael@0 33 typedef gfx::PointTyped<LayoutDevicePixel> LayoutDevicePoint;
michael@0 34 typedef gfx::IntPointTyped<LayoutDevicePixel> LayoutDeviceIntPoint;
michael@0 35 typedef gfx::SizeTyped<LayoutDevicePixel> LayoutDeviceSize;
michael@0 36 typedef gfx::IntSizeTyped<LayoutDevicePixel> LayoutDeviceIntSize;
michael@0 37 typedef gfx::RectTyped<LayoutDevicePixel> LayoutDeviceRect;
michael@0 38 typedef gfx::IntRectTyped<LayoutDevicePixel> LayoutDeviceIntRect;
michael@0 39 typedef gfx::MarginTyped<LayoutDevicePixel> LayoutDeviceMargin;
michael@0 40 typedef gfx::IntMarginTyped<LayoutDevicePixel> LayoutDeviceIntMargin;
michael@0 41
michael@0 42 typedef gfx::PointTyped<LayerPixel> LayerPoint;
michael@0 43 typedef gfx::IntPointTyped<LayerPixel> LayerIntPoint;
michael@0 44 typedef gfx::SizeTyped<LayerPixel> LayerSize;
michael@0 45 typedef gfx::IntSizeTyped<LayerPixel> LayerIntSize;
michael@0 46 typedef gfx::RectTyped<LayerPixel> LayerRect;
michael@0 47 typedef gfx::IntRectTyped<LayerPixel> LayerIntRect;
michael@0 48 typedef gfx::MarginTyped<LayerPixel> LayerMargin;
michael@0 49 typedef gfx::IntMarginTyped<LayerPixel> LayerIntMargin;
michael@0 50
michael@0 51 typedef gfx::PointTyped<ScreenPixel> ScreenPoint;
michael@0 52 typedef gfx::IntPointTyped<ScreenPixel> ScreenIntPoint;
michael@0 53 typedef gfx::SizeTyped<ScreenPixel> ScreenSize;
michael@0 54 typedef gfx::IntSizeTyped<ScreenPixel> ScreenIntSize;
michael@0 55 typedef gfx::RectTyped<ScreenPixel> ScreenRect;
michael@0 56 typedef gfx::IntRectTyped<ScreenPixel> ScreenIntRect;
michael@0 57 typedef gfx::MarginTyped<ScreenPixel> ScreenMargin;
michael@0 58 typedef gfx::IntMarginTyped<ScreenPixel> ScreenIntMargin;
michael@0 59
michael@0 60 typedef gfx::ScaleFactor<CSSPixel, LayoutDevicePixel> CSSToLayoutDeviceScale;
michael@0 61 typedef gfx::ScaleFactor<LayoutDevicePixel, CSSPixel> LayoutDeviceToCSSScale;
michael@0 62 typedef gfx::ScaleFactor<CSSPixel, LayerPixel> CSSToLayerScale;
michael@0 63 typedef gfx::ScaleFactor<LayerPixel, CSSPixel> LayerToCSSScale;
michael@0 64 typedef gfx::ScaleFactor<CSSPixel, ScreenPixel> CSSToScreenScale;
michael@0 65 typedef gfx::ScaleFactor<ScreenPixel, CSSPixel> ScreenToCSSScale;
michael@0 66 typedef gfx::ScaleFactor<LayoutDevicePixel, LayerPixel> LayoutDeviceToLayerScale;
michael@0 67 typedef gfx::ScaleFactor<LayerPixel, LayoutDevicePixel> LayerToLayoutDeviceScale;
michael@0 68 typedef gfx::ScaleFactor<LayoutDevicePixel, ScreenPixel> LayoutDeviceToScreenScale;
michael@0 69 typedef gfx::ScaleFactor<ScreenPixel, LayoutDevicePixel> ScreenToLayoutDeviceScale;
michael@0 70 typedef gfx::ScaleFactor<LayerPixel, ScreenPixel> LayerToScreenScale;
michael@0 71 typedef gfx::ScaleFactor<ScreenPixel, LayerPixel> ScreenToLayerScale;
michael@0 72
michael@0 73 /*
michael@0 74 * The pixels that content authors use to specify sizes in.
michael@0 75 */
michael@0 76 struct CSSPixel {
michael@0 77
michael@0 78 // Conversions from app units
michael@0 79
michael@0 80 static CSSPoint FromAppUnits(const nsPoint& aPoint) {
michael@0 81 return CSSPoint(NSAppUnitsToFloatPixels(aPoint.x, float(AppUnitsPerCSSPixel())),
michael@0 82 NSAppUnitsToFloatPixels(aPoint.y, float(AppUnitsPerCSSPixel())));
michael@0 83 }
michael@0 84
michael@0 85 static CSSRect FromAppUnits(const nsRect& aRect) {
michael@0 86 return CSSRect(NSAppUnitsToFloatPixels(aRect.x, float(AppUnitsPerCSSPixel())),
michael@0 87 NSAppUnitsToFloatPixels(aRect.y, float(AppUnitsPerCSSPixel())),
michael@0 88 NSAppUnitsToFloatPixels(aRect.width, float(AppUnitsPerCSSPixel())),
michael@0 89 NSAppUnitsToFloatPixels(aRect.height, float(AppUnitsPerCSSPixel())));
michael@0 90 }
michael@0 91
michael@0 92 static CSSMargin FromAppUnits(const nsMargin& aMargin) {
michael@0 93 return CSSMargin(NSAppUnitsToFloatPixels(aMargin.top, float(AppUnitsPerCSSPixel())),
michael@0 94 NSAppUnitsToFloatPixels(aMargin.right, float(AppUnitsPerCSSPixel())),
michael@0 95 NSAppUnitsToFloatPixels(aMargin.bottom, float(AppUnitsPerCSSPixel())),
michael@0 96 NSAppUnitsToFloatPixels(aMargin.left, float(AppUnitsPerCSSPixel())));
michael@0 97 }
michael@0 98
michael@0 99 static CSSIntPoint FromAppUnitsRounded(const nsPoint& aPoint) {
michael@0 100 return CSSIntPoint(NSAppUnitsToIntPixels(aPoint.x, float(AppUnitsPerCSSPixel())),
michael@0 101 NSAppUnitsToIntPixels(aPoint.y, float(AppUnitsPerCSSPixel())));
michael@0 102 }
michael@0 103
michael@0 104 static CSSIntSize FromAppUnitsRounded(const nsSize& aSize)
michael@0 105 {
michael@0 106 return CSSIntSize(NSAppUnitsToIntPixels(aSize.width, float(AppUnitsPerCSSPixel())),
michael@0 107 NSAppUnitsToIntPixels(aSize.height, float(AppUnitsPerCSSPixel())));
michael@0 108 }
michael@0 109
michael@0 110 static CSSIntRect FromAppUnitsRounded(const nsRect& aRect) {
michael@0 111 return CSSIntRect(NSAppUnitsToIntPixels(aRect.x, float(AppUnitsPerCSSPixel())),
michael@0 112 NSAppUnitsToIntPixels(aRect.y, float(AppUnitsPerCSSPixel())),
michael@0 113 NSAppUnitsToIntPixels(aRect.width, float(AppUnitsPerCSSPixel())),
michael@0 114 NSAppUnitsToIntPixels(aRect.height, float(AppUnitsPerCSSPixel())));
michael@0 115 }
michael@0 116
michael@0 117 // Conversions to app units
michael@0 118
michael@0 119 static nsPoint ToAppUnits(const CSSPoint& aPoint) {
michael@0 120 return nsPoint(NSToCoordRoundWithClamp(aPoint.x * float(AppUnitsPerCSSPixel())),
michael@0 121 NSToCoordRoundWithClamp(aPoint.y * float(AppUnitsPerCSSPixel())));
michael@0 122 }
michael@0 123
michael@0 124 static nsPoint ToAppUnits(const CSSIntPoint& aPoint) {
michael@0 125 return nsPoint(NSToCoordRoundWithClamp(float(aPoint.x) * float(AppUnitsPerCSSPixel())),
michael@0 126 NSToCoordRoundWithClamp(float(aPoint.y) * float(AppUnitsPerCSSPixel())));
michael@0 127 }
michael@0 128
michael@0 129 static nsRect ToAppUnits(const CSSRect& aRect) {
michael@0 130 return nsRect(NSToCoordRoundWithClamp(aRect.x * float(AppUnitsPerCSSPixel())),
michael@0 131 NSToCoordRoundWithClamp(aRect.y * float(AppUnitsPerCSSPixel())),
michael@0 132 NSToCoordRoundWithClamp(aRect.width * float(AppUnitsPerCSSPixel())),
michael@0 133 NSToCoordRoundWithClamp(aRect.height * float(AppUnitsPerCSSPixel())));
michael@0 134 }
michael@0 135 };
michael@0 136
michael@0 137 /*
michael@0 138 * The pixels that are referred to as "device pixels" in layout code. In
michael@0 139 * general this is obtained by converting a value in app units value by the
michael@0 140 * AppUnitsPerDevPixel() value. The size of these pixels
michael@0 141 * are affected by:
michael@0 142 * 1) the "full zoom" (see nsPresContext::SetFullZoom)
michael@0 143 * 2) the "widget scale" (see nsIWidget::GetDefaultScale)
michael@0 144 */
michael@0 145 struct LayoutDevicePixel {
michael@0 146 static LayoutDeviceIntPoint FromUntyped(const nsIntPoint& aPoint) {
michael@0 147 return LayoutDeviceIntPoint(aPoint.x, aPoint.y);
michael@0 148 }
michael@0 149
michael@0 150 static nsIntPoint ToUntyped(const LayoutDeviceIntPoint& aPoint) {
michael@0 151 return nsIntPoint(aPoint.x, aPoint.y);
michael@0 152 }
michael@0 153
michael@0 154 static LayoutDeviceIntRect FromUntyped(const nsIntRect& aRect) {
michael@0 155 return LayoutDeviceIntRect(aRect.x, aRect.y, aRect.width, aRect.height);
michael@0 156 }
michael@0 157
michael@0 158 static nsIntRect ToUntyped(const LayoutDeviceIntRect& aRect) {
michael@0 159 return nsIntRect(aRect.x, aRect.y, aRect.width, aRect.height);
michael@0 160 }
michael@0 161
michael@0 162 static LayoutDeviceRect FromAppUnits(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
michael@0 163 return LayoutDeviceRect(NSAppUnitsToFloatPixels(aRect.x, float(aAppUnitsPerDevPixel)),
michael@0 164 NSAppUnitsToFloatPixels(aRect.y, float(aAppUnitsPerDevPixel)),
michael@0 165 NSAppUnitsToFloatPixels(aRect.width, float(aAppUnitsPerDevPixel)),
michael@0 166 NSAppUnitsToFloatPixels(aRect.height, float(aAppUnitsPerDevPixel)));
michael@0 167 }
michael@0 168
michael@0 169 static LayoutDeviceIntPoint FromAppUnitsRounded(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
michael@0 170 return LayoutDeviceIntPoint(NSAppUnitsToIntPixels(aPoint.x, aAppUnitsPerDevPixel),
michael@0 171 NSAppUnitsToIntPixels(aPoint.y, aAppUnitsPerDevPixel));
michael@0 172 }
michael@0 173
michael@0 174 static LayoutDeviceIntPoint FromAppUnitsToNearest(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) {
michael@0 175 return FromUntyped(aPoint.ToNearestPixels(aAppUnitsPerDevPixel));
michael@0 176 }
michael@0 177
michael@0 178 static LayoutDeviceIntRect FromAppUnitsToNearest(const nsRect& aRect, nscoord aAppUnitsPerDevPixel) {
michael@0 179 return FromUntyped(aRect.ToNearestPixels(aAppUnitsPerDevPixel));
michael@0 180 }
michael@0 181 };
michael@0 182
michael@0 183 /*
michael@0 184 * The pixels that layout rasterizes and delivers to the graphics code.
michael@0 185 * These are generally referred to as "device pixels" in layout code. Layer
michael@0 186 * pixels are affected by:
michael@0 187 * 1) the "display resolution" (see nsIPresShell::SetResolution)
michael@0 188 * 2) the "full zoom" (see nsPresContext::SetFullZoom)
michael@0 189 * 3) the "widget scale" (see nsIWidget::GetDefaultScale)
michael@0 190 */
michael@0 191 struct LayerPixel {
michael@0 192 };
michael@0 193
michael@0 194 /*
michael@0 195 * The pixels that are displayed on the screen.
michael@0 196 * On non-OMTC platforms this should be equivalent to LayerPixel units.
michael@0 197 * On OMTC platforms these may diverge from LayerPixel units temporarily,
michael@0 198 * while an asynchronous zoom is happening, but should eventually converge
michael@0 199 * back to LayerPixel units. Some variables (such as those representing
michael@0 200 * chrome UI element sizes) that are not subject to content zoom should
michael@0 201 * generally be represented in ScreenPixel units.
michael@0 202 */
michael@0 203 struct ScreenPixel {
michael@0 204 };
michael@0 205
michael@0 206 // Operators to apply ScaleFactors directly to Points, Rects, Sizes and Margins
michael@0 207
michael@0 208 template<class src, class dst>
michael@0 209 gfx::PointTyped<dst> operator*(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactor<src, dst>& aScale) {
michael@0 210 return gfx::PointTyped<dst>(aPoint.x * aScale.scale,
michael@0 211 aPoint.y * aScale.scale);
michael@0 212 }
michael@0 213
michael@0 214 template<class src, class dst>
michael@0 215 gfx::PointTyped<dst> operator/(const gfx::PointTyped<src>& aPoint, const gfx::ScaleFactor<dst, src>& aScale) {
michael@0 216 return gfx::PointTyped<dst>(aPoint.x / aScale.scale,
michael@0 217 aPoint.y / aScale.scale);
michael@0 218 }
michael@0 219
michael@0 220 template<class src, class dst>
michael@0 221 gfx::RectTyped<dst> operator*(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactor<src, dst>& aScale) {
michael@0 222 return gfx::RectTyped<dst>(aRect.x * aScale.scale,
michael@0 223 aRect.y * aScale.scale,
michael@0 224 aRect.width * aScale.scale,
michael@0 225 aRect.height * aScale.scale);
michael@0 226 }
michael@0 227
michael@0 228 template<class src, class dst>
michael@0 229 gfx::RectTyped<dst> operator/(const gfx::RectTyped<src>& aRect, const gfx::ScaleFactor<dst, src>& aScale) {
michael@0 230 return gfx::RectTyped<dst>(aRect.x / aScale.scale,
michael@0 231 aRect.y / aScale.scale,
michael@0 232 aRect.width / aScale.scale,
michael@0 233 aRect.height / aScale.scale);
michael@0 234 }
michael@0 235
michael@0 236 template<class src, class dst>
michael@0 237 gfx::RectTyped<dst> operator*(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactor<src, dst>& aScale) {
michael@0 238 return gfx::RectTyped<dst>(float(aRect.x) * aScale.scale,
michael@0 239 float(aRect.y) * aScale.scale,
michael@0 240 float(aRect.width) * aScale.scale,
michael@0 241 float(aRect.height) * aScale.scale);
michael@0 242 }
michael@0 243
michael@0 244 template<class src, class dst>
michael@0 245 gfx::RectTyped<dst> operator/(const gfx::IntRectTyped<src>& aRect, const gfx::ScaleFactor<dst, src>& aScale) {
michael@0 246 return gfx::RectTyped<dst>(float(aRect.x) / aScale.scale,
michael@0 247 float(aRect.y) / aScale.scale,
michael@0 248 float(aRect.width) / aScale.scale,
michael@0 249 float(aRect.height) / aScale.scale);
michael@0 250 }
michael@0 251
michael@0 252 template<class src, class dst>
michael@0 253 gfx::SizeTyped<dst> operator*(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
michael@0 254 return gfx::SizeTyped<dst>(aSize.width * aScale.scale,
michael@0 255 aSize.height * aScale.scale);
michael@0 256 }
michael@0 257
michael@0 258 template<class src, class dst>
michael@0 259 gfx::SizeTyped<dst> operator/(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
michael@0 260 return gfx::SizeTyped<dst>(aSize.width / aScale.scale,
michael@0 261 aSize.height / aScale.scale);
michael@0 262 }
michael@0 263
michael@0 264 template<class src, class dst>
michael@0 265 gfx::SizeTyped<dst> operator*(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
michael@0 266 return gfx::SizeTyped<dst>(float(aSize.width) * aScale.scale,
michael@0 267 float(aSize.height) * aScale.scale);
michael@0 268 }
michael@0 269
michael@0 270 template<class src, class dst>
michael@0 271 gfx::SizeTyped<dst> operator/(const gfx::IntSizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
michael@0 272 return gfx::SizeTyped<dst>(float(aSize.width) / aScale.scale,
michael@0 273 float(aSize.height) / aScale.scale);
michael@0 274 }
michael@0 275
michael@0 276 template<class src, class dst>
michael@0 277 gfx::MarginTyped<dst> operator*(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactor<src, dst>& aScale) {
michael@0 278 return gfx::MarginTyped<dst>(aMargin.top * aScale.scale,
michael@0 279 aMargin.right * aScale.scale,
michael@0 280 aMargin.bottom * aScale.scale,
michael@0 281 aMargin.left * aScale.scale);
michael@0 282 }
michael@0 283
michael@0 284 template<class src, class dst>
michael@0 285 gfx::MarginTyped<dst> operator/(const gfx::MarginTyped<src>& aMargin, const gfx::ScaleFactor<dst, src>& aScale) {
michael@0 286 return gfx::MarginTyped<dst>(aMargin.top / aScale.scale,
michael@0 287 aMargin.right / aScale.scale,
michael@0 288 aMargin.bottom / aScale.scale,
michael@0 289 aMargin.left / aScale.scale);
michael@0 290 }
michael@0 291
michael@0 292 }
michael@0 293
michael@0 294 #endif

mercurial