MainWidget.cpp 50 KB

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