Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Sep 2, 2024
1 parent a2aa14d commit 38d355e
Showing 1 changed file with 135 additions and 78 deletions.
213 changes: 135 additions & 78 deletions src/web/vaev-layout/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TableColumn> &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<Col> _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<Row> _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<TableColumn> &cols) {
static Output _rowLayout(Tree &t, Frag &f, Input input, Vec<TableColumn> &cols) {
Px inlineSize = {};
Px rowBlockSize = _collectRowHeight(t, f, input);

Expand All @@ -112,10 +163,12 @@ static Output _placeColumns(Tree &t, Frag &f, Input input, Vec<TableColumn> &col
childInput
);

c.layout.position = {
inlineSize,
Px{0},
};
if (input.commit == Commit::YES) {
c.layout.position = {
inlineSize,
Px{0},
};
}

inlineSize += cols[i].width;
}
Expand Down Expand Up @@ -150,10 +203,10 @@ static Output _placeRows(Tree &t, Frag &f, Input input, Vec<TableColumn> &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()) {
Expand All @@ -162,10 +215,12 @@ static Output _placeRows(Tree &t, Frag &f, Input input, Vec<TableColumn> &cols)
layout(t, c, childInput);
}

c.layout.position = {
Px{0},
blockSize,
};
if (input.commit == Commit::YES) {
c.layout.position = {
Px{0},
blockSize,
};
}

blockSize += childBlockSize;
}
Expand All @@ -184,9 +239,11 @@ static Output _placeRows(Tree &t, Frag &f, Input input, Vec<TableColumn> &cols)
}

Output tableLayout(Tree &t, Frag &f, Input input) {
Vec<TableColumn> 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

0 comments on commit 38d355e

Please sign in to comment.