LayoutText.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <AK/Utf8View.h>
  28. #include <LibCore/DirIterator.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGUI/Painter.h>
  31. #include <LibHTML/DOM/Document.h>
  32. #include <LibHTML/Layout/LayoutBlock.h>
  33. #include <LibHTML/Layout/LayoutText.h>
  34. #include <ctype.h>
  35. LayoutText::LayoutText(const Text& text)
  36. : LayoutNode(&text)
  37. {
  38. set_inline(true);
  39. }
  40. LayoutText::~LayoutText()
  41. {
  42. }
  43. static bool is_all_whitespace(const String& string)
  44. {
  45. for (size_t i = 0; i < string.length(); ++i) {
  46. if (!isspace(string[i]))
  47. return false;
  48. }
  49. return true;
  50. }
  51. const String& LayoutText::text_for_style(const StyleProperties& style) const
  52. {
  53. static String one_space = " ";
  54. if (is_all_whitespace(node().data())) {
  55. if (style.string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "normal")
  56. return one_space;
  57. }
  58. return node().data();
  59. }
  60. void LayoutText::render_fragment(RenderingContext& context, const LineBoxFragment& fragment) const
  61. {
  62. auto& painter = context.painter();
  63. painter.set_font(style().font());
  64. auto background_color = style().property(CSS::PropertyID::BackgroundColor);
  65. if (background_color.has_value() && background_color.value()->is_color())
  66. painter.fill_rect(enclosing_int_rect(fragment.rect()), background_color.value()->to_color(document()));
  67. auto color = style().color_or_fallback(CSS::PropertyID::Color, document(), context.palette().base_text());
  68. auto text_decoration = style().string_or_fallback(CSS::PropertyID::TextDecoration, "none");
  69. if (document().inspected_node() == &node())
  70. context.painter().draw_rect(enclosing_int_rect(fragment.rect()), Color::Magenta);
  71. bool is_underline = text_decoration == "underline";
  72. if (is_underline)
  73. painter.draw_line(enclosing_int_rect(fragment.rect()).bottom_left().translated(0, 1), enclosing_int_rect(fragment.rect()).bottom_right().translated(0, 1), color);
  74. painter.draw_text(enclosing_int_rect(fragment.rect()), m_text_for_rendering.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::TopLeft, color);
  75. }
  76. template<typename Callback>
  77. void LayoutText::for_each_word(Callback callback) const
  78. {
  79. Utf8View view(m_text_for_rendering);
  80. if (view.is_empty())
  81. return;
  82. auto start_of_word = view.begin();
  83. auto commit_word = [&](auto it) {
  84. int start = view.byte_offset_of(start_of_word);
  85. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_word);
  86. if (length > 0) {
  87. callback(view.substring_view(start, length), start, length);
  88. }
  89. start_of_word = it;
  90. };
  91. bool last_was_space = isspace(*view.begin());
  92. for (auto it = view.begin(); it != view.end();) {
  93. bool is_space = isspace(*it);
  94. if (is_space == last_was_space) {
  95. ++it;
  96. continue;
  97. }
  98. last_was_space = is_space;
  99. commit_word(it);
  100. ++it;
  101. }
  102. if (start_of_word != view.end())
  103. commit_word(view.end());
  104. }
  105. void LayoutText::split_preformatted_into_lines(LayoutBlock& container)
  106. {
  107. auto& font = style().font();
  108. auto& line_boxes = container.line_boxes();
  109. m_text_for_rendering = node().data();
  110. Utf8View view(m_text_for_rendering);
  111. if (view.is_empty())
  112. return;
  113. auto start_of_line = view.begin();
  114. auto commit_line = [&](auto it) {
  115. int start = view.byte_offset_of(start_of_line);
  116. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_line);
  117. if (length > 0)
  118. line_boxes.last().add_fragment(*this, start, length, font.width(view), font.glyph_height());
  119. };
  120. bool last_was_newline = false;
  121. for (auto it = view.begin(); it != view.end();) {
  122. bool did_commit = false;
  123. if (*it == '\n') {
  124. commit_line(it);
  125. line_boxes.append(LineBox());
  126. did_commit = true;
  127. last_was_newline = true;
  128. } else {
  129. last_was_newline = false;
  130. }
  131. ++it;
  132. if (did_commit)
  133. start_of_line = it;
  134. }
  135. if (start_of_line != view.end() || last_was_newline)
  136. commit_line(view.end());
  137. }
  138. void LayoutText::split_into_lines(LayoutBlock& container)
  139. {
  140. auto& font = style().font();
  141. float space_width = font.glyph_width(' ') + font.glyph_spacing();
  142. auto& line_boxes = container.line_boxes();
  143. if (line_boxes.is_empty())
  144. line_boxes.append(LineBox());
  145. float available_width = container.width() - line_boxes.last().width();
  146. if (style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre") {
  147. split_preformatted_into_lines(container);
  148. return;
  149. }
  150. // Collapse whitespace into single spaces
  151. auto utf8_view = Utf8View(node().data());
  152. StringBuilder builder(node().data().length());
  153. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  154. if (!isspace(*it)) {
  155. builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.codepoint_length_in_bytes());
  156. } else {
  157. builder.append(' ');
  158. auto prev = it;
  159. while (it != utf8_view.end() && isspace(*it)) {
  160. prev = it;
  161. ++it;
  162. }
  163. it = prev;
  164. }
  165. }
  166. m_text_for_rendering = builder.to_string();
  167. struct Word {
  168. Utf8View view;
  169. int start;
  170. int length;
  171. };
  172. Vector<Word> words;
  173. for_each_word([&](const Utf8View& view, int start, int length) {
  174. words.append({ Utf8View(view), start, length });
  175. });
  176. for (size_t i = 0; i < words.size(); ++i) {
  177. auto& word = words[i];
  178. float word_width;
  179. bool is_whitespace = isspace(*word.view.begin());
  180. if (is_whitespace)
  181. word_width = space_width;
  182. else
  183. word_width = font.width(word.view) + font.glyph_spacing();
  184. if (line_boxes.last().width() > 0 && word_width > available_width) {
  185. line_boxes.append(LineBox());
  186. available_width = container.width();
  187. }
  188. if (is_whitespace && line_boxes.last().fragments().is_empty())
  189. continue;
  190. line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, font.glyph_height());
  191. available_width -= word_width;
  192. if (available_width < 0) {
  193. line_boxes.append(LineBox());
  194. available_width = container.width();
  195. }
  196. }
  197. }