LineBuilder.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. }
  13. LineBuilder::~LineBuilder()
  14. {
  15. update_last_line();
  16. }
  17. void LineBuilder::break_line()
  18. {
  19. update_last_line();
  20. m_context.containing_block().line_boxes().append(LineBox());
  21. begin_new_line();
  22. }
  23. void LineBuilder::begin_new_line()
  24. {
  25. m_current_y += m_max_height_on_current_line;
  26. auto space = m_context.available_space_for_line(m_current_y);
  27. m_available_width_for_current_line = space.right - space.left;
  28. m_max_height_on_current_line = 0;
  29. }
  30. void LineBuilder::append_box(Box& box)
  31. {
  32. m_context.containing_block().line_boxes().last().add_fragment(box, 0, 0, box.width(), box.height());
  33. m_max_height_on_current_line = max(m_max_height_on_current_line, box.height());
  34. }
  35. void LineBuilder::append_text_chunk(TextNode& text_node, size_t offset_in_node, size_t length_in_node, float width, float height)
  36. {
  37. m_context.containing_block().line_boxes().last().add_fragment(text_node, offset_in_node, length_in_node, width, height);
  38. m_max_height_on_current_line = max(m_max_height_on_current_line, height);
  39. }
  40. void LineBuilder::break_if_needed(LayoutMode layout_mode, float next_item_width)
  41. {
  42. if (layout_mode == LayoutMode::AllPossibleLineBreaks
  43. || (m_context.containing_block().line_boxes().last().width() + next_item_width) > m_available_width_for_current_line)
  44. break_line();
  45. }
  46. void LineBuilder::update_last_line()
  47. {
  48. if (m_context.containing_block().line_boxes().is_empty())
  49. return;
  50. auto& line_box = m_context.containing_block().line_boxes().last();
  51. auto text_align = m_context.containing_block().computed_values().text_align();
  52. float x_offset = m_context.available_space_for_line(m_current_y).left;
  53. float excess_horizontal_space = m_context.containing_block().width() - line_box.width();
  54. switch (text_align) {
  55. case CSS::TextAlign::Center:
  56. case CSS::TextAlign::LibwebCenter:
  57. x_offset += excess_horizontal_space / 2;
  58. break;
  59. case CSS::TextAlign::Right:
  60. x_offset += excess_horizontal_space;
  61. break;
  62. case CSS::TextAlign::Left:
  63. case CSS::TextAlign::Justify:
  64. default:
  65. break;
  66. }
  67. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  68. size_t whitespace_count = 0;
  69. if (text_align == CSS::TextAlign::Justify) {
  70. for (auto& fragment : line_box.fragments()) {
  71. if (fragment.is_justifiable_whitespace()) {
  72. ++whitespace_count;
  73. excess_horizontal_space_including_whitespace += fragment.width();
  74. }
  75. }
  76. }
  77. float justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0;
  78. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  79. auto& fragment = line_box.fragments()[i];
  80. // Vertically align everyone's bottom to the line.
  81. // FIXME: Support other kinds of vertical alignment.
  82. fragment.set_offset({ roundf(x_offset + fragment.offset().x()), m_current_y + (m_max_height_on_current_line - fragment.height()) });
  83. if (text_align == CSS::TextAlign::Justify
  84. && fragment.is_justifiable_whitespace()
  85. && fragment.width() != justified_space_width) {
  86. float diff = justified_space_width - fragment.width();
  87. fragment.set_width(justified_space_width);
  88. // Shift subsequent sibling fragments to the right to adjust for change in width.
  89. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
  90. auto offset = line_box.fragments()[j].offset();
  91. offset.translate_by(diff, 0);
  92. line_box.fragments()[j].set_offset(offset);
  93. }
  94. }
  95. }
  96. if (!line_box.fragments().is_empty()) {
  97. float left_edge = line_box.fragments().first().offset().x();
  98. float right_edge = line_box.fragments().last().offset().x() + line_box.fragments().last().width();
  99. float final_line_box_width = right_edge - left_edge;
  100. line_box.m_width = final_line_box_width;
  101. }
  102. }
  103. }