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

Fix issue 72 #1087

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2,501 changes: 1,486 additions & 1,015 deletions build/pdfmake.js

Large diffs are not rendered by default.

51 changes: 26 additions & 25 deletions build/pdfmake.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/pdfmake.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/docMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ DocMeasure.prototype.measureLeaf = function (node) {
styleStack.push(node);

var data = this.textTools.buildInlines(node.text, styleStack);

node._inlines = data.items;
node._minWidth = data.minWidth;
node._maxWidth = data.maxWidth;
Expand Down Expand Up @@ -507,6 +506,8 @@ DocMeasure.prototype.measureTable = function (node) {
extendTableWidths(node);
node._layout = getLayout(this.tableLayouts);
node._offsets = getOffsets(node._layout);
node._tableAlignment = this.styleStack.getProperty('tableAlignment');
node._alignment = this.styleStack.getProperty('alignment');

var colSpans = [];
var col, row, cols, rows;
Expand Down
18 changes: 11 additions & 7 deletions src/elementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function addPageItem(page, item, index) {
}
}

ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, index) {
ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, startX) {
var height = line.getHeight();
var context = this.context;
var page = context.getCurrentPage(),
Expand All @@ -37,12 +37,12 @@ ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, ind
line.x = context.x + (line.x || 0);
line.y = context.y + (line.y || 0);

this.alignLine(line);
this.alignLine(line, startX);

addPageItem(page, {
type: 'line',
item: line
}, index);
}, null);
this.tracker.emit('lineAdded', line);

if (!dontUpdateContextPosition) {
Expand All @@ -52,19 +52,23 @@ ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, ind
return position;
};

