Selaa lähdekoodia

Implement basic support for variable-width fonts.

Also add a nice new font called Katica. It's not used anywhere yet but
I'm definitely itching to start using it. :^)
Andreas Kling 6 vuotta sitten
vanhempi
commit
7f6c81d90f

+ 51 - 9
Applications/FontEditor/FontEditor.cpp

@@ -3,13 +3,14 @@
 #include <LibGUI/GButton.h>
 #include <LibGUI/GLabel.h>
 #include <LibGUI/GTextBox.h>
+#include <LibGUI/GCheckBox.h>
 
 FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_font, GWidget* parent)
     : GWidget(parent)
     , m_edited_font(move(edited_font))
 {
     if (path.is_empty())
-        m_path = "/saved.font";
+        m_path = "/tmp/saved.font";
     else
         m_path = path;
 
@@ -19,16 +20,28 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_
     m_glyph_editor_widget = new GlyphEditorWidget(*m_edited_font, this);
     m_glyph_editor_widget->move_to({ 5, 5 });
 
+    auto* fixed_width_checkbox = new GCheckBox(this);
+    fixed_width_checkbox->set_caption("Fixed width");
+    fixed_width_checkbox->set_checked(m_edited_font->is_fixed_width());
+    fixed_width_checkbox->set_relative_rect({ 5, 195, 100, 20 });
+
     m_name_textbox = new GTextBox(this);
-    m_name_textbox->set_relative_rect({ 5, 135, 100, 20 });
+    m_name_textbox->set_relative_rect({ 5, 220, 300, 20 });
     m_name_textbox->set_text(m_edited_font->name());
     m_name_textbox->on_change = [this] (GTextBox&) {
         m_edited_font->set_name(m_name_textbox->text());
     };
 
+    m_path_textbox = new GTextBox(this);
+    m_path_textbox->set_relative_rect({ 5, 245, 300, 20 });
+    m_path_textbox->set_text(m_path);
+    m_path_textbox->on_change = [this] (GTextBox&) {
+        m_path = m_path_textbox->text();
+    };
+
     auto* save_button = new GButton(this);
     save_button->set_caption("Save");
-    save_button->set_relative_rect({ 5, 170, 100, 20 });
+    save_button->set_relative_rect({ 5, 270, 100, 20 });
     save_button->on_click = [this] (GButton&) {
         dbgprintf("write to file: '%s'\n", m_path.characters());
         m_edited_font->write_to_file(m_path);
@@ -36,7 +49,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_
 
     auto* quit_button = new GButton(this);
     quit_button->set_caption("Quit");
-    quit_button->set_relative_rect({ 110, 170, 100, 20 });
+    quit_button->set_relative_rect({ 110, 270, 100, 20 });
     quit_button->on_click = [] (GButton&) {
         exit(0);
     };
@@ -45,6 +58,9 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_
     info_label->set_text_alignment(TextAlignment::CenterLeft);
     info_label->set_relative_rect({ 5, 110, 100, 20 });
 
+    auto* width_textbox = new GTextBox(this);
+    width_textbox->set_relative_rect({ 5, 135, 100, 20 });
+
     auto* demo_label_1 = new GLabel(this);
     demo_label_1->set_font(m_edited_font.copy_ref());
     demo_label_1->set_text("quick fox jumps nightly above wizard.");
@@ -55,17 +71,39 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_
     demo_label_2->set_text("QUICK FOX JUMPS NIGHTLY ABOVE WIZARD!");
     demo_label_2->set_relative_rect({ 110, 140, 300, 20 });
 
-    m_glyph_editor_widget->on_glyph_altered = [this, demo_label_1, demo_label_2] {
-        m_glyph_map_widget->update();
+    auto update_demo = [demo_label_1, demo_label_2] {
         demo_label_1->update();
         demo_label_2->update();
     };
 
-    m_glyph_map_widget->on_glyph_selected = [this, info_label] (byte glyph) {
+    m_glyph_editor_widget->on_glyph_altered = [this, update_demo] {
+        m_glyph_map_widget->update();
+        update_demo();
+    };
+
+    m_glyph_map_widget->on_glyph_selected = [this, info_label, width_textbox] (byte glyph) {
         m_glyph_editor_widget->set_glyph(glyph);
+        width_textbox->set_text(String::format("%u", m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph())));
         info_label->set_text(String::format("0x%b (%c)", glyph, glyph));
     };
 
+    fixed_width_checkbox->on_change = [this, width_textbox, update_demo] (GCheckBox&, bool is_checked) {
+        m_edited_font->set_fixed_width(is_checked);
+        width_textbox->set_text(String::format("%u", m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph())));
+        m_glyph_editor_widget->update();
+        update_demo();
+    };
+
+    width_textbox->on_change = [this, update_demo] (GTextBox& textbox) {
+        bool ok;
+        unsigned width = textbox.text().to_uint(ok);
+        if (ok) {
+            m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), width);
+            m_glyph_editor_widget->update();
+            update_demo();
+        }
+    };
+
     m_glyph_map_widget->set_selected_glyph('A');
 }
 
