FontPicker.cpp 7.7 KB

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