FontPicker.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/ItemListModel.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. struct FontWeightNameMapping {
  38. constexpr FontWeightNameMapping(int w, const char* n)
  39. : weight(w)
  40. , name(n)
  41. {
  42. }
  43. int weight { 0 };
  44. StringView name;
  45. };
  46. static constexpr FontWeightNameMapping font_weight_names[] = {
  47. { 100, "Thin" },
  48. { 200, "Extra Light" },
  49. { 300, "Light" },
  50. { 400, "Regular" },
  51. { 500, "Medium" },
  52. { 600, "Semi Bold" },
  53. { 700, "Bold" },
  54. { 800, "Extra Bold" },
  55. { 900, "Black" },
  56. { 950, "Extra Black" },
  57. };
  58. static constexpr StringView weight_to_name(int weight)
  59. {
  60. for (auto& it : font_weight_names) {
  61. if (it.weight == weight)
  62. return it.name;
  63. }
  64. return {};
  65. }
  66. class FontWeightListModel : public ItemListModel<int> {
  67. public:
  68. FontWeightListModel(const Vector<int>& weights)
  69. : ItemListModel(weights)
  70. {
  71. }
  72. virtual Variant data(const ModelIndex& index, ModelRole role) const override
  73. {
  74. if (role == ModelRole::Custom)
  75. return m_data.at(index.row());
  76. if (role == ModelRole::Display)
  77. return String(weight_to_name(m_data.at(index.row())));
  78. return ItemListModel::data(index, role);
  79. }
  80. };
  81. FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, bool fixed_width_only)
  82. : Dialog(parent_window)
  83. , m_fixed_width_only(fixed_width_only)
  84. {
  85. set_title("Font picker");
  86. resize(430, 280);
  87. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-font-editor.png"));
  88. auto& widget = set_main_widget<GUI::Widget>();
  89. if (!widget.load_from_gml(font_picker_dialog_gml))
  90. ASSERT_NOT_REACHED();
  91. m_family_list_view = *widget.find_descendant_of_type_named<ListView>("family_list_view");
  92. m_family_list_view->set_model(ItemListModel<String>::create(m_families));
  93. m_family_list_view->horizontal_scrollbar().set_visible(false);
  94. m_weight_list_view = *widget.find_descendant_of_type_named<ListView>("weight_list_view");
  95. m_weight_list_view->set_model(adopt(*new FontWeightListModel(m_weights)));
  96. m_weight_list_view->horizontal_scrollbar().set_visible(false);
  97. m_size_list_view = *widget.find_descendant_of_type_named<ListView>("size_list_view");
  98. m_size_list_view->set_model(ItemListModel<int>::create(m_sizes));
  99. m_size_list_view->horizontal_scrollbar().set_visible(false);
  100. m_sample_text_label = *widget.find_descendant_of_type_named<Label>("sample_text_label");
  101. m_families.clear();
  102. Gfx::FontDatabase::the().for_each_font([&](auto& font) {
  103. if (m_fixed_width_only && !font.is_fixed_width())
  104. return;
  105. if (!m_families.contains_slow(font.family()))
  106. m_families.append(font.family());
  107. });
  108. quick_sort(m_families);
  109. m_family_list_view->on_selection = [this](auto& index) {
  110. m_family = index.data().to_string();
  111. m_weights.clear();
  112. Gfx::FontDatabase::the().for_each_font([&](auto& font) {
  113. if (m_fixed_width_only && !font.is_fixed_width())
  114. return;
  115. if (font.family() == m_family.value() && !m_weights.contains_slow(font.weight())) {
  116. m_weights.append(font.weight());
  117. }
  118. });
  119. quick_sort(m_weights);
  120. Optional<size_t> index_of_old_weight_in_new_list;
  121. if (m_weight.has_value())
  122. index_of_old_weight_in_new_list = m_weights.find_first_index(m_weight.value());
  123. m_weight_list_view->model()->update();
  124. 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);
  125. update_font();
  126. };
  127. m_weight_list_view->on_selection = [this](auto& index) {
  128. m_weight = index.data(ModelRole::Custom).to_i32();
  129. m_sizes.clear();
  130. Gfx::FontDatabase::the().for_each_font([&](auto& font) {
  131. if (m_fixed_width_only && !font.is_fixed_width())
  132. return;
  133. if (font.family() == m_family.value() && font.weight() == m_weight.value()) {
  134. m_sizes.append(font.presentation_size());
  135. }
  136. });
  137. quick_sort(m_sizes);
  138. Optional<size_t> index_of_old_size_in_new_list;
  139. if (m_size.has_value()) {
  140. index_of_old_size_in_new_list = m_sizes.find_first_index(m_size.value());
  141. }
  142. m_size_list_view->model()->update();
  143. 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);
  144. update_font();
  145. };
  146. m_size_list_view->on_selection = [this](auto& index) {
  147. m_size = index.data().to_i32();
  148. update_font();
  149. };
  150. auto& ok_button = *widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  151. ok_button.on_click = [this](auto) {
  152. done(ExecOK);
  153. };
  154. auto& cancel_button = *widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  155. cancel_button.on_click = [this](auto) {
  156. done(ExecCancel);
  157. };
  158. set_font(current_font);
  159. }
  160. FontPicker::~FontPicker()
  161. {
  162. }
  163. void FontPicker::set_font(const Gfx::Font* font)
  164. {
  165. if (m_font == font)
  166. return;
  167. m_font = font;
  168. m_sample_text_label->set_font(m_font);
  169. if (!m_font) {
  170. m_family = {};
  171. m_weight = {};
  172. m_size = {};
  173. m_weights.clear();
  174. m_sizes.clear();
  175. m_weight_list_view->model()->update();
  176. m_size_list_view->model()->update();
  177. return;
  178. }
  179. m_family = font->family();
  180. m_weight = font->weight();
  181. m_size = font->presentation_size();
  182. size_t family_index = m_families.find_first_index(m_font->family()).value();
  183. m_family_list_view->set_cursor(m_family_list_view->model()->index(family_index), GUI::AbstractView::SelectionUpdate::Set);
  184. size_t weight_index = m_weights.find_first_index(m_font->weight()).value();
  185. m_weight_list_view->set_cursor(m_weight_list_view->model()->index(weight_index), GUI::AbstractView::SelectionUpdate::Set);
  186. size_t size_index = m_sizes.find_first_index(m_font->presentation_size()).value();
  187. m_size_list_view->set_cursor(m_size_list_view->model()->index(size_index), GUI::AbstractView::SelectionUpdate::Set);
  188. }
  189. void FontPicker::update_font()
  190. {
  191. if (m_family.has_value() && m_size.has_value() && m_weight.has_value()) {
  192. m_font = Gfx::FontDatabase::the().get(m_family.value(), m_size.value(), m_weight.value());
  193. m_sample_text_label->set_font(m_font);
  194. }
  195. }
  196. }