InlineFormattingContext.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Length.h>
  7. #include <LibWeb/DOM/Node.h>
  8. #include <LibWeb/Dump.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/Layout/BlockFormattingContext.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/InlineFormattingContext.h>
  13. #include <LibWeb/Layout/InlineLevelIterator.h>
  14. #include <LibWeb/Layout/LineBuilder.h>
  15. #include <LibWeb/Layout/ReplacedBox.h>
  16. #include <LibWeb/Layout/SVGSVGBox.h>
  17. namespace Web::Layout {
  18. InlineFormattingContext::InlineFormattingContext(
  19. LayoutState& state,
  20. BlockContainer const& containing_block,
  21. LayoutState::UsedValues& containing_block_used_values,
  22. BlockFormattingContext& parent)
  23. : FormattingContext(Type::Inline, state, containing_block, &parent)
  24. , m_containing_block_used_values(containing_block_used_values)
  25. {
  26. }
  27. InlineFormattingContext::~InlineFormattingContext() = default;
  28. BlockFormattingContext& InlineFormattingContext::parent()
  29. {
  30. return static_cast<BlockFormattingContext&>(*FormattingContext::parent());
  31. }
  32. BlockFormattingContext const& InlineFormattingContext::parent() const
  33. {
  34. return static_cast<BlockFormattingContext const&>(*FormattingContext::parent());
  35. }
  36. CSSPixels InlineFormattingContext::leftmost_x_offset_at(CSSPixels y) const
  37. {
  38. // NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
  39. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(m_containing_block_used_values, parent().root());
  40. CSSPixels y_in_root = box_in_root_rect.y() + y;
  41. auto space_and_containing_margin = parent().space_used_and_containing_margin_for_floats(y_in_root);
  42. auto left_side_floats_limit_to_right = space_and_containing_margin.left_total_containing_margin + space_and_containing_margin.left_used_space;
  43. if (box_in_root_rect.x() >= left_side_floats_limit_to_right) {
  44. // The left edge of the containing block is to the right of the rightmost left-side float.
  45. // We start placing inline content at the left edge of the containing block.
  46. return 0;
  47. }
  48. // The left edge of the containing block is to the left of the rightmost left-side float.
  49. // We adjust the inline content insertion point by the overlap between the containing block and the float.
  50. return left_side_floats_limit_to_right - max(CSSPixels(0), box_in_root_rect.x());
  51. }
  52. AvailableSize InlineFormattingContext::available_space_for_line(CSSPixels y) const
  53. {
  54. auto intrusions = parent().intrusion_by_floats_into_box(m_containing_block_used_values, y);
  55. if (m_available_space->width.is_definite()) {
  56. return AvailableSize::make_definite(m_available_space->width.to_px_or_zero() - (intrusions.left + intrusions.right));
  57. } else {
  58. return m_available_space->width;
  59. }
  60. }
  61. CSSPixels InlineFormattingContext::automatic_content_width() const
  62. {
  63. return m_automatic_content_width;
  64. }
  65. CSSPixels InlineFormattingContext::automatic_content_height() const
  66. {
  67. return m_automatic_content_height;
  68. }
  69. void InlineFormattingContext::run(Box const&, LayoutMode layout_mode, AvailableSpace const& available_space)
  70. {
  71. VERIFY(containing_block().children_are_inline());
  72. m_available_space = available_space;
  73. generate_line_boxes(layout_mode);
  74. CSSPixels content_height = 0;
  75. for (auto& line_box : m_containing_block_used_values.line_boxes) {
  76. content_height += line_box.height();
  77. }
  78. // NOTE: We ask the parent BFC to calculate the automatic content width of this IFC.
  79. // This ensures that any floated boxes are taken into account.
  80. m_automatic_content_width = parent().greatest_child_width(containing_block());
  81. m_automatic_content_height = content_height;
  82. }
  83. void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode layout_mode)
  84. {
  85. auto width_of_containing_block = m_available_space->width.to_px_or_zero();
  86. auto& box_state = m_state.get_mutable(box);
  87. auto const& computed_values = box.computed_values();
  88. box_state.margin_left = computed_values.margin().left().to_px(box, width_of_containing_block);
  89. box_state.border_left = computed_values.border_left().width;
  90. box_state.padding_left = computed_values.padding().left().to_px(box, width_of_containing_block);
  91. box_state.margin_right = computed_values.margin().right().to_px(box, width_of_containing_block);
  92. box_state.border_right = computed_values.border_right().width;
  93. box_state.padding_right = computed_values.padding().right().to_px(box, width_of_containing_block);
  94. box_state.margin_top = computed_values.margin().top().to_px(box, width_of_containing_block);
  95. box_state.border_top = computed_values.border_top().width;
  96. box_state.padding_top = computed_values.padding().top().to_px(box, width_of_containing_block);
  97. box_state.padding_bottom = computed_values.padding().bottom().to_px(box, width_of_containing_block);
  98. box_state.border_bottom = computed_values.border_bottom().width;
  99. box_state.margin_bottom = computed_values.margin().bottom().to_px(box, width_of_containing_block);
  100. if (box_is_sized_as_replaced_element(box)) {
  101. box_state.set_content_width(compute_width_for_replaced_element(box, *m_available_space));
  102. box_state.set_content_height(compute_height_for_replaced_element(box, *m_available_space));
  103. if (is<SVGSVGBox>(box))
  104. (void)layout_inside(box, layout_mode, box_state.available_inner_space_or_constraints_from(*m_available_space));
  105. return;
  106. }
  107. // Any box that has simple flow inside should have generated line box fragments already.
  108. if (box.display().is_flow_inside()) {
  109. dbgln("FIXME: InlineFormattingContext::dimension_box_on_line got unexpected box in inline context:");
  110. dump_tree(box);
  111. return;
  112. }
  113. auto const& width_value = box.computed_values().width();
  114. CSSPixels unconstrained_width = 0;
  115. if (should_treat_width_as_auto(box, *m_available_space)) {
  116. auto result = calculate_shrink_to_fit_widths(box);
  117. if (m_available_space->width.is_definite()) {
  118. auto available_width = m_available_space->width.to_px_or_zero()
  119. - box_state.margin_left
  120. - box_state.border_left
  121. - box_state.padding_left
  122. - box_state.padding_right
  123. - box_state.border_right
  124. - box_state.margin_right;
  125. unconstrained_width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  126. } else if (m_available_space->width.is_min_content()) {
  127. unconstrained_width = result.preferred_minimum_width;
  128. } else {
  129. unconstrained_width = result.preferred_width;
  130. }
  131. } else {
  132. if (width_value.contains_percentage() && !m_available_space->width.is_definite()) {
  133. // NOTE: We can't resolve percentages yet. We'll have to wait until after inner layout.
  134. } else {
  135. auto inner_width = calculate_inner_width(box, m_available_space->width, width_value);
  136. unconstrained_width = inner_width;
  137. }
  138. }
  139. CSSPixels width = unconstrained_width;
  140. if (!should_treat_max_width_as_none(box, m_available_space->width)) {
  141. auto max_width = calculate_inner_width(box, m_available_space->width, box.computed_values().max_width());
  142. width = min(width, max_width);
  143. }
  144. auto computed_min_width = box.computed_values().min_width();
  145. if (!computed_min_width.is_auto()) {
  146. auto min_width = calculate_inner_width(box, m_available_space->width, computed_min_width);
  147. width = max(width, min_width);
  148. }
  149. box_state.set_content_width(width);
  150. // NOTE: Flex containers with `auto` height are treated as `max-content`, so we can compute their height early.
  151. if (box_state.has_definite_height() || box.display().is_flex_inside())
  152. parent().compute_height(box, AvailableSpace(AvailableSize::make_definite(width), AvailableSize::make_definite(m_containing_block_used_values.content_height())));
  153. auto independent_formatting_context = layout_inside(box, layout_mode, box_state.available_inner_space_or_constraints_from(*m_available_space));
  154. auto const& height_value = box.computed_values().height();
  155. if (should_treat_height_as_auto(box, *m_available_space)) {
  156. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  157. parent().compute_height(box, AvailableSpace(AvailableSize::make_indefinite(), AvailableSize::make_indefinite()));
  158. } else {
  159. auto inner_height = calculate_inner_height(box, AvailableSize::make_definite(m_containing_block_used_values.content_height()), height_value);
  160. box_state.set_content_height(inner_height);
  161. }
  162. if (independent_formatting_context)
  163. independent_formatting_context->parent_context_did_dimension_child_root_box();
  164. }
  165. void InlineFormattingContext::apply_justification_to_fragments(CSS::TextJustify text_justify, LineBox& line_box, bool is_last_line)
  166. {
  167. switch (text_justify) {
  168. case CSS::TextJustify::None:
  169. return;
  170. // FIXME: These two cases currently fall back to auto, handle them as well.
  171. case CSS::TextJustify::InterCharacter:
  172. case CSS::TextJustify::InterWord:
  173. case CSS::TextJustify::Auto:
  174. break;
  175. }
  176. // https://www.w3.org/TR/css-text-3/#text-align-property
  177. // Unless otherwise specified by text-align-last, the last line before a forced break or the end of the block is start-aligned.
  178. // FIXME: Support text-align-last.
  179. if (is_last_line || line_box.m_has_forced_break)
  180. return;
  181. CSSPixels excess_horizontal_space = line_box.original_available_width().to_px_or_zero() - line_box.width();
  182. CSSPixels excess_horizontal_space_including_whitespace = excess_horizontal_space;
  183. size_t whitespace_count = 0;
  184. for (auto& fragment : line_box.fragments()) {
  185. if (fragment.is_justifiable_whitespace()) {
  186. ++whitespace_count;
  187. excess_horizontal_space_including_whitespace += fragment.width();
  188. }
  189. }
  190. CSSPixels justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / whitespace_count) : 0;
  191. // This is the amount that each fragment will be offset by. If a whitespace
  192. // fragment is shorter than the justified space width, it increases to push
  193. // subsequent fragments, and decreases to pull them back otherwise.
  194. CSSPixels running_diff = 0;
  195. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  196. auto& fragment = line_box.fragments()[i];
  197. auto offset = fragment.offset();
  198. offset.translate_by(running_diff, 0);
  199. fragment.set_offset(offset);
  200. if (fragment.is_justifiable_whitespace()
  201. && fragment.width() != justified_space_width) {
  202. running_diff += justified_space_width - fragment.width();
  203. fragment.set_width(justified_space_width);
  204. }
  205. }
  206. }
  207. void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
  208. {
  209. auto& line_boxes = m_containing_block_used_values.line_boxes;
  210. line_boxes.clear_with_capacity();
  211. InlineLevelIterator iterator(*this, m_state, containing_block(), m_containing_block_used_values, layout_mode);
  212. LineBuilder line_builder(*this, m_state, m_containing_block_used_values);
  213. // NOTE: When we ignore collapsible whitespace chunks at the start of a line,
  214. // we have to remember how much start margin that chunk had in the inline
  215. // axis, so that we can add it to the first non-whitespace chunk.
  216. CSSPixels leading_margin_from_collapsible_whitespace = 0;
  217. for (;;) {
  218. auto item_opt = iterator.next();
  219. if (!item_opt.has_value())
  220. break;
  221. auto& item = item_opt.value();
  222. // Ignore collapsible whitespace chunks at the start of line, and if the last fragment already ends in whitespace.
  223. if (item.is_collapsible_whitespace && (line_boxes.is_empty() || line_boxes.last().is_empty_or_ends_in_whitespace())) {
  224. if (item.node->computed_values().white_space() != CSS::WhiteSpace::Nowrap) {
  225. auto next_width = iterator.next_non_whitespace_sequence_width();
  226. if (next_width > 0)
  227. line_builder.break_if_needed(next_width);
  228. }
  229. leading_margin_from_collapsible_whitespace += item.margin_start;
  230. continue;
  231. }
  232. item.margin_start += leading_margin_from_collapsible_whitespace;
  233. leading_margin_from_collapsible_whitespace = 0;
  234. switch (item.type) {
  235. case InlineLevelIterator::Item::Type::ForcedBreak: {
  236. line_builder.break_line(LineBuilder::ForcedBreak::Yes);
  237. if (item.node) {
  238. auto introduce_clearance = parent().clear_floating_boxes(*item.node, *this);
  239. if (introduce_clearance == BlockFormattingContext::DidIntroduceClearance::Yes)
  240. parent().reset_margin_state();
  241. }
  242. break;
  243. }
  244. case InlineLevelIterator::Item::Type::Element: {
  245. auto& box = verify_cast<Layout::Box>(*item.node);
  246. compute_inset(box);
  247. if (containing_block().computed_values().white_space() != CSS::WhiteSpace::Nowrap) {
  248. auto minimum_space_needed_on_line = item.border_box_width();
  249. if (item.margin_start < 0)
  250. minimum_space_needed_on_line += item.margin_start;
  251. if (item.margin_end < 0)
  252. minimum_space_needed_on_line += item.margin_end;
  253. line_builder.break_if_needed(minimum_space_needed_on_line);
  254. }
  255. line_builder.append_box(box, item.border_start + item.padding_start, item.padding_end + item.border_end, item.margin_start, item.margin_end);
  256. break;
  257. }
  258. case InlineLevelIterator::Item::Type::AbsolutelyPositionedElement:
  259. if (is<Box>(*item.node))
  260. parent().add_absolutely_positioned_box(static_cast<Layout::Box const&>(*item.node));
  261. break;
  262. case InlineLevelIterator::Item::Type::FloatingElement:
  263. if (is<Box>(*item.node)) {
  264. auto introduce_clearance = parent().clear_floating_boxes(*item.node, *this);
  265. if (introduce_clearance == BlockFormattingContext::DidIntroduceClearance::Yes)
  266. parent().reset_margin_state();
  267. parent().layout_floating_box(static_cast<Layout::Box const&>(*item.node), containing_block(), layout_mode, *m_available_space, 0, &line_builder);
  268. }
  269. break;
  270. case InlineLevelIterator::Item::Type::Text: {
  271. auto& text_node = verify_cast<Layout::TextNode>(*item.node);
  272. if (text_node.computed_values().white_space() != CSS::WhiteSpace::Nowrap) {
  273. bool is_whitespace = false;
  274. CSSPixels next_width = 0;
  275. // If we're in a whitespace-collapsing context, we can simply check the flag.
  276. if (item.is_collapsible_whitespace) {
  277. is_whitespace = true;
  278. next_width = iterator.next_non_whitespace_sequence_width();
  279. } else {
  280. // In whitespace-preserving contexts (white-space: pre*), we have to check manually.
  281. auto view = text_node.text_for_rendering().bytes_as_string_view().substring_view(item.offset_in_node, item.length_in_node);
  282. is_whitespace = view.is_whitespace();
  283. if (is_whitespace)
  284. next_width = iterator.next_non_whitespace_sequence_width();
  285. }
  286. // If whitespace caused us to break, we swallow the whitespace instead of
  287. // putting it on the next line.
  288. if (is_whitespace && next_width > 0 && line_builder.break_if_needed(item.border_box_width() + next_width))
  289. break;
  290. }
  291. line_builder.append_text_chunk(
  292. text_node,
  293. item.offset_in_node,
  294. item.length_in_node,
  295. item.border_start + item.padding_start,
  296. item.padding_end + item.border_end,
  297. item.margin_start,
  298. item.margin_end,
  299. item.width,
  300. text_node.computed_values().line_height(),
  301. item.glyph_run);
  302. break;
  303. }
  304. }
  305. }
  306. for (auto& line_box : line_boxes) {
  307. line_box.trim_trailing_whitespace();
  308. }
  309. line_builder.remove_last_line_if_empty();
  310. auto const& containing_block = this->containing_block();
  311. auto text_align = containing_block.computed_values().text_align();
  312. auto text_justify = containing_block.computed_values().text_justify();
  313. if (text_align == CSS::TextAlign::Justify) {
  314. for (size_t i = 0; i < line_boxes.size(); i++) {
  315. auto& line_box = line_boxes[i];
  316. auto is_last_line = i == line_boxes.size() - 1;
  317. apply_justification_to_fragments(text_justify, line_box, is_last_line);
  318. }
  319. }
  320. }
  321. bool InlineFormattingContext::any_floats_intrude_at_y(CSSPixels y) const
  322. {
  323. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(m_containing_block_used_values, parent().root());
  324. CSSPixels y_in_root = box_in_root_rect.y() + y;
  325. auto space_and_containing_margin = parent().space_used_and_containing_margin_for_floats(y_in_root);
  326. return space_and_containing_margin.left_used_space > 0 || space_and_containing_margin.right_used_space > 0;
  327. }
  328. bool InlineFormattingContext::can_fit_new_line_at_y(CSSPixels y) const
  329. {
  330. auto top_intrusions = parent().intrusion_by_floats_into_box(m_containing_block_used_values, y);
  331. auto bottom_intrusions = parent().intrusion_by_floats_into_box(m_containing_block_used_values, y + containing_block().computed_values().line_height() - 1);
  332. auto left_edge = [](auto& space) -> CSSPixels {
  333. return space.left;
  334. };
  335. auto right_edge = [this](auto& space) -> CSSPixels {
  336. return m_available_space->width.to_px_or_zero() - space.right;
  337. };
  338. auto top_left_edge = left_edge(top_intrusions);
  339. auto top_right_edge = right_edge(top_intrusions);
  340. auto bottom_left_edge = left_edge(bottom_intrusions);
  341. auto bottom_right_edge = right_edge(bottom_intrusions);
  342. if (top_left_edge > bottom_right_edge)
  343. return false;
  344. if (bottom_left_edge > top_right_edge)
  345. return false;
  346. return true;
  347. }
  348. CSSPixels InlineFormattingContext::vertical_float_clearance() const
  349. {
  350. return m_vertical_float_clearance;
  351. }
  352. void InlineFormattingContext::set_vertical_float_clearance(CSSPixels vertical_float_clearance)
  353. {
  354. m_vertical_float_clearance = vertical_float_clearance;
  355. }
  356. }