LayoutText.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 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. if (length > 0) {
  91. callback(view.substring_view(start, length), start, length);
  92. }
  93. };
  94. for (auto it = view.begin(); it != view.end();) {
  95. bool did_commit = false;
  96. if (*it == '\n') {
  97. commit_line(it);
  98. did_commit = true;
  99. }
  100. ++it;
  101. if (did_commit)
  102. start_of_line = it;
  103. }
  104. if (start_of_line != view.end())
  105. commit_line(view.end());
  106. }
  107. void LayoutText::split_into_lines(LayoutBlock& container)
  108. {
  109. auto& font = style().font();
  110. float space_width = font.glyph_width(' ') + font.glyph_spacing();
  111. float line_height = style().line_height();
  112. auto& line_boxes = container.line_boxes();
  113. if (line_boxes.is_empty())
  114. line_boxes.append(LineBox());
  115. float available_width = container.width() - line_boxes.last().width();
  116. bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre";
  117. if (is_preformatted) {
  118. m_text_for_rendering = node().data();
  119. for_each_source_line([&](const Utf8View& view, int start, int length) {
  120. line_boxes.last().add_fragment(*this, start, length, font.width(view), line_height);
  121. line_boxes.append(LineBox());
  122. });
  123. return;
  124. }
  125. // Collapse whitespace into single spaces
  126. auto utf8_view = Utf8View(node().data());
  127. StringBuilder builder(node().data().length());
  128. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  129. if (!isspace(*it)) {
  130. builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.codepoint_length_in_bytes());
  131. } else {
  132. builder.append(' ');
  133. auto prev = it;
  134. while (it != utf8_view.end() && isspace(*it)) {
  135. prev = it;
  136. ++it;
  137. }
  138. it = prev;
  139. }
  140. }
  141. m_text_for_rendering = builder.to_string();
  142. struct Word {
  143. Utf8View view;
  144. int start;
  145. int length;
  146. };
  147. Vector<Word> words;
  148. for_each_word([&](const Utf8View& view, int start, int length) {
  149. words.append({ Utf8View(view), start, length });
  150. });
  151. for (int i = 0; i < words.size(); ++i) {
  152. auto& word = words[i];
  153. float word_width;
  154. bool is_whitespace = isspace(*word.view.begin());
  155. if (is_whitespace)
  156. word_width = space_width;
  157. else
  158. word_width = font.width(word.view) + font.glyph_spacing();
  159. if (line_boxes.last().width() > 0 && word_width > available_width) {
  160. line_boxes.append(LineBox());
  161. available_width = container.width();
  162. }
  163. if (is_whitespace && line_boxes.last().fragments().is_empty())
  164. continue;
  165. line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, line_height);
  166. available_width -= word_width;
  167. if (available_width < 0) {
  168. line_boxes.append(LineBox());
  169. available_width = container.width();
  170. }
  171. }
  172. }