InlineLevelIterator.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.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.border_top = computed_values.border_top().width;
  34. used_values.border_bottom = computed_values.border_bottom().width;
  35. used_values.padding_bottom = computed_values.padding().bottom().to_px(node, m_containing_block_used_values.content_width());
  36. used_values.padding_top = computed_values.padding().top().to_px(node, m_containing_block_used_values.content_width());
  37. m_extra_leading_metrics->margin += used_values.margin_left;
  38. m_extra_leading_metrics->border += used_values.border_left;
  39. m_extra_leading_metrics->padding += used_values.padding_left;
  40. // Now's our chance to resolve the inset properties for this node.
  41. m_inline_formatting_context.compute_inset(node, m_inline_formatting_context.content_box_rect(m_containing_block_used_values).size());
  42. m_box_model_node_stack.append(node);
  43. }
  44. void InlineLevelIterator::exit_node_with_box_model_metrics()
  45. {
  46. if (!m_extra_trailing_metrics.has_value())
  47. m_extra_trailing_metrics = ExtraBoxMetrics {};
  48. auto& node = m_box_model_node_stack.last();
  49. auto& used_values = m_layout_state.get_mutable(node);
  50. auto const& computed_values = node->computed_values();
  51. used_values.margin_right = computed_values.margin().right().to_px(node, m_containing_block_used_values.content_width());
  52. used_values.border_right = computed_values.border_right().width;
  53. used_values.padding_right = computed_values.padding().right().to_px(node, m_containing_block_used_values.content_width());
  54. m_extra_trailing_metrics->margin += used_values.margin_right;
  55. m_extra_trailing_metrics->border += used_values.border_right;
  56. m_extra_trailing_metrics->padding += used_values.padding_right;
  57. m_box_model_node_stack.take_last();
  58. }
  59. // This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
  60. Layout::Node const* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within)
  61. {
  62. if (current.first_child()
  63. && current.first_child()->display().is_inline_outside()
  64. && current.display().is_flow_inside()
  65. && !current.is_replaced_box()) {
  66. if (!current.is_box() || !static_cast<Box const&>(current).is_out_of_flow(m_inline_formatting_context))
  67. return current.first_child();
  68. }
  69. Layout::Node const* node = &current;
  70. Layout::Node const* next = nullptr;
  71. while (!(next = node->next_sibling())) {
  72. node = node->parent();
  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. if (!node || node == stay_within)
  79. return nullptr;
  80. }
  81. // If node is the last node on the "box model node stack", pop it off.
  82. if (!m_box_model_node_stack.is_empty()
  83. && m_box_model_node_stack.last() == node) {
  84. exit_node_with_box_model_metrics();
  85. }
  86. return next;
  87. }
  88. void InlineLevelIterator::compute_next()
  89. {
  90. if (m_next_node == nullptr)
  91. return;
  92. do {
  93. m_next_node = next_inline_node_in_pre_order(*m_next_node, m_containing_block);
  94. if (m_next_node && m_next_node->is_svg_mask_box()) {
  95. // NOTE: It is possible to encounter SVGMaskBox nodes while doing layout of formatting context established by <foreignObject> with a mask.
  96. // We should skip and let SVGFormattingContext take care of them.
  97. m_next_node = m_next_node->next_sibling();
  98. }
  99. } while (m_next_node && (!m_next_node->is_inline() && !m_next_node->is_out_of_flow(m_inline_formatting_context)));
  100. }
  101. void InlineLevelIterator::skip_to_next()
  102. {
  103. if (m_next_node
  104. && is<Layout::NodeWithStyleAndBoxModelMetrics>(*m_next_node)
  105. && m_next_node->display().is_flow_inside()
  106. && !m_next_node->is_out_of_flow(m_inline_formatting_context)
  107. && !m_next_node->is_replaced_box())
  108. enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*m_next_node));
  109. m_current_node = m_next_node;
  110. compute_next();
  111. }
  112. Optional<InlineLevelIterator::Item> InlineLevelIterator::next()
  113. {
  114. if (m_lookahead_items.is_empty())
  115. return next_without_lookahead();
  116. return m_lookahead_items.dequeue();
  117. }
  118. CSSPixels InlineLevelIterator::next_non_whitespace_sequence_width()
  119. {
  120. CSSPixels next_width = 0;
  121. for (;;) {
  122. auto next_item_opt = next_without_lookahead();
  123. if (!next_item_opt.has_value())
  124. break;
  125. m_lookahead_items.enqueue(next_item_opt.release_value());
  126. auto& next_item = m_lookahead_items.tail();
  127. if (next_item.type == InlineLevelIterator::Item::Type::ForcedBreak)
  128. break;
  129. if (next_item.node->computed_values().white_space() != CSS::WhiteSpace::Nowrap) {
  130. if (next_item.type != InlineLevelIterator::Item::Type::Text)
  131. break;
  132. if (next_item.is_collapsible_whitespace)
  133. break;
  134. auto& next_text_node = verify_cast<Layout::TextNode>(*(next_item.node));
  135. 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);
  136. if (next_view.is_whitespace())
  137. break;
  138. }
  139. next_width += next_item.border_box_width();
  140. }
  141. return next_width;
  142. }
  143. Gfx::GlyphRun::TextType InlineLevelIterator::resolve_text_direction_from_context()
  144. {
  145. VERIFY(m_text_node_context.has_value());
  146. Optional<Gfx::GlyphRun::TextType> next_known_direction;
  147. for (size_t i = 0;; ++i) {
  148. auto peek = m_text_node_context->chunk_iterator.peek(i);
  149. if (!peek.has_value())
  150. break;
  151. if (peek->text_type == Gfx::GlyphRun::TextType::Ltr || peek->text_type == Gfx::GlyphRun::TextType::Rtl) {
  152. next_known_direction = peek->text_type;
  153. break;
  154. }
  155. }
  156. auto last_known_direction = m_text_node_context->last_known_direction;
  157. if (last_known_direction.has_value() && next_known_direction.has_value() && *last_known_direction != *next_known_direction) {
  158. switch (m_containing_block->computed_values().direction()) {
  159. case CSS::Direction::Ltr:
  160. return Gfx::GlyphRun::TextType::Ltr;
  161. case CSS::Direction::Rtl:
  162. return Gfx::GlyphRun::TextType::Rtl;
  163. }
  164. }
  165. if (last_known_direction.has_value())
  166. return *last_known_direction;
  167. if (next_known_direction.has_value())
  168. return *next_known_direction;
  169. return Gfx::GlyphRun::TextType::ContextDependent;
  170. }
  171. Optional<InlineLevelIterator::Item> InlineLevelIterator::next_without_lookahead()
  172. {
  173. if (!m_current_node)
  174. return {};
  175. if (is<Layout::TextNode>(*m_current_node)) {
  176. auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
  177. if (!m_text_node_context.has_value())
  178. enter_text_node(text_node);
  179. auto chunk_opt = m_text_node_context->chunk_iterator.next();
  180. if (!chunk_opt.has_value()) {
  181. m_text_node_context = {};
  182. skip_to_next();
  183. return next_without_lookahead();
  184. }
  185. if (!m_text_node_context->chunk_iterator.peek(0).has_value())
  186. m_text_node_context->is_last_chunk = true;
  187. auto& chunk = chunk_opt.value();
  188. auto text_type = chunk.text_type;
  189. if (text_type == Gfx::GlyphRun::TextType::Ltr || text_type == Gfx::GlyphRun::TextType::Rtl)
  190. m_text_node_context->last_known_direction = text_type;
  191. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  192. m_text_node_context->is_last_chunk = true;
  193. if (chunk.is_all_whitespace)
  194. text_type = Gfx::GlyphRun::TextType::EndPadding;
  195. }
  196. if (text_type == Gfx::GlyphRun::TextType::ContextDependent)
  197. text_type = resolve_text_direction_from_context();
  198. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  199. return Item {
  200. .type = Item::Type::ForcedBreak,
  201. };
  202. }
  203. auto resolution_context = CSS::Length::ResolutionContext::for_layout_node(text_node);
  204. auto letter_spacing = text_node.computed_values().letter_spacing().resolved(resolution_context).to_px(text_node);
  205. auto word_spacing = text_node.computed_values().word_spacing().resolved(resolution_context).to_px(text_node);
  206. auto x = 0.0f;
  207. if (chunk.has_breaking_tab) {
  208. CSSPixels accumulated_width;
  209. // make sure to account for any fragments that take up a portion of the measured tab stop distance
  210. auto fragments = m_containing_block_used_values.line_boxes.last().fragments();
  211. for (auto const& frag : fragments) {
  212. accumulated_width += frag.width();
  213. }
  214. // https://drafts.csswg.org/css-text/#tab-size-property
  215. auto tab_size = text_node.computed_values().tab_size();
  216. auto resolution_context = CSS::Length::ResolutionContext::for_layout_node(text_node);
  217. CSSPixels tab_width;
  218. tab_width = tab_size.visit(
  219. [&](CSS::LengthOrCalculated const& t) -> CSSPixels {
  220. auto value = t.resolved(resolution_context);
  221. return value.to_px(text_node);
  222. },
  223. [&](CSS::NumberOrCalculated const& n) -> CSSPixels {
  224. auto tab_number = n.resolved(text_node);
  225. return CSSPixels::nearest_value_for(tab_number * (chunk.font->glyph_width(' ') + word_spacing.to_float() + letter_spacing.to_float()));
  226. });
  227. // https://drafts.csswg.org/css-text/#white-space-phase-2
  228. // if fragments have added to the width, calculate the net distance to the next tab stop, otherwise the shift will just be the tab width
  229. auto tab_stop_dist = accumulated_width > 0 ? (ceil((accumulated_width / tab_width)) * tab_width) - accumulated_width : tab_width;
  230. auto ch_width = chunk.font->glyph_width('0');
  231. // If this distance is less than 0.5ch, then the subsequent tab stop is used instead
  232. if (tab_stop_dist < ch_width * 0.5)
  233. tab_stop_dist += tab_width;
  234. // account for consecutive tabs
  235. auto num_of_tabs = 0;
  236. for (auto code_point : chunk.view) {
  237. if (code_point != '\t')
  238. break;
  239. num_of_tabs++;
  240. }
  241. tab_stop_dist = tab_stop_dist * num_of_tabs;
  242. // remove tabs, we don't want to render them when we shape the text
  243. chunk.view = chunk.view.substring_view(num_of_tabs);
  244. x = tab_stop_dist.to_float();
  245. }
  246. auto glyph_run = Gfx::shape_text({ x, 0 }, letter_spacing.to_float(), chunk.view, chunk.font, text_type);
  247. CSSPixels chunk_width = CSSPixels::nearest_value_for(glyph_run->width());
  248. // NOTE: We never consider `content: ""` to be collapsible whitespace.
  249. bool is_generated_empty_string = text_node.is_generated() && chunk.length == 0;
  250. Item item {
  251. .type = Item::Type::Text,
  252. .node = &text_node,
  253. .glyph_run = move(glyph_run),
  254. .offset_in_node = chunk.start,
  255. .length_in_node = chunk.length,
  256. .width = chunk_width,
  257. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace && !is_generated_empty_string,
  258. };
  259. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  260. return item;
  261. }
  262. if (m_current_node->is_absolutely_positioned()) {
  263. auto& node = *m_current_node;
  264. skip_to_next();
  265. return Item {
  266. .type = Item::Type::AbsolutelyPositionedElement,
  267. .node = &node,
  268. };
  269. }
  270. if (m_current_node->is_floating()) {
  271. auto& node = *m_current_node;
  272. skip_to_next();
  273. return Item {
  274. .type = Item::Type::FloatingElement,
  275. .node = &node,
  276. };
  277. }
  278. if (is<Layout::BreakNode>(*m_current_node)) {
  279. auto& node = *m_current_node;
  280. skip_to_next();
  281. return Item {
  282. .type = Item::Type::ForcedBreak,
  283. .node = &node,
  284. };
  285. }
  286. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  287. skip_to_next();
  288. return next_without_lookahead();
  289. }
  290. if (!is<Layout::Box>(*m_current_node)) {
  291. skip_to_next();
  292. return next_without_lookahead();
  293. }
  294. if (is<Layout::ReplacedBox>(*m_current_node)) {
  295. auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
  296. // FIXME: This const_cast is gross.
  297. const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
  298. }
  299. auto& box = verify_cast<Layout::Box>(*m_current_node);
  300. auto& box_state = m_layout_state.get(box);
  301. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  302. skip_to_next();
  303. auto item = Item {
  304. .type = Item::Type::Element,
  305. .node = &box,
  306. .offset_in_node = 0,
  307. .length_in_node = 0,
  308. .width = box_state.content_width(),
  309. .padding_start = box_state.padding_left,
  310. .padding_end = box_state.padding_right,
  311. .border_start = box_state.border_left,
  312. .border_end = box_state.border_right,
  313. .margin_start = box_state.margin_left,
  314. .margin_end = box_state.margin_right,
  315. };
  316. add_extra_box_model_metrics_to_item(item, true, true);
  317. return item;
  318. }
  319. void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node)
  320. {
  321. bool do_collapse = true;
  322. bool do_wrap_lines = true;
  323. bool do_respect_linebreaks = false;
  324. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  325. do_collapse = true;
  326. do_wrap_lines = false;
  327. do_respect_linebreaks = false;
  328. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  329. do_collapse = false;
  330. do_wrap_lines = false;
  331. do_respect_linebreaks = true;
  332. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  333. do_collapse = true;
  334. do_wrap_lines = true;
  335. do_respect_linebreaks = true;
  336. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  337. do_collapse = false;
  338. do_wrap_lines = true;
  339. do_respect_linebreaks = true;
  340. }
  341. if (text_node.dom_node().is_editable() && !text_node.dom_node().is_uninteresting_whitespace_node())
  342. do_collapse = false;
  343. m_text_node_context = TextNodeContext {
  344. .do_collapse = do_collapse,
  345. .do_wrap_lines = do_wrap_lines,
  346. .do_respect_linebreaks = do_respect_linebreaks,
  347. .is_first_chunk = true,
  348. .is_last_chunk = false,
  349. .chunk_iterator = TextNode::ChunkIterator { text_node, do_wrap_lines, do_respect_linebreaks },
  350. };
  351. }
  352. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  353. {
  354. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  355. item.margin_start += m_extra_leading_metrics->margin;
  356. item.border_start += m_extra_leading_metrics->border;
  357. item.padding_start += m_extra_leading_metrics->padding;
  358. m_extra_leading_metrics = {};
  359. }
  360. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  361. item.margin_end += m_extra_trailing_metrics->margin;
  362. item.border_end += m_extra_trailing_metrics->border;
  363. item.padding_end += m_extra_trailing_metrics->padding;
  364. m_extra_trailing_metrics = {};
  365. }
  366. }
  367. }