InlineLevelIterator.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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);
  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 x = 0.0f;
  204. if (chunk.has_breaking_tab) {
  205. CSSPixels accumulated_width;
  206. // make sure to account for any fragments that take up a portion of the measured tab stop distance
  207. auto fragments = m_containing_block_used_values.line_boxes.last().fragments();
  208. for (auto const& frag : fragments) {
  209. accumulated_width += frag.width();
  210. }
  211. // https://drafts.csswg.org/css-text/#tab-size-property
  212. auto tab_size = text_node.computed_values().tab_size();
  213. CSSPixels tab_width;
  214. tab_width = tab_size.visit(
  215. [&](CSS::LengthOrCalculated const& t) -> CSSPixels {
  216. auto resolution_context = CSS::Length::ResolutionContext::for_layout_node(text_node);
  217. auto value = t.resolved(resolution_context);
  218. return value.to_px(text_node);
  219. },
  220. [&](CSS::NumberOrCalculated const& n) -> CSSPixels {
  221. auto number = n.resolved(text_node);
  222. return CSSPixels::nearest_value_for(number * chunk.font->glyph_width(' '));
  223. });
  224. // https://drafts.csswg.org/css-text/#white-space-phase-2
  225. // 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
  226. auto tab_stop_dist = accumulated_width > 0 ? (ceil((accumulated_width / tab_width)) * tab_width) - accumulated_width : tab_width;
  227. auto ch_width = chunk.font->glyph_width('0');
  228. // If this distance is less than 0.5ch, then the subsequent tab stop is used instead
  229. if (tab_stop_dist < ch_width * 0.5)
  230. tab_stop_dist += tab_width;
  231. // account for consecutive tabs
  232. auto num_of_tabs = 0;
  233. for (auto code_point : chunk.view) {
  234. if (code_point != '\t')
  235. break;
  236. num_of_tabs++;
  237. }
  238. tab_stop_dist = tab_stop_dist * num_of_tabs;
  239. // remove tabs, we don't want to render them when we shape the text
  240. chunk.view = chunk.view.substring_view(num_of_tabs);
  241. x = tab_stop_dist.to_float();
  242. }
  243. auto glyph_run = Gfx::shape_text({ x, 0 }, chunk.view, chunk.font, text_type);
  244. CSSPixels chunk_width = CSSPixels::nearest_value_for(glyph_run->width());
  245. // NOTE: We never consider `content: ""` to be collapsible whitespace.
  246. bool is_generated_empty_string = text_node.is_generated() && chunk.length == 0;
  247. Item item {
  248. .type = Item::Type::Text,
  249. .node = &text_node,
  250. .glyph_run = move(glyph_run),
  251. .offset_in_node = chunk.start,
  252. .length_in_node = chunk.length,
  253. .width = chunk_width,
  254. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace && !is_generated_empty_string,
  255. };
  256. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  257. return item;
  258. }
  259. if (m_current_node->is_absolutely_positioned()) {
  260. auto& node = *m_current_node;
  261. skip_to_next();
  262. return Item {
  263. .type = Item::Type::AbsolutelyPositionedElement,
  264. .node = &node,
  265. };
  266. }
  267. if (m_current_node->is_floating()) {
  268. auto& node = *m_current_node;
  269. skip_to_next();
  270. return Item {
  271. .type = Item::Type::FloatingElement,
  272. .node = &node,
  273. };
  274. }
  275. if (is<Layout::BreakNode>(*m_current_node)) {
  276. auto& node = *m_current_node;
  277. skip_to_next();
  278. return Item {
  279. .type = Item::Type::ForcedBreak,
  280. .node = &node,
  281. };
  282. }
  283. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  284. skip_to_next();
  285. return next_without_lookahead();
  286. }
  287. if (!is<Layout::Box>(*m_current_node)) {
  288. skip_to_next();
  289. return next_without_lookahead();
  290. }
  291. if (is<Layout::ReplacedBox>(*m_current_node)) {
  292. auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
  293. // FIXME: This const_cast is gross.
  294. const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
  295. }
  296. auto& box = verify_cast<Layout::Box>(*m_current_node);
  297. auto& box_state = m_layout_state.get(box);
  298. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  299. skip_to_next();
  300. auto item = Item {
  301. .type = Item::Type::Element,
  302. .node = &box,
  303. .offset_in_node = 0,
  304. .length_in_node = 0,
  305. .width = box_state.content_width(),
  306. .padding_start = box_state.padding_left,
  307. .padding_end = box_state.padding_right,
  308. .border_start = box_state.border_left,
  309. .border_end = box_state.border_right,
  310. .margin_start = box_state.margin_left,
  311. .margin_end = box_state.margin_right,
  312. };
  313. add_extra_box_model_metrics_to_item(item, true, true);
  314. return item;
  315. }
  316. void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node)
  317. {
  318. bool do_collapse = true;
  319. bool do_wrap_lines = true;
  320. bool do_respect_linebreaks = false;
  321. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  322. do_collapse = true;
  323. do_wrap_lines = false;
  324. do_respect_linebreaks = false;
  325. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  326. do_collapse = false;
  327. do_wrap_lines = false;
  328. do_respect_linebreaks = true;
  329. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  330. do_collapse = true;
  331. do_wrap_lines = true;
  332. do_respect_linebreaks = true;
  333. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  334. do_collapse = false;
  335. do_wrap_lines = true;
  336. do_respect_linebreaks = true;
  337. }
  338. if (text_node.dom_node().is_editable() && !text_node.dom_node().is_uninteresting_whitespace_node())
  339. do_collapse = false;
  340. m_text_node_context = TextNodeContext {
  341. .do_collapse = do_collapse,
  342. .do_wrap_lines = do_wrap_lines,
  343. .do_respect_linebreaks = do_respect_linebreaks,
  344. .is_first_chunk = true,
  345. .is_last_chunk = false,
  346. .chunk_iterator = TextNode::ChunkIterator { text_node, do_wrap_lines, do_respect_linebreaks },
  347. };
  348. }
  349. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  350. {
  351. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  352. item.margin_start += m_extra_leading_metrics->margin;
  353. item.border_start += m_extra_leading_metrics->border;
  354. item.padding_start += m_extra_leading_metrics->padding;
  355. m_extra_leading_metrics = {};
  356. }
  357. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  358. item.margin_end += m_extra_trailing_metrics->margin;
  359. item.border_end += m_extra_trailing_metrics->border;
  360. item.padding_end += m_extra_trailing_metrics->padding;
  361. m_extra_trailing_metrics = {};
  362. }
  363. }
  364. }