Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/development/AGENT_RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,9 @@ Closes #123
完成 PPTX 图表提取功能:

改动文件:
- D:/Trans2Former/public/formats/pptx-input.js
- D:/Trans2Former/scripts/conversion-snapshot-test.js
- D:/Trans2Former/docs/FORMAT_ROADMAP.md
- D:/Projects/Trans2Former/public/formats/pptx-input.js
- D:/Projects/Trans2Former/scripts/conversion-snapshot-test.js
- D:/Projects/Trans2Former/docs/FORMAT_ROADMAP.md

完成功能:
- 添加 PPTX 柱状图和折线图解析
Expand Down
25 changes: 25 additions & 0 deletions scripts/format-validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ const sampleInputs = {
// Exercise PDF routes with the same pre-expanded input used by the workbench.
sampleInputs.pdf = await expandPdfContentForTextExtraction(sampleInputs.pdf);

// PDF 文本提取能力探针:pdfjs-dist 是可选依赖,npm ci 对 optionalDependency 安装失败不报错,
// 环境可能缺少它(Enhanced CI 曾在 main 的 Node 24 任务遇到)。此时核心解析器对本样例取不到正文,
// pdf → X 应诚实抛出 PDF_TEXT_UNAVAILABLE(而不是旧行为那样导出占位提示)。
// 探针在该环境下把矩阵中的 pdf 输入用例记为跳过;若 pdfjs 可用却取不到正文,按回归失败处理。
const pdfjsInstalled = await import("pdfjs-dist").then(() => true, () => false);
let pdfTextExtractionAvailable = true;
if (!pdfjsInstalled) {
try {
convertContent({ content: sampleInputs.pdf, from: "pdf", to: "txt", title: "probe.pdf", fileName: "probe.pdf" });
} catch (error) {
if (error?.code === "PDF_TEXT_UNAVAILABLE") {
pdfTextExtractionAvailable = false;
} else {
throw error;
}
}
if (!pdfTextExtractionAvailable) {
console.log("⊘ 当前环境无 PDF 文本提取能力(pdfjs-dist 未安装且核心解析器无可用正文);pdf → * 用例跳过,PDF_TEXT_UNAVAILABLE 错误码已断言。");
}
}

// 所有支持的格式
const formats = {
input: ["txt", "md", "html", "csv", "json", "xml", "docx", "xlsx", "epub", "pptx", "pdf", "doc"],
Expand Down Expand Up @@ -159,6 +180,10 @@ for (const from of formats.input) {
stats.skipped++;
continue;
}
if (from === "pdf" && !pdfTextExtractionAvailable) {
stats.skipped++;
continue;
}
stats.total++;
try {
const testData = sampleInputs[from] || testCases[from] || `Test content for ${from}`;
Expand Down
184 changes: 94 additions & 90 deletions scripts/ocr-baseline-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,99 +979,103 @@ function makeStubEngine(overrides = {}) {
}

// 29c. Mixed PDF OCR handles only textless pages and retains page order.
{
const pdf = writePdfHighFidelity({
model: {
title: "mixed-text-scan",
fixedLayout: {
pages: [
{ size: { width: 612, height: 792 }, textRuns: [
{ text: "FIRST", bbox: { x: 100, y: 700, w: 100, h: 12 }, fontSize: 12 },
] },
{ size: { width: 612, height: 792 }, textRuns: [] },
{ size: { width: 612, height: 792 }, textRuns: [
{ text: "LAST", bbox: { x: 100, y: 700, w: 100, h: 12 }, fontSize: 12 },
] },
],
// 依赖 pdfjs-dist 提取文本页;可选依赖缺失的环境(npm ci 可选安装失败)跳过本组。
const pdfjsAvailableForMixed = await import("pdfjs-dist").then(() => true, () => false);
if (pdfjsAvailableForMixed) {
const pdf = writePdfHighFidelity({
model: {
title: "mixed-text-scan",
fixedLayout: {
pages: [
{ size: { width: 612, height: 792 }, textRuns: [
{ text: "FIRST", bbox: { x: 100, y: 700, w: 100, h: 12 }, fontSize: 12 },
] },
{ size: { width: 612, height: 792 }, textRuns: [] },
{ size: { width: 612, height: 792 }, textRuns: [
{ text: "LAST", bbox: { x: 100, y: 700, w: 100, h: 12 }, fontSize: 12 },
] },
],
},
},
},
});
const detection = await isScannedPdf(pdf.data);
assert.equal(detection.reason, "mixed-text-and-textless-pages");
assert.deepEqual(detection.pageIndices, [1]);
const rasterizedPages = [];
setPdfPageRasterizer({
async countPages() { return 3; },
async rasterize({ pageIndex }) {
rasterizedPages.push(pageIndex);
return { dataUrl: "data:image/png;base64,YQ==", width: 100, height: 100 };
},
});
const engine = {
id: "mixed-page-engine",
taskCapabilities: ["ocr-text"],
isAvailable: () => true,
recognize: async () => createOCRResult({
pages: [{ pageIndex: 0, width: 100, height: 100, lines: [
{ text: "OCR MIDDLE", confidence: 0.9, bbox: { x: 10, y: 20, w: 40, h: 10 } },
] }],
fullText: "OCR MIDDLE",
averageConfidence: 0.9,
}),
};
defaultOCRRegistry.register(engine);
try {
const output = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "txt",
title: "mixed-text-scan",
options: { repair: false },
});
assert.deepEqual(rasterizedPages, [1], "existing text pages must not be OCR'ed again");
assert.deepEqual(output.data.trim().split(/\n\s*\n/), ["FIRST", "OCR MIDDLE", "LAST"]);
const pdfOutput = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "pdf",
title: "mixed-text-scan",
options: { repair: false },
});
assert.deepEqual(rasterizedPages, [1], "PDF identity copy must not run OCR or redraw pages");
assert.equal(pdfOutput.data, pdf.data, "PDF to PDF should preserve the source bytes");
assert.ok((pdfOutput.warnings || []).some((warning) => warning.code === "PDF_ORIGINAL_PRESERVED"));
const readBack = readPdf({ content: await expandPdfContentForTextExtraction(pdfOutput.data) });
assert.equal(readBack.metadata.pdf.pageCount, 3);
assert.equal(readBack.blocks.some((block) => String(block.text || "").includes("OCR MIDDLE")), false);
const verifiedOutput = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "txt",
title: "mixed-text-scan",
});
assert.deepEqual(verifiedOutput.quality.qualityReport.unresolvedPdfPages, []);
const incompleteOutput = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "txt",
title: "mixed-text-scan",
options: { ocr: { enabled: false } },
});
assert.deepEqual(incompleteOutput.quality.qualityReport.unresolvedPdfPages, [2]);
assert.ok(incompleteOutput.quality.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT"));
const bareOutput = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "txt",
title: "mixed-text-scan",
options: { repair: false, ocr: { enabled: false } },
const detection = await isScannedPdf(pdf.data);
assert.equal(detection.reason, "mixed-text-and-textless-pages");
assert.deepEqual(detection.pageIndices, [1]);
const rasterizedPages = [];
setPdfPageRasterizer({
async countPages() { return 3; },
async rasterize({ pageIndex }) {
rasterizedPages.push(pageIndex);
return { dataUrl: "data:image/png;base64,YQ==", width: 100, height: 100 };
},
});
assert.deepEqual(bareOutput.unresolvedPdfPages, [2]);
assert.ok(bareOutput.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT"));
} finally {
defaultOCRRegistry.unregister(engine.id);
resetPdfPageRasterizer();
}
const engine = {
id: "mixed-page-engine",
taskCapabilities: ["ocr-text"],
isAvailable: () => true,
recognize: async () => createOCRResult({
pages: [{ pageIndex: 0, width: 100, height: 100, lines: [
{ text: "OCR MIDDLE", confidence: 0.9, bbox: { x: 10, y: 20, w: 40, h: 10 } },
] }],
fullText: "OCR MIDDLE",
averageConfidence: 0.9,
}),
};
defaultOCRRegistry.register(engine);
try {
const output = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "txt",
title: "mixed-text-scan",
options: { repair: false },
});
assert.deepEqual(rasterizedPages, [1], "existing text pages must not be OCR'ed again");
assert.deepEqual(output.data.trim().split(/\n\s*\n/), ["FIRST", "OCR MIDDLE", "LAST"]);
const pdfOutput = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "pdf",
title: "mixed-text-scan",
options: { repair: false },
});
assert.deepEqual(rasterizedPages, [1], "PDF identity copy must not run OCR or redraw pages");
assert.equal(pdfOutput.data, pdf.data, "PDF to PDF should preserve the source bytes");
assert.ok((pdfOutput.warnings || []).some((warning) => warning.code === "PDF_ORIGINAL_PRESERVED"));
const readBack = readPdf({ content: await expandPdfContentForTextExtraction(pdfOutput.data) });
assert.equal(readBack.metadata.pdf.pageCount, 3);
assert.equal(readBack.blocks.some((block) => String(block.text || "").includes("OCR MIDDLE")), false);
const verifiedOutput = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "txt",
title: "mixed-text-scan",
});
assert.deepEqual(verifiedOutput.quality.qualityReport.unresolvedPdfPages, []);
const incompleteOutput = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "txt",
title: "mixed-text-scan",
options: { ocr: { enabled: false } },
});
assert.deepEqual(incompleteOutput.quality.qualityReport.unresolvedPdfPages, [2]);
assert.ok(incompleteOutput.quality.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT"));
const bareOutput = await convertContentAsync({
content: pdf.data,
from: "pdf",
to: "txt",
title: "mixed-text-scan",
options: { repair: false, ocr: { enabled: false } },
});
assert.deepEqual(bareOutput.unresolvedPdfPages, [2]);
assert.ok(bareOutput.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT"));
} finally {
defaultOCRRegistry.unregister(engine.id);
resetPdfPageRasterizer();
}
} else {
console.log("⊘ 29c mixed-page OCR skipped: pdfjs-dist not installed");
}

