Skip to content

Commit

Permalink
backport PR #2762
Browse files Browse the repository at this point in the history
  • Loading branch information
liborm85 committed Aug 2, 2024
1 parent d8f0ff0 commit 95240e0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/columnCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var isString = require('./helpers').isString;

function buildColumnWidths(columns, availableWidth) {
function buildColumnWidths(columns, availableWidth, offsetTotal = 0, tableNode) {
var autoColumns = [],
autoMin = 0, autoMax = 0,
starColumns = [],
Expand All @@ -25,10 +25,31 @@ function buildColumnWidths(columns, availableWidth) {
}
});

fixedColumns.forEach(function (col) {
fixedColumns.forEach(function (col, colIndex) {
// width specified as %
if (isString(col.width) && /\d+%/.test(col.width)) {
col.width = parseFloat(col.width) * initial_availableWidth / 100;
// In tables we have to take into consideration the reserved width for paddings and borders
var reservedWidth = 0;
if (tableNode) {
var paddingLeft = tableNode._layout.paddingLeft(colIndex, tableNode);
var paddingRight = tableNode._layout.paddingRight(colIndex, tableNode);
var borderLeft = tableNode._layout.vLineWidth(colIndex, tableNode);
var borderRight = tableNode._layout.vLineWidth(colIndex + 1, tableNode);
if (colIndex === 0) {
// first column assumes whole borderLeft and half of border right
reservedWidth = paddingLeft + paddingRight + borderLeft + (borderRight / 2);

} else if (colIndex === fixedColumns.length - 1) {
// last column assumes whole borderRight and half of border left
reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + borderRight;

} else {
// Columns in the middle assume half of each border
reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + (borderRight / 2);
}
}
var totalAvailableWidth = initial_availableWidth + offsetTotal;
col.width = (parseFloat(col.width) * totalAvailableWidth / 100) - reservedWidth;
}
if (col.width < (col._minWidth) && col.elasticWidth) {
col._calcWidth = col._minWidth;
Expand Down
2 changes: 1 addition & 1 deletion src/tableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TableProcessor.prototype.beginTable = function (writer) {
this.layout = tableNode._layout;

availableWidth = writer.context().availableWidth - this.offsets.total;
ColumnCalculator.buildColumnWidths(tableNode.table.widths, availableWidth);
ColumnCalculator.buildColumnWidths(tableNode.table.widths, availableWidth, this.offsets.total, tableNode);

this.tableWidth = tableNode._offsets.total + getTableInnerContentWidth();
this.rowSpanData = prepareRowSpanData();
Expand Down
78 changes: 78 additions & 0 deletions tests/columnCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,83 @@ describe('ColumnCalculator', function () {
assert.equal(columns[0]._calcWidth, columns[0]._calcWidth);
assert.equal(columns[0]._calcWidth + columns[1]._calcWidth + columns[2]._calcWidth, 320);
});

it('should calculate widths of columns correctly', function () {
const availableWidth = 200;
const columns = [
{ width: '25%', _minWidth: 30, _maxWidth: 41 },
{ width: '50%', _minWidth: 31, _maxWidth: 42 },
{ width: '25%', _minWidth: 33, _maxWidth: 421 },
];

ColumnCalculator.buildColumnWidths(columns, availableWidth);
assert.equal(columns[0]._calcWidth, 50);
assert.equal(columns[1]._calcWidth, 100);
assert.equal(columns[2]._calcWidth, 50);
});

it('should calculate widths of the table columns correctly when using percentages', function () {
const availableWidth = 149;
const offsetTotal = 51;
const columns = [
{
width: "50%",
_minWidth: 22.27734375,
_maxWidth: 22.27734375,
},
{
width: "25%",
_minWidth: 22.27734375,
_maxWidth: 22.27734375,
},
{
width: "25%",
_minWidth: 22.27734375,
_maxWidth: 22.27734375,
},
];
const tableNode = {
table: {
widths: ['50%', '25%', '25%'],
body: [['50%', '25%', '25%']]
},
_layout: {
vLineWidth: function (i) {
if (i === 0) {
return 4;
}
if (i === 1) {
return 6;
}
if (i === 2) {
return 5;
}
if (i === 3) {
return 4;
}
},
paddingLeft: function(i) {
return i === 0 ? 5 : 3;
},
paddingRight: function() {
return 7;
}
}
};

ColumnCalculator.buildColumnWidths(columns, availableWidth, offsetTotal, tableNode);
// 200 Total available width (availableWidth + offsetTotal) === 149 + 51
// Fist column is 50% width of 200 === 100
// 100 - 4 borderLeft - 6/2 = 3 borderRight - 5 paddingLeft - 7 paddingRight
assert.equal(columns[0].width, 81);
// Second column has 25% width of 200 which is 50
// 50 - 3 (6/2) borderLeft - 2.5 (5/2) borderRight - 3 paddingLeft - 7 paddingRight
assert.equal(columns[1].width, 34.5);
// Third column has 25% width of 200 which is 50
// 50 - 2.5 (5/2) borderLeft - 4 borderRight - 3 paddingLeft - 7 paddingRight
assert.equal(columns[2].width, 33.5);
// The sum of all column width should be equal to totalAvailableWidth - offsetTotal
assert.equal(availableWidth, 81 + 34.5 + 33.5);
});
});
});

0 comments on commit 95240e0

Please sign in to comment.