FontPickerWeightModel.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/ItemListModel.h>
  8. namespace GUI {
  9. struct FontWeightNameMapping {
  10. constexpr FontWeightNameMapping(int w, const char* n)
  11. : weight(w)
  12. , name(n)
  13. {
  14. }
  15. int weight { 0 };
  16. StringView name;
  17. };
  18. static constexpr FontWeightNameMapping font_weight_names[] = {
  19. { 100, "Thin" },
  20. { 200, "Extra Light" },
  21. { 300, "Light" },
  22. { 400, "Regular" },
  23. { 500, "Medium" },
  24. { 600, "Semi Bold" },
  25. { 700, "Bold" },
  26. { 800, "Extra Bold" },
  27. { 900, "Black" },
  28. { 950, "Extra Black" },
  29. };
  30. static constexpr StringView weight_to_name(int weight)
  31. {
  32. for (auto& it : font_weight_names) {
  33. if (it.weight == weight)
  34. return it.name;
  35. }
  36. return {};
  37. }
  38. static constexpr int name_to_weight(StringView name)
  39. {
  40. for (auto& it : font_weight_names) {
  41. if (it.name == name)
  42. return it.weight;
  43. }
  44. return {};
  45. }
  46. class FontWeightListModel : public ItemListModel<int> {
  47. public:
  48. FontWeightListModel(const Vector<int>& weights)
  49. : ItemListModel(weights)
  50. {
  51. }
  52. virtual Variant data(const ModelIndex& index, ModelRole role) const override
  53. {
  54. if (role == ModelRole::Custom)
  55. return m_data.at(index.row());
  56. if (role == ModelRole::Display)
  57. return String(weight_to_name(m_data.at(index.row())));
  58. return ItemListModel::data(index, role);
  59. }
  60. };
  61. }