Skip to content

Commit

Permalink
Refactor createpages
Browse files Browse the repository at this point in the history
  • Loading branch information
acestronautical committed Feb 9, 2024
1 parent db5fea1 commit 7c14028
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions src/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,42 +157,50 @@ export class Book {
let pages;
[this.managedDoc, pages] = await this.embedPagesInNewPdf(this.currentdoc);

for (var i = 0; i < pages.length; ++i) {
var page = pages[i];
var newPage = this.managedDoc.addPage();
var rotate90cw =
this.source_rotation == '90cw' ||
(this.source_rotation == 'out_binding' && i % 2 == 0) ||
(this.source_rotation == 'in_binding' && i % 2 == 1);
var rotate90ccw =
this.source_rotation == '90ccw' ||
(this.source_rotation == 'out_binding' && i % 2 == 1) ||
(this.source_rotation == 'in_binding' && i % 2 == 0);
if (this.source_rotation == 'none') {
newPage.setSize(page.width, page.height);
newPage.drawPage(page);
} else if (rotate90ccw) {
newPage.setSize(page.height, page.width);
newPage.drawPage(page, {
x: page.height,
y: 0,
rotate: degrees(90),
});
} else if (rotate90cw) {
pages.forEach((page, i) => {
const newPage = this.managedDoc.addPage();
let rotateAngle = null;
const evenPage = i % 2 === 0;

// Determine rotation angle
switch (this.source_rotation) {
case 'none':
rotateAngle = 0;
break;
case '90cw':
rotateAngle = -90;
break;
case '90ccw':
rotateAngle = 90;
break;
case 'out_binding':
rotateAngle = evenPage ? -90 : 90;
break;
case 'in_binding':
rotateAngle = evenPage ? 90 : -90;
break;
default:
var e = new Error('Invalid source rotation');
console.error(e);
throw e;
}

// Apply rotation to the new page
if (rotateAngle !== 0) {
newPage.setSize(page.height, page.width);
newPage.drawPage(page, {
x: 0,
y: page.width,
rotate: degrees(-90),
x: rotateAngle === 90 ? page.height : 0,
y: rotateAngle === -90 ? page.width : 0,
rotate: degrees(rotateAngle),
});
} else {
var e = new Error("??? what sorta' layout you think you're going to get?");
console.error(e);
throw e;
newPage.setSize(page.width, page.height);
newPage.drawPage(page);
}

page.embed();
this.cropbox = newPage.getCropBox();
}
});

console.log(
'The updatedDoc doc has : ',
Expand Down

0 comments on commit 7c14028

Please sign in to comment.