@@ -196,8 +234,12 @@ void GlyphEditorWidget::paint_event(GPaintEvent&)
     for (int y = 0; y < font().glyph_height(); ++y) {
         for (int x = 0; x < font().max_glyph_width(); ++x) {
             Rect rect { x * m_scale, y * m_scale, m_scale, m_scale };
-            if (bitmap.bit_at(x, y))
-                painter.fill_rect(rect, Color::Black);
+            if (x >= font().glyph_width(m_glyph)) {
+                painter.fill_rect(rect, Color::MidGray);
+            } else {
+                if (bitmap.bit_at(x, y))
+                    painter.fill_rect(rect, Color::Black);
+            }
         }
     }
 

+ 1 - 0
Applications/FontEditor/FontEditor.h

@@ -18,6 +18,7 @@ private:
     GlyphMapWidget* m_glyph_map_widget { nullptr };
     GlyphEditorWidget* m_glyph_editor_widget { nullptr };
     GTextBox* m_name_textbox { nullptr };
+    GTextBox* m_path_textbox { nullptr };
 
     String m_path;
 };

+ 1 - 2
Applications/FontEditor/main.cpp

@@ -26,9 +26,8 @@ int main(int argc, char** argv)
 
     auto* window = new GWindow;
     window->set_title("FontEditor");
-    window->set_rect({ 50, 50, 420, 200 });
+    window->set_rect({ 50, 50, 420, 300 });
     auto* font_editor = new FontEditorWidget(path, move(edited_font));
-    font_editor->set_relative_rect({ 0, 0, 420, 200 });
     window->set_main_widget(font_editor);
     window->set_should_exit_app_on_close(true);
     window->show();

+ 2 - 0
LibGUI/GCheckBox.cpp

@@ -47,6 +47,8 @@ void GCheckBox::set_checked(bool b)
     if (m_checked == b)
         return;
     m_checked = b;
+    if (on_change)
+        on_change(*this, b);
     update();
 }
 

+ 3 - 0
LibGUI/GCheckBox.h

@@ -2,6 +2,7 @@
 
 #include "GWidget.h"
 #include <AK/AKString.h>
+#include <AK/Function.h>
 
 class GCheckBox final : public GWidget {
 public:
@@ -14,6 +15,8 @@ public:
     bool is_checked() const { return m_checked; }
     void set_checked(bool);
 
+    Function<void(GCheckBox&, bool)> on_change;
+
 private:
     virtual void paint_event(GPaintEvent&) override;
     virtual void mousedown_event(GMouseEvent&) override;

+ 26 - 8
SharedGraphics/Font.cpp

@@ -17,7 +17,8 @@ struct [[gnu::packed]] FontFileHeader {
     byte glyph_width;
     byte glyph_height;
     byte type;
-    byte unused[7];
+    byte is_variable_width;
+    byte unused[6];
     char name[64];
 };
 
@@ -47,23 +48,33 @@ RetainPtr<Font> Font::clone() const
     // FIXME: This is leaked!
     auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * 256));
     memcpy(new_rows, m_rows, bytes_per_glyph * 256);
-    return adopt(*new Font(m_name, new_rows, m_glyph_width, m_glyph_height));
+    auto* new_widths = static_cast<byte*>(kmalloc(256));
+    if (m_glyph_widths)
+        memcpy(new_widths, m_glyph_widths, 256);
+    else
+        memset(new_widths, m_glyph_width, 256);
+    return adopt(*new Font(m_name, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height));
 }
 
