TextAlignment.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <AK/StringView.h>
  9. namespace Gfx {
  10. #define GFX_ENUMERATE_TEXT_ALIGNMENTS(M) \
  11. M(Center) \
  12. M(CenterLeft) \
  13. M(CenterRight) \
  14. M(TopLeft) \
  15. M(TopRight) \
  16. M(BottomLeft) \
  17. M(BottomRight)
  18. enum class TextAlignment {
  19. #define __ENUMERATE(x) x,
  20. GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
  21. #undef __ENUMERATE
  22. };
  23. inline bool is_right_text_alignment(TextAlignment alignment)
  24. {
  25. switch (alignment) {
  26. case TextAlignment::CenterRight:
  27. case TextAlignment::TopRight:
  28. case TextAlignment::BottomRight:
  29. return true;
  30. default:
  31. return false;
  32. }
  33. }
  34. inline bool is_vertically_centered_text_alignment(TextAlignment alignment)
  35. {
  36. switch (alignment) {
  37. case TextAlignment::CenterLeft:
  38. case TextAlignment::CenterRight:
  39. case TextAlignment::Center:
  40. return true;
  41. default:
  42. return false;
  43. }
  44. }
  45. inline Optional<TextAlignment> text_alignment_from_string(const StringView& string)
  46. {
  47. #define __ENUMERATE(x) \
  48. if (string == #x) \
  49. return TextAlignment::x;
  50. GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
  51. #undef __ENUMERATE
  52. return {};
  53. }
  54. inline const char* to_string(TextAlignment text_alignment)
  55. {
  56. #define __ENUMERATE(x) \
  57. if (text_alignment == TextAlignment::x) \
  58. return #x;
  59. GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
  60. #undef __ENUMERATE
  61. return {};
  62. }
  63. }