From 7f6bf8c7c163b0338b055509c27168097f7aa8b4 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 2 Aug 2022 14:08:31 +0100 Subject: [PATCH] FontEditor: Store glyph modified state on the undo stack This means that if you modify a glyph, then undo that modification, we stop showing the glyph as modified. --- Userland/Applications/FontEditor/MainWidget.cpp | 2 +- .../Applications/FontEditor/UndoSelection.h | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp index 19fecb4f8a0..da51a642612 100644 --- a/Userland/Applications/FontEditor/MainWidget.cpp +++ b/Userland/Applications/FontEditor/MainWidget.cpp @@ -547,7 +547,7 @@ ErrorOr MainWidget::initialize(String const& path, RefPtr return {}; auto selection = m_glyph_map_widget->selection().normalized(); - m_undo_selection = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) UndoSelection(selection.start(), selection.size(), m_glyph_map_widget->active_glyph(), *edited_font))); + m_undo_selection = TRY(try_make_ref_counted(selection.start(), selection.size(), m_glyph_map_widget->active_glyph(), *edited_font, *m_glyph_map_widget)); m_undo_stack->clear(); m_path = path; diff --git a/Userland/Applications/FontEditor/UndoSelection.h b/Userland/Applications/FontEditor/UndoSelection.h index 6965c013d70..f9444e03e8f 100644 --- a/Userland/Applications/FontEditor/UndoSelection.h +++ b/Userland/Applications/FontEditor/UndoSelection.h @@ -7,26 +7,33 @@ #pragma once #include +#include #include #include class UndoSelection : public RefCounted { public: - explicit UndoSelection(int const start, int const size, u32 const active_glyph, Gfx::BitmapFont const& font) + explicit UndoSelection(int const start, int const size, u32 const active_glyph, Gfx::BitmapFont const& font, NonnullRefPtr glyph_map_widget) : m_start(start) , m_size(size) , m_active_glyph(active_glyph) , m_font(font) + , m_glyph_map_widget(move(glyph_map_widget)) { } ErrorOr> save_state() { - auto state = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) UndoSelection(m_start, m_size, m_active_glyph, *m_font))); + 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; 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_restored_modified_state.try_ensure_capacity(m_size)); + for (int glyph = m_start; glyph < m_start + m_size; ++glyph) + TRY(state->m_restored_modified_state.try_append(m_glyph_map_widget->glyph_is_modified(glyph))); + return state; } void restore_state(UndoSelection const& state) @@ -36,6 +43,10 @@ public: 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); + + 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_restored_active_glyph = state.m_active_glyph; m_restored_start = state.m_start; m_restored_size = state.m_size; @@ -55,7 +66,9 @@ private: int m_restored_start { 0 }; int m_restored_size { 0 }; u32 m_restored_active_glyph { 0 }; + Vector m_restored_modified_state; RefPtr m_font; + NonnullRefPtr m_glyph_map_widget; ByteBuffer m_data; };