LineBuilder.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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)
  10. : m_context(context)
  11. , m_formatting_state(formatting_state)
  12. , m_containing_block_state(formatting_state.ensure(context.containing_block()))
  13. {
  14. begin_new_line(false);
  15. }
  16. LineBuilder::~LineBuilder()
  17. {
  18. if (m_last_line_needs_update)
  19. update_last_line();
  20. }
  21. void LineBuilder::break_line()
  22. {
  23. update_last_line();
  24. m_containing_block_state.line_boxes.append(LineBox());
  25. begin_new_line(true);
  26. }
  27. void LineBuilder::begin_new_line(bool increment_y)
  28. {
  29. if (increment_y)
  30. m_current_y += max(m_max_height_on_current_line, m_context.containing_block().line_height());
  31. auto space = m_context.available_space_for_line(m_current_y);
  32. m_available_width_for_current_line = space.right - space.left;
  33. m_max_height_on_current_line = 0;
  34. m_last_line_needs_update = true;
  35. }
  36. LineBox& LineBuilder::ensure_last_line_box()
  37. {
  38. auto& line_boxes = m_containing_block_state.line_boxes;
  39. if (line_boxes.is_empty())
  40. line_boxes.append(LineBox {});
  41. return line_boxes.last();
  42. }
  43. void LineBuilder::append_box(Box const& box, float leading_size, float trailing_size)
  44. {
  45. auto const& box_state = m_formatting_state.get(box);
  46. ensure_last_line_box().add_fragment(box, 0, 0, leading_size, trailing_size, box_state.content_width, box_state.content_height);
  47. m_max_height_on_current_line = max(m_max_height_on_current_line, box_state.content_height);
  48. }
  49. 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 content_width, float content_height)
  50. {
  51. ensure_last_line_box().add_fragment(text_node, offset_in_node, length_in_node, leading_size, trailing_size, content_width, content_height);
  52. m_max_height_on_current_line = max(m_max_height_on_current_line, content_height);
  53. }
  54. bool LineBuilder::should_break(LayoutMode layout_mode, float next_item_width, bool should_force_break)
  55. {
  56. if (layout_mode == LayoutMode::AllPossibleLineBreaks)
  57. return true;
  58. if (should_force_break)
  59. return true;
  60. if (layout_mode == LayoutMode::OnlyRequiredLineBreaks)
  61. return false;
  62. auto const& line_boxes = m_containing_block_state.line_boxes;
  63. if (line_boxes.is_empty() || line_boxes.last().is_empty())
  64. return false;
  65. auto current_line_width = line_boxes.last().width();
  66. return (current_line_width + next_item_width) > m_available_width_for_current_line;
  67. }
  68. void LineBuilder::update_last_line()
  69. {
  70. m_last_line_needs_update = false;
  71. auto& line_boxes = m_containing_block_state.line_boxes;
  72. if (line_boxes.is_empty())
  73. return;
  74. auto& line_box = line_boxes.last();
  75. auto text_align = m_context.containing_block().computed_values().text_align();
  76. float x_offset = m_context.available_space_for_line(m_current_y).left;
  77. float excess_horizontal_space = m_containing_block_state.content_width - line_box.width();
  78. switch (text_align) {
  79. case CSS::TextAlign::Center:
  80. case CSS::TextAlign::LibwebCenter:
  81. x_offset += excess_horizontal_space / 2;
  82. break;
  83. case CSS::TextAlign::Right:
  84. x_offset += excess_horizontal_space;
  85. break;
  86. case CSS::TextAlign::Left:
  87. case CSS::TextAlign::Justify:
  88. default:
  89. break;
  90. }
  91. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  92. size_t whitespace_count = 0;
  93. if (text_align == CSS::TextAlign::Justify) {
  94. for (auto& fragment : line_box.fragments()) {
  95. if (fragment.is_justifiable_whitespace()) {
  96. ++whitespace_count;
  97. excess_horizontal_space_including_whitespace += fragment.width();
  98. }
  99. }
  100. }
  101. float justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0;
  102. // HACK: This is where we determine the baseline of this line box.
  103. // We use the bottommost value of all the font baselines on the line and all the inline-block heights.
  104. // FIXME: Support all the various CSS baseline properties, etc.
  105. float max_height = max(m_max_height_on_current_line, m_context.containing_block().line_height());
  106. float line_box_baseline = 0;
  107. for (auto& fragment : line_box.fragments()) {
  108. float fragment_baseline;
  109. if (fragment.layout_node().is_box()) {
  110. fragment_baseline = m_formatting_state.get(static_cast<Box const&>(fragment.layout_node())).content_height;
  111. } else {
  112. float font_baseline = fragment.layout_node().font().baseline();
  113. fragment_baseline = (max_height / 2.0f) + (font_baseline / 2.0f);
  114. }
  115. line_box_baseline = max(line_box_baseline, fragment_baseline);
  116. }
  117. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  118. auto& fragment = line_box.fragments()[i];
  119. // Vertically align everyone's bottom to the baseline.
  120. // FIXME: Support other kinds of vertical alignment.
  121. fragment.set_offset({ roundf(x_offset + fragment.offset().x()), m_current_y + (line_box_baseline - fragment.height()) });
  122. if (text_align == CSS::TextAlign::Justify
  123. && fragment.is_justifiable_whitespace()
  124. && fragment.width() != justified_space_width) {
  125. float diff = justified_space_width - fragment.width();
  126. fragment.set_width(justified_space_width);
  127. // Shift subsequent sibling fragments to the right to adjust for change in width.
  128. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
  129. auto offset = line_box.fragments()[j].offset();
  130. offset.translate_by(diff, 0);
  131. line_box.fragments()[j].set_offset(offset);
  132. }
  133. }
  134. }
  135. if (!line_box.fragments().is_empty()) {
  136. float left_edge = line_box.fragments().first().offset().x();
  137. float right_edge = line_box.fragments().last().offset().x() + line_box.fragments().last().width();
  138. float final_line_box_width = right_edge - left_edge;
  139. line_box.m_width = final_line_box_width;
  140. }
  141. }
  142. void LineBuilder::remove_last_line_if_empty()
  143. {
  144. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  145. auto& line_boxes = m_containing_block_state.line_boxes;
  146. if (!line_boxes.is_empty() && line_boxes.last().fragments().is_empty()) {
  147. line_boxes.take_last();
  148. m_last_line_needs_update = false;
  149. }
  150. }
  151. }