MainWidget.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "MainWidget.h"
  8. #include "GlyphEditorWidget.h"
  9. #include "NewFontDialog.h"
  10. #include <AK/Array.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <AK/String.h>
  13. #include <AK/StringBuilder.h>
  14. #include <AK/StringUtils.h>
  15. #include <Applications/FontEditor/FontEditorWindowGML.h>
  16. #include <Applications/FontEditor/FontPreviewWindowGML.h>
  17. #include <LibConfig/Client.h>
  18. #include <LibDesktop/Launcher.h>
  19. #include <LibFileSystemAccessClient/Client.h>
  20. #include <LibGUI/Action.h>
  21. #include <LibGUI/Application.h>
  22. #include <LibGUI/Button.h>
  23. #include <LibGUI/CheckBox.h>
  24. #include <LibGUI/Clipboard.h>
  25. #include <LibGUI/ComboBox.h>
  26. #include <LibGUI/FilePicker.h>
  27. #include <LibGUI/GlyphMapWidget.h>
  28. #include <LibGUI/GroupBox.h>
  29. #include <LibGUI/InputBox.h>
  30. #include <LibGUI/ItemListModel.h>
  31. #include <LibGUI/Label.h>
  32. #include <LibGUI/ListView.h>
  33. #include <LibGUI/Menu.h>
  34. #include <LibGUI/Menubar.h>
  35. #include <LibGUI/MessageBox.h>
  36. #include <LibGUI/SpinBox.h>
  37. #include <LibGUI/Statusbar.h>
  38. #include <LibGUI/TextBox.h>
  39. #include <LibGUI/ToolbarContainer.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibGfx/Font/BitmapFont.h>
  42. #include <LibGfx/Font/Emoji.h>
  43. #include <LibGfx/Font/FontStyleMapping.h>
  44. #include <LibGfx/TextDirection.h>
  45. #include <LibUnicode/CharacterTypes.h>
  46. namespace FontEditor {
  47. Resources g_resources;
  48. static constexpr Array pangrams = {
  49. "quick fox jumps nightly above wizard"sv,
  50. "five quacking zephyrs jolt my wax bed"sv,
  51. "pack my box with five dozen liquor jugs"sv,
  52. "quick brown fox jumps over the lazy dog"sv,
  53. "waxy and quivering jocks fumble the pizza"sv,
  54. "~#:[@_1%]*{$2.3}/4^(5'6\")-&|7+8!=<9,0\\>?;"sv,
  55. "byxfjärmat föl gick på duvshowen"sv,
  56. "         "sv,
  57. "float Fox.quick(h){ is_brown && it_jumps_over(doges.lazy) }"sv,
  58. "<fox color=\"brown\" speed=\"quick\" jumps=\"over\">lazy dog</fox>"sv
  59. };
  60. ErrorOr<NonnullRefPtr<MainWidget>> MainWidget::try_create()
  61. {
  62. auto main_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MainWidget()));
  63. TRY(main_widget->create_widgets());
  64. TRY(main_widget->create_actions());
  65. TRY(main_widget->create_models());
  66. TRY(main_widget->create_toolbars());
  67. TRY(main_widget->create_undo_stack());
  68. return main_widget;
  69. }
  70. ErrorOr<RefPtr<GUI::Window>> MainWidget::create_preview_window()
  71. {
  72. auto window = TRY(GUI::Window::try_create(this));
  73. window->set_window_mode(GUI::WindowMode::RenderAbove);
  74. window->set_title("Preview");
  75. window->resize(400, 150);
  76. window->center_within(*this->window());
  77. auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
  78. TRY(main_widget->load_from_gml(font_preview_window_gml));
  79. m_preview_label = find_descendant_of_type_named<GUI::Label>("preview_label");
  80. m_preview_label->set_font(m_font);
  81. m_preview_textbox = find_descendant_of_type_named<GUI::TextBox>("preview_textbox");
  82. m_preview_textbox->on_change = [this] {
  83. auto maybe_preview = [](StringView text) -> ErrorOr<String> {
  84. return TRY(String::formatted("{}\n{}", text, TRY(Unicode::to_unicode_uppercase_full(text))));
  85. }(m_preview_textbox->text());
  86. if (maybe_preview.is_error())
  87. return show_error(maybe_preview.release_error(), "Formatting preview text failed"sv);
  88. m_preview_label->set_text(maybe_preview.release_value());
  89. };
  90. m_preview_textbox->set_text(pangrams[0]);
  91. auto& reload_button = *find_descendant_of_type_named<GUI::Button>("reload_button");
  92. reload_button.on_click = [this](auto) {
  93. static size_t i = 1;
  94. if (i >= pangrams.size())
  95. i = 0;
  96. m_preview_textbox->set_text(pangrams[i]);
  97. i++;
  98. };
  99. return window;
  100. }
  101. ErrorOr<void> MainWidget::create_actions()
  102. {
  103. m_new_action = GUI::Action::create("&New Font...", { Mod_Ctrl, Key_N }, g_resources.new_font, [this](auto&) {
  104. if (!request_close())
  105. return;
  106. auto new_font_wizard = NewFontDialog::construct(window());
  107. if (new_font_wizard->exec() != GUI::Dialog::ExecResult::OK)
  108. return;
  109. auto maybe_font = new_font_wizard->create_font();
  110. if (maybe_font.is_error())
  111. return show_error(maybe_font.release_error(), "Creating new font failed"sv);
  112. if (auto result = initialize({}, move(maybe_font.value())); result.is_error())
  113. show_error(result.release_error(), "Initializing new font failed"sv);
  114. });
  115. m_new_action->set_status_tip("Create a new font");
  116. m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
  117. if (!request_close())
  118. return;
  119. auto response = FileSystemAccessClient::Client::the().open_file(window(), "Open font file", "/res/fonts"sv);
  120. if (response.is_error())
  121. return;
  122. auto file = response.release_value();
  123. if (auto result = open_file(file.filename(), file.release_stream()); result.is_error())
  124. show_error(result.release_error(), "Opening"sv, file.filename());
  125. });
  126. m_save_action = GUI::CommonActions::make_save_action([this](auto&) {
  127. if (m_path.is_empty())
  128. return m_save_as_action->activate();
  129. auto response = FileSystemAccessClient::Client::the().request_file(window(), m_path.to_deprecated_string(), Core::File::OpenMode::Truncate | Core::File::OpenMode::Write);
  130. if (response.is_error())
  131. return;
  132. auto file = response.release_value();
  133. if (auto result = save_file(m_path, file.release_stream()); result.is_error())
  134. show_error(result.release_error(), "Saving"sv, m_path);
  135. });
  136. m_save_as_action = GUI::CommonActions::make_save_as_action([this](auto&) {
  137. auto default_path = LexicalPath(m_path.is_empty() ? "Untitled.font"sv : m_path);
  138. auto response = FileSystemAccessClient::Client::the().save_file(window(), default_path.title(), default_path.extension());
  139. if (response.is_error())
  140. return;
  141. auto file = response.release_value();
  142. if (auto result = save_file(file.filename(), file.release_stream()); result.is_error())
  143. show_error(result.release_error(), "Saving"sv, file.filename());
  144. else
  145. GUI::Application::the()->set_most_recently_open_file(file.filename());
  146. });
  147. m_cut_action = GUI::CommonActions::make_cut_action([this](auto&) {
  148. if (auto result = cut_selected_glyphs(); result.is_error())
  149. show_error(result.release_error(), "Cutting selection failed"sv);
  150. });
  151. m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
  152. if (auto result = copy_selected_glyphs(); result.is_error())
  153. show_error(result.release_error(), "Copying selection failed"sv);
  154. });
  155. m_paste_action = GUI::CommonActions::make_paste_action([this](auto&) {
  156. paste_glyphs();
  157. });
  158. m_paste_action->set_enabled(GUI::Clipboard::the().fetch_mime_type() == "glyph/x-fonteditor");
  159. GUI::Clipboard::the().on_change = [this](DeprecatedString const& data_type) {
  160. m_paste_action->set_enabled(data_type == "glyph/x-fonteditor");
  161. };
  162. m_delete_action = GUI::CommonActions::make_delete_action([this](auto&) {
  163. delete_selected_glyphs();
  164. });
  165. m_undo_action = GUI::CommonActions::make_undo_action([this](auto&) {
  166. undo();
  167. });
  168. m_undo_action->set_enabled(false);
  169. m_redo_action = GUI::CommonActions::make_redo_action([this](auto&) {
  170. redo();
  171. });
  172. m_redo_action->set_enabled(false);
  173. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  174. m_glyph_map_widget->set_selection(m_range.first, m_range.last - m_range.first + 1);
  175. m_glyph_map_widget->update();
  176. auto selection = m_glyph_map_widget->selection().normalized();
  177. m_undo_selection->set_start(selection.start());
  178. m_undo_selection->set_size(selection.size());
  179. update_statusbar();
  180. });
  181. m_open_preview_action = GUI::Action::create("&Preview Font", { Mod_Ctrl, Key_P }, g_resources.preview_font, [this](auto&) {
  182. if (!m_font_preview_window) {
  183. if (auto maybe_window = create_preview_window(); maybe_window.is_error())
  184. show_error(maybe_window.release_error(), "Creating preview window failed"sv);
  185. else
  186. m_font_preview_window = maybe_window.release_value();
  187. }
  188. if (m_font_preview_window)
  189. m_font_preview_window->show();
  190. });
  191. m_open_preview_action->set_status_tip("Preview the current font");
  192. bool show_metadata = Config::read_bool("FontEditor"sv, "Layout"sv, "ShowMetadata"sv, true);
  193. m_font_metadata_groupbox->set_visible(show_metadata);
  194. m_show_metadata_action = GUI::Action::create_checkable("Font &Metadata", { Mod_Ctrl, Key_M }, [this](auto& action) {
  195. m_font_metadata_groupbox->set_visible(action.is_checked());
  196. Config::write_bool("FontEditor"sv, "Layout"sv, "ShowMetadata"sv, action.is_checked());
  197. });
  198. m_show_metadata_action->set_checked(show_metadata);
  199. m_show_metadata_action->set_status_tip("Show or hide metadata about the current font");
  200. bool show_unicode_blocks = Config::read_bool("FontEditor"sv, "Layout"sv, "ShowUnicodeBlocks"sv, true);
  201. m_unicode_block_container->set_visible(show_unicode_blocks);
  202. m_show_unicode_blocks_action = GUI::Action::create_checkable("&Unicode Blocks", { Mod_Ctrl, Key_U }, [this](auto& action) {
  203. m_unicode_block_container->set_visible(action.is_checked());
  204. if (action.is_checked())
  205. m_search_textbox->set_focus(m_initialized);
  206. else
  207. m_glyph_map_widget->set_focus(m_initialized);
  208. Config::write_bool("FontEditor"sv, "Layout"sv, "ShowUnicodeBlocks"sv, action.is_checked());
  209. });
  210. m_show_unicode_blocks_action->set_checked(show_unicode_blocks);
  211. m_show_unicode_blocks_action->set_status_tip("Show or hide the Unicode block list");
  212. bool show_toolbar = Config::read_bool("FontEditor"sv, "Layout"sv, "ShowToolbar"sv, true);
  213. m_toolbar_container->set_visible(show_toolbar);
  214. m_show_toolbar_action = GUI::Action::create_checkable("&Toolbar", [this](auto& action) {
  215. m_toolbar_container->set_visible(action.is_checked());
  216. Config::write_bool("FontEditor"sv, "Layout"sv, "ShowToolbar"sv, action.is_checked());
  217. });
  218. m_show_toolbar_action->set_checked(show_toolbar);
  219. m_show_toolbar_action->set_status_tip("Show or hide the toolbar");
  220. bool show_statusbar = Config::read_bool("FontEditor"sv, "Layout"sv, "ShowStatusbar"sv, true);
  221. m_statusbar->set_visible(show_statusbar);
  222. m_show_statusbar_action = GUI::Action::create_checkable("&Status Bar", [this](auto& action) {
  223. m_statusbar->set_visible(action.is_checked());
  224. update_statusbar();
  225. Config::write_bool("FontEditor"sv, "Layout"sv, "ShowStatusbar"sv, action.is_checked());
  226. });
  227. m_show_statusbar_action->set_checked(show_statusbar);
  228. m_show_statusbar_action->set_status_tip("Show or hide the status bar");
  229. bool highlight_modifications = Config::read_bool("FontEditor"sv, "GlyphMap"sv, "HighlightModifications"sv, true);
  230. m_glyph_map_widget->set_highlight_modifications(highlight_modifications);
  231. m_highlight_modifications_action = GUI::Action::create_checkable("&Highlight Modifications", { Mod_Ctrl, Key_H }, [this](auto& action) {
  232. m_glyph_map_widget->set_highlight_modifications(action.is_checked());
  233. Config::write_bool("FontEditor"sv, "GlyphMap"sv, "HighlightModifications"sv, action.is_checked());
  234. });
  235. m_highlight_modifications_action->set_checked(highlight_modifications);
  236. m_highlight_modifications_action->set_status_tip("Show or hide highlights on modified glyphs. (Green = New, Blue = Modified, Red = Deleted)");
  237. bool show_system_emoji = Config::read_bool("FontEditor"sv, "GlyphMap"sv, "ShowSystemEmoji"sv, true);
  238. m_glyph_map_widget->set_show_system_emoji(show_system_emoji);
  239. m_show_system_emoji_action = GUI::Action::create_checkable("System &Emoji", { Mod_Ctrl, Key_E }, [this](auto& action) {
  240. m_glyph_map_widget->set_show_system_emoji(action.is_checked());
  241. Config::write_bool("FontEditor"sv, "GlyphMap"sv, "ShowSystemEmoji"sv, action.is_checked());
  242. });
  243. m_show_system_emoji_action->set_checked(show_system_emoji);
  244. m_show_system_emoji_action->set_status_tip("Show or hide system emoji");
  245. m_go_to_glyph_action = GUI::Action::create("&Go to Glyph...", { Mod_Ctrl, Key_G }, g_resources.go_to_glyph, [this](auto&) {
  246. String input;
  247. auto result = GUI::InputBox::try_show(window(), input, {}, "Go to glyph"sv, GUI::InputType::NonemptyText, "Hexadecimal"sv);
  248. if (!result.is_error() && result.value() == GUI::InputBox::ExecResult::OK) {
  249. auto maybe_code_point = AK::StringUtils::convert_to_uint_from_hex(input);
  250. if (!maybe_code_point.has_value())
  251. return;
  252. auto code_point = maybe_code_point.value();
  253. code_point = clamp(code_point, m_range.first, m_range.last);
  254. m_glyph_map_widget->set_focus(true);
  255. m_glyph_map_widget->set_active_glyph(code_point);
  256. m_glyph_map_widget->scroll_to_glyph(code_point);
  257. }
  258. });
  259. m_go_to_glyph_action->set_status_tip("Go to the specified code point");
  260. m_previous_glyph_action = GUI::Action::create("Pre&vious Glyph", { Mod_Alt, Key_Left }, g_resources.previous_glyph, [this](auto&) {
  261. m_glyph_map_widget->select_previous_existing_glyph();
  262. });
  263. m_previous_glyph_action->set_status_tip("Seek the previous visible glyph");
  264. m_next_glyph_action = GUI::Action::create("&Next Glyph", { Mod_Alt, Key_Right }, g_resources.next_glyph, [this](auto&) {
  265. m_glyph_map_widget->select_next_existing_glyph();
  266. });
  267. m_next_glyph_action->set_status_tip("Seek the next visible glyph");
  268. i32 scale = Config::read_i32("FontEditor"sv, "GlyphEditor"sv, "Scale"sv, 10);
  269. m_glyph_editor_widget->set_scale(scale);
  270. m_scale_five_action = GUI::Action::create_checkable("500%", { Mod_Ctrl, Key_1 }, [this](auto&) {
  271. set_scale_and_save(5);
  272. });
  273. m_scale_five_action->set_checked(scale == 5);
  274. m_scale_five_action->set_status_tip("Scale the editor in proportion to the current font");
  275. m_scale_ten_action = GUI::Action::create_checkable("1000%", { Mod_Ctrl, Key_2 }, [this](auto&) {
  276. set_scale_and_save(10);
  277. });
  278. m_scale_ten_action->set_checked(scale == 10);
  279. m_scale_ten_action->set_status_tip("Scale the editor in proportion to the current font");
  280. m_scale_fifteen_action = GUI::Action::create_checkable("1500%", { Mod_Ctrl, Key_3 }, [this](auto&) {
  281. set_scale_and_save(15);
  282. });
  283. m_scale_fifteen_action->set_checked(scale == 15);
  284. m_scale_fifteen_action->set_status_tip("Scale the editor in proportion to the current font");
  285. m_glyph_editor_scale_actions.add_action(*m_scale_five_action);
  286. m_glyph_editor_scale_actions.add_action(*m_scale_ten_action);
  287. m_glyph_editor_scale_actions.add_action(*m_scale_fifteen_action);
  288. m_glyph_editor_scale_actions.set_exclusive(true);
  289. m_paint_glyph_action = GUI::Action::create_checkable("Paint Glyph", { Mod_Ctrl, KeyCode::Key_J }, g_resources.paint_glyph, [this](auto&) {
  290. m_glyph_editor_widget->set_mode(GlyphEditorWidget::Paint);
  291. });
  292. m_paint_glyph_action->set_checked(true);
  293. m_move_glyph_action = GUI::Action::create_checkable("Move Glyph", { Mod_Ctrl, KeyCode::Key_K }, g_resources.move_glyph, [this](auto&) {
  294. m_glyph_editor_widget->set_mode(GlyphEditorWidget::Move);
  295. });
  296. m_glyph_tool_actions.add_action(*m_paint_glyph_action);
  297. m_glyph_tool_actions.add_action(*m_move_glyph_action);
  298. m_glyph_tool_actions.set_exclusive(true);
  299. m_rotate_counterclockwise_action = GUI::CommonActions::make_rotate_counterclockwise_action([this](auto&) {
  300. m_glyph_editor_widget->rotate_90(Gfx::RotationDirection::CounterClockwise);
  301. });
  302. m_rotate_clockwise_action = GUI::CommonActions::make_rotate_clockwise_action([this](auto&) {
  303. m_glyph_editor_widget->rotate_90(Gfx::RotationDirection::Clockwise);
  304. });
  305. m_flip_horizontal_action = GUI::Action::create("Flip Horizontally", { Mod_Ctrl | Mod_Shift, Key_Q }, g_resources.flip_horizontally, [this](auto&) {
  306. m_glyph_editor_widget->flip(Gfx::Orientation::Horizontal);
  307. });
  308. m_flip_vertical_action = GUI::Action::create("Flip Vertically", { Mod_Ctrl | Mod_Shift, Key_W }, g_resources.flip_vertically, [this](auto&) {
  309. m_glyph_editor_widget->flip(Gfx::Orientation::Vertical);
  310. });
  311. m_copy_text_action = GUI::Action::create("Copy as Te&xt", { Mod_Ctrl, Key_T }, g_resources.copy_as_text, [this](auto&) {
  312. StringBuilder builder;
  313. auto selection = m_glyph_map_widget->selection().normalized();
  314. for (auto code_point = selection.start(); code_point < selection.start() + selection.size(); ++code_point) {
  315. if (!m_glyph_map_widget->font().contains_glyph(code_point))
  316. continue;
  317. builder.append_code_point(code_point);
  318. }
  319. GUI::Clipboard::the().set_plain_text(builder.to_deprecated_string());
  320. });
  321. m_copy_text_action->set_status_tip("Copy to clipboard as text");
  322. return {};
  323. }
  324. ErrorOr<void> MainWidget::create_toolbars()
  325. {
  326. auto& toolbar = *find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  327. (void)TRY(toolbar.try_add_action(*m_new_action));
  328. (void)TRY(toolbar.try_add_action(*m_open_action));
  329. (void)TRY(toolbar.try_add_action(*m_save_action));
  330. TRY(toolbar.try_add_separator());
  331. (void)TRY(toolbar.try_add_action(*m_cut_action));
  332. (void)TRY(toolbar.try_add_action(*m_copy_action));
  333. (void)TRY(toolbar.try_add_action(*m_paste_action));
  334. (void)TRY(toolbar.try_add_action(*m_delete_action));
  335. TRY(toolbar.try_add_separator());
  336. (void)TRY(toolbar.try_add_action(*m_undo_action));
  337. (void)TRY(toolbar.try_add_action(*m_redo_action));
  338. TRY(toolbar.try_add_separator());
  339. (void)TRY(toolbar.try_add_action(*m_open_preview_action));
  340. TRY(toolbar.try_add_separator());
  341. (void)TRY(toolbar.try_add_action(*m_previous_glyph_action));
  342. (void)TRY(toolbar.try_add_action(*m_next_glyph_action));
  343. (void)TRY(toolbar.try_add_action(*m_go_to_glyph_action));
  344. auto& glyph_transform_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("glyph_transform_toolbar");
  345. (void)TRY(glyph_transform_toolbar.try_add_action(*m_flip_horizontal_action));
  346. (void)TRY(glyph_transform_toolbar.try_add_action(*m_flip_vertical_action));
  347. (void)TRY(glyph_transform_toolbar.try_add_action(*m_rotate_counterclockwise_action));
  348. (void)TRY(glyph_transform_toolbar.try_add_action(*m_rotate_clockwise_action));
  349. auto& glyph_mode_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("glyph_mode_toolbar");
  350. (void)TRY(glyph_mode_toolbar.try_add_action(*m_paint_glyph_action));
  351. (void)TRY(glyph_mode_toolbar.try_add_action(*m_move_glyph_action));
  352. return {};
  353. }
  354. ErrorOr<void> MainWidget::create_models()
  355. {
  356. TRY(m_font_slope_list.try_ensure_capacity(Gfx::font_slope_names.size()));
  357. for (auto& it : Gfx::font_slope_names)
  358. m_font_slope_list.unchecked_append(TRY(String::from_utf8(it.name)));
  359. m_slope_combobox->set_model(TRY(GUI::ItemListModel<String>::try_create(m_font_slope_list)));
  360. TRY(m_font_weight_list.try_ensure_capacity(Gfx::font_weight_names.size()));
  361. for (auto& it : Gfx::font_weight_names)
  362. m_font_weight_list.unchecked_append(TRY(String::from_utf8(it.name)));
  363. m_weight_combobox->set_model(TRY(GUI::ItemListModel<String>::try_create(m_font_weight_list)));
  364. auto unicode_blocks = Unicode::block_display_names();
  365. TRY(m_unicode_block_list.try_ensure_capacity(unicode_blocks.size() + 1));
  366. m_unicode_block_list.unchecked_append(TRY(String::from_utf8("Show All"sv)));
  367. for (auto& block : unicode_blocks)
  368. m_unicode_block_list.unchecked_append(TRY(String::from_utf8(block.display_name)));
  369. m_unicode_block_model = TRY(GUI::ItemListModel<String>::try_create(m_unicode_block_list));
  370. m_filter_model = TRY(GUI::FilteringProxyModel::create(*m_unicode_block_model));
  371. m_filter_model->set_filter_term(""sv);
  372. m_unicode_block_listview = find_descendant_of_type_named<GUI::ListView>("unicode_block_listview");
  373. m_unicode_block_listview->on_selection_change = [this, unicode_blocks] {
  374. auto index = m_unicode_block_listview->selection().first();
  375. auto mapped_index = m_filter_model->map(index);
  376. if (mapped_index.row() > 0)
  377. m_range = unicode_blocks[mapped_index.row() - 1].code_point_range;
  378. else
  379. m_range = { 0x0000, 0x10FFFF };
  380. m_glyph_map_widget->set_active_range(m_range);
  381. };
  382. m_unicode_block_listview->set_model(m_filter_model);
  383. m_unicode_block_listview->set_activates_on_selection(true);
  384. m_unicode_block_listview->horizontal_scrollbar().set_visible(false);
  385. m_unicode_block_listview->set_cursor(m_unicode_block_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
  386. m_unicode_block_listview->set_focus_proxy(m_search_textbox);
  387. return {};
  388. }
  389. ErrorOr<void> MainWidget::create_undo_stack()
  390. {
  391. m_undo_stack = TRY(try_make<GUI::UndoStack>());
  392. m_undo_stack->on_state_change = [this] {
  393. m_undo_action->set_enabled(m_undo_stack->can_undo());
  394. m_redo_action->set_enabled(m_undo_stack->can_redo());
  395. update_action_text();
  396. if (m_undo_stack->is_current_modified())
  397. did_modify_font();
  398. };
  399. return {};
  400. }
  401. void MainWidget::update_action_text()
  402. {
  403. auto text_or_error = [](auto prefix, auto suffix) -> ErrorOr<String> {
  404. StringBuilder builder;
  405. TRY(builder.try_append(prefix));
  406. if (suffix.has_value()) {
  407. TRY(builder.try_append(' '));
  408. TRY(builder.try_append(suffix.value()));
  409. }
  410. return builder.to_string();
  411. };
  412. if (auto maybe_text = text_or_error("&Undo"sv, m_undo_stack->undo_action_text()); !maybe_text.is_error())
  413. m_undo_action->set_text(maybe_text.release_value().to_deprecated_string());
  414. if (auto maybe_text = text_or_error("&Redo"sv, m_undo_stack->redo_action_text()); !maybe_text.is_error())
  415. m_redo_action->set_text(maybe_text.release_value().to_deprecated_string());
  416. }
  417. ErrorOr<void> MainWidget::create_widgets()
  418. {
  419. TRY(load_from_gml(font_editor_window_gml));
  420. m_font_metadata_groupbox = find_descendant_of_type_named<GUI::GroupBox>("font_metadata_groupbox");
  421. m_unicode_block_container = find_descendant_of_type_named<GUI::Widget>("unicode_block_container");
  422. m_toolbar_container = find_descendant_of_type_named<GUI::ToolbarContainer>("toolbar_container");
  423. m_width_control_container = find_descendant_of_type_named<GUI::Widget>("width_control_container");
  424. m_glyph_map_widget = find_descendant_of_type_named<GUI::GlyphMapWidget>("glyph_map_widget");
  425. m_glyph_editor_widget = find_descendant_of_type_named<GlyphEditorWidget>("glyph_editor_widget");
  426. m_glyph_editor_widget->on_glyph_altered = [this](int glyph) {
  427. m_glyph_map_widget->update_glyph(glyph);
  428. update_preview();
  429. did_modify_font();
  430. };
  431. m_glyph_editor_widget->on_undo_event = [this](auto action_text) {
  432. reset_selection();
  433. push_undo(action_text);
  434. };
  435. m_glyph_editor_width_spinbox = find_descendant_of_type_named<GUI::SpinBox>("glyph_editor_width_spinbox");
  436. m_glyph_editor_present_checkbox = find_descendant_of_type_named<GUI::CheckBox>("glyph_editor_present_checkbox");
  437. m_glyph_map_widget->on_active_glyph_changed = [this](int glyph) {
  438. if (m_undo_selection) {
  439. auto selection = m_glyph_map_widget->selection().normalized();
  440. m_undo_selection->set_start(selection.start());
  441. m_undo_selection->set_size(selection.size());
  442. m_undo_selection->set_active_glyph(glyph);
  443. }
  444. m_glyph_editor_widget->set_glyph(glyph);
  445. auto glyph_width = m_font->raw_glyph_width(glyph);
  446. if (m_font->is_fixed_width())
  447. m_glyph_editor_present_checkbox->set_checked(glyph_width > 0, GUI::AllowCallback::No);
  448. else
  449. m_glyph_editor_width_spinbox->set_value(glyph_width, GUI::AllowCallback::No);
  450. update_statusbar();
  451. };
  452. m_glyph_map_widget->on_context_menu_request = [this](auto& event) {
  453. m_context_menu->popup(event.screen_position());
  454. };
  455. m_glyph_map_widget->on_escape_pressed = [this]() {
  456. update_statusbar();
  457. };
  458. m_name_textbox = find_descendant_of_type_named<GUI::TextBox>("name_textbox");
  459. m_name_textbox->on_change = [this] {
  460. m_font->set_name(m_name_textbox->text());
  461. did_modify_font();
  462. };
  463. m_family_textbox = find_descendant_of_type_named<GUI::TextBox>("family_textbox");
  464. m_family_textbox->on_change = [this] {
  465. m_font->set_family(m_family_textbox->text());
  466. did_modify_font();
  467. };
  468. m_fixed_width_checkbox = find_descendant_of_type_named<GUI::CheckBox>("fixed_width_checkbox");
  469. m_fixed_width_checkbox->on_checked = [this](bool checked) {
  470. m_font->set_fixed_width(checked);
  471. auto glyph_width = m_font->raw_glyph_width(m_glyph_map_widget->active_glyph());
  472. m_glyph_editor_width_spinbox->set_visible(!checked);
  473. m_glyph_editor_width_spinbox->set_value(glyph_width, GUI::AllowCallback::No);
  474. m_glyph_editor_present_checkbox->set_visible(checked);
  475. m_glyph_editor_present_checkbox->set_checked(glyph_width > 0, GUI::AllowCallback::No);
  476. m_glyph_editor_widget->update();
  477. update_preview();
  478. did_modify_font();
  479. };
  480. m_glyph_editor_width_spinbox->on_change = [this](int value) {
  481. reset_selection();
  482. push_undo("Resize Glyph"sv);
  483. m_font->set_glyph_width(m_glyph_map_widget->active_glyph(), value);
  484. m_glyph_editor_widget->update();
  485. m_glyph_map_widget->update_glyph(m_glyph_map_widget->active_glyph());
  486. update_preview();
  487. update_statusbar();
  488. did_modify_font();
  489. };
  490. m_glyph_editor_present_checkbox->on_checked = [this](bool checked) {
  491. reset_selection();
  492. push_undo("Resize Glyph"sv);
  493. m_font->set_glyph_width(m_glyph_map_widget->active_glyph(), checked ? m_font->glyph_fixed_width() : 0);
  494. m_glyph_editor_widget->update();
  495. m_glyph_map_widget->update_glyph(m_glyph_map_widget->active_glyph());
  496. update_preview();
  497. update_statusbar();
  498. did_modify_font();
  499. };
  500. m_weight_combobox = find_descendant_of_type_named<GUI::ComboBox>("weight_combobox");
  501. m_weight_combobox->on_change = [this](auto&, auto&) {
  502. m_font->set_weight(Gfx::name_to_weight(m_weight_combobox->text()));
  503. did_modify_font();
  504. };
  505. m_slope_combobox = find_descendant_of_type_named<GUI::ComboBox>("slope_combobox");
  506. m_slope_combobox->on_change = [this](auto&, auto&) {
  507. m_font->set_slope(Gfx::name_to_slope(m_slope_combobox->text()));
  508. did_modify_font();
  509. };
  510. m_presentation_spinbox = find_descendant_of_type_named<GUI::SpinBox>("presentation_spinbox");
  511. m_presentation_spinbox->on_change = [this](int value) {
  512. m_font->set_presentation_size(value);
  513. update_preview();
  514. did_modify_font();
  515. };
  516. m_spacing_spinbox = find_descendant_of_type_named<GUI::SpinBox>("spacing_spinbox");
  517. m_spacing_spinbox->on_change = [this](int value) {
  518. m_font->set_glyph_spacing(value);
  519. update_preview();
  520. did_modify_font();
  521. };
  522. m_baseline_spinbox = find_descendant_of_type_named<GUI::SpinBox>("baseline_spinbox");
  523. m_baseline_spinbox->on_change = [this](int value) {
  524. m_font->set_baseline(value);
  525. m_glyph_editor_widget->update();
  526. update_preview();
  527. did_modify_font();
  528. };
  529. m_mean_line_spinbox = find_descendant_of_type_named<GUI::SpinBox>("mean_line_spinbox");
  530. m_mean_line_spinbox->on_change = [this](int value) {
  531. m_font->set_mean_line(value);
  532. m_glyph_editor_widget->update();
  533. update_preview();
  534. did_modify_font();
  535. };
  536. m_search_textbox = find_descendant_of_type_named<GUI::TextBox>("search_textbox");
  537. m_search_textbox->on_return_pressed = [this] {
  538. if (!m_unicode_block_listview->selection().is_empty())
  539. m_unicode_block_listview->activate_selected();
  540. };
  541. m_search_textbox->on_down_pressed = [this] {
  542. m_unicode_block_listview->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  543. };
  544. m_search_textbox->on_up_pressed = [this] {
  545. m_unicode_block_listview->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
  546. };
  547. m_search_textbox->on_change = [this] {
  548. m_filter_model->set_filter_term(m_search_textbox->text());
  549. if (m_filter_model->row_count() != 0)
  550. m_unicode_block_listview->set_cursor(m_filter_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
  551. };
  552. m_statusbar = find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  553. m_statusbar->segment(1).set_mode(GUI::Statusbar::Segment::Mode::Auto);
  554. m_statusbar->segment(1).set_clickable(true);
  555. m_statusbar->segment(1).on_click = [this](auto) {
  556. m_show_unicode_blocks_action->activate();
  557. };
  558. GUI::Application::the()->on_action_enter = [this](GUI::Action& action) {
  559. auto text = action.status_tip();
  560. if (text.is_empty())
  561. text = Gfx::parse_ampersand_string(action.text());
  562. m_statusbar->set_override_text(move(text));
  563. };
  564. GUI::Application::the()->on_action_leave = [this](GUI::Action&) {
  565. m_statusbar->set_override_text({});
  566. };
  567. return {};
  568. }
  569. ErrorOr<void> MainWidget::initialize(StringView path, RefPtr<Gfx::BitmapFont>&& mutable_font)
  570. {
  571. VERIFY(window());
  572. if (m_font == mutable_font)
  573. return {};
  574. ScopeGuard reset_on_error([&] {
  575. if (!m_initialized)
  576. reset();
  577. });
  578. m_initialized = false;
  579. m_path = TRY(String::from_utf8(path));
  580. m_font = move(mutable_font);
  581. TRY(m_glyph_map_widget->initialize(m_font));
  582. auto active_glyph = m_glyph_map_widget->active_glyph();
  583. m_glyph_map_widget->set_focus(true);
  584. m_glyph_map_widget->scroll_to_glyph(active_glyph);
  585. auto selection = m_glyph_map_widget->selection().normalized();
  586. m_undo_selection = TRY(try_make_ref_counted<UndoSelection>(selection.start(), selection.size(), active_glyph, *m_font, *m_glyph_map_widget));
  587. m_undo_stack->clear();
  588. if (m_preview_label)
  589. m_preview_label->set_font(*m_font);
  590. m_glyph_editor_widget->initialize(m_font);
  591. m_glyph_editor_widget->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height());
  592. m_glyph_editor_widget->set_glyph(active_glyph);
  593. m_glyph_editor_width_spinbox->set_visible(!m_font->is_fixed_width());
  594. m_glyph_editor_width_spinbox->set_max(m_font->max_glyph_width(), GUI::AllowCallback::No);
  595. m_glyph_editor_width_spinbox->set_value(m_font->raw_glyph_width(active_glyph), GUI::AllowCallback::No);
  596. m_glyph_editor_present_checkbox->set_visible(m_font->is_fixed_width());
  597. m_glyph_editor_present_checkbox->set_checked(m_font->contains_raw_glyph(active_glyph), GUI::AllowCallback::No);
  598. m_fixed_width_checkbox->set_checked(m_font->is_fixed_width(), GUI::AllowCallback::No);
  599. m_name_textbox->set_text(m_font->name(), GUI::AllowCallback::No);
  600. m_family_textbox->set_text(m_font->family(), GUI::AllowCallback::No);
  601. m_presentation_spinbox->set_value(m_font->presentation_size(), GUI::AllowCallback::No);
  602. m_spacing_spinbox->set_value(m_font->glyph_spacing(), GUI::AllowCallback::No);
  603. m_mean_line_spinbox->set_range(0, max(m_font->glyph_height() - 2, 0), GUI::AllowCallback::No);
  604. m_baseline_spinbox->set_range(0, max(m_font->glyph_height() - 2, 0), GUI::AllowCallback::No);
  605. m_mean_line_spinbox->set_value(m_font->mean_line(), GUI::AllowCallback::No);
  606. m_baseline_spinbox->set_value(m_font->baseline(), GUI::AllowCallback::No);
  607. for (size_t i = 0; i < Gfx::font_weight_names.size(); ++i) {
  608. if (Gfx::font_weight_names[i].style == m_font->weight()) {
  609. m_weight_combobox->set_selected_index(i, GUI::AllowCallback::No);
  610. break;
  611. }
  612. }
  613. for (size_t i = 0; i < Gfx::font_slope_names.size(); ++i) {
  614. if (Gfx::font_slope_names[i].style == m_font->slope()) {
  615. m_slope_combobox->set_selected_index(i, GUI::AllowCallback::No);
  616. break;
  617. }
  618. }
  619. window()->set_modified(false);
  620. update_title();
  621. update_statusbar();
  622. set_actions_enabled(true);
  623. set_widgets_enabled(true);
  624. m_initialized = true;
  625. return {};
  626. }
  627. ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
  628. {
  629. auto file_menu = TRY(window.try_add_menu("&File"_short_string));
  630. TRY(file_menu->try_add_action(*m_new_action));
  631. TRY(file_menu->try_add_action(*m_open_action));
  632. TRY(file_menu->try_add_action(*m_save_action));
  633. TRY(file_menu->try_add_action(*m_save_as_action));
  634. TRY(file_menu->try_add_separator());
  635. TRY(file_menu->add_recent_files_list([this](auto& action) {
  636. if (!request_close())
  637. return;
  638. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(this->window(), action.text());
  639. if (response.is_error())
  640. return;
  641. auto file = response.release_value();
  642. if (auto result = open_file(file.filename(), file.release_stream()); result.is_error())
  643. show_error(result.release_error(), "Opening"sv, file.filename());
  644. }));
  645. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  646. if (!request_close())
  647. return;
  648. GUI::Application::the()->quit();
  649. })));
  650. auto edit_menu = TRY(window.try_add_menu("&Edit"_short_string));
  651. TRY(edit_menu->try_add_action(*m_undo_action));
  652. TRY(edit_menu->try_add_action(*m_redo_action));
  653. TRY(edit_menu->try_add_separator());
  654. TRY(edit_menu->try_add_action(*m_cut_action));
  655. TRY(edit_menu->try_add_action(*m_copy_action));
  656. TRY(edit_menu->try_add_action(*m_paste_action));
  657. TRY(edit_menu->try_add_action(*m_delete_action));
  658. TRY(edit_menu->try_add_separator());
  659. TRY(edit_menu->try_add_action(*m_select_all_action));
  660. TRY(edit_menu->try_add_separator());
  661. TRY(edit_menu->try_add_action(*m_copy_text_action));
  662. m_context_menu = edit_menu;
  663. auto go_menu = TRY(window.try_add_menu("&Go"_short_string));
  664. TRY(go_menu->try_add_action(*m_previous_glyph_action));
  665. TRY(go_menu->try_add_action(*m_next_glyph_action));
  666. TRY(go_menu->try_add_action(*m_go_to_glyph_action));
  667. auto view_menu = TRY(window.try_add_menu("&View"_short_string));
  668. auto layout_menu = TRY(view_menu->try_add_submenu("&Layout"_short_string));
  669. TRY(layout_menu->try_add_action(*m_show_toolbar_action));
  670. TRY(layout_menu->try_add_action(*m_show_statusbar_action));
  671. TRY(layout_menu->try_add_action(*m_show_metadata_action));
  672. TRY(layout_menu->try_add_action(*m_show_unicode_blocks_action));
  673. TRY(view_menu->try_add_separator());
  674. TRY(view_menu->try_add_action(*m_open_preview_action));
  675. TRY(view_menu->try_add_separator());
  676. TRY(view_menu->try_add_action(*m_highlight_modifications_action));
  677. TRY(view_menu->try_add_action(*m_show_system_emoji_action));
  678. TRY(view_menu->try_add_separator());
  679. auto scale_menu = TRY(view_menu->try_add_submenu("&Scale"_short_string));
  680. scale_menu->set_icon(g_resources.scale_editor);
  681. TRY(scale_menu->try_add_action(*m_scale_five_action));
  682. TRY(scale_menu->try_add_action(*m_scale_ten_action));
  683. TRY(scale_menu->try_add_action(*m_scale_fifteen_action));
  684. auto help_menu = TRY(window.try_add_menu("&Help"_short_string));
  685. TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(&window)));
  686. TRY(help_menu->try_add_action(GUI::CommonActions::make_help_action([](auto&) {
  687. Desktop::Launcher::open(URL::create_with_file_scheme("/usr/share/man/man1/Applications/FontEditor.md"), "/bin/Help");
  688. })));
  689. 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)));
  690. return {};
  691. }
  692. ErrorOr<void> MainWidget::save_file(StringView path, NonnullOwnPtr<Core::File> file)
  693. {
  694. auto masked_font = TRY(m_font->masked_character_set());
  695. TRY(masked_font->write_to_file(move(file)));
  696. m_path = TRY(String::from_utf8(path));
  697. m_undo_stack->set_current_unmodified();
  698. window()->set_modified(false);
  699. update_title();
  700. return {};
  701. }
  702. ErrorOr<void> MainWidget::open_file(StringView path, NonnullOwnPtr<Core::File> file)
  703. {
  704. auto mapped_file = TRY(Core::MappedFile::map_from_file(move(file), path));
  705. auto unmasked_font = TRY(TRY(Gfx::BitmapFont::try_load_from_mapped_file(mapped_file))->unmasked_character_set());
  706. TRY(initialize(path, move(unmasked_font)));
  707. if (!path.is_empty())
  708. GUI::Application::the()->set_most_recently_open_file(TRY(String::from_utf8(path)));
  709. return {};
  710. }
  711. void MainWidget::push_undo(StringView action_text)
  712. {
  713. auto maybe_state = m_undo_selection->save_state();
  714. if (maybe_state.is_error())
  715. return show_error(maybe_state.release_error(), "Saving undo state failed"sv);
  716. auto maybe_text = String::from_utf8(action_text);
  717. if (maybe_text.is_error())
  718. return show_error(maybe_text.release_error(), "Creating action text failed"sv);
  719. auto maybe_command = try_make<SelectionUndoCommand>(*m_undo_selection, move(maybe_state.value()), move(maybe_text.value()));
  720. if (maybe_command.is_error())
  721. return show_error(maybe_command.release_error(), "Making undo command failed"sv);
  722. if (auto maybe_push = m_undo_stack->try_push(move(maybe_command.value())); maybe_push.is_error())
  723. show_error(maybe_push.release_error(), "Pushing undo stack failed"sv);
  724. }
  725. void MainWidget::reset_selection()
  726. {
  727. auto selection = m_glyph_map_widget->selection().normalized();
  728. if (selection.size() == 1)
  729. return;
  730. auto start = m_glyph_map_widget->active_glyph();
  731. m_undo_selection->set_start(start);
  732. m_undo_selection->set_size(1);
  733. m_glyph_map_widget->set_selection(start, 1);
  734. m_glyph_map_widget->update();
  735. }
  736. void MainWidget::restore_state()
  737. {
  738. auto glyph = m_undo_selection->restored_active_glyph();
  739. auto glyph_width = m_font->raw_glyph_width(glyph);
  740. if (glyph < m_range.first || glyph > m_range.last)
  741. m_search_textbox->set_text(""sv);
  742. auto start = m_undo_selection->restored_start();
  743. auto size = m_undo_selection->restored_size();
  744. m_glyph_map_widget->restore_selection(start, size, glyph);
  745. m_glyph_map_widget->scroll_to_glyph(glyph);
  746. m_glyph_map_widget->set_focus(true);
  747. if (m_font->is_fixed_width())
  748. m_glyph_editor_present_checkbox->set_checked(glyph_width > 0, GUI::AllowCallback::No);
  749. else
  750. m_glyph_editor_width_spinbox->set_value(glyph_width, GUI::AllowCallback::No);
  751. m_glyph_editor_widget->update();
  752. m_glyph_map_widget->update();
  753. update_preview();
  754. update_statusbar();
  755. }
  756. void MainWidget::undo()
  757. {
  758. if (!m_undo_stack->can_undo())
  759. return;
  760. m_undo_stack->undo();
  761. restore_state();
  762. }
  763. void MainWidget::redo()
  764. {
  765. if (!m_undo_stack->can_redo())
  766. return;
  767. m_undo_stack->redo();
  768. restore_state();
  769. }
  770. bool MainWidget::request_close()
  771. {
  772. if (!window()->is_modified())
  773. return true;
  774. auto result = GUI::MessageBox::try_ask_about_unsaved_changes(window(), m_path, m_undo_stack->last_unmodified_timestamp());
  775. if (result.is_error())
  776. return false;
  777. if (result.value() == GUI::MessageBox::ExecResult::Yes) {
  778. m_save_action->activate();
  779. if (!window()->is_modified())
  780. return true;
  781. }
  782. if (result.value() == GUI::MessageBox::ExecResult::No)
  783. return true;
  784. return false;
  785. }
  786. void MainWidget::update_title()
  787. {
  788. StringBuilder title;
  789. if (m_path.is_empty())
  790. title.append("Untitled"sv);
  791. else
  792. title.append(m_path);
  793. title.append("[*] - Font Editor"sv);
  794. window()->set_title(title.to_deprecated_string());
  795. }
  796. void MainWidget::did_modify_font()
  797. {
  798. if (!window() || window()->is_modified())
  799. return;
  800. window()->set_modified(true);
  801. update_title();
  802. }
  803. void MainWidget::update_statusbar()
  804. {
  805. if (!m_font)
  806. return;
  807. if (!m_statusbar->is_visible())
  808. return;
  809. auto glyph = m_glyph_map_widget->active_glyph();
  810. StringBuilder builder;
  811. builder.appendff("U+{:04X} (", glyph);
  812. if (auto abbreviation = Unicode::code_point_abbreviation(glyph); abbreviation.has_value()) {
  813. builder.append(*abbreviation);
  814. } else if (Gfx::get_char_bidi_class(glyph) == Gfx::BidirectionalClass::STRONG_RTL) {
  815. // FIXME: This is a necessary hack, as RTL text will mess up the painting of the statusbar text.
  816. // For now, replace RTL glyphs with U+FFFD, the replacement character.
  817. builder.append_code_point(0xFFFD);
  818. } else {
  819. builder.append_code_point(glyph);
  820. }
  821. builder.append(')');
  822. auto glyph_name = Unicode::code_point_display_name(glyph);
  823. if (glyph_name.has_value()) {
  824. builder.appendff(" {}", glyph_name.value());
  825. }
  826. if (m_font->contains_raw_glyph(glyph))
  827. builder.appendff(" [{}x{}]", m_font->raw_glyph_width(glyph), m_font->glyph_height());
  828. else if (Gfx::Emoji::emoji_for_code_point(glyph))
  829. builder.appendff(" [emoji]");
  830. m_statusbar->set_text(builder.to_deprecated_string());
  831. builder.clear();
  832. auto selection = m_glyph_map_widget->selection().normalized();
  833. if (selection.size() > 1)
  834. builder.appendff("{} glyphs selected", selection.size());
  835. else
  836. builder.appendff("U+{:04X}-U+{:04X}", m_range.first, m_range.last);
  837. m_statusbar->set_text(1, builder.to_deprecated_string());
  838. }
  839. void MainWidget::update_preview()
  840. {
  841. if (m_font_preview_window)
  842. m_font_preview_window->update();
  843. }
  844. void MainWidget::drag_enter_event(GUI::DragEvent& event)
  845. {
  846. auto const& mime_types = event.mime_types();
  847. if (mime_types.contains_slow("text/uri-list"))
  848. event.accept();
  849. }
  850. void MainWidget::drop_event(GUI::DropEvent& event)
  851. {
  852. event.accept();
  853. if (event.mime_data().has_urls()) {
  854. auto urls = event.mime_data().urls();
  855. if (urls.is_empty())
  856. return;
  857. window()->move_to_front();
  858. if (!request_close())
  859. return;
  860. auto file_path = urls.first().serialize_path();
  861. auto result = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), file_path);
  862. if (result.is_error())
  863. return;
  864. auto file = result.release_value();
  865. if (auto result = open_file(file.filename(), file.release_stream()); result.is_error())
  866. show_error(result.release_error(), "Opening"sv, file.filename());
  867. }
  868. }
  869. void MainWidget::set_scale_and_save(i32 scale)
  870. {
  871. Config::write_i32("FontEditor"sv, "GlyphEditor"sv, "Scale"sv, scale);
  872. m_glyph_editor_widget->set_scale(scale);
  873. m_glyph_editor_widget->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height());
  874. }
  875. ErrorOr<void> MainWidget::copy_selected_glyphs()
  876. {
  877. size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height();
  878. auto selection = m_glyph_map_widget->selection().normalized();
  879. auto* rows = m_font->rows() + selection.start() * bytes_per_glyph;
  880. auto* widths = m_font->widths() + selection.start();
  881. ByteBuffer buffer;
  882. TRY(buffer.try_append(rows, bytes_per_glyph * selection.size()));
  883. TRY(buffer.try_append(widths, selection.size()));
  884. HashMap<DeprecatedString, DeprecatedString> metadata;
  885. metadata.set("start", DeprecatedString::number(selection.start()));
  886. metadata.set("count", DeprecatedString::number(selection.size()));
  887. metadata.set("width", DeprecatedString::number(m_font->max_glyph_width()));
  888. metadata.set("height", DeprecatedString::number(m_font->glyph_height()));
  889. GUI::Clipboard::the().set_data(buffer.bytes(), "glyph/x-fonteditor", metadata);
  890. return {};
  891. }
  892. ErrorOr<void> MainWidget::cut_selected_glyphs()
  893. {
  894. TRY(copy_selected_glyphs());
  895. delete_selected_glyphs();
  896. return {};
  897. }
  898. void MainWidget::paste_glyphs()
  899. {
  900. auto [data, mime_type, metadata] = GUI::Clipboard::the().fetch_data_and_type();
  901. if (!mime_type.starts_with("glyph/"sv))
  902. return;
  903. auto glyph_count = metadata.get("count").value().to_uint().value_or(0);
  904. if (!glyph_count)
  905. return;
  906. auto height = metadata.get("height").value().to_uint().value_or(0);
  907. if (!height)
  908. return;
  909. auto selection = m_glyph_map_widget->selection().normalized();
  910. auto range_bound_glyph_count = min(glyph_count, 1 + m_range.last - selection.start());
  911. m_undo_selection->set_size(range_bound_glyph_count);
  912. auto action_text = range_bound_glyph_count == 1 ? "Paste Glyph"sv : "Paste Glyphs"sv;
  913. push_undo(action_text);
  914. size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height();
  915. size_t bytes_per_copied_glyph = Gfx::GlyphBitmap::bytes_per_row() * height;
  916. size_t copyable_bytes_per_glyph = min(bytes_per_glyph, bytes_per_copied_glyph);
  917. auto* rows = m_font->rows() + selection.start() * bytes_per_glyph;
  918. auto* widths = m_font->widths() + selection.start();
  919. for (size_t i = 0; i < range_bound_glyph_count; ++i) {
  920. auto copyable_width = m_font->is_fixed_width()
  921. ? data[bytes_per_copied_glyph * glyph_count + i] ? m_font->glyph_fixed_width() : 0
  922. : min(m_font->max_glyph_width(), data[bytes_per_copied_glyph * glyph_count + i]);
  923. memcpy(&rows[i * bytes_per_glyph], &data[i * bytes_per_copied_glyph], copyable_bytes_per_glyph);
  924. memset(&widths[i], copyable_width, sizeof(u8));
  925. m_glyph_map_widget->set_glyph_modified(selection.start() + i, true);
  926. }
  927. m_glyph_map_widget->set_selection(selection.start() + range_bound_glyph_count - 1, -range_bound_glyph_count + 1);
  928. if (m_font->is_fixed_width())
  929. m_glyph_editor_present_checkbox->set_checked(m_font->contains_raw_glyph(m_glyph_map_widget->active_glyph()), GUI::AllowCallback::No);
  930. else
  931. m_glyph_editor_width_spinbox->set_value(m_font->raw_glyph_width(m_glyph_map_widget->active_glyph()), GUI::AllowCallback::No);
  932. m_glyph_editor_widget->update();
  933. m_glyph_map_widget->update();
  934. update_preview();
  935. update_statusbar();
  936. }
  937. void MainWidget::delete_selected_glyphs()
  938. {
  939. auto selection = m_glyph_map_widget->selection().normalized();
  940. auto action_text = selection.size() == 1 ? "Delete Glyph"sv : "Delete Glyphs"sv;
  941. push_undo(action_text);
  942. size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_font->glyph_height();
  943. auto* rows = m_font->rows() + selection.start() * bytes_per_glyph;
  944. auto* widths = m_font->widths() + selection.start();
  945. memset(rows, 0, bytes_per_glyph * selection.size());
  946. memset(widths, 0, selection.size());
  947. if (m_font->is_fixed_width())
  948. m_glyph_editor_present_checkbox->set_checked(false, GUI::AllowCallback::No);
  949. else
  950. m_glyph_editor_width_spinbox->set_value(0, GUI::AllowCallback::No);
  951. m_glyph_editor_widget->update();
  952. m_glyph_map_widget->update();
  953. update_preview();
  954. update_statusbar();
  955. }
  956. void MainWidget::show_error(Error error, StringView action, StringView filename)
  957. {
  958. auto format = filename.is_null() ? "{}{}: {}"sv : "{} \"{}\" failed: {}"sv;
  959. auto file = filename.is_null() ? StringView {} : filename;
  960. warnln(format, action, file, error);
  961. auto maybe_message = String::formatted(format, action, file, error);
  962. if (!maybe_message.is_error())
  963. (void)GUI::MessageBox::try_show_error(window(), maybe_message.release_value());
  964. }
  965. void MainWidget::reset()
  966. {
  967. VERIFY(window());
  968. m_initialized = false;
  969. m_font = nullptr;
  970. m_path = {};
  971. m_undo_selection = nullptr;
  972. m_undo_stack->clear();
  973. (void)m_glyph_map_widget->initialize(nullptr);
  974. m_glyph_editor_widget->initialize(nullptr);
  975. if (m_font_preview_window)
  976. m_font_preview_window->close();
  977. if (m_preview_label)
  978. m_preview_label->set_font(nullptr);
  979. m_name_textbox->set_text({}, GUI::AllowCallback::No);
  980. m_family_textbox->set_text({}, GUI::AllowCallback::No);
  981. m_slope_combobox->set_text({}, GUI::AllowCallback::No);
  982. m_weight_combobox->set_text({}, GUI::AllowCallback::No);
  983. m_presentation_spinbox->set_text({}, GUI::AllowCallback::No);
  984. m_baseline_spinbox->set_text({}, GUI::AllowCallback::No);
  985. m_mean_line_spinbox->set_text({}, GUI::AllowCallback::No);
  986. m_spacing_spinbox->set_text({}, GUI::AllowCallback::No);
  987. m_fixed_width_checkbox->set_checked(false, GUI::AllowCallback::No);
  988. m_statusbar->set_text(0, {});
  989. m_statusbar->set_text(1, {});
  990. window()->set_modified(false);
  991. window()->set_title("Font Editor");
  992. set_actions_enabled(false);
  993. set_widgets_enabled(false);
  994. set_focus(true);
  995. }
  996. void MainWidget::set_actions_enabled(bool enabled)
  997. {
  998. m_save_action->set_enabled(enabled);
  999. m_save_as_action->set_enabled(enabled);
  1000. m_cut_action->set_enabled(enabled);
  1001. m_copy_action->set_enabled(enabled);
  1002. m_paste_action->set_enabled(enabled && GUI::Clipboard::the().fetch_mime_type() == "glyph/x-fonteditor");
  1003. m_delete_action->set_enabled(enabled);
  1004. m_copy_text_action->set_enabled(enabled);
  1005. m_select_all_action->set_enabled(enabled);
  1006. m_go_to_glyph_action->set_enabled(enabled);
  1007. m_previous_glyph_action->set_enabled(enabled);
  1008. m_next_glyph_action->set_enabled(enabled);
  1009. m_move_glyph_action->set_enabled(enabled);
  1010. m_paint_glyph_action->set_enabled(enabled);
  1011. m_flip_horizontal_action->set_enabled(enabled);
  1012. m_flip_vertical_action->set_enabled(enabled);
  1013. m_rotate_clockwise_action->set_enabled(enabled);
  1014. m_rotate_counterclockwise_action->set_enabled(enabled);
  1015. m_open_preview_action->set_enabled(enabled);
  1016. m_highlight_modifications_action->set_enabled(enabled);
  1017. m_show_system_emoji_action->set_enabled(enabled);
  1018. m_scale_five_action->set_enabled(enabled);
  1019. m_scale_ten_action->set_enabled(enabled);
  1020. m_scale_fifteen_action->set_enabled(enabled);
  1021. }
  1022. void MainWidget::set_widgets_enabled(bool enabled)
  1023. {
  1024. m_font_metadata_groupbox->set_enabled(enabled);
  1025. m_unicode_block_container->set_enabled(enabled);
  1026. m_width_control_container->set_enabled(enabled);
  1027. m_width_control_container->set_visible(enabled);
  1028. m_glyph_map_widget->set_enabled(enabled);
  1029. m_glyph_editor_widget->set_enabled(enabled);
  1030. m_glyph_editor_widget->set_visible(enabled);
  1031. m_statusbar->segment(1).set_visible(enabled);
  1032. }
  1033. }