Skip to content

Commit

Permalink
momijizukamori#57 adds marking for sewing
Browse files Browse the repository at this point in the history
  • Loading branch information
MikDal002 committed Apr 12, 2024
1 parent 7424d3d commit 3f578d7
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -496,6 +511,7 @@ export class Book {
cutmarks: this.cutmarks,
alt: config.alt,
side2flag: side2flag,
frenchStichLink: this.frenchStichLink
});
block_start += offset;
block_end += offset;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -567,6 +587,10 @@ export class Book {
currPage.drawLine(line);
});

drawPoints.forEach((point) => {
currPage.drawCircle(point);
})

if (alt) {
side2flag = !side2flag;
}
Expand Down
38 changes: 38 additions & 0 deletions src/html/page_layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@ <h2>Page Layout</h2>
</select>
</span>

<span class="row">
<label>Add marks for French link stitch</label>
<input
type="checkbox"
id="add_french_link_stich_checkbox"
name="add_french_link_stich_checkbox"
/>
<details>
<summary>Detailed settings for french link stitch</summary>
<span class="row">
<label>Margin (placement of kettle stitches):</label>
<input
type="number"
id="french_link_stitch_margin_pt"
name="french_link_stitch_margin_pt"
class="layout_margin_user_input_field"
/><sub>pt</sub><br />

<label>Amount of french link stitches (without kettles):</label>
<input
type="number"
id="french_link_stitch_amount_pt"
name="french_link_stitch_amount_pt"
class="layout_margin_user_input_field"
/><br />

<label>Space between french link stitches:</label>
<input
type="number"
id="french_link_stitch_space_pt"
name="french_link_stitch_space_pt"
class="layout_margin_user_input_field"
/><sub>pt</sub><br />

</span>
</details>
</span>

<span class="row">
<div class="layout_margin_description">
White Space Manipulation. All values are in points, relative to original document.<br /><br />1
Expand Down
5 changes: 5 additions & 0 deletions src/models/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof schema>} Configuration */
46 changes: 45 additions & 1 deletion src/utils/drawing.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LINE_LEN } from '../constants';
import { rgb } from '@cantoo/pdf-lib';
import { rgb, grayscale } from '@cantoo/pdf-lib';

/**
* @typedef Point
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/utils/formUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
});

/**
Expand Down
10 changes: 10 additions & 0 deletions src/utils/renderUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3f578d7

Please sign in to comment.