TextLayout.cpp 8.3 KB

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