Selaa lähdekoodia

FontEditor+ClipboardHistory: Use system-wide Clipboard

Glyphs can now be copied between editors.
thankyouverycool 4 vuotta sitten
vanhempi
commit
08f11d01d9

+ 12 - 0
Userland/Applets/ClipboardHistory/ClipboardHistoryModel.cpp

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

+ 48 - 17
Userland/Applications/FontEditor/GlyphEditorWidget.cpp

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

+ 0 - 3
Userland/Applications/FontEditor/GlyphEditorWidget.h

@@ -44,7 +44,6 @@ public:
     void copy_glyph();
     void paste_glyph();
     void delete_glyph();
-    void clear_clipboard_glyph();
 
     int preferred_width() const;
     int preferred_height() const;
@@ -63,8 +62,6 @@ private:
     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 };
 };