Something went wrong. Try again.
Simple web tool for turning images into printable label PDFs (various sizes)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import type { LabelPosition, LayoutOptions, LayoutPlan, PagePlan, Placement, SheetSpec, SourceImage} from './types';import { labelPositions } from './sheet';
function expandImages(images: SourceImage[]): SourceImage[] { const expanded: SourceImage[] = [];
for (const image of images) { const copies = Math.max(0, Math.floor(image.copies)); for (let i = 0; i < copies; i++) { expanded.push(image); } }
return expanded;}
export function planLayout( sheet: SheetSpec, images: SourceImage[], options: LayoutOptions): LayoutPlan { const expanded = expandImages(images); const positions = labelPositions(sheet);
if (expanded.length === 0 || positions.length === 0) { return { sheet, options, pages: [], labelCount: 0 }; }
// Skipping a whole sheet is just "print one fewer sheet", so the skip only // ever consumes part of the first page. let remainingSkip = Math.min( Math.max(0, Math.floor(options.skip)), Math.max(0, positions.length - 1) ); let imageIndex = 0; const pages: PagePlan[] = [];
while (imageIndex < expanded.length) { const placements: Placement[] = []; const emptyPositions: LabelPosition[] = [];
for (const position of positions) { if (remainingSkip > 0) { emptyPositions.push(position); remainingSkip--; } else if (imageIndex < expanded.length) { placements.push({ position, image: expanded[imageIndex] }); imageIndex++; } else { emptyPositions.push(position); } }
pages.push({ pageIndex: pages.length, placements, emptyPositions }); }
const labelCount = pages.reduce((total, page) => total + page.placements.length, 0);
return { sheet, options, pages, labelCount };}