Skip to content

Commit

Permalink
Fix for foliojs#1201 implicit column flow with visual tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Simolation committed May 29, 2021
1 parent 6c3c7bd commit ec13b0b
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 111 deletions.
27 changes: 21 additions & 6 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,25 @@ class PDFDocument extends stream.Readable {
return this;
}

continueOnNewPage(options) {
const pageMarkings = this.endPageMarkings(this.page);

this.addPage(options);
continueOnNextPage(options) {
let pageIndex;

if (
this._pageBuffer &&
(pageIndex = this._pageBuffer.indexOf(this.page)) !==
this._pageBuffer.length - 1
) {
// The page isn't the last in the buffer, so jump to the next page
this.switchToPage(pageIndex + 1);
// Now position the cursor at the top margin
this.y = this.page.margins.top;
} else {
const pageMarkings = this.endPageMarkings(this.page);
// If the page is the last one in the buffer, add a new page.
this.addPage(options);

this.initPageMarkings(pageMarkings);
this.initPageMarkings(pageMarkings);
}

return this;
}
Expand Down Expand Up @@ -216,7 +229,9 @@ class PDFDocument extends stream.Readable {
addNamedEmbeddedFile(name, ref) {
if (!this._root.data.Names.data.EmbeddedFiles) {
// disabling /Limits for this tree fixes attachments not showing in Adobe Reader
this._root.data.Names.data.EmbeddedFiles = new PDFNameTree({ limits: false });
this._root.data.Names.data.EmbeddedFiles = new PDFNameTree({
limits: false
});
}

// add filespec to EmbeddedFiles
Expand Down
2 changes: 1 addition & 1 deletion lib/line_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class LineWrapper extends EventEmitter {
return false;
}

this.document.continueOnNewPage();
this.document.continueOnNextPage();
this.column = 1;
this.startY = this.document.page.margins.top;
this.maxY = this.document.page.maxY();
Expand Down
172 changes: 69 additions & 103 deletions tests/unit/markings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ EMC
'binary'
);

document.markContent("Span");
document.markContent('Span');
document.endMarkedContent();
document.end();

Expand Down Expand Up @@ -56,9 +56,9 @@ EMC
'binary'
);

const structureContent1 = document.markStructureContent("Span");
const structureContent1 = document.markStructureContent('Span');
document.endMarkedContent();
const structureContent2 = document.markStructureContent("Span");
const structureContent2 = document.markStructureContent('Span');
document.endMarkedContent();
document.end();

Expand Down Expand Up @@ -97,8 +97,8 @@ EMC
'binary'
);

document.addStructure(document.struct('Span', () => {}));
document.addStructure(document.struct('Span', () => {}));
document.addStructure(document.struct('Span', () => {}));
document.addStructure(document.struct('Span', () => {}));
document.end();

