TextAlignment.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(TopCenter) \
  15. M(TopLeft) \
  16. M(TopRight) \
  17. M(BottomCenter) \
  18. M(BottomLeft) \
  19. M(BottomRight)
  20. enum class TextAlignment {
  21. #define __ENUMERATE(x) x,
  22. GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
  23. #undef __ENUMERATE
  24. };
  25. inline bool is_right_text_alignment(TextAlignment alignment)
  26. {
  27. switch (alignment) {
  28. case TextAlignment::CenterRight:
  29. case TextAlignment::TopRight:
  30. case TextAlignment::BottomRight:
  31. return true;
  32. default:
  33. return false;
  34. }
  35. }
  36. inline bool is_vertically_centered_text_alignment(TextAlignment alignment)
  37. {
  38. switch (alignment) {
  39. case TextAlignment::CenterLeft:
  40. case TextAlignment::CenterRight:
  41. case TextAlignment::Center:
  42. return true;
  43. default:
  44. return false;
  45. }
  46. }
  47. inline Optional<TextAlignment> text_alignment_from_string(StringView string)
  48. {
  49. #define __ENUMERATE(x) \
  50. if (string == #x) \
  51. return TextAlignment::x;
  52. GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
  53. #undef __ENUMERATE
  54. return {};
  55. }
  56. inline char const* to_string(TextAlignment text_alignment)
  57. {
  58. #define __ENUMERATE(x) \
  59. if (text_alignment == TextAlignment::x) \
  60. return #x;
  61. GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
  62. #undef __ENUMERATE
  63. return {};
  64. }
  65. }