FontPicker.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_typeface([&](auto& typeface) {
  103. if (m_fixed_width_only && !typeface.is_fixed_width())
  104. return;
  105. if (!m_families.contains_slow(typeface.family()))
  106. m_families.append(typeface.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_typeface([&](auto& typeface) {
  113. if (m_fixed_width_only && !typeface.is_fixed_width())
  114. return;
  115. if (typeface.family() == m_family.value() && !m_weights.contains_slow(typeface.weight())) {
  116. m_weights.append(typeface.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. dbgln("Selected weight: {}", m_weight.value());
  131. Gfx::FontDatabase::the().for_each_typeface([&](auto& typeface) {
  132. if (m_fixed_width_only && !typeface.is_fixed_width())
  133. return;
  134. if (typeface.family() == m_family.value() && (int)typeface.weight() == m_weight.value()) {
  135. if (typeface.is_fixed_size()) {
  136. typeface.for_each_fixed_size_font([&](auto& font) {
  137. m_sizes.append(font.presentation_size());
  138. });
  139. } else {
  140. m_sizes.append(12);
  141. }
  142. }
  143. });
  144. quick_sort(m_sizes);
  145. Optional<size_t> index_of_old_size_in_new_list;
  146. if (m_size.has_value()) {
  147. index_of_old_size_in_new_list = m_sizes.find_first_index(m_size.value());
  148. }
  149. m_size_list_view->model()->update();
  150. 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);
  151. update_font();
  152. };
  153. m_size_list_view->on_selection = [this](auto& index) {
  154. m_size = index.data().to_i32();
  155. update_font();
  156. };
  157. auto& ok_button = *widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  158. ok_button.on_click = [this](auto) {
  159. done(ExecOK);
  160. };
  161. auto& cancel_button = *widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  162. cancel_button.on_click = [this](auto) {
  163. done(ExecCancel);
  164. };
  165. set_font(current_font);
  166. }
  167. FontPicker::~FontPicker()
  168. {
  169. }
  170. void FontPicker::set_font(const Gfx::Font* font)
  171. {
  172. if (m_font == font)
  173. return;
  174. m_font = font;
  175. m_sample_text_label->set_font(m_font);
  176. if (!m_font) {
  177. m_family = {};
  178. m_weight = {};
  179. m_size = {};
  180. m_weights.clear();
  181. m_sizes.clear();
  182. m_weight_list_view->model()->update();
  183. m_size_list_view->model()->update();
  184. return;
  185. }
  186. m_family = font->family();
  187. m_weight = font->weight();
  188. m_size = font->presentation_size();
  189. auto family_index = m_families.find_first_index(m_font->family());
  190. if (family_index.has_value())
  191. m_family_list_view->set_cursor(m_family_list_view->model()->index(family_index.value()), GUI::AbstractView::SelectionUpdate::Set);
  192. auto weight_index = m_weights.find_first_index(m_font->weight());
  193. if (weight_index.has_value()) {
  194. m_weight_list_view->set_cursor(m_weight_list_view->model()->index(weight_index.value()), GUI::AbstractView::SelectionUpdate::Set);
  195. }
  196. auto size_index = m_sizes.find_first_index(m_font->presentation_size());
  197. if (size_index.has_value())
  198. m_size_list_view->set_cursor(m_size_list_view->model()->index(size_index.value()), GUI::AbstractView::SelectionUpdate::Set);
  199. }
  200. void FontPicker::update_font()
  201. {
  202. if (m_family.has_value() && m_size.has_value() && m_weight.has_value()) {
  203. m_font = Gfx::FontDatabase::the().get(m_family.value(), m_size.value(), m_weight.value());
  204. m_sample_text_label->set_font(m_font);
  205. }
  206. }
  207. }