InlineLevelIterator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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::FormattingState& formatting_state, Layout::BlockContainer const& container, LayoutMode layout_mode)
  14. : m_inline_formatting_context(inline_formatting_context)
  15. , m_formatting_state(formatting_state)
  16. , m_container(container)
  17. , m_next_node(container.first_child())
  18. , m_layout_mode(layout_mode)
  19. {
  20. skip_to_next();
  21. }
  22. void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const& node)
  23. {
  24. if (!m_extra_leading_metrics.has_value())
  25. m_extra_leading_metrics = ExtraBoxMetrics {};
  26. // FIXME: It's really weird that *this* is where we assign box model metrics for these layout nodes..
  27. auto& node_state = m_formatting_state.get_mutable(node);
  28. auto const& container_state = m_formatting_state.get(m_container);
  29. auto const& computed_values = node.computed_values();
  30. node_state.margin_left = computed_values.margin().left.resolved(node, CSS::Length::make_px(container_state.content_width)).to_px(node);
  31. node_state.border_left = computed_values.border_left().width;
  32. node_state.padding_left = computed_values.padding().left.resolved(node, CSS::Length::make_px(container_state.content_width)).to_px(node);
  33. m_extra_leading_metrics->margin += node_state.margin_left;
  34. m_extra_leading_metrics->border += node_state.border_left;
  35. m_extra_leading_metrics->padding += node_state.padding_left;
  36. m_box_model_node_stack.append(node);
  37. }
  38. void InlineLevelIterator::exit_node_with_box_model_metrics()
  39. {
  40. if (!m_extra_trailing_metrics.has_value())
  41. m_extra_trailing_metrics = ExtraBoxMetrics {};
  42. auto& node = m_box_model_node_stack.last();
  43. auto& node_state = m_formatting_state.get_mutable(node);
  44. auto const& container_state = m_formatting_state.get(m_container);
  45. auto const& computed_values = node.computed_values();
  46. node_state.margin_right = computed_values.margin().right.resolved(node, CSS::Length::make_px(container_state.content_width)).to_px(node);
  47. node_state.border_right = computed_values.border_right().width;
  48. node_state.padding_right = computed_values.padding().right.resolved(node, CSS::Length::make_px(container_state.content_width)).to_px(node);
  49. m_extra_trailing_metrics->margin += node_state.margin_right;
  50. m_extra_trailing_metrics->border += node_state.border_right;
  51. m_extra_trailing_metrics->padding += node_state.padding_right;
  52. m_box_model_node_stack.take_last();
  53. }
  54. // This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
  55. Layout::Node const* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within)
  56. {
  57. if (current.first_child() && current.first_child()->is_inline() && !current.is_inline_block()) {
  58. if (!current.is_box() || !static_cast<Box const&>(current).is_out_of_flow(m_inline_formatting_context))
  59. return current.first_child();
  60. }
  61. Layout::Node const* node = &current;
  62. Layout::Node const* next = nullptr;
  63. while (!(next = node->next_sibling())) {
  64. node = node->parent();
  65. // If node is the last node on the "box model node stack", pop it off.
  66. if (!m_box_model_node_stack.is_empty()
  67. && &m_box_model_node_stack.last() == node) {
  68. exit_node_with_box_model_metrics();
  69. }
  70. if (!node || node == stay_within)
  71. return nullptr;
  72. }
  73. // If node is the last node on the "box model node stack", pop it off.
  74. if (!m_box_model_node_stack.is_empty()
  75. && &m_box_model_node_stack.last() == node) {
  76. exit_node_with_box_model_metrics();
  77. }
  78. return next;
  79. }
  80. void InlineLevelIterator::compute_next()
  81. {
  82. if (m_next_node == nullptr)
  83. return;
  84. do {
  85. m_next_node = next_inline_node_in_pre_order(*m_next_node, &m_container);
  86. } while (m_next_node && (!m_next_node->is_inline() && !m_next_node->is_out_of_flow(m_inline_formatting_context)));
  87. }
  88. void InlineLevelIterator::skip_to_next()
  89. {
  90. if (m_next_node && is<Layout::NodeWithStyleAndBoxModelMetrics>(*m_next_node) && !m_next_node->is_inline_block() && !m_next_node->is_out_of_flow(m_inline_formatting_context))
  91. enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*m_next_node));
  92. m_current_node = m_next_node;
  93. compute_next();
  94. }
  95. Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_width)
  96. {
  97. if (!m_current_node)
  98. return {};
  99. if (is<Layout::TextNode>(*m_current_node)) {
  100. auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
  101. if (!m_text_node_context.has_value()) {
  102. auto& line_boxes = m_formatting_state.get(m_container).line_boxes;
  103. bool previous_is_empty_or_ends_in_whitespace = line_boxes.is_empty() || line_boxes.last().is_empty_or_ends_in_whitespace();
  104. enter_text_node(text_node, previous_is_empty_or_ends_in_whitespace);
  105. }
  106. auto chunk_opt = m_text_node_context->next_chunk;
  107. if (!chunk_opt.has_value()) {
  108. m_text_node_context = {};
  109. skip_to_next();
  110. return next(available_width);
  111. }
  112. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  113. if (!m_text_node_context->next_chunk.has_value())
  114. m_text_node_context->is_last_chunk = true;
  115. auto& chunk = chunk_opt.value();
  116. float chunk_width = text_node.font().width(chunk.view) + text_node.font().glyph_spacing();
  117. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  118. return Item {
  119. .type = Item::Type::ForcedBreak,
  120. };
  121. }
  122. Item item {
  123. .type = Item::Type::Text,
  124. .node = &text_node,
  125. .offset_in_node = chunk.start,
  126. .length_in_node = chunk.length,
  127. .width = chunk_width,
  128. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace,
  129. };
  130. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  131. return item;
  132. }
  133. if (m_current_node->is_absolutely_positioned()) {
  134. auto& node = *m_current_node;
  135. skip_to_next();
  136. return Item {
  137. .type = Item::Type::AbsolutelyPositionedElement,
  138. .node = &node,
  139. };
  140. }
  141. if (m_current_node->is_floating()) {
  142. auto& node = *m_current_node;
  143. skip_to_next();
  144. return Item {
  145. .type = Item::Type::FloatingElement,
  146. .node = &node,
  147. };
  148. }
  149. if (is<Layout::BreakNode>(*m_current_node)) {
  150. skip_to_next();
  151. return Item {
  152. .type = Item::Type::ForcedBreak,
  153. };
  154. }
  155. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  156. skip_to_next();
  157. return next(available_width);
  158. }
  159. if (!is<Layout::Box>(*m_current_node)) {
  160. skip_to_next();
  161. return next(available_width);
  162. }
  163. if (is<Layout::ReplacedBox>(*m_current_node)) {
  164. auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
  165. // FIXME: This const_cast is gross.
  166. const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
  167. }
  168. auto& box = verify_cast<Layout::Box>(*m_current_node);
  169. auto& box_state = m_formatting_state.get(box);
  170. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  171. skip_to_next();
  172. auto item = Item {
  173. .type = Item::Type::Element,
  174. .node = &box,
  175. .offset_in_node = 0,
  176. .length_in_node = 0,
  177. .width = box_state.content_width,
  178. .padding_start = box_state.padding_left,
  179. .padding_end = box_state.padding_right,
  180. .border_start = box_state.border_left,
  181. .border_end = box_state.border_right,
  182. .margin_start = box_state.margin_left,
  183. .margin_end = box_state.margin_right,
  184. };
  185. add_extra_box_model_metrics_to_item(item, true, true);
  186. return item;
  187. }
  188. void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node, bool previous_is_empty_or_ends_in_whitespace)
  189. {
  190. bool do_collapse = true;
  191. bool do_wrap_lines = true;
  192. bool do_respect_linebreaks = false;
  193. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  194. do_collapse = true;
  195. do_wrap_lines = false;
  196. do_respect_linebreaks = false;
  197. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  198. do_collapse = false;
  199. do_wrap_lines = false;
  200. do_respect_linebreaks = true;
  201. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  202. do_collapse = true;
  203. do_wrap_lines = true;
  204. do_respect_linebreaks = true;
  205. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  206. do_collapse = false;
  207. do_wrap_lines = true;
  208. do_respect_linebreaks = true;
  209. }
  210. // FIXME: The const_cast here is gross.
  211. const_cast<TextNode&>(text_node).compute_text_for_rendering(do_collapse, previous_is_empty_or_ends_in_whitespace);
  212. m_text_node_context = TextNodeContext {
  213. .do_collapse = do_collapse,
  214. .do_wrap_lines = do_wrap_lines,
  215. .do_respect_linebreaks = do_respect_linebreaks,
  216. .is_first_chunk = true,
  217. .is_last_chunk = false,
  218. .chunk_iterator = TextNode::ChunkIterator { text_node.text_for_rendering(), m_layout_mode, do_wrap_lines, do_respect_linebreaks },
  219. };
  220. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  221. }
  222. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  223. {
  224. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  225. item.margin_start += m_extra_leading_metrics->margin;
  226. item.border_start += m_extra_leading_metrics->border;
  227. item.padding_start += m_extra_leading_metrics->padding;
  228. m_extra_leading_metrics = {};
  229. }
  230. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  231. item.margin_end += m_extra_trailing_metrics->margin;
  232. item.border_end += m_extra_trailing_metrics->border;
  233. item.padding_end += m_extra_trailing_metrics->padding;
  234. m_extra_trailing_metrics = {};
  235. }
  236. }
  237. }