LineBuilder.cpp 19 KB

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