From 3f578d7edbc0025a0d1b0f692b1821a00246b3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Dalecki?= Date: Fri, 12 Apr 2024 23:28:58 +0200 Subject: [PATCH] #57 adds marking for sewing --- src/book.js | 26 ++++++++++++++++++++- src/html/page_layout.html | 38 ++++++++++++++++++++++++++++++ src/models/configuration.js | 5 ++++ src/utils/drawing.js | 46 ++++++++++++++++++++++++++++++++++++- src/utils/formUtils.js | 5 ++++ src/utils/renderUtils.js | 10 ++++++++ 6 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/book.js b/src/book.js index af666c1..3f02094 100644 --- a/src/book.js +++ b/src/book.js @@ -10,7 +10,7 @@ import { PAGE_LAYOUTS, PAGE_SIZES } from './constants.js'; import { updatePageLayoutInfo } from './utils/renderUtils.js'; import JSZip from 'jszip'; import { loadConfiguration } from './utils/formUtils.js'; -import { drawFoldlines, drawCropmarks, drawSpineMarks } from './utils/drawing.js'; +import { drawFoldlines, drawCropmarks, drawSpineMarks, drawFrenchStichMarks } from './utils/drawing.js'; import { calculateDimensions, calculateLayout } from './utils/layout.js'; import { interleavePages, embedPagesInNewPdf } from './utils/pdf.js'; @@ -36,6 +36,15 @@ import { interleavePages, embedPagesInNewPdf } from './utils/pdf.js'; * @property {boolean} [isLeftPage]: isLeftPage, */ +/** + * @typedef FrenchMarks + * @type {object} + * @property {boolean} isEnabled - specifies if marks should be drawed, + * @property {number} amount - amount of places to saw. + * @property {number} marginPt - distance from the end of page to a kettle point, + * @property {number} spacingPt - distance between two seing points + */ + export class Book { /** @param { import("./models/configuration.js").Configuration } configuration */ constructor(configuration) { @@ -80,6 +89,12 @@ export class Book { this.page_positioning = configuration.pagePositioning; this.flyleafs = configuration.flyleafs; this.cropmarks = configuration.cropMarks; + this.frenchStichLink = { + isEnabled: configuration.frenchStitchLinkEnabled, + amount: configuration.frenchStitchesAmount, + marginPt: configuration.frenchStitchLinkMarginPt, + spacingPt: configuration.frenchStitchesSpacingPt + }; this.pdfEdgeMarks = configuration.pdfEdgeMarks; this.cutmarks = configuration.cutMarks; this.format = configuration.sigFormat; @@ -496,6 +511,7 @@ export class Book { cutmarks: this.cutmarks, alt: config.alt, side2flag: side2flag, + frenchStichLink: this.frenchStichLink }); block_start += offset; block_end += offset; @@ -519,6 +535,7 @@ export class Book { * @param {Position[]} config.positions: list of page positions * @param {PDFDocument} [config.outPDF]: PDF to write to, in addition to PDF created w/ `outname` (or null) * @param {(PDFEmbeddedPage|string)[]} [config.embeddedPages] : pages already embedded in the `destPdf` to assemble in addition (or null) + * @param {FrenchMarks} config.frenchStichLink: config for drawing FrenchMarks */ draw_block_onto_page(config) { @@ -533,14 +550,17 @@ export class Book { const cutmarks = config.cutmarks; const alt = config.alt; let side2flag = config.side2flag; + const frenchStichLink = config.frenchStichLink; const block = config.embeddedPages.slice(block_start, block_end); const currPage = outPDF.addPage(papersize); + const frenchPoints = frenchStichLink.isEnabled ? drawFrenchStichMarks(papersize, frenchStichLink.amount, frenchStichLink.marginPt, frenchStichLink.spacingPt) : []; const cropLines = cutmarks ? drawCropmarks(papersize, this.per_sheet) : []; const foldLines = foldmarks ? drawFoldlines(side2flag, this.duplexrotate, papersize, this.per_sheet) : []; const drawLines = [...cropLines, ...foldLines]; + const drawPoints = [ ...frenchPoints]; block.forEach((page, i) => { if (page == 'b' || page === undefined) { @@ -567,6 +587,10 @@ export class Book { currPage.drawLine(line); }); + drawPoints.forEach((point) => { + currPage.drawCircle(point); + }) + if (alt) { side2flag = !side2flag; } diff --git a/src/html/page_layout.html b/src/html/page_layout.html index 4b55021..6e06cef 100644 --- a/src/html/page_layout.html +++ b/src/html/page_layout.html @@ -81,6 +81,44 @@

Page Layout

+ + + +
+ Detailed settings for french link stitch + + + pt
+ + +
+ + + pt
+ +
+
+
+
White Space Manipulation. All values are in points, relative to original document.

