MainWidget.cpp 50 KB

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