/* -*- 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 * 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" #include "mozilla/gfx/Rect.h" #include "mozilla/gfx/ScaleFactor.h" #include "nsDeviceContext.h" #include "nsRect.h" namespace mozilla { struct CSSPixel; struct LayoutDevicePixel; struct LayerPixel; struct ScreenPixel; typedef gfx::PointTyped CSSPoint; typedef gfx::IntPointTyped CSSIntPoint; typedef gfx::SizeTyped CSSSize; typedef gfx::IntSizeTyped CSSIntSize; typedef gfx::RectTyped CSSRect; typedef gfx::IntRectTyped CSSIntRect; typedef gfx::MarginTyped CSSMargin; typedef gfx::PointTyped LayoutDevicePoint; typedef gfx::IntPointTyped LayoutDeviceIntPoint; typedef gfx::SizeTyped LayoutDeviceSize; typedef gfx::IntSizeTyped LayoutDeviceIntSize; typedef gfx::RectTyped LayoutDeviceRect; typedef gfx::IntRectTyped LayoutDeviceIntRect; typedef gfx::MarginTyped LayoutDeviceMargin; typedef gfx::PointTyped LayerPoint; typedef gfx::IntPointTyped LayerIntPoint; typedef gfx::SizeTyped LayerSize; typedef gfx::IntSizeTyped LayerIntSize; typedef gfx::RectTyped LayerRect; typedef gfx::IntRectTyped LayerIntRect; typedef gfx::MarginTyped LayerMargin; typedef gfx::PointTyped ScreenPoint; typedef gfx::IntPointTyped ScreenIntPoint; typedef gfx::SizeTyped ScreenSize; typedef gfx::IntSizeTyped ScreenIntSize; typedef gfx::RectTyped ScreenRect; typedef gfx::IntRectTyped ScreenIntRect; typedef gfx::MarginTyped ScreenMargin; typedef gfx::ScaleFactor CSSToLayoutDeviceScale; typedef gfx::ScaleFactor LayoutDeviceToCSSScale; typedef gfx::ScaleFactor CSSToLayerScale; typedef gfx::ScaleFactor LayerToCSSScale; typedef gfx::ScaleFactor CSSToScreenScale; typedef gfx::ScaleFactor ScreenToCSSScale; typedef gfx::ScaleFactor LayoutDeviceToLayerScale; typedef gfx::ScaleFactor LayerToLayoutDeviceScale; typedef gfx::ScaleFactor LayoutDeviceToScreenScale; typedef gfx::ScaleFactor ScreenToLayoutDeviceScale; typedef gfx::ScaleFactor LayerToScreenScale; typedef gfx::ScaleFactor ScreenToLayerScale; typedef gfx::ScaleFactor ScreenToScreenScale; /* * The pixels that content authors use to specify sizes in. */ struct CSSPixel { // 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()))); } static CSSIntPoint FromAppUnitsRounded(const nsPoint& aPoint) { return CSSIntPoint(NSAppUnitsToIntPixels(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())), NSAppUnitsToIntPixels(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel()))); } static CSSIntSize FromAppUnitsRounded(const nsSize& aSize) { return CSSIntSize(NSAppUnitsToIntPixels(aSize.width, float(nsDeviceContext::AppUnitsPerCSSPixel())), NSAppUnitsToIntPixels(aSize.height, float(nsDeviceContext::AppUnitsPerCSSPixel()))); } 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) { return nsPoint(NSFloatPixelsToAppUnits(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())), NSFloatPixelsToAppUnits(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel()))); } static nsPoint ToAppUnits(const CSSIntPoint& aPoint) { return nsPoint(NSIntPixelsToAppUnits(aPoint.x, nsDeviceContext::AppUnitsPerCSSPixel()), NSIntPixelsToAppUnits(aPoint.y, nsDeviceContext::AppUnitsPerCSSPixel())); } static nsRect ToAppUnits(const CSSRect& aRect) { 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()))); } }; /* * The pixels that are referred to as "device pixels" in layout code. In * general this is obtained by converting a value in app units value by the * nsDeviceContext::AppUnitsPerDevPixel() value. The size of these pixels * are affected by: * 1) the "full zoom" (see nsPresContext::SetFullZoom) * 2) the "widget scale" (see nsIWidget::GetDefaultScale) */ struct LayoutDevicePixel { static LayoutDeviceIntPoint FromUntyped(const nsIntPoint& aPoint) { return LayoutDeviceIntPoint(aPoint.x, aPoint.y); } static nsIntPoint ToUntyped(const LayoutDeviceIntPoint& aPoint) { return nsIntPoint(aPoint.x, aPoint.y); } static LayoutDeviceIntPoint FromAppUnits(const nsPoint& aPoint, nscoord aAppUnitsPerDevPixel) { return LayoutDeviceIntPoint(NSAppUnitsToIntPixels(aPoint.x, aAppUnitsPerDevPixel), NSAppUnitsToIntPixels(aPoint.y, aAppUnitsPerDevPixel)); } static LayoutDeviceIntPoint FromAppUnitsToNearest(const nsPoint& aPoint, nscoord appUnitsPerDevPixel) { return FromUntyped(aPoint.ToNearestPixels(appUnitsPerDevPixel)); } }; /* * 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" (see nsIWidget::GetDefaultScale) */ struct LayerPixel { }; /* * 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. */ struct ScreenPixel { }; // Operators to apply ScaleFactors directly to Points and Rects template gfx::PointTyped operator*(const gfx::PointTyped& aPoint, const gfx::ScaleFactor& aScale) { return gfx::PointTyped(aPoint.x * aScale.scale, aPoint.y * aScale.scale); } template gfx::PointTyped operator/(const gfx::PointTyped& aPoint, const gfx::ScaleFactor& aScale) { return gfx::PointTyped(aPoint.x / aScale.scale, aPoint.y / aScale.scale); } template gfx::RectTyped operator*(const gfx::RectTyped& aRect, const gfx::ScaleFactor& aScale) { return gfx::RectTyped(aRect.x * aScale.scale, aRect.y * aScale.scale, aRect.width * aScale.scale, aRect.height * aScale.scale); } template gfx::RectTyped operator/(const gfx::RectTyped& aRect, const gfx::ScaleFactor& aScale) { return gfx::RectTyped(aRect.x / aScale.scale, aRect.y / aScale.scale, aRect.width / aScale.scale, aRect.height / aScale.scale); } template gfx::RectTyped operator*(const gfx::IntRectTyped& aRect, const gfx::ScaleFactor& aScale) { return gfx::RectTyped(float(aRect.x) * aScale.scale, float(aRect.y) * aScale.scale, float(aRect.width) * aScale.scale, float(aRect.height) * aScale.scale); } template gfx::RectTyped operator/(const gfx::IntRectTyped& aRect, const gfx::ScaleFactor& aScale) { return gfx::RectTyped(float(aRect.x) / aScale.scale, float(aRect.y) / aScale.scale, float(aRect.width) / aScale.scale, float(aRect.height) / aScale.scale); } }; #endif