TextLayout.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/Forward.h>
  10. #include <AK/Utf32View.h>
  11. #include <AK/Utf8View.h>
  12. #include <AK/Vector.h>
  13. #include <LibGfx/Font/Font.h>
  14. #include <LibGfx/Forward.h>
  15. #include <LibGfx/Rect.h>
  16. #include <LibGfx/TextElision.h>
  17. #include <LibGfx/TextWrapping.h>
  18. namespace Gfx {
  19. // FIXME: This currently isn't an ideal way of doing things; ideally, TextLayout
  20. // would be doing the rendering by painting individual glyphs. However, this
  21. // would regress our Unicode bidirectional text support. Therefore, fixing this
  22. // requires:
  23. // - Moving the bidirectional algorithm either here, or some place TextLayout
  24. // can access;
  25. // - Making TextLayout render the given text into something like a Vector<Line>
  26. // where:
  27. // using Line = Vector<DirectionalRun>;
  28. // struct DirectionalRun {
  29. // Utf32View glyphs;
  30. // Vector<int> advance;
  31. // TextDirection direction;
  32. // };
  33. // - Either;
  34. // a) Making TextLayout output these Lines directly using a given Painter, or
  35. // b) Taking the Lines from TextLayout and painting each glyph.
  36. class TextLayout {
  37. public:
  38. TextLayout(Gfx::Font const& font, Utf8View const& text, FloatRect const& rect)
  39. : m_font(font)
  40. , m_font_metrics(font.pixel_metrics())
  41. , m_text(text)
  42. , m_rect(rect)
  43. {
  44. }
  45. Vector<DeprecatedString, 32> lines(TextElision elision, TextWrapping wrapping) const
  46. {
  47. return wrap_lines(elision, wrapping);
  48. }
  49. FloatRect bounding_rect(TextWrapping) const;
  50. private:
  51. Vector<DeprecatedString, 32> wrap_lines(TextElision, TextWrapping) const;
  52. DeprecatedString elide_text_from_right(Utf8View) const;
  53. Font const& m_font;
  54. FontPixelMetrics m_font_metrics;
  55. Utf8View m_text;
  56. FloatRect m_rect;
  57. };
  58. }