Sfoglia il codice sorgente

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.
Nico Weber 1 anno fa
parent
commit
7d9294b9a4

+ 7 - 2
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<Font> ScaledFont::with_size(float point_size) const
+NonnullRefPtr<ScaledFont> ScaledFont::scaled_with_size(float point_size) const
 {
     if (point_size == m_point_height && point_size == m_point_width)
-        return const_cast<ScaledFont*>(this);
+        return *const_cast<ScaledFont*>(this);
     return m_font->scaled_font(point_size);
 }
 
+RefPtr<Font> ScaledFont::with_size(float point_size) const
+{
+    return scaled_with_size(point_size);
+}
+
 Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const
 {
     return m_pixel_metrics;

+ 1 - 0
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<ScaledFont> scaled_with_size(float point_size) const;
     virtual RefPtr<Font> with_size(float point_size) const override;
 
     virtual bool has_color_bitmaps() const override { return m_font->has_color_bitmaps(); }