From 7c14028b68f3e85daff2bc0e9b94ce9e33d46cd2 Mon Sep 17 00:00:00 2001 From: acestronautical Date: Fri, 9 Feb 2024 12:47:54 -0800 Subject: [PATCH] Refactor createpages --- src/book.js | 66 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/book.js b/src/book.js index b0e6bc7..f2e9404 100644 --- a/src/book.js +++ b/src/book.js @@ -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 : ',