LineBuilder.cpp 15 KB

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