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 NSSIZE_H
8 #define NSSIZE_H
9 
10 #include "nsCoord.h"
11 #include "mozilla/gfx/BaseSize.h"
12 #include "mozilla/gfx/Point.h"
13 
14 // Maximum allowable size
15 #define NS_MAXSIZE nscoord_MAX
16 
17 typedef mozilla::gfx::IntSize nsIntSize;
18 
19 struct nsSize : public mozilla::gfx::BaseSize<nscoord, nsSize> {
20   typedef mozilla::gfx::BaseSize<nscoord, nsSize> Super;
21 
nsSizensSize22   constexpr nsSize() : Super() {}
nsSizensSize23   constexpr nsSize(nscoord aWidth, nscoord aHeight) : Super(aWidth, aHeight) {}
24 
25   inline mozilla::gfx::IntSize ScaleToNearestPixels(
26       float aXScale, float aYScale, nscoord aAppUnitsPerPixel) const;
27   inline mozilla::gfx::IntSize ToNearestPixels(nscoord aAppUnitsPerPixel) const;
28 
29   /**
30    * Return this size scaled to a different appunits per pixel (APP) ratio.
31    * @param aFromAPP the APP to scale from
32    * @param aToAPP the APP to scale to
33    */
34   [[nodiscard]] inline nsSize ScaleToOtherAppUnits(int32_t aFromAPP,
35                                                    int32_t aToAPP) const;
36 };
37 
ScaleToNearestPixels(float aXScale,float aYScale,nscoord aAppUnitsPerPixel)38 inline mozilla::gfx::IntSize nsSize::ScaleToNearestPixels(
39     float aXScale, float aYScale, nscoord aAppUnitsPerPixel) const {
40   return mozilla::gfx::IntSize(
41       NSToIntRoundUp(NSAppUnitsToDoublePixels(width, aAppUnitsPerPixel) *
42                      aXScale),
43       NSToIntRoundUp(NSAppUnitsToDoublePixels(height, aAppUnitsPerPixel) *
44                      aYScale));
45 }
46 
ToNearestPixels(nscoord aAppUnitsPerPixel)47 inline mozilla::gfx::IntSize nsSize::ToNearestPixels(
48     nscoord aAppUnitsPerPixel) const {
49   return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel);
50 }
51 
ScaleToOtherAppUnits(int32_t aFromAPP,int32_t aToAPP)52 inline nsSize nsSize::ScaleToOtherAppUnits(int32_t aFromAPP,
53                                            int32_t aToAPP) const {
54   if (aFromAPP != aToAPP) {
55     nsSize size;
56     size.width = NSToCoordRound(NSCoordScale(width, aFromAPP, aToAPP));
57     size.height = NSToCoordRound(NSCoordScale(height, aFromAPP, aToAPP));
58     return size;
59   }
60   return *this;
61 }
62 
IntSizeToAppUnits(mozilla::gfx::IntSize aSize,nscoord aAppUnitsPerPixel)63 inline nsSize IntSizeToAppUnits(mozilla::gfx::IntSize aSize,
64                                 nscoord aAppUnitsPerPixel) {
65   return nsSize(NSIntPixelsToAppUnits(aSize.width, aAppUnitsPerPixel),
66                 NSIntPixelsToAppUnits(aSize.height, aAppUnitsPerPixel));
67 }
68 
69 #endif /* NSSIZE_H */
70