TextLayout.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/CharacterTypes.h>
  9. #include <AK/DeprecatedString.h>
  10. #include <AK/Forward.h>
  11. #include <AK/Utf32View.h>
  12. #include <AK/Utf8View.h>
  13. #include <AK/Vector.h>
  14. #include <LibGfx/Font/Font.h>
  15. #include <LibGfx/Forward.h>
  16. #include <LibGfx/Rect.h>
  17. #include <LibGfx/TextElision.h>
  18. #include <LibGfx/TextWrapping.h>
  19. namespace Gfx {
  20. // FIXME: This currently isn't an ideal way of doing things; ideally, TextLayout
  21. // would be doing the rendering by painting individual glyphs. However, this
  22. // would regress our Unicode bidirectional text support. Therefore, fixing this
  23. // requires:
  24. // - Moving the bidirectional algorithm either here, or some place TextLayout
  25. // can access;
  26. // - Making TextLayout render the given text into something like a Vector<Line>
  27. // where:
  28. // using Line = Vector<DirectionalRun>;
  29. // struct DirectionalRun {
  30. // Utf32View glyphs;
  31. // Vector<int> advance;
  32. // TextDirection direction;
  33. // };
  34. // - Either;
  35. // a) Making TextLayout output these Lines directly using a given Painter, or
  36. // b) Taking the Lines from TextLayout and painting each glyph.
  37. class TextLayout {
  38. public:
  39. TextLayout(Gfx::Font const& font, Utf8View const& text, FloatRect const& rect)
  40. : m_font(font)
  41. , m_font_metrics(font.pixel_metrics())
  42. , m_text(text)
  43. , m_rect(rect)
  44. {
  45. }
  46. Vector<DeprecatedString, 32> lines(TextElision elision, TextWrapping wrapping) const
  47. {
  48. return wrap_lines(elision, wrapping);
  49. }
  50. FloatRect bounding_rect(TextWrapping) const;
  51. private:
  52. Vector<DeprecatedString, 32> wrap_lines(TextElision, TextWrapping) const;
  53. DeprecatedString elide_text_from_right(Utf8View) const;
  54. Font const& m_font;
  55. FontPixelMetrics m_font_metrics;
  56. Utf8View m_text;
  57. FloatRect m_rect;
  58. };
  59. inline bool should_paint_as_space(u32 code_point)
  60. {
  61. return is_ascii_space(code_point) || code_point == 0xa0;
  62. }
  63. enum class IncludeLeftBearing {
  64. Yes,
  65. No
  66. };
  67. struct GlyphPosition {
  68. FloatPoint position;
  69. float glyph_width;
  70. AK::Utf8CodePointIterator& it;
  71. };
  72. template<typename Callback>
  73. void for_each_glyph_position(FloatPoint start, Utf8View text, Font const& font, Callback callback, IncludeLeftBearing include_left_bearing = IncludeLeftBearing::No)
  74. {
  75. float space_width = font.glyph_width(' ') + font.glyph_spacing();
  76. u32 last_code_point = 0;
  77. auto point = start;
  78. point.translate_by(0, -font.pixel_metrics().ascent);
  79. for (auto code_point_iterator = text.begin(); code_point_iterator != text.end(); ++code_point_iterator) {
  80. auto code_point = *code_point_iterator;
  81. if (should_paint_as_space(code_point)) {
  82. point.translate_by(space_width, 0);
  83. last_code_point = code_point;
  84. continue;
  85. }
  86. auto kerning = font.glyphs_horizontal_kerning(last_code_point, code_point);
  87. if (kerning != 0.0f)
  88. point.translate_by(kerning, 0);
  89. auto it = code_point_iterator; // The callback function will advance the iterator, so create a copy for this lookup.
  90. auto glyph_width = font.glyph_or_emoji_width(it) + font.glyph_spacing();
  91. auto glyph_point = point;
  92. if (include_left_bearing == IncludeLeftBearing::Yes)
  93. glyph_point += FloatPoint(font.glyph_left_bearing(code_point), 0);
  94. callback(GlyphPosition {
  95. .position = glyph_point,
  96. .glyph_width = glyph_width,
  97. .it = code_point_iterator });
  98. point.translate_by(glyph_width, 0);
  99. last_code_point = code_point;
  100. }
  101. }
  102. }