瀏覽代碼

LibGfx: Add Font::pixel_size_rounded_up()

This returns the font's size (distance between ascender and descender)
in pixels, rounded up to the nearest integer.

This is the number we want to use in a lot of UI code, so let's have
a friendly API for it instead of ceil'ing the pixel_size() in a million
random places.
Andreas Kling 2 年之前
父節點
當前提交
93c9344e35

+ 1 - 0
Userland/Libraries/LibGfx/Font/BitmapFont.h

@@ -43,6 +43,7 @@ public:
     void set_presentation_size(u8 size) { m_presentation_size = size; }
 
     virtual float pixel_size() const override { return m_glyph_height; }
+    virtual int pixel_size_rounded_up() const override { return m_glyph_height; }
 
     u16 width() const override { return FontWidth::Normal; }
 

+ 6 - 1
Userland/Libraries/LibGfx/Font/Font.h

@@ -155,9 +155,14 @@ public:
     virtual FontPixelMetrics pixel_metrics() const = 0;
 
     virtual u8 presentation_size() const = 0;
-    virtual float pixel_size() const = 0;
     virtual u8 slope() const = 0;
 
+    // Font pixel size (distance between ascender and descender).
+    virtual float pixel_size() const = 0;
+
+    // Font pixel size, rounded up to the nearest integer.
+    virtual int pixel_size_rounded_up() const = 0;
+
     virtual u16 width() const = 0;
 
     virtual u16 weight() const = 0;

+ 13 - 0
Userland/Libraries/LibGfx/Font/ScaledFont.cpp

@@ -22,6 +22,9 @@ ScaledFont::ScaledFont(NonnullRefPtr<VectorFont> font, float point_width, float
 
     auto metrics = m_font->metrics(m_x_scale, m_y_scale);
 
+    m_pixel_size = m_point_height * 1.33333333f;
+    m_pixel_size_rounded_up = static_cast<int>(ceilf(m_pixel_size));
+
     m_pixel_metrics = Gfx::FontPixelMetrics {
         .size = (float)pixel_size(),
         .x_height = (float)x_height(),
@@ -148,4 +151,14 @@ Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const
     return m_pixel_metrics;
 }
 
+float ScaledFont::pixel_size() const
+{
+    return m_pixel_size;
+}
+
+int ScaledFont::pixel_size_rounded_up() const
+{
+    return m_pixel_size_rounded_up;
+}
+
 }

+ 5 - 1
Userland/Libraries/LibGfx/Font/ScaledFont.h

@@ -36,7 +36,8 @@ public:
     virtual NonnullRefPtr<Font> clone() const override { return MUST(try_clone()); } // FIXME: clone() should not need to be implemented
     virtual ErrorOr<NonnullRefPtr<Font>> try_clone() const override { return const_cast<ScaledFont&>(*this); }
     virtual u8 presentation_size() const override { return m_point_height; }
-    virtual float pixel_size() const override { return m_point_height * 1.33333333f; }
+    virtual float pixel_size() const override;
+    virtual int pixel_size_rounded_up() const override;
     virtual Gfx::FontPixelMetrics pixel_metrics() const override;
     virtual u8 slope() const override { return m_font->slope(); }
     virtual u16 width() const override { return m_font->width(); }
@@ -80,6 +81,9 @@ private:
     mutable HashMap<GlyphIndexWithSubpixelOffset, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps;
     Gfx::FontPixelMetrics m_pixel_metrics;
 
+    float m_pixel_size { 0.0f };
+    int m_pixel_size_rounded_up { 0 };
+
     template<typename T>
     float unicode_view_width(T const& view) const;
 };