Adapt table implementation from crossink
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "Page.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <Logging.h>
|
||||
#include <Serialization.h>
|
||||
|
||||
@@ -48,6 +49,135 @@ std::unique_ptr<PageImage> PageImage::deserialize(FsFile& file) {
|
||||
return std::unique_ptr<PageImage>(new PageImage(std::move(ib), xPos, yPos));
|
||||
}
|
||||
|
||||
void PageTableFragment::render(GfxRenderer& renderer, const int fontId, const int xOffset, const int yOffset) {
|
||||
const int drawX = xPos + xOffset;
|
||||
const int drawY = yPos + yOffset;
|
||||
|
||||
// Outer border
|
||||
renderer.drawRect(drawX, drawY, totalWidth, totalHeight, true);
|
||||
|
||||
// Vertical column separators
|
||||
int colX = drawX;
|
||||
for (uint8_t c = 0; c < columnCount - 1; c++) {
|
||||
colX += colWidths[c];
|
||||
renderer.drawLine(colX, drawY, colX, drawY + totalHeight - 1, true);
|
||||
}
|
||||
|
||||
// Rows: text content + horizontal separators
|
||||
int rowY = drawY;
|
||||
for (size_t r = 0; r < rows.size(); r++) {
|
||||
const TableRow& row = rows[r];
|
||||
int cellX = drawX;
|
||||
for (uint8_t c = 0; c < columnCount && c < static_cast<uint8_t>(row.cells.size()); c++) {
|
||||
const TableCell& cell = row.cells[c];
|
||||
int lineY = rowY + TABLE_CELL_PADDING;
|
||||
for (const auto& line : cell.lines) {
|
||||
line->render(renderer, fontId, cellX + TABLE_CELL_PADDING, lineY);
|
||||
lineY += renderer.getLineHeight(fontId);
|
||||
}
|
||||
cellX += colWidths[c];
|
||||
}
|
||||
rowY += row.height;
|
||||
// Draw horizontal separator (skip after last row — outer border covers it)
|
||||
if (r + 1 < rows.size()) {
|
||||
const int sepLineWidth = row.isHeaderRow ? 2 : 1;
|
||||
renderer.drawLine(drawX, rowY, drawX + totalWidth - 1, rowY, sepLineWidth, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool PageTableFragment::serialize(FsFile& file) {
|
||||
serialization::writePod(file, xPos);
|
||||
serialization::writePod(file, yPos);
|
||||
serialization::writePod(file, columnCount);
|
||||
serialization::writePod(file, totalWidth);
|
||||
serialization::writePod(file, totalHeight);
|
||||
for (uint8_t c = 0; c < MAX_TABLE_COLS; c++) {
|
||||
serialization::writePod(file, colWidths[c]);
|
||||
}
|
||||
const uint16_t rowCount = static_cast<uint16_t>(rows.size());
|
||||
serialization::writePod(file, rowCount);
|
||||
for (const auto& row : rows) {
|
||||
serialization::writePod(file, row.height);
|
||||
serialization::writePod(file, row.isHeaderRow);
|
||||
const uint8_t cellCount = static_cast<uint8_t>(row.cells.size());
|
||||
serialization::writePod(file, cellCount);
|
||||
for (const auto& cell : row.cells) {
|
||||
serialization::writePod(file, cell.isHeader);
|
||||
const uint8_t lineCount = static_cast<uint8_t>(cell.lines.size());
|
||||
serialization::writePod(file, lineCount);
|
||||
for (const auto& line : cell.lines) {
|
||||
if (!line->serialize(file)) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<PageTableFragment> PageTableFragment::deserialize(FsFile& file) {
|
||||
int16_t xPos, yPos;
|
||||
serialization::readPod(file, xPos);
|
||||
serialization::readPod(file, yPos);
|
||||
|
||||
uint8_t columnCount;
|
||||
uint16_t totalWidth, totalHeight;
|
||||
serialization::readPod(file, columnCount);
|
||||
serialization::readPod(file, totalWidth);
|
||||
serialization::readPod(file, totalHeight);
|
||||
|
||||
if (columnCount == 0 || columnCount > MAX_TABLE_COLS) {
|
||||
LOG_ERR("PGE", "TableFragment: invalid columnCount %u", columnCount);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::array<uint16_t, MAX_TABLE_COLS> colWidths = {};
|
||||
for (uint8_t c = 0; c < MAX_TABLE_COLS; c++) {
|
||||
serialization::readPod(file, colWidths[c]);
|
||||
}
|
||||
|
||||
uint16_t rowCount;
|
||||
serialization::readPod(file, rowCount);
|
||||
if (rowCount > MAX_TABLE_ROWS) {
|
||||
LOG_ERR("PGE", "TableFragment: invalid rowCount %u", rowCount);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<TableRow> rows;
|
||||
rows.reserve(rowCount);
|
||||
for (uint16_t r = 0; r < rowCount; r++) {
|
||||
TableRow row;
|
||||
serialization::readPod(file, row.height);
|
||||
serialization::readPod(file, row.isHeaderRow);
|
||||
uint8_t cellCount;
|
||||
serialization::readPod(file, cellCount);
|
||||
if (cellCount > MAX_TABLE_COLS) {
|
||||
LOG_ERR("PGE", "TableFragment: invalid cellCount %u in row %u", cellCount, r);
|
||||
return nullptr;
|
||||
}
|
||||
row.cells.reserve(cellCount);
|
||||
for (uint8_t c = 0; c < cellCount; c++) {
|
||||
TableCell cell;
|
||||
serialization::readPod(file, cell.isHeader);
|
||||
uint8_t lineCount;
|
||||
serialization::readPod(file, lineCount);
|
||||
cell.lines.reserve(lineCount);
|
||||
for (uint8_t l = 0; l < lineCount; l++) {
|
||||
auto tb = TextBlock::deserialize(file);
|
||||
if (!tb) {
|
||||
LOG_ERR("PGE", "TableFragment: TextBlock deserialize failed at row %u cell %u line %u", r, c, l);
|
||||
return nullptr;
|
||||
}
|
||||
cell.lines.push_back(std::move(tb));
|
||||
}
|
||||
row.cells.push_back(std::move(cell));
|
||||
}
|
||||
rows.push_back(std::move(row));
|
||||
}
|
||||
|
||||
return std::unique_ptr<PageTableFragment>(
|
||||
new PageTableFragment(columnCount, totalWidth, totalHeight, colWidths, std::move(rows), xPos, yPos));
|
||||
}
|
||||
|
||||
void Page::render(GfxRenderer& renderer, const int fontId, const int xOffset, const int yOffset) const {
|
||||
for (auto& element : elements) {
|
||||
element->render(renderer, fontId, xOffset, yOffset);
|
||||
@@ -99,6 +229,10 @@ std::unique_ptr<Page> Page::deserialize(FsFile& file) {
|
||||
} else if (tag == TAG_PageImage) {
|
||||
auto pi = PageImage::deserialize(file);
|
||||
page->elements.push_back(std::move(pi));
|
||||
} else if (tag == TAG_PageTable) {
|
||||
auto pt = PageTableFragment::deserialize(file);
|
||||
if (!pt) return nullptr;
|
||||
page->elements.push_back(std::move(pt));
|
||||
} else {
|
||||
LOG_ERR("PGE", "Deserialization failed: Unknown tag %u", tag);
|
||||
return nullptr;
|
||||
|
||||
Reference in New Issue
Block a user