Browse Source

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

Sam Atkins 1 năm trước cách đây
mục cha
commit
18dfc61280

+ 10 - 10
Userland/Applications/FontEditor/MainWidget.cpp

@@ -1026,12 +1026,12 @@ ErrorOr<void> 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<DeprecatedString, DeprecatedString> 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);

+ 7 - 7
Userland/Applications/FontEditor/UndoSelection.h

@@ -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));
         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]);

+ 2 - 2
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<u8> widths() { return m_glyph_widths; }
 
     virtual float point_size() const override { return m_presentation_size; }