NewFontDialog.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "NewFontDialog.h"
  7. #include <AK/StringBuilder.h>
  8. #include <Applications/FontEditor/NewFontDialogPage1GML.h>
  9. #include <Applications/FontEditor/NewFontDialogPage2GML.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/CheckBox.h>
  12. #include <LibGUI/ComboBox.h>
  13. #include <LibGUI/FontPickerWeightModel.h>
  14. #include <LibGUI/Label.h>
  15. #include <LibGUI/MessageBox.h>
  16. #include <LibGUI/Painter.h>
  17. #include <LibGUI/SpinBox.h>
  18. #include <LibGUI/TextBox.h>
  19. #include <LibGUI/Widget.h>
  20. #include <LibGUI/Wizards/WizardDialog.h>
  21. #include <LibGfx/BitmapFont.h>
  22. #include <LibGfx/Palette.h>
  23. static constexpr int s_max_width = 32;
  24. static constexpr int s_max_height = 36;
  25. namespace GUI {
  26. class GlyphPreviewWidget final : public Frame {
  27. C_OBJECT(GlyphPreviewWidget)
  28. public:
  29. void set_preview_size(int width, int height)
  30. {
  31. m_width = width;
  32. m_height = height;
  33. m_glyph_width = width;
  34. for (int i = 10; i > 0; i--) {
  35. if ((frame_thickness() * 2 + (m_width * i) - 1) <= 250
  36. && (frame_thickness() * 2 + (m_height * i) - 1) <= 205) {
  37. set_scale(i);
  38. break;
  39. }
  40. }
  41. set_fixed_width(frame_thickness() * 2 + (m_width * m_scale) - 1);
  42. set_fixed_height(frame_thickness() * 2 + (m_height * m_scale) - 1);
  43. }
  44. void set_scale(int scale) { m_scale = scale; }
  45. void set_baseline(int i) { m_baseline = i; }
  46. void set_mean_line(int i) { m_mean_line = i; }
  47. private:
  48. GlyphPreviewWidget()
  49. {
  50. set_preview_size(m_width, m_height);
  51. }
  52. virtual void paint_event(PaintEvent& event) override
  53. {
  54. Frame::paint_event(event);
  55. Painter painter(*this);
  56. painter.add_clip_rect(frame_inner_rect());
  57. painter.add_clip_rect(event.rect());
  58. painter.fill_rect(frame_inner_rect(), palette().base());
  59. painter.translate(frame_thickness(), frame_thickness());
  60. painter.translate(-1, -1);
  61. for (int y = 1; y < m_height; ++y) {
  62. int y_below = y - 1;
  63. bool bold_line = y_below == m_baseline || y_below == m_mean_line;
  64. painter.draw_line({ 0, y * m_scale }, { m_width * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1);
  65. }
  66. for (int x = 1; x < m_width; ++x)
  67. painter.draw_line({ x * m_scale, 0 }, { x * m_scale, m_height * m_scale }, palette().threed_shadow2());
  68. for (int y = 0; y < m_height; ++y) {
  69. for (int x = 0; x < m_width; ++x) {
  70. Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale };
  71. if (x >= m_glyph_width) {
  72. painter.fill_rect(rect, palette().threed_shadow1());
  73. } else {
  74. if (m_bits[x][y])
  75. painter.fill_rect(rect, palette().base_text());
  76. }
  77. }
  78. }
  79. }
  80. virtual void mousedown_event(MouseEvent& event) override
  81. {
  82. draw_at_mouse(event);
  83. }
  84. virtual void mousemove_event(MouseEvent& event) override
  85. {
  86. if (event.buttons() & (GUI::MouseButton::Left | GUI::MouseButton::Right))
  87. draw_at_mouse(event);
  88. }
  89. void draw_at_mouse(const MouseEvent& event)
  90. {
  91. bool set = event.buttons() & MouseButton::Left;
  92. bool unset = event.buttons() & MouseButton::Right;
  93. if (!(set ^ unset))
  94. return;
  95. int x = (event.x() - 1) / m_scale;
  96. int y = (event.y() - 1) / m_scale;
  97. if (x < 0 || x >= m_width)
  98. return;
  99. if (y < 0 || y >= m_height)
  100. return;
  101. if (m_bits[x][y] == set)
  102. return;
  103. m_bits[x][y] = set;
  104. update();
  105. }
  106. int m_scale { 20 };
  107. int m_width { 20 };
  108. int m_height { 20 };
  109. int m_glyph_width { 20 };
  110. int m_mean_line { 2 };
  111. int m_baseline { 16 };
  112. u8 m_bits[s_max_width][s_max_height] {};
  113. };
  114. }
  115. NewFontDialog::NewFontDialog(GUI::Window* parent_window)
  116. : GUI::WizardDialog(parent_window)
  117. {
  118. set_title("New Font");
  119. m_font_properties_page = GUI::WizardPage::construct("Font properties", "Edit details about this font.");
  120. m_font_properties_page->body_widget().load_from_gml(new_font_dialog_page_1_gml);
  121. m_name_textbox = m_font_properties_page->body_widget().find_descendant_of_type_named<GUI::TextBox>("name_textbox");
  122. m_family_textbox = m_font_properties_page->body_widget().find_descendant_of_type_named<GUI::TextBox>("family_textbox");
  123. m_type_combobox = m_font_properties_page->body_widget().find_descendant_of_type_named<GUI::ComboBox>("type_combobox");
  124. m_type_info_label = m_font_properties_page->body_widget().find_descendant_of_type_named<GUI::Label>("type_info_label");
  125. m_weight_combobox = m_font_properties_page->body_widget().find_descendant_of_type_named<GUI::ComboBox>("weight_combobox");
  126. m_presentation_spinbox = m_font_properties_page->body_widget().find_descendant_of_type_named<GUI::SpinBox>("presentation_spinbox");
  127. m_type_info_label->set_text("\xE2\x84\xB9\t");
  128. m_type_info_label->set_tooltip("Font type governs maximum glyph count");
  129. for (auto& it : GUI::font_weight_names)
  130. m_font_weight_list.append(it.name);
  131. m_weight_combobox->set_model(*GUI::ItemListModel<String>::create(m_font_weight_list));
  132. m_weight_combobox->set_selected_index(3);
  133. StringBuilder type_count;
  134. for (int i = 0; i < Gfx::FontTypes::__Count; i++) {
  135. type_count.appendff("{} ({})",
  136. Gfx::BitmapFont::type_name_by_type(static_cast<Gfx::FontTypes>(i)),
  137. Gfx::BitmapFont::glyph_count_by_type(static_cast<Gfx::FontTypes>(i)));
  138. m_font_type_list.append(type_count.to_string());
  139. type_count.clear();
  140. }
  141. m_type_combobox->set_model(*GUI::ItemListModel<String>::create(m_font_type_list));
  142. m_type_combobox->set_selected_index(0);
  143. m_presentation_spinbox->set_value(12);
  144. m_font_properties_page->on_page_enter = [&]() {
  145. m_name_textbox->set_focus(true);
  146. };
  147. m_font_properties_page->on_next_page = [&]() {
  148. return m_glyph_properties_page;
  149. };
  150. m_glyph_properties_page = GUI::WizardPage::construct("Glyph properties", "Edit details about this font.");
  151. m_glyph_properties_page->body_widget().load_from_gml(new_font_dialog_page_2_gml);
  152. m_glyph_properties_page->set_is_final_page(true);
  153. m_glyph_editor_container = m_glyph_properties_page->body_widget().find_descendant_of_type_named<GUI::Widget>("glyph_editor_container");
  154. m_glyph_height_spinbox = m_glyph_properties_page->body_widget().find_descendant_of_type_named<GUI::SpinBox>("height_spinbox");
  155. m_glyph_width_spinbox = m_glyph_properties_page->body_widget().find_descendant_of_type_named<GUI::SpinBox>("width_spinbox");
  156. m_baseline_spinbox = m_glyph_properties_page->body_widget().find_descendant_of_type_named<GUI::SpinBox>("baseline_spinbox");
  157. m_mean_line_spinbox = m_glyph_properties_page->body_widget().find_descendant_of_type_named<GUI::SpinBox>("mean_line_spinbox");
  158. m_spacing_spinbox = m_glyph_properties_page->body_widget().find_descendant_of_type_named<GUI::SpinBox>("spacing_spinbox");
  159. m_fixed_width_checkbox = m_glyph_properties_page->body_widget().find_descendant_of_type_named<GUI::CheckBox>("fixed_width_checkbox");
  160. m_glyph_height_spinbox->set_value(20);
  161. m_glyph_width_spinbox->set_value(20);
  162. m_glyph_height_spinbox->set_max(s_max_height);
  163. m_glyph_width_spinbox->set_max(s_max_width);
  164. m_mean_line_spinbox->set_value(2);
  165. m_baseline_spinbox->set_value(16);
  166. m_mean_line_spinbox->set_max(max(m_glyph_height_spinbox->value() - 2, 0));
  167. m_baseline_spinbox->set_max(max(m_glyph_height_spinbox->value() - 2, 0));
  168. m_spacing_spinbox->set_value(1);
  169. m_fixed_width_checkbox->set_checked(false);
  170. auto& preview_editor = m_glyph_editor_container->add<GUI::GlyphPreviewWidget>();
  171. preview_editor.set_preview_size(20, 20);
  172. m_glyph_editor_container->set_fixed_height(20 * 20 + preview_editor.frame_thickness() * 4);
  173. m_glyph_width_spinbox->on_change = [&](int value) {
  174. preview_editor.set_preview_size(value, m_glyph_height_spinbox->value());
  175. deferred_invoke([&] {
  176. m_glyph_editor_container->set_fixed_height(1 + preview_editor.height() + preview_editor.frame_thickness() * 2);
  177. });
  178. };
  179. m_glyph_height_spinbox->on_change = [&](int value) {
  180. preview_editor.set_preview_size(m_glyph_width_spinbox->value(), value);
  181. m_mean_line_spinbox->set_max(max(value - 2, 0));
  182. m_baseline_spinbox->set_max(max(value - 2, 0));
  183. deferred_invoke([&] {
  184. m_glyph_editor_container->set_fixed_height(1 + preview_editor.height() + preview_editor.frame_thickness() * 2);
  185. });
  186. };
  187. m_baseline_spinbox->on_change = [&](int value) {
  188. preview_editor.set_baseline(value);
  189. preview_editor.update();
  190. };
  191. m_mean_line_spinbox->on_change = [&](int value) {
  192. preview_editor.set_mean_line(value);
  193. preview_editor.update();
  194. };
  195. push_page(*m_font_properties_page);
  196. }
  197. void NewFontDialog::save_metadata()
  198. {
  199. m_new_font_metadata.name = m_name_textbox->text();
  200. m_new_font_metadata.family = m_family_textbox->text();
  201. m_new_font_metadata.weight = GUI::name_to_weight(m_weight_combobox->text());
  202. m_new_font_metadata.type = static_cast<Gfx::FontTypes>(m_type_combobox->selected_index());
  203. m_new_font_metadata.presentation_size = m_presentation_spinbox->value();
  204. m_new_font_metadata.baseline = m_baseline_spinbox->value();
  205. m_new_font_metadata.mean_line = m_mean_line_spinbox->value();
  206. m_new_font_metadata.glyph_height = m_glyph_height_spinbox->value();
  207. m_new_font_metadata.glyph_width = m_glyph_width_spinbox->value();
  208. m_new_font_metadata.glyph_spacing = m_spacing_spinbox->value();
  209. m_new_font_metadata.is_fixed_width = m_fixed_width_checkbox->is_checked();
  210. }