TextLayout.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/ByteString.h>
  9. #include <AK/CharacterTypes.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/FontCascadeList.h>
  16. #include <LibGfx/Forward.h>
  17. #include <LibGfx/Rect.h>
  18. #include <LibGfx/TextElision.h>
  19. #include <LibGfx/TextWrapping.h>
  20. namespace Gfx {
  21. // FIXME: This currently isn't an ideal way of doing things; ideally, TextLayout
  22. // would be doing the rendering by painting individual glyphs. However, this
  23. // would regress our Unicode bidirectional text support. Therefore, fixing this
  24. // requires:
  25. // - Moving the bidirectional algorithm either here, or some place TextLayout
  26. // can access;
  27. // - Making TextLayout render the given text into something like a Vector<Line>
  28. // where:
  29. // using Line = Vector<DirectionalRun>;
  30. // struct DirectionalRun {
  31. // Utf32View glyphs;
  32. // Vector<int> advance;
  33. // TextDirection direction;
  34. // };
  35. // - Either;
  36. // a) Making TextLayout output these Lines directly using a given Painter, or
  37. // b) Taking the Lines from TextLayout and painting each glyph.
  38. class TextLayout {
  39. public:
  40. TextLayout(Gfx::Font const& font, Utf8View const& text, FloatRect const& rect)
  41. : m_font(font)
  42. , m_font_metrics(font.pixel_metrics())
  43. , m_text(text)
  44. , m_rect(rect)
  45. {
  46. }
  47. Vector<ByteString, 32> lines(TextElision elision, TextWrapping wrapping) const
  48. {
  49. return wrap_lines(elision, wrapping);
  50. }
  51. FloatRect bounding_rect(TextWrapping) const;
  52. private:
  53. Vector<ByteString, 32> wrap_lines(TextElision, TextWrapping) const;
  54. ByteString elide_text_from_right(Utf8View) const;
  55. Font const& m_font;
  56. FontPixelMetrics m_font_metrics;
  57. Utf8View m_text;
  58. FloatRect m_rect;
  59. };
  60. inline bool should_paint_as_space(u32 code_point)
  61. {
  62. return is_ascii_space(code_point) || code_point == 0xa0;
  63. }
  64. enum class IncludeLeftBearing {
  65. Yes,
  66. No
  67. };
  68. struct DrawGlyph {
  69. FloatPoint position;
  70. u32 code_point;
  71. NonnullRefPtr<Font const> font;
  72. void translate_by(FloatPoint const& delta)
  73. {
  74. position.translate_by(delta);
  75. }
  76. };
  77. struct DrawEmoji {
  78. FloatPoint position;
  79. Gfx::Bitmap const* emoji;
  80. NonnullRefPtr<Font const> font;
  81. void translate_by(FloatPoint const& delta)
  82. {
  83. position.translate_by(delta);
  84. }
  85. };
  86. using DrawGlyphOrEmoji = Variant<DrawGlyph, DrawEmoji>;
  87. class GlyphRun : public RefCounted<GlyphRun> {
  88. public:
  89. GlyphRun() = default;
  90. GlyphRun(Vector<Gfx::DrawGlyphOrEmoji>&& glyphs)
  91. : m_glyphs(move(glyphs))
  92. {
  93. }
  94. [[nodiscard]] Vector<Gfx::DrawGlyphOrEmoji> const& glyphs() const { return m_glyphs; }
  95. [[nodiscard]] bool is_empty() const { return m_glyphs.is_empty(); }
  96. void append(Gfx::DrawGlyphOrEmoji glyph) { m_glyphs.append(glyph); }
  97. private:
  98. Vector<Gfx::DrawGlyphOrEmoji> m_glyphs;
  99. };
  100. Variant<DrawGlyph, DrawEmoji> prepare_draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font);
  101. template<typename Callback>
  102. void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, FontCascadeList const& font_list, Callback callback, IncludeLeftBearing include_left_bearing = IncludeLeftBearing::No, Optional<float&> width = {})
  103. {
  104. auto const& space_glyph_font = font_list.font_for_code_point(' ');
  105. float space_width = space_glyph_font.glyph_width(' ') + space_glyph_font.glyph_spacing();
  106. u32 last_code_point = 0;
  107. auto point = baseline_start;
  108. for (auto code_point_iterator = string.begin(); code_point_iterator != string.end(); ++code_point_iterator) {
  109. auto it = code_point_iterator; // The callback function will advance the iterator, so create a copy for this lookup.
  110. auto code_point = *code_point_iterator;
  111. RefPtr<Gfx::Font const> font = font_list.font_for_code_point(code_point);
  112. point.set_y(baseline_start.y() - font->pixel_metrics().ascent);
  113. if (should_paint_as_space(code_point)) {
  114. point.translate_by(space_width, 0);
  115. last_code_point = code_point;
  116. continue;
  117. }
  118. auto kerning = font->glyphs_horizontal_kerning(last_code_point, code_point);
  119. if (kerning != 0.0f)
  120. point.translate_by(kerning, 0);
  121. auto glyph_width = font->glyph_or_emoji_width(it) + font->glyph_spacing();
  122. auto glyph_or_emoji = prepare_draw_glyph_or_emoji(point, code_point_iterator, *font);
  123. if (include_left_bearing == IncludeLeftBearing::Yes) {
  124. if (glyph_or_emoji.has<DrawGlyph>())
  125. glyph_or_emoji.get<DrawGlyph>().position += FloatPoint(font->glyph_left_bearing(code_point), 0);
  126. }
  127. callback(glyph_or_emoji);
  128. point.translate_by(glyph_width, 0);
  129. last_code_point = code_point;
  130. }
  131. if (width.has_value())
  132. *width = point.x() - font_list.first().glyph_spacing();
  133. }
  134. }