1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef NSPOINT_H
7 #define NSPOINT_H
8 
9 #include "nsCoord.h"
10 #include "mozilla/gfx/BaseSize.h"
11 #include "mozilla/gfx/BasePoint.h"
12 #include "nsSize.h"
13 #include "mozilla/gfx/Point.h"
14 
15 // nsIntPoint represents a point in one of the types of pixels.
16 // Uses of nsIntPoint should eventually be converted to CSSIntPoint,
17 // LayoutDeviceIntPoint, etc. (see layout/base/Units.h).
18 typedef mozilla::gfx::IntPoint nsIntPoint;
19 
20 // nsPoint represents a point in app units.
21 
22 struct nsPoint : public mozilla::gfx::BasePoint<nscoord, nsPoint> {
23   typedef mozilla::gfx::BasePoint<nscoord, nsPoint> Super;
24 
nsPointnsPoint25   nsPoint() : Super() {}
nsPointnsPoint26   nsPoint(const nsPoint& aPoint) : Super(aPoint) {}
nsPointnsPoint27   nsPoint(nscoord aX, nscoord aY) : Super(aX, aY) {}
28 
29   inline nsIntPoint ScaleToNearestPixels(float aXScale, float aYScale,
30                                          nscoord aAppUnitsPerPixel) const;
31   inline nsIntPoint ToNearestPixels(nscoord aAppUnitsPerPixel) const;
32 
33   /**
34    * Return this point scaled to a different appunits per pixel (APP) ratio.
35    * @param aFromAPP the APP to scale from
36    * @param aToAPP the APP to scale to
37    */
38   MOZ_MUST_USE inline nsPoint
39     ScaleToOtherAppUnits(int32_t aFromAPP, int32_t aToAPP) const;
40 
41   MOZ_MUST_USE inline nsPoint
42     RemoveResolution(const float resolution) const;
43   MOZ_MUST_USE inline nsPoint
44     ApplyResolution(const float resolution) const;
45 };
46 
47 inline nsPoint ToAppUnits(const nsIntPoint& aPoint, nscoord aAppUnitsPerPixel);
48 
49 inline nsIntPoint
ScaleToNearestPixels(float aXScale,float aYScale,nscoord aAppUnitsPerPixel)50 nsPoint::ScaleToNearestPixels(float aXScale, float aYScale,
51                               nscoord aAppUnitsPerPixel) const
52 {
53   return nsIntPoint(
54       NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale),
55       NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale));
56 }
57 
58 inline nsIntPoint
ToNearestPixels(nscoord aAppUnitsPerPixel)59 nsPoint::ToNearestPixels(nscoord aAppUnitsPerPixel) const
60 {
61   return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel);
62 }
63 
64 inline nsPoint
ScaleToOtherAppUnits(int32_t aFromAPP,int32_t aToAPP)65 nsPoint::ScaleToOtherAppUnits(int32_t aFromAPP, int32_t aToAPP) const
66 {
67   if (aFromAPP != aToAPP) {
68     nsPoint point;
69     point.x = NSToCoordRound(NSCoordScale(x, aFromAPP, aToAPP));
70     point.y = NSToCoordRound(NSCoordScale(y, aFromAPP, aToAPP));
71     return point;
72   }
73   return *this;
74 }
75 
76 inline nsPoint
RemoveResolution(const float resolution)77 nsPoint::RemoveResolution(const float resolution) const {
78   if (resolution != 1.0f) {
79     nsPoint point;
80     point.x = NSToCoordRound(NSCoordToFloat(x) / resolution);
81     point.y = NSToCoordRound(NSCoordToFloat(y) / resolution);
82     return point;
83   }
84   return *this;
85 }
86 
87 inline nsPoint
ApplyResolution(const float resolution)88 nsPoint::ApplyResolution(const float resolution) const {
89   if (resolution != 1.0f) {
90     nsPoint point;
91     point.x = NSToCoordRound(NSCoordToFloat(x) * resolution);
92     point.y = NSToCoordRound(NSCoordToFloat(y) * resolution);
93     return point;
94   }
95   return *this;
96 }
97 
98 // app units are integer multiples of pixels, so no rounding needed
99 inline nsPoint
ToAppUnits(const nsIntPoint & aPoint,nscoord aAppUnitsPerPixel)100 ToAppUnits(const nsIntPoint& aPoint, nscoord aAppUnitsPerPixel)
101 {
102   return nsPoint(NSIntPixelsToAppUnits(aPoint.x, aAppUnitsPerPixel),
103                  NSIntPixelsToAppUnits(aPoint.y, aAppUnitsPerPixel));
104 }
105 
106 #endif /* NSPOINT_H */
107