MainWidget.cpp 45 KB

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