FontEditor+ClipboardHistory: Use system-wide Clipboard

Glyphs can now be copied between editors.
This commit is contained in:
thankyouverycool 2021-04-07 12:07:46 -04:00 committed by Andreas Kling
parent ebf3ce7806
commit 08f11d01d9
Notes: sideshowbarker 2024-07-18 20:39:43 +09:00
3 changed files with 60 additions and 20 deletions

View file

@ -95,6 +95,18 @@ GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::Mode
builder.append("]"); builder.append("]");
return builder.to_string(); return builder.to_string();
} }
if (data_and_type.mime_type.starts_with("glyph/")) {
StringBuilder builder;
builder.append("[");
builder.append(data_and_type.metadata.get("width").value_or("?"));
builder.append("x");
builder.append(data_and_type.metadata.get("height").value_or("?"));
builder.append("] ");
builder.append("(");
builder.append(data_and_type.metadata.get("char").value_or(""));
builder.append(")");
return builder.to_string();
}
return "<...>"; return "<...>";
case Column::Type: case Column::Type:
return data_and_type.mime_type; return data_and_type.mime_type;

View file

@ -25,6 +25,8 @@
*/ */
#include "GlyphEditorWidget.h" #include "GlyphEditorWidget.h"
#include <AK/StringBuilder.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/Painter.h> #include <LibGUI/Painter.h>
#include <LibGfx/BitmapFont.h> #include <LibGfx/BitmapFont.h>
#include <LibGfx/Palette.h> #include <LibGfx/Palette.h>
@ -39,9 +41,7 @@ void GlyphEditorWidget::initialize(Gfx::BitmapFont& mutable_font)
return; return;
m_font = mutable_font; m_font = mutable_font;
set_relative_rect({ 0, 0, preferred_width(), preferred_height() }); set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
m_clipboard_font = m_font->clone(); m_glyph = 0;
m_clipboard_glyph = m_clipboard_font->glyph(0).glyph_bitmap();
clear_clipboard_glyph();
} }
void GlyphEditorWidget::set_glyph(int glyph) void GlyphEditorWidget::set_glyph(int glyph)
@ -71,31 +71,62 @@ void GlyphEditorWidget::cut_glyph()
void GlyphEditorWidget::copy_glyph() void GlyphEditorWidget::copy_glyph()
{ {
clear_clipboard_glyph();
auto bitmap = font().glyph(m_glyph).glyph_bitmap(); auto bitmap = font().glyph(m_glyph).glyph_bitmap();
for (int x = 0; x < bitmap.width(); x++) u8 bits[bitmap.width()][bitmap.height()];
for (int y = 0; y < bitmap.height(); y++) for (int x = 0; x < bitmap.width(); x++) {
m_clipboard_glyph.set_bit_at(x, y, bitmap.bit_at(x, y)); for (int y = 0; y < bitmap.height(); y++) {
bits[x][y] = bitmap.bit_at(x, y);
}
}
StringBuilder glyph_builder;
if (m_glyph < 128) {
glyph_builder.append(m_glyph);
} else {
glyph_builder.append(128 | 64 | (m_glyph / 64));
glyph_builder.append(128 | (m_glyph % 64));
}
HashMap<String, String> metadata;
metadata.set("char", glyph_builder.to_string());
metadata.set("width", String::format("%d", bitmap.width()));
metadata.set("height", String::format("%d", bitmap.height()));
auto data = ByteBuffer::copy(&bits[0], bitmap.width() * bitmap.height());
GUI::Clipboard::the().set_data(data, "glyph/x-fonteditor", metadata);
} }
void GlyphEditorWidget::paste_glyph() void GlyphEditorWidget::paste_glyph()
{ {
auto mime_type = GUI::Clipboard::the().mime_type();
if (!mime_type.starts_with("glyph/"))
return;
auto byte_buffer = GUI::Clipboard::the().data();
auto buffer_height = GUI::Clipboard::the().data_and_type().metadata.get("height").value().to_int();
auto buffer_width = GUI::Clipboard::the().data_and_type().metadata.get("width").value().to_int();
u8 bits[buffer_width.value()][buffer_height.value()];
int i = 0;
for (int x = 0; x < buffer_width.value(); x++) {
for (int y = 0; y < buffer_height.value(); y++) {
bits[x][y] = byte_buffer[i];
i++;
}
}
auto bitmap = font().glyph(m_glyph).glyph_bitmap(); auto bitmap = font().glyph(m_glyph).glyph_bitmap();
for (int x = 0; x < bitmap.width(); x++) for (int x = 0; x < min(bitmap.width(), buffer_width.value()); x++) {
for (int y = 0; y < bitmap.height(); y++) for (int y = 0; y < min(bitmap.height(), buffer_height.value()); y++) {
bitmap.set_bit_at(x, y, m_clipboard_glyph.bit_at(x, y)); if (bits[x][y])
bitmap.set_bit_at(x, y, bits[x][y]);
}
}
if (on_glyph_altered) if (on_glyph_altered)
on_glyph_altered(m_glyph); on_glyph_altered(m_glyph);
update(); update();
} }
void GlyphEditorWidget::clear_clipboard_glyph()
{
for (int x = 0; x < m_clipboard_glyph.width(); x++)
for (int y = 0; y < m_clipboard_glyph.height(); y++)
m_clipboard_glyph.set_bit_at(x, y, false);
}
void GlyphEditorWidget::paint_event(GUI::PaintEvent& event) void GlyphEditorWidget::paint_event(GUI::PaintEvent& event)
{ {
GUI::Frame::paint_event(event); GUI::Frame::paint_event(event);

View file

@ -44,7 +44,6 @@ public:
void copy_glyph(); void copy_glyph();
void paste_glyph(); void paste_glyph();
void delete_glyph(); void delete_glyph();
void clear_clipboard_glyph();
int preferred_width() const; int preferred_width() const;
int preferred_height() const; int preferred_height() const;
@ -63,8 +62,6 @@ private:
void draw_at_mouse(const GUI::MouseEvent&); void draw_at_mouse(const GUI::MouseEvent&);
RefPtr<Gfx::BitmapFont> m_font; RefPtr<Gfx::BitmapFont> m_font;
RefPtr<Gfx::Font> m_clipboard_font;
Gfx::GlyphBitmap m_clipboard_glyph;
int m_glyph { 0 }; int m_glyph { 0 };
int m_scale { 10 }; int m_scale { 10 };
}; };