MainWidget.cpp 50 KB

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