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