InlineLevelIterator.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/BreakNode.h>
  7. #include <LibWeb/Layout/InlineFormattingContext.h>
  8. #include <LibWeb/Layout/InlineLevelIterator.h>
  9. #include <LibWeb/Layout/InlineNode.h>
  10. #include <LibWeb/Layout/ListItemMarkerBox.h>
  11. #include <LibWeb/Layout/ReplacedBox.h>
  12. namespace Web::Layout {
  13. InlineLevelIterator::InlineLevelIterator(Layout::InlineFormattingContext& inline_formatting_context, Layout::BlockContainer& container, LayoutMode layout_mode)
  14. : m_inline_formatting_context(inline_formatting_context)
  15. , m_container(container)
  16. , m_next_node(container.first_child())
  17. , m_layout_mode(layout_mode)
  18. {
  19. skip_to_next();
  20. }
  21. void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics& node)
  22. {
  23. if (!m_extra_leading_metrics.has_value())
  24. m_extra_leading_metrics = ExtraBoxMetrics {};
  25. node.box_model().margin.left = node.computed_values().margin().left.resolved(node, CSS::Length::make_px(m_container.content_width())).to_px(node);
  26. node.box_model().border.left = node.computed_values().border_left().width;
  27. node.box_model().padding.left = node.computed_values().padding().left.resolved(node, CSS::Length::make_px(m_container.content_width())).to_px(node);
  28. m_extra_leading_metrics->margin += node.box_model().margin.left;
  29. m_extra_leading_metrics->border += node.box_model().border.left;
  30. m_extra_leading_metrics->padding += node.box_model().padding.left;
  31. m_box_model_node_stack.append(node);
  32. }
  33. void InlineLevelIterator::exit_node_with_box_model_metrics()
  34. {
  35. if (!m_extra_trailing_metrics.has_value())
  36. m_extra_trailing_metrics = ExtraBoxMetrics {};
  37. auto& node = m_box_model_node_stack.last();
  38. node.box_model().margin.right = node.computed_values().margin().right.resolved(node, CSS::Length::make_px(m_container.content_width())).to_px(node);
  39. node.box_model().border.right = node.computed_values().border_right().width;
  40. node.box_model().padding.right = node.computed_values().padding().right.resolved(node, CSS::Length::make_px(m_container.content_width())).to_px(node);
  41. m_extra_trailing_metrics->margin += node.box_model().margin.right;
  42. m_extra_trailing_metrics->border += node.box_model().border.right;
  43. m_extra_trailing_metrics->padding += node.box_model().padding.right;
  44. m_box_model_node_stack.take_last();
  45. }
  46. // This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
  47. Layout::Node* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node& current, Layout::Node const* stay_within)
  48. {
  49. if (current.first_child() && current.first_child()->is_inline() && !current.is_inline_block())
  50. return current.first_child();
  51. Layout::Node* node = &current;
  52. Layout::Node* next = nullptr;
  53. while (!(next = node->next_sibling())) {
  54. node = node->parent();
  55. // If node is the last node on the "box model node stack", pop it off.
  56. if (!m_box_model_node_stack.is_empty()
  57. && &m_box_model_node_stack.last() == node) {
  58. exit_node_with_box_model_metrics();
  59. }
  60. if (!node || node == stay_within)
  61. return nullptr;
  62. }
  63. return next;
  64. }
  65. void InlineLevelIterator::compute_next()
  66. {
  67. if (m_next_node == nullptr)
  68. return;
  69. do {
  70. m_next_node = next_inline_node_in_pre_order(*m_next_node, &m_container);
  71. } while (m_next_node && !m_next_node->is_inline());
  72. }
  73. void InlineLevelIterator::skip_to_next()
  74. {
  75. if (m_next_node && is<Layout::NodeWithStyleAndBoxModelMetrics>(*m_next_node))
  76. enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics&>(*m_next_node));
  77. m_current_node = m_next_node;
  78. compute_next();
  79. }
  80. Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_width)
  81. {
  82. if (!m_current_node)
  83. return {};
  84. if (is<Layout::TextNode>(*m_current_node)) {
  85. auto& text_node = static_cast<Layout::TextNode&>(*m_current_node);
  86. if (!m_text_node_context.has_value()) {
  87. bool previous_is_empty_or_ends_in_whitespace = m_container.line_boxes().is_empty() || m_container.line_boxes().last().is_empty_or_ends_in_whitespace();
  88. enter_text_node(text_node, previous_is_empty_or_ends_in_whitespace);
  89. }
  90. auto chunk_opt = m_text_node_context->next_chunk;
  91. if (!chunk_opt.has_value()) {
  92. m_text_node_context = {};
  93. skip_to_next();
  94. return next(available_width);
  95. }
  96. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  97. if (!m_text_node_context->next_chunk.has_value())
  98. m_text_node_context->is_last_chunk = true;
  99. auto& chunk = chunk_opt.value();
  100. float chunk_width = text_node.font().width(chunk.view) + text_node.font().glyph_spacing();
  101. Item item {
  102. .type = Item::Type::Text,
  103. .node = &text_node,
  104. .offset_in_node = chunk.start,
  105. .length_in_node = chunk.length,
  106. .width = chunk_width,
  107. .should_force_break = m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline,
  108. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace,
  109. };
  110. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  111. return item;
  112. }
  113. if (m_current_node->is_positioned()) {
  114. skip_to_next();
  115. return next(available_width);
  116. }
  117. if (is<Layout::BreakNode>(*m_current_node)) {
  118. skip_to_next();
  119. return Item {
  120. .type = Item::Type::ForcedBreak,
  121. };
  122. }
  123. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  124. skip_to_next();
  125. return next(available_width);
  126. }
  127. if (!is<Layout::Box>(*m_current_node)) {
  128. skip_to_next();
  129. return next(available_width);
  130. }
  131. if (is<Layout::ReplacedBox>(*m_current_node)) {
  132. auto& replaced_box = static_cast<Layout::ReplacedBox&>(*m_current_node);
  133. replaced_box.prepare_for_replaced_layout();
  134. }
  135. auto& box = verify_cast<Layout::Box>(*m_current_node);
  136. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  137. skip_to_next();
  138. auto item = Item {
  139. .type = Item::Type::Element,
  140. .node = &box,
  141. .offset_in_node = 0,
  142. .length_in_node = 0,
  143. .width = box.content_width(),
  144. .padding_start = box.box_model().padding.left,
  145. .padding_end = box.box_model().padding.right,
  146. .border_start = box.box_model().border.left,
  147. .border_end = box.box_model().border.right,
  148. .margin_start = box.box_model().margin.left,
  149. .margin_end = box.box_model().margin.right,
  150. };
  151. add_extra_box_model_metrics_to_item(item, true, true);
  152. return item;
  153. }
  154. void InlineLevelIterator::enter_text_node(Layout::TextNode& text_node, bool previous_is_empty_or_ends_in_whitespace)
  155. {
  156. bool do_collapse = true;
  157. bool do_wrap_lines = true;
  158. bool do_respect_linebreaks = false;
  159. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  160. do_collapse = true;
  161. do_wrap_lines = false;
  162. do_respect_linebreaks = false;
  163. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  164. do_collapse = false;
  165. do_wrap_lines = false;
  166. do_respect_linebreaks = true;
  167. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  168. do_collapse = true;
  169. do_wrap_lines = true;
  170. do_respect_linebreaks = true;
  171. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  172. do_collapse = false;
  173. do_wrap_lines = true;
  174. do_respect_linebreaks = true;
  175. }
  176. text_node.compute_text_for_rendering(do_collapse, previous_is_empty_or_ends_in_whitespace);
  177. m_text_node_context = TextNodeContext {
  178. .do_collapse = do_collapse,
  179. .do_wrap_lines = do_wrap_lines,
  180. .do_respect_linebreaks = do_respect_linebreaks,
  181. .is_first_chunk = true,
  182. .is_last_chunk = false,
  183. .chunk_iterator = TextNode::ChunkIterator { text_node.text_for_rendering(), m_layout_mode, do_wrap_lines, do_respect_linebreaks },
  184. };
  185. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  186. }
  187. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  188. {
  189. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  190. item.margin_start += m_extra_leading_metrics->margin;
  191. item.border_start += m_extra_leading_metrics->border;
  192. item.padding_start += m_extra_leading_metrics->padding;
  193. m_extra_leading_metrics = {};
  194. }
  195. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  196. item.margin_end += m_extra_trailing_metrics->margin;
  197. item.border_end += m_extra_trailing_metrics->border;
  198. item.padding_end += m_extra_trailing_metrics->padding;
  199. m_extra_trailing_metrics = {};
  200. }
  201. }
  202. }