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.
This commit is contained in:
Andreas Kling 2023-03-03 19:30:55 +01:00
parent 6c8e9fa1b3
commit 93c9344e35
Notes: sideshowbarker 2024-07-17 06:46:15 +09:00
4 changed files with 25 additions and 2 deletions

View file

@ -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; }

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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;
};