From 24046f9adfbe72efc8982c62abab19c32d355e0a Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Wed, 10 May 2023 17:01:12 -0400 Subject: [PATCH] FontEditor: Let GlyphEditorWidget initialize a null BitmapFont And remove public reference getters as there is no guarantee they are nonnull. --- .../FontEditor/GlyphEditorWidget.cpp | 42 +++++++++---------- .../FontEditor/GlyphEditorWidget.h | 4 +- .../Applications/FontEditor/MainWidget.cpp | 2 +- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Userland/Applications/FontEditor/GlyphEditorWidget.cpp b/Userland/Applications/FontEditor/GlyphEditorWidget.cpp index 712cb377fe..4af38bee3a 100644 --- a/Userland/Applications/FontEditor/GlyphEditorWidget.cpp +++ b/Userland/Applications/FontEditor/GlyphEditorWidget.cpp @@ -14,7 +14,7 @@ REGISTER_WIDGET(FontEditor, GlyphEditorWidget); namespace FontEditor { -void GlyphEditorWidget::set_font(Gfx::BitmapFont& mutable_font) +void GlyphEditorWidget::initialize(Gfx::BitmapFont* mutable_font) { if (m_font == mutable_font) return; @@ -43,21 +43,21 @@ void GlyphEditorWidget::paint_event(GUI::PaintEvent& event) painter.translate(frame_thickness(), frame_thickness()); painter.translate(-1, -1); - for (int y = 1; y < font().glyph_height(); ++y) { + for (int y = 1; y < m_font->glyph_height(); ++y) { int y_below = y - 1; - bool bold_line = y_below == font().baseline() || y_below == font().mean_line(); - painter.draw_line({ 0, y * m_scale }, { font().max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1); + bool bold_line = y_below == m_font->baseline() || y_below == m_font->mean_line(); + painter.draw_line({ 0, y * m_scale }, { m_font->max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1); } - for (int x = 1; x < font().max_glyph_width(); ++x) - painter.draw_line({ x * m_scale, 0 }, { x * m_scale, font().glyph_height() * m_scale }, palette().threed_shadow2()); + for (int x = 1; x < m_font->max_glyph_width(); ++x) + painter.draw_line({ x * m_scale, 0 }, { x * m_scale, m_font->glyph_height() * m_scale }, palette().threed_shadow2()); - auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); + auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap(); - for (int y = 0; y < font().glyph_height(); ++y) { - for (int x = 0; x < font().max_glyph_width(); ++x) { + for (int y = 0; y < m_font->glyph_height(); ++y) { + for (int x = 0; x < m_font->max_glyph_width(); ++x) { Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale }; - if (x >= font().raw_glyph_width(m_glyph)) { + if (x >= m_font->raw_glyph_width(m_glyph)) { painter.fill_rect(rect, palette().threed_shadow1()); } else { if (bitmap.bit_at(x, y)) @@ -69,9 +69,9 @@ void GlyphEditorWidget::paint_event(GUI::PaintEvent& event) bool GlyphEditorWidget::is_glyph_empty() { - auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); - for (int x = 0; x < font().max_glyph_width(); x++) - for (int y = 0; y < font().glyph_height(); y++) + auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap(); + for (int x = 0; x < m_font->max_glyph_width(); x++) + for (int y = 0; y < m_font->glyph_height(); y++) if (bitmap.bit_at(x, y)) return false; return true; @@ -79,7 +79,7 @@ bool GlyphEditorWidget::is_glyph_empty() void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event) { - if ((event.x() - 1) / m_scale + 1 > font().raw_glyph_width(m_glyph)) + if ((event.x() - 1) / m_scale + 1 > m_font->raw_glyph_width(m_glyph)) return; if (mode() == Move && is_glyph_empty()) return; @@ -90,7 +90,7 @@ void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event) draw_at_mouse(event); } else { memset(m_movable_bits, 0, sizeof(m_movable_bits)); - auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); + auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap(); for (int x = 0; x < bitmap.width(); x++) { for (int y = 0; y < bitmap.height(); y++) { int movable_x = Gfx::GlyphBitmap::max_width() + x; @@ -139,7 +139,7 @@ void GlyphEditorWidget::draw_at_mouse(GUI::MouseEvent const& event) return; int x = (event.x() - 1) / m_scale; int y = (event.y() - 1) / m_scale; - auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); + auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap(); if (x < 0 || x >= bitmap.width()) return; if (y < 0 || y >= bitmap.height()) @@ -156,7 +156,7 @@ void GlyphEditorWidget::move_at_mouse(GUI::MouseEvent const& event) { int x_delta = ((event.x() - 1) / m_scale) - m_scaled_offset_x; int y_delta = ((event.y() - 1) / m_scale) - m_scaled_offset_y; - auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); + auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap(); if (abs(x_delta) > bitmap.width() || abs(y_delta) > bitmap.height()) return; for (int x = 0; x < bitmap.width(); x++) { @@ -193,7 +193,7 @@ void GlyphEditorWidget::rotate_90(Gfx::RotationDirection direction) if (on_undo_event) on_undo_event(); - auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); + auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap(); auto matrix = glyph_as_matrix(bitmap); auto clockwise = direction == Gfx::RotationDirection::Clockwise; @@ -219,7 +219,7 @@ void GlyphEditorWidget::flip(Gfx::Orientation orientation) if (on_undo_event) on_undo_event(); - auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); + auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap(); auto matrix = glyph_as_matrix(bitmap); auto vertical = orientation == Gfx::Orientation::Vertical; @@ -239,12 +239,12 @@ void GlyphEditorWidget::flip(Gfx::Orientation orientation) int GlyphEditorWidget::preferred_width() const { - return frame_thickness() * 2 + font().max_glyph_width() * m_scale - 1; + return frame_thickness() * 2 + m_font->max_glyph_width() * m_scale - 1; } int GlyphEditorWidget::preferred_height() const { - return frame_thickness() * 2 + font().glyph_height() * m_scale - 1; + return frame_thickness() * 2 + m_font->glyph_height() * m_scale - 1; } void GlyphEditorWidget::set_scale(int scale) diff --git a/Userland/Applications/FontEditor/GlyphEditorWidget.h b/Userland/Applications/FontEditor/GlyphEditorWidget.h index 05f18ece74..eac3225ec7 100644 --- a/Userland/Applications/FontEditor/GlyphEditorWidget.h +++ b/Userland/Applications/FontEditor/GlyphEditorWidget.h @@ -33,9 +33,7 @@ public: int preferred_width() const; int preferred_height() const; - Gfx::BitmapFont& font() { return *m_font; } - Gfx::BitmapFont const& font() const { return *m_font; } - void set_font(Gfx::BitmapFont&); + void initialize(Gfx::BitmapFont*); int scale() const { return m_scale; } void set_scale(int scale); diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp index 86c045c333..c506449e35 100644 --- a/Userland/Applications/FontEditor/MainWidget.cpp +++ b/Userland/Applications/FontEditor/MainWidget.cpp @@ -653,7 +653,7 @@ ErrorOr MainWidget::initialize(StringView path, RefPtr&& if (m_preview_label) m_preview_label->set_font(*m_font); - m_glyph_editor_widget->set_font(*m_font); + m_glyph_editor_widget->initialize(m_font); m_glyph_editor_widget->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height()); m_glyph_editor_widget->set_glyph(active_glyph);