LineBuilder.cpp 17 KB

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