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

Fixed folio duplexrotate - but at what cost... #111

Open
wants to merge 3 commits into
base: main
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
34 changes: 13 additions & 21 deletions src/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Book {
this.duplex = configuration.printerType === 'duplex';
this.duplexrotate = configuration.rotatePage;
this.paper_rotation_90 = configuration.paperRotation90;
/** @type {number[]} */
/** @type {[number, number]} */
this.papersize = PAGE_SIZES[configuration.paperSize];
if (configuration.paperRotation90) {
this.papersize = [this.papersize[1], this.papersize[0]];
Expand All @@ -94,6 +94,11 @@ export class Book {
const pageLayout = PAGE_LAYOUTS[configuration.pageLayout];

this.page_layout = pageLayout;
// Slightly messy hack because folio with duplex rotation is the only layout where different sides have different rotations
this.alt_layout =
configuration.rotatePage && configuration.pageLayout === 'folio'
? PAGE_LAYOUTS.folio_alt
: null;
this.per_sheet = pageLayout.per_sheet;
this.pack_pages = configuration.wackySpacing === 'wacky_pack';
this.fore_edge_padding_pt = configuration.foreEdgePaddingPt;
Expand Down Expand Up @@ -452,7 +457,6 @@ export class Book {
* @param {Object} config - object /w the following parameters:
* @param {PageInfo[]} config.pageList : objects that contain 3 values: { isSigStart: boolean, isSigEnd: boolean, info: either the page number or 'b'}
* @param {boolean} config.back : is 'back' of page (boolean)
* @param {boolean} config.alt : alternate pages (boolean)
* @return reference to the new PDF created
*/
async writepages(config) {
Expand All @@ -477,13 +481,12 @@ export class Book {

// const alt_folio = this.per_sheet == 4 && back;

const positions = calculateLayout(this);

let side2flag = back;
const positions =
this.alt_layout && !back ? calculateLayout(this) : calculateLayout(this, true);

while (block_end <= pagelist.length) {
const sigDetails = config.pageList.slice(block_start, block_end);
side2flag = this.draw_block_onto_page({
this.draw_block_onto_page({
outPDF: outPDF,
embeddedPages: embeddedPages,
block_start: block_start,
Expand All @@ -494,8 +497,7 @@ export class Book {
cropmarks: this.cropmarks,
pdfEdgeMarks: this.pdfEdgeMarks,
cutmarks: this.cutmarks,
alt: config.alt,
side2flag: side2flag,
back: back,
});
block_start += offset;
block_end += offset;
Expand All @@ -506,13 +508,11 @@ export class Book {
/**
*
* @param {Object} config - object /w the following parameters:
* @param {string|null} config.outname : name of pdf added to ongoing zip file. Ex: 'signature1duplex.pdf' (or null if no signature file needed)
* @param {PageInfo[]} config.sigDetails : objects that contain 3 values: { isSigStart: boolean, isSigEnd: boolean, info: either the page number or 'b'}
* @param {boolean} config.side2flag : is 'back' of page (boolean)
* @param {boolean} config.back : is 'back' of page (boolean)
* @param {[number, number]} config.papersize : paper size (as [number, number])
* @param {number} config.block_start: Starting page index
* @param {number} config.block_end: Ending page index
* @param {boolean} config.alt : alternate pages (boolean)
* @param {boolean} config.cutmarks: whether to print cutmarks
* @param {boolean} config.cropmarks: whether to print cropmarks
* @param {boolean} config.pdfEdgeMarks: whether to print PDF edge marks
Expand All @@ -531,14 +531,13 @@ export class Book {
const foldmarks = config.cropmarks;
const pdfEdgeMarks = config.pdfEdgeMarks;
const cutmarks = config.cutmarks;
const alt = config.alt;
let side2flag = config.side2flag;
const back = config.back;

const block = config.embeddedPages.slice(block_start, block_end);
const currPage = outPDF.addPage(papersize);
const cropLines = cutmarks ? drawCropmarks(papersize, this.per_sheet) : [];
const foldLines = foldmarks
? drawFoldlines(side2flag, this.duplexrotate, papersize, this.per_sheet)
? drawFoldlines(back, this.duplexrotate, papersize, this.per_sheet)
: [];
const drawLines = [...cropLines, ...foldLines];

Expand Down Expand Up @@ -566,11 +565,6 @@ export class Book {
drawLines.forEach((line) => {
currPage.drawLine(line);
});

if (alt) {
side2flag = !side2flag;
}
return side2flag;
}

/**
Expand All @@ -585,12 +579,10 @@ export class Book {
this.writepages({
pageList: pages[0],
back: false,
alt: false,
}),
this.writepages({
pageList: pages[1],
back: true,
alt: false,
}),
];
const [pdfFront, pdfBack] = await Promise.all(tasks);
Expand Down
1 change: 1 addition & 0 deletions src/book.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Book model', () => {
cols: 1,
per_sheet: 4,
},
alt_layout: null,
per_sheet: 4,
cropmarks: false,
pdfEdgeMarks: false,
Expand Down
12 changes: 6 additions & 6 deletions src/utils/drawing.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { rgb } from '@cantoo/pdf-lib';
*/

/**
* @param {boolean} side2flag - whether we're on the back or not.
* @param {boolean} back - whether we're on the back or not.
* @param {boolean} duplexrotate - if alternate sides are rotated or not
* @param {number[]} papersize - paper dimensions
* @param {number} per_sheet - pages per sheet of paper
* @returns {Line[]}
*/
export function drawFoldlines(side2flag, duplexrotate, papersize, per_sheet) {
export function drawFoldlines(back, duplexrotate, papersize, per_sheet) {
const lineSettings = {
opacity: 0.4,
dashArray: [1, 5],
Expand All @@ -36,7 +36,7 @@ export function drawFoldlines(side2flag, duplexrotate, papersize, per_sheet) {

switch (per_sheet) {
case 32:
if (side2flag) {
if (back) {
lineSettings.dashArray = [1, 5];

x = duplexrotate ? width * 0.75 : width * 0.25;
Expand All @@ -47,7 +47,7 @@ export function drawFoldlines(side2flag, duplexrotate, papersize, per_sheet) {
}
/* falls through */
case 16:
if (side2flag) {
if (back) {
lineSettings.dashArray = [3, 5];

y = duplexrotate ? height * 0.75 : height * 0.25;
Expand All @@ -58,7 +58,7 @@ export function drawFoldlines(side2flag, duplexrotate, papersize, per_sheet) {
}
/* falls through */
case 8:
if (side2flag) {
if (back) {
lineSettings.dashArray = [5, 5];

x = width * 0.5;
Expand All @@ -69,7 +69,7 @@ export function drawFoldlines(side2flag, duplexrotate, papersize, per_sheet) {
}
/* falls through */
case 4:
if (!side2flag) {
if (!back) {
lineSettings.dashArray = [10, 5];
lines.push({ ...drawHLine(height * 0.5, 0, width), ...lineSettings });
}
Expand Down
17 changes: 13 additions & 4 deletions src/utils/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @return {import("../book.js").Position[]}
*/
export function calculateLayout(book) {
export function calculateLayout(book, alt) {
const l = calculateDimensions(book);
const {
layoutCell,
Expand All @@ -16,8 +16,8 @@ export function calculateLayout(book) {
} = l;
const [cellWidth, cellHeight] = layoutCell;
const positions = [];

l.layout.rotations.forEach((row, i) => {
const rotations = alt && l.alt_layout ? l.alt_layout.rotations : l.layout.rotations;
rotations.forEach((row, i) => {
row.forEach((col, j) => {
const xForeEdgeShift = xForeEdgeShiftFunc();
const xBindingShift = xBindingShiftFunc();
Expand Down Expand Up @@ -94,7 +94,15 @@ export function calculateLayout(book) {
* }
*/
export function calculateDimensions(book) {
const { cropbox, padding_pt, papersize, page_layout, page_positioning, page_scaling } = book;
const {
cropbox,
padding_pt,
papersize,
page_layout,
alt_layout,
page_positioning,
page_scaling,
} = book;

const { width, height } = cropbox;
const pageX = width + Math.max(padding_pt.binding, 0) + Math.max(padding_pt.fore_edge, 0);
Expand Down Expand Up @@ -160,6 +168,7 @@ export function calculateDimensions(book) {
};
return {
layout: page_layout,
alt_layout: alt_layout,
rawPdfSize: [width, height],
pdfScale: [sx, sy],
pdfSize: [pageX, pageY],
Expand Down
Loading