LineBuilder.cpp 6.1 KB

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