-Font::Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_height)
+Font::Font(const String& name, unsigned* rows, byte* widths, bool is_fixed_width, byte glyph_width, byte glyph_height)
     : m_name(name)
     , m_rows(rows)
+    , m_glyph_widths(widths)
     , m_glyph_width(glyph_width)
     , m_glyph_height(glyph_height)
     , m_min_glyph_width(glyph_width)
     , m_max_glyph_width(glyph_width)
+    , m_fixed_width(is_fixed_width)
 {
-    m_fixed_width = true;
     if (!m_fixed_width) {
+        byte maximum = 0;
         byte minimum = 255;
-        for (int i = 0; i < 256; ++i)
+        for (int i = 0; i < 256; ++i) {
             minimum = min(minimum, m_glyph_widths[i]);
+            maximum = max(maximum, m_glyph_widths[i]);
+        }
         m_min_glyph_width = minimum;
+        m_max_glyph_width = maximum;
     }
 }
 
@@ -87,8 +98,13 @@ RetainPtr<Font> Font::load_from_memory(const byte* data)
         return nullptr;
     }
 
+    size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
+
     auto* rows = (unsigned*)(data + sizeof(FontFileHeader));
-    return adopt(*new Font(String(header.name), rows, header.glyph_width, header.glyph_height));
+    byte* widths = nullptr;
+    if (header.is_variable_width)
+        widths = (byte*)(rows) + 256 * bytes_per_glyph;
+    return adopt(*new Font(String(header.name), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height));
 }
 
 RetainPtr<Font> Font::load_from_file(const String& path)
@@ -129,15 +145,17 @@ bool Font::write_to_file(const String& path)
     header.glyph_width = m_glyph_width;
     header.glyph_height = m_glyph_height;
     header.type = 0;
+    header.is_variable_width = !m_fixed_width;
     memcpy(header.name, m_name.characters(), min(m_name.length(), 63));
 
     size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
 
-    auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (256 * bytes_per_glyph));
+    auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (256 * bytes_per_glyph) + 256);
     BufferStream stream(buffer);
 
     stream << ByteBuffer::wrap((byte*)&header, sizeof(FontFileHeader));
     stream << ByteBuffer::wrap((byte*)m_rows, (256 * bytes_per_glyph));
+    stream << ByteBuffer::wrap((byte*)m_glyph_widths, 256);
 
     ASSERT(stream.at_end());
     ssize_t nwritten = write(fd, buffer.pointer(), buffer.size());
@@ -154,6 +172,6 @@ int Font::width(const String& string) const
 
     int width = 0;
     for (int i = 0; i < string.length(); ++i)
-        width += glyph_width(string[i]);
+        width += glyph_width(string[i]) + 1;
     return width;
 }

+ 9 - 1
SharedGraphics/Font.h

@@ -58,15 +58,23 @@ public:
     byte glyph_height() const { return m_glyph_height; }
     byte min_glyph_width() const { return m_min_glyph_width; }
     byte max_glyph_width() const { return m_max_glyph_width; }
+    byte glyph_spacing() const { return m_fixed_width ? 0 : 1; }
     int width(const String& string) const;
 
     String name() const { return m_name; }
     void set_name(const String& name) { m_name = name; }
 
     bool is_fixed_width() const { return m_fixed_width; }
+    void set_fixed_width(bool b) { m_fixed_width = b; }
+
+    void set_glyph_width(char ch, byte width)
+    {
+        ASSERT(m_glyph_widths);
+        m_glyph_widths[(byte)ch] = width;
+    }
 
 private:
-    Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_height);
+    Font(const String& name, unsigned* rows, byte* widths, bool is_fixed_width, byte glyph_width, byte glyph_height);
 
     String m_name;
 

+ 2 - 2
SharedGraphics/Painter.cpp

@@ -342,7 +342,7 @@ void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alig
         ASSERT_NOT_REACHED();
     }
 
-    int space_width = font().glyph_width(' ');
+    int space_width = font().glyph_width(' ') + font().glyph_spacing();
     for (ssize_t i = 0; i < text.length(); ++i) {
         byte ch = text[i];
         if (ch == ' ') {
@@ -350,7 +350,7 @@ void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alig
             continue;
         }
         draw_glyph(point, ch, color);
-        point.move_by(font().glyph_width(ch), 0);
+        point.move_by(font().glyph_width(ch) + font().glyph_spacing(), 0);
     }
 }