fix(label-designer): 修复导入PDF文字重影
- 根因: 文字层提取后底图仍保留原字 -> 原字+叠加字两层同位(阴影/叠字) - 修复: 提取时在 toDataURL 之前把底图对应文字擦白, 底图只留版式/条码/logo - 只在近白低饱和背景擦除(中位亮度>=205且色差<=40), 彩色/深色区保留原字不叠加, 避免白块 验证: 基准PDF墨迹 0.15->0; ICLR 抽检9/9擦除; 视觉清晰单份无重影 ✓ CHANGELOG记录
This commit is contained in:
1 parent
8f10f1f47b
commit
ea6acf3f5f
3 files changed
+66
-19
No files matched your search
@@ -345,9 +345,33 @@ async function onPdfFile(e: Event) {
|
||||
ctx.fillStyle = '#ffffff';
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
await page.render({ canvasContext: ctx, viewport }).promise;
|
||||
const src = canvas.toDataURL('image/png');
|
||||
|
||||
// 提取文字层:把 PDF 里的文字转成可编辑文本元素(按基线分行合并)
|
||||
// 提取文字层:把 PDF 里的文字转成可编辑文本元素(按基线分行合并),
|
||||
// 同时把底图上对应的文字擦掉,避免"原字 + 叠加字"形成重影
|
||||
function boxBackground(xMm: number, yMm: number, wMm: number, hMm: number) {
|
||||
const k = EXPORT_DPI / 25.4;
|
||||
let x = Math.max(0, Math.round(xMm * k));
|
||||
let y = Math.max(0, Math.round(yMm * k));
|
||||
let w = Math.min(canvas.width - x, Math.round(wMm * k));
|
||||
let h = Math.min(canvas.height - y, Math.round(hMm * k));
|
||||
if (w <= 0 || h <= 0) {
|
||||
return { lum: 255, spread: 0 };
|
||||
}
|
||||
const d = ctx.getImageData(x, y, w, h).data;
|
||||
const lum: number[] = [];
|
||||
const spr: number[] = [];
|
||||
for (let i = 0; i < d.length; i += 4) {
|
||||
const r = d[i];
|
||||
const g = d[i + 1];
|
||||
const b = d[i + 2];
|
||||
lum.push(r * 0.299 + g * 0.587 + b * 0.114);
|
||||
spr.push(Math.max(r, g, b) - Math.min(r, g, b));
|
||||
}
|
||||
lum.sort((a, b2) => a - b2);
|
||||
spr.sort((a, b2) => a - b2);
|
||||
const mid = Math.floor(lum.length / 2);
|
||||
return { lum: lum[mid], spread: spr[mid] };
|
||||
}
|
||||
const textEls: LabelElement[] = [];
|
||||
try {
|
||||
const PT = 25.4 / 72;
|
||||
@@ -399,26 +423,42 @@ async function onPdfFile(e: Event) {
|
||||
if (!str || textEls.length >= 300) {
|
||||
continue;
|
||||
}
|
||||
const wMM = Math.max(6, (x1 - x0) * PT + 2); // 略放宽,避免 pre-wrap 换行被裁
|
||||
const hMM = Math.max(3, fs * 1.4 * PT);
|
||||
const yMM = (g.y - fs * 0.9) * PT;
|
||||
textEls.push({
|
||||
id: newId(),
|
||||
type: 'text',
|
||||
x: Math.max(0, Math.round(x0 * PT * 10) / 10),
|
||||
y: Math.max(0, Math.round(yMM * 10) / 10),
|
||||
w: Math.round(wMM * 10) / 10,
|
||||
h: Math.round(hMM * 10) / 10,
|
||||
text: str,
|
||||
fontSize: Math.round(fs * 10) / 10,
|
||||
bold: false,
|
||||
align: 'left',
|
||||
color: '#000000',
|
||||
});
|
||||
const txtW = Math.max(1, (x1 - x0) * PT); // 精确文字宽度(用于擦除)
|
||||
const elW = Math.max(6, txtW + 2); // 元素显示宽度,+2 防 pre-wrap 换行被裁
|
||||
const elH = Math.max(3, fs * 1.4 * PT);
|
||||
const elY = (g.y - fs * 0.9) * PT;
|
||||
const elX = Math.max(0, x0 * PT);
|
||||
// 只在「近白、低饱和」背景处擦除底图原字——彩色/深色区域保留原字且不叠加,避免白块与重影
|
||||
const bg = boxBackground(elX, elY, txtW, elH);
|
||||
if (bg.lum >= 205 && bg.spread <= 40) {
|
||||
const kk = EXPORT_DPI / 25.4;
|
||||
ctx.fillStyle = '#ffffff';
|
||||
ctx.fillRect(
|
||||
Math.max(0, Math.round(elX * kk)),
|
||||
Math.max(0, Math.round(elY * kk)),
|
||||
Math.max(1, Math.round(txtW * kk)),
|
||||
Math.max(1, Math.round(elH * kk)),
|
||||
);
|
||||
textEls.push({
|
||||
id: newId(),
|
||||
type: 'text',
|
||||
x: Math.round(elX * 10) / 10,
|
||||
y: Math.max(0, Math.round(elY * 10) / 10),
|
||||
w: Math.round(elW * 10) / 10,
|
||||
h: Math.round(elH * 10) / 10,
|
||||
text: str,
|
||||
fontSize: Math.round(fs * 10) / 10,
|
||||
bold: false,
|
||||
align: 'left',
|
||||
color: '#000000',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { /* 无文字层或提取失败:仅保留底图 */ }
|
||||
|
||||
const src = canvas.toDataURL('image/png'); // 擦除原字后再生成底图
|
||||
|
||||
doc.value = {
|
||||
widthMM: wMM,
|
||||
heightMM: hMM,
|
||||
|
||||
Reference in new issue
Block a user