/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- * 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 "nsDeviceContext.h" namespace mozilla { // The pixels that content authors use to specify sizes in. struct CSSPixel { static gfx::PointTyped FromAppUnits(const nsPoint &pt) { return gfx::PointTyped(NSAppUnitsToFloatPixels(pt.x, float(nsDeviceContext::AppUnitsPerCSSPixel())), NSAppUnitsToFloatPixels(pt.y, float(nsDeviceContext::AppUnitsPerCSSPixel()))); } static nsPoint ToAppUnits(const gfx::PointTyped &pt) { return nsPoint(NSFloatPixelsToAppUnits(pt.x, float(nsDeviceContext::AppUnitsPerCSSPixel())), NSFloatPixelsToAppUnits(pt.y, float(nsDeviceContext::AppUnitsPerCSSPixel()))); } static gfx::RectTyped FromAppUnits(const nsRect &rect) { return gfx::RectTyped(NSAppUnitsToFloatPixels(rect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())), NSAppUnitsToFloatPixels(rect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())), NSAppUnitsToFloatPixels(rect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())), NSAppUnitsToFloatPixels(rect.height, float(nsDeviceContext::AppUnitsPerCSSPixel()))); } }; typedef gfx::PointTyped CSSPoint; typedef gfx::RectTyped CSSRect; typedef gfx::IntRectTyped CSSIntRect; struct LayerPixel { static gfx::IntPointTyped FromCSSPointRounded(const CSSPoint& pt, float resolutionX, float resolutionY) { return gfx::IntPointTyped(NS_lround(pt.x * resolutionX), NS_lround(pt.y * resolutionY)); } static gfx::IntRectTyped RoundToInt(const gfx::RectTyped& rect) { return gfx::IntRectTyped(NS_lround(rect.x), NS_lround(rect.y), NS_lround(rect.width), NS_lround(rect.height)); } static gfx::RectTyped FromCSSRect(const CSSRect& rect, float resolutionX, float resolutionY) { return gfx::RectTyped(rect.x * resolutionX, rect.y * resolutionY, rect.width * resolutionX, rect.height * resolutionY); } static gfx::IntRectTyped FromCSSRectRounded(const CSSRect& rect, float resolutionX, float resolutionY) { return RoundToInt(FromCSSRect(rect, resolutionX, resolutionY)); } static gfx::IntRectTyped FromCSSRectRoundOut(const CSSRect& rect, gfxFloat resolution) { gfx::RectTyped scaled(rect.x, rect.y, rect.width, rect.height); scaled.ScaleInverseRoundOut(resolution); return gfx::IntRectTyped(scaled.x, scaled.y, scaled.width, scaled.height); } static CSSIntRect ToCSSIntRectRoundIn(const gfx::IntRectTyped& rect, gfxFloat resolution) { gfx::IntRectTyped ret(rect.x, rect.y, rect.width, rect.height); ret.ScaleInverseRoundIn(resolution, resolution); return ret; } static CSSIntRect ToCSSIntRectRoundIn(const gfx::IntRectTyped& rect, gfxSize resolution) { gfx::IntRectTyped ret(rect.x, rect.y, rect.width, rect.height); ret.ScaleInverseRoundIn(resolution.width, resolution.height); return ret; } }; typedef gfx::PointTyped LayerPoint; typedef gfx::IntPointTyped LayerIntPoint; typedef gfx::IntSizeTyped LayerIntSize; typedef gfx::RectTyped LayerRect; typedef gfx::IntRectTyped LayerIntRect; struct ScreenPixel { }; typedef gfx::PointTyped ScreenPoint; }; #endif