FontStyleMapping.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // NOTE: __builtin_strlen required to make this work at compile time.
  12. constexpr FontStyleMapping(int s, char const* n)
  13. : style(s)
  14. , name(StringView { n, __builtin_strlen(n) })
  15. {
  16. }
  17. int style { 0 };
  18. StringView name;
  19. };
  20. static constexpr FontStyleMapping font_weight_names[] = {
  21. { 100, "Thin" },
  22. { 200, "Extra Light" },
  23. { 300, "Light" },
  24. { 400, "Regular" },
  25. { 500, "Medium" },
  26. { 600, "Semi Bold" },
  27. { 700, "Bold" },
  28. { 800, "Extra Bold" },
  29. { 900, "Black" },
  30. { 950, "Extra Black" },
  31. };
  32. static constexpr FontStyleMapping font_slope_names[] = {
  33. { 0, "Regular" },
  34. { 1, "Italic" },
  35. { 2, "Oblique" },
  36. { 3, "Reclined" }
  37. };
  38. static constexpr StringView weight_to_name(int weight)
  39. {
  40. for (auto& it : font_weight_names) {
  41. if (it.style == weight)
  42. return it.name;
  43. }
  44. return {};
  45. }
  46. static constexpr int name_to_weight(StringView name)
  47. {
  48. for (auto& it : font_weight_names) {
  49. if (it.name == name)
  50. return it.style;
  51. }
  52. return {};
  53. }
  54. static constexpr StringView slope_to_name(int slope)
  55. {
  56. for (auto& it : font_slope_names) {
  57. if (it.style == slope)
  58. return it.name;
  59. }
  60. return {};
  61. }
  62. static constexpr int name_to_slope(StringView name)
  63. {
  64. for (auto& it : font_slope_names) {
  65. if (it.name == name)
  66. return it.style;
  67. }
  68. return {};
  69. }
  70. }