From 38d355eedebfffc12e78cd1f14904aa40d4c5359 Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Mon, 2 Sep 2024 12:49:11 +0200 Subject: [PATCH] wip --- src/web/vaev-layout/table.cpp | 213 +++++++++++++++++++++------------- 1 file changed, 135 insertions(+), 78 deletions(-) diff --git a/src/web/vaev-layout/table.cpp b/src/web/vaev-layout/table.cpp index 7f24065..2b1dda7 100644 --- a/src/web/vaev-layout/table.cpp +++ b/src/web/vaev-layout/table.cpp @@ -4,93 +4,144 @@ namespace Vaev::Layout { -struct TableColumn { - Px width; +struct TableFormatingContext { + struct Col { + Px width; - void repr(Io::Emit &e) const { - e("(column {})", width); - } -}; + void repr(Io::Emit &e) const { + e("(column {})", width); + } + }; -static void _collectColumnSize(Tree &t, Frag &f, Input input, Vec &cols, usize col = 0) { - auto display = f.style->display; - if (display == Display::TABLE_CELL) { - Input childInput = { - .commit = Commit::NO, - .containingBlock = input.containingBlock - }; + struct Row { + Px height; - if (cols.len() <= col) { - cols.pushBack({ - .width = layout(t, f, childInput).size.width, - }); - } else { - cols[col].width = max( - cols[col].width, - layout(t, f, childInput).size.width - ); + void repr(Io::Emit &e) const { + e("(row {})", height); + } + }; + + // MARK: Columns ----------------------------------------------------------- + + Vec _cols; + Px _width; + + void _collectColumnsSizeInner(Tree &t, Frag &f, Input input, usize col) { + auto display = f.style->display; + if (display == Display::TABLE_CELL) { + Input childInput = { + .commit = Commit::NO, + .containingBlock = input.containingBlock + }; + + auto childOutput = layout(t, f, childInput); + + if (_cols.len() <= col) { + _cols.pushBack({ + .width = childOutput.size.width, + }); + } else { + _cols[col].width = max( + _cols[col].width, + childOutput.size.width + ); + } + return; + } + + for (auto &c : f.children()) { + Input childInput = { + .commit = Commit::NO, + .containingBlock = input.containingBlock, + }; + + if (display.isTableGroup() or display == Display::TABLE) { + _collectColumnsSizeInner(t, c, childInput, 0); + continue; + } + + if (display == Display::TABLE_ROW) { + _collectColumnsSizeInner(t, c, childInput, col++); + continue; + } } - return; } - for (auto &c : f.children()) { - Input childInput = { - .commit = Commit::NO, - .containingBlock = input.containingBlock, - }; + void _collectColumnsSize(Tree &t, Frag &f, Input input) { + _cols.clear(); + _collectColumnsSizeInner(t, f, input, 0); + + _width = Px{0}; + for (auto &c : _cols) + _width += c.width; + } - if (display.isTableGroup() or display == Display::TABLE) { - _collectColumnSize(t, c, childInput, cols); - continue; + // MARK: Rows -------------------------------------------------------------- + + Vec _rows; + Px _height; + + Px _collectRowSize(Tree &t, Frag &f, Input input) { + auto display = f.style->display; + + if (display == Display::TABLE_CELL) { + Input childInput = { + .commit = Commit::NO, + .containingBlock = input.containingBlock, + }; + return layout(t, f, childInput).size.height; } - if (display == Display::TABLE_ROW) { - _collectColumnSize(t, c, childInput, cols, col++); - continue; + Px res = Px{0}; + for (auto &c : f.children()) { + Input childInput = { + .commit = Commit::NO, + .availableSpace = Px{0}, + .containingBlock = input.containingBlock, + }; + + res = max(res, _collectRowSize(t, c, childInput)); } + + return res; } -} -static Px _collectRowHeight(Tree &t, Frag &f, Input input) { - auto display = f.style->display; + void _collectRowsSizeInner(Tree &t, Frag &f, Input input) { + if (not f.style->display.isTableGroup()) { + _rows.pushBack(Row{_collectRowSize(t, f, input)}); + return; + } - if (display == Display::TABLE_CELL) { - Input childInput = { - .commit = Commit::NO, - .containingBlock = input.containingBlock, - }; - return layout(t, f, childInput).size.height; + for (auto &c : f.children()) { + Input childInput = { + .commit = Commit::NO, + .availableSpace = Px{0}, + .containingBlock = input.containingBlock, + }; + + _collectRowsSizeInner(t, c, childInput); + } } - Px res = Px{0}; - for (auto &c : f.children()) { - Input childInput = { - .commit = Commit::NO, - .availableSpace = Px{0}, - .containingBlock = input.containingBlock, - }; + void _collectRowsSize(Tree &t, Frag &f, Input input) { + _rows.clear(); + _collectRowsSizeInner(t, f, input); - res = max(res, _collectRowHeight(t, c, childInput)); + _height = Px{0}; + for (auto &r : _rows) + _height += r.height; } - return res; -} + // MARK: Layout ------------------------------------------------------------ -static Px _collectRowsHeight(Tree &t, Frag &f, Input input) { - Px res = Px{0}; - for (auto &c : f.children()) { - Input childInput = { - .commit = Commit::NO, - .availableSpace = Px{0}, - .containingBlock = input.containingBlock, - }; - res += _collectRowHeight(t, c, childInput); + void _layoutRow(Tree &t, Frag &f, Input input) { } - return res; -} + void _layoutGroup(Tree &t, Frag &f, Input input) { + } +}; -static Output _placeColumns(Tree &t, Frag &f, Input input, Vec &cols) { +static Output _rowLayout(Tree &t, Frag &f, Input input, Vec &cols) { Px inlineSize = {}; Px rowBlockSize = _collectRowHeight(t, f, input); @@ -112,10 +163,12 @@ static Output _placeColumns(Tree &t, Frag &f, Input input, Vec &col childInput ); - c.layout.position = { - inlineSize, - Px{0}, - }; + if (input.commit == Commit::YES) { + c.layout.position = { + inlineSize, + Px{0}, + }; + } inlineSize += cols[i].width; } @@ -150,10 +203,10 @@ static Output _placeRows(Tree &t, Frag &f, Input input, Vec &cols) else childBlockSize = _collectRowsHeight(t, c, childInput); - input.availableSpace.height = childBlockSize; + childInput.knownSize.height = childBlockSize; if (display == Display::TABLE_ROW) { - _placeColumns(t, c, input, cols); + _rowLayout(t, c, input, cols); } else if (display == Display::TABLE_CAPTION) { layout(t, c, childInput); } else if (display.isTableGroup()) { @@ -162,10 +215,12 @@ static Output _placeRows(Tree &t, Frag &f, Input input, Vec &cols) layout(t, c, childInput); } - c.layout.position = { - Px{0}, - blockSize, - }; + if (input.commit == Commit::YES) { + c.layout.position = { + Px{0}, + blockSize, + }; + } blockSize += childBlockSize; } @@ -184,9 +239,11 @@ static Output _placeRows(Tree &t, Frag &f, Input input, Vec &cols) } Output tableLayout(Tree &t, Frag &f, Input input) { - Vec cols; - _collectColumnSize(t, f, input, cols); - return _placeRows(t, f, input, cols); + TableFormatingContext ctx; + ctx._collectColumnsSize(t, f, input); + ctx._collectRowsSize(t, f, input); + + return _placeRows(t, f, input); } } // namespace Vaev::Layout