NewFontDialog.cpp 9.2 KB

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