diff --git a/Userland/Libraries/LibGfx/Size.h b/Userland/Libraries/LibGfx/Size.h index 200e764c5e..16f3a623e6 100644 --- a/Userland/Libraries/LibGfx/Size.h +++ b/Userland/Libraries/LibGfx/Size.h @@ -93,6 +93,25 @@ public: return static_cast(width()) / static_cast(height()); } + // Horizontal means preserve the width, Vertical means preserve the height. + [[nodiscard]] constexpr Size match_aspect_ratio(float aspect_ratio, Orientation side_to_preserve) const + { + VERIFY(aspect_ratio != 0.0f); + auto matched = *this; + auto height_corresponding_to_width = static_cast(static_cast(width()) / aspect_ratio); + auto width_corresponding_to_height = static_cast(static_cast(height()) * aspect_ratio); + + switch (side_to_preserve) { + case Orientation::Vertical: + matched.m_width = width_corresponding_to_height; + break; + case Orientation::Horizontal: + matched.m_height = height_corresponding_to_width; + break; + } + return matched; + } + template [[nodiscard]] constexpr bool contains(Size const& other) const {