TableFormattingContext.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(LayoutState& 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, AvailableSpace const& available_space)
  22. {
  23. auto& box_state = m_state.get_mutable(box);
  24. compute_width(box, available_space);
  25. auto table_width = CSS::Length::make_px(box_state.content_width());
  26. auto table_width_is_auto = box.computed_values().width().is_auto();
  27. float total_content_width = 0;
  28. float total_content_height = 0;
  29. Vector<ColumnWidth> column_widths;
  30. box.for_each_child_of_type<TableRowGroupBox>([&](auto& row_group_box) {
  31. compute_width(row_group_box, available_space);
  32. auto column_count = row_group_box.column_count();
  33. column_widths.resize(max(column_count, column_widths.size()));
  34. row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
  35. calculate_column_widths(row, table_width, column_widths, available_space);
  36. });
  37. });
  38. box.for_each_child_of_type<TableRowGroupBox>([&](auto& row_group_box) {
  39. auto& row_group_box_state = m_state.get_mutable(row_group_box);
  40. float remaining_for_max = box_state.content_width();
  41. float remaining_for_min = box_state.content_width();
  42. for (auto& column_width : column_widths) {
  43. remaining_for_max -= column_width.max;
  44. remaining_for_min -= column_width.min;
  45. }
  46. bool max_fits = remaining_for_max >= 0;
  47. bool min_fits = remaining_for_min >= 0;
  48. if (max_fits) {
  49. for (auto& column_width : column_widths)
  50. column_width.used = column_width.max;
  51. } else {
  52. for (auto& column_width : column_widths)
  53. column_width.used = column_width.min;
  54. }
  55. if (!table_width_is_auto || (min_fits && !max_fits)) {
  56. float missing_width = max_fits ? remaining_for_max : remaining_for_min;
  57. if (missing_width > 0) {
  58. size_t num_auto_columns = 0;
  59. for (auto& column_width : column_widths) {
  60. if (column_width.is_auto)
  61. num_auto_columns++;
  62. }
  63. if (num_auto_columns) {
  64. float extra = missing_width / (float)num_auto_columns;
  65. for (auto& column_width : column_widths) {
  66. if (column_width.is_auto)
  67. column_width.used += extra;
  68. }
  69. }
  70. }
  71. }
  72. float content_width = 0;
  73. float content_height = 0;
  74. row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
  75. auto& row_state = m_state.get_mutable(row);
  76. row_state.offset = { 0, content_height };
  77. layout_row(row, column_widths, available_space);
  78. content_width = max(content_width, row_state.content_width());
  79. content_height += row_state.content_height();
  80. });
  81. if (row_group_box.computed_values().width().is_auto())
  82. row_group_box_state.set_content_width(content_width);
  83. row_group_box_state.set_content_height(content_height);
  84. row_group_box_state.offset = { 0, total_content_height };
  85. total_content_height += content_height;
  86. total_content_width = max(total_content_width, row_group_box_state.content_width());
  87. });
  88. if (table_width_is_auto)
  89. box_state.set_content_width(total_content_width);
  90. // FIXME: This is a total hack, we should respect the 'height' property.
  91. m_automatic_content_height = total_content_height;
  92. }
  93. void TableFormattingContext::calculate_column_widths(Box const& row, CSS::Length const& table_width, Vector<ColumnWidth>& column_widths, AvailableSpace const& available_space)
  94. {
  95. m_state.get_mutable(row);
  96. size_t column_index = 0;
  97. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  98. auto& cell_state = m_state.get_mutable(cell);
  99. auto const& computed_values = cell.computed_values();
  100. auto specified_width = computed_values.width().resolved(cell, table_width).resolved(cell);
  101. if (specified_width.is_auto()) {
  102. auto width = calculate_max_content_width(cell);
  103. cell_state.set_content_width(width);
  104. } else {
  105. compute_width(cell, AvailableSpace(AvailableSize::make_indefinite(), AvailableSize::make_indefinite()), LayoutMode::Normal);
  106. }
  107. if (auto independent_formatting_context = layout_inside(cell, LayoutMode::Normal, cell_state.available_inner_space_or_constraints_from(available_space)))
  108. independent_formatting_context->parent_context_did_dimension_child_root_box();
  109. if (cell.colspan() == 1) {
  110. auto min_width = calculate_min_content_width(cell);
  111. auto max_width = calculate_max_content_width(cell);
  112. min_width = max(min_width, cell_state.border_box_width());
  113. max_width = max(max_width, cell_state.border_box_width());
  114. column_widths[column_index].min = max(column_widths[column_index].min, min_width);
  115. column_widths[column_index].max = max(column_widths[column_index].max, max_width);
  116. column_widths[column_index].is_auto &= specified_width.is_auto();
  117. }
  118. column_index += cell.colspan();
  119. });
  120. column_index = 0;
  121. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  122. size_t colspan = cell.colspan();
  123. if (colspan > 1) {
  124. auto& cell_state = m_state.get_mutable(cell);
  125. auto min_width = calculate_min_content_width(cell);
  126. auto max_width = calculate_max_content_width(cell);
  127. float missing_min = max(min_width, cell_state.border_box_width());
  128. float missing_max = max(max_width, cell_state.border_box_width());
  129. for (size_t i = 0; i < colspan; ++i) {
  130. missing_min -= column_widths[column_index + i].min;
  131. missing_max -= column_widths[column_index + i].max;
  132. }
  133. if (missing_min > 0) {
  134. float extra = missing_min / (float)colspan;
  135. for (size_t i = 0; i < colspan; ++i)
  136. column_widths[column_index + i].min += extra;
  137. }
  138. if (missing_max > 0) {
  139. float extra = missing_max / (float)colspan;
  140. for (size_t i = 0; i < colspan; ++i)
  141. column_widths[column_index + i].max += extra;
  142. }
  143. }
  144. column_index += colspan;
  145. });
  146. }
  147. void TableFormattingContext::layout_row(Box const& row, Vector<ColumnWidth>& column_widths, AvailableSpace const& available_space)
  148. {
  149. auto& row_state = m_state.get_mutable(row);
  150. size_t column_index = 0;
  151. float tallest_cell_height = 0;
  152. float content_width = 0;
  153. auto* table = row.first_ancestor_of_type<TableBox>();
  154. bool use_auto_layout = !table || table->computed_values().width().is_auto();
  155. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  156. auto& cell_state = m_state.get_mutable(cell);
  157. float span_width = 0;
  158. for (size_t i = 0; i < cell.colspan(); ++i)
  159. span_width += column_widths[column_index++].used;
  160. cell_state.set_content_width(span_width - cell_state.border_box_left() - cell_state.border_box_right());
  161. BlockFormattingContext::compute_height(cell, AvailableSpace(AvailableSize::make_indefinite(), AvailableSize::make_indefinite()));
  162. cell_state.offset = row_state.offset.translated(cell_state.border_box_left() + content_width, cell_state.border_box_top());
  163. // Layout the cell contents a second time, now that we know its final width.
  164. if (auto independent_formatting_context = layout_inside(cell, LayoutMode::Normal, cell_state.available_inner_space_or_constraints_from(available_space)))
  165. independent_formatting_context->parent_context_did_dimension_child_root_box();
  166. content_width += span_width;
  167. tallest_cell_height = max(tallest_cell_height, cell_state.border_box_height());
  168. });
  169. row_state.set_content_height(tallest_cell_height);
  170. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  171. auto& cell_state = m_state.get_mutable(cell);
  172. cell_state.set_content_height(tallest_cell_height - cell_state.border_box_top() - cell_state.border_box_bottom());
  173. });
  174. if (use_auto_layout) {
  175. row_state.set_content_width(content_width);
  176. } else {
  177. auto& table_state = m_state.get_mutable(*table);
  178. row_state.set_content_width(table_state.content_width());
  179. }
  180. }
  181. float TableFormattingContext::automatic_content_height() const
  182. {
  183. return m_automatic_content_height;
  184. }
  185. }