// 30. convertContentAsync routes scanned PDF through OCR stage when stubs registered
Expand Down
18 changes: 12 additions & 6 deletions scripts/pdf-reader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,18 @@ const mixedPdf = writePdfHighFidelity({
},
});
const mixedExpanded = await expandPdfContentForTextExtraction(mixedPdf.data);
const mixedModel = readPdf({ content: mixedExpanded, title: "mixed-pages" });
assert.equal(mixedModel.metadata.pdf.pageCount, 2);
assert.deepEqual(mixedModel.metadata.pdf.pagesWithoutText, [2]);
assert.equal(mixedModel.fixedLayout.pages.length, 2);
assert.ok(mixedModel.metadata.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT"));
console.log(" ✅ 无文字页保留在布局中,并产生可见 warning");
// 混合页检测依赖 PDF.js 提取文本页;可选依赖缺失的环境(npm ci 可选安装失败)跳过断言。
const pdfjsAvailable = await import("pdfjs-dist").then(() => true, () => false);
if (pdfjsAvailable) {
const mixedModel = readPdf({ content: mixedExpanded, title: "mixed-pages" });
assert.equal(mixedModel.metadata.pdf.pageCount, 2);
assert.deepEqual(mixedModel.metadata.pdf.pagesWithoutText, [2]);
assert.equal(mixedModel.fixedLayout.pages.length, 2);
assert.ok(mixedModel.metadata.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT"));
console.log(" ✅ 无文字页保留在布局中,并产生可见 warning");
} else {
console.log(" ⊘ 跳过:pdfjs-dist 未安装,混合页页级检测依赖 PDF.js 提取");
}

// 测试 12: 同格式 PDF 不重新绘制,原字节在同步、异步及预提取入口一致。
console.log("\nTest 12: PDF identity copy");
Expand Down
Loading