1 diff --git a/src/models/configuration.js b/src/models/configuration.js index 27f370c..5a01e04 100644 --- a/src/models/configuration.js +++ b/src/models/configuration.js @@ -96,6 +96,11 @@ export const schema = z.object({ flyleafs: urlSafe(z.coerce.number()).default(1), paperSizeCustomWidth: urlSafe(z.coerce.number()), paperSizeCustomHeight: urlSafe(z.coerce.number()), + + frenchStitchLinkEnabled: urlSafe(coercedBoolean).default(false), + frenchStitchLinkMarginPt: urlSafe(z.coerce.number()).default(72), + frenchStitchesAmount: urlSafe(z.coerce.number()).default(3), + frenchStitchesSpacingPt: urlSafe(z.coerce.number()).default(36) }); /** @typedef {z.infer} Configuration */ diff --git a/src/utils/drawing.js b/src/utils/drawing.js index 9b366c8..791100e 100644 --- a/src/utils/drawing.js +++ b/src/utils/drawing.js @@ -1,5 +1,5 @@ import { LINE_LEN } from '../constants'; -import { rgb } from '@cantoo/pdf-lib'; +import { rgb, grayscale } from '@cantoo/pdf-lib'; /** * @typedef Point @@ -17,6 +17,15 @@ import { rgb } from '@cantoo/pdf-lib'; * @property {number[]} [dashArray] - sequence of dash and gap lengths to be repeated for a dashed line */ +/** + * @typedef Point + * @property {number} x, + * @property {number} y, + * @property {number} size, + * @property {Grayscale|RGB|CMYK} color, + * + */ + /** * @param {boolean} side2flag - whether we're on the back or not. * @param {boolean} duplexrotate - if alternate sides are rotated or not @@ -112,6 +121,41 @@ export function drawCropmarks(papersize, per_sheet) { return lines; } +/** + * @param {number[]} papersize - paper dimensions + * @param {number} amount - amount of sewing crosses. + * @param {number} marginPt - distance from the end of sheet of paper to kettle mark + * @param {number} spacingPt - distance between two points in a single sewwing cross. + * @returns {Point[]} + */ +export function drawFrenchStichMarks(papersize, amount, marginPt, spacingPt) { + console.log("French marking!"); + const [width, height] = papersize; + + const y = height * 0.5; + const commonCircleValues = {y: y, size: 1, color: grayscale(0.0)} + + const workingWidth = width - 2 * marginPt; + const spaceBettwenPoints = workingWidth / (amount + 1); + + let sewingPoints = []; + + for (let index = 1; index <= amount ; index++) { + const halfOfSpaceing = spacingPt / 2; + sewingPoints.push( + {x: marginPt + spaceBettwenPoints * index + halfOfSpaceing, ...commonCircleValues}, + {x: marginPt + spaceBettwenPoints * index - halfOfSpaceing, ...commonCircleValues} + ) + + } + + return [ + { x : marginPt, ...commonCircleValues}, + { x : width - marginPt, ...commonCircleValues }, + ...sewingPoints + ]; +} + /** * @param {import("../book.js").PageInfo} sigDetails - page info object * @param {import("../book.js").Position} position - position info object diff --git a/src/utils/formUtils.js b/src/utils/formUtils.js index a477b5b..9ea9517 100644 --- a/src/utils/formUtils.js +++ b/src/utils/formUtils.js @@ -40,6 +40,11 @@ const fromFormToConfiguration = (form) => flyleafs: form.get('flyleafs'), paperSizeCustomWidth: form.get('paper_size_custom_width'), paperSizeCustomHeight: form.get('paper_size_custom_height'), + + frenchStitchLinkEnabled: form.get('add_french_link_stich_checkbox'), + frenchStitchLinkMarginPt: form.get('french_link_stitch_margin_pt'), + frenchStitchesAmount: form.get('french_link_stitch_amount_pt'), + frenchStitchesSpacingPt: form.get('french_link_stitch_space_pt') }); /** diff --git a/src/utils/renderUtils.js b/src/utils/renderUtils.js index b627be1..93f6e31 100644 --- a/src/utils/renderUtils.js +++ b/src/utils/renderUtils.js @@ -198,6 +198,16 @@ export function renderFormFromSettings(configuration) { `input[name="wacky_spacing"][value="${configuration.wackySpacing}"]` ).checked = true; + // Set french link stitches settings + document.querySelector('input[name="add_french_link_stich_checkbox"]').checked = + configuration.frenchStitchLinkEnabled; + document.querySelector('input[name="french_link_stitch_margin_pt"]').value = + configuration.frenchStitchLinkMarginPt; + document.querySelector('input[name="french_link_stitch_amount_pt"]').value = + configuration.frenchStitchesAmount; + document.querySelector('input[name="french_link_stitch_space_pt"]').value = + configuration.frenchStitchesSpacingPt; + // Set freeform inputs document.querySelector('input[name="main_fore_edge_padding_pt"]').value = configuration.mainForeEdgePaddingPt;