Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issues #2783 #1943 #1509 #2017 and #2298 #2785

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/DocumentContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DocumentContext extends EventEmitter {
this.addPage(pageSize);
}

beginColumnGroup() {
beginColumnGroup(marginXTopParent) {
this.snapshots.push({
x: this.x,
y: this.y,
Expand All @@ -41,6 +41,13 @@ class DocumentContext extends EventEmitter {
});

this.lastColumnWidth = 0;
if (marginXTopParent) {
this.marginXTopParent = marginXTopParent;
}
}

resetMarginXTopParent () {
this.marginXTopParent = null;
}

beginColumn(width, offset, endingCell) {
Expand Down Expand Up @@ -128,16 +135,21 @@ class DocumentContext extends EventEmitter {
initializePage() {
this.y = this.pageMargins.top;
this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
this.pageSnapshot().availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
const {pageCtx, isSnapshot} = this.pageSnapshot();
pageCtx.availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
if (isSnapshot && this.marginXTopParent) {
pageCtx.availableWidth -= this.marginXTopParent[0];
pageCtx.availableWidth -= this.marginXTopParent[1];
}
}

pageSnapshot() {
if (this.snapshots[0]) {
return this.snapshots[0];
} else {
return this;
}
}
if (this.snapshots[0]) {
return {pageCtx: this.snapshots[0], isSnapshot: true};
} else {
return {pageCtx: this, isSnapshot: false};
}
}

moveTo(x, y) {
if (x !== undefined && x !== null) {
Expand Down
47 changes: 40 additions & 7 deletions src/LayoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class LayoutBuilder {
this.pageMargins = pageMargins;
this.svgMeasure = svgMeasure;
this.tableLayouts = {};
this.nestedLevel = 0;
}

registerTableLayouts(tableLayouts) {
Expand Down Expand Up @@ -369,31 +370,50 @@ class LayoutBuilder {
}
}

if (margin) {
const isDetachedBlock = node.relativePosition || node.absolutePosition;

// Detached nodes have no margins, their position is only determined by 'x' and 'y'
if (margin && !isDetachedBlock) {
const availableHeight = this.writer.context().availableHeight;
// If top margin is bigger than available space, move to next page
// Necessary for nodes inside tables
if (availableHeight - margin[1] < 0) {
// Consume the whole available space
this.writer.context().moveDown(availableHeight);
this.writer.moveToNextPage(node.pageOrientation);
/**
* TODO - Something to consider:
* Right now the node starts at the top of next page (after header)
* Another option would be to apply just the top margin that has not been consumed in the page before
* It would something like: this.write.context().moveDown(margin[1] - availableHeight)
*/
} else {
this.writer.context().moveDown(margin[1]);
this.writer.context().addMargin(margin[0], margin[2]);
}
// Apply lateral margins
this.writer.context().addMargin(margin[0], margin[2]);
}
callback();

if (margin) {
// Detached nodes have no margins, their position is only determined by 'x' and 'y'
if (margin && !isDetachedBlock) {
const availableHeight = this.writer.context().availableHeight;
// If bottom margin is bigger than available space, move to next page
// Necessary for nodes inside tables
if (availableHeight - margin[3] < 0) {
this.writer.context().moveDown(availableHeight);
this.writer.moveToNextPage(node.pageOrientation);
/**
* TODO - Something to consider:
* Right now next node starts at the top of next page (after header)
* Another option would be to apply the bottom margin that has not been consumed in the next page?
* It would something like: this.write.context().moveDown(margin[3] - availableHeight)
*/
} else {
this.writer.context().addMargin(-margin[0], -margin[2]);
this.writer.context().moveDown(margin[3]);
}
// Apply lateral margins
this.writer.context().addMargin(-margin[0], -margin[2]);
}

if (node.pageBreak === 'after') {
Expand Down Expand Up @@ -482,6 +502,7 @@ class LayoutBuilder {

// columns
processColumns(columnNode) {
this.nestedLevel++;
let columns = columnNode.columns;
let availableWidth = this.writer.context().availableWidth;
let gaps = gapArray(columnNode._gap);
Expand All @@ -492,12 +513,16 @@ class LayoutBuilder {

ColumnCalculator.buildColumnWidths(columns, availableWidth);
let result = this.processRow({
marginX: columnNode._margin ? [columnNode._margin[0], columnNode._margin[2]] : [0, 0],
cells: columns,
widths: columns,
gaps
});
addAll(columnNode.positions, result.positions);

this.nestedLevel--;
if (this.nestedLevel === 0) {
this.writer.context().resetMarginXTopParent();
}
function gapArray(gap) {
if (!gap) {
return null;
Expand Down Expand Up @@ -529,7 +554,7 @@ class LayoutBuilder {
return null;
}

processRow({dontBreakRows = false, rowsWithoutPageBreak = 0, cells, widths, gaps, tableBody, rowIndex, height}) {
processRow({marginX = [0, 0], dontBreakRows = false, rowsWithoutPageBreak = 0, cells, widths, gaps, tableBody, rowIndex, height}) {
const updatePageBreakData = (page, prevY) => {
let pageDesc;
// Find page break data for this row and page
Expand Down Expand Up @@ -577,8 +602,10 @@ class LayoutBuilder {
}

widths = widths || cells;
// Use the marginX if we are in a top level table/column (not nested)
const marginXParent = this.nestedLevel === 1 ? marginX : null;

this.writer.context().beginColumnGroup();
this.writer.context().beginColumnGroup(marginXParent);

for (let i = 0, l = cells.length; i < l; i++) {
let column = cells[i];
Expand Down Expand Up @@ -740,6 +767,7 @@ class LayoutBuilder {

// tables
processTable(tableNode) {
this.nestedLevel++;
let processor = new TableProcessor(tableNode);

processor.beginTable(this.writer);
Expand Down Expand Up @@ -772,6 +800,7 @@ class LayoutBuilder {
}

let result = this.processRow({
marginX: tableNode._margin ? [tableNode._margin[0], tableNode._margin[2]] : [0, 0],
dontBreakRows: processor.dontBreakRows,
rowsWithoutPageBreak: processor.rowsWithoutPageBreak,
cells: tableNode.table.body[i],
Expand All @@ -788,6 +817,10 @@ class LayoutBuilder {
}

processor.endTable(this.writer);
this.nestedLevel--;
if (this.nestedLevel === 0) {
this.writer.context().resetMarginXTopParent();
}
}

// leafs (texts)
Expand Down
Loading