LibGUI: Make GlyphMapWidget work with vector fonts

This basically just meant replacing the `m_font` field with the one
inherited from Widget.
This commit is contained in:
Sam Atkins 2022-01-09 17:29:06 +00:00 committed by Andreas Kling
parent 8175cd0a28
commit 21a24c36a8
Notes: sideshowbarker 2024-07-17 20:46:39 +09:00
3 changed files with 15 additions and 23 deletions

View File

@ -488,7 +488,7 @@ void FontEditorWidget::initialize(String const& path, RefPtr<Gfx::BitmapFont>&&
m_path = path;
m_edited_font = edited_font;
m_glyph_map_widget->initialize(*m_edited_font);
m_glyph_map_widget->set_font(*m_edited_font);
m_glyph_editor_widget->initialize(*m_edited_font);
did_resize_glyph_editor();

View File

@ -12,6 +12,8 @@
#include <LibGfx/Emoji.h>
#include <LibGfx/Palette.h>
REGISTER_WIDGET(GUI, GlyphMapWidget);
namespace GUI {
GlyphMapWidget::Selection GlyphMapWidget::Selection::normalized() const
@ -49,26 +51,15 @@ GlyphMapWidget::GlyphMapWidget()
{
set_focus_policy(FocusPolicy::StrongFocus);
horizontal_scrollbar().set_visible(false);
did_change_font();
}
GlyphMapWidget::~GlyphMapWidget()
{
}
void GlyphMapWidget::initialize(Gfx::BitmapFont& mutable_font)
{
if (m_font == mutable_font)
return;
m_font = mutable_font;
vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
set_active_glyph('A');
}
void GlyphMapWidget::resize_event(ResizeEvent& event)
{
if (!m_font)
return;
int event_width = event.size().width() - vertical_scrollbar().width() - (frame_thickness() * 2) - m_horizontal_spacing;
int event_height = event.size().height() - (frame_thickness() * 2);
m_visible_glyphs = (event_width * event_height) / (font().max_glyph_width() * font().glyph_height());
@ -139,16 +130,16 @@ void GlyphMapWidget::paint_event(PaintEvent& event)
font().glyph_height());
if (m_selection.contains(glyph)) {
painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
if (m_font->contains_raw_glyph(glyph))
if (font().contains_glyph(glyph))
painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph))
painter.draw_emoji(inner_rect.location(), *emoji, *m_font);
} else if (m_font->contains_raw_glyph(glyph)) {
painter.draw_emoji(inner_rect.location(), *emoji, font());
} else if (font().contains_glyph(glyph)) {
painter.fill_rect(outer_rect, palette().base());
painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
} else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph)) {
painter.fill_rect(outer_rect, Gfx::Color { 255, 150, 150 });
painter.draw_emoji(inner_rect.location(), *emoji, *m_font);
painter.draw_emoji(inner_rect.location(), *emoji, font());
}
}
painter.draw_focus_rect(get_outer_rect(m_active_glyph), Gfx::Color::Black);
@ -252,6 +243,12 @@ void GlyphMapWidget::keydown_event(KeyEvent& event)
}
}
void GlyphMapWidget::did_change_font()
{
vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
set_active_glyph('A');
}
void GlyphMapWidget::scroll_to_glyph(int glyph)
{
int row = glyph / columns();

View File

@ -19,8 +19,6 @@ class GlyphMapWidget final : public AbstractScrollableWidget {
public:
virtual ~GlyphMapWidget() override;
void initialize(Gfx::BitmapFont&);
class Selection {
public:
Selection() = default;
@ -61,9 +59,6 @@ public:
int rows() const { return m_rows; }
int columns() const { return m_columns; }
Gfx::BitmapFont& font() { return *m_font; }
Gfx::BitmapFont const& font() const { return *m_font; }
Function<void(int)> on_active_glyph_changed;
private:
@ -72,6 +67,7 @@ private:
virtual void mousedown_event(MouseEvent&) override;
virtual void keydown_event(KeyEvent&) override;
virtual void resize_event(ResizeEvent&) override;
virtual void did_change_font() override;
Gfx::IntRect get_outer_rect(int glyph) const;
@ -80,7 +76,6 @@ private:
void paste_glyph(int glyph);
void delete_glyph(int glyph);
RefPtr<Gfx::BitmapFont> m_font;
int m_glyph_count { 0x110000 };
int m_columns { 0 };
int m_rows { 0 };