2013-06-12 11:00:08 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-05-31 05:30:13 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MOZ_UNITS_H_
|
|
|
|
#define MOZ_UNITS_H_
|
|
|
|
|
|
|
|
#include "mozilla/gfx/Point.h"
|
2013-06-15 00:11:30 +04:00
|
|
|
#include "mozilla/gfx/Rect.h"
|
2013-05-31 05:30:13 +04:00
|
|
|
#include "nsDeviceContext.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
struct CSSPixel;
|
|
|
|
struct LayerPixel;
|
|
|
|
struct ScreenPixel;
|
|
|
|
|
|
|
|
typedef gfx::PointTyped<CSSPixel> CSSPoint;
|
|
|
|
typedef gfx::IntPointTyped<CSSPixel> CSSIntPoint;
|
|
|
|
typedef gfx::SizeTyped<CSSPixel> CSSSize;
|
|
|
|
typedef gfx::IntSizeTyped<CSSPixel> CSSIntSize;
|
|
|
|
typedef gfx::RectTyped<CSSPixel> CSSRect;
|
|
|
|
typedef gfx::IntRectTyped<CSSPixel> CSSIntRect;
|
|
|
|
|
|
|
|
typedef gfx::PointTyped<LayerPixel> LayerPoint;
|
|
|
|
typedef gfx::IntPointTyped<LayerPixel> LayerIntPoint;
|
|
|
|
typedef gfx::SizeTyped<LayerPixel> LayerSize;
|
|
|
|
typedef gfx::IntSizeTyped<LayerPixel> LayerIntSize;
|
|
|
|
typedef gfx::RectTyped<LayerPixel> LayerRect;
|
|
|
|
typedef gfx::IntRectTyped<LayerPixel> LayerIntRect;
|
|
|
|
|
|
|
|
typedef gfx::PointTyped<ScreenPixel> ScreenPoint;
|
|
|
|
typedef gfx::IntPointTyped<ScreenPixel> ScreenIntPoint;
|
|
|
|
typedef gfx::SizeTyped<ScreenPixel> ScreenSize;
|
|
|
|
typedef gfx::IntSizeTyped<ScreenPixel> ScreenIntSize;
|
|
|
|
typedef gfx::RectTyped<ScreenPixel> ScreenRect;
|
|
|
|
typedef gfx::IntRectTyped<ScreenPixel> ScreenIntRect;
|
|
|
|
|
2013-06-10 17:05:43 +04:00
|
|
|
/*
|
|
|
|
* The pixels that content authors use to specify sizes in.
|
|
|
|
*/
|
2013-05-31 05:30:13 +04:00
|
|
|
struct CSSPixel {
|
2013-06-15 00:11:30 +04:00
|
|
|
|
|
|
|
// Conversions from app units
|
|
|
|
|
|
|
|
static CSSPoint FromAppUnits(const nsPoint& aPoint) {
|
|
|
|
return CSSPoint(NSAppUnitsToFloatPixels(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSAppUnitsToFloatPixels(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel())));
|
|
|
|
}
|
|
|
|
|
|
|
|
static CSSRect FromAppUnits(const nsRect& aRect) {
|
|
|
|
return CSSRect(NSAppUnitsToFloatPixels(aRect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSAppUnitsToFloatPixels(aRect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSAppUnitsToFloatPixels(aRect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSAppUnitsToFloatPixels(aRect.height, float(nsDeviceContext::AppUnitsPerCSSPixel())));
|
2013-05-31 05:30:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
static CSSIntPoint FromAppUnitsRounded(const nsPoint& aPoint) {
|
|
|
|
return CSSIntPoint(NSAppUnitsToIntPixels(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSAppUnitsToIntPixels(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel())));
|
2013-06-12 11:00:08 +04:00
|
|
|
}
|
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
static CSSIntRect FromAppUnitsRounded(const nsRect& aRect) {
|
|
|
|
return CSSIntRect(NSAppUnitsToIntPixels(aRect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSAppUnitsToIntPixels(aRect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSAppUnitsToIntPixels(aRect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSAppUnitsToIntPixels(aRect.height, float(nsDeviceContext::AppUnitsPerCSSPixel())));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conversions to app units
|
|
|
|
|
|
|
|
static nsPoint ToAppUnits(const CSSPoint& aPoint) {
|
2013-06-10 17:05:43 +04:00
|
|
|
return nsPoint(NSFloatPixelsToAppUnits(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSFloatPixelsToAppUnits(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel())));
|
2013-05-31 05:30:13 +04:00
|
|
|
}
|
2013-06-03 17:52:44 +04:00
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
static nsPoint ToAppUnits(const CSSIntPoint& aPoint) {
|
2013-06-12 11:00:08 +04:00
|
|
|
return nsPoint(NSIntPixelsToAppUnits(aPoint.x, nsDeviceContext::AppUnitsPerCSSPixel()),
|
|
|
|
NSIntPixelsToAppUnits(aPoint.y, nsDeviceContext::AppUnitsPerCSSPixel()));
|
|
|
|
}
|
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
static nsRect ToAppUnits(const CSSRect& aRect) {
|
2013-06-10 17:05:43 +04:00
|
|
|
return nsRect(NSFloatPixelsToAppUnits(aRect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSFloatPixelsToAppUnits(aRect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSFloatPixelsToAppUnits(aRect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())),
|
|
|
|
NSFloatPixelsToAppUnits(aRect.height, float(nsDeviceContext::AppUnitsPerCSSPixel())));
|
|
|
|
}
|
2013-05-31 05:30:13 +04:00
|
|
|
};
|
|
|
|
|
2013-06-10 17:05:43 +04:00
|
|
|
/*
|
|
|
|
* The pixels that layout rasterizes and delivers to the graphics code.
|
|
|
|
* These are generally referred to as "device pixels" in layout code. Layer
|
|
|
|
* pixels are affected by:
|
|
|
|
* 1) the "display resolution" (see nsIPresShell::SetResolution)
|
|
|
|
* 2) the "full zoom" (see nsPresContext::SetFullZoom)
|
|
|
|
* 3) the "widget scale" (nsIWidget::GetDefaultScale)
|
|
|
|
*/
|
2013-06-03 17:52:44 +04:00
|
|
|
struct LayerPixel {
|
2013-06-15 00:11:30 +04:00
|
|
|
|
|
|
|
// Conversions from CSS units
|
|
|
|
|
|
|
|
static LayerPoint FromCSSPoint(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
|
|
|
|
return LayerPoint(aPoint.x * aResolutionX,
|
|
|
|
aPoint.y * aResolutionY);
|
2013-06-03 17:58:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
static LayerIntPoint FromCSSPointRounded(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
|
|
|
|
return LayerIntPoint(NS_lround(aPoint.x * aResolutionX),
|
|
|
|
NS_lround(aPoint.y * aResolutionY));
|
2013-06-10 17:05:42 +04:00
|
|
|
}
|
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
static LayerRect FromCSSRect(const CSSRect& aRect, float aResolutionX, float aResolutionY) {
|
|
|
|
return LayerRect(aRect.x * aResolutionX,
|
|
|
|
aRect.y * aResolutionY,
|
|
|
|
aRect.width * aResolutionX,
|
|
|
|
aRect.height * aResolutionY);
|
2013-06-10 17:05:42 +04:00
|
|
|
}
|
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
static LayerIntRect FromCSSRectRounded(const CSSRect& aRect, float aResolutionX, float aResolutionY) {
|
2013-06-15 00:11:31 +04:00
|
|
|
return gfx::RoundedToInt(FromCSSRect(aRect, aResolutionX, aResolutionY));
|
2013-06-03 17:58:07 +04:00
|
|
|
}
|
2013-06-03 17:52:44 +04:00
|
|
|
};
|
|
|
|
|
2013-06-10 17:05:43 +04:00
|
|
|
/*
|
|
|
|
* The pixels that are displayed on the screen.
|
|
|
|
* On non-OMTC platforms this should be equivalent to LayerPixel units.
|
|
|
|
* On OMTC platforms these may diverge from LayerPixel units temporarily,
|
|
|
|
* while an asynchronous zoom is happening, but should eventually converge
|
|
|
|
* back to LayerPixel units. Some variables (such as those representing
|
|
|
|
* chrome UI element sizes) that are not subject to content zoom should
|
|
|
|
* generally be represented in ScreenPixel units.
|
|
|
|
*/
|
2013-06-03 17:53:32 +04:00
|
|
|
struct ScreenPixel {
|
2013-06-15 00:11:30 +04:00
|
|
|
|
|
|
|
// Conversions from CSS units
|
|
|
|
|
|
|
|
static ScreenPoint FromCSSPoint(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
|
|
|
|
return ScreenPoint(aPoint.x * aResolutionX,
|
|
|
|
aPoint.y * aResolutionY);
|
2013-06-15 00:11:29 +04:00
|
|
|
}
|
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
// Conversions to CSS units
|
|
|
|
|
|
|
|
static CSSPoint ToCSSPoint(const ScreenPoint& aPoint, float aResolutionX, float aResolutionY) {
|
2013-06-12 02:13:11 +04:00
|
|
|
return CSSPoint(aPoint.x * aResolutionX,
|
|
|
|
aPoint.y * aResolutionY);
|
|
|
|
}
|
2013-06-15 00:11:29 +04:00
|
|
|
|
2013-06-15 00:11:30 +04:00
|
|
|
static CSSIntRect ToCSSIntRectRoundIn(const ScreenIntRect& aRect, float aResolutionX, float aResolutionY) {
|
|
|
|
CSSIntRect ret(aRect.x, aRect.y, aRect.width, aRect.height);
|
2013-06-15 00:11:29 +04:00
|
|
|
ret.ScaleInverseRoundIn(aResolutionX, aResolutionY);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-06-03 17:53:32 +04:00
|
|
|
};
|
|
|
|
|
2013-05-31 05:30:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|