hwpx2f 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +124 -2
- package/THIRD_PARTY_NOTICES.md +75 -0
- package/dist/cli.js +76 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +32 -0
- package/dist/lib/chunk-36PRCVPO.js +131 -0
- package/dist/lib/chunk-45BAITEX.js +9749 -0
- package/dist/lib/chunk-4NRZ6MWV.js +133 -0
- package/dist/lib/chunk-6CFXTS7A.js +11971 -0
- package/dist/lib/chunk-6CPCDYOD.js +104 -0
- package/dist/lib/chunk-B6MTS4XP.js +313 -0
- package/dist/lib/chunk-BX24E7EG.js +1427 -0
- package/dist/lib/chunk-C64I2SU4.js +82 -0
- package/dist/lib/chunk-CEKQASS3.js +258 -0
- package/dist/lib/chunk-CMWTKXM2.js +204 -0
- package/dist/lib/chunk-E6KSYRHG.js +2189 -0
- package/dist/lib/chunk-IZDWVMTV.js +28786 -0
- package/dist/lib/chunk-JK6YR5NT.js +4276 -0
- package/dist/lib/chunk-O6FSHPNE.js +111 -0
- package/dist/lib/chunk-OYXOOP2U.js +199 -0
- package/dist/lib/chunk-PM4KBWF4.js +49 -0
- package/dist/lib/chunk-R5F3GHLO.js +290 -0
- package/dist/lib/chunk-R76DWHJR.js +1596 -0
- package/dist/lib/chunk-RFS3RTSR.js +6876 -0
- package/dist/lib/chunk-RYJRNK5Z.js +47 -0
- package/dist/lib/chunk-UOXPGSFH.js +43 -0
- package/dist/lib/chunk-UZH4UBMX.js +44 -0
- package/dist/lib/chunk-VZTE4GKI.js +52 -0
- package/dist/lib/chunk-WEOKEO6L.js +35291 -0
- package/dist/lib/detect-4XZNNYZA-O7IPA4XQ.js +32 -0
- package/dist/lib/dist-HVSOXNCJ.js +263 -0
- package/dist/lib/formula-LC62GV4D-K3VFC5GV.js +1173 -0
- package/dist/lib/generator-KYTTL4OS-TRAQ66QE.js +14 -0
- package/dist/lib/hancom-EM62ZVDM-WZKVI7XB.js +91 -0
- package/dist/lib/image-ocr-ZUZQXLYS-U66LBMRA.js +293 -0
- package/dist/lib/page-images-VCIFZAXK-KJLWQDRH.js +95 -0
- package/dist/lib/page-range-7R3JU6SQ-MDDQRZVK.js +10 -0
- package/dist/lib/parser-JQNH5QMM-HFFI4HYK.js +4310 -0
- package/dist/lib/pdf-ocr-C3MBR6J2-J3UGJKWT.js +46 -0
- package/dist/lib/to-hwpx-JUT2SOTT-MOPPK6OW.js +17 -0
- package/dist/notices/core-util-is/LICENSE +19 -0
- package/dist/notices/entities/LICENSE +11 -0
- package/dist/notices/immediate/LICENSE.txt +20 -0
- package/dist/notices/inherits/LICENSE +16 -0
- package/dist/notices/jszip/LICENSE.markdown +651 -0
- package/dist/notices/kordoc/LICENSE +21 -0
- package/dist/notices/kordoc/NOTICE +376 -0
- package/dist/notices/kordoc/THIRD_PARTY/apache-2.0.LICENSE +201 -0
- package/dist/notices/kordoc/THIRD_PARTY/claw-hwp.LICENSE +21 -0
- package/dist/notices/kordoc/THIRD_PARTY/hayro-pdf.txt +361 -0
- package/dist/notices/kordoc/THIRD_PARTY/hml-equation-parser.LICENSE +201 -0
- package/dist/notices/kordoc/THIRD_PARTY/hml-equation-parser.txt +35 -0
- package/dist/notices/kordoc/THIRD_PARTY/korean-report-hwpx.LICENSE +21 -0
- package/dist/notices/kordoc/THIRD_PARTY/memchr.LICENSE +21 -0
- package/dist/notices/kordoc/THIRD_PARTY/opendataloader-pdf.txt +32 -0
- package/dist/notices/kordoc/THIRD_PARTY/pix2text.txt +17 -0
- package/dist/notices/kordoc/THIRD_PARTY/quick-xml.LICENSE +23 -0
- package/dist/notices/kordoc/THIRD_PARTY/rhwp-forms.txt +35 -0
- package/dist/notices/lie/license.md +7 -0
- package/dist/notices/linkify-it/LICENSE +22 -0
- package/dist/notices/markdown-it/LICENSE +22 -0
- package/dist/notices/mdurl/LICENSE +45 -0
- package/dist/notices/pako/LICENSE +21 -0
- package/dist/notices/process-nextick-args/license.md +19 -0
- package/dist/notices/punycode.js/LICENSE-MIT.txt +20 -0
- package/dist/notices/readable-stream/LICENSE +47 -0
- package/dist/notices/safe-buffer/LICENSE +21 -0
- package/dist/notices/setimmediate/LICENSE.txt +20 -0
- package/dist/notices/string_decoder/LICENSE +48 -0
- package/dist/notices/uc.micro/LICENSE.txt +20 -0
- package/dist/notices/util-deprecate/LICENSE +24 -0
- package/package.json +66 -4
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
markdownToHwpx
|
|
4
|
+
} from "./chunk-IZDWVMTV.js";
|
|
5
|
+
import "./chunk-36PRCVPO.js";
|
|
6
|
+
import "./chunk-BX24E7EG.js";
|
|
7
|
+
import "./chunk-45BAITEX.js";
|
|
8
|
+
import "./chunk-C64I2SU4.js";
|
|
9
|
+
import "./chunk-R76DWHJR.js";
|
|
10
|
+
import "./chunk-OYXOOP2U.js";
|
|
11
|
+
import "./chunk-RYJRNK5Z.js";
|
|
12
|
+
export {
|
|
13
|
+
markdownToHwpx
|
|
14
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
NO_HANCOM_ENV,
|
|
4
|
+
hancomAllowed,
|
|
5
|
+
powershellPath
|
|
6
|
+
} from "./chunk-36PRCVPO.js";
|
|
7
|
+
import {
|
|
8
|
+
KordocError
|
|
9
|
+
} from "./chunk-OYXOOP2U.js";
|
|
10
|
+
import "./chunk-RYJRNK5Z.js";
|
|
11
|
+
|
|
12
|
+
// node_modules/kordoc/dist/hancom-EM62ZVDM.js
|
|
13
|
+
import { execFileSync } from "child_process";
|
|
14
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
15
|
+
import { platform, tmpdir } from "os";
|
|
16
|
+
import { join } from "path";
|
|
17
|
+
function hancomAvailable() {
|
|
18
|
+
if (!hancomAllowed()) return false;
|
|
19
|
+
try {
|
|
20
|
+
const out = execFileSync(powershellPath(), [
|
|
21
|
+
"-NoProfile",
|
|
22
|
+
"-NonInteractive",
|
|
23
|
+
"-Command",
|
|
24
|
+
"if ([type]::GetTypeFromProgID('HWPFrame.HwpObject')) { 'yes' } else { 'no' }"
|
|
25
|
+
], { encoding: "utf-8", timeout: 3e4, windowsHide: true });
|
|
26
|
+
return out.trim().endsWith("yes");
|
|
27
|
+
} catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function hancomSaveInvocation(src, out) {
|
|
32
|
+
const ps1 = `
|
|
33
|
+
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
34
|
+
$ErrorActionPreference = 'Stop'
|
|
35
|
+
$src = $env:KORDOC_COM_SRC
|
|
36
|
+
$out = $env:KORDOC_COM_OUT
|
|
37
|
+
$tmpDir = Join-Path $env:TEMP ('hwp-com-' + [guid]::NewGuid().ToString('N'))
|
|
38
|
+
try {
|
|
39
|
+
[void](New-Item -ItemType Directory -Path $tmpDir -Force)
|
|
40
|
+
$tmpFile = Join-Path $tmpDir ('src' + [System.IO.Path]::GetExtension($src))
|
|
41
|
+
$tmpOut = Join-Path $tmpDir 'out.hwpx'
|
|
42
|
+
Copy-Item -LiteralPath $src -Destination $tmpFile -Force
|
|
43
|
+
$hwp = New-Object -ComObject HWPFrame.HwpObject
|
|
44
|
+
$hwp.RegisterModule('FilePathCheckDLL', 'FilePathCheckerModuleExample') | Out-Null
|
|
45
|
+
try { $hwp.XHwpWindows.Item(0).Visible = $false } catch { }
|
|
46
|
+
$hwp.SetMessageBoxMode(0x2FFF1) | Out-Null
|
|
47
|
+
$opened = $hwp.Open($tmpFile, '', 'suspendpassword:TRUE;forceopen:TRUE;versionwarning:FALSE')
|
|
48
|
+
if (-not $opened) { throw 'OPEN' }
|
|
49
|
+
$saved = $hwp.SaveAs($tmpOut, 'HWPX', '')
|
|
50
|
+
$hwp.Clear(1) | Out-Null
|
|
51
|
+
try { $hwp.Quit() | Out-Null } catch { }
|
|
52
|
+
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($hwp) | Out-Null
|
|
53
|
+
if (-not $saved -or -not (Test-Path -LiteralPath $tmpOut)) { throw 'SAVE' }
|
|
54
|
+
Copy-Item -LiteralPath $tmpOut -Destination $out -Force
|
|
55
|
+
'OK'
|
|
56
|
+
} catch {
|
|
57
|
+
'ERR ' + $_.Exception.Message
|
|
58
|
+
} finally {
|
|
59
|
+
try { Remove-Item -LiteralPath $tmpDir -Recurse -Force -ErrorAction SilentlyContinue } catch { }
|
|
60
|
+
[GC]::Collect(); [GC]::WaitForPendingFinalizers()
|
|
61
|
+
}
|
|
62
|
+
`;
|
|
63
|
+
return {
|
|
64
|
+
args: ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", ps1],
|
|
65
|
+
env: { ...process.env, KORDOC_COM_SRC: src, KORDOC_COM_OUT: out }
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function hwpToHwpxViaHancom(bytes, ext) {
|
|
69
|
+
if (platform() !== "win32") throw new KordocError('\uD55C/\uAE00 \uC800\uC7A5 \uBCC0\uD658\uC740 Windows + \uD55C/\uAE00 \uC124\uCE58 \uD658\uACBD\uC5D0\uC11C\uB9CC \uC501\uB2C8\uB2E4 \u2014 engine \uC744 "kordoc" \uC774\uB098 "auto" \uB85C \uC8FC\uC138\uC694');
|
|
70
|
+
if (!hancomAllowed()) throw new KordocError(`${NO_HANCOM_ENV}=1 \uC124\uC815\uC73C\uB85C \uD55C/\uAE00 \uC790\uB3D9\uD654\uAC00 \uAEBC\uC838 \uC788\uC2B5\uB2C8\uB2E4. engine\uC744 "kordoc"\uC774\uB098 "auto"\uB85C \uC120\uD0DD\uD558\uC138\uC694`);
|
|
71
|
+
const dir = mkdtempSync(join(tmpdir(), "kordoc-hancom-"));
|
|
72
|
+
try {
|
|
73
|
+
const src = join(dir, `in.${ext}`), out = join(dir, "out.hwpx");
|
|
74
|
+
writeFileSync(src, bytes);
|
|
75
|
+
const { args, env } = hancomSaveInvocation(src, out);
|
|
76
|
+
const res = execFileSync(powershellPath(), args, { encoding: "utf-8", env, timeout: 3e5, windowsHide: true }).trim();
|
|
77
|
+
const last = res.split(/\r?\n/).pop() ?? "";
|
|
78
|
+
if (last !== "OK") {
|
|
79
|
+
if (last === "ERR OPEN") throw new KordocError('\uD55C/\uAE00\uC774 \uBB38\uC11C\uB97C \uC5F4\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4 \u2014 \uC5F4\uAE30 \uC554\uD638\uAC00 \uAC78\uB9B0 \uBB38\uC11C\uB294 engine "kordoc" \uACFC password \uB85C \uBC14\uAFB8\uC138\uC694');
|
|
80
|
+
throw new KordocError(`\uD55C/\uAE00 \uC800\uC7A5 \uBCC0\uD658 \uC2E4\uD328: ${last.replace(/^ERR\s*/, "") || res.slice(0, 200)}`);
|
|
81
|
+
}
|
|
82
|
+
return new Uint8Array(readFileSync(out));
|
|
83
|
+
} finally {
|
|
84
|
+
rmSync(dir, { recursive: true, force: true });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export {
|
|
88
|
+
hancomAvailable,
|
|
89
|
+
hancomSaveInvocation,
|
|
90
|
+
hwpToHwpxViaHancom
|
|
91
|
+
};
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
dcxPages,
|
|
4
|
+
heicToRgba,
|
|
5
|
+
pcxToRgba
|
|
6
|
+
} from "./chunk-6CPCDYOD.js";
|
|
7
|
+
import {
|
|
8
|
+
imageFormat
|
|
9
|
+
} from "./chunk-4NRZ6MWV.js";
|
|
10
|
+
import "./chunk-CMWTKXM2.js";
|
|
11
|
+
import "./chunk-45BAITEX.js";
|
|
12
|
+
import {
|
|
13
|
+
MAX_OCR_PIXELS,
|
|
14
|
+
builtinOcrConfig,
|
|
15
|
+
cellEdgeHorizontals,
|
|
16
|
+
cellEdgeVerticals,
|
|
17
|
+
deskewPage,
|
|
18
|
+
detectRulingLines,
|
|
19
|
+
getOcrEngine,
|
|
20
|
+
ocrItemsToBlocks,
|
|
21
|
+
pageLines,
|
|
22
|
+
recognizeOcrPage,
|
|
23
|
+
reportModelDownload,
|
|
24
|
+
rescueEmptyCells,
|
|
25
|
+
rotateQuarter,
|
|
26
|
+
rulingToPdfLines,
|
|
27
|
+
straightenPhoto,
|
|
28
|
+
verticalsStraightened
|
|
29
|
+
} from "./chunk-JK6YR5NT.js";
|
|
30
|
+
import "./chunk-C64I2SU4.js";
|
|
31
|
+
import "./chunk-UOXPGSFH.js";
|
|
32
|
+
import {
|
|
33
|
+
flushHeifCache
|
|
34
|
+
} from "./chunk-UZH4UBMX.js";
|
|
35
|
+
import {
|
|
36
|
+
MAX_PIXELS,
|
|
37
|
+
bmpToRgba,
|
|
38
|
+
encodePng
|
|
39
|
+
} from "./chunk-CEKQASS3.js";
|
|
40
|
+
import {
|
|
41
|
+
ensureOcrModelsFor
|
|
42
|
+
} from "./chunk-6CFXTS7A.js";
|
|
43
|
+
import "./chunk-O6FSHPNE.js";
|
|
44
|
+
import "./chunk-R76DWHJR.js";
|
|
45
|
+
import "./chunk-B6MTS4XP.js";
|
|
46
|
+
import "./chunk-PM4KBWF4.js";
|
|
47
|
+
import {
|
|
48
|
+
KordocError,
|
|
49
|
+
OPTIONAL_DEP_INSTALL_HINT
|
|
50
|
+
} from "./chunk-OYXOOP2U.js";
|
|
51
|
+
import "./chunk-RYJRNK5Z.js";
|
|
52
|
+
|
|
53
|
+
// node_modules/kordoc/dist/image-ocr-ZUZQXLYS.js
|
|
54
|
+
async function applyImageFormulas(items, rgba, width, height, warnings) {
|
|
55
|
+
let regions;
|
|
56
|
+
try {
|
|
57
|
+
const { FormulaPipeline, ensureFormulaModels } = await import("./formula-LC62GV4D-K3VFC5GV.js");
|
|
58
|
+
await ensureFormulaModels();
|
|
59
|
+
const pipeline = await FormulaPipeline.create();
|
|
60
|
+
try {
|
|
61
|
+
regions = await pipeline.recognizeFrame({ width, height, data: rgba });
|
|
62
|
+
} finally {
|
|
63
|
+
await pipeline.destroy();
|
|
64
|
+
}
|
|
65
|
+
} catch (e) {
|
|
66
|
+
warnings.push({ page: 1, message: `\uC218\uC2DD OCR \uC2E4\uD328: ${e instanceof Error ? e.message : String(e)}`, code: "PARTIAL_PARSE" });
|
|
67
|
+
return items;
|
|
68
|
+
}
|
|
69
|
+
return replaceFormulaItems(items, regions);
|
|
70
|
+
}
|
|
71
|
+
function replaceFormulaItems(items, regions) {
|
|
72
|
+
const covered = (r) => items.filter((it) => Math.min(it.x + it.w, r.bbox.x2) - Math.max(it.x, r.bbox.x1) > 0 && Math.min(it.y + it.h, r.bbox.y2) - Math.max(it.y, r.bbox.y1) >= it.h * 0.5).map((it) => {
|
|
73
|
+
const cs = [...it.text], n = cs.length, at = (x) => Math.max(0, Math.min(n, Math.round((x - it.x) / it.w * n)));
|
|
74
|
+
return cs.slice(at(r.bbox.x1), at(r.bbox.x2)).join("");
|
|
75
|
+
}).join("");
|
|
76
|
+
const hangulShare = (t) => {
|
|
77
|
+
const l = t.match(/[\p{L}\p{N}]/gu) ?? [];
|
|
78
|
+
return l.length ? l.filter((c) => /[가-힣]/.test(c)).length / l.length : 0;
|
|
79
|
+
};
|
|
80
|
+
const formulas = regions.filter((r) => {
|
|
81
|
+
const latex = r.latex?.trim() ?? "";
|
|
82
|
+
return latex && /\\frac|\^|_\{|_[a-z0-9]|\\sqrt|\\int|\\sum|\\lim|\\log|=|\\leq|\\geq|<|>/.test(latex) && !/\\fbox|\\Xi|\\exists|\\models|\\mathcal|\\mathrm\{[a-z~]{3,}/.test(latex) && hangulShare(covered(r)) < 0.3;
|
|
83
|
+
});
|
|
84
|
+
if (!formulas.length) return items;
|
|
85
|
+
const out = [];
|
|
86
|
+
for (const it of items) {
|
|
87
|
+
let pieces = [it];
|
|
88
|
+
for (const f of formulas) {
|
|
89
|
+
const next = [];
|
|
90
|
+
for (const p of pieces) {
|
|
91
|
+
const ox = Math.min(p.x + p.w, f.bbox.x2) - Math.max(p.x, f.bbox.x1);
|
|
92
|
+
const oy = Math.min(p.y + p.h, f.bbox.y2) - Math.max(p.y, f.bbox.y1);
|
|
93
|
+
if (ox <= 0 || oy < p.h * 0.5) {
|
|
94
|
+
next.push(p);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (ox >= p.w * 0.5) continue;
|
|
98
|
+
const chars = [...p.text], n = chars.length;
|
|
99
|
+
const cut = (x) => Math.max(0, Math.min(n, Math.round((x - p.x) / p.w * n)));
|
|
100
|
+
const a = cut(f.bbox.x1), b = cut(f.bbox.x2);
|
|
101
|
+
const left = chars.slice(0, a).join("").trimEnd(), right = chars.slice(b).join("").trimStart();
|
|
102
|
+
const base = p.candidates || p.chars ? { ...p, candidates: void 0, chars: void 0 } : p;
|
|
103
|
+
if (left) next.push({ ...base, text: left, w: Math.max(1, f.bbox.x1 - p.x) });
|
|
104
|
+
if (right) next.push({ ...base, text: right, x: f.bbox.x2, w: Math.max(1, p.x + p.w - f.bbox.x2) });
|
|
105
|
+
}
|
|
106
|
+
pieces = next;
|
|
107
|
+
}
|
|
108
|
+
out.push(...pieces);
|
|
109
|
+
}
|
|
110
|
+
for (const f of formulas) {
|
|
111
|
+
const text = f.kind === "display" ? `$$${f.latex.trim()}$$` : `$${f.latex.trim()}$`;
|
|
112
|
+
out.push({ text, x: f.bbox.x1, y: f.bbox.y1, w: f.bbox.x2 - f.bbox.x1, h: f.bbox.y2 - f.bbox.y1, confidence: 1 });
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
var IMAGE_SCALE = 3;
|
|
117
|
+
function imageScale(width, height, densityDpi, skewed = false) {
|
|
118
|
+
if (densityDpi && densityDpi >= 100 && densityDpi <= 1200) return densityDpi / 72;
|
|
119
|
+
const long = Math.max(width, height), short = Math.min(width, height);
|
|
120
|
+
if (short > 0) {
|
|
121
|
+
const r = long / short;
|
|
122
|
+
if (Math.abs(r / Math.SQRT2 - 1) <= 0.02) return long / 842;
|
|
123
|
+
if (Math.abs(r / (11 / 8.5) - 1) <= 0.02) return long / 792;
|
|
124
|
+
if (!skewed && Math.abs(r / (257 / 188) - 1) <= 0.02) return long / 728.5;
|
|
125
|
+
}
|
|
126
|
+
return IMAGE_SCALE;
|
|
127
|
+
}
|
|
128
|
+
async function parseImageDocument(buffer, options, page = 1) {
|
|
129
|
+
const warnings = [];
|
|
130
|
+
if (typeof options?.ocr === "function") {
|
|
131
|
+
const kind = imageFormat(buffer);
|
|
132
|
+
let bytes = new Uint8Array(buffer), mime = detectImageMime(buffer);
|
|
133
|
+
if (kind !== "png" && kind !== "jpeg" && kind !== "webp") {
|
|
134
|
+
const px = await decodeToRgba(buffer, page - 1);
|
|
135
|
+
bytes = encodePng(px.width, px.height, px.data);
|
|
136
|
+
mime = "image/png";
|
|
137
|
+
}
|
|
138
|
+
const text = await options.ocr(bytes, page, mime);
|
|
139
|
+
if (!text.trim()) {
|
|
140
|
+
warnings.push({ page, message: "OCR \uACB0\uACFC \uC5C6\uC74C", code: "OCR_FAILED" });
|
|
141
|
+
return { blocks: [], warnings };
|
|
142
|
+
}
|
|
143
|
+
return { blocks: [{ type: "paragraph", text: text.trim(), pageNumber: page }], warnings };
|
|
144
|
+
}
|
|
145
|
+
const { data: decoded, width: decodedW, height: decodedH, density, shrink } = await decodeToRgba(buffer, page - 1);
|
|
146
|
+
let width = decodedW, height = decodedH;
|
|
147
|
+
if (shrink < 1) warnings.push({ page, code: "PARTIAL_PARSE", message: "OCR \uB798\uC2A4\uD130 \uD53D\uC140 \uC0C1\uD55C\uC73C\uB85C \uC774\uBBF8\uC9C0 \uD574\uC0C1\uB3C4\uB97C \uCD95\uC18C\uD588\uC2B5\uB2C8\uB2E4 (\uC791\uC740 \uAE00\uC790 \uC778\uC2DD \uACB0\uC190 \uAC00\uB2A5)" });
|
|
148
|
+
const photo = straightenPhoto(decoded, width, height);
|
|
149
|
+
if (photo) ({ width, height } = photo);
|
|
150
|
+
const deskewed = photo ? { rgba: photo.rgba, angle: 0, dewarp: void 0 } : deskewPage(decoded, width, height);
|
|
151
|
+
const scale = imageScale(width / shrink, height / shrink, density, !!photo || deskewed.angle !== 0 || !!deskewed.dewarp) * shrink;
|
|
152
|
+
let data = deskewed.rgba;
|
|
153
|
+
const angle = deskewed.angle;
|
|
154
|
+
const config = builtinOcrConfig(options);
|
|
155
|
+
await ensureOcrModelsFor(config.models, reportModelDownload);
|
|
156
|
+
if (config.handwriting && !config.autoRec?.lazy) await ensureOcrModelsFor(config.handwriting, reportModelDownload);
|
|
157
|
+
const engine = await getOcrEngine(config.models);
|
|
158
|
+
const stats = { droppedLowConf: 0, ...config.lines === "all" ? { dropped: [] } : {} };
|
|
159
|
+
let ruling = detectRulingLines(data, width, height, scale, { slopeTolerant: deskewed.angle === 0 && verticalsStraightened(deskewed.dewarp, height) });
|
|
160
|
+
let items = await recognizeOcrPage(
|
|
161
|
+
engine,
|
|
162
|
+
config,
|
|
163
|
+
{ ...config.tuning, flipProbe: true },
|
|
164
|
+
data,
|
|
165
|
+
width,
|
|
166
|
+
height,
|
|
167
|
+
stats,
|
|
168
|
+
[...ruling.cellDividers ?? [], ...cellEdgeVerticals(ruling)],
|
|
169
|
+
cellEdgeHorizontals(ruling)
|
|
170
|
+
);
|
|
171
|
+
let dewarp = deskewed.dewarp;
|
|
172
|
+
if (stats.pageRotation) {
|
|
173
|
+
({ rgba: data, width, height } = rotateQuarter(data, width, height, stats.pageRotation));
|
|
174
|
+
delete stats.pageRotation;
|
|
175
|
+
dewarp = void 0;
|
|
176
|
+
ruling = detectRulingLines(data, width, height, scale);
|
|
177
|
+
items = await recognizeOcrPage(
|
|
178
|
+
engine,
|
|
179
|
+
config,
|
|
180
|
+
{ ...config.tuning, orientPage: false, flipProbe: false },
|
|
181
|
+
data,
|
|
182
|
+
width,
|
|
183
|
+
height,
|
|
184
|
+
stats,
|
|
185
|
+
[...ruling.cellDividers ?? [], ...cellEdgeVerticals(ruling)],
|
|
186
|
+
cellEdgeHorizontals(ruling)
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
if (config.tuning.postprocess) items = [...items, ...await rescueEmptyCells(engine, config.tuning, data, width, height, ruling, items)];
|
|
190
|
+
if (options?.formulaOcr) items = await applyImageFormulas(items, data, width, height, warnings);
|
|
191
|
+
const lines = config.lines ? pageLines(page, width, height, scale, angle, items, stats.dropped, false, dewarp) : void 0;
|
|
192
|
+
const raster = options?.pageImages ? { raster: { data, width, height } } : void 0;
|
|
193
|
+
if (stats.droppedLowConf > 0) {
|
|
194
|
+
warnings.push({
|
|
195
|
+
page,
|
|
196
|
+
message: `\uC800\uC2E0\uB8B0 OCR \uB77C\uC778 ${stats.droppedLowConf}\uAC1C \uD3D0\uAE30 (\uC778\uC2DD \uACB0\uC190 \uAC00\uB2A5)`,
|
|
197
|
+
code: "OCR_LOW_CONF"
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (stats.truncatedBoxes) {
|
|
201
|
+
warnings.push({ page, message: `OCR \uAC80\uCD9C \uC0C1\uC790\uAC00 \uB108\uBB34 \uB9CE\uC544 ${stats.truncatedBoxes}\uAC1C\uB294 \uC778\uC2DD\uD558\uC9C0 \uC54A\uC74C (\uC77C\uBD80 \uAE00 \uACB0\uC190)`, code: "PARTIAL_PARSE" });
|
|
202
|
+
}
|
|
203
|
+
if (items.length === 0) {
|
|
204
|
+
warnings.push({ page, message: "\uC774\uBBF8\uC9C0\uC5D0\uC11C \uD14D\uC2A4\uD2B8\uB97C \uC778\uC2DD\uD558\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4", code: "OCR_FAILED" });
|
|
205
|
+
return { blocks: [], warnings, lines, ...raster };
|
|
206
|
+
}
|
|
207
|
+
const pdfW = width / scale;
|
|
208
|
+
const pdfH = height / scale;
|
|
209
|
+
const extraLines = rulingToPdfLines(ruling, scale, pdfH);
|
|
210
|
+
return { blocks: ocrItemsToBlocks(items, page, pdfW, pdfH, scale, extraLines, options?.tables !== false), warnings, lines, ...raster };
|
|
211
|
+
}
|
|
212
|
+
async function loadSharp() {
|
|
213
|
+
try {
|
|
214
|
+
const mod = await import("sharp");
|
|
215
|
+
return typeof mod === "function" ? mod : mod.default ?? mod;
|
|
216
|
+
} catch (e) {
|
|
217
|
+
throw new KordocError(
|
|
218
|
+
`\uC774\uBBF8\uC9C0 \uD30C\uC2F1\uC5D0\uB294 optional dependency 'sharp' \uAC00 \uD544\uC694\uD569\uB2C8\uB2E4. \`npm install sharp\` \uD6C4 \uB2E4\uC2DC \uC2E4\uD589\uD558\uC138\uC694.${OPTIONAL_DEP_INSTALL_HINT} \uC6D0\uC778: ${e.message}`
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async function imagePageCount(buffer) {
|
|
223
|
+
const kind = imageFormat(buffer);
|
|
224
|
+
if (kind === "dcx") return Math.max(1, dcxPages(new Uint8Array(buffer)).length);
|
|
225
|
+
if (kind !== "tiff") return 1;
|
|
226
|
+
const meta = await (await loadSharp())(Buffer.from(buffer), { limitInputPixels: false }).metadata().catch(() => ({}));
|
|
227
|
+
return Math.max(1, meta.pages ?? 1);
|
|
228
|
+
}
|
|
229
|
+
async function imagePagePixels(buffer, page) {
|
|
230
|
+
if (imageFormat(buffer) === "dcx") {
|
|
231
|
+
const p = dcxPages(new Uint8Array(buffer))[page - 1];
|
|
232
|
+
if (!p || p.length < 12) return 0;
|
|
233
|
+
const v = new DataView(p.buffer, p.byteOffset, p.byteLength);
|
|
234
|
+
return Math.max(0, v.getUint16(8, true) - v.getUint16(4, true) + 1) * Math.max(0, v.getUint16(10, true) - v.getUint16(6, true) + 1);
|
|
235
|
+
}
|
|
236
|
+
const m = await (await loadSharp())(Buffer.from(buffer), { page: page - 1, limitInputPixels: false }).metadata().catch(() => ({}));
|
|
237
|
+
return (m.width ?? 0) * (m.height ?? 0);
|
|
238
|
+
}
|
|
239
|
+
var EXT = { png: "png", jpeg: "jpg", webp: "webp", tiff: "tif", gif: "gif", avif: "avif", heic: "heic", bmp: "bmp", pcx: "pcx", dcx: "dcx" };
|
|
240
|
+
async function imagePage(buffer, page, pageCount) {
|
|
241
|
+
const name = `image_${String(page).padStart(3, "0")}`;
|
|
242
|
+
if (pageCount > 1 && imageFormat(buffer) === "dcx") {
|
|
243
|
+
const px = pcxToRgba(dcxPages(new Uint8Array(buffer))[page - 1]);
|
|
244
|
+
return px && { filename: `${name}.png`, data: encodePng(px.width, px.height, px.data), mimeType: "image/png" };
|
|
245
|
+
}
|
|
246
|
+
if (pageCount > 1) {
|
|
247
|
+
const png = await (await loadSharp())(Buffer.from(buffer), { page: page - 1, failOn: "error", limitInputPixels: MAX_PIXELS }).rotate().png().toBuffer().catch(() => null);
|
|
248
|
+
return png && { filename: `${name}.png`, data: new Uint8Array(png.buffer, png.byteOffset, png.byteLength), mimeType: "image/png" };
|
|
249
|
+
}
|
|
250
|
+
const kind = imageFormat(buffer) ?? "png";
|
|
251
|
+
return { filename: `${name}.${EXT[kind]}`, data: new Uint8Array(buffer), mimeType: `image/${kind}` };
|
|
252
|
+
}
|
|
253
|
+
async function decodeToRgba(buffer, page = 0) {
|
|
254
|
+
const sharp = await loadSharp();
|
|
255
|
+
const input = Buffer.from(buffer);
|
|
256
|
+
const kind = imageFormat(buffer);
|
|
257
|
+
const pcx = kind === "pcx" ? pcxToRgba(input) : kind === "dcx" ? pcxToRgba(dcxPages(input)[page] ?? new Uint8Array(0)) : null;
|
|
258
|
+
const px = kind === "bmp" ? bmpToRgba(input) : kind === "heic" ? await heicToRgba(input) : pcx;
|
|
259
|
+
if (kind === "bmp" && !px) throw new KordocError(`BMP \uB514\uCF54\uB529 \uC2E4\uD328: \uC190\uC0C1\uB41C \uD30C\uC77C, \uC9C0\uC6D0\uD558\uC9C0 \uC54A\uB294 \uC555\uCD95 \uBC29\uC2DD(BI_JPEG\xB7BI_PNG \uB4F1) \uB610\uB294 ${MAX_PIXELS / 1e4}\uB9CC \uD53D\uC140 \uCD08\uACFC`);
|
|
260
|
+
if ((kind === "pcx" || kind === "dcx") && !px) throw new KordocError(`${kind.toUpperCase()} \uB514\uCF54\uB529 \uC2E4\uD328: \uC190\uC0C1\uB41C \uD30C\uC77C, \uC9C0\uC6D0\uD558\uC9C0 \uC54A\uB294 \uBCC0\uD615 \uB610\uB294 ${MAX_PIXELS / 1e4}\uB9CC \uD53D\uC140 \uCD08\uACFC`);
|
|
261
|
+
const open = () => px ? sharp(Buffer.from(px.data.buffer, px.data.byteOffset, px.data.byteLength), { raw: { width: px.width, height: px.height, channels: 4 } }) : kind === "tiff" ? sharp(input, { page }) : sharp(input);
|
|
262
|
+
const bmpDpi = kind === "bmp" && input.readUInt32LE(14) >= 40 ? input.readInt32LE(38) * 0.0254 : 0;
|
|
263
|
+
const dpi = pcx ? pcx.dpi : bmpDpi;
|
|
264
|
+
const meta = px ? { width: px.width, height: px.height, ...dpi > 0 ? { density: dpi } : {} } : await open().metadata().catch(() => ({}));
|
|
265
|
+
const [w0, h0] = (meta.orientation ?? 1) >= 5 ? [meta.height ?? 0, meta.width ?? 0] : [meta.width ?? 0, meta.height ?? 0];
|
|
266
|
+
const shrink = w0 * h0 > MAX_OCR_PIXELS ? Math.sqrt(MAX_OCR_PIXELS / (w0 * h0)) : 1;
|
|
267
|
+
if (px && shrink === 1) return { ...px, density: meta.density, shrink };
|
|
268
|
+
let chain = open().rotate();
|
|
269
|
+
if (shrink < 1) chain = chain.resize(Math.max(1, Math.floor(w0 * shrink)), Math.max(1, Math.floor(h0 * shrink)), { fit: "fill" });
|
|
270
|
+
const { data, info } = await chain.ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
271
|
+
if (kind === "avif") flushHeifCache(sharp);
|
|
272
|
+
return {
|
|
273
|
+
data: new Uint8Array(data.buffer, data.byteOffset, data.byteLength),
|
|
274
|
+
width: info.width,
|
|
275
|
+
height: info.height,
|
|
276
|
+
density: meta.density,
|
|
277
|
+
shrink: w0 ? info.width / w0 : 1
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function detectImageMime(buffer) {
|
|
281
|
+
const b = new Uint8Array(buffer, 0, Math.min(12, buffer.byteLength));
|
|
282
|
+
if (b[0] === 255 && b[1] === 216) return "image/jpeg";
|
|
283
|
+
if (b[0] === 82 && b[1] === 73) return "image/webp";
|
|
284
|
+
return "image/png";
|
|
285
|
+
}
|
|
286
|
+
export {
|
|
287
|
+
decodeToRgba,
|
|
288
|
+
imagePage,
|
|
289
|
+
imagePageCount,
|
|
290
|
+
imagePagePixels,
|
|
291
|
+
imageScale,
|
|
292
|
+
parseImageDocument
|
|
293
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
loadWebpEncoder
|
|
4
|
+
} from "./chunk-UZH4UBMX.js";
|
|
5
|
+
import {
|
|
6
|
+
encodePng
|
|
7
|
+
} from "./chunk-CEKQASS3.js";
|
|
8
|
+
import {
|
|
9
|
+
validateOcrOptions
|
|
10
|
+
} from "./chunk-PM4KBWF4.js";
|
|
11
|
+
import {
|
|
12
|
+
KordocError
|
|
13
|
+
} from "./chunk-OYXOOP2U.js";
|
|
14
|
+
import "./chunk-RYJRNK5Z.js";
|
|
15
|
+
|
|
16
|
+
// node_modules/kordoc/dist/page-images-VCIFZAXK.js
|
|
17
|
+
var MAX_BYTES = 128 * 1024 * 1024;
|
|
18
|
+
var MAX_PIXELS = 1e9;
|
|
19
|
+
var MIN_PAGE_PIXELS = 1e6;
|
|
20
|
+
var WEBP_MAX_DIM = 16383;
|
|
21
|
+
var PageImageSink = class {
|
|
22
|
+
/** format 은 `pageImages` 옵션 값(켜진 것) — 모르는 값은 throw (parse() 는 먼저 걸러 실패 결과로 낸다) */
|
|
23
|
+
constructor(format, maxBytes = MAX_BYTES, maxPixels = MAX_PIXELS) {
|
|
24
|
+
this.format = format;
|
|
25
|
+
this.maxBytes = maxBytes;
|
|
26
|
+
this.maxPixels = maxPixels;
|
|
27
|
+
const bad = validateOcrOptions({ pageImages: format });
|
|
28
|
+
if (bad) throw new KordocError(bad);
|
|
29
|
+
}
|
|
30
|
+
format;
|
|
31
|
+
maxBytes;
|
|
32
|
+
maxPixels;
|
|
33
|
+
images = [];
|
|
34
|
+
/** 다룬 페이지 — 담은 것과 상한으로 뺀 것 (뺀 페이지를 다시 렌더링하려다 두 번 세지 않게) */
|
|
35
|
+
seen = /* @__PURE__ */ new Set();
|
|
36
|
+
bytes = 0;
|
|
37
|
+
pixels = 0;
|
|
38
|
+
skipped = 0;
|
|
39
|
+
/** WebP 를 청했는데 PNG 로 낸 페이지 수 */
|
|
40
|
+
asPng = 0;
|
|
41
|
+
webp;
|
|
42
|
+
/** 이미 다룬 페이지인가 (담았거나 상한으로 뺐다) */
|
|
43
|
+
has(pageNumber) {
|
|
44
|
+
return this.seen.has(pageNumber);
|
|
45
|
+
}
|
|
46
|
+
/** 상한에 닿았다 — 이 뒤로는 디코딩·렌더링하지 않고 skip 으로 세기만 한다 */
|
|
47
|
+
get full() {
|
|
48
|
+
return this.bytes > this.maxBytes || this.pixels > this.maxPixels;
|
|
49
|
+
}
|
|
50
|
+
skip(pageNumber) {
|
|
51
|
+
this.seen.add(pageNumber);
|
|
52
|
+
this.skipped++;
|
|
53
|
+
}
|
|
54
|
+
/** 페이지 하나의 래스터 픽셀을 예산에서 잡는다 — 렌더링·인코딩 전에 부른다. 상한을 넘었으면 false (뺀 페이지로 센다) */
|
|
55
|
+
reserve(pixels) {
|
|
56
|
+
if (this.bytes <= this.maxBytes && (this.pixels += Math.max(MIN_PAGE_PIXELS, pixels)) <= this.maxPixels) return true;
|
|
57
|
+
this.skipped++;
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
/** 래스터 → 페이지 이미지 (담지는 않는다) */
|
|
61
|
+
async encode(pageNumber, { data, width, height }) {
|
|
62
|
+
let webp;
|
|
63
|
+
if (this.format === "webp") {
|
|
64
|
+
if (this.webp === void 0) this.webp = await loadWebpEncoder();
|
|
65
|
+
if (this.webp && width <= WEBP_MAX_DIM && height <= WEBP_MAX_DIM) webp = await this.webp(data, width, height).catch(() => void 0);
|
|
66
|
+
}
|
|
67
|
+
const ext = webp ? "webp" : "png";
|
|
68
|
+
return { pageNumber, filename: `page_${String(pageNumber).padStart(3, "0")}.${ext}`, width, height, mimeType: `image/${ext}`, data: webp ?? encodePng(width, height, data) };
|
|
69
|
+
}
|
|
70
|
+
/** 인코딩한 페이지 이미지를 담는다 — 바이트 상한을 넘긴 페이지부터는 담지 않는다 (이미지 입력의 페이지 이미지와 같은 규칙) */
|
|
71
|
+
push(image) {
|
|
72
|
+
this.seen.add(image.pageNumber);
|
|
73
|
+
if ((this.bytes += image.data.length) > this.maxBytes) {
|
|
74
|
+
this.skipped++;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (this.format === "webp" && image.mimeType !== "image/webp") this.asPng++;
|
|
78
|
+
this.images.push(image);
|
|
79
|
+
}
|
|
80
|
+
/** 이미 있는 래스터(OCR 이 읽은 것·디코딩한 이미지)를 예산을 잡고 인코딩해 담는다 */
|
|
81
|
+
async add(pageNumber, raster) {
|
|
82
|
+
this.seen.add(pageNumber);
|
|
83
|
+
if (this.reserve(raster.width * raster.height)) this.push(await this.encode(pageNumber, raster));
|
|
84
|
+
}
|
|
85
|
+
/** 쪽 번호 순 목록. 뺀 페이지·PNG 로 바꿔 낸 페이지는 경고로 알린다 */
|
|
86
|
+
finish(warnings) {
|
|
87
|
+
if (this.skipped) warnings.push({ message: `pageImages ${this.skipped}\uD398\uC774\uC9C0 \uC0DD\uB7B5 \u2014 \uC0C1\uD55C(\uB204\uC801 ${Math.round(this.maxBytes / 1048576)}MB\xB7\uB798\uC2A4\uD130 \uD53D\uC140 \uD569\uACC4 ${this.maxPixels / 1e8}\uC5B5) \uB3C4\uB2EC. pages \uB85C \uB098\uB220\uC11C \uC77D\uC73C\uC138\uC694`, code: "SKIPPED_IMAGE" });
|
|
88
|
+
if (this.asPng && !this.webp) warnings.push({ message: "pageImages \uB97C WebP \uB85C \uB0B4\uB824\uBA74 optional dependency 'sharp' \uAC00 \uD544\uC694\uD569\uB2C8\uB2E4 \u2014 PNG \uB85C \uB0C8\uC2B5\uB2C8\uB2E4. `npm install sharp` \uD6C4 \uB2E4\uC2DC \uC2E4\uD589\uD558\uC138\uC694", code: "PARTIAL_PARSE" });
|
|
89
|
+
else if (this.asPng) warnings.push({ message: `pageImages ${this.asPng}\uD398\uC774\uC9C0\uB294 WebP \uB85C \uB0B4\uC9C0 \uBABB\uD574 PNG \uB85C \uB0C8\uC2B5\uB2C8\uB2E4 (\uAC00\uB85C\xB7\uC138\uB85C 16,383px \uD55C\uB3C4 \uCD08\uACFC \uB610\uB294 \uC778\uCF54\uB529 \uC2E4\uD328)`, code: "SKIPPED_IMAGE" });
|
|
90
|
+
return this.images.sort((a, b) => a.pageNumber - b.pageNumber);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
export {
|
|
94
|
+
PageImageSink
|
|
95
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
hasRequestedPagesAfter,
|
|
4
|
+
parsePageRange
|
|
5
|
+
} from "./chunk-VZTE4GKI.js";
|
|
6
|
+
import "./chunk-RYJRNK5Z.js";
|
|
7
|
+
export {
|
|
8
|
+
hasRequestedPagesAfter,
|
|
9
|
+
parsePageRange
|
|
10
|
+
};
|