TableFormattingContext.cpp 4.0 KB

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