From 18dfc612800b116bed30809e439ff190f11404e2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 2 Oct 2023 20:29:42 +0100 Subject: [PATCH] LibGfx: Expose BitmapFont glyph data as Spans instead of raw pointers --- .../Applications/FontEditor/MainWidget.cpp | 20 +++++++++---------- .../Applications/FontEditor/UndoSelection.h | 14 ++++++------- Userland/Libraries/LibGfx/Font/BitmapFont.h | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp index 60015035bc..6887638f3f 100644 --- a/Userland/Applications/FontEditor/MainWidget.cpp +++ b/Userland/Applications/FontEditor/MainWidget.cpp @@ -1026,12 +1026,12 @@ ErrorOr MainWidget::copy_selected_glyphs() { size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height(); auto selection = m_glyph_map_widget->selection().normalized(); - auto* rows = m_font->rows() + selection.start() * bytes_per_glyph; - auto* widths = m_font->widths() + selection.start(); + auto rows = m_font->rows().slice(selection.start() * bytes_per_glyph, selection.size() * bytes_per_glyph); + auto widths = m_font->widths().slice(selection.start(), selection.size()); ByteBuffer buffer; - TRY(buffer.try_append(rows, bytes_per_glyph * selection.size())); - TRY(buffer.try_append(widths, selection.size())); + TRY(buffer.try_append(rows)); + TRY(buffer.try_append(widths)); HashMap metadata; metadata.set("start", DeprecatedString::number(selection.start())); @@ -1073,8 +1073,8 @@ void MainWidget::paste_glyphs() size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height(); size_t bytes_per_copied_glyph = Gfx::GlyphBitmap::bytes_per_row() * height; size_t copyable_bytes_per_glyph = min(bytes_per_glyph, bytes_per_copied_glyph); - auto* rows = m_font->rows() + selection.start() * bytes_per_glyph; - auto* widths = m_font->widths() + selection.start(); + auto rows = m_font->rows().slice(selection.start() * bytes_per_glyph); + auto widths = m_font->widths().slice(selection.start()); for (size_t i = 0; i < range_bound_glyph_count; ++i) { auto copyable_width = m_font->is_fixed_width() @@ -1105,10 +1105,10 @@ void MainWidget::delete_selected_glyphs() push_undo(action_text); size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height(); - auto* rows = m_font->rows() + selection.start() * bytes_per_glyph; - auto* widths = m_font->widths() + selection.start(); - memset(rows, 0, bytes_per_glyph * selection.size()); - memset(widths, 0, selection.size()); + auto rows = m_font->rows().slice(selection.start() * bytes_per_glyph, selection.size() * bytes_per_glyph); + auto widths = m_font->widths().slice(selection.start(), selection.size()); + memset(rows.data(), 0, bytes_per_glyph * selection.size()); + memset(widths.data(), 0, selection.size()); if (m_font->is_fixed_width()) m_glyph_editor_present_checkbox->set_checked(false, GUI::AllowCallback::No); diff --git a/Userland/Applications/FontEditor/UndoSelection.h b/Userland/Applications/FontEditor/UndoSelection.h index f73e478e29..67a15d1cad 100644 --- a/Userland/Applications/FontEditor/UndoSelection.h +++ b/Userland/Applications/FontEditor/UndoSelection.h @@ -25,10 +25,10 @@ public: { auto state = TRY(try_make_ref_counted(m_start, m_size, m_active_glyph, *m_font, m_glyph_map_widget)); size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * font().glyph_height(); - auto* rows = font().rows() + m_start * bytes_per_glyph; - auto* widths = font().widths() + m_start; + auto rows = font().rows().slice(m_start * bytes_per_glyph, m_size * bytes_per_glyph); + auto widths = font().widths().slice(m_start, m_size); TRY(state->m_data.try_append(&rows[0], bytes_per_glyph * m_size)); - TRY(state->m_data.try_append(&widths[0], m_size)); + TRY(state->m_data.try_append(widths)); TRY(state->m_restored_modified_state.try_ensure_capacity(m_size)); for (int glyph = m_start; glyph < m_start + m_size; ++glyph) @@ -39,10 +39,10 @@ public: void restore_state(UndoSelection const& state) { size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * font().glyph_height(); - auto* rows = font().rows() + state.m_start * bytes_per_glyph; - auto* widths = font().widths() + state.m_start; - memcpy(rows, &state.m_data[0], bytes_per_glyph * state.m_size); - memcpy(widths, &state.m_data[bytes_per_glyph * state.m_size], state.m_size); + auto rows = font().rows().slice(state.m_start * bytes_per_glyph, state.m_size * bytes_per_glyph); + auto widths = font().widths().slice(state.m_start, state.m_size); + memcpy(rows.data(), &state.m_data[0], bytes_per_glyph * state.m_size); + memcpy(widths.data(), &state.m_data[bytes_per_glyph * state.m_size], state.m_size); for (int i = 0; i < state.m_size; ++i) m_glyph_map_widget->set_glyph_modified(state.m_start + i, state.m_restored_modified_state[i]); diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.h b/Userland/Libraries/LibGfx/Font/BitmapFont.h index d7893cb157..2915417d55 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.h +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.h @@ -38,8 +38,8 @@ public: ~BitmapFont(); - u8* rows() { return m_rows.data(); } - u8* widths() { return m_glyph_widths.data(); } + Bytes rows() { return m_rows; } + Span widths() { return m_glyph_widths; } virtual float point_size() const override { return m_presentation_size; }