LineBuilder.cpp 18 KB

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