FontPicker.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibGUI/Button.h>
  8. #include <LibGUI/FontPicker.h>
  9. #include <LibGUI/FontPickerDialogGML.h>
  10. #include <LibGUI/FontPickerWeightModel.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGUI/ListView.h>
  13. #include <LibGUI/Scrollbar.h>
  14. #include <LibGUI/SpinBox.h>
  15. #include <LibGUI/Widget.h>
  16. #include <LibGfx/FontDatabase.h>
  17. namespace GUI {
  18. FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, bool fixed_width_only)
  19. : Dialog(parent_window)
  20. , m_fixed_width_only(fixed_width_only)
  21. {
  22. set_title("Font picker");
  23. resize(430, 280);
  24. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-font-editor.png"));
  25. auto& widget = set_main_widget<GUI::Widget>();
  26. if (!widget.load_from_gml(font_picker_dialog_gml))
  27. VERIFY_NOT_REACHED();
  28. m_family_list_view = *widget.find_descendant_of_type_named<ListView>("family_list_view");
  29. m_family_list_view->set_model(ItemListModel<String>::create(m_families));
  30. m_family_list_view->horizontal_scrollbar().set_visible(false);
  31. m_weight_list_view = *widget.find_descendant_of_type_named<ListView>("weight_list_view");
  32. m_weight_list_view->set_model(adopt_ref(*new FontWeightListModel(m_weights)));
  33. m_weight_list_view->horizontal_scrollbar().set_visible(false);
  34. m_size_spin_box = *widget.find_descendant_of_type_named<SpinBox>("size_spin_box");
  35. m_size_spin_box->set_range(1, 255);
  36. m_size_list_view = *widget.find_descendant_of_type_named<ListView>("size_list_view");
  37. m_size_list_view->set_model(ItemListModel<int>::create(m_sizes));
  38. m_size_list_view->horizontal_scrollbar().set_visible(false);
  39. m_sample_text_label = *widget.find_descendant_of_type_named<Label>("sample_text_label");
  40. m_families.clear();
  41. Gfx::FontDatabase::the().for_each_typeface([&](auto& typeface) {
  42. if (m_fixed_width_only && !typeface.is_fixed_width())
  43. return;
  44. if (!m_families.contains_slow(typeface.family()))
  45. m_families.append(typeface.family());
  46. });
  47. quick_sort(m_families);
  48. m_family_list_view->on_selection = [this](auto& index) {
  49. m_family = index.data().to_string();
  50. m_weights.clear();
  51. Gfx::FontDatabase::the().for_each_typeface([&](auto& typeface) {
  52. if (m_fixed_width_only && !typeface.is_fixed_width())
  53. return;
  54. if (typeface.family() == m_family.value() && !m_weights.contains_slow(typeface.weight())) {
  55. m_weights.append(typeface.weight());
  56. }
  57. });
  58. quick_sort(m_weights);
  59. Optional<size_t> index_of_old_weight_in_new_list;
  60. if (m_weight.has_value())
  61. index_of_old_weight_in_new_list = m_weights.find_first_index(m_weight.value());
  62. m_weight_list_view->model()->update();
  63. m_weight_list_view->set_cursor(m_weight_list_view->model()->index(index_of_old_weight_in_new_list.value_or(0)), GUI::AbstractView::SelectionUpdate::Set);
  64. update_font();
  65. };
  66. m_weight_list_view->on_selection = [this](auto& index) {
  67. bool font_is_fixed_size = false;
  68. m_weight = index.data(ModelRole::Custom).to_i32();
  69. m_sizes.clear();
  70. dbgln("Selected weight: {}", m_weight.value());
  71. Gfx::FontDatabase::the().for_each_typeface([&](auto& typeface) {
  72. if (m_fixed_width_only && !typeface.is_fixed_width())
  73. return;
  74. if (typeface.family() == m_family.value() && (int)typeface.weight() == m_weight.value()) {
  75. font_is_fixed_size = typeface.is_fixed_size();
  76. if (font_is_fixed_size) {
  77. m_size_spin_box->set_visible(false);
  78. typeface.for_each_fixed_size_font([&](auto& font) {
  79. m_sizes.append(font.presentation_size());
  80. });
  81. } else {
  82. m_size_spin_box->set_visible(true);
  83. m_sizes.append(8);
  84. m_sizes.append(10);
  85. m_sizes.append(12);
  86. m_sizes.append(14);
  87. m_sizes.append(16);
  88. m_sizes.append(18);
  89. m_sizes.append(20);
  90. m_sizes.append(22);
  91. m_sizes.append(24);
  92. m_sizes.append(36);
  93. }
  94. }
  95. });
  96. quick_sort(m_sizes);
  97. m_size_list_view->model()->update();
  98. m_size_list_view->set_selection_mode(GUI::AbstractView::SelectionMode::SingleSelection);
  99. if (m_size.has_value()) {
  100. Optional<size_t> index_of_old_size_in_new_list = m_sizes.find_first_index(m_size.value());
  101. if (index_of_old_size_in_new_list.has_value()) {
  102. m_size_list_view->set_cursor(m_size_list_view->model()->index(index_of_old_size_in_new_list.value()), GUI::AbstractView::SelectionUpdate::Set);
  103. } else {
  104. if (font_is_fixed_size) {
  105. m_size_list_view->set_cursor(m_size_list_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set);
  106. } else {
  107. m_size_list_view->set_selection_mode(GUI::AbstractView::SelectionMode::NoSelection);
  108. m_size_spin_box->set_value(m_size.value());
  109. }
  110. }
  111. } else {
  112. m_size_list_view->set_cursor(m_size_list_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set);
  113. }
  114. update_font();
  115. };
  116. m_size_list_view->on_selection = [this](auto& index) {
  117. m_size = index.data().to_i32();
  118. m_size_spin_box->set_value(m_size.value());
  119. update_font();
  120. };
  121. m_size_spin_box->on_change = [this](int value) {
  122. m_size = value;
  123. Optional<size_t> index_of_new_size_in_list = m_sizes.find_first_index(m_size.value());
  124. if (index_of_new_size_in_list.has_value()) {
  125. m_size_list_view->set_selection_mode(GUI::AbstractView::SelectionMode::SingleSelection);
  126. m_size_list_view->set_cursor(m_size_list_view->model()->index(index_of_new_size_in_list.value()), GUI::AbstractView::SelectionUpdate::Set);
  127. } else {
  128. m_size_list_view->set_selection_mode(GUI::AbstractView::SelectionMode::NoSelection);
  129. }
  130. update_font();
  131. };
  132. auto& ok_button = *widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  133. ok_button.on_click = [this](auto) {
  134. done(ExecOK);
  135. };
  136. auto& cancel_button = *widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  137. cancel_button.on_click = [this](auto) {
  138. done(ExecCancel);
  139. };
  140. set_font(current_font);
  141. }
  142. FontPicker::~FontPicker()
  143. {
  144. }
  145. void FontPicker::set_font(const Gfx::Font* font)
  146. {
  147. if (m_font == font)
  148. return;
  149. m_font = font;
  150. m_sample_text_label->set_font(m_font);
  151. if (!m_font) {
  152. m_family = {};
  153. m_weight = {};
  154. m_size = {};
  155. m_weights.clear();
  156. m_sizes.clear();
  157. m_weight_list_view->model()->update();
  158. m_size_list_view->model()->update();
  159. return;
  160. }
  161. m_family = font->family();
  162. m_weight = font->weight();
  163. m_size = font->presentation_size();
  164. auto family_index = m_families.find_first_index(m_font->family());
  165. if (family_index.has_value())
  166. m_family_list_view->set_cursor(m_family_list_view->model()->index(family_index.value()), GUI::AbstractView::SelectionUpdate::Set);
  167. auto weight_index = m_weights.find_first_index(m_font->weight());
  168. if (weight_index.has_value()) {
  169. m_weight_list_view->set_cursor(m_weight_list_view->model()->index(weight_index.value()), GUI::AbstractView::SelectionUpdate::Set);
  170. }
  171. auto size_index = m_sizes.find_first_index(m_font->presentation_size());
  172. if (size_index.has_value())
  173. m_size_list_view->set_cursor(m_size_list_view->model()->index(size_index.value()), GUI::AbstractView::SelectionUpdate::Set);
  174. }
  175. void FontPicker::update_font()
  176. {
  177. if (m_family.has_value() && m_size.has_value() && m_weight.has_value()) {
  178. m_font = Gfx::FontDatabase::the().get(m_family.value(), m_size.value(), m_weight.value());
  179. m_sample_text_label->set_font(m_font);
  180. }
  181. }
  182. }