Repository navigation
Expand file tree
/
Copy pathstudio.html
More file actions
319 lines (300 loc) · 14.5 KB
/
Copy pathstudio.html
File metadata and controls
319 lines (300 loc) · 14.5 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>Filzra 本地模板工作台</title>
<style>
@font-face{font-family:'CourierLocal';src:local('Courier New'),url('./fonts/cour_clean.ttf') format('truetype')}
@font-face{font-family:'OCRBReal';src:url('./fonts/OCR-b-clean.ttf') format('truetype')}
@font-face{font-family:'SimSunReal';src:local('SimSun'),local('NSimSun'),url('./fonts/simsun_clean.ttf') format('truetype')}
@font-face{font-family:'SimSunLocal';src:url('./fonts/simsun_clean.ttf') format('truetype')}
@font-face{font-family:'XingkaiSig';src:url('./fonts/STXINGKA.TTF') format('truetype')}
:root{--bg:#0f1419;--card:#1a2129;--line:#2a3441;--ink:#e6edf3;--muted:#8b98a5;--acc:#10b981}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--ink);font:14px/1.5 "Segoe UI","Microsoft YaHei",sans-serif}
header{padding:10px 16px;border-bottom:1px solid var(--line);display:flex;gap:12px;align-items:center;flex-wrap:wrap;position:sticky;top:0;background:var(--bg);z-index:9}
h1{font-size:16px;margin:0;font-weight:700}
.badge{background:#16382c;color:var(--acc);border:1px solid #1f6f54;border-radius:999px;padding:2px 10px;font-size:12px}
main{display:grid;grid-template-columns:250px minmax(0,1fr) 330px;gap:12px;padding:12px;height:calc(100vh - 58px)}
.panel{background:var(--card);border:1px solid var(--line);border-radius:10px;display:flex;flex-direction:column;min-height:0}
.panel h2{font-size:13px;margin:0;padding:10px 12px;border-bottom:1px solid var(--line);color:var(--muted);font-weight:600}
.scroll{overflow:auto;padding:10px;flex:1;min-height:0;overflow-x:hidden}
#fields{overflow-x:hidden}
input,select,button,textarea{font:inherit;color:var(--ink);background:#111820;border:1px solid var(--line);border-radius:7px;padding:7px 9px;outline:none}
input:focus,select:focus{border-color:var(--acc)}
button{cursor:pointer;background:#1f2a36}
button:hover{border-color:var(--acc)}
button.primary{background:var(--acc);color:#04140e;border:none;font-weight:700}
.tpl{padding:7px 9px;border-radius:7px;cursor:pointer;display:flex;gap:8px;align-items:center;font-size:13px}
.toolbar{display:flex;gap:8px;padding:10px;border-top:1px solid var(--line);flex-wrap:wrap;align-items:center}
.toolbar .grow{flex:1;min-width:0}
@media (max-width:1500px){
main{grid-template-columns:210px minmax(0,1fr) 300px;gap:10px}
.row{grid-template-columns:84px minmax(0,1fr)}
}
@media (max-width:1250px){
main{grid-template-columns:180px minmax(0,1fr) 270px}
.tpl .slug{display:none}
}
.tpl:hover{background:#202b36}
.tpl.active{background:#16382c;color:#a7f3d0}
.tpl .slug{color:var(--muted);font-size:11px}
#stage{display:flex;align-items:center;justify-content:center;padding:14px;min-height:0;overflow:auto}
canvas{max-width:100%;max-height:100%;box-shadow:0 8px 30px #0008;background:#fff}
.row{display:grid;grid-template-columns:92px minmax(0,1fr);gap:8px;align-items:center;margin-bottom:6px;width:100%}
.row input{width:100%;min-width:0;box-sizing:border-box}
.row label{color:var(--muted);font-size:12px;text-align:right;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.hint{color:var(--muted);font-size:12px;padding:8px 10px;border-top:1px solid var(--line)}
.toolbar{display:flex;gap:8px;padding:10px;border-top:1px solid var(--line);flex-wrap:wrap}
.chip{font-size:11px;color:var(--muted)}
#status{font-size:12px;color:var(--acc)}
</style>
</head>
<body>
<header>
<h1>Filzra 本地模板工作台</h1>
<span class="badge" id="stat">加载中…</span>
<input id="q" placeholder="搜索模板 slug / 名称" style="width:240px">
<select id="cat"><option value="">全部类别</option></select>
<span id="status"></span>
</header>
<main>
<section class="panel">
<h2>模板(200)</h2>
<div class="scroll" id="list"></div>
</section>
<section class="panel">
<h2>预览 <span id="curName" style="color:var(--acc)"></span></h2>
<div id="stage"><canvas id="cv"></canvas></div>
<div class="toolbar">
<button class="primary" id="btnPng">导出 PNG(当前尺寸)</button>
<button id="btnJpg">导出 JPEG(扫描质感)</button>
<label class="chip" style="display:flex;gap:6px;align-items:center">
输出倍率 <select id="scale"><option>1</option><option selected>2</option><option>3</option><option>4</option></select>
</label>
<label class="chip" style="display:flex;gap:6px;align-items:center">
<input type="checkbox" id="wm"> 叠加「测试模板」水印
</label>
<label class="chip" style="display:flex;gap:6px;align-items:center">
<input type="checkbox" id="scan"> 扫描质感(颗粒+降对比)
</label>
</div>
<div class="hint">点字段右侧输入框实时改稿;人像槽位上传任意照片,自动按 layout.photo 裁切。文字超出字段可用宽度时会自动缩字号,多行地址按 maxCharsPerLine 折行。所有渲染都在本机 canvas 完成。</div>
</section>
<section class="panel">
<h2>字段 / 数据</h2>
<div class="scroll" id="fields"></div>
<div class="hint" id="meta"></div>
</section>
</main>
<script>
const KYC = './kyc/';
let LIMIT = {};
const state = { reg: [], layout: null, slug: null, base: null, overlay: null, photo: null, values: {} };
const $ = (id) => document.getElementById(id);
const p2 = (n) => String(n).padStart(2, '0');
async function boot() {
state.reg = await (await fetch(KYC + 'registry.json')).json();
try { LIMIT = await (await fetch('./scripts/limit.json')).json(); } catch { LIMIT = {}; }
const cats = [...new Set(state.reg.map(t => t.category))];
cats.forEach(c => $('cat').add(new Option(c, c)));
renderList();
$('q').oninput = renderList;
$('cat').onchange = renderList;
$('btnPng').onclick = () => exportImg('png');
$('btnJpg').onclick = () => exportImg('jpeg');
['wm','scan','scale'].forEach(id => $(id).onchange = draw);
$('stat').textContent = state.reg.length + ' 套模板就绪';
load(state.reg[0].slug);
}
function renderList() {
const q = $('q').value.trim().toLowerCase();
const cat = $('cat').value;
const items = state.reg.filter(t =>
(!cat || t.category === cat) &&
(!q || (t.slug + ' ' + t.name + ' ' + (t.zh || '')).toLowerCase().includes(q)));
$('list').innerHTML = items.map(t => `
<div class="tpl ${t.slug === state.slug ? 'active' : ''}" data-s="${t.slug}">
<span>${t.flag || '📄'}</span>
<span style="flex:1;min-width:0">
<div style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap">${t.zh || t.name}</div>
<div class="slug">${t.slug}</div>
</span>
</div>`).join('') || '<div class="hint">无匹配</div>';
[...document.querySelectorAll('.tpl')].forEach(el => el.onclick = () => load(el.dataset.s));
}
function loadImg(src) {
return new Promise((res, rej) => { const i = new Image(); i.onload = () => res(i); i.onerror = rej; i.src = src; });
}
async function load(slug) {
state.slug = slug;
renderList();
$('status').textContent = '读取中…';
const layout = await (await fetch(`${KYC}${slug}-layout.json`)).json();
state.layout = layout;
state.values = Object.assign({}, (layout.sampleData || [])[0] || {});
try { state.base = await loadImg(`${KYC}${slug}-base.png`); } catch { state.base = null; }
try { state.overlay = await loadImg(`${KYC}${slug}-overlay.png`); } catch { state.overlay = null; }
$('curName').textContent = `${layout.name || slug}`;
$('meta').textContent = `源文件 ${layout.sourceFile || '-'} · ${layout.width}×${layout.height} · 字段 ${Object.keys(layout.fields || {}).length} 个` + (layout.photo ? ' · 含人像槽' : '');
renderFields();
draw();
$('status').textContent = '就绪';
}
const SHORT = { sur:'surname', giv:'givenName', pno:'passportNo', dob:'dob', exp:'exp', iss:'iss', bp:'birthplace', sex:'sex', idno:'idNo', cn:'cnName' };
function renderFields() {
const l = state.layout;
const f = l.fields || {};
const keys = Object.keys(f);
const tiles = keys.map(k => `
<div class="row">
<label title="${k}">${k}</label>
<input data-k="${k}" value="${(state.values[k] ?? '').toString().replace(/"/g,'"')}">
</div>`).join('');
const extras = ['sur','giv','pno','dob','exp','iss','bp','sex','idno'].filter(k => !keys.includes(SHORT[k]))
.map(k => `<div class="row"><label>${k}</label><input data-k="${k}" value="${(state.values[k] ?? '')}"></div>`).join('');
$('fields').innerHTML = `
<div style="margin-bottom:10px;display:flex;gap:8px">
<input id="photo" type="file" accept="image/*" style="flex:1">
</div>
${tiles}${extras}`;
[...document.querySelectorAll('#fields input[data-k]')].forEach(inp => inp.oninput = () => {
state.values[inp.dataset.k] = inp.value;
const short = Object.keys(SHORT).find(s => SHORT[s] === inp.dataset.k);
if (short) state.values[short] = inp.value;
draw();
});
$('photo').onchange = e => {
const file = e.target.files[0]; if (!file) return;
const r = new FileReader();
r.onload = () => { const im = new Image(); im.onload = () => { state.photo = im; draw(); }; im.src = r.result; };
r.readAsDataURL(file);
};
}
function mrz(ss) {
const A = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', w = [7,3,1];
let c = 0;
[...ss.toUpperCase()].forEach((ch, i) => {
const v = ch === '<' ? 0 : /[0-9]/.test(ch) ? +ch : A.indexOf(ch);
c += Math.max(0, v) * w[i % 3];
});
return c % 10;
}
function draw() {
const l = state.layout; if (!l) return;
const cv = $('cv'), W = l.width, H = l.height;
cv.width = W; cv.height = H;
const ctx = cv.getContext('2d');
ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, W, H);
if (state.base) ctx.drawImage(state.base, 0, 0, W, H);
if (l.photo && state.photo) {
const {x,y,w,h} = l.photo;
ctx.save(); ctx.beginPath(); ctx.rect(x,y,w,h); ctx.clip();
const s = Math.max(w / state.photo.width, h / state.photo.height);
const dw = state.photo.width * s, dh = state.photo.height * s;
ctx.drawImage(state.photo, x + (w-dw)/2, y + (h-dh)/2, dw, dh);
ctx.restore();
}
let v = state.values;
if (l.fields && l.fields.mrz1) {
const sur = (v.sur||v.surname||'').toUpperCase(), giv = (v.giv||v.givenName||'').toUpperCase();
const nm = (sur + '<<' + giv.replace(/ /g,'<')).slice(0,39);
v = Object.assign({}, v, { mrz1: ('P<' + (l.countryCode||'XXX') + nm).padEnd(44,'<').slice(0,44) });
}
for (const [k, f] of Object.entries(l.fields || {})) {
if (!f || f.x == null || f.hidden) continue;
const t = v[k] != null ? String(v[k]) : '';
if (!t) continue;
ctx.save();
ctx.fillStyle = f.color || '#1b1b1b';
ctx.font = f.font || '20px sans-serif';
try { if (f.letterSpacing) ctx.letterSpacing = f.letterSpacing; } catch {}
// 自动限宽
const pad = 8;
const lm = (LIMIT[state.slug] || {})[k] || {};
const F = Object.assign({}, f, lm);
const avail = (F.maxWidth && F.maxWidth > 0) ? F.maxWidth : Math.max(10, W - F.x - pad);
const shrink = (str) => {
let tw = ctx.measureText(str).width;
if (tw <= avail) return tw;
const m = /([\d.]+)px/.exec(F.font || '');
const base = m ? parseFloat(m[1]) : 16;
ctx.font = (f.font || '').replace(/[\d.]+px/, Math.max(6, base * (avail / tw)).toFixed(2) + 'px');
if (f.letterSpacing) {
const ls = parseFloat(f.letterSpacing) || 0;
try { ctx.letterSpacing = (ls * (avail / tw)).toFixed(2) + 'px'; } catch {}
}
return ctx.measureText(str).width;
};
if (F.maxCharsPerLine || F.lineHeight) {
// 多行字段(地址等):按 maxCharsPerLine 折行;行数超限则整段缩字号
const cpl = F.maxCharsPerLine || 30, lh = F.lineHeight || 20;
const maxLines = F.maxLines || Math.floor((H - F.y - 6) / lh);
const words = t.split(/\s+/), lines = [];
let cur = '';
for (const w of words) {
const test = cur ? cur + ' ' + w : w;
if (test.length > cpl && cur) { lines.push(cur); cur = w; } else cur = test;
}
if (cur) lines.push(cur);
if (lines.length > maxLines) {
// 行数爆了:先缩字号,再截断
const mm = /([\d.]+)px/.exec(F.font || '');
const base = mm ? parseFloat(mm[1]) : 14;
const k2 = Math.max(0.6, maxLines / lines.length);
ctx.font = (F.font || '').replace(/[\d.]+px/, (base * k2).toFixed(2) + 'px');
if (F.letterSpacing) { const ls = parseFloat(F.letterSpacing) || 0; try { ctx.letterSpacing = (ls * k2).toFixed(2) + 'px'; } catch {} }
}
const shown = lines.slice(0, maxLines);
shown.forEach((ln, i) => {
if (F.y + i * lh > H - 4) return;
shrink(ln);
ctx.fillText(ln, F.x, F.y + i * lh);
});
} else {
shrink(t);
if (F.rotation) { ctx.translate(F.x, F.y); ctx.rotate(F.rotation * Math.PI/180); ctx.fillText(t, 0, 0); }
else ctx.fillText(t, F.x, F.y);
}
ctx.restore();
}
if (state.overlay) { ctx.save(); ctx.globalAlpha = .5; ctx.drawImage(state.overlay, 0, 0, W, H); ctx.restore(); }
if ($('scan').checked) {
ctx.save(); ctx.globalAlpha = .18; ctx.fillStyle = '#7a6a52'; ctx.fillRect(0,0,W,H); ctx.restore();
for (let i = 0; i < 2600; i++) {
ctx.fillStyle = `rgba(40,35,28,${Math.random()*.16})`;
ctx.fillRect(Math.random()*W, Math.random()*H, 1.2, 1.2);
}
}
if ($('wm').checked) {
ctx.save();
ctx.translate(W/2, H/2); ctx.rotate(-0.48);
const fs = Math.max(18, Math.round(Math.min(W,H) * .044));
ctx.font = `800 ${fs}px "Microsoft YaHei",sans-serif`;
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillStyle = 'rgba(220,38,38,.28)';
const d = Math.ceil(Math.hypot(W,H) * .68), sx = fs*5.6, sy = fs*3.5;
for (let y = -d, row = 0; y <= d; y += sy, row++)
for (let x = -d; x <= d; x += sx) ctx.fillText('测试模板', x + (row%2?sx/2:0), y);
ctx.restore();
}
}
function exportImg(kind) {
const l = state.layout; if (!l) return;
const mul = +$('scale').value || 1;
const src = $('cv');
const out = document.createElement('canvas');
out.width = l.width * mul; out.height = l.height * mul;
const c = out.getContext('2d');
c.imageSmoothingQuality = 'high';
c.drawImage(src, 0, 0, out.width, out.height);
const name = `${state.slug}${$('scan').checked ? '-scan' : ''}${$('wm').checked ? '-wm' : ''}@${mul}x.${kind === 'jpeg' ? 'jpg' : 'png'}`;
const a = document.createElement('a');
a.download = name;
a.href = out.toDataURL(kind === 'jpeg' ? 'image/jpeg' : 'image/png', kind === 'jpeg' ? .92 : undefined);
a.click();
}
boot();
</script>
</body>
</html>