mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 00:37:34 +00:00
Applications: FontEditor, relocate form items by fonts size
FontEditor widget rewritten for respect to the font size and added fixed width and height header values.
This commit is contained in:
parent
e68a08ad7c
commit
3874664752
9 changed files with 243 additions and 88 deletions
|
@ -27,75 +27,225 @@
|
|||
#include "FontEditor.h"
|
||||
#include "GlyphEditorWidget.h"
|
||||
#include "GlyphMapWidget.h"
|
||||
#include "UI_FontEditorBottom.h"
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/CheckBox.h>
|
||||
#include <LibGUI/GroupBox.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGUI/SpinBox.h>
|
||||
#include <LibGUI/TextBox.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Font.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edited_font)
|
||||
: m_edited_font(move(edited_font))
|
||||
, m_path(path)
|
||||
{
|
||||
set_fill_with_background_color(true);
|
||||
set_layout<GUI::VerticalBoxLayout>();
|
||||
|
||||
if (path.is_empty())
|
||||
m_path = "/tmp/saved.font";
|
||||
else
|
||||
m_path = path;
|
||||
// Top
|
||||
auto& main_container = add<GUI::Widget>();
|
||||
main_container.set_layout<GUI::HorizontalBoxLayout>();
|
||||
main_container.layout()->set_margins({ 4, 4, 4, 4 });
|
||||
main_container.set_background_role(Gfx::ColorRole::SyntaxKeyword);
|
||||
main_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
|
||||
|
||||
m_glyph_map_widget = add<GlyphMapWidget>(*m_edited_font);
|
||||
m_glyph_map_widget->move_to({ 90, 5 });
|
||||
// Top-Left Glyph Ediyor and info
|
||||
auto& editor_container = main_container.add<GUI::Widget>();
|
||||
editor_container.set_layout<GUI::VerticalBoxLayout>();
|
||||
editor_container.layout()->set_margins({ 4, 4, 4, 4 });
|
||||
editor_container.set_background_role(Gfx::ColorRole::SyntaxKeyword);
|
||||
editor_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
|
||||
|
||||
m_glyph_editor_widget = add<GlyphEditorWidget>(*m_edited_font);
|
||||
m_glyph_editor_widget->move_to({ 5, 5 });
|
||||
m_glyph_editor_widget = editor_container.add<GlyphEditorWidget>(*m_edited_font);
|
||||
m_glyph_editor_widget->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
||||
m_glyph_editor_widget->set_preferred_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height());
|
||||
|
||||
m_ui = make<UI_FontEditorBottom>();
|
||||
add_child(*m_ui->main_widget);
|
||||
m_ui->main_widget->set_relative_rect(5, 110, 380, 240);
|
||||
editor_container.set_preferred_size(m_glyph_editor_widget->preferred_width(), 0);
|
||||
|
||||
m_ui->name_textbox->set_text(m_edited_font->name());
|
||||
m_ui->name_textbox->on_change = [this] {
|
||||
m_edited_font->set_name(m_ui->name_textbox->text());
|
||||
auto& glyph_width_label = editor_container.add<GUI::Label>();
|
||||
glyph_width_label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
glyph_width_label.set_preferred_size(0, 22);
|
||||
glyph_width_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
glyph_width_label.set_text("Glyph width:");
|
||||
|
||||
auto& glyph_width_spinbox = editor_container.add<GUI::SpinBox>();
|
||||
glyph_width_spinbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
glyph_width_spinbox.set_preferred_size(0, 22);
|
||||
glyph_width_spinbox.set_min(0);
|
||||
glyph_width_spinbox.set_max(32);
|
||||
glyph_width_spinbox.set_value(0);
|
||||
glyph_width_spinbox.set_enabled(!m_edited_font->is_fixed_width());
|
||||
|
||||
auto& info_label = editor_container.add<GUI::Label>();
|
||||
info_label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
info_label.set_preferred_size(0, 22);
|
||||
info_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
info_label.set_text("info_label");
|
||||
|
||||
/// Top-Right glyph map and font meta data
|
||||
|
||||
auto& map_and_test_container = main_container.add<GUI::Widget>();
|
||||
map_and_test_container.set_layout<GUI::VerticalBoxLayout>();
|
||||
map_and_test_container.layout()->set_margins({ 4, 4, 4, 4 });
|
||||
map_and_test_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
|
||||
|
||||
m_glyph_map_widget = map_and_test_container.add<GlyphMapWidget>(*m_edited_font);
|
||||
m_glyph_map_widget->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
||||
m_glyph_map_widget->set_preferred_size(m_glyph_map_widget->preferred_width(), m_glyph_map_widget->preferred_height());
|
||||
|
||||
auto& font_mtest_group_box = map_and_test_container.add<GUI::GroupBox>();
|
||||
font_mtest_group_box.set_layout<GUI::VerticalBoxLayout>();
|
||||
font_mtest_group_box.layout()->set_margins({ 5, 15, 5, 5 });
|
||||
font_mtest_group_box.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
font_mtest_group_box.set_preferred_size(0, 2 * m_edited_font->glyph_height() + 50);
|
||||
font_mtest_group_box.set_title("Test");
|
||||
|
||||
auto& demo_label_1 = font_mtest_group_box.add<GUI::Label>();
|
||||
demo_label_1.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
|
||||
demo_label_1.set_font(m_edited_font);
|
||||
demo_label_1.set_text("quick fox jumps nightly above wizard.");
|
||||
|
||||
auto& demo_label_2 = font_mtest_group_box.add<GUI::Label>();
|
||||
demo_label_2.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
|
||||
demo_label_2.set_font(m_edited_font);
|
||||
demo_label_2.set_text("QUICK FOX JUMPS NIGHTLY ABOVE WIZARD!");
|
||||
|
||||
auto& font_metadata_group_box = map_and_test_container.add<GUI::GroupBox>();
|
||||
font_metadata_group_box.set_layout<GUI::VerticalBoxLayout>();
|
||||
font_metadata_group_box.layout()->set_margins({ 5, 15, 5, 5 });
|
||||
font_metadata_group_box.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
font_metadata_group_box.set_preferred_size(0, 145);
|
||||
font_metadata_group_box.set_title("Font metadata");
|
||||
|
||||
//// Name Row
|
||||
auto& namecontainer = font_metadata_group_box.add<GUI::Widget>();
|
||||
namecontainer.set_layout<GUI::HorizontalBoxLayout>();
|
||||
namecontainer.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
namecontainer.set_preferred_size(0, 22);
|
||||
|
||||
auto& name_label = namecontainer.add<GUI::Label>();
|
||||
name_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
|
||||
name_label.set_preferred_size(100, 0);
|
||||
name_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
name_label.set_text("Name:");
|
||||
|
||||
auto& name_textbox = namecontainer.add<GUI::TextBox>();
|
||||
name_textbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
|
||||
name_textbox.set_text(m_edited_font->name());
|
||||
name_textbox.on_change = [&] {
|
||||
m_edited_font->set_name(name_textbox.text());
|
||||
};
|
||||
|
||||
m_ui->fixed_width_checkbox->set_text("Fixed width");
|
||||
m_ui->fixed_width_checkbox->set_checked(m_edited_font->is_fixed_width());
|
||||
//// Glyph spacing Row
|
||||
auto& glyph_spacing_container = font_metadata_group_box.add<GUI::Widget>();
|
||||
glyph_spacing_container.set_layout<GUI::HorizontalBoxLayout>();
|
||||
glyph_spacing_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
glyph_spacing_container.set_preferred_size(0, 22);
|
||||
|
||||
m_ui->spacing_spinbox->set_value(m_edited_font->glyph_spacing());
|
||||
auto& glyph_spacing = glyph_spacing_container.add<GUI::Label>();
|
||||
glyph_spacing.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
|
||||
glyph_spacing.set_preferred_size(100, 0);
|
||||
glyph_spacing.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
glyph_spacing.set_text("Glyph spacing:");
|
||||
|
||||
m_ui->path_textbox->set_text(m_path);
|
||||
m_ui->path_textbox->on_change = [this] {
|
||||
m_path = m_ui->path_textbox->text();
|
||||
auto& spacing_spinbox = glyph_spacing_container.add<GUI::SpinBox>();
|
||||
spacing_spinbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
|
||||
spacing_spinbox.set_min(0);
|
||||
spacing_spinbox.set_max(255);
|
||||
spacing_spinbox.set_value(m_edited_font->glyph_spacing());
|
||||
|
||||
//// Glyph Height Row
|
||||
auto& glyph_height_container = font_metadata_group_box.add<GUI::Widget>();
|
||||
glyph_height_container.set_layout<GUI::HorizontalBoxLayout>();
|
||||
glyph_height_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
glyph_height_container.set_preferred_size(0, 22);
|
||||
|
||||
auto& glyph_height = glyph_height_container.add<GUI::Label>();
|
||||
glyph_height.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
|
||||
glyph_height.set_preferred_size(100, 0);
|
||||
glyph_height.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
glyph_height.set_text("Glyph height:");
|
||||
|
||||
auto& glyph_height_spinbox = glyph_height_container.add<GUI::SpinBox>();
|
||||
glyph_height_spinbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
|
||||
glyph_height_spinbox.set_min(0);
|
||||
glyph_height_spinbox.set_max(255);
|
||||
glyph_height_spinbox.set_value(m_edited_font->glyph_height());
|
||||
glyph_height_spinbox.set_enabled(false);
|
||||
|
||||
//// Glyph width Row
|
||||
auto& glyph_weight_container = font_metadata_group_box.add<GUI::Widget>();
|
||||
glyph_weight_container.set_layout<GUI::HorizontalBoxLayout>();
|
||||
glyph_weight_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
glyph_weight_container.set_preferred_size(0, 22);
|
||||
|
||||
auto& glyph_header_width_label = glyph_weight_container.add<GUI::Label>();
|
||||
glyph_header_width_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
|
||||
glyph_header_width_label.set_preferred_size(100, 0);
|
||||
glyph_header_width_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
glyph_header_width_label.set_text("Glyph width:");
|
||||
|
||||
auto& glyph_header_width_spinbox = glyph_weight_container.add<GUI::SpinBox>();
|
||||
glyph_header_width_spinbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
|
||||
glyph_header_width_spinbox.set_min(0);
|
||||
glyph_header_width_spinbox.set_max(255);
|
||||
glyph_header_width_spinbox.set_value(m_edited_font->glyph_fixed_width());
|
||||
glyph_header_width_spinbox.set_enabled(false);
|
||||
|
||||
//// Fixed checkbox Row
|
||||
auto& fixed_width_checkbox = font_metadata_group_box.add<GUI::CheckBox>();
|
||||
fixed_width_checkbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
fixed_width_checkbox.set_preferred_size(0, 22);
|
||||
fixed_width_checkbox.set_text("Fixed width");
|
||||
fixed_width_checkbox.set_checked(m_edited_font->is_fixed_width());
|
||||
|
||||
// Bottom
|
||||
auto& bottom_container = add<GUI::Widget>();
|
||||
bottom_container.set_layout<GUI::HorizontalBoxLayout>();
|
||||
bottom_container.layout()->set_margins({ 8, 0, 8, 8 });
|
||||
bottom_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
bottom_container.set_preferred_size(0, 32);
|
||||
|
||||
bottom_container.layout()->add_spacer();
|
||||
|
||||
auto& save_button = bottom_container.add<GUI::Button>();
|
||||
save_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
|
||||
save_button.set_preferred_size(80, 0);
|
||||
save_button.set_text("Save");
|
||||
save_button.on_click = [this] {
|
||||
auto ret_val = m_edited_font->write_to_file(m_path);
|
||||
if (!ret_val) {
|
||||
GUI::MessageBox::show("The font file could not be saved.", "Save failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
|
||||
}
|
||||
};
|
||||
|
||||
m_ui->save_button->set_text("Save");
|
||||
m_ui->save_button->on_click = [this] {
|
||||
dbgprintf("write to file: '%s'\n", m_path.characters());
|
||||
m_edited_font->write_to_file(m_path);
|
||||
};
|
||||
|
||||
m_ui->quit_button->set_text("Quit");
|
||||
m_ui->quit_button->on_click = [] {
|
||||
auto& quit_button = bottom_container.add<GUI::Button>();
|
||||
quit_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
|
||||
quit_button.set_preferred_size(80, 0);
|
||||
quit_button.set_text("Quit");
|
||||
quit_button.on_click = [this] {
|
||||
exit(0);
|
||||
};
|
||||
|
||||
m_ui->info_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||
// Event hanglers
|
||||
auto update_demo = [&] {
|
||||
demo_label_1.update();
|
||||
demo_label_2.update();
|
||||
};
|
||||
|
||||
m_ui->demo_label_1->set_font(m_edited_font);
|
||||
m_ui->demo_label_1->set_text("quick fox jumps nightly above wizard.");
|
||||
auto calculate_prefed_sizes = [&] {
|
||||
int right_site_width = m_edited_font->width("QUICK FOX JUMPS NIGHTLY ABOVE WIZARD!") + 20;
|
||||
right_site_width = max(right_site_width, m_glyph_map_widget->preferred_width());
|
||||
|
||||
m_ui->demo_label_2->set_font(m_edited_font);
|
||||
m_ui->demo_label_2->set_text("QUICK FOX JUMPS NIGHTLY ABOVE WIZARD!");
|
||||
|
||||
auto update_demo = [this] {
|
||||
m_ui->demo_label_1->update();
|
||||
m_ui->demo_label_2->update();
|
||||
m_preferred_width = m_glyph_editor_widget->width() + right_site_width + 20;
|
||||
m_preferred_height = m_glyph_map_widget->relative_rect().height() + 2 * m_edited_font->glyph_height() + 250;
|
||||
};
|
||||
|
||||
m_glyph_editor_widget->on_glyph_altered = [this, update_demo](u8 glyph) {
|
||||
|
@ -103,9 +253,9 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edite
|
|||
update_demo();
|
||||
};
|
||||
|
||||
m_glyph_map_widget->on_glyph_selected = [this](u8 glyph) {
|
||||
m_glyph_map_widget->on_glyph_selected = [&](size_t glyph) {
|
||||
m_glyph_editor_widget->set_glyph(glyph);
|
||||
m_ui->width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
|
||||
glyph_width_spinbox.set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
|
||||
StringBuilder builder;
|
||||
builder.appendf("0x%b (", glyph);
|
||||
if (glyph < 128) {
|
||||
|
@ -115,28 +265,31 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edite
|
|||
builder.append(128 | (glyph % 64));
|
||||
}
|
||||
builder.append(')');
|
||||
m_ui->info_label->set_text(builder.to_string());
|
||||
info_label.set_text(builder.to_string());
|
||||
};
|
||||
|
||||
m_ui->fixed_width_checkbox->on_checked = [this, update_demo](bool checked) {
|
||||
fixed_width_checkbox.on_checked = [&](bool checked) {
|
||||
m_edited_font->set_fixed_width(checked);
|
||||
m_ui->width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
|
||||
glyph_width_spinbox.set_enabled(!m_edited_font->is_fixed_width());
|
||||
glyph_width_spinbox.set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
|
||||
m_glyph_editor_widget->update();
|
||||
update_demo();
|
||||
};
|
||||
|
||||
m_ui->width_spinbox->on_change = [this, update_demo](int value) {
|
||||
glyph_width_spinbox.on_change = [this, update_demo](int value) {
|
||||
m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), value);
|
||||
m_glyph_editor_widget->update();
|
||||
m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
|
||||
update_demo();
|
||||
};
|
||||
|
||||
m_ui->spacing_spinbox->on_change = [this, update_demo](int value) {
|
||||
spacing_spinbox.on_change = [this, update_demo](int value) {
|
||||
m_edited_font->set_glyph_spacing(value);
|
||||
update_demo();
|
||||
};
|
||||
|
||||
// init widget
|
||||
calculate_prefed_sizes();
|
||||
m_glyph_map_widget->set_selected_glyph('A');
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue