1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/src/nsPoint.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef NSPOINT_H 1.10 +#define NSPOINT_H 1.11 + 1.12 +#include "nsCoord.h" 1.13 +#include "mozilla/gfx/BaseSize.h" 1.14 +#include "mozilla/gfx/BasePoint.h" 1.15 +#include "nsSize.h" 1.16 + 1.17 +struct nsIntPoint; 1.18 + 1.19 +// nsPoint represents a point in app units. 1.20 + 1.21 +struct nsPoint : public mozilla::gfx::BasePoint<nscoord, nsPoint> { 1.22 + typedef mozilla::gfx::BasePoint<nscoord, nsPoint> Super; 1.23 + 1.24 + nsPoint() : Super() {} 1.25 + nsPoint(const nsPoint& aPoint) : Super(aPoint) {} 1.26 + nsPoint(nscoord aX, nscoord aY) : Super(aX, aY) {} 1.27 + 1.28 + inline nsIntPoint ScaleToNearestPixels(float aXScale, float aYScale, 1.29 + nscoord aAppUnitsPerPixel) const; 1.30 + inline nsIntPoint ToNearestPixels(nscoord aAppUnitsPerPixel) const; 1.31 + 1.32 + // Converts this point from aFromAPP, an appunits per pixel ratio, to aToAPP. 1.33 + inline nsPoint ConvertAppUnits(int32_t aFromAPP, int32_t aToAPP) const; 1.34 +}; 1.35 + 1.36 +// nsIntPoint represents a point in one of the types of pixels. 1.37 +// Uses of nsIntPoint should eventually be converted to CSSIntPoint, 1.38 +// LayoutDeviceIntPoint, etc. (see layout/base/Units.h). 1.39 + 1.40 +struct nsIntPoint : public mozilla::gfx::BasePoint<int32_t, nsIntPoint> { 1.41 + typedef mozilla::gfx::BasePoint<int32_t, nsIntPoint> Super; 1.42 + 1.43 + nsIntPoint() : Super() {} 1.44 + nsIntPoint(const nsIntPoint& aPoint) : Super(aPoint) {} 1.45 + nsIntPoint(int32_t aX, int32_t aY) : Super(aX, aY) {} 1.46 + 1.47 + inline nsPoint ToAppUnits(nscoord aAppUnitsPerPixel) const; 1.48 +}; 1.49 + 1.50 +inline nsIntPoint 1.51 +nsPoint::ScaleToNearestPixels(float aXScale, float aYScale, 1.52 + nscoord aAppUnitsPerPixel) const 1.53 +{ 1.54 + return nsIntPoint( 1.55 + NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale), 1.56 + NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale)); 1.57 +} 1.58 + 1.59 +inline nsIntPoint 1.60 +nsPoint::ToNearestPixels(nscoord aAppUnitsPerPixel) const 1.61 +{ 1.62 + return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel); 1.63 +} 1.64 + 1.65 +inline nsPoint 1.66 +nsPoint::ConvertAppUnits(int32_t aFromAPP, int32_t aToAPP) const 1.67 +{ 1.68 + if (aFromAPP != aToAPP) { 1.69 + nsPoint point; 1.70 + point.x = NSToCoordRound(NSCoordScale(x, aFromAPP, aToAPP)); 1.71 + point.y = NSToCoordRound(NSCoordScale(y, aFromAPP, aToAPP)); 1.72 + return point; 1.73 + } 1.74 + return *this; 1.75 +} 1.76 + 1.77 +// app units are integer multiples of pixels, so no rounding needed 1.78 +inline nsPoint 1.79 +nsIntPoint::ToAppUnits(nscoord aAppUnitsPerPixel) const 1.80 +{ 1.81 + return nsPoint(NSIntPixelsToAppUnits(x, aAppUnitsPerPixel), 1.82 + NSIntPixelsToAppUnits(y, aAppUnitsPerPixel)); 1.83 +} 1.84 + 1.85 +#endif /* NSPOINT_H */