TextLayout.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/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<DeprecatedString, 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<DeprecatedString, 32> wrap_lines(TextElision, TextWrapping) const;
  54. DeprecatedString 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. };
  73. struct DrawEmoji {
  74. FloatPoint position;
  75. Gfx::Bitmap const* emoji;
  76. NonnullRefPtr<Font const> font;
  77. };
  78. using DrawGlyphOrEmoji = Variant<DrawGlyph, DrawEmoji>;
  79. Variant<DrawGlyph, DrawEmoji> prepare_draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font);
  80. template<typename Callback>
  81. 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 = {})
  82. {
  83. float space_width = font_list.first().glyph_width(' ') + font_list.first().glyph_spacing();
  84. u32 last_code_point = 0;
  85. auto point = baseline_start;
  86. for (auto code_point_iterator = string.begin(); code_point_iterator != string.end(); ++code_point_iterator) {
  87. auto it = code_point_iterator; // The callback function will advance the iterator, so create a copy for this lookup.
  88. auto code_point = *code_point_iterator;
  89. RefPtr<Gfx::Font const> font = font_list.font_for_code_point(code_point);
  90. point.set_y(baseline_start.y() - font->pixel_metrics().ascent);
  91. if (should_paint_as_space(code_point)) {
  92. point.translate_by(space_width, 0);
  93. last_code_point = code_point;
  94. continue;
  95. }
  96. auto kerning = font->glyphs_horizontal_kerning(last_code_point, code_point);
  97. if (kerning != 0.0f)
  98. point.translate_by(kerning, 0);
  99. auto glyph_width = font->glyph_or_emoji_width(it) + font->glyph_spacing();
  100. auto glyph_or_emoji = prepare_draw_glyph_or_emoji(point, code_point_iterator, *font);
  101. if (include_left_bearing == IncludeLeftBearing::Yes) {
  102. if (glyph_or_emoji.has<DrawGlyph>())
  103. glyph_or_emoji.get<DrawGlyph>().position += FloatPoint(font->glyph_left_bearing(code_point), 0);
  104. }
  105. callback(glyph_or_emoji);
  106. point.translate_by(glyph_width, 0);
  107. last_code_point = code_point;
  108. }
  109. if (width.has_value())
  110. *width = point.x() - font_list.first().glyph_spacing();
  111. }
  112. }