InlineLevelIterator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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::LayoutState& layout_state, Layout::BlockContainer const& containing_block, LayoutState::UsedValues const& containing_block_used_values, LayoutMode layout_mode)
  14. : m_inline_formatting_context(inline_formatting_context)
  15. , m_layout_state(layout_state)
  16. , m_containing_block(containing_block)
  17. , m_containing_block_used_values(containing_block_used_values)
  18. , m_next_node(containing_block.first_child())
  19. , m_layout_mode(layout_mode)
  20. {
  21. skip_to_next();
  22. }
  23. void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const& node)
  24. {
  25. if (!m_extra_leading_metrics.has_value())
  26. m_extra_leading_metrics = ExtraBoxMetrics {};
  27. // FIXME: It's really weird that *this* is where we assign box model metrics for these layout nodes..
  28. auto& used_values = m_layout_state.get_mutable(node);
  29. auto const& computed_values = node.computed_values();
  30. used_values.margin_left = computed_values.margin().left().to_px(node, m_containing_block_used_values.content_width());
  31. used_values.border_left = computed_values.border_left().width;
  32. used_values.padding_left = computed_values.padding().left().to_px(node, m_containing_block_used_values.content_width());
  33. used_values.padding_bottom = computed_values.padding().bottom().to_px(node, m_containing_block_used_values.content_width());
  34. used_values.padding_top = computed_values.padding().top().to_px(node, m_containing_block_used_values.content_width());
  35. m_extra_leading_metrics->margin += used_values.margin_left;
  36. m_extra_leading_metrics->border += used_values.border_left;
  37. m_extra_leading_metrics->padding += used_values.padding_left;
  38. // Now's our chance to resolve the inset properties for this node.
  39. m_inline_formatting_context.compute_inset(node);
  40. m_box_model_node_stack.append(node);
  41. }
  42. void InlineLevelIterator::exit_node_with_box_model_metrics()
  43. {
  44. if (!m_extra_trailing_metrics.has_value())
  45. m_extra_trailing_metrics = ExtraBoxMetrics {};
  46. auto& node = m_box_model_node_stack.last();
  47. auto& used_values = m_layout_state.get_mutable(node);
  48. auto const& computed_values = node->computed_values();
  49. used_values.margin_right = computed_values.margin().right().to_px(node, m_containing_block_used_values.content_width());
  50. used_values.border_right = computed_values.border_right().width;
  51. used_values.padding_right = computed_values.padding().right().to_px(node, m_containing_block_used_values.content_width());
  52. m_extra_trailing_metrics->margin += used_values.margin_right;
  53. m_extra_trailing_metrics->border += used_values.border_right;
  54. m_extra_trailing_metrics->padding += used_values.padding_right;
  55. m_box_model_node_stack.take_last();
  56. }
  57. // This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
  58. Layout::Node const* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within)
  59. {
  60. if (current.first_child()
  61. && current.first_child()->display().is_inline_outside()
  62. && current.display().is_flow_inside()
  63. && !current.is_replaced_box()) {
  64. if (!current.is_box() || !static_cast<Box const&>(current).is_out_of_flow(m_inline_formatting_context))
  65. return current.first_child();
  66. }
  67. Layout::Node const* node = &current;
  68. Layout::Node const* next = nullptr;
  69. while (!(next = node->next_sibling())) {
  70. node = node->parent();
  71. // If node is the last node on the "box model node stack", pop it off.
  72. if (!m_box_model_node_stack.is_empty()
  73. && m_box_model_node_stack.last() == node) {
  74. exit_node_with_box_model_metrics();
  75. }
  76. if (!node || node == stay_within)
  77. return nullptr;
  78. }
  79. // If node is the last node on the "box model node stack", pop it off.
  80. if (!m_box_model_node_stack.is_empty()
  81. && m_box_model_node_stack.last() == node) {
  82. exit_node_with_box_model_metrics();
  83. }
  84. return next;
  85. }
  86. void InlineLevelIterator::compute_next()
  87. {
  88. if (m_next_node == nullptr)
  89. return;
  90. do {
  91. m_next_node = next_inline_node_in_pre_order(*m_next_node, m_containing_block);
  92. if (m_next_node && m_next_node->is_svg_mask_box()) {
  93. // NOTE: It is possible to encounter SVGMaskBox nodes while doing layout of formatting context established by <foreignObject> with a mask.
  94. // We should skip and let SVGFormattingContext take care of them.
  95. m_next_node = m_next_node->next_sibling();
  96. }
  97. } while (m_next_node && (!m_next_node->is_inline() && !m_next_node->is_out_of_flow(m_inline_formatting_context)));
  98. }
  99. void InlineLevelIterator::skip_to_next()
  100. {
  101. if (m_next_node
  102. && is<Layout::NodeWithStyleAndBoxModelMetrics>(*m_next_node)
  103. && m_next_node->display().is_flow_inside()
  104. && !m_next_node->is_out_of_flow(m_inline_formatting_context)
  105. && !m_next_node->is_replaced_box())
  106. enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*m_next_node));
  107. m_current_node = m_next_node;
  108. compute_next();
  109. }
  110. Optional<InlineLevelIterator::Item> InlineLevelIterator::next()
  111. {
  112. if (m_lookahead_items.is_empty())
  113. return next_without_lookahead();
  114. return m_lookahead_items.dequeue();
  115. }
  116. CSSPixels InlineLevelIterator::next_non_whitespace_sequence_width()
  117. {
  118. CSSPixels next_width = 0;
  119. for (;;) {
  120. auto next_item_opt = next_without_lookahead();
  121. if (!next_item_opt.has_value())
  122. break;
  123. m_lookahead_items.enqueue(next_item_opt.release_value());
  124. auto& next_item = m_lookahead_items.tail();
  125. if (next_item.type == InlineLevelIterator::Item::Type::ForcedBreak)
  126. break;
  127. if (next_item.node->computed_values().white_space() != CSS::WhiteSpace::Nowrap) {
  128. if (next_item.type != InlineLevelIterator::Item::Type::Text)
  129. break;
  130. if (next_item.is_collapsible_whitespace)
  131. break;
  132. auto& next_text_node = verify_cast<Layout::TextNode>(*(next_item.node));
  133. auto next_view = next_text_node.text_for_rendering().bytes_as_string_view().substring_view(next_item.offset_in_node, next_item.length_in_node);
  134. if (next_view.is_whitespace())
  135. break;
  136. }
  137. next_width += next_item.border_box_width();
  138. }
  139. return next_width;
  140. }
  141. Optional<InlineLevelIterator::Item> InlineLevelIterator::next_without_lookahead()
  142. {
  143. if (!m_current_node)
  144. return {};
  145. if (is<Layout::TextNode>(*m_current_node)) {
  146. auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
  147. if (!m_text_node_context.has_value())
  148. enter_text_node(text_node);
  149. auto chunk_opt = m_text_node_context->next_chunk;
  150. if (!chunk_opt.has_value()) {
  151. m_text_node_context = {};
  152. skip_to_next();
  153. return next_without_lookahead();
  154. }
  155. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  156. if (!m_text_node_context->next_chunk.has_value())
  157. m_text_node_context->is_last_chunk = true;
  158. auto& chunk = chunk_opt.value();
  159. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  160. return Item {
  161. .type = Item::Type::ForcedBreak,
  162. };
  163. }
  164. Vector<Gfx::DrawGlyphOrEmoji> glyph_run;
  165. float glyph_run_width = 0;
  166. Gfx::for_each_glyph_position(
  167. { 0, 0 }, chunk.view, chunk.font, [&](Gfx::DrawGlyphOrEmoji const& glyph_or_emoji) {
  168. glyph_run.append(glyph_or_emoji);
  169. },
  170. glyph_run_width);
  171. CSSPixels chunk_width = CSSPixels::nearest_value_for(glyph_run_width);
  172. // NOTE: We never consider `content: ""` to be collapsible whitespace.
  173. bool is_generated_empty_string = text_node.is_generated() && chunk.length == 0;
  174. Item item {
  175. .type = Item::Type::Text,
  176. .node = &text_node,
  177. .glyph_run = adopt_ref(*new Gfx::GlyphRun(move(glyph_run), chunk.font)),
  178. .offset_in_node = chunk.start,
  179. .length_in_node = chunk.length,
  180. .width = chunk_width,
  181. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace && !is_generated_empty_string,
  182. };
  183. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  184. return item;
  185. }
  186. if (m_current_node->is_absolutely_positioned()) {
  187. auto& node = *m_current_node;
  188. skip_to_next();
  189. return Item {
  190. .type = Item::Type::AbsolutelyPositionedElement,
  191. .node = &node,
  192. };
  193. }
  194. if (m_current_node->is_floating()) {
  195. auto& node = *m_current_node;
  196. skip_to_next();
  197. return Item {
  198. .type = Item::Type::FloatingElement,
  199. .node = &node,
  200. };
  201. }
  202. if (is<Layout::BreakNode>(*m_current_node)) {
  203. auto& node = *m_current_node;
  204. skip_to_next();
  205. return Item {
  206. .type = Item::Type::ForcedBreak,
  207. .node = &node,
  208. };
  209. }
  210. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  211. skip_to_next();
  212. return next_without_lookahead();
  213. }
  214. if (!is<Layout::Box>(*m_current_node)) {
  215. skip_to_next();
  216. return next_without_lookahead();
  217. }
  218. if (is<Layout::ReplacedBox>(*m_current_node)) {
  219. auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
  220. // FIXME: This const_cast is gross.
  221. const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
  222. }
  223. auto& box = verify_cast<Layout::Box>(*m_current_node);
  224. auto& box_state = m_layout_state.get(box);
  225. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  226. skip_to_next();
  227. auto item = Item {
  228. .type = Item::Type::Element,
  229. .node = &box,
  230. .offset_in_node = 0,
  231. .length_in_node = 0,
  232. .width = box_state.content_width(),
  233. .padding_start = box_state.padding_left,
  234. .padding_end = box_state.padding_right,
  235. .border_start = box_state.border_left,
  236. .border_end = box_state.border_right,
  237. .margin_start = box_state.margin_left,
  238. .margin_end = box_state.margin_right,
  239. };
  240. add_extra_box_model_metrics_to_item(item, true, true);
  241. return item;
  242. }
  243. void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node)
  244. {
  245. bool do_collapse = true;
  246. bool do_wrap_lines = true;
  247. bool do_respect_linebreaks = false;
  248. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  249. do_collapse = true;
  250. do_wrap_lines = false;
  251. do_respect_linebreaks = false;
  252. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  253. do_collapse = false;
  254. do_wrap_lines = false;
  255. do_respect_linebreaks = true;
  256. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  257. do_collapse = true;
  258. do_wrap_lines = true;
  259. do_respect_linebreaks = true;
  260. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  261. do_collapse = false;
  262. do_wrap_lines = true;
  263. do_respect_linebreaks = true;
  264. }
  265. if (text_node.dom_node().is_editable() && !text_node.dom_node().is_uninteresting_whitespace_node())
  266. do_collapse = false;
  267. m_text_node_context = TextNodeContext {
  268. .do_collapse = do_collapse,
  269. .do_wrap_lines = do_wrap_lines,
  270. .do_respect_linebreaks = do_respect_linebreaks,
  271. .is_first_chunk = true,
  272. .is_last_chunk = false,
  273. .chunk_iterator = TextNode::ChunkIterator { text_node.text_for_rendering(), do_wrap_lines, do_respect_linebreaks, text_node.computed_values().font_list() },
  274. };
  275. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  276. }
  277. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  278. {
  279. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  280. item.margin_start += m_extra_leading_metrics->margin;
  281. item.border_start += m_extra_leading_metrics->border;
  282. item.padding_start += m_extra_leading_metrics->padding;
  283. m_extra_leading_metrics = {};
  284. }
  285. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  286. item.margin_end += m_extra_trailing_metrics->margin;
  287. item.border_end += m_extra_trailing_metrics->border;
  288. item.padding_end += m_extra_trailing_metrics->padding;
  289. m_extra_trailing_metrics = {};
  290. }
  291. }
  292. }