MainWidget.cpp 44 KB

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