LineBuilder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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<float> 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. float 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, float leading_size, float trailing_size, float leading_margin, float 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.border_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, float leading_size, float trailing_size, float leading_margin, float trailing_margin, float content_width, float 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. float LineBuilder::y_for_float_to_be_inserted_here(Box const& box)
  85. {
  86. auto const& box_state = m_layout_state.get(box);
  87. auto const width = box_state.margin_box_width();
  88. auto const height = box_state.margin_box_height();
  89. float candidate_y = m_current_y;
  90. float 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(float next_item_width)
  112. {
  113. if (!isfinite(m_available_width_for_current_line))
  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. static float box_baseline(LayoutState const& state, Box const& box)
  128. {
  129. auto const& box_state = state.get(box);
  130. auto const& vertical_align = box.computed_values().vertical_align();
  131. if (vertical_align.has<CSS::VerticalAlign>()) {
  132. switch (vertical_align.get<CSS::VerticalAlign>()) {
  133. case CSS::VerticalAlign::Top:
  134. return box_state.border_box_top();
  135. case CSS::VerticalAlign::Bottom:
  136. return box_state.content_height() + box_state.border_box_bottom();
  137. default:
  138. break;
  139. }
  140. }
  141. if (!box_state.line_boxes.is_empty())
  142. return box_state.border_box_top() + box_state.offset.y() + box_state.line_boxes.last().baseline();
  143. if (box.has_children() && !box.children_are_inline()) {
  144. auto const* child_box = box.last_child_of_type<Box>();
  145. VERIFY(child_box);
  146. return box_baseline(state, *child_box);
  147. }
  148. return box_state.border_box_height();
  149. }
  150. void LineBuilder::update_last_line()
  151. {
  152. m_last_line_needs_update = false;
  153. auto& line_boxes = m_containing_block_state.line_boxes;
  154. if (line_boxes.is_empty())
  155. return;
  156. auto& line_box = line_boxes.last();
  157. auto text_align = m_context.containing_block().computed_values().text_align();
  158. auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height());
  159. float x_offset_top = m_context.leftmost_x_offset_at(m_current_y);
  160. float x_offset_bottom = m_context.leftmost_x_offset_at(m_current_y + current_line_height - 1);
  161. float x_offset = max(x_offset_top, x_offset_bottom);
  162. float excess_horizontal_space = m_available_width_for_current_line - line_box.width();
  163. switch (text_align) {
  164. case CSS::TextAlign::Center:
  165. case CSS::TextAlign::LibwebCenter:
  166. x_offset += excess_horizontal_space / 2;
  167. break;
  168. case CSS::TextAlign::Right:
  169. x_offset += excess_horizontal_space;
  170. break;
  171. case CSS::TextAlign::Left:
  172. case CSS::TextAlign::Justify:
  173. default:
  174. break;
  175. }
  176. auto line_box_baseline = [&] {
  177. float line_box_baseline = 0;
  178. for (auto& fragment : line_box.fragments()) {
  179. auto const& font = fragment.layout_node().font();
  180. auto const line_height = fragment.layout_node().line_height();
  181. auto const font_metrics = font.pixel_metrics();
  182. auto const typographic_height = font_metrics.ascent + font_metrics.descent;
  183. auto const leading = line_height - typographic_height;
  184. auto const half_leading = leading / 2;
  185. // The CSS specification calls this AD (A+D, Ascent + Descent).
  186. float fragment_baseline = 0;
  187. if (fragment.layout_node().is_text_node()) {
  188. fragment_baseline = font_metrics.ascent + half_leading;
  189. } else {
  190. auto const& box = verify_cast<Layout::Box>(fragment.layout_node());
  191. fragment_baseline = box_baseline(m_layout_state, box);
  192. }
  193. // Remember the baseline used for this fragment. This will be used when painting the fragment.
  194. fragment.set_baseline(fragment_baseline);
  195. // NOTE: For fragments with a <length> vertical-align, shift the line box baseline down by the length.
  196. // This ensures that we make enough vertical space on the line for any manually-aligned fragments.
  197. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().template get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  198. fragment_baseline += length_percentage->length().to_px(fragment.layout_node());
  199. line_box_baseline = max(line_box_baseline, fragment_baseline);
  200. }
  201. return line_box_baseline;
  202. }();
  203. // Start with the "strut", an imaginary zero-width box at the start of each line box.
  204. auto strut_top = m_current_y;
  205. auto strut_bottom = m_current_y + m_context.containing_block().line_height();
  206. float uppermost_box_top = strut_top;
  207. float lowermost_box_bottom = strut_bottom;
  208. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  209. auto& fragment = line_box.fragments()[i];
  210. float new_fragment_x = roundf(x_offset + fragment.offset().x());
  211. float new_fragment_y = 0;
  212. auto y_value_for_alignment = [&](CSS::VerticalAlign vertical_align) {
  213. switch (vertical_align) {
  214. case CSS::VerticalAlign::Baseline:
  215. return m_current_y + line_box_baseline - fragment.baseline() + fragment.border_box_top();
  216. case CSS::VerticalAlign::Top:
  217. return m_current_y + fragment.border_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() + fragment.border_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 length_percentage = vertical_align.get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length()) {
  234. auto vertical_align_amount = length_percentage->length().to_px(fragment.layout_node());
  235. new_fragment_y = y_value_for_alignment(CSS::VerticalAlign::Baseline) - vertical_align_amount;
  236. }
  237. }
  238. fragment.set_offset({ new_fragment_x, floorf(new_fragment_y) });
  239. float top_of_inline_box = 0;
  240. float bottom_of_inline_box = 0;
  241. {
  242. // FIXME: Support inline-table elements.
  243. if (fragment.layout_node().is_replaced_box() || fragment.layout_node().is_inline_block()) {
  244. auto const& fragment_box_state = m_layout_state.get(static_cast<Box const&>(fragment.layout_node()));
  245. top_of_inline_box = fragment.offset().y() - fragment_box_state.margin_box_top();
  246. bottom_of_inline_box = fragment.offset().y() + fragment_box_state.content_height() + fragment_box_state.margin_box_bottom();
  247. } else {
  248. auto font_metrics = fragment.layout_node().font().pixel_metrics();
  249. auto typographic_height = font_metrics.ascent + font_metrics.descent;
  250. auto leading = fragment.layout_node().line_height() - typographic_height;
  251. auto half_leading = leading / 2;
  252. top_of_inline_box = fragment.offset().y() + fragment.baseline() - font_metrics.ascent - half_leading;
  253. bottom_of_inline_box = fragment.offset().y() + fragment.baseline() + font_metrics.descent + half_leading;
  254. }
  255. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().template get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  256. bottom_of_inline_box += length_percentage->length().to_px(fragment.layout_node());
  257. }
  258. uppermost_box_top = min(uppermost_box_top, top_of_inline_box);
  259. lowermost_box_bottom = max(lowermost_box_bottom, bottom_of_inline_box);
  260. }
  261. // 3. The line box height is the distance between the uppermost box top and the lowermost box bottom.
  262. line_box.m_height = lowermost_box_bottom - uppermost_box_top;
  263. line_box.m_bottom = m_current_y + line_box.m_height;
  264. line_box.m_baseline = line_box_baseline;
  265. }
  266. void LineBuilder::remove_last_line_if_empty()
  267. {
  268. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  269. auto& line_boxes = m_containing_block_state.line_boxes;
  270. if (!line_boxes.is_empty() && line_boxes.last().fragments().is_empty()) {
  271. line_boxes.take_last();
  272. m_last_line_needs_update = false;
  273. }
  274. }
  275. void LineBuilder::recalculate_available_space()
  276. {
  277. auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height());
  278. auto available_at_top_of_line_box = m_context.available_space_for_line(m_current_y);
  279. auto available_at_bottom_of_line_box = m_context.available_space_for_line(m_current_y + current_line_height - 1);
  280. m_available_width_for_current_line = min(available_at_bottom_of_line_box, available_at_top_of_line_box);
  281. }
  282. }