(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' || el.type === 'qr') { w = el.w || 40; h = w; }
if (el.type === 'asset') { w = el.w || 30; 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 renderQR(el) {
const size = el.w || 40;
const color = el.color || '#17140f';
const light = el.light === 'none' ? null : (el.light || '#ffffff');
let qr = null;
try {
if (window.PL && window.PL.qrcode && window.PL.qrcode.build) qr = window.PL.qrcode.build(String(el.value || ' '));
} catch (e) { qr = null; }
const bg = light ? '' : '';
if (!qr) {
return bg + '';
}
const quiet = 2;
const total = qr.size + quiet * 2;
const unit = size / total;
let d = '';
for (let y = 0; y < qr.size; y += 1) {
for (let x = 0; x < qr.size; x += 1) {
if (!qr.matrix[y][x]) continue;
const px = (x + quiet) * unit;
const py = (y + quiet) * unit;
d += 'M' + px.toFixed(3) + ' ' + py.toFixed(3) + 'h' + unit.toFixed(3) + 'v' + unit.toFixed(3) + 'h-' + unit.toFixed(3) + 'z';
}
}
return bg + '';
}
function renderAsset(el) {
const lib = window.PL && window.PL.boxlib;
if (!lib || !lib.assetSvg || !el.asset) return '';
const svg = lib.assetSvg(el.asset, el.color || '#17140f');
if (!svg) return '';
const inner = svg.replace(/^