TextLayout.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. namespace Gfx {
  9. enum class BlockType {
  10. Newline,
  11. Whitespace,
  12. Word
  13. };
  14. struct Block {
  15. BlockType type;
  16. Utf8View characters;
  17. };
  18. IntRect TextLayout::bounding_rect(TextWrapping wrapping, int line_spacing) const
  19. {
  20. auto lines = wrap_lines(TextElision::None, wrapping, line_spacing, FitWithinRect::No);
  21. if (!lines.size()) {
  22. return {};
  23. }
  24. IntRect bounding_rect = {
  25. 0, 0, 0, static_cast<int>((lines.size() * (m_font->glyph_height() + line_spacing)) - line_spacing)
  26. };
  27. for (auto& line : lines) {
  28. auto line_width = m_font->width(line);
  29. if (line_width > bounding_rect.width())
  30. bounding_rect.set_width(line_width);
  31. }
  32. return bounding_rect;
  33. }
  34. Vector<String, 32> TextLayout::wrap_lines(TextElision elision, TextWrapping wrapping, int line_spacing, FitWithinRect fit_within_rect) const
  35. {
  36. Vector<Block> blocks;
  37. Optional<BlockType> current_block_type;
  38. size_t block_start_offset = 0;
  39. size_t offset = 0;
  40. for (auto it = m_text.begin(); !it.done(); ++it) {
  41. offset = m_text.iterator_offset(it);
  42. switch (*it) {
  43. case '\t':
  44. case ' ': {
  45. if (current_block_type.has_value() && current_block_type.value() != BlockType::Whitespace) {
  46. blocks.append({
  47. current_block_type.value(),
  48. m_text.substring_view(block_start_offset, offset - block_start_offset),
  49. });
  50. current_block_type.clear();
  51. }
  52. if (!current_block_type.has_value()) {
  53. current_block_type = BlockType::Whitespace;
  54. block_start_offset = offset;
  55. }
  56. continue;
  57. }
  58. case '\n':
  59. case '\r': {
  60. if (current_block_type.has_value()) {
  61. blocks.append({
  62. current_block_type.value(),
  63. m_text.substring_view(block_start_offset, offset - block_start_offset),
  64. });
  65. current_block_type.clear();
  66. }
  67. blocks.append({ BlockType::Newline, Utf8View {} });
  68. continue;
  69. }
  70. default: {
  71. if (current_block_type.has_value() && current_block_type.value() != BlockType::Word) {
  72. blocks.append({
  73. current_block_type.value(),
  74. m_text.substring_view(block_start_offset, offset - block_start_offset),
  75. });
  76. current_block_type.clear();
  77. }
  78. if (!current_block_type.has_value()) {
  79. current_block_type = BlockType::Word;
  80. block_start_offset = offset;
  81. }
  82. }
  83. }
  84. }
  85. if (current_block_type.has_value()) {
  86. blocks.append({
  87. current_block_type.value(),
  88. m_text.substring_view(block_start_offset, m_text.byte_length() - block_start_offset),
  89. });
  90. }
  91. size_t max_lines_that_can_fit = 0;
  92. if (m_rect.height() >= m_font->glyph_height()) {
  93. // NOTE: If glyph height is 10 and line spacing is 1, we can fit a
  94. // single line into a 10px rect and a 20px rect, but 2 lines into a
  95. // 21px rect.
  96. max_lines_that_can_fit = 1 + (m_rect.height() - m_font->glyph_height()) / (m_font->glyph_height() + line_spacing);
  97. }
  98. if (max_lines_that_can_fit == 0)
  99. return {};
  100. Vector<String> lines;
  101. StringBuilder builder;
  102. size_t line_width = 0;
  103. bool did_not_finish = false;
  104. for (Block& block : blocks) {
  105. switch (block.type) {
  106. case BlockType::Newline: {
  107. lines.append(builder.to_string());
  108. builder.clear();
  109. line_width = 0;
  110. if (lines.size() == max_lines_that_can_fit && fit_within_rect == FitWithinRect::Yes) {
  111. did_not_finish = true;
  112. goto blocks_processed;
  113. }
  114. continue;
  115. }
  116. case BlockType::Whitespace:
  117. case BlockType::Word: {
  118. size_t block_width = font().width(block.characters);
  119. if (line_width > 0) {
  120. block_width += font().glyph_width('x');
  121. if (wrapping == TextWrapping::Wrap && line_width + block_width > static_cast<unsigned>(m_rect.width())) {
  122. lines.append(builder.to_string());
  123. builder.clear();
  124. line_width = 0;
  125. if (lines.size() == max_lines_that_can_fit && fit_within_rect == FitWithinRect::Yes) {
  126. did_not_finish = true;
  127. goto blocks_processed;
  128. }
  129. }
  130. }
  131. if (lines.size() == max_lines_that_can_fit && fit_within_rect == FitWithinRect::Yes) {
  132. did_not_finish = true;
  133. break;
  134. }
  135. builder.append(block.characters.as_string());
  136. line_width += block_width;
  137. }
  138. }
  139. }
  140. blocks_processed:
  141. if (!did_not_finish) {
  142. auto last_line = builder.to_string();
  143. if (!last_line.is_empty())
  144. lines.append(last_line);
  145. }
  146. switch (elision) {
  147. case TextElision::None:
  148. break;
  149. case TextElision::Right: {
  150. lines.at(lines.size() - 1) = elide_text_from_right(Utf8View { lines.at(lines.size() - 1) }, did_not_finish);
  151. break;
  152. }
  153. }
  154. return lines;
  155. }
  156. String TextLayout::elide_text_from_right(Utf8View text, bool force_elision) const
  157. {
  158. size_t text_width = m_font->width(text);
  159. if (force_elision || text_width > static_cast<unsigned>(m_rect.width())) {
  160. size_t ellipsis_width = m_font->width("...");
  161. size_t current_width = ellipsis_width;
  162. size_t glyph_spacing = m_font->glyph_spacing();
  163. // FIXME: This code will break when the font has glyphs with advance
  164. // amounts different from the actual width of the glyph
  165. // (which is the case with many TrueType fonts).
  166. if (ellipsis_width < text_width) {
  167. size_t offset = 0;
  168. for (auto it = text.begin(); !it.done(); ++it) {
  169. auto code_point = *it;
  170. int glyph_width = m_font->glyph_or_emoji_width(code_point);
  171. // NOTE: Glyph spacing should not be added after the last glyph on the line,
  172. // but since we are here because the last glyph does not actually fit on the line,
  173. // we don't have to worry about spacing.
  174. int width_with_this_glyph_included = current_width + glyph_width + glyph_spacing;
  175. if (width_with_this_glyph_included > m_rect.width())
  176. break;
  177. current_width += glyph_width + glyph_spacing;
  178. offset = text.iterator_offset(it);
  179. }
  180. StringBuilder builder;
  181. builder.append(text.substring_view(0, offset).as_string());
  182. builder.append("...");
  183. return builder.to_string();
  184. }
  185. }
  186. return text.as_string();
  187. }
  188. }