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/. */
12 #include "mozilla/gfx/BasePoint.h"
13 #include "mozilla/gfx/Point.h"
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
;
20 // nsPoint represents a point in app units.
22 struct nsPoint
: public mozilla::gfx::BasePoint
<nscoord
, nsPoint
> {
23 typedef mozilla::gfx::BasePoint
<nscoord
, nsPoint
> Super
;
26 nsPoint(const nsPoint
& aPoint
) = default;
27 nsPoint(nscoord aX
, nscoord aY
) : Super(aX
, aY
) {}
29 inline nsIntPoint
ScaleToNearestPixels(float aXScale
, float aYScale
,
30 nscoord aAppUnitsPerPixel
) const;
31 inline nsIntPoint
ToNearestPixels(nscoord aAppUnitsPerPixel
) const;
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
38 [[nodiscard
]] inline nsPoint
ScaleToOtherAppUnits(int32_t aFromAPP
,
39 int32_t aToAPP
) const;
41 [[nodiscard
]] inline nsPoint
RemoveResolution(const float resolution
) const;
42 [[nodiscard
]] inline nsPoint
ApplyResolution(const float resolution
) const;
45 inline nsPoint
ToAppUnits(const nsIntPoint
& aPoint
, nscoord aAppUnitsPerPixel
);
47 inline nsIntPoint
nsPoint::ScaleToNearestPixels(
48 float aXScale
, float aYScale
, nscoord aAppUnitsPerPixel
) const {
50 NSToIntRoundUp(NSAppUnitsToDoublePixels(x
, aAppUnitsPerPixel
) * aXScale
),
51 NSToIntRoundUp(NSAppUnitsToDoublePixels(y
, aAppUnitsPerPixel
) * aYScale
));
54 inline nsIntPoint
nsPoint::ToNearestPixels(nscoord aAppUnitsPerPixel
) const {
55 return ScaleToNearestPixels(1.0f
, 1.0f
, aAppUnitsPerPixel
);
58 inline nsPoint
nsPoint::ScaleToOtherAppUnits(int32_t aFromAPP
,
59 int32_t aToAPP
) const {
60 if (aFromAPP
!= aToAPP
) {
62 point
.x
= NSToCoordRound(NSCoordScale(x
, aFromAPP
, aToAPP
));
63 point
.y
= NSToCoordRound(NSCoordScale(y
, aFromAPP
, aToAPP
));
69 inline nsPoint
nsPoint::RemoveResolution(const float resolution
) const {
70 if (resolution
!= 1.0f
) {
72 point
.x
= NSToCoordRound(NSCoordToFloat(x
) / resolution
);
73 point
.y
= NSToCoordRound(NSCoordToFloat(y
) / resolution
);
79 inline nsPoint
nsPoint::ApplyResolution(const float resolution
) const {
80 if (resolution
!= 1.0f
) {
82 point
.x
= NSToCoordRound(NSCoordToFloat(x
) * resolution
);
83 point
.y
= NSToCoordRound(NSCoordToFloat(y
) * resolution
);
89 // app units are integer multiples of pixels, so no rounding needed
90 inline nsPoint
ToAppUnits(const nsIntPoint
& aPoint
, nscoord aAppUnitsPerPixel
) {
91 return nsPoint(NSIntPixelsToAppUnits(aPoint
.x
, aAppUnitsPerPixel
),
92 NSIntPixelsToAppUnits(aPoint
.y
, aAppUnitsPerPixel
));
95 #endif /* NSPOINT_H */