建立 Chromium、Word 与 WPS 的可编辑内容、段落几何、页眉页脚、封面、媒体和逐页视觉比较链路。 支持封面独立分节、正文页码重启、主题页边距与横向纸张,并将自然分页差异降为诊断项。 修正代码块内边距和折叠表格单元格边框映射,14 套主题生产矩阵与六组布局视觉矩阵通过。
76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
aggregatePdfTextLines,
|
|
buildPageContentText,
|
|
normalizePdfEditableText,
|
|
normalizePdfText,
|
|
type PdfTextItemSnapshot,
|
|
} from "../src/index.js";
|
|
|
|
function item(
|
|
text: string,
|
|
x: number,
|
|
y: number,
|
|
width = 20,
|
|
height = 10,
|
|
): PdfTextItemSnapshot {
|
|
return {
|
|
text,
|
|
normalizedText: normalizePdfText(text),
|
|
bounds: { x, y, width, height },
|
|
baselineY: y + height,
|
|
hasEol: false,
|
|
};
|
|
}
|
|
|
|
describe("PDF 文本行聚合", () => {
|
|
it("按坐标聚合中文文本并规范化兼容字符", () => {
|
|
const lines = aggregatePdfTextLines(
|
|
[
|
|
item("ABC", 10, 100, 30),
|
|
item("中文", 41, 100, 20),
|
|
item("第二行", 10, 120, 40),
|
|
],
|
|
842,
|
|
);
|
|
expect(lines.map((line) => line.normalizedText)).toEqual([
|
|
"ABC中文",
|
|
"第二行",
|
|
]);
|
|
});
|
|
|
|
it("将字体 ToUnicode 中的传统户部件归一为简体字符", () => {
|
|
expect(normalizePdfText("戶⼾")).toBe("户户");
|
|
});
|
|
|
|
it("从可编辑正文契约中移除 Word 自动列表装饰符", () => {
|
|
expect(normalizePdfEditableText("• 项目一 ◦ 子项 ▪ 末项 WPS")).toBe(
|
|
"项目一子项末项WPS"
|
|
);
|
|
});
|
|
|
|
it("只在页眉页脚坐标带识别独立页码", () => {
|
|
const lines = aggregatePdfTextLines(
|
|
[
|
|
item("章节 1", 72, 400, 50),
|
|
item("— 1 —", 280, 731, 35, 14),
|
|
],
|
|
842,
|
|
);
|
|
expect(lines.map((line) => line.role)).toEqual([
|
|
"content",
|
|
"page-number",
|
|
]);
|
|
expect(buildPageContentText(lines)).toBe("章节 1");
|
|
});
|
|
|
|
it("不会把页脚中的普通说明误判为页码", () => {
|
|
const lines = aggregatePdfTextLines(
|
|
[item("内部资料 1", 72, 800, 70)],
|
|
842,
|
|
);
|
|
expect(lines[0]?.role).toBe("content");
|
|
});
|
|
});
|