ElementWriter.prototype.alignLine = function (line) {
ElementWriter.prototype.alignLine = function (line, startX) {
var width = this.context.availableWidth;

var lineWidth = line.getWidth();

var alignment = line.inlines && line.inlines.length > 0 && line.inlines[0].alignment;

var offset = 0;
// startX allows to align the line inside the aligned table
var offset = startX || 0

// text alignment in page or text in table cell alignment
switch (alignment) {
case 'right':
offset = width - lineWidth;
offset += width - lineWidth;
break;
case 'center':
offset = (width - lineWidth) / 2;
offset += (width - lineWidth) / 2;
break;
}

Expand Down
22 changes: 8 additions & 14 deletions src/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,8 @@ function decorateNode(node) {
};
}

LayoutBuilder.prototype.processNode = function (node) {
LayoutBuilder.prototype.processNode = function (node, startX) {
var self = this;

this.linearNodeList.push(node);
decorateNode(node);

Expand Down Expand Up @@ -348,7 +347,7 @@ LayoutBuilder.prototype.processNode = function (node) {
} else if (node.table) {
self.processTable(node);
} else if (node.text !== undefined) {
self.processLeaf(node);
self.processLeaf(node, startX);
} else if (node.toc) {
self.processToc(node);
} else if (node.image) {
Expand Down Expand Up @@ -437,7 +436,7 @@ LayoutBuilder.prototype.processColumns = function (columnNode) {
}
};

LayoutBuilder.prototype.processRow = function (columns, widths, gaps, tableBody, tableRow) {
LayoutBuilder.prototype.processRow = function (columns, widths, gaps, tableBody, tableRow, startX) {
var self = this;
var pageBreaks = [], positions = [];

Expand All @@ -459,7 +458,7 @@ LayoutBuilder.prototype.processRow = function (columns, widths, gaps, tableBody,

self.writer.context().beginColumn(width, leftOffset, getEndingCell(column, i));
if (!column._span) {
self.processNode(column);
self.processNode(column, startX);
addAll(positions, column.positions);
} else if (column._columnEndingContext) {
// row-span ending
Expand Down Expand Up @@ -555,14 +554,12 @@ LayoutBuilder.prototype.processList = function (orderedList, node) {

// tables
LayoutBuilder.prototype.processTable = function (tableNode) {
var processor = new TableProcessor(tableNode);

var processor = new TableProcessor(tableNode);
processor.beginTable(this.writer);

for (var i = 0, l = tableNode.table.body.length; i < l; i++) {
processor.beginRow(i, this.writer);

var result = this.processRow(tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i);
var result = this.processRow(tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i, processor.startX);
addAll(tableNode.positions, result.positions);

processor.endRow(i, this.writer, result.pageBreaks);
Expand All @@ -572,7 +569,7 @@ LayoutBuilder.prototype.processTable = function (tableNode) {
};

// leafs (texts)
LayoutBuilder.prototype.processLeaf = function (node) {
LayoutBuilder.prototype.processLeaf = function (node, startX) {
var line = this.buildNextLine(node);
var currentHeight = (line) ? line.getHeight() : 0;
var maxHeight = node.maxHeight || -1;
Expand All @@ -582,7 +579,7 @@ LayoutBuilder.prototype.processLeaf = function (node) {
}

while (line && (maxHeight === -1 || currentHeight < maxHeight)) {
var positions = this.writer.addLine(line);
var positions = this.writer.addLine(line, false, startX);
node.positions.push(positions);
line = this.buildNextLine(node);
if (line) {
Expand All @@ -599,7 +596,6 @@ LayoutBuilder.prototype.processToc = function (node) {
};

LayoutBuilder.prototype.buildNextLine = function (textNode) {

function cloneInline(inline) {
var newInline = inline.constructor();
for (var key in inline) {
Expand Down Expand Up @@ -636,12 +632,10 @@ LayoutBuilder.prototype.buildNextLine = function (textNode) {
textNode._inlines.unshift(newInline);
}
}

line.addInline(inline);
}

line.lastLineInParagraph = textNode._inlines.length === 0;

return line;
};

Expand Down
4 changes: 2 additions & 2 deletions src/pageElementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function fitOnPage(self, addFct) {
return position;
}

PageElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, index) {
PageElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, startX) {
return fitOnPage(this, function (self) {
return self.writer.addLine(line, dontUpdateContextPosition, index);
return self.writer.addLine(line, dontUpdateContextPosition, startX);
});
};

Expand Down
1 change: 1 addition & 0 deletions src/styleContextStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ StyleContextStack.prototype.autopush = function (item) {
'characterSpacing',
'noWrap',
'markerColor',
'tableAlignment',
'leadingIndent'
//'tableCellPadding'
// 'cellBorder',
Expand Down
39 changes: 27 additions & 12 deletions src/tableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ TableProcessor.prototype.beginTable = function (writer) {
var tableNode;
var availableWidth;
var self = this;

tableNode = this.tableNode;
this.offsets = tableNode._offsets;
this.layout = tableNode._layout;

this.tableAlignment = tableNode._tableAlignment;
availableWidth = writer.context().availableWidth - this.offsets.total;

ColumnCalculator.buildColumnWidths(tableNode.table.widths, availableWidth);

this.tableWidth = tableNode._offsets.total + getTableInnerContentWidth();
this.startX = 0;
switch (this.tableAlignment) {
case 'right':
this.startX = writer.context().availableWidth - this.tableWidth;
break;
case 'center':
this.startX = (writer.context().availableWidth - this.tableWidth) / 2;
}
this.rowSpanData = prepareRowSpanData();
this.cleanUpRepeatables = false;

Expand Down Expand Up @@ -48,7 +56,7 @@ TableProcessor.prototype.beginTable = function (writer) {

function prepareRowSpanData() {
var rsd = [];
var x = 0;
var x = self.startX;
var lastWidth = 0;

rsd.push({left: 0, rowSpan: 0});
Expand Down Expand Up @@ -81,7 +89,7 @@ TableProcessor.prototype.beginTable = function (writer) {
var rowSpan = cell.rowSpan || 1;
var colSpan = cell.colSpan || 1;

for (var rowOffset = 0; rowOffset < rowSpan; rowOffset++) {
for (var rowOffset = self.startX; rowOffset < rowSpan; rowOffset++) {
// set left border
if (cell.border[0] !== undefined && rowOffset > 0) {
setBorder(rowIndex + rowOffset, colIndex, 0, cell.border[0]);
Expand Down Expand Up @@ -120,10 +128,11 @@ TableProcessor.prototype.beginTable = function (writer) {
TableProcessor.prototype.onRowBreak = function (rowIndex, writer) {
var self = this;
return function () {
var offset = self.rowPaddingTop + (!self.headerRows ? self.topLineWidth : 0);
writer.context().availableHeight -= self.reservedAtBottom;
//console.log('moving by : ', topLineWidth, rowPaddingTop);
var offset = self.rowPaddingTop + (!self.headerRows ? self.topLineWidth : 0)+ self.startX;
writer.context().moveDown(offset);
};

};

TableProcessor.prototype.beginRow = function (rowIndex, writer) {
Expand Down Expand Up @@ -188,10 +197,15 @@ TableProcessor.prototype.drawHorizontalLine = function (lineIndex, writer, overr

if (!shouldDrawLine || i === l - 1) {
if (currentLine && currentLine.width) {
var x1 = currentLine.left;
// if (this.tableNode._alignment) {
if (this.tableAlignment) {
x1 = this.startX;
}
writer.addVector({
type: 'line',
x1: currentLine.left,
x2: currentLine.left + currentLine.width,
x1: x1,
x2: x1 + currentLine.width,
y1: y,
y2: y,
lineWidth: lineWidth,
Expand Down Expand Up @@ -390,18 +404,19 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {

for (var i = 0, l = self.tableNode.table.body[rowIndex].length; i < l; i++) {
if (!cols) {
result.push({x: self.rowSpanData[i].left, index: i});

if (i) {
result.push({x: self.rowSpanData[i].left, index: i});
} else {
result.push({x: self.rowSpanData[i].left + self.startX , index: i});
}
var item = self.tableNode.table.body[rowIndex][i];
cols = (item._colSpan || item.colSpan || 0);
}
if (cols > 0) {
cols--;
}
}

result.push({x: self.rowSpanData[self.rowSpanData.length - 1].left, index: self.rowSpanData.length - 1});

return result;
}
};
Expand Down
49 changes: 49 additions & 0 deletions tests/integration/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,53 @@ describe('Integration test: tables', function () {
assert.deepEqual(getColumnText(lines, {cell: 1}), 'Row 2');
});

it('renders a simple table in a stack with alignment (issue #72)', function () {
var dd = {
content: {
stack:[{
table: {
body: [
['Column 1', 'Column 2'],
['Value 1', 'Value 2']
]
},
}],
tableAlignment: 'right'
}
};

var pages = testHelper.renderPages('A6', dd);
var lines = getCells(pages, {pageNumber: 0});

const availableWidth = sizes.A6[0] - testHelper.MARGINS.right
const tableWidth = TABLE_PADDING_X * 3 + TABLE_BORDER_STRENGTH * 2 + lines[0].item.maxWidth * 2
const startXAligned = availableWidth - tableWidth

assert.equal(lines[0].item.x, startXAligned);
});

it('renders a simple table in a stack with alignment (issue #72)', function () {
var dd = {
content: {
stack:[{
table: {
body: [
['Column 1', 'Column 2'],
['Value 1', 'Value 2']
]
},
}],
tableAlignment: 'center'
}
};

var pages = testHelper.renderPages('A6', dd);
var lines = getCells(pages, {pageNumber: 0});

const tableWidth = TABLE_PADDING_X * 2 + TABLE_BORDER_STRENGTH + lines[0].item.maxWidth * 2
const startXAligned = (sizes.A6[0] - tableWidth) / 2

assert.equal(lines[0].item.x, startXAligned);
});

});