From 79dfe9846de4ce99879964d5e1c953542ac8f041 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Sun, 3 Jan 2021 17:31:18 +0100 Subject: [PATCH] LibGfx: Generalize glyph placement in Painter --- Userland/Libraries/LibGfx/BitmapFont.cpp | 7 ++++++- Userland/Libraries/LibGfx/Font.h | 5 ++++- Userland/Libraries/LibGfx/Painter.cpp | 8 ++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibGfx/BitmapFont.cpp b/Userland/Libraries/LibGfx/BitmapFont.cpp index 2ffd87cf6f..492f921053 100644 --- a/Userland/Libraries/LibGfx/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/BitmapFont.cpp @@ -223,7 +223,12 @@ bool BitmapFont::write_to_file(const StringView& path) Glyph BitmapFont::glyph(u32 code_point) const { - return Glyph(GlyphBitmap(&m_rows[code_point * m_glyph_height], { glyph_width(code_point), m_glyph_height })); + auto width = glyph_width(code_point); + return Glyph( + GlyphBitmap(&m_rows[code_point * m_glyph_height], { width, m_glyph_height }), + 0, + width, + m_glyph_height); } int BitmapFont::glyph_or_emoji_width(u32 code_point) const diff --git a/Userland/Libraries/LibGfx/Font.h b/Userland/Libraries/LibGfx/Font.h index 2ec7fece4d..c5a588ecde 100644 --- a/Userland/Libraries/LibGfx/Font.h +++ b/Userland/Libraries/LibGfx/Font.h @@ -70,8 +70,11 @@ private: class Glyph { public: - Glyph(const GlyphBitmap& glyph_bitmap) + Glyph(const GlyphBitmap& glyph_bitmap, int left_bearing, int advance, int ascent) : m_glyph_bitmap(glyph_bitmap) + , m_left_bearing(left_bearing) + , m_advance(advance) + , m_ascent(ascent) { } diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index 3780ea6d6a..5ee9b56ccb 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -901,11 +901,11 @@ FLATTEN void Painter::draw_glyph(const IntPoint& point, u32 code_point, Color co FLATTEN void Painter::draw_glyph(const IntPoint& point, u32 code_point, const Font& font, Color color) { auto glyph = font.glyph(code_point); - if (glyph.is_glyph_bitmap()) { - draw_bitmap(point, glyph.glyph_bitmap(), color); - } else { - auto top_left = point + IntPoint(glyph.left_bearing(), font.x_height() - glyph.ascent()); + auto top_left = point + IntPoint(glyph.left_bearing(), font.glyph_height() - glyph.ascent()); + if (glyph.is_glyph_bitmap()) { + draw_bitmap(top_left, glyph.glyph_bitmap(), color); + } else { blit_filtered(top_left, *glyph.bitmap(), glyph.bitmap()->rect(), [color](Color pixel) -> Color { return pixel.multiply(color); });