(function () {
const D = window.PL.data;
let uidCounter = 0;
const uid = function (p) { uidCounter += 1; return (p || 'u') + uidCounter; };
const measureCanvas = document.createElement('canvas');
const measureCtx = measureCanvas.getContext('2d');
function esc(s) {
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
});
}
function hexRgb(hex) {
let h = String(hex || '#000').replace('#', '');
if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];
const n = parseInt(h, 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
function shade(hex, amt) {
const rgb = hexRgb(hex);
const target = amt < 0 ? 0 : 255;
const k = Math.abs(amt);
return '#' + rgb.map(function (v) {
return Math.round(v + (target - v) * k).toString(16).padStart(2, '0');
}).join('');
}
function alpha(hex, a) {
const c = hexRgb(hex);
return 'rgba(' + c[0] + ',' + c[1] + ',' + c[2] + ',' + a + ')';
}
function fontStack(key) {
return (D.fonts[key] || D.fonts.sans).stack;
}
function fontAttr(key) {
return fontStack(key).replace(/"/g, "'");
}
function fontCss(el) {
return (el.italic ? 'italic ' : '') + (el.weight || 400) + ' ' + (el.size || 12) * 3.779527559 + 'px ' + fontStack(el.font);
}
function charWidth(ch, el) {
measureCtx.font = fontCss(el);
return measureCtx.measureText(ch).width / 3.779527559;
}
function segment(text) {
if (typeof Intl !== 'undefined' && Intl.Segmenter) {
const seg = new Intl.Segmenter('zh', { granularity: 'word' });
return Array.from(seg.segment(text), function (s) { return s.segment; });
}
return text.split(/(\s+)/).filter(function (x) { return x !== ''; });
}
function textLines(el) {
const w = Math.max(4, el.w || 60);
const size = el.size || 12;
const spacing = (el.spacing || 0) * size;
const paragraphs = String(el.text || '').split('\n');
const out = [];
paragraphs.forEach(function (p) {
const tokens = segment(p);
let line = '';
let lineW = 0;
const push = function () {
out.push(line.replace(/\s+$/, ''));
line = '';
lineW = 0;
};
tokens.forEach(function (tok) {
const tw = charWidth(tok, el) + Array.from(tok).length * spacing;
if (line && lineW + tw > w) push();
if (tw > w && Array.from(tok).length > 1) {
Array.from(tok).forEach(function (ch) {
const cw = charWidth(ch, el) + spacing;
if (line && lineW + cw > w) push();
line += ch;
lineW += cw;
});
} else {
line += tok;
lineW += tw;
}
});
out.push(line.replace(/\s+$/, ''));
});
return out.length ? out : [''];
}
function textHeight(el) {
const lines = textLines(el);
const lh = (el.lineHeight || 1.25) * (el.size || 12);
return Math.max((el.size || 12) * 1.1, lines.length * lh);
}
function elementBox(el) {
let w = el.w || 0;
let h = el.h || 0;
if (el.type === 'text') h = textHeight(el);
if (el.type === 'badge') { w = el.w || 40; h = w; }
if (el.type === 'line') h = Math.max(4, el.h || 0);
return { w: Math.max(1, w), h: Math.max(1, h) };
}
function barcodeBits(value) {
const L = ['0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011'];
const G = ['0100111', '0110011', '0011011', '0100001', '0011101', '0111001', '0000101', '0010001', '0001001', '0010111'];
const R = ['1110010', '1100110', '1101100', '1000010', '1011100', '1001110', '1010000', '1000100', '1001000', '1110100'];
const P = ['LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL'];
let digits = String(value || '').replace(/\D/g, '');
if (digits.length < 12) {
let seed = 0;
String(value || 'POUCHLAB').split('').forEach(function (c) { seed = (seed * 31 + c.charCodeAt(0)) % 9973; });
while (digits.length < 12) { seed = (seed * 37 + 11) % 10000; digits += String(seed % 10); }
}
digits = digits.slice(0, 12);
let sum = 0;
for (let i = 0; i < 12; i += 1) sum += Number(digits[i]) * (i % 2 === 0 ? 1 : 3);
const check = (10 - (sum % 10)) % 10;
const code = digits + check;
const parity = P[Number(code[0])];
let bits = '101';
for (let i = 1; i <= 6; i += 1) {
const d = Number(code[i]);
bits += parity[i - 1] === 'L' ? L[d] : G[d];
}
bits += '01010';
for (let i = 7; i <= 12; i += 1) bits += R[Number(code[i])];
bits += '101';
return { bits: bits, code: code };
}
function renderBarcode(el, ctx) {
const w = el.w || 50;
const h = el.h || 20;
const color = el.color || '#17140f';
const bars = barcodeBits(el.value || '6971234567890');
const showText = el.showText !== false;
const textH = showText ? Math.min(h * 0.22, 4.2) : 0;
const barH = h - textH - (showText ? 0.8 : 0);
const module = w / 95;
let rects = '';
let x = 0;
for (let i = 0; i < bars.bits.length; i += 1) {
if (bars.bits[i] === '1') {
rects += '';
}
x += 1;
}
const text = showText ? '' + esc(bars.code) + '' : '';
return rects + text;
}
function renderSticker(el) {
const def = D.stickers[el.sticker] || D.stickers.star;
const color = el.color || '#17140f';
const size = el.w || 30;
const scale = size / 24;
const paths = def.paths.map(function (p) {
const style = p.stroke
? 'fill="none" stroke="' + color + '" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"'
: 'fill="' + color + '"';
return '';
}).join('');
return '' + paths + '';
}
function renderBadge(el, ctx) {
const w = el.w || 40;
const color = el.color || '#17140f';
const r = w / 2 - w * 0.035;
const cid = ctx.uid + '-circle';
const size = el.size || w * 0.09;
const circumference = 2 * Math.PI * r;
return '' +
'' +
'' +
'' + esc(el.text || 'POUCHLAB · ') + '' +
'';
}
function renderText(el, ctx) {
const lines = textLines(el);
const size = el.size || 12;
const lh = (el.lineHeight || 1.25) * size;
const ls = (el.spacing || 0) * size;
const anchor = el.align === 'center' ? 'middle' : el.align === 'right' ? 'end' : 'start';
const x = el.align === 'center' ? (el.w || 60) / 2 : el.align === 'right' ? (el.w || 60) : 0;
const fill = el.color === 'none' ? 'none' : (el.color || '#17140f');
// 描边文字最小线宽:字号的 4.5%(发丝级描边在印刷预览/贴图光栅化时不可见)
const strokeW = el.stroke && el.stroke !== 'none' ? Math.max(el.strokeWidth || 0, size * 0.045) : 0;
const stroke = el.stroke && el.stroke !== 'none' ? ' stroke="' + el.stroke + '" stroke-width="' + strokeW.toFixed(3) + '"' : '';
const tspans = lines.map(function (line, i) {
return '' + esc(line) + '';
}).join('');
return tspans;
}
function renderElement(el, ctx) {
if (el.hidden) return '';
const box = elementBox(el);
const rot = el.rotation || 0;
const cx = box.w / 2;
const cy = box.h / 2;
let body = '';
if (el.type === 'image') {
if (el.src) {
const fit = el.fit === 'cover' ? 'xMidYMid slice' : 'xMidYMid meet';
const filter = el.mono ? ' filter="url(#' + ctx.uid + '-mono)"' : '';
body = '';
} else {
body = '';
}
} else if (el.type === 'text') {
body = renderText(el, ctx);
} else if (el.type === 'badge') {
body = renderBadge(el, ctx);
} else if (el.type === 'barcode') {
body = renderBarcode(el, ctx);
} else if (el.type === 'sticker') {
body = renderSticker(el);
} else if (el.type === 'shape') {
const fill = el.fill || 'none';
const stroke = el.stroke && el.stroke !== 'none' ? ' stroke="' + el.stroke + '" stroke-width="' + (el.strokeWidth || 0.5) + '"' : '';
if (el.shape === 'circle') {
body = '';
} else if (el.shape === 'line') {
body = '';
} else {
body = '';
}
}
const hit = '';
return '' + hit + body + '';
}
function defsForBag(product, uidv) {
const base = product.mockColor;
return '' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'';
}
function effectDefs(u) {
return '' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'';
}
function effectLayer(u, mat, fin, shape, W, H, clipId) {
let s = '';
if (mat === 'gloss') {
s += '' +
'';
} else if (mat === 'kraft') {
s += '';
} else if (mat === 'compost') {
s += '' +
'';
} else {
s += '';
}
if (fin === 'uv') {
s += '' +
'' +
'';
} else if (fin === 'foil') {
s += '' +
'';
} else if (fin === 'soft') {
s += '' +
'';
}
return s ? '' + s + '' : '';
}
function bagShape(product, W, H) {
const p = product.pad;
return { x: -p.l, y: -p.t, w: W + p.l + p.r, h: H + p.t + p.b, rx: product.shape === 'kraft' ? 2.5 : product.shape === 'mailer' ? 4 : 6 };
}
function overlayUI(opts, product, sideDesign, ctx) {
const W = opts.size.w;
const H = opts.size.h;
const p = product.pad;
const scale = opts.pxPerMm || 3;
const hs = 5.2 / scale;
const sw = 1 / scale;
let s = '';
if (opts.showGuides !== false) {
s += '';
s += '';
}
if (opts.showDims !== false) {
const lx = -p.l - 13;
const by = H + p.b + 13;
const fs = 4.2;
s += '' +
'' +
'' +
'' +
'' +
'' +
'' + H + ' mm' +
'' + W + ' mm';
// 侧折边宽度(左右各一)与顶部封边高度标注
if (p.l > 2) {
const gx = -p.l / 2;
s += '' +
'' +
'折边 ' + Math.round(p.l) + '';
const gx2 = W + p.r / 2;
s += '' +
'' +
'折边 ' + Math.round(p.r) + '';
}
if (p.t > 2) {
const gy = -p.t / 2;
s += '' +
'' +
'封边 ' + Math.round(p.t) + '';
}
// 印刷面尺寸水印(面内底部居中)
s += '面 ' + W + ' × ' + H + ' mm';
}
if (opts.guides) {
if (opts.guides.x != null) s += '';
if (opts.guides.y != null) s += '';
}
const sel = opts.selectedId && sideDesign ? sideDesign.elements.find(function (e) { return e.id === opts.selectedId; }) : null;
if (sel) {
const box = elementBox(sel);
const rot = sel.rotation || 0;
const cx = box.w / 2;
const cy = box.h / 2;
let handles = '';
const pts = [['nw', 0, 0], ['ne', box.w, 0], ['sw', 0, box.h], ['se', box.w, box.h]];
if (sel.type === 'text') pts.push(['w', 0, box.h / 2], ['e', box.w, box.h / 2]);
pts.forEach(function (h) {
handles += '';
});
const ro = 26 / scale;
handles += '';
handles += '';
const label = box.w.toFixed(0) + ' × ' + box.h.toFixed(0) + ' mm';
const labelW = label.length * 5.4 / scale + 6 / scale;
handles += '' +
'' +
'' + label + '';
s += '' +
'' + handles + '';
}
return s;
}
function bagSVG(product, side, sideDesign, opts) {
opts = opts || {};
const size = opts.size || D.sizeOf(product, 'm');
const W = size.w;
const H = size.h;
const p = product.pad;
const u = uid('bag');
const ctx = { uid: u };
const tight = !!opts.tight;
const card = !!opts.card;
const mat = opts.material || 'matte';
const fin = opts.finish || 'none';
const extra = card ? 9 : 0;
const vb = tight
? { x: -p.l, y: -p.t, w: W + p.l + p.r, h: H + p.t + p.b }
: { x: -p.l - (opts.ui ? 18 : extra), y: -p.t - (opts.ui ? 12 : extra), w: W + p.l + p.r + (opts.ui ? 30 : extra * 2), h: H + p.t + p.b + (opts.ui ? 34 : extra * 2) };
const shape = bagShape(product, W, H);
const base = product.mockColor;
const design = sideDesign || { bg: null, elements: [] };
const clipId = u + '-bagclip';
const printId = u + '-printclip';
const elements = (design.elements || []).map(function (el) { return renderElement(el, ctx); }).join('');
let body = '';
body += '';
body += '';
if (product.shape === 'kraft') body += '';
// 设计底色铺满整个袋身(含左右侧折边):真实包装侧折与正面同膜同底色
if (design.bg) body += '';
body += '';
body += elements;
body += '';
body += '' +
'' +
'' +
'' +
'' +
'';
body += effectLayer(u, mat, fin, shape, W, H, clipId);
const seal = p.t * 0.48;
if (product.shape === 'mailer') {
body += '';
body += '';
body += '';
} else if (product.shape === 'garment') {
body += '';
body += '';
body += '';
} else {
body += '';
body += '';
body += '';
body += '';
if (product.shape === 'kraft') {
body += '';
body += '';
body += '';
}
}
// 自封袋族顶部胶条(压敏双胶带 + 封口折线;对标行业袋型结构)
if (product.shape === 'standup' || product.shape === 'flatbottom' || product.shape === 'coffee') {
const stripH = Math.max(2.6, p.t * 0.15);
const stripX = shape.x + shape.w * 0.07;
const stripW = shape.w * 0.86;
const y0 = shape.y + p.t * 0.18;
if (side === 'front') {
// 两条压敏胶带
body += '';
body += '';
body += '';
// 封口折线
body += '';
} else {
// 背面:Folded Flap 翻折盖虚线区
const flapH = p.t * 0.92;
body += '';
body += 'Folded Flap';
}
}
if (product.shape === 'coffee' && side === 'front') {
body += '';
body += '';
body += '';
}
if (product.shape === 'standup') {
body += '';
body += '';
} else if (product.shape === 'flatbottom') {
body += '';
body += '';
body += '';
}
body += '';
const defs = defsForBag(product, u).replace('',
'' +
'') + effectDefs(u);
const ui = opts.ui ? overlayUI(opts, product, design, ctx) : '';
const vbStr = vb.x + ' ' + vb.y + ' ' + vb.w + ' ' + vb.h;
return '';
}
function designSVG(design, W, H) {
const u = uid('art');
const ctx = { uid: u };
const elements = (design.elements || []).map(function (el) { return renderElement(el, ctx); }).join('');
return '';
}
function designFromTemplate(templateId, W, H, side) {
const t = D.templateById(templateId);
if (!t) return { bg: null, elements: [] };
const source = side === 'back' ? (t.back || []) : (t.front || []);
const elements = source.map(function (item) {
const e = Object.assign({}, item);
e.id = uid('el');
e.x = (item.x || 0) * W;
e.y = (item.y || 0) * H;
if (e.type === 'text') {
e.w = (item.w || 1) * W;
e.size = (item.size || 0.04) * W;
e.spacing = item.spacing || 0;
e.lineHeight = item.lineHeight || 1.25;
e.strokeWidth = (item.sw || 0) * W;
delete e.sw;
} else if (e.type === 'shape') {
e.w = (item.w || 0.2) * W;
e.h = item.shape === 'line' ? 4 : (item.h == null ? 0.1 : item.h) * H;
e.strokeWidth = (item.sw || 0) * W;
e.radius = (item.radius || 0) * W;
delete e.sw;
} else if (e.type === 'sticker') {
e.w = (item.w || 0.2) * W;
e.h = e.w;
} else if (e.type === 'badge') {
e.w = (item.w || 0.3) * W;
e.h = e.w;
e.size = (item.size || 0.03) * W;
} else if (e.type === 'barcode') {
e.w = (item.w || 0.5) * W;
e.h = (item.h || 0.12) * H;
}
e.rotation = 0;
return e;
});
return { bg: t.bg, elements: elements };
}
function templatePreview(template, productOverride) {
const product = D.productById(productOverride || template.product);
const size = D.sizeOf(product, product.sizes[Math.floor((product.sizes.length - 1) / 2)].id);
const design = designFromTemplate(template.id, size.w, size.h, 'front');
return bagSVG(product, 'front', design, { size: size, card: true, tight: false });
}
function sampleDesign(product, seed) {
const pool = D.templates.filter(function (t) { return t.product === product.id; });
if (!pool.length) pool.push(D.templateById('citrus'));
const t = pool[(seed || 0) % pool.length];
const size = D.sizeOf(product, product.sizes[Math.floor((product.sizes.length - 1) / 2)].id);
return { size: size, design: designFromTemplate(t.id, size.w, size.h, 'front'), template: t };
}
window.PL.render = {
uid: uid,
esc: esc,
shade: shade,
alpha: alpha,
fontStack: fontStack,
textLines: textLines,
textHeight: textHeight,
elementBox: elementBox,
elementSVG: renderElement,
bagSVG: bagSVG,
designFromTemplate: designFromTemplate,
designSVG: designSVG,
templatePreview: templatePreview,
sampleDesign: sampleDesign,
barcodeBits: barcodeBits
};
})();