LineBuilder.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/LineBuilder.h>
  7. #include <LibWeb/Layout/TextNode.h>
  8. namespace Web::Layout {
  9. LineBuilder::LineBuilder(InlineFormattingContext& context, FormattingState& formatting_state, LayoutMode layout_mode)
  10. : m_context(context)
  11. , m_formatting_state(formatting_state)
  12. , m_containing_block_state(formatting_state.get_mutable(context.containing_block()))
  13. , m_layout_mode(layout_mode)
  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()
  23. {
  24. update_last_line();
  25. m_containing_block_state.line_boxes.append(LineBox());
  26. begin_new_line(true);
  27. }
  28. void LineBuilder::begin_new_line(bool increment_y)
  29. {
  30. if (increment_y)
  31. m_current_y += max(m_max_height_on_current_line, m_context.containing_block().line_height());
  32. switch (m_layout_mode) {
  33. case LayoutMode::Normal:
  34. m_available_width_for_current_line = m_context.available_space_for_line(m_current_y);
  35. break;
  36. case LayoutMode::MinContent:
  37. m_available_width_for_current_line = 0;
  38. break;
  39. case LayoutMode::MaxContent:
  40. m_available_width_for_current_line = INFINITY;
  41. break;
  42. }
  43. m_max_height_on_current_line = 0;
  44. m_last_line_needs_update = true;
  45. }
  46. LineBox& LineBuilder::ensure_last_line_box()
  47. {
  48. auto& line_boxes = m_containing_block_state.line_boxes;
  49. if (line_boxes.is_empty())
  50. line_boxes.append(LineBox {});
  51. return line_boxes.last();
  52. }
  53. void LineBuilder::append_box(Box const& box, float leading_size, float trailing_size, float leading_margin, float trailing_margin)
  54. {
  55. auto& box_state = m_formatting_state.get_mutable(box);
  56. auto& line_box = ensure_last_line_box();
  57. 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());
  58. m_max_height_on_current_line = max(m_max_height_on_current_line, box_state.border_box_height());
  59. box_state.containing_line_box_fragment = LineBoxFragmentCoordinate {
  60. .line_box_index = m_containing_block_state.line_boxes.size() - 1,
  61. .fragment_index = line_box.fragments().size() - 1,
  62. };
  63. }
  64. 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)
  65. {
  66. 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);
  67. m_max_height_on_current_line = max(m_max_height_on_current_line, content_height);
  68. }
  69. bool LineBuilder::should_break(LayoutMode layout_mode, float next_item_width)
  70. {
  71. if (layout_mode == LayoutMode::MinContent)
  72. return true;
  73. if (layout_mode == LayoutMode::MaxContent)
  74. return false;
  75. auto const& line_boxes = m_containing_block_state.line_boxes;
  76. if (line_boxes.is_empty() || line_boxes.last().is_empty())
  77. return false;
  78. auto current_line_width = line_boxes.last().width();
  79. return (current_line_width + next_item_width) > m_available_width_for_current_line;
  80. }
  81. static float box_baseline(FormattingState const& state, Box const& box)
  82. {
  83. auto const& box_state = state.get(box);
  84. auto const& vertical_align = box.computed_values().vertical_align();
  85. if (vertical_align.has<CSS::VerticalAlign>()) {
  86. switch (vertical_align.get<CSS::VerticalAlign>()) {
  87. case CSS::VerticalAlign::Top:
  88. return box_state.border_box_top();
  89. case CSS::VerticalAlign::Bottom:
  90. return box_state.content_height + box_state.border_box_bottom();
  91. default:
  92. break;
  93. }
  94. }
  95. if (!box_state.line_boxes.is_empty())
  96. return box_state.border_box_top() + box_state.offset.y() + box_state.line_boxes.last().baseline();
  97. if (box.has_children() && !box.children_are_inline()) {
  98. auto const* child_box = box.last_child_of_type<Box>();
  99. VERIFY(child_box);
  100. return box_baseline(state, *child_box);
  101. }
  102. return box_state.border_box_height();
  103. }
  104. void LineBuilder::update_last_line()
  105. {
  106. m_last_line_needs_update = false;
  107. auto& line_boxes = m_containing_block_state.line_boxes;
  108. if (line_boxes.is_empty())
  109. return;
  110. auto& line_box = line_boxes.last();
  111. auto text_align = m_context.containing_block().computed_values().text_align();
  112. float x_offset = m_context.leftmost_x_offset_at(m_current_y);
  113. float excess_horizontal_space = m_containing_block_state.content_width - line_box.width();
  114. switch (text_align) {
  115. case CSS::TextAlign::Center:
  116. case CSS::TextAlign::LibwebCenter:
  117. x_offset += excess_horizontal_space / 2;
  118. break;
  119. case CSS::TextAlign::Right:
  120. x_offset += excess_horizontal_space;
  121. break;
  122. case CSS::TextAlign::Left:
  123. case CSS::TextAlign::Justify:
  124. default:
  125. break;
  126. }
  127. auto line_box_baseline = [&] {
  128. float line_box_baseline = 0;
  129. for (auto& fragment : line_box.fragments()) {
  130. auto const& font = fragment.layout_node().font();
  131. auto const line_height = fragment.layout_node().line_height();
  132. auto const font_metrics = font.pixel_metrics();
  133. auto const typographic_height = font_metrics.ascent + font_metrics.descent;
  134. auto const leading = line_height - typographic_height;
  135. auto const half_leading = leading / 2;
  136. // The CSS specification calls this AD (A+D, Ascent + Descent).
  137. float fragment_baseline = 0;
  138. if (fragment.layout_node().is_text_node()) {
  139. fragment_baseline = font_metrics.ascent;
  140. } else {
  141. auto const& box = verify_cast<Layout::Box>(fragment.layout_node());
  142. fragment_baseline = box_baseline(m_formatting_state, box);
  143. }
  144. fragment_baseline += half_leading;
  145. // Remember the baseline used for this fragment. This will be used when painting the fragment.
  146. fragment.set_baseline(fragment_baseline);
  147. // NOTE: For fragments with a <length> vertical-align, shift the line box baseline down by the length.
  148. // This ensures that we make enough vertical space on the line for any manually-aligned fragments.
  149. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().template get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  150. fragment_baseline += length_percentage->length().to_px(fragment.layout_node());
  151. line_box_baseline = max(line_box_baseline, fragment_baseline);
  152. }
  153. return line_box_baseline;
  154. }();
  155. // Start with the "strut", an imaginary zero-width box at the start of each line box.
  156. auto strut_top = m_current_y;
  157. auto strut_bottom = m_current_y + m_context.containing_block().line_height();
  158. float uppermost_box_top = strut_top;
  159. float lowermost_box_bottom = strut_bottom;
  160. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  161. auto& fragment = line_box.fragments()[i];
  162. float new_fragment_x = roundf(x_offset + fragment.offset().x());
  163. float new_fragment_y = 0;
  164. auto y_value_for_alignment = [&](CSS::VerticalAlign vertical_align) {
  165. switch (vertical_align) {
  166. case CSS::VerticalAlign::Baseline:
  167. return m_current_y + line_box_baseline - fragment.baseline() + fragment.border_box_top();
  168. case CSS::VerticalAlign::Top:
  169. return m_current_y + fragment.border_box_top();
  170. case CSS::VerticalAlign::Middle:
  171. case CSS::VerticalAlign::Bottom:
  172. case CSS::VerticalAlign::Sub:
  173. case CSS::VerticalAlign::Super:
  174. case CSS::VerticalAlign::TextBottom:
  175. case CSS::VerticalAlign::TextTop:
  176. // FIXME: These are all 'baseline'
  177. return m_current_y + line_box_baseline - fragment.baseline() + fragment.border_box_top();
  178. }
  179. VERIFY_NOT_REACHED();
  180. };
  181. auto const& vertical_align = fragment.layout_node().computed_values().vertical_align();
  182. if (vertical_align.has<CSS::VerticalAlign>()) {
  183. new_fragment_y = y_value_for_alignment(vertical_align.get<CSS::VerticalAlign>());
  184. } else {
  185. if (auto length_percentage = vertical_align.get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length()) {
  186. auto vertical_align_amount = length_percentage->length().to_px(fragment.layout_node());
  187. new_fragment_y = y_value_for_alignment(CSS::VerticalAlign::Baseline) - vertical_align_amount;
  188. }
  189. }
  190. fragment.set_offset({ new_fragment_x, floorf(new_fragment_y) });
  191. float top_of_inline_box = 0;
  192. float bottom_of_inline_box = 0;
  193. {
  194. // FIXME: Support inline-table elements.
  195. if (fragment.layout_node().is_replaced_box() || fragment.layout_node().is_inline_block()) {
  196. auto const& fragment_box_state = m_formatting_state.get(static_cast<Box const&>(fragment.layout_node()));
  197. top_of_inline_box = fragment.offset().y() - fragment_box_state.margin_box_top();
  198. bottom_of_inline_box = fragment.offset().y() + fragment_box_state.content_height + fragment_box_state.margin_box_bottom();
  199. } else {
  200. auto font_metrics = fragment.layout_node().font().pixel_metrics();
  201. auto typographic_height = font_metrics.ascent + font_metrics.descent;
  202. auto leading = fragment.layout_node().line_height() - typographic_height;
  203. auto half_leading = leading / 2;
  204. top_of_inline_box = fragment.offset().y() + fragment.baseline() - font_metrics.ascent - half_leading;
  205. bottom_of_inline_box = fragment.offset().y() + fragment.baseline() + font_metrics.descent + half_leading;
  206. }
  207. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().template get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  208. bottom_of_inline_box += length_percentage->length().to_px(fragment.layout_node());
  209. }
  210. uppermost_box_top = min(uppermost_box_top, top_of_inline_box);
  211. lowermost_box_bottom = max(lowermost_box_bottom, bottom_of_inline_box);
  212. }
  213. // 3. The line box height is the distance between the uppermost box top and the lowermost box bottom.
  214. line_box.m_height = lowermost_box_bottom - uppermost_box_top;
  215. line_box.m_bottom = m_current_y + line_box.m_height;
  216. line_box.m_baseline = line_box_baseline;
  217. }
  218. void LineBuilder::remove_last_line_if_empty()
  219. {
  220. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  221. auto& line_boxes = m_containing_block_state.line_boxes;
  222. if (!line_boxes.is_empty() && line_boxes.last().fragments().is_empty()) {
  223. line_boxes.take_last();
  224. m_last_line_needs_update = false;
  225. }
  226. }
  227. void LineBuilder::adjust_last_line_after_inserting_floating_box(Badge<BlockFormattingContext>, CSS::Float float_, float space_used_by_float)
  228. {
  229. // NOTE: LineBuilder generates lines from left-to-right, so if we've just added a left-side float,
  230. // that means every fragment already on this line has to move towards the right.
  231. if (float_ == CSS::Float::Left && !m_containing_block_state.line_boxes.is_empty()) {
  232. for (auto& fragment : m_containing_block_state.line_boxes.last().fragments())
  233. fragment.set_offset(fragment.offset().translated(space_used_by_float, 0));
  234. m_containing_block_state.line_boxes.last().m_width += space_used_by_float;
  235. }
  236. m_available_width_for_current_line -= space_used_by_float;
  237. if (m_available_width_for_current_line < 0)
  238. m_available_width_for_current_line = 0;
  239. }
  240. }