-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
56 lines (49 loc) · 1.81 KB
/
utils.js
File metadata and controls
56 lines (49 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { getPageSizeMm } from "./layout.js";
export function getOptions({ layout, pageSize, orientation, marginMm, title, captionMode, removeBg, enhance, useRemoveBgApi, removeBgApiKey }) {
const pageSizeKey = pageSize.toLowerCase(); // jsPDF expects "a4", "letter", "legal"
const { w, h } = getPageSizeMm(pageSizeKey);
const isLandscape = orientation === "landscape";
const pageWmm = isLandscape ? Math.max(w, h) : Math.min(w, h);
const pageHmm = isLandscape ? Math.min(w, h) : Math.max(w, h);
return {
layoutKey: layout,
pageSizeKey,
pageWmm,
pageHmm,
orientation,
marginMm: Math.max(0, marginMm),
title,
captionMode: captionMode || "below",
removeBg: !!removeBg,
enhance: !!enhance,
useRemoveBgApi: !!useRemoveBgApi,
removeBgApiKey: removeBgApiKey || ""
};
}
// Heuristic layout suggestion based on image count and aspect ratios
export function suggestLayout(items, { pageWmm, pageHmm }) {
const n = items.length;
const ratios = [];
for (const it of items) {
// Try to read dimensions from file name patterns if possible; fallback to square
ratios.push(1);
}
// Prefer denser grids for many images
let layoutKey = "4_grid";
if (n <= 1) layoutKey = "1_center";
else if (n === 2) layoutKey = "2_row";
else if (n <= 3) layoutKey = pageWmm >= pageHmm ? "3_row" : "3_col";
else if (n <= 6) layoutKey = "6_grid";
else if (n <= 12) layoutKey = pageWmm >= pageHmm ? "4x3" : "3x4";
else if (n <= 16) layoutKey = "4x4";
else if (n <= 20) layoutKey = "4x5";
else layoutKey = "4x6";
// Margins: smaller for dense pages, larger for few items
let marginMm = 15;
if (n <= 2) marginMm = 20;
else if (n <= 6) marginMm = 15;
else if (n <= 12) marginMm = 12;
else if (n <= 20) marginMm = 10;
else marginMm = 8;
return { layoutKey, marginMm };
}