TableFormattingContext.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Node.h>
  7. #include <LibWeb/HTML/BrowsingContext.h>
  8. #include <LibWeb/Layout/Box.h>
  9. #include <LibWeb/Layout/InlineFormattingContext.h>
  10. #include <LibWeb/Layout/TableBox.h>
  11. #include <LibWeb/Layout/TableCellBox.h>
  12. #include <LibWeb/Layout/TableFormattingContext.h>
  13. #include <LibWeb/Layout/TableRowBox.h>
  14. #include <LibWeb/Layout/TableRowGroupBox.h>
  15. namespace Web::Layout {
  16. TableFormattingContext::TableFormattingContext(FormattingState& state, BlockContainer const& block_container, FormattingContext* parent)
  17. : BlockFormattingContext(state, block_container, parent)
  18. {
  19. }
  20. TableFormattingContext::~TableFormattingContext() = default;
  21. void TableFormattingContext::run(Box const& box, LayoutMode)
  22. {
  23. auto& box_state = m_state.get_mutable(box);
  24. compute_width(box);
  25. auto table_width = CSS::Length::make_px(box_state.content_width);
  26. float total_content_width = 0;
  27. float total_content_height = 0;
  28. box.for_each_child_of_type<TableRowGroupBox>([&](auto& row_group_box) {
  29. auto& row_group_box_state = m_state.get_mutable(row_group_box);
  30. compute_width(row_group_box);
  31. auto column_count = row_group_box.column_count();
  32. Vector<float> column_widths;
  33. column_widths.resize(column_count);
  34. row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
  35. calculate_column_widths(row, table_width, column_widths);
  36. });
  37. float missing_width = box_state.content_width;
  38. for (auto column_width : column_widths)
  39. missing_width -= column_width;
  40. if (missing_width > 0)
  41. column_widths[column_widths.size() - 1] += missing_width;
  42. float content_width = 0;
  43. float content_height = 0;
  44. row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
  45. auto& row_state = m_state.get_mutable(row);
  46. row_state.offset = { 0, content_height };
  47. layout_row(row, column_widths);
  48. content_width = max(content_width, row_state.content_width);
  49. content_height += row_state.content_height;
  50. });
  51. if (row_group_box.computed_values().width().has_value() && row_group_box.computed_values().width()->is_length() && row_group_box.computed_values().width()->length().is_auto())
  52. row_group_box_state.content_width = content_width;
  53. row_group_box_state.content_height = content_height;
  54. row_group_box_state.offset = { 0, total_content_height };
  55. total_content_height += content_height;
  56. total_content_width = max(total_content_width, row_group_box_state.content_width);
  57. });
  58. if (box.computed_values().width().has_value() && box.computed_values().width()->is_length() && box.computed_values().width()->length().is_auto())
  59. box_state.content_width = total_content_width;
  60. // FIXME: This is a total hack, we should respect the 'height' property.
  61. box_state.content_height = total_content_height;
  62. }
  63. void TableFormattingContext::calculate_column_widths(Box const& row, CSS::Length const& table_width, Vector<float>& column_widths)
  64. {
  65. m_state.get_mutable(row);
  66. size_t column_index = 0;
  67. bool use_auto_layout = table_width.is_auto();
  68. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  69. auto& cell_state = m_state.get_mutable(cell);
  70. auto const& computed_values = cell.computed_values();
  71. auto specified_width = computed_values.width().has_value() ? computed_values.width()->resolved(cell, table_width).resolved(cell) : CSS::Length::make_auto();
  72. compute_width(cell, specified_width.is_auto() ? LayoutMode::MinContent : LayoutMode::Normal);
  73. if (use_auto_layout) {
  74. (void)layout_inside(cell, LayoutMode::MaxContent);
  75. } else {
  76. (void)layout_inside(cell, LayoutMode::Normal);
  77. }
  78. if (cell.colspan() == 1)
  79. column_widths[column_index] = max(column_widths[column_index], cell_state.border_box_width());
  80. column_index += cell.colspan();
  81. });
  82. column_index = 0;
  83. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  84. auto& cell_state = m_state.get_mutable(cell);
  85. size_t colspan = cell.colspan();
  86. if (colspan != 1) {
  87. float missing = cell_state.border_box_width();
  88. for (size_t i = 0; i < colspan; ++i)
  89. missing -= column_widths[column_index + i];
  90. if (missing > 0) {
  91. float extra = missing / (float)colspan;
  92. for (size_t i = 0; i < colspan; ++i)
  93. column_widths[column_index + i] += extra;
  94. }
  95. }
  96. column_index += colspan;
  97. });
  98. }
  99. void TableFormattingContext::layout_row(Box const& row, Vector<float>& column_widths)
  100. {
  101. auto& row_state = m_state.get_mutable(row);
  102. size_t column_index = 0;
  103. float tallest_cell_height = 0;
  104. float content_width = 0;
  105. auto* table = row.first_ancestor_of_type<TableBox>();
  106. bool use_auto_layout = !table || (!table->computed_values().width().has_value() || (table->computed_values().width()->is_length() && table->computed_values().width()->length().is_auto()));
  107. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  108. auto& cell_state = m_state.get_mutable(cell);
  109. float span_width = 0;
  110. for (size_t i = 0; i < cell.colspan(); ++i)
  111. span_width += column_widths[column_index++];
  112. cell_state.content_width = span_width - cell_state.border_box_left() - cell_state.border_box_right();
  113. BlockFormattingContext::compute_height(cell, m_state);
  114. cell_state.offset = row_state.offset.translated(cell_state.border_box_left() + content_width, cell_state.border_box_top());
  115. // Layout the cell contents a second time, now that we know its final width.
  116. if (use_auto_layout) {
  117. (void)layout_inside(cell, LayoutMode::MaxContent);
  118. } else {
  119. (void)layout_inside(cell, LayoutMode::Normal);
  120. }
  121. content_width += span_width;
  122. tallest_cell_height = max(tallest_cell_height, cell_state.border_box_height());
  123. });
  124. row_state.content_height = tallest_cell_height;
  125. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  126. auto& cell_state = m_state.get_mutable(cell);
  127. cell_state.content_height = tallest_cell_height - cell_state.border_box_top() - cell_state.border_box_bottom();
  128. });
  129. if (use_auto_layout) {
  130. row_state.content_width = content_width;
  131. } else {
  132. auto& table_state = m_state.get_mutable(*table);
  133. row_state.content_width = table_state.content_width;
  134. }
  135. }
  136. }