LayoutText.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <ctype.h>
  35. namespace Web {
  36. LayoutText::LayoutText(const Text& text)
  37. : LayoutNode(&text)
  38. {
  39. set_inline(true);
  40. }
  41. LayoutText::~LayoutText()
  42. {
  43. }
  44. static bool is_all_whitespace(const String& string)
  45. {
  46. for (size_t i = 0; i < string.length(); ++i) {
  47. if (!isspace(string[i]))
  48. return false;
  49. }
  50. return true;
  51. }
  52. const String& LayoutText::text_for_style(const StyleProperties& style) const
  53. {
  54. static String one_space = " ";
  55. if (is_all_whitespace(node().data())) {
  56. if (style.string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "normal")
  57. return one_space;
  58. }
  59. return node().data();
  60. }
  61. void LayoutText::render_fragment(RenderingContext& context, const LineBoxFragment& fragment) const
  62. {
  63. auto& painter = context.painter();
  64. painter.set_font(style().font());
  65. auto background_color = style().property(CSS::PropertyID::BackgroundColor);
  66. if (background_color.has_value() && background_color.value()->is_color())
  67. painter.fill_rect(enclosing_int_rect(fragment.absolute_rect()), background_color.value()->to_color(document()));
  68. auto color = style().color_or_fallback(CSS::PropertyID::Color, document(), context.palette().base_text());
  69. auto text_decoration = style().string_or_fallback(CSS::PropertyID::TextDecoration, "none");
  70. if (document().inspected_node() == &node())
  71. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  72. bool is_underline = text_decoration == "underline";
  73. if (is_underline)
  74. 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);
  75. auto text = m_text_for_rendering;
  76. auto text_transform = style().string_or_fallback(CSS::PropertyID::TextTransform, "none");
  77. if (text_transform == "uppercase")
  78. text = m_text_for_rendering.to_uppercase();
  79. if (text_transform == "lowercase")
  80. text = m_text_for_rendering.to_lowercase();
  81. painter.draw_text(enclosing_int_rect(fragment.absolute_rect()), text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::TopLeft, color);
  82. }
  83. template<typename Callback>
  84. void LayoutText::for_each_chunk(Callback callback, LayoutMode layout_mode, bool do_wrap_lines, bool do_wrap_breaks) const
  85. {
  86. Utf8View view(m_text_for_rendering);
  87. if (view.is_empty())
  88. return;
  89. auto start_of_chunk = view.begin();
  90. auto commit_chunk = [&](auto it, bool has_breaking_newline, bool must_commit = false) {
  91. if (layout_mode == LayoutMode::OnlyRequiredLineBreaks && !must_commit)
  92. return;
  93. int start = view.byte_offset_of(start_of_chunk);
  94. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_chunk);
  95. if (has_breaking_newline || length > 0) {
  96. callback(view.substring_view(start, length), start, length, has_breaking_newline);
  97. }
  98. start_of_chunk = it;
  99. };
  100. bool last_was_space = isspace(*view.begin());
  101. bool last_was_newline = false;
  102. for (auto it = view.begin(); it != view.end();) {
  103. if (layout_mode == LayoutMode::AllPossibleLineBreaks) {
  104. commit_chunk(it, false);
  105. }
  106. if (last_was_newline) {
  107. last_was_newline = false;
  108. commit_chunk(it, true);
  109. }
  110. if (do_wrap_breaks && *it == '\n') {
  111. last_was_newline = true;
  112. commit_chunk(it, false);
  113. }
  114. if (do_wrap_lines) {
  115. bool is_space = isspace(*it);
  116. if (is_space != last_was_space) {
  117. last_was_space = is_space;
  118. commit_chunk(it, false);
  119. }
  120. }
  121. ++it;
  122. }
  123. if (last_was_newline)
  124. commit_chunk(view.end(), true);
  125. if (start_of_chunk != view.end())
  126. commit_chunk(view.end(), false, true);
  127. }
  128. void LayoutText::split_into_lines_by_rules(LayoutBlock& container, LayoutMode layout_mode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks)
  129. {
  130. auto& font = style().font();
  131. float space_width = font.glyph_width(' ') + font.glyph_spacing();
  132. auto& line_boxes = container.line_boxes();
  133. if (line_boxes.is_empty())
  134. line_boxes.append(LineBox());
  135. float available_width = container.width() - line_boxes.last().width();
  136. // Collapse whitespace into single spaces
  137. if (do_collapse) {
  138. auto utf8_view = Utf8View(node().data());
  139. StringBuilder builder(node().data().length());
  140. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  141. if (!isspace(*it)) {
  142. builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.codepoint_length_in_bytes());
  143. } else {
  144. builder.append(' ');
  145. auto prev = it;
  146. while (it != utf8_view.end() && isspace(*it)) {
  147. prev = it;
  148. ++it;
  149. }
  150. it = prev;
  151. }
  152. }
  153. m_text_for_rendering = builder.to_string();
  154. } else {
  155. m_text_for_rendering = node().data();
  156. }
  157. // do_wrap_lines => chunks_are_words
  158. // !do_wrap_lines => chunks_are_lines
  159. struct Chunk {
  160. Utf8View view;
  161. int start;
  162. int length;
  163. bool is_break;
  164. };
  165. Vector<Chunk> chunks;
  166. for_each_chunk(
  167. [&](const Utf8View& view, int start, int length, bool is_break) {
  168. chunks.append({ Utf8View(view), start, length, is_break });
  169. },
  170. layout_mode, do_wrap_lines, do_wrap_breaks);
  171. for (size_t i = 0; i < chunks.size(); ++i) {
  172. auto& chunk = chunks[i];
  173. float chunk_width;
  174. bool need_collapse = false;
  175. if (do_wrap_lines) {
  176. bool need_collapse = do_collapse && isspace(*chunk.view.begin());
  177. if (need_collapse)
  178. chunk_width = space_width;
  179. else
  180. chunk_width = font.width(chunk.view) + font.glyph_spacing();
  181. if (line_boxes.last().width() > 0 && chunk_width > available_width) {
  182. line_boxes.append(LineBox());
  183. available_width = container.width();
  184. }
  185. if (need_collapse & line_boxes.last().fragments().is_empty())
  186. continue;
  187. } else {
  188. chunk_width = font.width(chunk.view);
  189. }
  190. line_boxes.last().add_fragment(*this, chunk.start, need_collapse ? 1 : chunk.length, chunk_width, font.glyph_height());
  191. available_width -= chunk_width;
  192. if (do_wrap_lines) {
  193. if (available_width < 0) {
  194. line_boxes.append(LineBox());
  195. available_width = container.width();
  196. }
  197. }
  198. if (do_wrap_breaks) {
  199. if (chunk.is_break) {
  200. line_boxes.append(LineBox());
  201. available_width = container.width();
  202. }
  203. }
  204. }
  205. }
  206. void LayoutText::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  207. {
  208. bool do_collapse = true;
  209. bool do_wrap_lines = true;
  210. bool do_wrap_breaks = false;
  211. auto white_space_prop = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal");
  212. if (white_space_prop == "nowrap") {
  213. do_collapse = true;
  214. do_wrap_lines = false;
  215. do_wrap_breaks = false;
  216. } else if (white_space_prop == "pre") {
  217. do_collapse = false;
  218. do_wrap_lines = false;
  219. do_wrap_breaks = true;
  220. } else if (white_space_prop == "pre-line") {
  221. do_collapse = true;
  222. do_wrap_lines = true;
  223. do_wrap_breaks = true;
  224. } else if (white_space_prop == "pre-wrap") {
  225. do_collapse = false;
  226. do_wrap_lines = true;
  227. do_wrap_breaks = true;
  228. }
  229. split_into_lines_by_rules(container, layout_mode, do_collapse, do_wrap_lines, do_wrap_breaks);
  230. }
  231. }