summaryrefslogtreecommitdiff
path: root/Userland/Applications/FontEditor/UndoGlyph.h
blob: 849fe7bcf1f0fe0a3a53f024a7b8c684bc8edf63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibGUI/Command.h>
#include <LibGUI/UndoStack.h>
#include <LibGfx/BitmapFont.h>

class UndoGlyph : public RefCounted<UndoGlyph> {
public:
    explicit UndoGlyph(u32 const code_point, Gfx::BitmapFont const& font)
        : m_code_point(code_point)
        , m_font(font)
    {
    }
    NonnullRefPtr<UndoGlyph> save_state() const
    {
        auto state = adopt_ref(*new UndoGlyph(m_code_point, *m_font));
        auto glyph = font().glyph(m_code_point).glyph_bitmap();
        for (int x = 0; x < font().max_glyph_width(); x++)
            for (int y = 0; y < font().glyph_height(); y++)
                state->m_bits[x][y] = glyph.bit_at(x, y);
        state->m_width = glyph.width();
        return state;
    }
    void restore_state(UndoGlyph const& state) const
    {
        auto bitmap = font().glyph(state.m_code_point).glyph_bitmap();
        for (int x = 0; x < font().max_glyph_width(); x++)
            for (int y = 0; y < font().glyph_height(); y++)
                bitmap.set_bit_at(x, y, state.m_bits[x][y]);
        m_restored_width = state.m_width;
        m_restored_code_point = state.m_code_point;
    }
    void set_code_point(u32 code_point) { m_code_point = code_point; }
    void set_font(Gfx::BitmapFont& font) { m_font = font; }
    Gfx::BitmapFont const& font() const { return *m_font; }
    u8 restored_width() const { return m_restored_width; }
    u32 restored_code_point() const { return m_restored_code_point; }

private:
    u32 m_code_point { 0 };
    RefPtr<Gfx::BitmapFont> m_font;
    u8 m_bits[Gfx::GlyphBitmap::max_width()][Gfx::GlyphBitmap::max_height()] {};
    u8 m_width { 0 };
    mutable u8 m_restored_width { 0 };
    mutable u32 m_restored_code_point { 0 };
};

class GlyphUndoCommand : public GUI::Command {
public:
    GlyphUndoCommand(UndoGlyph& glyph)
        : m_undo_state(glyph.save_state())
        , m_undo_glyph(glyph)
    {
    }
    virtual void undo() override
    {
        if (!m_redo_state)
            m_redo_state = m_undo_state->save_state();
        m_undo_glyph.restore_state(*m_undo_state);
    }
    virtual void redo() override
    {
        m_undo_glyph.restore_state(*m_redo_state);
    }

private:
    NonnullRefPtr<UndoGlyph> m_undo_state;
    RefPtr<UndoGlyph> m_redo_state;
    UndoGlyph& m_undo_glyph;
};