Skip to content

Commit

Permalink
Fixess issues 995 and 2490 (#2790)
Browse files Browse the repository at this point in the history
Co-authored-by: fe-cj <fe-cj@users.noreply.github.com>
  • Loading branch information
fe-cj and fe-cj authored Sep 25, 2024
1 parent 1637fab commit 348f76d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/ElementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,12 @@ class ElementWriter extends EventEmitter {
}
}

addVector(vector, ignoreContextX, ignoreContextY, index) {
addVector(vector, ignoreContextX, ignoreContextY, index, forcePage) {
let context = this.context();
let page = context.getCurrentPage();
if (isNumber(forcePage)) {
page = context.pages[forcePage];
}
let position = this.getCurrentPositionOnPage();

if (page) {
Expand Down
4 changes: 2 additions & 2 deletions src/PageElementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class PageElementWriter extends ElementWriter {
return this._fitOnPage(() => super.addAttachment(attachment, index));
}

addVector(vector, ignoreContextX, ignoreContextY, index) {
return super.addVector(vector, ignoreContextX, ignoreContextY, index);
addVector(vector, ignoreContextX, ignoreContextY, index, forcePage) {
return super.addVector(vector, ignoreContextX, ignoreContextY, index, forcePage);
}

beginClip(width, height) {
Expand Down
52 changes: 37 additions & 15 deletions src/TableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,18 @@ class TableProcessor {

this.dontBreakRows = tableNode.table.dontBreakRows || false;

if (this.rowsWithoutPageBreak) {
if (this.rowsWithoutPageBreak || this.dontBreakRows) {
writer.beginUnbreakableBlock();
// Draw the top border of the table
this.drawHorizontalLine(0, writer);
if (this.rowsWithoutPageBreak && this.dontBreakRows) {
// We just increase the value of transactionLevel
writer.beginUnbreakableBlock();
}
}

// update the border properties of all cells before drawing any lines
prepareCellBorders(this.tableNode.table.body);

this.drawHorizontalLine(0, writer);
}

onRowBreak(rowIndex, writer) {
Expand All @@ -151,7 +155,12 @@ class TableProcessor {

this.rowCallback = this.onRowBreak(rowIndex, writer);
writer.addListener('pageChanged', this.rowCallback);
if (this.dontBreakRows) {
if (rowIndex == 0 && !this.dontBreakRows && !this.rowsWithoutPageBreak) {
// We store the 'y' to draw later and if necessary the top border of the table
this._tableTopBorderY = writer.context().y;
writer.context().moveDown(this.topLineWidth);
}
if (this.dontBreakRows && rowIndex > 0) {
writer.beginUnbreakableBlock();
}
this.rowTopY = writer.context().y;
Expand All @@ -162,7 +171,7 @@ class TableProcessor {
writer.context().moveDown(this.rowPaddingTop);
}

drawHorizontalLine(lineIndex, writer, overrideY) {
drawHorizontalLine(lineIndex, writer, overrideY, moveDown = true, forcePage) {
let lineWidth = this.layout.hLineWidth(lineIndex, this.tableNode);
if (lineWidth) {
let style = this.layout.hLineStyle(lineIndex, this.tableNode);
Expand Down Expand Up @@ -260,7 +269,7 @@ class TableProcessor {
lineWidth: lineWidth,
dash: dash,
lineColor: borderColor
}, false, overrideY);
}, false, isNumber(overrideY), null, forcePage);
currentLine = null;
borderColor = null;
cellAbove = null;
Expand All @@ -270,7 +279,9 @@ class TableProcessor {
}
}

writer.context().moveDown(lineWidth);
if (moveDown) {
writer.context().moveDown(lineWidth);
}
}
}

Expand Down Expand Up @@ -406,6 +417,15 @@ class TableProcessor {
ys[ys.length - 1].y1 = endingY;

let skipOrphanePadding = (ys[0].y1 - ys[0].y0 === this.rowPaddingTop);
if (rowIndex === 0 && !skipOrphanePadding && !this.rowsWithoutPageBreak && !this.dontBreakRows) {
// Draw the top border of the table
let pageTableStartedAt = null;
if (pageBreaks && pageBreaks.length > 0) {
// Get the page where table started at
pageTableStartedAt = pageBreaks[0].prevPage;
}
this.drawHorizontalLine(0, writer, this._tableTopBorderY, false, pageTableStartedAt);
}
for (let yi = (skipOrphanePadding ? 1 : 0), yl = ys.length; yi < yl; yi++) {
let willBreak = yi < ys.length - 1;
let rowBreakWithoutHeader = (yi > 0 && !this.headerRows);
Expand All @@ -425,6 +445,14 @@ class TableProcessor {
this.reservedAtBottom = 0;
}

// Draw horizontal lines before the vertical lines so they are not overridden
if (willBreak && this.layout.hLineWhenBroken !== false) {
this.drawHorizontalLine(rowIndex + 1, writer, y2);
}
if (rowBreakWithoutHeader && this.layout.hLineWhenBroken !== false) {
this.drawHorizontalLine(rowIndex, writer, y1);
}

for (let i = 0, l = xs.length; i < l; i++) {
let leftCellBorder = false;
let rightCellBorder = false;
Expand Down Expand Up @@ -511,13 +539,6 @@ class TableProcessor {
}
}
}

if (willBreak && this.layout.hLineWhenBroken !== false) {
this.drawHorizontalLine(rowIndex + 1, writer, y2);
}
if (rowBreakWithoutHeader && this.layout.hLineWhenBroken !== false) {
this.drawHorizontalLine(rowIndex, writer, y1);
}
}

writer.context().page = endingPage;
Expand Down Expand Up @@ -556,7 +577,8 @@ class TableProcessor {

if (this.dontBreakRows) {
const pageChangedCallback = () => {
if (!this.headerRows && this.layout.hLineWhenBroken !== false) {
if (rowIndex > 0 && !this.headerRows && this.layout.hLineWhenBroken !== false) {
// Draw the top border of the row after a page break
this.drawHorizontalLine(rowIndex, writer);
}
};
Expand Down

0 comments on commit 348f76d

Please sign in to comment.