Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "FontEditor.h"
28#include "GlyphEditorWidget.h"
29#include "GlyphMapWidget.h"
30#include "UI_FontEditorBottom.h"
31#include <AK/StringBuilder.h>
32#include <LibGUI/Button.h>
33#include <LibGUI/CheckBox.h>
34#include <LibGUI/GroupBox.h>
35#include <LibGUI/Label.h>
36#include <LibGUI/Painter.h>
37#include <LibGUI/SpinBox.h>
38#include <LibGUI/TextBox.h>
39#include <LibGfx/Font.h>
40#include <stdlib.h>
41
42FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edited_font)
43 : m_edited_font(move(edited_font))
44{
45 set_fill_with_background_color(true);
46
47 if (path.is_empty())
48 m_path = "/tmp/saved.font";
49 else
50 m_path = path;
51
52 m_glyph_map_widget = add<GlyphMapWidget>(*m_edited_font);
53 m_glyph_map_widget->move_to({ 90, 5 });
54
55 m_glyph_editor_widget = add<GlyphEditorWidget>(*m_edited_font);
56 m_glyph_editor_widget->move_to({ 5, 5 });
57
58 m_ui = make<UI_FontEditorBottom>();
59 add_child(*m_ui->main_widget);
60 m_ui->main_widget->set_relative_rect(5, 110, 380, 240);
61
62 m_ui->name_textbox->set_text(m_edited_font->name());
63 m_ui->name_textbox->on_change = [this] {
64 m_edited_font->set_name(m_ui->name_textbox->text());
65 };
66
67 m_ui->fixed_width_checkbox->set_text("Fixed width");
68 m_ui->fixed_width_checkbox->set_checked(m_edited_font->is_fixed_width());
69
70 m_ui->spacing_spinbox->set_value(m_edited_font->glyph_spacing());
71
72 m_ui->path_textbox->set_text(m_path);
73 m_ui->path_textbox->on_change = [this] {
74 m_path = m_ui->path_textbox->text();
75 };
76
77 m_ui->save_button->set_text("Save");
78 m_ui->save_button->on_click = [this] {
79 dbgprintf("write to file: '%s'\n", m_path.characters());
80 m_edited_font->write_to_file(m_path);
81 };
82
83 m_ui->quit_button->set_text("Quit");
84 m_ui->quit_button->on_click = [] {
85 exit(0);
86 };
87
88 m_ui->info_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
89
90 m_ui->demo_label_1->set_font(m_edited_font);
91 m_ui->demo_label_1->set_text("quick fox jumps nightly above wizard.");
92
93 m_ui->demo_label_2->set_font(m_edited_font);
94 m_ui->demo_label_2->set_text("QUICK FOX JUMPS NIGHTLY ABOVE WIZARD!");
95
96 auto update_demo = [this] {
97 m_ui->demo_label_1->update();
98 m_ui->demo_label_2->update();
99 };
100
101 m_glyph_editor_widget->on_glyph_altered = [this, update_demo](u8 glyph) {
102 m_glyph_map_widget->update_glyph(glyph);
103 update_demo();
104 };
105
106 m_glyph_map_widget->on_glyph_selected = [this](u8 glyph) {
107 m_glyph_editor_widget->set_glyph(glyph);
108 m_ui->width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
109 StringBuilder builder;
110 builder.appendf("0x%b (", glyph);
111 if (glyph < 128) {
112 builder.append(glyph);
113 } else {
114 builder.append(128 | 64 | (glyph / 64));
115 builder.append(128 | (glyph % 64));
116 }
117 builder.append(')');
118 m_ui->info_label->set_text(builder.to_string());
119 };
120
121 m_ui->fixed_width_checkbox->on_checked = [this, update_demo](bool checked) {
122 m_edited_font->set_fixed_width(checked);
123 m_ui->width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
124 m_glyph_editor_widget->update();
125 update_demo();
126 };
127
128 m_ui->width_spinbox->on_change = [this, update_demo](int value) {
129 m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), value);
130 m_glyph_editor_widget->update();
131 m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
132 update_demo();
133 };
134
135 m_ui->spacing_spinbox->on_change = [this, update_demo](int value) {
136 m_edited_font->set_glyph_spacing(value);
137 update_demo();
138 };
139
140 m_glyph_map_widget->set_selected_glyph('A');
141}
142
143FontEditorWidget::~FontEditorWidget()
144{
145}