TextLayout.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include "TextLayout.h"
  8. #include "Font/Emoji.h"
  9. #include <AK/Debug.h>
  10. #include <LibUnicode/CharacterTypes.h>
  11. #include <LibUnicode/Emoji.h>
  12. namespace Gfx {
  13. enum class BlockType {
  14. Newline,
  15. Whitespace,
  16. Word
  17. };
  18. struct Block {
  19. BlockType type;
  20. Utf8View characters;
  21. };
  22. FloatRect TextLayout::bounding_rect(TextWrapping wrapping) const
  23. {
  24. auto lines = wrap_lines(TextElision::None, wrapping);
  25. if (lines.is_empty()) {
  26. return {};
  27. }
  28. FloatRect bounding_rect = {
  29. 0, 0, 0, (static_cast<float>(lines.size()) * (m_font_metrics.ascent + m_font_metrics.descent + m_font_metrics.line_gap)) - m_font_metrics.line_gap
  30. };
  31. for (auto& line : lines) {
  32. auto line_width = m_font.width(line);
  33. if (line_width > bounding_rect.width())
  34. bounding_rect.set_width(line_width);
  35. }
  36. return bounding_rect;
  37. }
  38. Vector<ByteString, 32> TextLayout::wrap_lines(TextElision elision, TextWrapping wrapping) const
  39. {
  40. Vector<Block> blocks;
  41. Optional<BlockType> current_block_type;
  42. size_t block_start_offset = 0;
  43. size_t offset = 0;
  44. for (auto it = m_text.begin(); !it.done(); ++it) {
  45. offset = m_text.iterator_offset(it);
  46. switch (*it) {
  47. case '\t':
  48. case ' ': {
  49. if (current_block_type.has_value() && current_block_type.value() != BlockType::Whitespace) {
  50. blocks.append({
  51. current_block_type.value(),
  52. m_text.substring_view(block_start_offset, offset - block_start_offset),
  53. });
  54. current_block_type.clear();
  55. }
  56. if (!current_block_type.has_value()) {
  57. current_block_type = BlockType::Whitespace;
  58. block_start_offset = offset;
  59. }
  60. continue;
  61. }
  62. case '\r':
  63. if (it.peek(1) == static_cast<u32>('\n'))
  64. ++it;
  65. [[fallthrough]];
  66. case '\n': {
  67. if (current_block_type.has_value()) {
  68. blocks.append({
  69. current_block_type.value(),
  70. m_text.substring_view(block_start_offset, offset - block_start_offset),
  71. });
  72. current_block_type.clear();
  73. }
  74. blocks.append({ BlockType::Newline, Utf8View {} });
  75. continue;
  76. }
  77. default: {
  78. if (current_block_type.has_value() && current_block_type.value() != BlockType::Word) {
  79. blocks.append({
  80. current_block_type.value(),
  81. m_text.substring_view(block_start_offset, offset - block_start_offset),
  82. });
  83. current_block_type.clear();
  84. }
  85. if (!current_block_type.has_value()) {
  86. current_block_type = BlockType::Word;
  87. block_start_offset = offset;
  88. }
  89. }
  90. }
  91. }
  92. if (current_block_type.has_value()) {
  93. blocks.append({
  94. current_block_type.value(),
  95. m_text.substring_view(block_start_offset, m_text.byte_length() - block_start_offset),
  96. });
  97. }
  98. Vector<ByteString> lines;
  99. StringBuilder builder;
  100. float line_width = 0;
  101. size_t current_block = 0;
  102. for (Block& block : blocks) {
  103. switch (block.type) {
  104. case BlockType::Newline: {
  105. lines.append(builder.to_byte_string());
  106. builder.clear();
  107. line_width = 0;
  108. current_block++;
  109. continue;
  110. }
  111. case BlockType::Whitespace:
  112. case BlockType::Word: {
  113. float block_width = m_font.width(block.characters);
  114. // FIXME: This should look at the specific advance amount of the
  115. // last character, but we don't support that yet.
  116. if (current_block != blocks.size() - 1) {
  117. block_width += m_font.glyph_spacing();
  118. }
  119. if (wrapping == TextWrapping::Wrap && line_width + block_width > m_rect.width()) {
  120. lines.append(builder.to_byte_string());
  121. builder.clear();
  122. line_width = 0;
  123. }
  124. builder.append(block.characters.as_string());
  125. line_width += block_width;
  126. current_block++;
  127. }
  128. }
  129. }
  130. auto last_line = builder.to_byte_string();
  131. if (!last_line.is_empty())
  132. lines.append(last_line);
  133. switch (elision) {
  134. case TextElision::None:
  135. break;
  136. case TextElision::Right: {
  137. lines.at(lines.size() - 1) = elide_text_from_right(Utf8View { lines.at(lines.size() - 1) });
  138. break;
  139. }
  140. }
  141. return lines;
  142. }
  143. ByteString TextLayout::elide_text_from_right(Utf8View text) const
  144. {
  145. float text_width = m_font.width(text);
  146. if (text_width > static_cast<float>(m_rect.width())) {
  147. float ellipsis_width = m_font.width("..."sv);
  148. float current_width = ellipsis_width;
  149. size_t glyph_spacing = m_font.glyph_spacing();
  150. // FIXME: This code will break when the font has glyphs with advance
  151. // amounts different from the actual width of the glyph
  152. // (which is the case with many TrueType fonts).
  153. if (ellipsis_width < text_width) {
  154. size_t offset = 0;
  155. for (auto it = text.begin(); !it.done(); ++it) {
  156. auto glyph_width = m_font.glyph_or_emoji_width(it);
  157. // NOTE: Glyph spacing should not be added after the last glyph on the line,
  158. // but since we are here because the last glyph does not actually fit on the line,
  159. // we don't have to worry about spacing.
  160. auto width_with_this_glyph_included = current_width + glyph_width + glyph_spacing;
  161. if (width_with_this_glyph_included > m_rect.width())
  162. break;
  163. current_width += glyph_width + glyph_spacing;
  164. offset = text.iterator_offset(it);
  165. }
  166. StringBuilder builder;
  167. builder.append(text.substring_view(0, offset).as_string());
  168. builder.append("..."sv);
  169. return builder.to_byte_string();
  170. }
  171. }
  172. return text.as_string();
  173. }
  174. DrawGlyphOrEmoji prepare_draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font)
  175. {
  176. u32 code_point = *it;
  177. auto next_code_point = it.peek(1);
  178. ScopeGuard consume_variation_selector = [&, initial_it = it] {
  179. static auto const variation_selector = Unicode::property_from_string("Variation_Selector"sv);
  180. if (!variation_selector.has_value())
  181. return;
  182. // If we advanced the iterator to consume an emoji sequence, don't look for another variation selector.
  183. if (initial_it != it)
  184. return;
  185. // Otherwise, discard one code point if it's a variation selector.
  186. if (next_code_point.has_value() && Unicode::code_point_has_property(*next_code_point, *variation_selector))
  187. ++it;
  188. };
  189. // NOTE: We don't check for emoji
  190. auto font_contains_glyph = font.contains_glyph(code_point);
  191. auto check_for_emoji = !font.has_color_bitmaps() && Unicode::could_be_start_of_emoji_sequence(it, font_contains_glyph ? Unicode::SequenceType::EmojiPresentation : Unicode::SequenceType::Any);
  192. // If the font contains the glyph, and we know it's not the start of an emoji, draw a text glyph.
  193. if (font_contains_glyph && !check_for_emoji) {
  194. return DrawGlyph {
  195. .position = point,
  196. .code_point = code_point,
  197. .font = font,
  198. };
  199. }
  200. // If we didn't find a text glyph, or have an emoji variation selector or regional indicator, try to draw an emoji glyph.
  201. if (auto const* emoji = Emoji::emoji_for_code_point_iterator(it)) {
  202. return DrawEmoji {
  203. .position = point,
  204. .emoji = emoji,
  205. .font = font,
  206. };
  207. }
  208. // If that failed, but we have a text glyph fallback, draw that.
  209. if (font_contains_glyph) {
  210. return DrawGlyph {
  211. .position = point,
  212. .code_point = code_point,
  213. .font = font,
  214. };
  215. }
  216. // No suitable glyph found, draw a replacement character.
  217. dbgln_if(EMOJI_DEBUG, "Failed to find a glyph or emoji for code_point {}", code_point);
  218. return DrawGlyph {
  219. .position = point,
  220. .code_point = 0xFFFD,
  221. .font = font,
  222. };
  223. }
  224. }