Skip to content

Commit

Permalink
Merge pull request #3 from blforce/features/index-headers
Browse files Browse the repository at this point in the history
Index headers can now be enabled and they generate bookmarks.
  • Loading branch information
theBenForce authored Jun 19, 2018
2 parents 9ca6494 + 3617288 commit a457497
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 28 deletions.
38 changes: 31 additions & 7 deletions src/docMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,23 @@ DocMeasure.prototype.measureToc = function (node) {
var textStyle = node.toc.textStyle || {};
var numberStyle = node.toc.numberStyle || textStyle;
var textMargin = node.toc.textMargin || [0, 0, 0, 0];

for (var i = 0, l = node.toc._items.length; i < l; i++) {
var item = node.toc._items[i];
var lineStyle = node.toc._items[i].tocStyle || textStyle;
var lineMargin = node.toc._items[i].tocMargin || textMargin;
body.push([
{text: item.text, alignment: 'left', style: lineStyle, margin: lineMargin},
{text: '00000', alignment: 'right', _tocItemRef: item, style: numberStyle, margin: [0, lineMargin[1], 0, lineMargin[3]]}
]);
var lineStyle = item.tocStyle || textStyle;
var lineMargin = item.tocMargin || textMargin;

if (item.isHeader) {
body.push([
{text: item.text, colSpan: 2, fontSize: 16, bold: true, alignment: 'left', margin: lineMargin, _outline: {level: 1, text: item.text}}
]);
}
else {
body.push([
{text: item.text, alignment: 'left', style: lineStyle, margin: lineMargin},
{text: '00000', alignment: 'right', _tocItemRef: item, style: numberStyle, margin: [0, lineMargin[1], 0, lineMargin[3]]}
]);
}
}


Expand Down Expand Up @@ -535,6 +544,16 @@ DocMeasure.prototype.measureColumns = function (node) {
return node;
};

function getColumnCount(row) {
var total = 0;

for (var i = 0, l = row.length; i < l; i++) {
total += row[i].colSpan || 1;
}

return total;
}

DocMeasure.prototype.measureTable = function (node) {
extendTableWidths(node);
node._layout = getLayout(this.tableLayouts);
Expand All @@ -543,13 +562,18 @@ DocMeasure.prototype.measureTable = function (node) {
var colSpans = [];
var col, row, cols, rows;

for (col = 0, cols = node.table.body[0].length; col < cols; col++) {
for (col = 0, cols = getColumnCount(node.table.body[0]); col < cols; col++) {
var c = node.table.widths[col];
c._minWidth = 0;
c._maxWidth = 0;

for (row = 0, rows = node.table.body.length; row < rows; row++) {
var rowData = node.table.body[row];

if (col >= rowData.length) {
continue;
}

var data = rowData[col];
if (data === undefined) {
console.error('Malformed table row ', rowData, 'in node ', node);
Expand Down
66 changes: 66 additions & 0 deletions src/docPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,64 @@ DocPreprocessor.prototype.preprocessText = function (node) {
return node;
};

function compareItem(a, b) {
if (a.text < b.text) {
return -1;
}

if (a.text > b.text) {
return 1;
}

return 0;
}

function insertHeaders(items) {
// Add number header if needed
var firstChar = items[0].text.substring(0, 1);
var lastSection;

if (!isNaN(firstChar)) {
items.unshift({
text: '0-9',
isHeader: true
});
lastSection = 'numeric';
}

var idx = 0;

while (idx < items.length) {
firstChar = items[idx].text.substring(0, 1);

if (lastSection === 'numeric' && !isNaN(firstChar)) {
idx++;
continue;
}

if (firstChar !== lastSection) {
if (idx > 0) {
var previousItem = items[idx - 1];
var prevMargin = previousItem.tocMargin;

if (prevMargin) {
prevMargin[3] = 16;
} else {
previousItem.tocMargin = [0, 0, 0, 16]
}
}
lastSection = firstChar;
items.splice(idx, 0, {
text: firstChar.toUpperCase(),
isHeader: true
});
idx++;
}

idx++;
}
};

DocPreprocessor.prototype.preprocessToc = function (node) {
if (!node.toc.id) {
node.toc.id = '_default_';
Expand All @@ -215,6 +273,14 @@ DocPreprocessor.prototype.preprocessToc = function (node) {
}

node.toc._items = this.tocs[node.toc.id].toc._items;

node.toc._items.sort(compareItem);

// TODO: Combine similar items

if (node.toc.showSectionHeaders) {
insertHeaders(node.toc._items);
}
}

this.tocs[node.toc.id] = node;
Expand Down
8 changes: 0 additions & 8 deletions src/elementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ function addPageItem(page, item, index) {
}
}

ElementWriter.prototype.addOutlineItem = function (item) {
var page = this.context.getCurrentPage();

addPageItem(page, {
type: 'outlineItem',
item
}, undefined);
};

ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, index) {
var height = line.getHeight();
Expand Down
14 changes: 10 additions & 4 deletions src/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,6 @@ LayoutBuilder.prototype.processNode = function (node) {
self.writer.context().moveTo((relPosition.x || 0) + self.writer.context().x, (relPosition.y || 0) + self.writer.context().y);
}

if (node._outline) {
self.writer.writer.addOutlineItem(node._outline);
}

if (node.stack) {
self.processVerticalContainer(node);
} else if (node.columnCount) {
Expand Down Expand Up @@ -605,6 +601,11 @@ LayoutBuilder.prototype.processList = function (orderedList, node) {
LayoutBuilder.prototype.processTable = function (tableNode) {
var processor = new TableProcessor(tableNode);

if (tableNode._outline) {
tableNode.table.body[tableNode.table.headerRows][0]._outline = tableNode._outline;
delete tableNode._outline;
}

processor.beginTable(this.writer);

var rowHeights = tableNode.table.heights;
Expand Down Expand Up @@ -681,6 +682,11 @@ LayoutBuilder.prototype.buildNextLine = function (textNode) {
var line = new Line(this.writer.context().availableWidth);
var textTools = new TextTools(null);

if (textNode._outline) {
line._outline = textNode._outline;
delete textNode._outline;
}

while (textNode._inlines && textNode._inlines.length > 0 && line.hasEnoughSpaceForInline(textNode._inlines[0])) {
var inline = textNode._inlines.shift();

Expand Down
17 changes: 8 additions & 9 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ function PdfPrinter(fontDescriptors) {
PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) {
options = options || {};

this.currentOutlineLevel = 0

var pageSize = fixPageSize(docDefinition.pageSize, docDefinition.pageOrientation);
var compressPdf = isBoolean(docDefinition.compress) ? docDefinition.compress : true;
var bufferPages = options.bufferPages || false;
Expand Down Expand Up @@ -337,9 +335,6 @@ function renderPages(pages, fontProvider, pdfKitDoc, progressCallback) {
case 'image':
renderImage(item.item, item.item.x, item.item.y, pdfKitDoc);
break;
case 'outlineItem':
addOutlineItem.call(this, item.item.level, item.item.text, pdfKitDoc)
break;
case 'beginClip':
beginClip(item.item, pdfKitDoc);
break;
Expand All @@ -357,16 +352,16 @@ function renderPages(pages, fontProvider, pdfKitDoc, progressCallback) {
}

function addOutlineItem(level, text, pdfKitDoc) {
while (level < this.currentOutlineLevel) {

while (level < pdfKitDoc.getOutlineLevel()) {
pdfKitDoc.endOutlineSublevel();
this.currentOutlineLevel--;
}

if (level === this.currentOutlineLevel) {
var currentLevel = pdfKitDoc.getOutlineLevel();
if (level === currentLevel) {
pdfKitDoc.addOutline(text);
} else {
pdfKitDoc.addSublevelOutline(text);
this.currentOutlineLevel++;
}
}

Expand All @@ -393,6 +388,10 @@ function renderLine(line, x, y, pdfKitDoc) {
}
}

if (line._outline) {
addOutlineItem(line._outline.level, line._outline.text, pdfKitDoc);
}

x = x || 0;
y = y || 0;

Expand Down

0 comments on commit a457497

Please sign in to comment.