expect(docData).toContainChunk([
Expand Down Expand Up @@ -135,17 +135,17 @@ EMC
'binary'
);

document.markContent("Artifact", {
type: "Pagination",
document.markContent('Artifact', {
type: 'Pagination',
bbox: [40, 50, 570, 70],
attached: [ "Top" ]
attached: ['Top']
});
document.endMarkedContent();
document.markContent("Span", {
lang: "en-AU",
alt: "Hi, earth! ",
actual: "Hello, world! ",
expanded: "Greetings, terrestrial sphere! "
document.markContent('Span', {
lang: 'en-AU',
alt: 'Hi, earth! ',
actual: 'Hello, world! ',
expanded: 'Greetings, terrestrial sphere! '
});
document.endMarkedContent();
document.end();
Expand Down Expand Up @@ -195,14 +195,14 @@ EMC
'binary'
);

document.markContent("Span");
document.markStructureContent("P");
document.markContent("Span");
document.markStructureContent("P");
document.markContent("Artifact");
document.markContent("Artifact");
document.markStructureContent("P");
document.markStructureContent("P");
document.markContent('Span');
document.markStructureContent('P');
document.markContent('Span');
document.markStructureContent('P');
document.markContent('Artifact');
document.markContent('Artifact');
document.markStructureContent('P');
document.markStructureContent('P');
document.end();

expect(docData).toContainChunk([
Expand Down Expand Up @@ -232,10 +232,10 @@ EMC
'binary'
);

const structureContent = document.markStructureContent("P");
document.markContent("Span");
document.text("on the first page");
document.continueOnNewPage();
const structureContent = document.markStructureContent('P');
document.markContent('Span');
document.text('on the first page');
document.continueOnNextPage();
document.end();

expect(structureContent.refs.length).toEqual(2);
Expand All @@ -260,11 +260,11 @@ EMC
test('atomically constructed', () => {
const docData = logData(document);

const pContent1 = document.markStructureContent("P");
const linkContent = document.markStructureContent("Link");
const pContent2 = document.markStructureContent("P");
const pContent3 = document.markStructureContent("P");
document.markContent("Span");
const pContent1 = document.markStructureContent('P');
const linkContent = document.markStructureContent('Link');
const pContent2 = document.markStructureContent('P');
const pContent3 = document.markStructureContent('P');
document.markContent('Span');

const section1 = document.struct('Sect', [
document.struct('P', [
Expand All @@ -274,9 +274,7 @@ EMC
])
]);
const section2 = document.struct('Sect', [
document.struct('P', [
pContent3
])
document.struct('P', [pContent3])
]);
document.addStructure(section1).addStructure(section2);

Expand All @@ -287,11 +285,7 @@ EMC
/\/StructTreeRoot 8 0 R/,
`endobj`
]);
expect(docData).toContainChunk([
`3 0 obj`,
/\/Markings 9 0 R/,
`endobj`
]);
expect(docData).toContainChunk([`3 0 obj`, /\/Markings 9 0 R/, `endobj`]);
expect(docData).toContainChunk([
`8 0 obj`,
`<<
Expand Down Expand Up @@ -331,21 +325,17 @@ EMC
`<<\n/S /P\n/P 14 0 R\n/K [3]\n/Pg 7 0 R\n>>`,
`endobj`
]);
expect(docData).toContainChunk([
`9 0 obj`,
`<<\n>>`,
`endobj`
]);
expect(docData).toContainChunk([`9 0 obj`, `<<\n>>`, `endobj`]);
});

test('incrementally constructed', () => {
const docData = logData(document);

const pContent1 = document.markStructureContent("P");
const linkContent = document.markStructureContent("Link");
const pContent2 = document.markStructureContent("P");
const pContent3 = document.markStructureContent("P");
document.markContent("Span");
const pContent1 = document.markStructureContent('P');
const linkContent = document.markStructureContent('Link');
const pContent2 = document.markStructureContent('P');
const pContent3 = document.markStructureContent('P');
document.markContent('Span');

const section1 = document.struct('Sect');
document.addStructure(section1);
Expand All @@ -355,7 +345,10 @@ EMC
link.add(linkContent);
const p1 = document.struct('P');
section1.add(p1);
p1.add(pContent1).add(link).add(pContent2).end();
p1.add(pContent1)
.add(link)
.add(pContent2)
.end();
const p2 = document.struct('P');
p2.add(pContent3);
section2.add(p2);
Expand All @@ -367,11 +360,7 @@ EMC
/\/StructTreeRoot 8 0 R/,
`endobj`
]);
expect(docData).toContainChunk([
`3 0 obj`,
/\/Markings 9 0 R/,
`endobj`
]);
expect(docData).toContainChunk([`3 0 obj`, /\/Markings 9 0 R/, `endobj`]);
expect(docData).toContainChunk([
`8 0 obj`,
`<<
Expand Down Expand Up @@ -411,11 +400,7 @@ EMC
`<<\n/S /P\n/P 11 0 R\n/K [3]\n/Pg 7 0 R\n>>`,
`endobj`
]);
expect(docData).toContainChunk([
`9 0 obj`,
`<<\n>>`,
`endobj`
]);
expect(docData).toContainChunk([`9 0 obj`, `<<\n>>`, `endobj`]);
});

test('constructed with closures', () => {
Expand All @@ -427,7 +412,10 @@ EMC
const link = document.struct('Link', () => {});
const p1 = document.struct('P');
section1.add(p1);
p1.add(() => {}).add(link).add(() => {}).end();
p1.add(() => {})
.add(link)
.add(() => {})
.end();
const p2 = document.struct('P', [() => {}]);
section2.add(p2);
document.addStructure(section2);
Expand Down Expand Up @@ -483,23 +471,21 @@ EMC
`<<\n/S /P\n/P 10 0 R\n/K [3]\n/Pg 7 0 R\n>>`,
`endobj`
]);
expect(docData).toContainChunk([
`13 0 obj`,
`<<\n>>`,
`endobj`
]);
expect(docData).toContainChunk([`13 0 obj`, `<<\n>>`, `endobj`]);
});

test('with options', () => {
const docData = logData(document);

document.addStructure(document.struct('P', {
title: "My Title",
lang: "en-AU",
alt: "My Alternative",
expanded: "My Expansion",
actual: "My Actual Text"
}));
document.addStructure(
document.struct('P', {
title: 'My Title',
lang: 'en-AU',
alt: 'My Alternative',
expanded: 'My Expansion',
actual: 'My Actual Text'
})
);

document.end();

Expand Down Expand Up @@ -549,7 +535,7 @@ EMC
document = new PDFDocument({
info: {
CreationDate: new Date(Date.UTC(2018, 1, 1)),
Title: "My Title"
Title: 'My Title'
},
displayTitle: true,
compress: false,
Expand All @@ -562,16 +548,8 @@ EMC

document.end();

expect(docData).toContainChunk([
`3 0 obj`,
/\/Lang \(en-AU\)/,
`endobj`
]);
expect(docData).toContainChunk([
`3 0 obj`,
/\/Markings 5 0 R/,
`endobj`
]);
expect(docData).toContainChunk([`3 0 obj`, /\/Lang \(en-AU\)/, `endobj`]);
expect(docData).toContainChunk([`3 0 obj`, /\/Markings 5 0 R/, `endobj`]);
expect(docData).toContainChunk([
`5 0 obj`,
`<<\n/Marked true\n>>`,
Expand All @@ -587,21 +565,9 @@ EMC
/\/DisplayDocTitle true/,
`endobj`
]);
expect(docData).toContainChunk([
`trailer`,
/\/Info 10 0 R/,
`startxref`
]);
expect(docData).toContainChunk([
`10 0 obj`,
/\/Title 14 0 R/,
`endobj`
]);
expect(docData).toContainChunk([
`14 0 obj`,
`(My Title)`,
`endobj`
]);
expect(docData).toContainChunk([`trailer`, /\/Info 10 0 R/, `startxref`]);
expect(docData).toContainChunk([`10 0 obj`, /\/Title 14 0 R/, `endobj`]);
expect(docData).toContainChunk([`14 0 obj`, `(My Title)`, `endobj`]);
});
});

Expand Down Expand Up @@ -641,7 +607,7 @@ EMC

const section = document.struct('Sect');
document.addStructure(section);
document.text("Paragraph 1\nParagraph 2", { structParent: section });
document.text('Paragraph 1\nParagraph 2', { structParent: section });
document.end();

expect(docData).toContainChunk([
Expand All @@ -657,17 +623,17 @@ EMC
expect(docData).toContainChunk([
'11 0 obj',
'<<\n/S /P\n/P 8 0 R\n/K [0]\n/Pg 7 0 R\n>>',
'endobj',
'endobj'
]);
expect(docData).toContainChunk([
'13 0 obj',
'<<\n/S /P\n/P 8 0 R\n/K [1]\n/Pg 7 0 R\n>>',
'endobj',
'endobj'
]);
expect(docData).toContainChunk([
'8 0 obj',
'<<\n/S /Sect\n/P 9 0 R\n/K [11 0 R 13 0 R]\n>>',
'endobj',
'endobj'
]);
});

Expand Down Expand Up @@ -728,7 +694,7 @@ EMC

const list = document.struct('List');
document.addStructure(list);
document.list(["Item 1","Item 2"], { structParent: list });
document.list(['Item 1', 'Item 2'], { structParent: list });
document.end();

expect(docData).toContainChunk([
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ec13b0b

Please sign in to comment.