TableFormattingContext.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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()
  21. {
  22. }
  23. void TableFormattingContext::run(Box const& box, LayoutMode)
  24. {
  25. auto& box_state = m_state.ensure(box);
  26. compute_width(box);
  27. float total_content_width = 0;
  28. float total_content_height = 0;
  29. box.for_each_child_of_type<TableRowGroupBox>([&](auto& row_group_box) {
  30. auto& row_group_box_state = m_state.ensure(row_group_box);
  31. compute_width(row_group_box);
  32. auto column_count = row_group_box.column_count();
  33. Vector<float> column_widths;
  34. column_widths.resize(column_count);
  35. row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
  36. calculate_column_widths(row, column_widths);
  37. });
  38. float content_width = 0;
  39. float content_height = 0;
  40. row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
  41. auto& row_state = m_state.ensure(row);
  42. row_state.offset = { 0, content_height };
  43. layout_row(row, column_widths);
  44. content_width = max(content_width, row_state.content_width);
  45. content_height += row_state.content_height;
  46. });
  47. 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())
  48. row_group_box_state.content_width = content_width;
  49. row_group_box_state.content_height = content_height;
  50. row_group_box_state.offset = { 0, total_content_height };
  51. total_content_height += content_height;
  52. total_content_width = max(total_content_width, row_group_box_state.content_width);
  53. });
  54. if (box.computed_values().width().has_value() && box.computed_values().width()->is_length() && box.computed_values().width()->length().is_auto())
  55. box_state.content_width = total_content_width;
  56. // FIXME: This is a total hack, we should respect the 'height' property.
  57. box_state.content_height = total_content_height;
  58. }
  59. void TableFormattingContext::calculate_column_widths(Box const& row, Vector<float>& column_widths)
  60. {
  61. m_state.ensure(row);
  62. size_t column_index = 0;
  63. auto* table = row.first_ancestor_of_type<TableBox>();
  64. bool use_auto_layout = !table || (!table->computed_values().width().has_value() || (table->computed_values().width()->is_length() && table->computed_values().width()->length().is_auto()));
  65. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  66. auto& cell_state = m_state.ensure(cell);
  67. compute_width(cell);
  68. if (use_auto_layout) {
  69. (void)layout_inside(cell, LayoutMode::OnlyRequiredLineBreaks);
  70. } else {
  71. (void)layout_inside(cell, LayoutMode::Default);
  72. }
  73. column_widths[column_index] = max(column_widths[column_index], cell_state.content_width);
  74. column_index += cell.colspan();
  75. });
  76. }
  77. void TableFormattingContext::layout_row(Box const& row, Vector<float>& column_widths)
  78. {
  79. auto& row_state = m_state.ensure(row);
  80. size_t column_index = 0;
  81. float tallest_cell_height = 0;
  82. float content_width = 0;
  83. auto* table = row.first_ancestor_of_type<TableBox>();
  84. bool use_auto_layout = !table || (!table->computed_values().width().has_value() || (table->computed_values().width()->is_length() && table->computed_values().width()->length().is_auto()));
  85. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  86. auto& cell_state = m_state.ensure(cell);
  87. cell_state.offset = row_state.offset.translated(content_width, 0);
  88. // Layout the cell contents a second time, now that we know its final width.
  89. if (use_auto_layout) {
  90. (void)layout_inside(cell, LayoutMode::OnlyRequiredLineBreaks);
  91. } else {
  92. (void)layout_inside(cell, LayoutMode::Default);
  93. }
  94. size_t cell_colspan = cell.colspan();
  95. for (size_t i = 0; i < cell_colspan; ++i)
  96. content_width += column_widths[column_index++];
  97. tallest_cell_height = max(tallest_cell_height, cell_state.content_height);
  98. });
  99. if (use_auto_layout) {
  100. row_state.content_width = content_width;
  101. } else {
  102. auto& table_state = m_state.ensure(*table);
  103. row_state.content_width = table_state.content_width;
  104. }
  105. row_state.content_height = tallest_cell_height;
  106. }
  107. }