TableFormattingContext.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/InlineFormattingContext.h>
  29. #include <LibWeb/Layout/LayoutBlock.h>
  30. #include <LibWeb/Layout/LayoutBox.h>
  31. #include <LibWeb/Layout/LayoutTable.h>
  32. #include <LibWeb/Layout/LayoutTableCell.h>
  33. #include <LibWeb/Layout/LayoutTableRow.h>
  34. #include <LibWeb/Layout/LayoutTableRowGroup.h>
  35. #include <LibWeb/Layout/TableFormattingContext.h>
  36. #include <LibWeb/Page/Frame.h>
  37. namespace Web::Layout {
  38. TableFormattingContext::TableFormattingContext(LayoutBox& context_box)
  39. : BlockFormattingContext(context_box)
  40. {
  41. }
  42. TableFormattingContext::~TableFormattingContext()
  43. {
  44. }
  45. void TableFormattingContext::run(LayoutMode)
  46. {
  47. compute_width(context_box());
  48. context_box().for_each_child_of_type<LayoutTableRowGroup>([&](auto& box) {
  49. compute_width(box);
  50. auto column_count = box.column_count();
  51. Vector<float> column_widths;
  52. column_widths.resize(column_count);
  53. box.template for_each_child_of_type<LayoutTableRow>([&](auto& row) {
  54. calculate_column_widths(row, column_widths);
  55. });
  56. float content_height = 0;
  57. box.template for_each_child_of_type<LayoutTableRow>([&](auto& row) {
  58. row.set_offset(0, content_height);
  59. layout_row(row, column_widths);
  60. content_height += row.height();
  61. });
  62. box.set_height(content_height);
  63. });
  64. compute_height(context_box());
  65. }
  66. void TableFormattingContext::calculate_column_widths(LayoutBox& row, Vector<float>& column_widths)
  67. {
  68. size_t column_index = 0;
  69. auto* table = row.first_ancestor_of_type<LayoutTable>();
  70. bool use_auto_layout = !table || table->style().width().is_undefined_or_auto();
  71. row.for_each_child_of_type<LayoutTableCell>([&](auto& cell) {
  72. compute_width(cell);
  73. if (use_auto_layout) {
  74. layout_inside(cell, LayoutMode::OnlyRequiredLineBreaks);
  75. } else {
  76. layout_inside(cell, LayoutMode::Default);
  77. }
  78. column_widths[column_index] = max(column_widths[column_index], cell.width());
  79. column_index += cell.colspan();
  80. });
  81. }
  82. void TableFormattingContext::layout_row(LayoutBox& row, Vector<float>& column_widths)
  83. {
  84. size_t column_index = 0;
  85. float tallest_cell_height = 0;
  86. float content_width = 0;
  87. auto* table = row.first_ancestor_of_type<LayoutTable>();
  88. bool use_auto_layout = !table || table->style().width().is_undefined_or_auto();
  89. row.for_each_child_of_type<LayoutTableCell>([&](auto& cell) {
  90. cell.set_offset(row.effective_offset().translated(content_width, 0));
  91. // Layout the cell contents a second time, now that we know its final width.
  92. if (use_auto_layout) {
  93. layout_inside(cell, LayoutMode::OnlyRequiredLineBreaks);
  94. } else {
  95. layout_inside(cell, LayoutMode::Default);
  96. }
  97. size_t cell_colspan = cell.colspan();
  98. for (size_t i = 0; i < cell_colspan; ++i)
  99. content_width += column_widths[column_index++];
  100. tallest_cell_height = max(tallest_cell_height, cell.height());
  101. });
  102. if (use_auto_layout) {
  103. row.set_width(content_width);
  104. } else {
  105. row.set_width(table->width());
  106. }
  107. row.set_height(tallest_cell_height);
  108. }
  109. }