From 7d9294b9a469e871e520989c1d103abdb359d6bf Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 1 Mar 2024 08:06:42 -0500 Subject: [PATCH] LibGfx: Add ScaledFont::scaled_with_size() It's like ScaledFont::with_size(), except that it guarantees that the result is non-null and ScaledFont. (Smart pointers don't allow covariant return types, else we could just narrow down the return type of with_size() while still overriding the base method.) No behavior change. --- Userland/Libraries/LibGfx/Font/ScaledFont.cpp | 9 +++++++-- Userland/Libraries/LibGfx/Font/ScaledFont.h | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.cpp b/Userland/Libraries/LibGfx/Font/ScaledFont.cpp index 84f3bfc5c5..a528dcff25 100644 --- a/Userland/Libraries/LibGfx/Font/ScaledFont.cpp +++ b/Userland/Libraries/LibGfx/Font/ScaledFont.cpp @@ -153,13 +153,18 @@ u8 ScaledFont::glyph_fixed_width() const return glyph_metrics(glyph_id_for_code_point(' ')).advance_width; } -RefPtr ScaledFont::with_size(float point_size) const +NonnullRefPtr ScaledFont::scaled_with_size(float point_size) const { if (point_size == m_point_height && point_size == m_point_width) - return const_cast(this); + return *const_cast(this); return m_font->scaled_font(point_size); } +RefPtr ScaledFont::with_size(float point_size) const +{ + return scaled_with_size(point_size); +} + Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const { return m_pixel_metrics; diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.h b/Userland/Libraries/LibGfx/Font/ScaledFont.h index e15e83a781..d2c09fa0a7 100644 --- a/Userland/Libraries/LibGfx/Font/ScaledFont.h +++ b/Userland/Libraries/LibGfx/Font/ScaledFont.h @@ -69,6 +69,7 @@ public: virtual String qualified_name() const override { return MUST(String::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope())); } virtual String human_readable_name() const override { return MUST(String::formatted("{} {} {}", family(), variant(), presentation_size())); } + virtual NonnullRefPtr scaled_with_size(float point_size) const; virtual RefPtr with_size(float point_size) const override; virtual bool has_color_bitmaps() const override { return m_font->has_color_bitmaps(); }