LayoutText.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <AK/StringBuilder.h>
  2. #include <AK/Utf8View.h>
  3. #include <LibCore/CDirIterator.h>
  4. #include <LibDraw/Font.h>
  5. #include <LibGUI/GPainter.h>
  6. #include <LibHTML/DOM/Document.h>
  7. #include <LibHTML/Layout/LayoutBlock.h>
  8. #include <LibHTML/Layout/LayoutText.h>
  9. #include <ctype.h>
  10. LayoutText::LayoutText(const Text& text)
  11. : LayoutNode(&text)
  12. {
  13. set_inline(true);
  14. }
  15. LayoutText::~LayoutText()
  16. {
  17. }
  18. static bool is_all_whitespace(const String& string)
  19. {
  20. for (size_t i = 0; i < string.length(); ++i) {
  21. if (!isspace(string[i]))
  22. return false;
  23. }
  24. return true;
  25. }
  26. const String& LayoutText::text_for_style(const StyleProperties& style) const
  27. {
  28. static String one_space = " ";
  29. if (is_all_whitespace(node().data())) {
  30. if (style.string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "normal")
  31. return one_space;
  32. }
  33. return node().data();
  34. }
  35. void LayoutText::render_fragment(RenderingContext& context, const LineBoxFragment& fragment) const
  36. {
  37. auto& painter = context.painter();
  38. painter.set_font(style().font());
  39. auto background_color = style().property(CSS::PropertyID::BackgroundColor);
  40. if (background_color.has_value() && background_color.value()->is_color())
  41. painter.fill_rect(enclosing_int_rect(fragment.rect()), background_color.value()->to_color(document()));
  42. auto color = style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black);
  43. auto text_decoration = style().string_or_fallback(CSS::PropertyID::TextDecoration, "none");
  44. if (document().inspected_node() == &node())
  45. context.painter().draw_rect(enclosing_int_rect(fragment.rect()), Color::Magenta);
  46. bool is_underline = text_decoration == "underline";
  47. if (is_underline)
  48. painter.draw_line(enclosing_int_rect(fragment.rect()).bottom_left().translated(0, 1), enclosing_int_rect(fragment.rect()).bottom_right().translated(0, 1), color);
  49. painter.draw_text(enclosing_int_rect(fragment.rect()), m_text_for_rendering.substring_view(fragment.start(), fragment.length()), TextAlignment::TopLeft, color);
  50. }
  51. template<typename Callback>
  52. void LayoutText::for_each_word(Callback callback) const
  53. {
  54. Utf8View view(m_text_for_rendering);
  55. if (view.is_empty())
  56. return;
  57. auto start_of_word = view.begin();
  58. auto commit_word = [&](auto it) {
  59. int start = view.byte_offset_of(start_of_word);
  60. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_word);
  61. if (length > 0) {
  62. callback(view.substring_view(start, length), start, length);
  63. }
  64. start_of_word = it;
  65. };
  66. bool last_was_space = isspace(*view.begin());
  67. for (auto it = view.begin(); it != view.end();) {
  68. bool is_space = isspace(*it);
  69. if (is_space == last_was_space) {
  70. ++it;
  71. continue;
  72. }
  73. last_was_space = is_space;
  74. commit_word(it);
  75. ++it;
  76. }
  77. if (start_of_word != view.end())
  78. commit_word(view.end());
  79. }
  80. template<typename Callback>
  81. void LayoutText::for_each_source_line(Callback callback) const
  82. {
  83. Utf8View view(m_text_for_rendering);
  84. if (view.is_empty())
  85. return;
  86. auto start_of_line = view.begin();
  87. auto commit_line = [&](auto it) {
  88. int start = view.byte_offset_of(start_of_line);
  89. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_line);
  90. callback(view.substring_view(start, length), start, length);
  91. };
  92. bool last_was_newline = false;
  93. for (auto it = view.begin(); it != view.end();) {
  94. bool did_commit = false;
  95. if (*it == '\n') {
  96. commit_line(it);
  97. did_commit = true;
  98. last_was_newline = true;
  99. } else {
  100. last_was_newline = false;
  101. }
  102. ++it;
  103. if (did_commit)
  104. start_of_line = it;
  105. }
  106. if (start_of_line != view.end() || last_was_newline)
  107. commit_line(view.end());
  108. }
  109. void LayoutText::split_into_lines(LayoutBlock& container)
  110. {
  111. auto& font = style().font();
  112. float space_width = font.glyph_width(' ') + font.glyph_spacing();
  113. auto& line_boxes = container.line_boxes();
  114. if (line_boxes.is_empty())
  115. line_boxes.append(LineBox());
  116. float available_width = container.width() - line_boxes.last().width();
  117. bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre";
  118. if (is_preformatted) {
  119. m_text_for_rendering = node().data();
  120. int line_count = 0;
  121. for_each_source_line([&](const Utf8View& view, int start, int length) {
  122. if (line_count == 1)
  123. line_boxes.append(LineBox());
  124. line_boxes.last().add_fragment(*this, start, length, font.width(view), font.glyph_height());
  125. if (line_count != 0 && line_boxes.last().width() > 0)
  126. line_boxes.append(LineBox());
  127. ++line_count;
  128. });
  129. return;
  130. }
  131. // Collapse whitespace into single spaces
  132. auto utf8_view = Utf8View(node().data());
  133. StringBuilder builder(node().data().length());
  134. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  135. if (!isspace(*it)) {
  136. builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.codepoint_length_in_bytes());
  137. } else {
  138. builder.append(' ');
  139. auto prev = it;
  140. while (it != utf8_view.end() && isspace(*it)) {
  141. prev = it;
  142. ++it;
  143. }
  144. it = prev;
  145. }
  146. }
  147. m_text_for_rendering = builder.to_string();
  148. struct Word {
  149. Utf8View view;
  150. int start;
  151. int length;
  152. };
  153. Vector<Word> words;
  154. for_each_word([&](const Utf8View& view, int start, int length) {
  155. words.append({ Utf8View(view), start, length });
  156. });
  157. for (int i = 0; i < words.size(); ++i) {
  158. auto& word = words[i];
  159. float word_width;
  160. bool is_whitespace = isspace(*word.view.begin());
  161. if (is_whitespace)
  162. word_width = space_width;
  163. else
  164. word_width = font.width(word.view) + font.glyph_spacing();
  165. if (line_boxes.last().width() > 0 && word_width > available_width) {
  166. line_boxes.append(LineBox());
  167. available_width = container.width();
  168. }
  169. if (is_whitespace && line_boxes.last().fragments().is_empty())
  170. continue;
  171. line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, font.glyph_height());
  172. available_width -= word_width;
  173. if (available_width < 0) {
  174. line_boxes.append(LineBox());
  175. available_width = container.width();
  176. }
  177. }
  178. }