|
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 |
|
14 struct nsIntPoint; |
|
15 |
|
16 // nsPoint represents a point in app units. |
|
17 |
|
18 struct nsPoint : public mozilla::gfx::BasePoint<nscoord, nsPoint> { |
|
19 typedef mozilla::gfx::BasePoint<nscoord, nsPoint> Super; |
|
20 |
|
21 nsPoint() : Super() {} |
|
22 nsPoint(const nsPoint& aPoint) : Super(aPoint) {} |
|
23 nsPoint(nscoord aX, nscoord aY) : Super(aX, aY) {} |
|
24 |
|
25 inline nsIntPoint ScaleToNearestPixels(float aXScale, float aYScale, |
|
26 nscoord aAppUnitsPerPixel) const; |
|
27 inline nsIntPoint ToNearestPixels(nscoord aAppUnitsPerPixel) const; |
|
28 |
|
29 // Converts this point from aFromAPP, an appunits per pixel ratio, to aToAPP. |
|
30 inline nsPoint ConvertAppUnits(int32_t aFromAPP, int32_t aToAPP) const; |
|
31 }; |
|
32 |
|
33 // nsIntPoint represents a point in one of the types of pixels. |
|
34 // Uses of nsIntPoint should eventually be converted to CSSIntPoint, |
|
35 // LayoutDeviceIntPoint, etc. (see layout/base/Units.h). |
|
36 |
|
37 struct nsIntPoint : public mozilla::gfx::BasePoint<int32_t, nsIntPoint> { |
|
38 typedef mozilla::gfx::BasePoint<int32_t, nsIntPoint> Super; |
|
39 |
|
40 nsIntPoint() : Super() {} |
|
41 nsIntPoint(const nsIntPoint& aPoint) : Super(aPoint) {} |
|
42 nsIntPoint(int32_t aX, int32_t aY) : Super(aX, aY) {} |
|
43 |
|
44 inline nsPoint ToAppUnits(nscoord aAppUnitsPerPixel) const; |
|
45 }; |
|
46 |
|
47 inline nsIntPoint |
|
48 nsPoint::ScaleToNearestPixels(float aXScale, float aYScale, |
|
49 nscoord aAppUnitsPerPixel) const |
|
50 { |
|
51 return nsIntPoint( |
|
52 NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale), |
|
53 NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale)); |
|
54 } |
|
55 |
|
56 inline nsIntPoint |
|
57 nsPoint::ToNearestPixels(nscoord aAppUnitsPerPixel) const |
|
58 { |
|
59 return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel); |
|
60 } |
|
61 |
|
62 inline nsPoint |
|
63 nsPoint::ConvertAppUnits(int32_t aFromAPP, int32_t aToAPP) const |
|
64 { |
|
65 if (aFromAPP != aToAPP) { |
|
66 nsPoint point; |
|
67 point.x = NSToCoordRound(NSCoordScale(x, aFromAPP, aToAPP)); |
|
68 point.y = NSToCoordRound(NSCoordScale(y, aFromAPP, aToAPP)); |
|
69 return point; |
|
70 } |
|
71 return *this; |
|
72 } |
|
73 |
|
74 // app units are integer multiples of pixels, so no rounding needed |
|
75 inline nsPoint |
|
76 nsIntPoint::ToAppUnits(nscoord aAppUnitsPerPixel) const |
|
77 { |
|
78 return nsPoint(NSIntPixelsToAppUnits(x, aAppUnitsPerPixel), |
|
79 NSIntPixelsToAppUnits(y, aAppUnitsPerPixel)); |
|
80 } |
|
81 |
|
82 #endif /* NSPOINT_H */ |