FontEditor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "FontEditor.h"
  27. #include "GlyphEditorWidget.h"
  28. #include "GlyphMapWidget.h"
  29. #include <AK/StringBuilder.h>
  30. #include <Applications/FontEditor/FontEditorWindowGML.h>
  31. #include <LibDesktop/Launcher.h>
  32. #include <LibGUI/Action.h>
  33. #include <LibGUI/Application.h>
  34. #include <LibGUI/BoxLayout.h>
  35. #include <LibGUI/Button.h>
  36. #include <LibGUI/CheckBox.h>
  37. #include <LibGUI/Clipboard.h>
  38. #include <LibGUI/ComboBox.h>
  39. #include <LibGUI/FilePicker.h>
  40. #include <LibGUI/FontPickerWeightModel.h>
  41. #include <LibGUI/GroupBox.h>
  42. #include <LibGUI/Label.h>
  43. #include <LibGUI/Menu.h>
  44. #include <LibGUI/MenuBar.h>
  45. #include <LibGUI/MessageBox.h>
  46. #include <LibGUI/Painter.h>
  47. #include <LibGUI/SpinBox.h>
  48. #include <LibGUI/StatusBar.h>
  49. #include <LibGUI/TextBox.h>
  50. #include <LibGUI/ToolBarContainer.h>
  51. #include <LibGUI/Window.h>
  52. #include <LibGfx/BitmapFont.h>
  53. #include <LibGfx/Palette.h>
  54. #include <stdlib.h>
  55. static RefPtr<GUI::Window> create_font_preview_window(FontEditorWidget& editor)
  56. {
  57. auto window = GUI::Window::construct();
  58. window->set_window_type(GUI::WindowType::ToolWindow);
  59. window->set_title("Font preview");
  60. window->resize(400, 150);
  61. window->set_minimum_size(200, 100);
  62. window->center_within(*editor.window());
  63. auto& main_widget = window->set_main_widget<GUI::Widget>();
  64. main_widget.set_fill_with_background_color(true);
  65. main_widget.set_layout<GUI::VerticalBoxLayout>();
  66. main_widget.layout()->set_margins({ 2, 2, 2, 2 });
  67. main_widget.layout()->set_spacing(4);
  68. auto& preview_box = main_widget.add<GUI::GroupBox>();
  69. preview_box.set_layout<GUI::VerticalBoxLayout>();
  70. preview_box.layout()->set_margins({ 8, 8, 8, 8 });
  71. auto& preview_label = preview_box.add<GUI::Label>();
  72. preview_label.set_font(editor.edited_font());
  73. editor.on_initialize = [&] {
  74. preview_label.set_font(editor.edited_font());
  75. };
  76. auto& preview_textbox = main_widget.add<GUI::TextBox>();
  77. preview_textbox.set_text("waxy and quivering jocks fumble the pizza");
  78. preview_textbox.set_placeholder("Preview text");
  79. preview_textbox.on_change = [&] {
  80. auto preview = String::formatted("{}\n{}",
  81. preview_textbox.text(),
  82. preview_textbox.text().to_uppercase());
  83. preview_label.set_text(preview);
  84. };
  85. return window;
  86. }
  87. FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&& edited_font)
  88. {
  89. load_from_gml(font_editor_window_gml);
  90. auto& toolbar = *find_descendant_of_type_named<GUI::ToolBar>("toolbar");
  91. auto& status_bar = *find_descendant_of_type_named<GUI::StatusBar>("status_bar");
  92. auto& glyph_map_container = *find_descendant_of_type_named<GUI::Widget>("glyph_map_container");
  93. m_glyph_editor_container = *find_descendant_of_type_named<GUI::Widget>("glyph_editor_container");
  94. m_left_column_container = *find_descendant_of_type_named<GUI::Widget>("left_column_container");
  95. m_glyph_editor_width_spinbox = *find_descendant_of_type_named<GUI::SpinBox>("glyph_editor_width_spinbox");
  96. m_name_textbox = *find_descendant_of_type_named<GUI::TextBox>("name_textbox");
  97. m_family_textbox = *find_descendant_of_type_named<GUI::TextBox>("family_textbox");
  98. m_presentation_spinbox = *find_descendant_of_type_named<GUI::SpinBox>("presentation_spinbox");
  99. m_weight_combobox = *find_descendant_of_type_named<GUI::ComboBox>("weight_combobox");
  100. m_type_combobox = *find_descendant_of_type_named<GUI::ComboBox>("type_combobox");
  101. m_spacing_spinbox = *find_descendant_of_type_named<GUI::SpinBox>("spacing_spinbox");
  102. m_mean_line_spinbox = *find_descendant_of_type_named<GUI::SpinBox>("mean_line_spinbox");
  103. m_baseline_spinbox = *find_descendant_of_type_named<GUI::SpinBox>("baseline_spinbox");
  104. m_fixed_width_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("fixed_width_checkbox");
  105. m_font_metadata_groupbox = *find_descendant_of_type_named<GUI::GroupBox>("font_metadata_groupbox");
  106. m_glyph_editor_widget = m_glyph_editor_container->add<GlyphEditorWidget>();
  107. m_glyph_map_widget = glyph_map_container.add<GlyphMapWidget>();
  108. auto update_demo = [&] {
  109. if (m_font_preview_window)
  110. m_font_preview_window->update();
  111. };
  112. auto open_action = GUI::CommonActions::make_open_action([&](auto&) {
  113. Optional<String> open_path = GUI::FilePicker::get_open_filepath(window(), {}, "/res/fonts/");
  114. if (!open_path.has_value())
  115. return;
  116. auto bitmap_font = Gfx::BitmapFont::load_from_file(open_path.value());
  117. if (!bitmap_font) {
  118. String message = String::formatted("Couldn't load font: {}\n", open_path.value());
  119. GUI::MessageBox::show(window(), message, "Font Editor", GUI::MessageBox::Type::Error);
  120. return;
  121. }
  122. RefPtr<Gfx::BitmapFont> new_font = static_ptr_cast<Gfx::BitmapFont>(bitmap_font->clone());
  123. if (!new_font) {
  124. String message = String::formatted("Couldn't load font: {}\n", open_path.value());
  125. GUI::MessageBox::show(window(), message, "Font Editor", GUI::MessageBox::Type::Error);
  126. return;
  127. }
  128. window()->set_title(String::formatted("{} - Font Editor", open_path.value()));
  129. initialize(open_path.value(), move(new_font));
  130. });
  131. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  132. save_as(m_path);
  133. });
  134. m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  135. LexicalPath lexical_path(m_path);
  136. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), lexical_path.title(), lexical_path.extension());
  137. if (!save_path.has_value())
  138. return;
  139. if (save_as(save_path.value()))
  140. window()->set_title(String::formatted("{} - Font Editor", save_path.value()));
  141. });
  142. m_cut_action = GUI::CommonActions::make_cut_action([&](auto&) {
  143. m_glyph_editor_widget->cut_glyph();
  144. });
  145. m_copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
  146. m_glyph_editor_widget->copy_glyph();
  147. });
  148. m_paste_action = GUI::CommonActions::make_paste_action([&](auto&) {
  149. m_glyph_editor_widget->paste_glyph();
  150. m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
  151. });
  152. m_paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "glyph/x-fonteditor");
  153. m_delete_action = GUI::CommonActions::make_delete_action([&](auto&) {
  154. m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), m_edited_font->max_glyph_width());
  155. m_glyph_editor_widget->delete_glyph();
  156. m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
  157. m_glyph_editor_width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
  158. });
  159. m_open_preview_action = GUI::Action::create("Preview Font", Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"), [&](auto&) {
  160. if (!m_font_preview_window)
  161. m_font_preview_window = create_font_preview_window(*this);
  162. m_font_preview_window->show();
  163. m_font_preview_window->move_to_front();
  164. });
  165. m_open_preview_action->set_checked(false);
  166. m_show_metadata_action = GUI::Action::create_checkable("Font Metadata", { Mod_Ctrl, Key_M }, [&](auto& action) {
  167. set_show_font_metadata(action.is_checked());
  168. });
  169. m_show_metadata_action->set_checked(true);
  170. toolbar.add_action(*m_new_action);
  171. toolbar.add_action(*m_open_action);
  172. toolbar.add_action(*m_save_action);
  173. toolbar.add_separator();
  174. toolbar.add_action(*m_cut_action);
  175. toolbar.add_action(*m_copy_action);
  176. toolbar.add_action(*m_paste_action);
  177. toolbar.add_action(*m_delete_action);
  178. toolbar.add_separator();
  179. toolbar.add_action(*m_open_preview_action);
  180. GUI::Clipboard::the().on_change = [&](const String& data_type) {
  181. m_paste_action->set_enabled(data_type == "glyph/x-fonteditor");
  182. };
  183. m_glyph_editor_widget->on_glyph_altered = [this, update_demo](int glyph) {
  184. m_glyph_map_widget->update_glyph(glyph);
  185. update_demo();
  186. };
  187. m_glyph_map_widget->on_glyph_selected = [&](int glyph) {
  188. m_glyph_editor_widget->set_glyph(glyph);
  189. m_glyph_editor_width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
  190. StringBuilder builder;
  191. builder.appendff("{:#02x} (", glyph);
  192. if (glyph < 128) {
  193. if (glyph == 10)
  194. builder.append("LF");
  195. else
  196. builder.append(glyph);
  197. } else {
  198. builder.append(128 | 64 | (glyph / 64));
  199. builder.append(128 | (glyph % 64));
  200. }
  201. builder.append(") ");
  202. builder.appendff("[{}x{}]", m_edited_font->glyph_width(glyph), m_edited_font->glyph_height());
  203. status_bar.set_text(builder.to_string());
  204. };
  205. m_name_textbox->on_change = [&] {
  206. m_edited_font->set_name(m_name_textbox->text());
  207. };
  208. m_family_textbox->on_change = [&] {
  209. m_edited_font->set_family(m_family_textbox->text());
  210. };
  211. m_fixed_width_checkbox->on_checked = [&, update_demo](bool checked) {
  212. m_edited_font->set_fixed_width(checked);
  213. m_glyph_editor_width_spinbox->set_enabled(!m_edited_font->is_fixed_width());
  214. m_glyph_editor_width_spinbox->set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
  215. m_glyph_editor_widget->update();
  216. update_demo();
  217. };
  218. m_glyph_editor_width_spinbox->on_change = [this, update_demo](int value) {
  219. m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), value);
  220. m_glyph_editor_widget->update();
  221. m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
  222. update_demo();
  223. };
  224. m_weight_combobox->on_change = [this]() {
  225. m_edited_font->set_weight(GUI::name_to_weight(m_weight_combobox->text()));
  226. };
  227. m_type_combobox->on_change = [this](auto&, const auto& index) {
  228. m_edited_font->set_type(static_cast<Gfx::FontTypes>(index.row()));
  229. };
  230. m_presentation_spinbox->on_change = [this, update_demo](int value) {
  231. m_edited_font->set_presentation_size(value);
  232. update_demo();
  233. };
  234. m_spacing_spinbox->on_change = [this, update_demo](int value) {
  235. m_edited_font->set_glyph_spacing(value);
  236. update_demo();
  237. };
  238. m_baseline_spinbox->on_change = [this, update_demo](int value) {
  239. m_edited_font->set_baseline(value);
  240. m_glyph_editor_widget->update();
  241. update_demo();
  242. };
  243. m_mean_line_spinbox->on_change = [this, update_demo](int value) {
  244. m_edited_font->set_mean_line(value);
  245. m_glyph_editor_widget->update();
  246. update_demo();
  247. };
  248. initialize(path, move(edited_font));
  249. }
  250. FontEditorWidget::~FontEditorWidget()
  251. {
  252. }
  253. void FontEditorWidget::initialize(const String& path, RefPtr<Gfx::BitmapFont>&& edited_font)
  254. {
  255. if (m_edited_font == edited_font)
  256. return;
  257. m_path = path;
  258. m_edited_font = edited_font;
  259. m_glyph_map_widget->initialize(*m_edited_font);
  260. m_glyph_editor_widget->initialize(*m_edited_font);
  261. m_glyph_editor_container->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height());
  262. m_left_column_container->set_fixed_width(m_glyph_editor_widget->preferred_width());
  263. m_glyph_editor_width_spinbox->set_enabled(!m_edited_font->is_fixed_width());
  264. m_glyph_editor_width_spinbox->set_max(m_edited_font->max_glyph_width());
  265. m_name_textbox->set_text(m_edited_font->name());
  266. m_family_textbox->set_text(m_edited_font->family());
  267. m_presentation_spinbox->set_value(m_edited_font->presentation_size());
  268. m_spacing_spinbox->set_value(m_edited_font->glyph_spacing());
  269. m_mean_line_spinbox->set_value(m_edited_font->mean_line());
  270. m_baseline_spinbox->set_value(m_edited_font->baseline());
  271. m_font_weight_list.clear();
  272. for (auto& it : GUI::font_weight_names)
  273. m_font_weight_list.append(it.name);
  274. m_weight_combobox->set_model(*GUI::ItemListModel<String>::create(m_font_weight_list));
  275. int i = 0;
  276. for (auto it : GUI::font_weight_names) {
  277. if (it.weight == m_edited_font->weight()) {
  278. m_weight_combobox->set_selected_index(i);
  279. break;
  280. }
  281. i++;
  282. }
  283. m_font_type_list.clear();
  284. StringBuilder type_count;
  285. for (int i = 0; i < Gfx::FontTypes::__Count; i++) {
  286. type_count.appendff("{}", Gfx::BitmapFont::type_name_by_type(static_cast<Gfx::FontTypes>(i)));
  287. m_font_type_list.append(type_count.to_string());
  288. type_count.clear();
  289. }
  290. m_type_combobox->set_model(*GUI::ItemListModel<String>::create(m_font_type_list));
  291. m_type_combobox->set_selected_index(m_edited_font->type());
  292. m_fixed_width_checkbox->set_checked(m_edited_font->is_fixed_width());
  293. m_glyph_map_widget->set_selected_glyph('A');
  294. deferred_invoke([this](auto&) {
  295. m_glyph_map_widget->set_focus(true);
  296. m_glyph_map_widget->scroll_to_glyph(m_glyph_map_widget->selected_glyph());
  297. });
  298. if (on_initialize)
  299. on_initialize();
  300. }
  301. void FontEditorWidget::initialize_menubar(GUI::MenuBar& menubar)
  302. {
  303. auto& app_menu = menubar.add_menu("&File");
  304. app_menu.add_action(*m_new_action);
  305. app_menu.add_action(*m_open_action);
  306. app_menu.add_action(*m_save_action);
  307. app_menu.add_action(*m_save_as_action);
  308. app_menu.add_separator();
  309. app_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  310. GUI::Application::the()->quit();
  311. }));
  312. auto& edit_menu = menubar.add_menu("&Edit");
  313. edit_menu.add_action(*m_cut_action);
  314. edit_menu.add_action(*m_copy_action);
  315. edit_menu.add_action(*m_paste_action);
  316. edit_menu.add_action(*m_delete_action);
  317. auto& view_menu = menubar.add_menu("&View");
  318. view_menu.add_action(*m_open_preview_action);
  319. view_menu.add_separator();
  320. view_menu.add_action(*m_show_metadata_action);
  321. auto& help_menu = menubar.add_menu("&Help");
  322. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  323. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/FontEditor.md"), "/bin/Help");
  324. }));
  325. help_menu.add_action(GUI::CommonActions::make_about_action("Font Editor", GUI::Icon::default_icon("app-font-editor"), window()));
  326. }
  327. bool FontEditorWidget::save_as(const String& path)
  328. {
  329. auto ret_val = m_edited_font->write_to_file(path);
  330. if (!ret_val) {
  331. GUI::MessageBox::show(window(), "The font file could not be saved.", "Save failed", GUI::MessageBox::Type::Error);
  332. return false;
  333. }
  334. m_path = path;
  335. return true;
  336. }
  337. void FontEditorWidget::set_show_font_metadata(bool show)
  338. {
  339. if (m_font_metadata == show)
  340. return;
  341. m_font_metadata = show;
  342. m_font_metadata_groupbox->set_visible(m_font_metadata);
  343. }