FontPicker.cpp 9.4 KB

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