TableFormattingContext.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/CSS/Length.h>
  27. #include <LibWeb/DOM/Node.h>
  28. #include <LibWeb/Layout/BlockBox.h>
  29. #include <LibWeb/Layout/Box.h>
  30. #include <LibWeb/Layout/InlineFormattingContext.h>
  31. #include <LibWeb/Layout/TableBox.h>
  32. #include <LibWeb/Layout/TableCellBox.h>
  33. #include <LibWeb/Layout/TableFormattingContext.h>
  34. #include <LibWeb/Layout/TableRowBox.h>
  35. #include <LibWeb/Layout/TableRowGroupBox.h>
  36. #include <LibWeb/Page/Frame.h>
  37. namespace Web::Layout {
  38. TableFormattingContext::TableFormattingContext(Box& context_box, FormattingContext* parent)
  39. : BlockFormattingContext(context_box, parent)
  40. {
  41. }
  42. TableFormattingContext::~TableFormattingContext()
  43. {
  44. }
  45. void TableFormattingContext::run(Box& box, LayoutMode)
  46. {
  47. compute_width(box);
  48. float total_content_height = 0;
  49. box.for_each_child_of_type<TableRowGroupBox>([&](auto& row_group_box) {
  50. compute_width(row_group_box);
  51. auto column_count = row_group_box.column_count();
  52. Vector<float> column_widths;
  53. column_widths.resize(column_count);
  54. row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
  55. calculate_column_widths(row, column_widths);
  56. });
  57. float content_height = 0;
  58. row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
  59. row.set_offset(0, content_height);
  60. layout_row(row, column_widths);
  61. content_height += row.height();
  62. });
  63. row_group_box.set_height(content_height);
  64. total_content_height += content_height;
  65. });
  66. // FIXME: This is a total hack, we should respect the 'height' property.
  67. box.set_height(total_content_height);
  68. }
  69. void TableFormattingContext::calculate_column_widths(Box& row, Vector<float>& column_widths)
  70. {
  71. size_t column_index = 0;
  72. auto* table = row.first_ancestor_of_type<TableBox>();
  73. bool use_auto_layout = !table || table->computed_values().width().is_undefined_or_auto();
  74. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  75. compute_width(cell);
  76. if (use_auto_layout) {
  77. layout_inside(cell, LayoutMode::OnlyRequiredLineBreaks);
  78. } else {
  79. layout_inside(cell, LayoutMode::Default);
  80. }
  81. column_widths[column_index] = max(column_widths[column_index], cell.width());
  82. column_index += cell.colspan();
  83. });
  84. }
  85. void TableFormattingContext::layout_row(Box& row, Vector<float>& column_widths)
  86. {
  87. size_t column_index = 0;
  88. float tallest_cell_height = 0;
  89. float content_width = 0;
  90. auto* table = row.first_ancestor_of_type<TableBox>();
  91. bool use_auto_layout = !table || table->computed_values().width().is_undefined_or_auto();
  92. row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
  93. cell.set_offset(row.effective_offset().translated(content_width, 0));
  94. // Layout the cell contents a second time, now that we know its final width.
  95. if (use_auto_layout) {
  96. layout_inside(cell, LayoutMode::OnlyRequiredLineBreaks);
  97. } else {
  98. layout_inside(cell, LayoutMode::Default);
  99. }
  100. size_t cell_colspan = cell.colspan();
  101. for (size_t i = 0; i < cell_colspan; ++i)
  102. content_width += column_widths[column_index++];
  103. tallest_cell_height = max(tallest_cell_height, cell.height());
  104. });
  105. if (use_auto_layout) {
  106. row.set_width(content_width);
  107. } else {
  108. row.set_width(table->width());
  109. }
  110. row.set_height(tallest_cell_height);
  111. }
  112. }