LayoutText.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 (int 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 color = style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black);
  40. auto text_decoration = style().string_or_fallback(CSS::PropertyID::TextDecoration, "none");
  41. if (document().inspected_node() == &node())
  42. context.painter().draw_rect(enclosing_int_rect(fragment.rect()), Color::Magenta);
  43. bool is_underline = text_decoration == "underline";
  44. if (is_underline)
  45. painter.draw_line(enclosing_int_rect(fragment.rect()).bottom_left().translated(0, -1), enclosing_int_rect(fragment.rect()).bottom_right().translated(0, -1), color);
  46. painter.draw_text(enclosing_int_rect(fragment.rect()), m_text_for_rendering.substring_view(fragment.start(), fragment.length()), TextAlignment::TopLeft, color);
  47. }
  48. template<typename Callback>
  49. void LayoutText::for_each_word(Callback callback) const
  50. {
  51. Utf8View view(m_text_for_rendering);
  52. if (view.is_empty())
  53. return;
  54. auto start_of_word = view.begin();
  55. auto commit_word = [&](auto it) {
  56. int start = view.byte_offset_of(start_of_word);
  57. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_word);
  58. if (length > 0) {
  59. callback(view.substring_view(start, length), start, length);
  60. }
  61. start_of_word = it;
  62. };
  63. bool last_was_space = isspace(*view.begin());
  64. for (auto it = view.begin(); it != view.end();) {
  65. bool is_space = isspace(*it);
  66. if (is_space == last_was_space) {
  67. ++it;
  68. continue;
  69. }
  70. last_was_space = is_space;
  71. commit_word(it);
  72. ++it;
  73. }
  74. if (start_of_word != view.end())
  75. commit_word(view.end());
  76. }
  77. template<typename Callback>
  78. void LayoutText::for_each_source_line(Callback callback) const
  79. {
  80. Utf8View view(m_text_for_rendering);
  81. if (view.is_empty())
  82. return;
  83. auto start_of_line = view.begin();
  84. auto commit_line = [&](auto it) {
  85. int start = view.byte_offset_of(start_of_line);
  86. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_line);
  87. if (length > 0) {
  88. callback(view.substring_view(start, length), start, length);
  89. }
  90. };
  91. for (auto it = view.begin(); it != view.end();) {
  92. bool did_commit = false;
  93. if (*it == '\n') {
  94. commit_line(it);
  95. did_commit = true;
  96. }
  97. ++it;
  98. if (did_commit)
  99. start_of_line = it;
  100. }
  101. if (start_of_line != view.end())
  102. commit_line(view.end());
  103. }
  104. void LayoutText::split_into_lines(LayoutBlock& container)
  105. {
  106. auto& font = style().font();
  107. int space_width = font.glyph_width(' ') + font.glyph_spacing();
  108. int line_height = style().line_height();
  109. auto& line_boxes = container.line_boxes();
  110. if (line_boxes.is_empty())
  111. line_boxes.append(LineBox());
  112. int available_width = container.width() - line_boxes.last().width();
  113. bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre";
  114. if (is_preformatted) {
  115. m_text_for_rendering = node().data();
  116. for_each_source_line([&](const Utf8View& view, int start, int length) {
  117. line_boxes.last().add_fragment(*this, start, length, font.width(view), line_height);
  118. line_boxes.append(LineBox());
  119. });
  120. return;
  121. }
  122. // Collapse whitespace into single spaces
  123. auto utf8_view = Utf8View(node().data());
  124. StringBuilder builder(node().data().length());
  125. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  126. if (!isspace(*it)) {
  127. builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.codepoint_length_in_bytes());
  128. } else {
  129. builder.append(' ');
  130. auto prev = it;
  131. while (it != utf8_view.end() && isspace(*it)) {
  132. prev = it;
  133. ++it;
  134. }
  135. it = prev;
  136. }
  137. }
  138. m_text_for_rendering = builder.to_string();
  139. struct Word {
  140. Utf8View view;
  141. int start;
  142. int length;
  143. };
  144. Vector<Word> words;
  145. for_each_word([&](const Utf8View& view, int start, int length) {
  146. words.append({ Utf8View(view), start, length });
  147. });
  148. for (int i = 0; i < words.size(); ++i) {
  149. auto& word = words[i];
  150. int word_width;
  151. bool is_whitespace = isspace(*word.view.begin());
  152. if (is_whitespace)
  153. word_width = space_width;
  154. else
  155. word_width = font.width(word.view) + font.glyph_spacing();
  156. if (line_boxes.last().width() > 0 && word_width > available_width) {
  157. line_boxes.append(LineBox());
  158. available_width = container.width();
  159. }
  160. if (is_whitespace && line_boxes.last().fragments().is_empty())
  161. continue;
  162. line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, line_height);
  163. available_width -= word_width;
  164. if (available_width < 0) {
  165. line_boxes.append(LineBox());
  166. available_width = container.width();
  167. }
  168. }
  169. }