Skip to content

Commit

Permalink
backport PR #2790
Browse files Browse the repository at this point in the history
  • Loading branch information
liborm85 committed Sep 25, 2024
1 parent 4d8101f commit 37ca6e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/elementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,13 @@ ElementWriter.prototype.alignCanvas = function (node) {
}
};

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

if (page) {
offsetVector(vector, ignoreContextX ? 0 : context.x, ignoreContextY ? 0 : context.y);
Expand Down
4 changes: 2 additions & 2 deletions src/pageElementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ PageElementWriter.prototype.addQr = function (qr, index) {
});
};

PageElementWriter.prototype.addVector = function (vector, ignoreContextX, ignoreContextY, index) {
return this.writer.addVector(vector, ignoreContextX, ignoreContextY, index);
PageElementWriter.prototype.addVector = function (vector, ignoreContextX, ignoreContextY, index, forcePage) {
return this.writer.addVector(vector, ignoreContextX, ignoreContextY, index, forcePage);
};

PageElementWriter.prototype.beginClip = function (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 @@ -49,15 +49,19 @@ TableProcessor.prototype.beginTable = function (writer) {

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);

function getTableInnerContentWidth() {
var width = 0;

Expand Down Expand Up @@ -156,7 +160,12 @@ TableProcessor.prototype.beginRow = function (rowIndex, writer) {

this.rowCallback = this.onRowBreak(rowIndex, writer);
writer.tracker.startTracking('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 @@ -167,7 +176,7 @@ TableProcessor.prototype.beginRow = function (rowIndex, writer) {
writer.context().moveDown(this.rowPaddingTop);
};

TableProcessor.prototype.drawHorizontalLine = function (lineIndex, writer, overrideY) {
TableProcessor.prototype.drawHorizontalLine = function (lineIndex, writer, overrideY, moveDown = true, forcePage) {
var lineWidth = this.layout.hLineWidth(lineIndex, this.tableNode);
if (lineWidth) {
var style = this.layout.hLineStyle(lineIndex, this.tableNode);
Expand Down Expand Up @@ -266,7 +275,7 @@ TableProcessor.prototype.drawHorizontalLine = function (lineIndex, writer, overr
lineWidth: lineWidth,
dash: dash,
lineColor: borderColor
}, false, overrideY);
}, false, isNumber(overrideY), null, forcePage);
currentLine = null;
borderColor = null;
cellAbove = null;
Expand All @@ -276,7 +285,9 @@ TableProcessor.prototype.drawHorizontalLine = function (lineIndex, writer, overr
}
}

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

Expand Down Expand Up @@ -392,6 +403,15 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
ys[ys.length - 1].y1 = endingY;

var skipOrphanePadding = (ys[0].y1 - ys[0].y0 === this.rowPaddingTop);
if (rowIndex === 0 && !skipOrphanePadding && !this.rowsWithoutPageBreak && !this.dontBreakRows) {
// Draw the top border of the table
var 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 (var yi = (skipOrphanePadding ? 1 : 0), yl = ys.length; yi < yl; yi++) {
var willBreak = yi < ys.length - 1;
var rowBreakWithoutHeader = (yi > 0 && !this.headerRows);
Expand All @@ -411,6 +431,14 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
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 (i = 0, l = xs.length; i < l; i++) {
var leftCellBorder = false;
var rightCellBorder = false;
Expand Down Expand Up @@ -497,13 +525,6 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
}
}
}

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 @@ -542,7 +563,8 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
if (this.dontBreakRows) {
writer.tracker.auto('pageChanged',
function () {
if (!self.headerRows && self.layout.hLineWhenBroken !== false) {
if (rowIndex > 0 && !self.headerRows && self.layout.hLineWhenBroken !== false) {
// Draw the top border of the row after a page break
self.drawHorizontalLine(rowIndex, writer);
}
},
Expand Down

0 comments on commit 37ca6e4

Please sign in to comment.