nsSize.h (2186B)
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 inline constexpr nscoord 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 22 constexpr nsSize() = default; 23 constexpr nsSize(nscoord aWidth, nscoord aHeight) : Super(aWidth, aHeight) {} 24 25 mozilla::gfx::IntSize ScaleToNearestPixels(float aXScale, float aYScale, 26 nscoord aAppUnitsPerPixel) const { 27 return {NSToIntRoundUp(NSAppUnitsToDoublePixels(width, aAppUnitsPerPixel) * 28 aXScale), 29 NSToIntRoundUp(NSAppUnitsToDoublePixels(height, aAppUnitsPerPixel) * 30 aYScale)}; 31 } 32 33 mozilla::gfx::IntSize ToNearestPixels(nscoord aAppUnitsPerPixel) const { 34 return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel); 35 } 36 37 /** 38 * Return this size scaled to a different appunits per pixel (APP) ratio. 39 * @param aFromAPP the APP to scale from 40 * @param aToAPP the APP to scale to 41 */ 42 [[nodiscard]] nsSize ScaleToOtherAppUnits(int32_t aFromAPP, 43 int32_t aToAPP) const { 44 if (aFromAPP != aToAPP) { 45 nsSize size; 46 size.width = NSToCoordRound(NSCoordScale(width, aFromAPP, aToAPP)); 47 size.height = NSToCoordRound(NSCoordScale(height, aFromAPP, aToAPP)); 48 return size; 49 } 50 return *this; 51 } 52 }; 53 54 inline nsSize IntSizeToAppUnits(mozilla::gfx::IntSize aSize, 55 nscoord aAppUnitsPerPixel) { 56 return nsSize(NSIntPixelsToAppUnits(aSize.width, aAppUnitsPerPixel), 57 NSIntPixelsToAppUnits(aSize.height, aAppUnitsPerPixel)); 58 } 59 60 #endif /* NSSIZE_H */