FontStyleMapping.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/StringView.h>
  9. namespace Gfx {
  10. struct FontStyleMapping {
  11. int style { 0 };
  12. StringView name;
  13. };
  14. static constexpr Array<FontStyleMapping, 10> font_weight_names = { {
  15. { 100, "Thin"sv },
  16. { 200, "Extra Light"sv },
  17. { 300, "Light"sv },
  18. { 400, "Regular"sv },
  19. { 500, "Medium"sv },
  20. { 600, "Semi Bold"sv },
  21. { 700, "Bold"sv },
  22. { 800, "Extra Bold"sv },
  23. { 900, "Black"sv },
  24. { 950, "Extra Black"sv },
  25. } };
  26. static constexpr Array<FontStyleMapping, 4> font_slope_names = { {
  27. { 0, "Regular"sv },
  28. { 1, "Italic"sv },
  29. { 2, "Oblique"sv },
  30. { 3, "Reclined"sv },
  31. } };
  32. static constexpr Array<FontStyleMapping, 9> font_width_names = { {
  33. { 1, "Ultra Condensed"sv },
  34. { 2, "Extra Condensed"sv },
  35. { 3, "Condensed"sv },
  36. { 4, "Semi Condensed"sv },
  37. { 5, "Normal"sv },
  38. { 6, "Semi Expanded"sv },
  39. { 7, "Expanded"sv },
  40. { 8, "Extra Expanded"sv },
  41. { 9, "Ultra Expanded"sv },
  42. } };
  43. static constexpr StringView weight_to_name(int weight)
  44. {
  45. for (auto& it : font_weight_names) {
  46. if (it.style == weight)
  47. return it.name;
  48. }
  49. return {};
  50. }
  51. static constexpr int name_to_weight(StringView name)
  52. {
  53. for (auto& it : font_weight_names) {
  54. if (it.name == name)
  55. return it.style;
  56. }
  57. return {};
  58. }
  59. static constexpr StringView slope_to_name(int slope)
  60. {
  61. for (auto& it : font_slope_names) {
  62. if (it.style == slope)
  63. return it.name;
  64. }
  65. return {};
  66. }
  67. static constexpr int name_to_slope(StringView name)
  68. {
  69. for (auto& it : font_slope_names) {
  70. if (it.name == name)
  71. return it.style;
  72. }
  73. return {};
  74. }
  75. static constexpr StringView width_to_name(int width)
  76. {
  77. for (auto& it : font_width_names) {
  78. if (it.style == width)
  79. return it.name;
  80. }
  81. return {};
  82. }
  83. static constexpr int name_to_width(StringView name)
  84. {
  85. for (auto& it : font_width_names) {
  86. if (it.name == name)
  87. return it.style;
  88. }
  89. return {};
  90. }
  91. }