FontStyleMapping.h 1.5 KB

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