From fae0b96fe48e1c0dbb7cad553b730852ca1aee6c Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Sun, 4 Dec 2022 20:02:28 +0300 Subject: [PATCH] LibWeb: Add vertical-align support for table cells --- .../LibWeb/Layout/TableFormattingContext.cpp | 29 +++++++++++++++++-- .../LibWeb/Layout/TableFormattingContext.h | 2 ++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp index 676468b85d..548e898b07 100644 --- a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp @@ -251,8 +251,11 @@ void TableFormattingContext::run(Box const& box, LayoutMode, AvailableSpace cons BlockFormattingContext::compute_height(cell.box, AvailableSpace(AvailableSize::make_indefinite(), AvailableSize::make_indefinite())); - Row& row = m_rows[cell.row_index]; + cell.baseline = box_baseline(m_state, cell.box); + + auto& row = m_rows[cell.row_index]; row.used_width = max(row.used_width, cell_state.border_box_height()); + row.baseline = max(row.baseline, cell.baseline); } for (size_t y = 0; y < m_rows.size(); y++) { @@ -293,7 +296,29 @@ void TableFormattingContext::run(Box const& box, LayoutMode, AvailableSpace cons for (auto& cell : m_cells) { auto& cell_state = m_state.get_mutable(cell.box); auto& row_state = m_state.get(m_rows[cell.row_index].box); - cell_state.set_content_height(row_state.content_height() - cell_state.border_box_top() - cell_state.border_box_bottom()); + float const cell_border_box_height = cell_state.content_height() + cell_state.border_box_top() + cell_state.border_box_bottom(); + float const row_content_height = row_state.content_height(); + auto const& vertical_align = cell.box.computed_values().vertical_align(); + if (vertical_align.has()) { + switch (vertical_align.get()) { + case CSS::VerticalAlign::Middle: { + cell_state.padding_top += (row_content_height - cell_border_box_height) / 2; + cell_state.padding_bottom += (row_content_height - cell_border_box_height) / 2; + break; + } + // FIXME: implement actual 'top' and 'bottom' support instead of fall back to 'baseline' + case CSS::VerticalAlign::Top: + case CSS::VerticalAlign::Bottom: + case CSS::VerticalAlign::Baseline: { + cell_state.padding_top += m_rows[cell.row_index].baseline - cell.baseline; + cell_state.padding_bottom += row_content_height - cell_border_box_height; + break; + } + default: + VERIFY_NOT_REACHED(); + } + } + cell_state.offset = row_state.offset.translated(cell_state.border_box_left() + m_columns[cell.column_index].left_offset, cell_state.border_box_top()); } diff --git a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h index 4df2ac97ad..216b440b2f 100644 --- a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h @@ -38,6 +38,7 @@ private: struct Row { Box& box; float used_width { 0 }; + float baseline { 0 }; }; struct Cell { @@ -46,6 +47,7 @@ private: size_t row_index; size_t column_span; size_t raw_span; + float baseline { 0 }; }; Vector m_cells;