LayoutText.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <LibGUI/Painter.h>
  30. #include <LibGfx/Font.h>
  31. #include <LibWeb/DOM/Document.h>
  32. #include <LibWeb/Layout/LayoutBlock.h>
  33. #include <LibWeb/Layout/LayoutText.h>
  34. #include <LibWeb/Page/Frame.h>
  35. #include <ctype.h>
  36. namespace Web {
  37. LayoutText::LayoutText(DOM::Document& document, DOM::Text& text)
  38. : LayoutNode(document, &text)
  39. {
  40. set_inline(true);
  41. }
  42. LayoutText::~LayoutText()
  43. {
  44. }
  45. static bool is_all_whitespace(const StringView& string)
  46. {
  47. for (size_t i = 0; i < string.length(); ++i) {
  48. if (!isspace(string[i]))
  49. return false;
  50. }
  51. return true;
  52. }
  53. const String& LayoutText::text_for_style(const CSS::StyleProperties& style) const
  54. {
  55. static String one_space = " ";
  56. if (is_all_whitespace(node().data())) {
  57. if (style.white_space().value_or(CSS::WhiteSpace::Normal) == CSS::WhiteSpace::Normal)
  58. return one_space;
  59. }
  60. return node().data();
  61. }
  62. void LayoutText::paint_fragment(PaintContext& context, const LineBoxFragment& fragment) const
  63. {
  64. auto& painter = context.painter();
  65. painter.set_font(specified_style().font());
  66. auto background_color = specified_style().property(CSS::PropertyID::BackgroundColor);
  67. if (background_color.has_value() && background_color.value()->is_color())
  68. painter.fill_rect(enclosing_int_rect(fragment.absolute_rect()), background_color.value()->to_color(document()));
  69. auto color = specified_style().color_or_fallback(CSS::PropertyID::Color, document(), context.palette().base_text());
  70. auto text_decoration = specified_style().string_or_fallback(CSS::PropertyID::TextDecoration, "none");
  71. if (document().inspected_node() == &node())
  72. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  73. bool is_underline = text_decoration == "underline";
  74. if (is_underline)
  75. painter.draw_line(enclosing_int_rect(fragment.absolute_rect()).bottom_left().translated(0, 1), enclosing_int_rect(fragment.absolute_rect()).bottom_right().translated(0, 1), color);
  76. // FIXME: text-transform should be done already in layout, since uppercase glyphs may be wider than lowercase, etc.
  77. auto text = m_text_for_rendering;
  78. auto text_transform = specified_style().string_or_fallback(CSS::PropertyID::TextTransform, "none");
  79. if (text_transform == "uppercase")
  80. text = m_text_for_rendering.to_uppercase();
  81. if (text_transform == "lowercase")
  82. text = m_text_for_rendering.to_lowercase();
  83. painter.draw_text(enclosing_int_rect(fragment.absolute_rect()), text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::TopLeft, color);
  84. auto selection_rect = fragment.selection_rect(specified_style().font());
  85. if (!selection_rect.is_empty()) {
  86. painter.fill_rect(enclosing_int_rect(selection_rect), context.palette().selection());
  87. Gfx::PainterStateSaver saver(painter);
  88. painter.add_clip_rect(enclosing_int_rect(selection_rect));
  89. painter.draw_text(enclosing_int_rect(fragment.absolute_rect()), text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::TopLeft, context.palette().selection_text());
  90. }
  91. paint_cursor_if_needed(context, fragment);
  92. }
  93. void LayoutText::paint_cursor_if_needed(PaintContext& context, const LineBoxFragment& fragment) const
  94. {
  95. if (!frame().cursor_blink_state())
  96. return;
  97. if (frame().cursor_position().node() != &node())
  98. return;
  99. if (!(frame().cursor_position().offset() >= (unsigned)fragment.start() && frame().cursor_position().offset() < (unsigned)(fragment.start() + fragment.length())))
  100. return;
  101. auto fragment_rect = fragment.absolute_rect();
  102. float cursor_x = fragment_rect.x() + specified_style().font().width(fragment.text().substring_view(0, frame().cursor_position().offset() - fragment.start()));
  103. float cursor_top = fragment_rect.top();
  104. float cursor_height = fragment_rect.height();
  105. Gfx::IntRect cursor_rect(cursor_x, cursor_top, 1, cursor_height);
  106. context.painter().draw_rect(cursor_rect, context.palette().text_cursor());
  107. }
  108. template<typename Callback>
  109. void LayoutText::for_each_chunk(Callback callback, LayoutMode layout_mode, bool do_wrap_lines, bool do_wrap_breaks) const
  110. {
  111. Utf8View view(m_text_for_rendering);
  112. if (view.is_empty())
  113. return;
  114. auto start_of_chunk = view.begin();
  115. auto commit_chunk = [&](auto it, bool has_breaking_newline, bool must_commit = false) {
  116. if (layout_mode == LayoutMode::OnlyRequiredLineBreaks && !must_commit)
  117. return;
  118. int start = view.byte_offset_of(start_of_chunk);
  119. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_chunk);
  120. if (has_breaking_newline || length > 0) {
  121. auto chunk_view = view.substring_view(start, length);
  122. callback(chunk_view, start, length, has_breaking_newline, is_all_whitespace(chunk_view.as_string()));
  123. }
  124. start_of_chunk = it;
  125. };
  126. bool last_was_space = isspace(*view.begin());
  127. bool last_was_newline = false;
  128. for (auto it = view.begin(); it != view.end();) {
  129. if (layout_mode == LayoutMode::AllPossibleLineBreaks) {
  130. commit_chunk(it, false);
  131. }
  132. if (last_was_newline) {
  133. last_was_newline = false;
  134. commit_chunk(it, true);
  135. }
  136. if (do_wrap_breaks && *it == '\n') {
  137. last_was_newline = true;
  138. commit_chunk(it, false);
  139. }
  140. if (do_wrap_lines) {
  141. bool is_space = isspace(*it);
  142. if (is_space != last_was_space) {
  143. last_was_space = is_space;
  144. commit_chunk(it, false);
  145. }
  146. }
  147. ++it;
  148. }
  149. if (last_was_newline)
  150. commit_chunk(view.end(), true);
  151. if (start_of_chunk != view.end())
  152. commit_chunk(view.end(), false, true);
  153. }
  154. void LayoutText::split_into_lines_by_rules(LayoutBlock& container, LayoutMode layout_mode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks)
  155. {
  156. auto& font = specified_style().font();
  157. float space_width = font.glyph_width(' ') + font.glyph_spacing();
  158. auto& line_boxes = container.line_boxes();
  159. if (line_boxes.is_empty())
  160. line_boxes.append(LineBox());
  161. float available_width = container.width() - line_boxes.last().width();
  162. // Collapse whitespace into single spaces
  163. if (do_collapse) {
  164. auto utf8_view = Utf8View(node().data());
  165. StringBuilder builder(node().data().length());
  166. auto it = utf8_view.begin();
  167. auto skip_over_whitespace = [&] {
  168. auto prev = it;
  169. while (it != utf8_view.end() && isspace(*it)) {
  170. prev = it;
  171. ++it;
  172. }
  173. it = prev;
  174. };
  175. if (line_boxes.last().ends_in_whitespace())
  176. skip_over_whitespace();
  177. for (; it != utf8_view.end(); ++it) {
  178. if (!isspace(*it)) {
  179. builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.codepoint_length_in_bytes());
  180. } else {
  181. builder.append(' ');
  182. skip_over_whitespace();
  183. }
  184. }
  185. m_text_for_rendering = builder.to_string();
  186. } else {
  187. m_text_for_rendering = node().data();
  188. }
  189. // do_wrap_lines => chunks_are_words
  190. // !do_wrap_lines => chunks_are_lines
  191. struct Chunk {
  192. Utf8View view;
  193. int start { 0 };
  194. int length { 0 };
  195. bool is_break { false };
  196. bool is_all_whitespace { false };
  197. };
  198. Vector<Chunk> chunks;
  199. for_each_chunk(
  200. [&](const Utf8View& view, int start, int length, bool is_break, bool is_all_whitespace) {
  201. chunks.append({ Utf8View(view), start, length, is_break, is_all_whitespace });
  202. },
  203. layout_mode, do_wrap_lines, do_wrap_breaks);
  204. for (size_t i = 0; i < chunks.size(); ++i) {
  205. auto& chunk = chunks[i];
  206. // Collapse entire fragment into non-existence if previous fragment on line ended in whitespace.
  207. if (do_collapse && line_boxes.last().ends_in_whitespace() && chunk.is_all_whitespace)
  208. continue;
  209. float chunk_width;
  210. bool need_collapse = false;
  211. if (do_wrap_lines) {
  212. need_collapse = do_collapse && isspace(*chunk.view.begin()) && line_boxes.last().ends_in_whitespace();
  213. if (need_collapse)
  214. chunk_width = space_width;
  215. else
  216. chunk_width = font.width(chunk.view) + font.glyph_spacing();
  217. if (line_boxes.last().width() > 0 && chunk_width > available_width) {
  218. line_boxes.append(LineBox());
  219. available_width = container.width();
  220. }
  221. if (need_collapse & line_boxes.last().fragments().is_empty())
  222. continue;
  223. } else {
  224. chunk_width = font.width(chunk.view);
  225. }
  226. line_boxes.last().add_fragment(*this, chunk.start, need_collapse ? 1 : chunk.length, chunk_width, font.glyph_height());
  227. available_width -= chunk_width;
  228. if (do_wrap_lines) {
  229. if (available_width < 0) {
  230. line_boxes.append(LineBox());
  231. available_width = container.width();
  232. }
  233. }
  234. if (do_wrap_breaks) {
  235. if (chunk.is_break) {
  236. line_boxes.append(LineBox());
  237. available_width = container.width();
  238. }
  239. }
  240. }
  241. }
  242. void LayoutText::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  243. {
  244. bool do_collapse = true;
  245. bool do_wrap_lines = true;
  246. bool do_wrap_breaks = false;
  247. if (style().white_space() == CSS::WhiteSpace::Nowrap) {
  248. do_collapse = true;
  249. do_wrap_lines = false;
  250. do_wrap_breaks = false;
  251. } else if (style().white_space() == CSS::WhiteSpace::Pre) {
  252. do_collapse = false;
  253. do_wrap_lines = false;
  254. do_wrap_breaks = true;
  255. } else if (style().white_space() == CSS::WhiteSpace::PreLine) {
  256. do_collapse = true;
  257. do_wrap_lines = true;
  258. do_wrap_breaks = true;
  259. } else if (style().white_space() == CSS::WhiteSpace::PreWrap) {
  260. do_collapse = false;
  261. do_wrap_lines = true;
  262. do_wrap_breaks = true;
  263. }
  264. split_into_lines_by_rules(container, layout_mode, do_collapse, do_wrap_lines, do_wrap_breaks);
  265. }
  266. }