TextLayout.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. Variant<DrawGlyph, DrawEmoji> prepare_draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font);
  88. template<typename Callback>
  89. 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 = {})
  90. {
  91. auto const& space_glyph_font = font_list.font_for_code_point(' ');
  92. float space_width = space_glyph_font.glyph_width(' ') + space_glyph_font.glyph_spacing();
  93. u32 last_code_point = 0;
  94. auto point = baseline_start;
  95. for (auto code_point_iterator = string.begin(); code_point_iterator != string.end(); ++code_point_iterator) {
  96. auto it = code_point_iterator; // The callback function will advance the iterator, so create a copy for this lookup.
  97. auto code_point = *code_point_iterator;
  98. RefPtr<Gfx::Font const> font = font_list.font_for_code_point(code_point);
  99. point.set_y(baseline_start.y() - font->pixel_metrics().ascent);
  100. if (should_paint_as_space(code_point)) {
  101. point.translate_by(space_width, 0);
  102. last_code_point = code_point;
  103. continue;
  104. }
  105. auto kerning = font->glyphs_horizontal_kerning(last_code_point, code_point);
  106. if (kerning != 0.0f)
  107. point.translate_by(kerning, 0);
  108. auto glyph_width = font->glyph_or_emoji_width(it) + font->glyph_spacing();
  109. auto glyph_or_emoji = prepare_draw_glyph_or_emoji(point, code_point_iterator, *font);
  110. if (include_left_bearing == IncludeLeftBearing::Yes) {
  111. if (glyph_or_emoji.has<DrawGlyph>())
  112. glyph_or_emoji.get<DrawGlyph>().position += FloatPoint(font->glyph_left_bearing(code_point), 0);
  113. }
  114. callback(glyph_or_emoji);
  115. point.translate_by(glyph_width, 0);
  116. last_code_point = code_point;
  117. }
  118. if (width.has_value())
  119. *width = point.x() - font_list.first().glyph_spacing();
  120. }
  121. }