LineBuilder.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/BlockFormattingContext.h>
  7. #include <LibWeb/Layout/LineBuilder.h>
  8. #include <LibWeb/Layout/TextNode.h>
  9. namespace Web::Layout {
  10. LineBuilder::LineBuilder(InlineFormattingContext& context, LayoutState& layout_state)
  11. : m_context(context)
  12. , m_layout_state(layout_state)
  13. , m_containing_block_state(layout_state.get_mutable(context.containing_block()))
  14. {
  15. begin_new_line(false);
  16. }
  17. LineBuilder::~LineBuilder()
  18. {
  19. if (m_last_line_needs_update)
  20. update_last_line();
  21. }
  22. void LineBuilder::break_line(Optional<CSSPixels> next_item_width)
  23. {
  24. update_last_line();
  25. size_t break_count = 0;
  26. bool floats_intrude_at_current_y = false;
  27. do {
  28. m_containing_block_state.line_boxes.append(LineBox());
  29. begin_new_line(true, break_count == 0);
  30. break_count++;
  31. floats_intrude_at_current_y = m_context.any_floats_intrude_at_y(m_current_y);
  32. } while ((floats_intrude_at_current_y && !m_context.can_fit_new_line_at_y(m_current_y))
  33. || (next_item_width.has_value()
  34. && next_item_width.value() > m_available_width_for_current_line
  35. && floats_intrude_at_current_y));
  36. }
  37. void LineBuilder::begin_new_line(bool increment_y, bool is_first_break_in_sequence)
  38. {
  39. if (increment_y) {
  40. if (is_first_break_in_sequence) {
  41. // First break is simple, just go to the start of the next line.
  42. m_current_y += max(m_max_height_on_current_line, m_context.containing_block().line_height());
  43. } else {
  44. // We're doing more than one break in a row.
  45. // This means we're trying to squeeze past intruding floats.
  46. // Scan 1px at a time until we find a Y value where a new line can fit.
  47. // FIXME: This is super dumb and inefficient.
  48. CSSPixels candidate_y = m_current_y + 1;
  49. while (true) {
  50. if (m_context.can_fit_new_line_at_y(candidate_y))
  51. break;
  52. ++candidate_y;
  53. }
  54. m_current_y = candidate_y;
  55. }
  56. }
  57. recalculate_available_space();
  58. m_max_height_on_current_line = 0;
  59. m_last_line_needs_update = true;
  60. }
  61. LineBox& LineBuilder::ensure_last_line_box()
  62. {
  63. auto& line_boxes = m_containing_block_state.line_boxes;
  64. if (line_boxes.is_empty())
  65. line_boxes.append(LineBox {});
  66. return line_boxes.last();
  67. }
  68. void LineBuilder::append_box(Box const& box, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin)
  69. {
  70. auto& box_state = m_layout_state.get_mutable(box);
  71. auto& line_box = ensure_last_line_box();
  72. line_box.add_fragment(box, 0, 0, leading_size, trailing_size, leading_margin, trailing_margin, box_state.content_width(), box_state.content_height(), box_state.border_box_top(), box_state.border_box_bottom());
  73. m_max_height_on_current_line = max(m_max_height_on_current_line, box_state.margin_box_height());
  74. box_state.containing_line_box_fragment = LineBoxFragmentCoordinate {
  75. .line_box_index = m_containing_block_state.line_boxes.size() - 1,
  76. .fragment_index = line_box.fragments().size() - 1,
  77. };
  78. }
  79. void LineBuilder::append_text_chunk(TextNode const& text_node, size_t offset_in_node, size_t length_in_node, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height)
  80. {
  81. ensure_last_line_box().add_fragment(text_node, offset_in_node, length_in_node, leading_size, trailing_size, leading_margin, trailing_margin, content_width, content_height, 0, 0);
  82. m_max_height_on_current_line = max(m_max_height_on_current_line, content_height);
  83. }
  84. CSSPixels LineBuilder::y_for_float_to_be_inserted_here(Box const& box)
  85. {
  86. auto const& box_state = m_layout_state.get(box);
  87. CSSPixels const width = box_state.margin_box_width();
  88. CSSPixels const height = box_state.margin_box_height();
  89. CSSPixels candidate_y = m_current_y;
  90. CSSPixels current_line_width = ensure_last_line_box().width();
  91. // If there's already inline content on the current line, check if the new float can fit
  92. // alongside the content. If not, place it on the next line.
  93. if (current_line_width > 0 && (current_line_width + width) > m_available_width_for_current_line)
  94. candidate_y += m_context.containing_block().line_height();
  95. // Then, look for the next Y position where we can fit the new float.
  96. // FIXME: This is super dumb, we move 1px downwards per iteration and stop
  97. // when we find an Y value where we don't collide with other floats.
  98. while (true) {
  99. auto space_at_y_top = m_context.available_space_for_line(candidate_y);
  100. auto space_at_y_bottom = m_context.available_space_for_line(candidate_y + height);
  101. if (width > space_at_y_top || width > space_at_y_bottom) {
  102. if (!m_context.any_floats_intrude_at_y(candidate_y) && !m_context.any_floats_intrude_at_y(candidate_y + height)) {
  103. return candidate_y;
  104. }
  105. } else {
  106. return candidate_y;
  107. }
  108. candidate_y += 1;
  109. }
  110. }
  111. bool LineBuilder::should_break(CSSPixels next_item_width)
  112. {
  113. if (!isfinite(m_available_width_for_current_line.value()))
  114. return false;
  115. auto const& line_boxes = m_containing_block_state.line_boxes;
  116. if (line_boxes.is_empty() || line_boxes.last().is_empty()) {
  117. // If we don't have a single line box yet *and* there are no floats intruding
  118. // at this Y coordinate, we don't need to break before inserting anything.
  119. if (!m_context.any_floats_intrude_at_y(m_current_y))
  120. return false;
  121. if (!m_context.any_floats_intrude_at_y(m_current_y + m_context.containing_block().line_height()))
  122. return false;
  123. }
  124. auto current_line_width = ensure_last_line_box().width();
  125. return (current_line_width + next_item_width) > m_available_width_for_current_line;
  126. }
  127. void LineBuilder::update_last_line()
  128. {
  129. m_last_line_needs_update = false;
  130. auto& line_boxes = m_containing_block_state.line_boxes;
  131. if (line_boxes.is_empty())
  132. return;
  133. auto& line_box = line_boxes.last();
  134. auto text_align = m_context.containing_block().computed_values().text_align();
  135. auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height());
  136. CSSPixels x_offset_top = m_context.leftmost_x_offset_at(m_current_y);
  137. CSSPixels x_offset_bottom = m_context.leftmost_x_offset_at(m_current_y + current_line_height - 1);
  138. CSSPixels x_offset = max(x_offset_top, x_offset_bottom);
  139. // If the IFC's containing block has left-side margin, it has already been shifted to the right by that amount.
  140. // We subtract the margin-left here to ensure that the left-side "space used by floats" doesn't get applied twice.
  141. x_offset = max(CSSPixels(0), x_offset - m_containing_block_state.margin_left);
  142. CSSPixels excess_horizontal_space = m_available_width_for_current_line - line_box.width();
  143. switch (text_align) {
  144. case CSS::TextAlign::Center:
  145. case CSS::TextAlign::LibwebCenter:
  146. x_offset += excess_horizontal_space / 2;
  147. break;
  148. case CSS::TextAlign::Right:
  149. x_offset += excess_horizontal_space;
  150. break;
  151. case CSS::TextAlign::Left:
  152. case CSS::TextAlign::Justify:
  153. default:
  154. break;
  155. }
  156. auto strut_baseline = [&] {
  157. auto& font = m_context.containing_block().font();
  158. auto const line_height = m_context.containing_block().line_height();
  159. auto const font_metrics = font.pixel_metrics();
  160. auto const typographic_height = font_metrics.ascent + font_metrics.descent;
  161. auto const leading = line_height - typographic_height;
  162. auto const half_leading = leading / 2;
  163. return CSSPixels(font_metrics.ascent) + half_leading;
  164. }();
  165. auto line_box_baseline = [&] {
  166. CSSPixels line_box_baseline = strut_baseline;
  167. for (auto& fragment : line_box.fragments()) {
  168. auto const& font = fragment.layout_node().font();
  169. auto const line_height = fragment.layout_node().line_height();
  170. auto const font_metrics = font.pixel_metrics();
  171. auto const typographic_height = CSSPixels(font_metrics.ascent + font_metrics.descent);
  172. auto const leading = line_height - typographic_height;
  173. auto const half_leading = leading / 2;
  174. // The CSS specification calls this AD (A+D, Ascent + Descent).
  175. CSSPixels fragment_baseline = 0;
  176. if (fragment.layout_node().is_text_node()) {
  177. fragment_baseline = CSSPixels(font_metrics.ascent) + half_leading;
  178. } else {
  179. auto const& box = verify_cast<Layout::Box>(fragment.layout_node());
  180. fragment_baseline = box_baseline(m_layout_state, box);
  181. }
  182. // Remember the baseline used for this fragment. This will be used when painting the fragment.
  183. fragment.set_baseline(fragment_baseline);
  184. // NOTE: For fragments with a <length> vertical-align, shift the line box baseline down by the length.
  185. // This ensures that we make enough vertical space on the line for any manually-aligned fragments.
  186. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().template get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  187. fragment_baseline += length_percentage->length().to_px(fragment.layout_node());
  188. line_box_baseline = max(line_box_baseline, fragment_baseline);
  189. }
  190. return line_box_baseline;
  191. }();
  192. // Start with the "strut", an imaginary zero-width box at the start of each line box.
  193. auto strut_top = m_current_y;
  194. auto strut_bottom = m_current_y + m_context.containing_block().line_height();
  195. CSSPixels uppermost_box_top = strut_top;
  196. CSSPixels lowermost_box_bottom = strut_bottom;
  197. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  198. auto& fragment = line_box.fragments()[i];
  199. CSSPixels new_fragment_x = round(x_offset + fragment.offset().x());
  200. CSSPixels new_fragment_y = 0;
  201. auto y_value_for_alignment = [&](CSS::VerticalAlign vertical_align) {
  202. CSSPixels effective_box_top = fragment.border_box_top();
  203. if (fragment.is_atomic_inline()) {
  204. auto const& fragment_box_state = m_layout_state.get(static_cast<Box const&>(fragment.layout_node()));
  205. effective_box_top = fragment_box_state.margin_box_top();
  206. }
  207. switch (vertical_align) {
  208. case CSS::VerticalAlign::Baseline:
  209. return m_current_y + line_box_baseline - fragment.baseline() + effective_box_top;
  210. case CSS::VerticalAlign::Top:
  211. return m_current_y + effective_box_top;
  212. case CSS::VerticalAlign::Middle:
  213. case CSS::VerticalAlign::Bottom:
  214. case CSS::VerticalAlign::Sub:
  215. case CSS::VerticalAlign::Super:
  216. case CSS::VerticalAlign::TextBottom:
  217. case CSS::VerticalAlign::TextTop:
  218. // FIXME: These are all 'baseline'
  219. return m_current_y + line_box_baseline - fragment.baseline() + effective_box_top;
  220. }
  221. VERIFY_NOT_REACHED();
  222. };
  223. auto const& vertical_align = fragment.layout_node().computed_values().vertical_align();
  224. if (vertical_align.has<CSS::VerticalAlign>()) {
  225. new_fragment_y = y_value_for_alignment(vertical_align.get<CSS::VerticalAlign>());
  226. } else {
  227. if (auto length_percentage = vertical_align.get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length()) {
  228. auto vertical_align_amount = length_percentage->length().to_px(fragment.layout_node());
  229. new_fragment_y = y_value_for_alignment(CSS::VerticalAlign::Baseline) - vertical_align_amount;
  230. }
  231. }
  232. fragment.set_offset({ new_fragment_x, floor(new_fragment_y) });
  233. CSSPixels top_of_inline_box = 0;
  234. CSSPixels bottom_of_inline_box = 0;
  235. {
  236. // FIXME: Support inline-table elements.
  237. if (fragment.is_atomic_inline()) {
  238. auto const& fragment_box_state = m_layout_state.get(static_cast<Box const&>(fragment.layout_node()));
  239. top_of_inline_box = (fragment.offset().y() - fragment_box_state.margin_box_top());
  240. bottom_of_inline_box = (fragment.offset().y() + fragment_box_state.content_height() + fragment_box_state.margin_box_bottom());
  241. } else {
  242. auto font_metrics = fragment.layout_node().font().pixel_metrics();
  243. auto typographic_height = font_metrics.ascent + font_metrics.descent;
  244. auto leading = fragment.layout_node().line_height() - typographic_height;
  245. auto half_leading = leading / 2;
  246. top_of_inline_box = (fragment.offset().y() + fragment.baseline() - font_metrics.ascent - half_leading);
  247. bottom_of_inline_box = (fragment.offset().y() + fragment.baseline() + font_metrics.descent + half_leading);
  248. }
  249. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().template get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  250. bottom_of_inline_box += length_percentage->length().to_px(fragment.layout_node());
  251. }
  252. uppermost_box_top = min(uppermost_box_top, top_of_inline_box);
  253. lowermost_box_bottom = max(lowermost_box_bottom, bottom_of_inline_box);
  254. }
  255. // 3. The line box height is the distance between the uppermost box top and the lowermost box bottom.
  256. line_box.m_height = lowermost_box_bottom - uppermost_box_top;
  257. line_box.m_bottom = m_current_y + line_box.m_height;
  258. line_box.m_baseline = line_box_baseline;
  259. }
  260. void LineBuilder::remove_last_line_if_empty()
  261. {
  262. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  263. auto& line_boxes = m_containing_block_state.line_boxes;
  264. if (!line_boxes.is_empty() && line_boxes.last().fragments().is_empty()) {
  265. line_boxes.take_last();
  266. m_last_line_needs_update = false;
  267. }
  268. }
  269. void LineBuilder::recalculate_available_space()
  270. {
  271. auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height());
  272. auto available_at_top_of_line_box = m_context.available_space_for_line(m_current_y);
  273. auto available_at_bottom_of_line_box = m_context.available_space_for_line(m_current_y + current_line_height - 1);
  274. m_available_width_for_current_line = min(available_at_bottom_of_line_box, available_at_top_of_line_box);
  275. }
  276. }