InlineLevelIterator.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. Gfx::GlyphRun::TextType InlineLevelIterator::resolve_text_direction_from_context()
  142. {
  143. VERIFY(m_text_node_context.has_value());
  144. Optional<Gfx::GlyphRun::TextType> next_known_direction;
  145. for (size_t i = 0;; ++i) {
  146. auto peek = m_text_node_context->chunk_iterator.peek(i);
  147. if (!peek.has_value())
  148. break;
  149. if (peek->text_type == Gfx::GlyphRun::TextType::Ltr || peek->text_type == Gfx::GlyphRun::TextType::Rtl) {
  150. next_known_direction = peek->text_type;
  151. break;
  152. }
  153. }
  154. auto last_known_direction = m_text_node_context->last_known_direction;
  155. if (last_known_direction.has_value() && next_known_direction.has_value() && *last_known_direction != *next_known_direction) {
  156. switch (m_containing_block->computed_values().direction()) {
  157. case CSS::Direction::Ltr:
  158. return Gfx::GlyphRun::TextType::Ltr;
  159. case CSS::Direction::Rtl:
  160. return Gfx::GlyphRun::TextType::Rtl;
  161. }
  162. }
  163. if (last_known_direction.has_value())
  164. return *last_known_direction;
  165. if (next_known_direction.has_value())
  166. return *next_known_direction;
  167. return Gfx::GlyphRun::TextType::ContextDependent;
  168. }
  169. Optional<InlineLevelIterator::Item> InlineLevelIterator::next_without_lookahead()
  170. {
  171. if (!m_current_node)
  172. return {};
  173. if (is<Layout::TextNode>(*m_current_node)) {
  174. auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
  175. if (!m_text_node_context.has_value())
  176. enter_text_node(text_node);
  177. auto chunk_opt = m_text_node_context->chunk_iterator.next();
  178. if (!chunk_opt.has_value()) {
  179. m_text_node_context = {};
  180. skip_to_next();
  181. return next_without_lookahead();
  182. }
  183. if (!m_text_node_context->chunk_iterator.peek(0).has_value())
  184. m_text_node_context->is_last_chunk = true;
  185. auto& chunk = chunk_opt.value();
  186. auto text_type = chunk.text_type;
  187. if (text_type == Gfx::GlyphRun::TextType::Ltr || text_type == Gfx::GlyphRun::TextType::Rtl)
  188. m_text_node_context->last_known_direction = text_type;
  189. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  190. m_text_node_context->is_last_chunk = true;
  191. if (chunk.is_all_whitespace)
  192. text_type = Gfx::GlyphRun::TextType::EndPadding;
  193. }
  194. if (text_type == Gfx::GlyphRun::TextType::ContextDependent)
  195. text_type = resolve_text_direction_from_context();
  196. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  197. return Item {
  198. .type = Item::Type::ForcedBreak,
  199. };
  200. }
  201. auto glyph_run = Gfx::shape_text({ 0, 0 }, chunk.view, chunk.font, text_type);
  202. CSSPixels chunk_width = CSSPixels::nearest_value_for(glyph_run->width());
  203. // NOTE: We never consider `content: ""` to be collapsible whitespace.
  204. bool is_generated_empty_string = text_node.is_generated() && chunk.length == 0;
  205. Item item {
  206. .type = Item::Type::Text,
  207. .node = &text_node,
  208. .glyph_run = move(glyph_run),
  209. .offset_in_node = chunk.start,
  210. .length_in_node = chunk.length,
  211. .width = chunk_width,
  212. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace && !is_generated_empty_string,
  213. };
  214. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  215. return item;
  216. }
  217. if (m_current_node->is_absolutely_positioned()) {
  218. auto& node = *m_current_node;
  219. skip_to_next();
  220. return Item {
  221. .type = Item::Type::AbsolutelyPositionedElement,
  222. .node = &node,
  223. };
  224. }
  225. if (m_current_node->is_floating()) {
  226. auto& node = *m_current_node;
  227. skip_to_next();
  228. return Item {
  229. .type = Item::Type::FloatingElement,
  230. .node = &node,
  231. };
  232. }
  233. if (is<Layout::BreakNode>(*m_current_node)) {
  234. auto& node = *m_current_node;
  235. skip_to_next();
  236. return Item {
  237. .type = Item::Type::ForcedBreak,
  238. .node = &node,
  239. };
  240. }
  241. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  242. skip_to_next();
  243. return next_without_lookahead();
  244. }
  245. if (!is<Layout::Box>(*m_current_node)) {
  246. skip_to_next();
  247. return next_without_lookahead();
  248. }
  249. if (is<Layout::ReplacedBox>(*m_current_node)) {
  250. auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
  251. // FIXME: This const_cast is gross.
  252. const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
  253. }
  254. auto& box = verify_cast<Layout::Box>(*m_current_node);
  255. auto& box_state = m_layout_state.get(box);
  256. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  257. skip_to_next();
  258. auto item = Item {
  259. .type = Item::Type::Element,
  260. .node = &box,
  261. .offset_in_node = 0,
  262. .length_in_node = 0,
  263. .width = box_state.content_width(),
  264. .padding_start = box_state.padding_left,
  265. .padding_end = box_state.padding_right,
  266. .border_start = box_state.border_left,
  267. .border_end = box_state.border_right,
  268. .margin_start = box_state.margin_left,
  269. .margin_end = box_state.margin_right,
  270. };
  271. add_extra_box_model_metrics_to_item(item, true, true);
  272. return item;
  273. }
  274. void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node)
  275. {
  276. bool do_collapse = true;
  277. bool do_wrap_lines = true;
  278. bool do_respect_linebreaks = false;
  279. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  280. do_collapse = true;
  281. do_wrap_lines = false;
  282. do_respect_linebreaks = false;
  283. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  284. do_collapse = false;
  285. do_wrap_lines = false;
  286. do_respect_linebreaks = true;
  287. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  288. do_collapse = true;
  289. do_wrap_lines = true;
  290. do_respect_linebreaks = true;
  291. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  292. do_collapse = false;
  293. do_wrap_lines = true;
  294. do_respect_linebreaks = true;
  295. }
  296. if (text_node.dom_node().is_editable() && !text_node.dom_node().is_uninteresting_whitespace_node())
  297. do_collapse = false;
  298. m_text_node_context = TextNodeContext {
  299. .do_collapse = do_collapse,
  300. .do_wrap_lines = do_wrap_lines,
  301. .do_respect_linebreaks = do_respect_linebreaks,
  302. .is_first_chunk = true,
  303. .is_last_chunk = false,
  304. .chunk_iterator = TextNode::ChunkIterator { text_node.text_for_rendering(), do_wrap_lines, do_respect_linebreaks, text_node.computed_values().font_list() },
  305. };
  306. }
  307. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  308. {
  309. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  310. item.margin_start += m_extra_leading_metrics->margin;
  311. item.border_start += m_extra_leading_metrics->border;
  312. item.padding_start += m_extra_leading_metrics->padding;
  313. m_extra_leading_metrics = {};
  314. }
  315. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  316. item.margin_end += m_extra_trailing_metrics->margin;
  317. item.border_end += m_extra_trailing_metrics->border;
  318. item.padding_end += m_extra_trailing_metrics->padding;
  319. m_extra_trailing_metrics = {};
  320. }
  321. }
  322. }