import { canvas, dimensions, encode, escapeHTML, raster } from '../src/io.js';
import { zipSync, strToU8 } from 'fflate';
export { canvas, dimensions, encode, raster };
export const num = (key, label, value, min, max, step = 1) => ({ key, label, value, min, max, step, type: 'number' });
export const text = (key, label, value, multiline = false) => ({ key, label, value, type: multiline ? 'textarea' : 'text' });
export const color = (key, label, value) => ({ key, label, value, type: 'color' });
export const choice = (key, label, options, value = options[0][0]) => ({ key, label, options, value, type: 'select' });
export const width = () => num('width', '宽度(px)', 512, 32, 2048);
export const height = () => num('height', '高度(px)', 384, 32, 2048);
export const foreground = () => color('foreground', '前景色', '#167d68');
export const background = () => color('background', '背景色', '#ffffff');
export function controls(fields) {
return fields.map((f) => `<label class="field"><span>${escapeHTML(f.label)}</span>${f.type === 'textarea' ? `<textarea name="${f.key}" maxlength="${f.maxLength || 20000}" required>${escapeHTML(f.value)}</textarea>` : f.type === 'select' ? `<select name="${f.key}">${f.options.map(([v, l]) => `<option value="${escapeHTML(v)}" ${v === f.value ? 'selected' : ''}>${escapeHTML(l)}</option>`).join('')}</select>` : `<input name="${f.key}" type="${f.type}" value="${escapeHTML(f.value)}" ${f.type === 'number' ? `min="${f.min}" max="${f.max}" step="${f.step}"` : 'maxlength="2000"'} required>`}</label>`).join('');
}
export const file = (blob, name, note = '') => ({ blob, name, note });
export const json = (data, name = 'result.json') => file(new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }), name);
export const plain = (value, name, type = 'text/plain') => file(new Blob([value], { type }), name);
export async function png(node, name = 'result.png') { return file(await encode(node), name, `${node.width} × ${node.height}`); }
export async function zip(entries, name = 'images.zip') {
const values = {}; let total = 0;
for (const entry of entries) { const data = entry.blob ? new Uint8Array(await entry.blob.arrayBuffer()) : strToU8(entry.text); total += data.length; if (total > 64 * 1024 * 1024) throw new Error('导出总量超过 64 MB,请减少文件或尺寸。'); values[entry.name] = data; }
return file(new Blob([zipSync(values, { level: 6 })], { type: 'application/zip' }), name);
}
export function source(images, maxPixels = 4_000_000) {
const image = images[0]; if (!image) throw new Error('请先选择图片。');
if (image.width * image.height > maxPixels) throw new Error(`本工具处理上限 ${maxPixels / 1_000_000} MP,请先缩小图片。`);
return raster(image);
}
export function pixels(node) { return node.getContext('2d', { willReadFrequently: true }).getImageData(0, 0, node.width, node.height); }
export const luminance = (r, g, b) => 0.2126 * r + 0.7152 * g + 0.0722 * b;
export function rgb(hex) { return [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)); }
export function wrap(ctx, value, x, y, maxWidth, lineHeight, maxLines = 30) {
const lines = []; let line = '';
for (const char of value) {
if (char === '\n' || (line && ctx.measureText(line + char).width > maxWidth)) { lines.push(line); line = char === '\n' ? '' : char; } else line += char;
}
if (line) lines.push(line);
if (lines.length > maxLines) throw new Error('文字超出可用行数,请减少文字或缩小字号。');
lines.forEach((v, i) => ctx.fillText(v, x, y + i * lineHeight));
return lines.length;
}
export function fit(ctx, image, x, y, w, h, cover = false, fx = 0.5, fy = 0.5) {
const scale = cover ? Math.max(w / image.width, h / image.height) : Math.min(w / image.width, h / image.height);
const dw = image.width * scale; const dh = image.height * scale;
ctx.save(); ctx.beginPath(); ctx.rect(x, y, w, h); ctx.clip(); ctx.drawImage(image.image || image, x + (w - dw) * fx, y + (h - dh) * fy, dw, dh); ctx.restore();
}
export function requireCount(images, count) { if (images.length !== count) throw new Error(`请准确选择 ${count} 张图片。`); }