LineBuilder.cpp 6.0 KB

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