pdfxtract 2.0.1 → 2.0.2
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/dist/cli.js +32 -20
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -60,11 +60,11 @@ const BASELINE_TOLERANCE = .3;
|
|
|
60
60
|
*/
|
|
61
61
|
const ADJACENCY_TOLERANCE = .5;
|
|
62
62
|
/**
|
|
63
|
-
* How many of the most recently started lines a run may join. A
|
|
64
|
-
*
|
|
65
|
-
*
|
|
63
|
+
* How many of the most recently started lines a run may join. A page rarely
|
|
64
|
+
* has more lines than this, so a run can reach any line on its page, while a
|
|
65
|
+
* page made of tens of thousands of scattered runs still stays fast.
|
|
66
66
|
*/
|
|
67
|
-
const LOOK_BACK_LINES =
|
|
67
|
+
const LOOK_BACK_LINES = 256;
|
|
68
68
|
/**
|
|
69
69
|
* A run moves ahead of an earlier one only if it ends left of where that one
|
|
70
70
|
* starts, give or take this many font sizes. Runs that overlap, such as a
|
|
@@ -79,6 +79,9 @@ const SPACE_GAP = .1;
|
|
|
79
79
|
function fontSize(run) {
|
|
80
80
|
return Math.hypot(run.transform[2], run.transform[3]);
|
|
81
81
|
}
|
|
82
|
+
function weigh(str) {
|
|
83
|
+
return str.replace(/\s/g, "").length;
|
|
84
|
+
}
|
|
82
85
|
function onSameBaseline(p, q) {
|
|
83
86
|
const scale = Math.max(fontSize(p), fontSize(q));
|
|
84
87
|
return Math.abs(p.transform[5] - q.transform[5]) <= BASELINE_TOLERANCE * scale;
|
|
@@ -108,7 +111,7 @@ function measureSpaces(runs) {
|
|
|
108
111
|
}
|
|
109
112
|
return measured;
|
|
110
113
|
}
|
|
111
|
-
function startLine(lines, run, closed) {
|
|
114
|
+
function startLine(lines, run, index, closed) {
|
|
112
115
|
const x = run.transform[4];
|
|
113
116
|
const line = {
|
|
114
117
|
baseline: run.transform[5],
|
|
@@ -120,6 +123,8 @@ function startLine(lines, run, closed) {
|
|
|
120
123
|
width: run.width,
|
|
121
124
|
str: run.str
|
|
122
125
|
}],
|
|
126
|
+
anchor: index,
|
|
127
|
+
weight: weigh(run.str),
|
|
123
128
|
movable: run.dir === "ltr",
|
|
124
129
|
reordered: false,
|
|
125
130
|
closed,
|
|
@@ -153,13 +158,19 @@ function insertRun(line, run) {
|
|
|
153
158
|
if (at < line.runs.length) line.reordered = true;
|
|
154
159
|
line.runs.splice(at, 0, run);
|
|
155
160
|
}
|
|
156
|
-
function placeRun(lines, run) {
|
|
161
|
+
function placeRun(lines, run, index, previous) {
|
|
157
162
|
const [a, b, , , x] = run.transform;
|
|
158
163
|
const horizontal = a > 0 && Math.abs(b) <= Math.abs(a) * .001;
|
|
159
|
-
if (!horizontal || run.dir === "ttb") return startLine(lines, run, true);
|
|
164
|
+
if (!horizontal || run.dir === "ttb") return startLine(lines, run, index, true);
|
|
160
165
|
const line = findLine(lines, run);
|
|
161
|
-
if (!line) return startLine(lines, run, false);
|
|
166
|
+
if (!line) return startLine(lines, run, index, false);
|
|
167
|
+
if (line !== previous) line.reordered = true;
|
|
162
168
|
if (run.dir !== "ltr") line.movable = false;
|
|
169
|
+
const weight = weigh(run.str);
|
|
170
|
+
if (weight > line.weight) {
|
|
171
|
+
line.anchor = index;
|
|
172
|
+
line.weight = weight;
|
|
173
|
+
}
|
|
163
174
|
line.size = Math.max(line.size, fontSize(run));
|
|
164
175
|
line.left = Math.min(line.left, x);
|
|
165
176
|
line.right = Math.max(line.right, x + run.width);
|
|
@@ -186,25 +197,26 @@ function lineText(line) {
|
|
|
186
197
|
* Join a page's text runs in reading order.
|
|
187
198
|
*
|
|
188
199
|
* PDF.js reports runs in the order the content stream draws them, which is
|
|
189
|
-
* not always the order they are read
|
|
190
|
-
* in a second pass, so "try nano now" comes out as "try now nano"
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
200
|
+
* not always the order they are read. Chrome draws a line's emphasized words
|
|
201
|
+
* in a second pass, so "try nano now" comes out as "try now nano", and it
|
|
202
|
+
* draws a list's markers before the items, so "Step 1." lands far from its
|
|
203
|
+
* text. Runs that sit on one baseline and touch horizontally are gathered
|
|
204
|
+
* into a line, and a run drawn later moves ahead of the runs it sits wholly to
|
|
205
|
+
* the left of. Each line goes where its longest run was drawn, so columns,
|
|
206
|
+
* tables and anything already in order come out as before.
|
|
195
207
|
*/
|
|
196
208
|
function orderPageText(runs) {
|
|
197
209
|
const lines = [];
|
|
198
210
|
let last;
|
|
199
|
-
|
|
211
|
+
measureSpaces(runs).forEach((run, index) => {
|
|
200
212
|
if (run.str === "") {
|
|
201
|
-
if (run.hasEOL) (last ?? startLine(lines, run, true)).eol = true;
|
|
202
|
-
|
|
213
|
+
if (run.hasEOL) (last ?? startLine(lines, run, index, true)).eol = true;
|
|
214
|
+
return;
|
|
203
215
|
}
|
|
204
|
-
last = placeRun(lines, run);
|
|
216
|
+
last = placeRun(lines, run, index, last);
|
|
205
217
|
if (run.hasEOL) last.eol = true;
|
|
206
|
-
}
|
|
207
|
-
return lines.map((line) => lineText(line) + (line.eol ? "\n" : "")).join("");
|
|
218
|
+
});
|
|
219
|
+
return lines.sort((p, q) => p.anchor - q.anchor).map((line) => lineText(line) + (line.eol ? "\n" : "")).join("");
|
|
208
220
|
}
|
|
209
221
|
|
|
210
222
|
//#endregion
|