123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2022, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "MainWidget.h"
- #include "GlyphEditorWidget.h"
- #include "NewFontDialog.h"
- #include <AK/Array.h>
- #include <AK/ScopeGuard.h>
- #include <AK/String.h>
- #include <AK/StringBuilder.h>
- #include <AK/StringUtils.h>
- #include <Applications/FontEditor/FontEditorWindowGML.h>
- #include <Applications/FontEditor/FontPreviewWindowGML.h>
- #include <LibConfig/Client.h>
- #include <LibDesktop/Launcher.h>
- #include <LibFileSystemAccessClient/Client.h>
- #include <LibGUI/Action.h>
- #include <LibGUI/Application.h>
- #include <LibGUI/Button.h>
- #include <LibGUI/CheckBox.h>
- #include <LibGUI/Clipboard.h>
- #include <LibGUI/ComboBox.h>
- #include <LibGUI/FilePicker.h>
- #include <LibGUI/GlyphMapWidget.h>
- #include <LibGUI/GroupBox.h>
- #include <LibGUI/InputBox.h>
- #include <LibGUI/ItemListModel.h>
- #include <LibGUI/Label.h>
- #include <LibGUI/ListView.h>
- #include <LibGUI/Menu.h>
- #include <LibGUI/Menubar.h>
- #include <LibGUI/MessageBox.h>
- #include <LibGUI/SpinBox.h>
- #include <LibGUI/Statusbar.h>
- #include <LibGUI/TextBox.h>
- #include <LibGUI/ToolbarContainer.h>
- #include <LibGUI/Window.h>
- #include <LibGfx/Font/BitmapFont.h>
- #include <LibGfx/Font/Emoji.h>
- #include <LibGfx/Font/FontStyleMapping.h>
- #include <LibGfx/TextDirection.h>
- #include <LibUnicode/CharacterTypes.h>
- namespace FontEditor {
- Resources g_resources;
- static constexpr Array pangrams = {
- "quick fox jumps nightly above wizard"sv,
- "five quacking zephyrs jolt my wax bed"sv,
- "pack my box with five dozen liquor jugs"sv,
- "quick brown fox jumps over the lazy dog"sv,
- "waxy and quivering jocks fumble the pizza"sv,
- "~#:[@_1%]*{$2.3}/4^(5'6\")-&|7+8!=<9,0\\>?;"sv,
- "byxfjärmat föl gick på duvshowen"sv,
- " "sv,
- "float Fox.quick(h){ is_brown && it_jumps_over(doges.lazy) }"sv,
- "<fox color=\"brown\" speed=\"quick\" jumps=\"over\">lazy dog</fox>"sv
- };
- ErrorOr<NonnullRefPtr<MainWidget>> MainWidget::try_create()
- {
- auto main_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MainWidget()));
- TRY(main_widget->create_widgets());
- TRY(main_widget->create_actions());
- TRY(main_widget->create_models());
- TRY(main_widget->create_toolbars());
- TRY(main_widget->create_undo_stack());
- return main_widget;
- }
- ErrorOr<RefPtr<GUI::Window>> MainWidget::create_preview_window()
- {
- auto window = TRY(GUI::Window::try_create(this));
- window->set_window_mode(GUI::WindowMode::RenderAbove);
- window->set_title("Preview");
- window->resize(400, 150);
- window->center_within(*this->window());
- auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
- TRY(main_widget->load_from_gml(font_preview_window_gml));
- m_preview_label = find_descendant_of_type_named<GUI::Label>("preview_label");
- m_preview_label->set_font(m_font);
- m_preview_textbox = find_descendant_of_type_named<GUI::TextBox>("preview_textbox");
- m_preview_textbox->on_change = [this] {
- auto maybe_preview = [](StringView text) -> ErrorOr<String> {
- return TRY(String::formatted("{}\n{}", text, TRY(Unicode::to_unicode_uppercase_full(text))));
- }(m_preview_textbox->text());
- if (maybe_preview.is_error())
- return show_error(maybe_preview.release_error(), "Formatting preview text failed"sv);
- m_preview_label->set_text(maybe_preview.release_value());
- };
- m_preview_textbox->set_text(pangrams[0]);
- auto& reload_button = *find_descendant_of_type_named<GUI::Button>("reload_button");
- reload_button.on_click = [this](auto) {
- static size_t i = 1;
- if (i >= pangrams.size())
- i = 0;
- m_preview_textbox->set_text(pangrams[i]);
- i++;
- };
- return window;
- }
- ErrorOr<void> MainWidget::create_actions()
- {
- m_new_action = GUI::Action::create("&New Font...", { Mod_Ctrl, Key_N }, g_resources.new_font, [this](auto&) {
- if (!request_close())
- return;
- auto new_font_wizard = NewFontDialog::construct(window());
- if (new_font_wizard->exec() != GUI::Dialog::ExecResult::OK)
- return;
- auto maybe_font = new_font_wizard->create_font();
- if (maybe_font.is_error())
- return show_error(maybe_font.release_error(), "Creating new font failed"sv);
- if (auto result = initialize({}, move(maybe_font.value())); result.is_error())
- show_error(result.release_error(), "Initializing new font failed"sv);
- });
- m_new_action->set_status_tip("Create a new font");
- m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
- if (!request_close())
- return;
- auto response = FileSystemAccessClient::Client::the().open_file(window(), "Open font file", "/res/fonts"sv);
- if (response.is_error())
- return;
- auto file = response.release_value();
- if (auto result = open_file(file.filename(), file.release_stream()); result.is_error())
- show_error(result.release_error(), "Opening"sv, file.filename());
- });
- m_save_action = GUI::CommonActions::make_save_action([this](auto&) {
- if (m_path.is_empty())
- return m_save_as_action->activate();
- auto response = FileSystemAccessClient::Client::the().request_file(window(), m_path.to_deprecated_string(), Core::File::OpenMode::Truncate | Core::File::OpenMode::Write);
- if (response.is_error())
- return;
- auto file = response.release_value();
- if (auto result = save_file(m_path, file.release_stream()); result.is_error())
- show_error(result.release_error(), "Saving"sv, m_path);
- });
- m_save_as_action = GUI::CommonActions::make_save_as_action([this](auto&) {
- auto default_path = LexicalPath(m_path.is_empty() ? "Untitled.font"sv : m_path);
- auto response = FileSystemAccessClient::Client::the().save_file(window(), default_path.title(), default_path.extension());
- if (response.is_error())
- return;
- auto file = response.release_value();
- if (auto result = save_file(file.filename(), file.release_stream()); result.is_error())
- show_error(result.release_error(), "Saving"sv, file.filename());
- else
- GUI::Application::the()->set_most_recently_open_file(file.filename());
- });
- m_cut_action = GUI::CommonActions::make_cut_action([this](auto&) {
- if (auto result = cut_selected_glyphs(); result.is_error())
- show_error(result.release_error(), "Cutting selection failed"sv);
- });
- m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
- if (auto result = copy_selected_glyphs(); result.is_error())
- show_error(result.release_error(), "Copying selection failed"sv);
- });
- m_paste_action = GUI::CommonActions::make_paste_action([this](auto&) {
- paste_glyphs();
- });
- m_paste_action->set_enabled(GUI::Clipboard::the().fetch_mime_type() == "glyph/x-fonteditor");
- GUI::Clipboard::the().on_change = [this](DeprecatedString const& data_type) {
- m_paste_action->set_enabled(data_type == "glyph/x-fonteditor");
- };
- m_delete_action = GUI::CommonActions::make_delete_action([this](auto&) {
- delete_selected_glyphs();
- });
- m_undo_action = GUI::CommonActions::make_undo_action([this](auto&) {
- undo();
- });
- m_undo_action->set_enabled(false);
- m_redo_action = GUI::CommonActions::make_redo_action([this](auto&) {
- redo();
- });
- m_redo_action->set_enabled(false);
- m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
- m_glyph_map_widget->set_selection(m_range.first, m_range.last - m_range.first + 1);
- m_glyph_map_widget->update();
- auto selection = m_glyph_map_widget->selection().normalized();
- m_undo_selection->set_start(selection.start());
- m_undo_selection->set_size(selection.size());
- update_statusbar();
- });
- m_open_preview_action = GUI::Action::create("&Preview Font", { Mod_Ctrl, Key_P }, g_resources.preview_font, [this](auto&) {
- if (!m_font_preview_window) {
- if (auto maybe_window = create_preview_window(); maybe_window.is_error())
- show_error(maybe_window.release_error(), "Creating preview window failed"sv);
- else
- m_font_preview_window = maybe_window.release_value();
- }
- if (m_font_preview_window)
- m_font_preview_window->show();
- });
- m_open_preview_action->set_status_tip("Preview the current font");
- bool show_metadata = Config::read_bool("FontEditor"sv, "Layout"sv, "ShowMetadata"sv, true);
- m_font_metadata_groupbox->set_visible(show_metadata);
- m_show_metadata_action = GUI::Action::create_checkable("Font &Metadata", { Mod_Ctrl, Key_M }, [this](auto& action) {
- m_font_metadata_groupbox->set_visible(action.is_checked());
- Config::write_bool("FontEditor"sv, "Layout"sv, "ShowMetadata"sv, action.is_checked());
- });
- m_show_metadata_action->set_checked(show_metadata);
- m_show_metadata_action->set_status_tip("Show or hide metadata about the current font");
- bool show_unicode_blocks = Config::read_bool("FontEditor"sv, "Layout"sv, "ShowUnicodeBlocks"sv, true);
- m_unicode_block_container->set_visible(show_unicode_blocks);
- m_show_unicode_blocks_action = GUI::Action::create_checkable("&Unicode Blocks", { Mod_Ctrl, Key_U }, [this](auto& action) {
- m_unicode_block_container->set_visible(action.is_checked());
- if (action.is_checked())
- m_search_textbox->set_focus(m_initialized);
- else
- m_glyph_map_widget->set_focus(m_initialized);
- Config::write_bool("FontEditor"sv, "Layout"sv, "ShowUnicodeBlocks"sv, action.is_checked());
- });
- m_show_unicode_blocks_action->set_checked(show_unicode_blocks);
- m_show_unicode_blocks_action->set_status_tip("Show or hide the Unicode block list");
- bool show_toolbar = Config::read_bool("FontEditor"sv, "Layout"sv, "ShowToolbar"sv, true);
- m_toolbar_container->set_visible(show_toolbar);
- m_show_toolbar_action = GUI::Action::create_checkable("&Toolbar", [this](auto& action) {
- m_toolbar_container->set_visible(action.is_checked());
- Config::write_bool("FontEditor"sv, "Layout"sv, "ShowToolbar"sv, action.is_checked());
- });
- m_show_toolbar_action->set_checked(show_toolbar);
- m_show_toolbar_action->set_status_tip("Show or hide the toolbar");
- bool show_statusbar = Config::read_bool("FontEditor"sv, "Layout"sv, "ShowStatusbar"sv, true);
- m_statusbar->set_visible(show_statusbar);
- m_show_statusbar_action = GUI::Action::create_checkable("&Status Bar", [this](auto& action) {
- m_statusbar->set_visible(action.is_checked());
- update_statusbar();
- Config::write_bool("FontEditor"sv, "Layout"sv, "ShowStatusbar"sv, action.is_checked());
- });
- m_show_statusbar_action->set_checked(show_statusbar);
- m_show_statusbar_action->set_status_tip("Show or hide the status bar");
- bool highlight_modifications = Config::read_bool("FontEditor"sv, "GlyphMap"sv, "HighlightModifications"sv, true);
- m_glyph_map_widget->set_highlight_modifications(highlight_modifications);
- m_highlight_modifications_action = GUI::Action::create_checkable("&Highlight Modifications", { Mod_Ctrl, Key_H }, [this](auto& action) {
- m_glyph_map_widget->set_highlight_modifications(action.is_checked());
- Config::write_bool("FontEditor"sv, "GlyphMap"sv, "HighlightModifications"sv, action.is_checked());
- });
- m_highlight_modifications_action->set_checked(highlight_modifications);
- m_highlight_modifications_action->set_status_tip("Show or hide highlights on modified glyphs. (Green = New, Blue = Modified, Red = Deleted)");
- bool show_system_emoji = Config::read_bool("FontEditor"sv, "GlyphMap"sv, "ShowSystemEmoji"sv, true);
- m_glyph_map_widget->set_show_system_emoji(show_system_emoji);
- m_show_system_emoji_action = GUI::Action::create_checkable("System &Emoji", { Mod_Ctrl, Key_E }, [this](auto& action) {
- m_glyph_map_widget->set_show_system_emoji(action.is_checked());
- Config::write_bool("FontEditor"sv, "GlyphMap"sv, "ShowSystemEmoji"sv, action.is_checked());
- });
- m_show_system_emoji_action->set_checked(show_system_emoji);
- m_show_system_emoji_action->set_status_tip("Show or hide system emoji");
- m_go_to_glyph_action = GUI::Action::create("&Go to Glyph...", { Mod_Ctrl, Key_G }, g_resources.go_to_glyph, [this](auto&) {
- String input;
- auto result = GUI::InputBox::try_show(window(), input, {}, "Go to glyph"sv, GUI::InputType::NonemptyText, "Hexadecimal"sv);
- if (!result.is_error() && result.value() == GUI::InputBox::ExecResult::OK) {
- auto maybe_code_point = AK::StringUtils::convert_to_uint_from_hex(input);
- if (!maybe_code_point.has_value())
- return;
- auto code_point = maybe_code_point.value();
- code_point = clamp(code_point, m_range.first, m_range.last);
- m_glyph_map_widget->set_focus(true);
- m_glyph_map_widget->set_active_glyph(code_point);
- m_glyph_map_widget->scroll_to_glyph(code_point);
- }
- });
- m_go_to_glyph_action->set_status_tip("Go to the specified code point");
- m_previous_glyph_action = GUI::Action::create("Pre&vious Glyph", { Mod_Alt, Key_Left }, g_resources.previous_glyph, [this](auto&) {
- m_glyph_map_widget->select_previous_existing_glyph();
- });
- m_previous_glyph_action->set_status_tip("Seek the previous visible glyph");
- m_next_glyph_action = GUI::Action::create("&Next Glyph", { Mod_Alt, Key_Right }, g_resources.next_glyph, [this](auto&) {
- m_glyph_map_widget->select_next_existing_glyph();
- });
- m_next_glyph_action->set_status_tip("Seek the next visible glyph");
- i32 scale = Config::read_i32("FontEditor"sv, "GlyphEditor"sv, "Scale"sv, 10);
- m_glyph_editor_widget->set_scale(scale);
- m_scale_five_action = GUI::Action::create_checkable("500%", { Mod_Ctrl, Key_1 }, [this](auto&) {
- set_scale_and_save(5);
- });
- m_scale_five_action->set_checked(scale == 5);
- m_scale_five_action->set_status_tip("Scale the editor in proportion to the current font");
- m_scale_ten_action = GUI::Action::create_checkable("1000%", { Mod_Ctrl, Key_2 }, [this](auto&) {
- set_scale_and_save(10);
- });
- m_scale_ten_action->set_checked(scale == 10);
- m_scale_ten_action->set_status_tip("Scale the editor in proportion to the current font");
- m_scale_fifteen_action = GUI::Action::create_checkable("1500%", { Mod_Ctrl, Key_3 }, [this](auto&) {
- set_scale_and_save(15);
- });
- m_scale_fifteen_action->set_checked(scale == 15);
- m_scale_fifteen_action->set_status_tip("Scale the editor in proportion to the current font");
- m_glyph_editor_scale_actions.add_action(*m_scale_five_action);
- m_glyph_editor_scale_actions.add_action(*m_scale_ten_action);
- m_glyph_editor_scale_actions.add_action(*m_scale_fifteen_action);
- m_glyph_editor_scale_actions.set_exclusive(true);
- m_paint_glyph_action = GUI::Action::create_checkable("Paint Glyph", { Mod_Ctrl, KeyCode::Key_J }, g_resources.paint_glyph, [this](auto&) {
- m_glyph_editor_widget->set_mode(GlyphEditorWidget::Paint);
- });
- m_paint_glyph_action->set_checked(true);
- m_move_glyph_action = GUI::Action::create_checkable("Move Glyph", { Mod_Ctrl, KeyCode::Key_K }, g_resources.move_glyph, [this](auto&) {
- m_glyph_editor_widget->set_mode(GlyphEditorWidget::Move);
- });
- m_glyph_tool_actions.add_action(*m_paint_glyph_action);
- m_glyph_tool_actions.add_action(*m_move_glyph_action);
- m_glyph_tool_actions.set_exclusive(true);
- m_rotate_counterclockwise_action = GUI::CommonActions::make_rotate_counterclockwise_action([this](auto&) {
- m_glyph_editor_widget->rotate_90(Gfx::RotationDirection::CounterClockwise);
- });
- m_rotate_clockwise_action = GUI::CommonActions::make_rotate_clockwise_action([this](auto&) {
- m_glyph_editor_widget->rotate_90(Gfx::RotationDirection::Clockwise);
- });
- m_flip_horizontal_action = GUI::Action::create("Flip Horizontally", { Mod_Ctrl | Mod_Shift, Key_Q }, g_resources.flip_horizontally, [this](auto&) {
- m_glyph_editor_widget->flip(Gfx::Orientation::Horizontal);
- });
- m_flip_vertical_action = GUI::Action::create("Flip Vertically", { Mod_Ctrl | Mod_Shift, Key_W }, g_resources.flip_vertically, [this](auto&) {
- m_glyph_editor_widget->flip(Gfx::Orientation::Vertical);
- });
- m_copy_text_action = GUI::Action::create("Copy as Te&xt", { Mod_Ctrl, Key_T }, g_resources.copy_as_text, [this](auto&) {
- StringBuilder builder;
- auto selection = m_glyph_map_widget->selection().normalized();
- for (auto code_point = selection.start(); code_point < selection.start() + selection.size(); ++code_point) {
- if (!m_glyph_map_widget->font().contains_glyph(code_point))
- continue;
- builder.append_code_point(code_point);
- }
- GUI::Clipboard::the().set_plain_text(builder.to_deprecated_string());
- });
- m_copy_text_action->set_status_tip("Copy to clipboard as text");
- return {};
- }
- ErrorOr<void> MainWidget::create_toolbars()
- {
- auto& toolbar = *find_descendant_of_type_named<GUI::Toolbar>("toolbar");
- (void)TRY(toolbar.try_add_action(*m_new_action));
- (void)TRY(toolbar.try_add_action(*m_open_action));
- (void)TRY(toolbar.try_add_action(*m_save_action));
- TRY(toolbar.try_add_separator());
- (void)TRY(toolbar.try_add_action(*m_cut_action));
- (void)TRY(toolbar.try_add_action(*m_copy_action));
- (void)TRY(toolbar.try_add_action(*m_paste_action));
- (void)TRY(toolbar.try_add_action(*m_delete_action));
- TRY(toolbar.try_add_separator());
- (void)TRY(toolbar.try_add_action(*m_undo_action));
- (void)TRY(toolbar.try_add_action(*m_redo_action));
- TRY(toolbar.try_add_separator());
- (void)TRY(toolbar.try_add_action(*m_open_preview_action));
- TRY(toolbar.try_add_separator());
- (void)TRY(toolbar.try_add_action(*m_previous_glyph_action));
- (void)TRY(toolbar.try_add_action(*m_next_glyph_action));
- (void)TRY(toolbar.try_add_action(*m_go_to_glyph_action));
- auto& glyph_transform_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("glyph_transform_toolbar");
- (void)TRY(glyph_transform_toolbar.try_add_action(*m_flip_horizontal_action));
- (void)TRY(glyph_transform_toolbar.try_add_action(*m_flip_vertical_action));
- (void)TRY(glyph_transform_toolbar.try_add_action(*m_rotate_counterclockwise_action));
- (void)TRY(glyph_transform_toolbar.try_add_action(*m_rotate_clockwise_action));
- auto& glyph_mode_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("glyph_mode_toolbar");
- (void)TRY(glyph_mode_toolbar.try_add_action(*m_paint_glyph_action));
- (void)TRY(glyph_mode_toolbar.try_add_action(*m_move_glyph_action));
- return {};
- }
- ErrorOr<void> MainWidget::create_models()
- {
- TRY(m_font_slope_list.try_ensure_capacity(Gfx::font_slope_names.size()));
- for (auto& it : Gfx::font_slope_names)
- m_font_slope_list.unchecked_append(TRY(String::from_utf8(it.name)));
- m_slope_combobox->set_model(TRY(GUI::ItemListModel<String>::try_create(m_font_slope_list)));
- TRY(m_font_weight_list.try_ensure_capacity(Gfx::font_weight_names.size()));
- for (auto& it : Gfx::font_weight_names)
- m_font_weight_list.unchecked_append(TRY(String::from_utf8(it.name)));
- m_weight_combobox->set_model(TRY(GUI::ItemListModel<String>::try_create(m_font_weight_list)));
- auto unicode_blocks = Unicode::block_display_names();
- TRY(m_unicode_block_list.try_ensure_capacity(unicode_blocks.size() + 1));
- m_unicode_block_list.unchecked_append(TRY(String::from_utf8("Show All"sv)));
- for (auto& block : unicode_blocks)
- m_unicode_block_list.unchecked_append(TRY(String::from_utf8(block.display_name)));
- m_unicode_block_model = TRY(GUI::ItemListModel<String>::try_create(m_unicode_block_list));
- m_filter_model = TRY(GUI::FilteringProxyModel::create(*m_unicode_block_model));
- m_filter_model->set_filter_term(""sv);
- m_unicode_block_listview = find_descendant_of_type_named<GUI::ListView>("unicode_block_listview");
- m_unicode_block_listview->on_selection_change = [this, unicode_blocks] {
- auto index = m_unicode_block_listview->selection().first();
- auto mapped_index = m_filter_model->map(index);
- if (mapped_index.row() > 0)
- m_range = unicode_blocks[mapped_index.row() - 1].code_point_range;
- else
- m_range = { 0x0000, 0x10FFFF };
- m_glyph_map_widget->set_active_range(m_range);
- };
- m_unicode_block_listview->set_model(m_filter_model);
- m_unicode_block_listview->set_activates_on_selection(true);
- m_unicode_block_listview->horizontal_scrollbar().set_visible(false);
- m_unicode_block_listview->set_cursor(m_unicode_block_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
- m_unicode_block_listview->set_focus_proxy(m_search_textbox);
- return {};
- }
- ErrorOr<void> MainWidget::create_undo_stack()
- {
- m_undo_stack = TRY(try_make<GUI::UndoStack>());
- m_undo_stack->on_state_change = [this] {
- m_undo_action->set_enabled(m_undo_stack->can_undo());
- m_redo_action->set_enabled(m_undo_stack->can_redo());
- update_action_text();
- if (m_undo_stack->is_current_modified())
- did_modify_font();
- };
- return {};
- }
- void MainWidget::update_action_text()
- {
- auto text_or_error = [](auto prefix, auto suffix) -> ErrorOr<String> {
- StringBuilder builder;
- TRY(builder.try_append(prefix));
- if (suffix.has_value()) {
- TRY(builder.try_append(' '));
- TRY(builder.try_append(suffix.value()));
- }
- return builder.to_string();
- };
- if (auto maybe_text = text_or_error("&Undo"sv, m_undo_stack->undo_action_text()); !maybe_text.is_error())
- m_undo_action->set_text(maybe_text.release_value().to_deprecated_string());
- if (auto maybe_text = text_or_error("&Redo"sv, m_undo_stack->redo_action_text()); !maybe_text.is_error())
- m_redo_action->set_text(maybe_text.release_value().to_deprecated_string());
- }
- ErrorOr<void> MainWidget::create_widgets()
- {
- TRY(load_from_gml(font_editor_window_gml));
- m_font_metadata_groupbox = find_descendant_of_type_named<GUI::GroupBox>("font_metadata_groupbox");
- m_unicode_block_container = find_descendant_of_type_named<GUI::Widget>("unicode_block_container");
- m_toolbar_container = find_descendant_of_type_named<GUI::ToolbarContainer>("toolbar_container");
- m_width_control_container = find_descendant_of_type_named<GUI::Widget>("width_control_container");
- m_glyph_map_widget = find_descendant_of_type_named<GUI::GlyphMapWidget>("glyph_map_widget");
- m_glyph_editor_widget = find_descendant_of_type_named<GlyphEditorWidget>("glyph_editor_widget");
- m_glyph_editor_widget->on_glyph_altered = [this](int glyph) {
- m_glyph_map_widget->update_glyph(glyph);
- update_preview();
- did_modify_font();
- };
- m_glyph_editor_widget->on_undo_event = [this](auto action_text) {
- reset_selection();
- push_undo(action_text);
- };
- m_glyph_editor_width_spinbox = find_descendant_of_type_named<GUI::SpinBox>("glyph_editor_width_spinbox");
- m_glyph_editor_present_checkbox = find_descendant_of_type_named<GUI::CheckBox>("glyph_editor_present_checkbox");
- m_glyph_map_widget->on_active_glyph_changed = [this](int glyph) {
- if (m_undo_selection) {
- auto selection = m_glyph_map_widget->selection().normalized();
- m_undo_selection->set_start(selection.start());
- m_undo_selection->set_size(selection.size());
- m_undo_selection->set_active_glyph(glyph);
- }
- m_glyph_editor_widget->set_glyph(glyph);
- auto glyph_width = m_font->raw_glyph_width(glyph);
- if (m_font->is_fixed_width())
- m_glyph_editor_present_checkbox->set_checked(glyph_width > 0, GUI::AllowCallback::No);
- else
- m_glyph_editor_width_spinbox->set_value(glyph_width, GUI::AllowCallback::No);
- update_statusbar();
- };
- m_glyph_map_widget->on_context_menu_request = [this](auto& event) {
- m_context_menu->popup(event.screen_position());
- };
- m_glyph_map_widget->on_escape_pressed = [this]() {
- update_statusbar();
- };
- m_name_textbox = find_descendant_of_type_named<GUI::TextBox>("name_textbox");
- m_name_textbox->on_change = [this] {
- m_font->set_name(m_name_textbox->text());
- did_modify_font();
- };
- m_family_textbox = find_descendant_of_type_named<GUI::TextBox>("family_textbox");
- m_family_textbox->on_change = [this] {
- m_font->set_family(m_family_textbox->text());
- did_modify_font();
- };
- m_fixed_width_checkbox = find_descendant_of_type_named<GUI::CheckBox>("fixed_width_checkbox");
- m_fixed_width_checkbox->on_checked = [this](bool checked) {
- m_font->set_fixed_width(checked);
- auto glyph_width = m_font->raw_glyph_width(m_glyph_map_widget->active_glyph());
- m_glyph_editor_width_spinbox->set_visible(!checked);
- m_glyph_editor_width_spinbox->set_value(glyph_width, GUI::AllowCallback::No);
- m_glyph_editor_present_checkbox->set_visible(checked);
- m_glyph_editor_present_checkbox->set_checked(glyph_width > 0, GUI::AllowCallback::No);
- m_glyph_editor_widget->update();
- update_preview();
- did_modify_font();
- };
- m_glyph_editor_width_spinbox->on_change = [this](int value) {
- reset_selection();
- push_undo("Resize Glyph"sv);
- m_font->set_glyph_width(m_glyph_map_widget->active_glyph(), value);
- m_glyph_editor_widget->update();
- m_glyph_map_widget->update_glyph(m_glyph_map_widget->active_glyph());
- update_preview();
- update_statusbar();
- did_modify_font();
- };
- m_glyph_editor_present_checkbox->on_checked = [this](bool checked) {
- reset_selection();
- push_undo("Resize Glyph"sv);
- m_font->set_glyph_width(m_glyph_map_widget->active_glyph(), checked ? m_font->glyph_fixed_width() : 0);
- m_glyph_editor_widget->update();
- m_glyph_map_widget->update_glyph(m_glyph_map_widget->active_glyph());
- update_preview();
- update_statusbar();
- did_modify_font();
- };
- m_weight_combobox = find_descendant_of_type_named<GUI::ComboBox>("weight_combobox");
- m_weight_combobox->on_change = [this](auto&, auto&) {
- m_font->set_weight(Gfx::name_to_weight(m_weight_combobox->text()));
- did_modify_font();
- };
- m_slope_combobox = find_descendant_of_type_named<GUI::ComboBox>("slope_combobox");
- m_slope_combobox->on_change = [this](auto&, auto&) {
- m_font->set_slope(Gfx::name_to_slope(m_slope_combobox->text()));
- did_modify_font();
- };
- m_presentation_spinbox = find_descendant_of_type_named<GUI::SpinBox>("presentation_spinbox");
- m_presentation_spinbox->on_change = [this](int value) {
- m_font->set_presentation_size(value);
- update_preview();
- did_modify_font();
- };
- m_spacing_spinbox = find_descendant_of_type_named<GUI::SpinBox>("spacing_spinbox");
- m_spacing_spinbox->on_change = [this](int value) {
- m_font->set_glyph_spacing(value);
- update_preview();
- did_modify_font();
- };
- m_baseline_spinbox = find_descendant_of_type_named<GUI::SpinBox>("baseline_spinbox");
- m_baseline_spinbox->on_change = [this](int value) {
- m_font->set_baseline(value);
- m_glyph_editor_widget->update();
- update_preview();
- did_modify_font();
- };
- m_mean_line_spinbox = find_descendant_of_type_named<GUI::SpinBox>("mean_line_spinbox");
- m_mean_line_spinbox->on_change = [this](int value) {
- m_font->set_mean_line(value);
- m_glyph_editor_widget->update();
- update_preview();
- did_modify_font();
- };
- m_search_textbox = find_descendant_of_type_named<GUI::TextBox>("search_textbox");
- m_search_textbox->on_return_pressed = [this] {
- if (!m_unicode_block_listview->selection().is_empty())
- m_unicode_block_listview->activate_selected();
- };
- m_search_textbox->on_down_pressed = [this] {
- m_unicode_block_listview->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
- };
- m_search_textbox->on_up_pressed = [this] {
- m_unicode_block_listview->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
- };
- m_search_textbox->on_change = [this] {
- m_filter_model->set_filter_term(m_search_textbox->text());
- if (m_filter_model->row_count() != 0)
- m_unicode_block_listview->set_cursor(m_filter_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
- };
- m_statusbar = find_descendant_of_type_named<GUI::Statusbar>("statusbar");
- m_statusbar->segment(1).set_mode(GUI::Statusbar::Segment::Mode::Auto);
- m_statusbar->segment(1).set_clickable(true);
- m_statusbar->segment(1).on_click = [this](auto) {
- m_show_unicode_blocks_action->activate();
- };
- GUI::Application::the()->on_action_enter = [this](GUI::Action& action) {
- auto text = action.status_tip();
- if (text.is_empty())
- text = Gfx::parse_ampersand_string(action.text());
- m_statusbar->set_override_text(move(text));
- };
- GUI::Application::the()->on_action_leave = [this](GUI::Action&) {
- m_statusbar->set_override_text({});
- };
- return {};
- }
- ErrorOr<void> MainWidget::initialize(StringView path, RefPtr<Gfx::BitmapFont>&& mutable_font)
- {
- VERIFY(window());
- if (m_font == mutable_font)
- return {};
- ScopeGuard reset_on_error([&] {
- if (!m_initialized)
- reset();
- });
- m_initialized = false;
- m_path = TRY(String::from_utf8(path));
- m_font = move(mutable_font);
- TRY(m_glyph_map_widget->initialize(m_font));
- auto active_glyph = m_glyph_map_widget->active_glyph();
- m_glyph_map_widget->set_focus(true);
- m_glyph_map_widget->scroll_to_glyph(active_glyph);
- auto selection = m_glyph_map_widget->selection().normalized();
- m_undo_selection = TRY(try_make_ref_counted<UndoSelection>(selection.start(), selection.size(), active_glyph, *m_font, *m_glyph_map_widget));
- m_undo_stack->clear();
- if (m_preview_label)
- m_preview_label->set_font(*m_font);
- m_glyph_editor_widget->initialize(m_font);
- m_glyph_editor_widget->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height());
- m_glyph_editor_widget->set_glyph(active_glyph);
- m_glyph_editor_width_spinbox->set_visible(!m_font->is_fixed_width());
- m_glyph_editor_width_spinbox->set_max(m_font->max_glyph_width(), GUI::AllowCallback::No);
- m_glyph_editor_width_spinbox->set_value(m_font->raw_glyph_width(active_glyph), GUI::AllowCallback::No);
- m_glyph_editor_present_checkbox->set_visible(m_font->is_fixed_width());
- m_glyph_editor_present_checkbox->set_checked(m_font->contains_raw_glyph(active_glyph), GUI::AllowCallback::No);
- m_fixed_width_checkbox->set_checked(m_font->is_fixed_width(), GUI::AllowCallback::No);
- m_name_textbox->set_text(m_font->name(), GUI::AllowCallback::No);
- m_family_textbox->set_text(m_font->family(), GUI::AllowCallback::No);
- m_presentation_spinbox->set_value(m_font->presentation_size(), GUI::AllowCallback::No);
- m_spacing_spinbox->set_value(m_font->glyph_spacing(), GUI::AllowCallback::No);
- m_mean_line_spinbox->set_range(0, max(m_font->glyph_height() - 2, 0), GUI::AllowCallback::No);
- m_baseline_spinbox->set_range(0, max(m_font->glyph_height() - 2, 0), GUI::AllowCallback::No);
- m_mean_line_spinbox->set_value(m_font->mean_line(), GUI::AllowCallback::No);
- m_baseline_spinbox->set_value(m_font->baseline(), GUI::AllowCallback::No);
- for (size_t i = 0; i < Gfx::font_weight_names.size(); ++i) {
- if (Gfx::font_weight_names[i].style == m_font->weight()) {
- m_weight_combobox->set_selected_index(i, GUI::AllowCallback::No);
- break;
- }
- }
- for (size_t i = 0; i < Gfx::font_slope_names.size(); ++i) {
- if (Gfx::font_slope_names[i].style == m_font->slope()) {
- m_slope_combobox->set_selected_index(i, GUI::AllowCallback::No);
- break;
- }
- }
- window()->set_modified(false);
- update_title();
- update_statusbar();
- set_actions_enabled(true);
- set_widgets_enabled(true);
- m_initialized = true;
- return {};
- }
- ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
- {
- auto file_menu = TRY(window.try_add_menu("&File"_short_string));
- TRY(file_menu->try_add_action(*m_new_action));
- TRY(file_menu->try_add_action(*m_open_action));
- TRY(file_menu->try_add_action(*m_save_action));
- TRY(file_menu->try_add_action(*m_save_as_action));
- TRY(file_menu->try_add_separator());
- TRY(file_menu->add_recent_files_list([this](auto& action) {
- if (!request_close())
- return;
- auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(this->window(), action.text());
- if (response.is_error())
- return;
- auto file = response.release_value();
- if (auto result = open_file(file.filename(), file.release_stream()); result.is_error())
- show_error(result.release_error(), "Opening"sv, file.filename());
- }));
- TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([this](auto&) {
- if (!request_close())
- return;
- GUI::Application::the()->quit();
- })));
- auto edit_menu = TRY(window.try_add_menu("&Edit"_short_string));
- TRY(edit_menu->try_add_action(*m_undo_action));
- TRY(edit_menu->try_add_action(*m_redo_action));
- TRY(edit_menu->try_add_separator());
- TRY(edit_menu->try_add_action(*m_cut_action));
- TRY(edit_menu->try_add_action(*m_copy_action));
- TRY(edit_menu->try_add_action(*m_paste_action));
- TRY(edit_menu->try_add_action(*m_delete_action));
- TRY(edit_menu->try_add_separator());
- TRY(edit_menu->try_add_action(*m_select_all_action));
- TRY(edit_menu->try_add_separator());
- TRY(edit_menu->try_add_action(*m_copy_text_action));
- m_context_menu = edit_menu;
- auto go_menu = TRY(window.try_add_menu("&Go"_short_string));
- TRY(go_menu->try_add_action(*m_previous_glyph_action));
- TRY(go_menu->try_add_action(*m_next_glyph_action));
- TRY(go_menu->try_add_action(*m_go_to_glyph_action));
- auto view_menu = TRY(window.try_add_menu("&View"_short_string));
- auto layout_menu = TRY(view_menu->try_add_submenu("&Layout"_short_string));
- TRY(layout_menu->try_add_action(*m_show_toolbar_action));
- TRY(layout_menu->try_add_action(*m_show_statusbar_action));
- TRY(layout_menu->try_add_action(*m_show_metadata_action));
- TRY(layout_menu->try_add_action(*m_show_unicode_blocks_action));
- TRY(view_menu->try_add_separator());
- TRY(view_menu->try_add_action(*m_open_preview_action));
- TRY(view_menu->try_add_separator());
- TRY(view_menu->try_add_action(*m_highlight_modifications_action));
- TRY(view_menu->try_add_action(*m_show_system_emoji_action));
- TRY(view_menu->try_add_separator());
- auto scale_menu = TRY(view_menu->try_add_submenu("&Scale"_short_string));
- scale_menu->set_icon(g_resources.scale_editor);
- TRY(scale_menu->try_add_action(*m_scale_five_action));
- TRY(scale_menu->try_add_action(*m_scale_ten_action));
- TRY(scale_menu->try_add_action(*m_scale_fifteen_action));
- auto help_menu = TRY(window.try_add_menu("&Help"_short_string));
- TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(&window)));
- TRY(help_menu->try_add_action(GUI::CommonActions::make_help_action([](auto&) {
- Desktop::Launcher::open(URL::create_with_file_scheme("/usr/share/man/man1/Applications/FontEditor.md"), "/bin/Help");
- })));
- TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Font Editor", TRY(GUI::Icon::try_create_default_icon("app-font-editor"sv)), &window)));
- return {};
- }
- ErrorOr<void> MainWidget::save_file(StringView path, NonnullOwnPtr<Core::File> file)
- {
- auto masked_font = TRY(m_font->masked_character_set());
- TRY(masked_font->write_to_file(move(file)));
- m_path = TRY(String::from_utf8(path));
- m_undo_stack->set_current_unmodified();
- window()->set_modified(false);
- update_title();
- return {};
- }
- ErrorOr<void> MainWidget::open_file(StringView path, NonnullOwnPtr<Core::File> file)
- {
- auto mapped_file = TRY(Core::MappedFile::map_from_file(move(file), path));
- auto unmasked_font = TRY(TRY(Gfx::BitmapFont::try_load_from_mapped_file(mapped_file))->unmasked_character_set());
- TRY(initialize(path, move(unmasked_font)));
- if (!path.is_empty())
- GUI::Application::the()->set_most_recently_open_file(TRY(String::from_utf8(path)));
- return {};
- }
- void MainWidget::push_undo(StringView action_text)
- {
- auto maybe_state = m_undo_selection->save_state();
- if (maybe_state.is_error())
- return show_error(maybe_state.release_error(), "Saving undo state failed"sv);
- auto maybe_text = String::from_utf8(action_text);
- if (maybe_text.is_error())
- return show_error(maybe_text.release_error(), "Creating action text failed"sv);
- auto maybe_command = try_make<SelectionUndoCommand>(*m_undo_selection, move(maybe_state.value()), move(maybe_text.value()));
- if (maybe_command.is_error())
- return show_error(maybe_command.release_error(), "Making undo command failed"sv);
- if (auto maybe_push = m_undo_stack->try_push(move(maybe_command.value())); maybe_push.is_error())
- show_error(maybe_push.release_error(), "Pushing undo stack failed"sv);
- }
- void MainWidget::reset_selection()
- {
- auto selection = m_glyph_map_widget->selection().normalized();
- if (selection.size() == 1)
- return;
- auto start = m_glyph_map_widget->active_glyph();
- m_undo_selection->set_start(start);
- m_undo_selection->set_size(1);
- m_glyph_map_widget->set_selection(start, 1);
- m_glyph_map_widget->update();
- }
- void MainWidget::restore_state()
- {
- auto glyph = m_undo_selection->restored_active_glyph();
- auto glyph_width = m_font->raw_glyph_width(glyph);
- if (glyph < m_range.first || glyph > m_range.last)
- m_search_textbox->set_text(""sv);
- auto start = m_undo_selection->restored_start();
- auto size = m_undo_selection->restored_size();
- m_glyph_map_widget->restore_selection(start, size, glyph);
- m_glyph_map_widget->scroll_to_glyph(glyph);
- m_glyph_map_widget->set_focus(true);
- if (m_font->is_fixed_width())
- m_glyph_editor_present_checkbox->set_checked(glyph_width > 0, GUI::AllowCallback::No);
- else
- m_glyph_editor_width_spinbox->set_value(glyph_width, GUI::AllowCallback::No);
- m_glyph_editor_widget->update();
- m_glyph_map_widget->update();
- update_preview();
- update_statusbar();
- }
- void MainWidget::undo()
- {
- if (!m_undo_stack->can_undo())
- return;
- m_undo_stack->undo();
- restore_state();
- }
- void MainWidget::redo()
- {
- if (!m_undo_stack->can_redo())
- return;
- m_undo_stack->redo();
- restore_state();
- }
- bool MainWidget::request_close()
- {
- if (!window()->is_modified())
- return true;
- auto result = GUI::MessageBox::try_ask_about_unsaved_changes(window(), m_path, m_undo_stack->last_unmodified_timestamp());
- if (result.is_error())
- return false;
- if (result.value() == GUI::MessageBox::ExecResult::Yes) {
- m_save_action->activate();
- if (!window()->is_modified())
- return true;
- }
- if (result.value() == GUI::MessageBox::ExecResult::No)
- return true;
- return false;
- }
- void MainWidget::update_title()
- {
- StringBuilder title;
- if (m_path.is_empty())
- title.append("Untitled"sv);
- else
- title.append(m_path);
- title.append("[*] - Font Editor"sv);
- window()->set_title(title.to_deprecated_string());
- }
- void MainWidget::did_modify_font()
- {
- if (!window() || window()->is_modified())
- return;
- window()->set_modified(true);
- update_title();
- }
- void MainWidget::update_statusbar()
- {
- if (!m_font)
- return;
- if (!m_statusbar->is_visible())
- return;
- auto glyph = m_glyph_map_widget->active_glyph();
- StringBuilder builder;
- builder.appendff("U+{:04X} (", glyph);
- if (auto abbreviation = Unicode::code_point_abbreviation(glyph); abbreviation.has_value()) {
- builder.append(*abbreviation);
- } else if (Gfx::get_char_bidi_class(glyph) == Gfx::BidirectionalClass::STRONG_RTL) {
- // FIXME: This is a necessary hack, as RTL text will mess up the painting of the statusbar text.
- // For now, replace RTL glyphs with U+FFFD, the replacement character.
- builder.append_code_point(0xFFFD);
- } else {
- builder.append_code_point(glyph);
- }
- builder.append(')');
- auto glyph_name = Unicode::code_point_display_name(glyph);
- if (glyph_name.has_value()) {
- builder.appendff(" {}", glyph_name.value());
- }
- if (m_font->contains_raw_glyph(glyph))
- builder.appendff(" [{}x{}]", m_font->raw_glyph_width(glyph), m_font->glyph_height());
- else if (Gfx::Emoji::emoji_for_code_point(glyph))
- builder.appendff(" [emoji]");
- m_statusbar->set_text(builder.to_deprecated_string());
- builder.clear();
- auto selection = m_glyph_map_widget->selection().normalized();
- if (selection.size() > 1)
- builder.appendff("{} glyphs selected", selection.size());
- else
- builder.appendff("U+{:04X}-U+{:04X}", m_range.first, m_range.last);
- m_statusbar->set_text(1, builder.to_deprecated_string());
- }
- void MainWidget::update_preview()
- {
- if (m_font_preview_window)
- m_font_preview_window->update();
- }
- void MainWidget::drag_enter_event(GUI::DragEvent& event)
- {
- auto const& mime_types = event.mime_types();
- if (mime_types.contains_slow("text/uri-list"))
- event.accept();
- }
- void MainWidget::drop_event(GUI::DropEvent& event)
- {
- event.accept();
- if (event.mime_data().has_urls()) {
- auto urls = event.mime_data().urls();
- if (urls.is_empty())
- return;
- window()->move_to_front();
- if (!request_close())
- return;
- auto file_path = urls.first().serialize_path();
- auto result = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), file_path);
- if (result.is_error())
- return;
- auto file = result.release_value();
- if (auto result = open_file(file.filename(), file.release_stream()); result.is_error())
- show_error(result.release_error(), "Opening"sv, file.filename());
- }
- }
- void MainWidget::set_scale_and_save(i32 scale)
- {
- Config::write_i32("FontEditor"sv, "GlyphEditor"sv, "Scale"sv, scale);
- m_glyph_editor_widget->set_scale(scale);
- m_glyph_editor_widget->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height());
- }
- ErrorOr<void> MainWidget::copy_selected_glyphs()
- {
- size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height();
- auto selection = m_glyph_map_widget->selection().normalized();
- auto* rows = m_font->rows() + selection.start() * bytes_per_glyph;
- auto* widths = m_font->widths() + selection.start();
- ByteBuffer buffer;
- TRY(buffer.try_append(rows, bytes_per_glyph * selection.size()));
- TRY(buffer.try_append(widths, selection.size()));
- HashMap<DeprecatedString, DeprecatedString> metadata;
- metadata.set("start", DeprecatedString::number(selection.start()));
- metadata.set("count", DeprecatedString::number(selection.size()));
- metadata.set("width", DeprecatedString::number(m_font->max_glyph_width()));
- metadata.set("height", DeprecatedString::number(m_font->glyph_height()));
- GUI::Clipboard::the().set_data(buffer.bytes(), "glyph/x-fonteditor", metadata);
- return {};
- }
- ErrorOr<void> MainWidget::cut_selected_glyphs()
- {
- TRY(copy_selected_glyphs());
- delete_selected_glyphs();
- return {};
- }
- void MainWidget::paste_glyphs()
- {
- auto [data, mime_type, metadata] = GUI::Clipboard::the().fetch_data_and_type();
- if (!mime_type.starts_with("glyph/"sv))
- return;
- auto glyph_count = metadata.get("count").value().to_uint().value_or(0);
- if (!glyph_count)
- return;
- auto height = metadata.get("height").value().to_uint().value_or(0);
- if (!height)
- return;
- auto selection = m_glyph_map_widget->selection().normalized();
- auto range_bound_glyph_count = min(glyph_count, 1 + m_range.last - selection.start());
- m_undo_selection->set_size(range_bound_glyph_count);
- auto action_text = range_bound_glyph_count == 1 ? "Paste Glyph"sv : "Paste Glyphs"sv;
- push_undo(action_text);
- size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height();
- size_t bytes_per_copied_glyph = Gfx::GlyphBitmap::bytes_per_row() * height;
- size_t copyable_bytes_per_glyph = min(bytes_per_glyph, bytes_per_copied_glyph);
- auto* rows = m_font->rows() + selection.start() * bytes_per_glyph;
- auto* widths = m_font->widths() + selection.start();
- for (size_t i = 0; i < range_bound_glyph_count; ++i) {
- auto copyable_width = m_font->is_fixed_width()
- ? data[bytes_per_copied_glyph * glyph_count + i] ? m_font->glyph_fixed_width() : 0
- : min(m_font->max_glyph_width(), data[bytes_per_copied_glyph * glyph_count + i]);
- memcpy(&rows[i * bytes_per_glyph], &data[i * bytes_per_copied_glyph], copyable_bytes_per_glyph);
- memset(&widths[i], copyable_width, sizeof(u8));
- m_glyph_map_widget->set_glyph_modified(selection.start() + i, true);
- }
- m_glyph_map_widget->set_selection(selection.start() + range_bound_glyph_count - 1, -range_bound_glyph_count + 1);
- if (m_font->is_fixed_width())
- m_glyph_editor_present_checkbox->set_checked(m_font->contains_raw_glyph(m_glyph_map_widget->active_glyph()), GUI::AllowCallback::No);
- else
- m_glyph_editor_width_spinbox->set_value(m_font->raw_glyph_width(m_glyph_map_widget->active_glyph()), GUI::AllowCallback::No);
- m_glyph_editor_widget->update();
- m_glyph_map_widget->update();
- update_preview();
- update_statusbar();
- }
- void MainWidget::delete_selected_glyphs()
- {
- auto selection = m_glyph_map_widget->selection().normalized();
- auto action_text = selection.size() == 1 ? "Delete Glyph"sv : "Delete Glyphs"sv;
- push_undo(action_text);
- size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height();
- auto* rows = m_font->rows() + selection.start() * bytes_per_glyph;
- auto* widths = m_font->widths() + selection.start();
- memset(rows, 0, bytes_per_glyph * selection.size());
- memset(widths, 0, selection.size());
- if (m_font->is_fixed_width())
- m_glyph_editor_present_checkbox->set_checked(false, GUI::AllowCallback::No);
- else
- m_glyph_editor_width_spinbox->set_value(0, GUI::AllowCallback::No);
- m_glyph_editor_widget->update();
- m_glyph_map_widget->update();
- update_preview();
- update_statusbar();
- }
- void MainWidget::show_error(Error error, StringView action, StringView filename)
- {
- auto format = filename.is_null() ? "{}{}: {}"sv : "{} \"{}\" failed: {}"sv;
- auto file = filename.is_null() ? StringView {} : filename;
- warnln(format, action, file, error);
- auto maybe_message = String::formatted(format, action, file, error);
- if (!maybe_message.is_error())
- (void)GUI::MessageBox::try_show_error(window(), maybe_message.release_value());
- }
- void MainWidget::reset()
- {
- VERIFY(window());
- m_initialized = false;
- m_font = nullptr;
- m_path = {};
- m_undo_selection = nullptr;
- m_undo_stack->clear();
- (void)m_glyph_map_widget->initialize(nullptr);
- m_glyph_editor_widget->initialize(nullptr);
- if (m_font_preview_window)
- m_font_preview_window->close();
- if (m_preview_label)
- m_preview_label->set_font(nullptr);
- m_name_textbox->set_text({}, GUI::AllowCallback::No);
- m_family_textbox->set_text({}, GUI::AllowCallback::No);
- m_slope_combobox->set_text({}, GUI::AllowCallback::No);
- m_weight_combobox->set_text({}, GUI::AllowCallback::No);
- m_presentation_spinbox->set_text({}, GUI::AllowCallback::No);
- m_baseline_spinbox->set_text({}, GUI::AllowCallback::No);
- m_mean_line_spinbox->set_text({}, GUI::AllowCallback::No);
- m_spacing_spinbox->set_text({}, GUI::AllowCallback::No);
- m_fixed_width_checkbox->set_checked(false, GUI::AllowCallback::No);
- m_statusbar->set_text(0, {});
- m_statusbar->set_text(1, {});
- window()->set_modified(false);
- window()->set_title("Font Editor");
- set_actions_enabled(false);
- set_widgets_enabled(false);
- set_focus(true);
- }
- void MainWidget::set_actions_enabled(bool enabled)
- {
- m_save_action->set_enabled(enabled);
- m_save_as_action->set_enabled(enabled);
- m_cut_action->set_enabled(enabled);
- m_copy_action->set_enabled(enabled);
- m_paste_action->set_enabled(enabled && GUI::Clipboard::the().fetch_mime_type() == "glyph/x-fonteditor");
- m_delete_action->set_enabled(enabled);
- m_copy_text_action->set_enabled(enabled);
- m_select_all_action->set_enabled(enabled);
- m_go_to_glyph_action->set_enabled(enabled);
- m_previous_glyph_action->set_enabled(enabled);
- m_next_glyph_action->set_enabled(enabled);
- m_move_glyph_action->set_enabled(enabled);
- m_paint_glyph_action->set_enabled(enabled);
- m_flip_horizontal_action->set_enabled(enabled);
- m_flip_vertical_action->set_enabled(enabled);
- m_rotate_clockwise_action->set_enabled(enabled);
- m_rotate_counterclockwise_action->set_enabled(enabled);
- m_open_preview_action->set_enabled(enabled);
- m_highlight_modifications_action->set_enabled(enabled);
- m_show_system_emoji_action->set_enabled(enabled);
- m_scale_five_action->set_enabled(enabled);
- m_scale_ten_action->set_enabled(enabled);
- m_scale_fifteen_action->set_enabled(enabled);
- }
- void MainWidget::set_widgets_enabled(bool enabled)
- {
- m_font_metadata_groupbox->set_enabled(enabled);
- m_unicode_block_container->set_enabled(enabled);
- m_width_control_container->set_enabled(enabled);
- m_width_control_container->set_visible(enabled);
- m_glyph_map_widget->set_enabled(enabled);
- m_glyph_editor_widget->set_enabled(enabled);
- m_glyph_editor_widget->set_visible(enabled);
- m_statusbar->segment(1).set_visible(enabled);
- }
- }
|