1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:27:35 +00:00

LibGfx: Expose BitmapFont glyph data as Spans instead of raw pointers

This commit is contained in:
Sam Atkins 2023-10-02 20:29:42 +01:00 committed by Tim Schumacher
parent 80e756daef
commit 18dfc61280
3 changed files with 19 additions and 19 deletions

View file

@ -1026,12 +1026,12 @@ ErrorOr<void> MainWidget::copy_selected_glyphs()
{ {
size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height(); size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height();
auto selection = m_glyph_map_widget->selection().normalized(); auto selection = m_glyph_map_widget->selection().normalized();
auto* rows = m_font->rows() + selection.start() * bytes_per_glyph; auto rows = m_font->rows().slice(selection.start() * bytes_per_glyph, selection.size() * bytes_per_glyph);
auto* widths = m_font->widths() + selection.start(); auto widths = m_font->widths().slice(selection.start(), selection.size());
ByteBuffer buffer; ByteBuffer buffer;
TRY(buffer.try_append(rows, bytes_per_glyph * selection.size())); TRY(buffer.try_append(rows));
TRY(buffer.try_append(widths, selection.size())); TRY(buffer.try_append(widths));
HashMap<DeprecatedString, DeprecatedString> metadata; HashMap<DeprecatedString, DeprecatedString> metadata;
metadata.set("start", DeprecatedString::number(selection.start())); 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_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height();
size_t bytes_per_copied_glyph = Gfx::GlyphBitmap::bytes_per_row() * 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); 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 rows = m_font->rows().slice(selection.start() * bytes_per_glyph);
auto* widths = m_font->widths() + selection.start(); auto widths = m_font->widths().slice(selection.start());
for (size_t i = 0; i < range_bound_glyph_count; ++i) { for (size_t i = 0; i < range_bound_glyph_count; ++i) {
auto copyable_width = m_font->is_fixed_width() auto copyable_width = m_font->is_fixed_width()
@ -1105,10 +1105,10 @@ void MainWidget::delete_selected_glyphs()
push_undo(action_text); push_undo(action_text);
size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height(); 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 rows = m_font->rows().slice(selection.start() * bytes_per_glyph, selection.size() * bytes_per_glyph);
auto* widths = m_font->widths() + selection.start(); auto widths = m_font->widths().slice(selection.start(), selection.size());
memset(rows, 0, bytes_per_glyph * selection.size()); memset(rows.data(), 0, bytes_per_glyph * selection.size());
memset(widths, 0, selection.size()); memset(widths.data(), 0, selection.size());
if (m_font->is_fixed_width()) if (m_font->is_fixed_width())
m_glyph_editor_present_checkbox->set_checked(false, GUI::AllowCallback::No); m_glyph_editor_present_checkbox->set_checked(false, GUI::AllowCallback::No);

View file

@ -25,10 +25,10 @@ public:
{ {
auto state = TRY(try_make_ref_counted<UndoSelection>(m_start, m_size, m_active_glyph, *m_font, m_glyph_map_widget)); auto state = TRY(try_make_ref_counted<UndoSelection>(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(); size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * font().glyph_height();
auto* rows = font().rows() + m_start * bytes_per_glyph; auto rows = font().rows().slice(m_start * bytes_per_glyph, m_size * bytes_per_glyph);
auto* widths = font().widths() + m_start; 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(&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)); TRY(state->m_restored_modified_state.try_ensure_capacity(m_size));
for (int glyph = m_start; glyph < m_start + m_size; ++glyph) for (int glyph = m_start; glyph < m_start + m_size; ++glyph)
@ -39,10 +39,10 @@ public:
void restore_state(UndoSelection const& state) void restore_state(UndoSelection const& state)
{ {
size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * font().glyph_height(); size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * font().glyph_height();
auto* rows = font().rows() + state.m_start * bytes_per_glyph; auto rows = font().rows().slice(state.m_start * bytes_per_glyph, state.m_size * bytes_per_glyph);
auto* widths = font().widths() + state.m_start; auto widths = font().widths().slice(state.m_start, state.m_size);
memcpy(rows, &state.m_data[0], bytes_per_glyph * state.m_size); memcpy(rows.data(), &state.m_data[0], bytes_per_glyph * state.m_size);
memcpy(widths, &state.m_data[bytes_per_glyph * state.m_size], 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) 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]); m_glyph_map_widget->set_glyph_modified(state.m_start + i, state.m_restored_modified_state[i]);

View file

@ -38,8 +38,8 @@ public:
~BitmapFont(); ~BitmapFont();
u8* rows() { return m_rows.data(); } Bytes rows() { return m_rows; }
u8* widths() { return m_glyph_widths.data(); } Span<u8> widths() { return m_glyph_widths; }
virtual float point_size() const override { return m_presentation_size; } virtual float point_size() const override { return m_presentation_size; }