Skip to content

Commit

Permalink
FontEditor+ClipboardHistory: Use system-wide Clipboard
Browse files Browse the repository at this point in the history
Glyphs can now be copied between editors.
  • Loading branch information
thankyouverycool authored and awesomekling committed Apr 7, 2021
1 parent ebf3ce7 commit 08f11d0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
12 changes: 12 additions & 0 deletions Userland/Applets/ClipboardHistory/ClipboardHistoryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::Mode
builder.append("]");
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 "<...>";
case Column::Type:
return data_and_type.mime_type;
Expand Down
65 changes: 48 additions & 17 deletions Userland/Applications/FontEditor/GlyphEditorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/

#include "GlyphEditorWidget.h"
#include <AK/StringBuilder.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/Painter.h>
#include <LibGfx/BitmapFont.h>
#include <LibGfx/Palette.h>
Expand All @@ -39,9 +41,7 @@ void GlyphEditorWidget::initialize(Gfx::BitmapFont& mutable_font)
return;
m_font = mutable_font;
set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
m_clipboard_font = m_font->clone();
m_clipboard_glyph = m_clipboard_font->glyph(0).glyph_bitmap();
clear_clipboard_glyph();
m_glyph = 0;
}

void GlyphEditorWidget::set_glyph(int glyph)
Expand Down Expand Up @@ -71,31 +71,62 @@ void GlyphEditorWidget::cut_glyph()

void GlyphEditorWidget::copy_glyph()
{
clear_clipboard_glyph();
auto bitmap = font().glyph(m_glyph).glyph_bitmap();
for (int x = 0; x < bitmap.width(); x++)
for (int y = 0; y < bitmap.height(); y++)
m_clipboard_glyph.set_bit_at(x, y, bitmap.bit_at(x, y));
u8 bits[bitmap.width()][bitmap.height()];
for (int x = 0; x < bitmap.width(); x++) {
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()
{
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();
for (int x = 0; x < bitmap.width(); x++)
for (int y = 0; y < bitmap.height(); y++)
bitmap.set_bit_at(x, y, m_clipboard_glyph.bit_at(x, y));
for (int x = 0; x < min(bitmap.width(), buffer_width.value()); x++) {
for (int y = 0; y < min(bitmap.height(), buffer_height.value()); y++) {
if (bits[x][y])
bitmap.set_bit_at(x, y, bits[x][y]);
}
}
if (on_glyph_altered)
on_glyph_altered(m_glyph);
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)
{
GUI::Frame::paint_event(event);
Expand Down
3 changes: 0 additions & 3 deletions Userland/Applications/FontEditor/GlyphEditorWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class GlyphEditorWidget final : public GUI::Frame {
void copy_glyph();
void paste_glyph();
void delete_glyph();
void clear_clipboard_glyph();

int preferred_width() const;
int preferred_height() const;
Expand All @@ -63,8 +62,6 @@ class GlyphEditorWidget final : public GUI::Frame {
void draw_at_mouse(const GUI::MouseEvent&);

RefPtr<Gfx::BitmapFont> m_font;
RefPtr<Gfx::Font> m_clipboard_font;
Gfx::GlyphBitmap m_clipboard_glyph;
int m_glyph { 0 };
int m_scale { 10 };
};

0 comments on commit 08f11d0

Please sign in to comment.