@li-canghai/dsh-paper 0.8.2 → 0.8.6
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/README.md +42 -93
- package/THIRD_PARTY_NOTICES.md +69 -0
- package/lib/annotate-tool.js +58 -10
- package/lib/block-cache.js +45 -26
- package/lib/blocks-reconcile.js +56 -17
- package/lib/client-parts/01-pure-helpers.js +216 -7
- package/lib/client-parts/02-author-year.js +111 -24
- package/lib/client-parts/03-pdf-stack.js +97 -0
- package/lib/client.js +1206 -293
- package/lib/code-source.js +8 -8
- package/lib/discussion.js +2 -101
- package/lib/export.js +228 -14
- package/lib/file-lock.js +128 -16
- package/lib/gloss.js +12 -3
- package/lib/index.js +137 -68
- package/lib/inline-html.js +1 -1
- package/lib/item-ops.js +14 -2
- package/lib/library-service.js +32 -8
- package/lib/library-tool.js +6 -0
- package/lib/metadata-enrichment.js +8 -1
- package/lib/pdf-analysis-tool.js +72 -0
- package/lib/pdf-page.js +600 -8
- package/lib/picker.js +19 -5
- package/lib/reader-store.js +14 -10
- package/lib/rename-document.js +25 -3
- package/lib/search-index.js +0 -9
- package/lib/selection-message.js +2 -3
- package/lib/storage.js +122 -10
- package/lib/store-paths.js +0 -13
- package/lib/table-gfm.js +42 -14
- package/lib/tex-fetch.js +0 -1
- package/lib/tex-merge.js +0 -1
- package/lib/tex-prepare.js +74 -13
- package/lib/text-similarity.js +0 -9
- package/lib/tool-host.js +4 -1
- package/lib/translate-queue.js +1 -34
- package/lib/translate-task.js +1 -1
- package/lib/translate.js +57 -26
- package/lib/update-contracts.js +5 -5
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +4 -2
package/lib/pdf-page.js
CHANGED
|
@@ -202,21 +202,466 @@ export function pdfQuoteInText(text, quote) {
|
|
|
202
202
|
*/
|
|
203
203
|
const STRUCTURAL_KINDS = new Set(['image', 'figure', 'table', 'chart', 'code'])
|
|
204
204
|
|
|
205
|
+
/**
|
|
206
|
+
* **页边材料**:页眉、页脚、脚注、页码、侧栏批注 —— 它们不在正文流程里。
|
|
207
|
+
*
|
|
208
|
+
* 🔴 排到**整页正文之后**,不参与正文的阅读顺序。理由是这个功能的直接用途:
|
|
209
|
+
* 用户框选一段正文时,浏览器取的是 DOM 里两端点之间的全部节点,所以夹在正文流程
|
|
210
|
+
* 中间的脚注会被一并涂蓝。用户实测报的就是这个(「框选正文时下面的小字说明也被框进来了」)。
|
|
211
|
+
*
|
|
212
|
+
* 判据用块类型,不用几何位置:脚注在物理上位于页面底部,但按「左栏从上到下、
|
|
213
|
+
* 再右栏从上到下」的顺序,它正好落在左栏正文之后、右栏之前 —— 用户从摘要一路往下拖到
|
|
214
|
+
* 右栏就跨过了它。几何位置算不出「它不属于正文」这件事,块类型可以。
|
|
215
|
+
*
|
|
216
|
+
* 🔴 **口径是译文块类型(`splitBlocks` 的产出),不是 MinerU 的原始 type。**
|
|
217
|
+
* 这一层拿到的 `blocks` 已经过归一:`page_footnote` → `note`,而 `aside_text` /
|
|
218
|
+
* `header` / `footer` / `page_number` 在切块阶段就被整类跳过、根本不存在对应块
|
|
219
|
+
* (见 blocks.js 的 `SKIPPED_TYPES`,它们不占页内序号)。所以真正会走到这里的页边
|
|
220
|
+
* 材料只有 `note` 一种。
|
|
221
|
+
*
|
|
222
|
+
* 曾经在这里写的是 MinerU 原名(`page_footnote` 等),结果一个都匹配不上、整段空转:
|
|
223
|
+
* 实测 GrainGrasp 第 1 页脚注仍排在左栏正文之后、右栏之前,拖选正文照样把它涂蓝。
|
|
224
|
+
* 旧用例之所以是绿的,是因为夹具直接喂了原名,绕过了 `splitBlocks` 这一层归一。
|
|
225
|
+
*
|
|
226
|
+
* 原始名一并留着:这一层是导出的纯函数,调用方未必都走切块(测试、离线工具)。
|
|
227
|
+
*
|
|
228
|
+
* 🔴 `ref_text` → `references` **不在此列**:它是参考文献条目,属于正文流程
|
|
229
|
+
* (用户读综述时会框它)。
|
|
230
|
+
*/
|
|
231
|
+
const MARGIN_KINDS = new Set(['note', 'page_footnote', 'aside_text', 'header', 'footer', 'page_number'])
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 这一行是不是**竖排文字**(旋转 90° 的 arXiv 侧栏章、书脊、侧边水印)。
|
|
235
|
+
*
|
|
236
|
+
* 🔴 为什么必须单独判:这类文字在 PDF 里是「一整串字符沿 y 推进、每个字符自己只占
|
|
237
|
+
* 一个字宽」,抽出来的词只有一个、bbox 却是一根瘦高的长条(实测 GrainGrasp 第 1 页
|
|
238
|
+
* `arXiv:2405.09310v2`:x=23–59、y=497–707,宽 36 高 210)。它离它下面那段正文的
|
|
239
|
+
* 距离只有 20 出头,**几何上就是"紧挨着"**,于是被兜底分支吸进最近的正文块 ——
|
|
240
|
+
* 那个块因此被拆成上下两段(摘要 p1-p2 → 印章段 + 摘要段),DOM 顺序里凭空多出一个
|
|
241
|
+
* 跨了半页的容器:用户从摘要首字拖下去,印章跟着涂蓝,而中间那段正文则被拆散。
|
|
242
|
+
*
|
|
243
|
+
* 判据落在**字符推进方向**上,不落在外框长宽比:公式里的分式、表格里的竖排表头
|
|
244
|
+
* 同样是瘦高外框,但它们的字符仍沿 x 推进。只看 bbox 比例会误伤。
|
|
245
|
+
*
|
|
246
|
+
* @param {object[]} words 同一物理行的词
|
|
247
|
+
* @returns {boolean} 字符主要沿 y 推进,且这一行明显是竖的
|
|
248
|
+
*/
|
|
249
|
+
function rotatedWord(word) {
|
|
250
|
+
const chars = Array.isArray(word.chars) ? word.chars : []
|
|
251
|
+
if (chars.length < 2) return false
|
|
252
|
+
let down = 0, across = 0
|
|
253
|
+
for (let i = 1; i < chars.length; i += 1) {
|
|
254
|
+
const previous = chars[i - 1].bbox
|
|
255
|
+
const current = chars[i].bbox
|
|
256
|
+
down += Math.abs((current[1] + current[3]) / 2 - (previous[1] + previous[3]) / 2)
|
|
257
|
+
across += Math.abs((current[0] + current[2]) / 2 - (previous[0] + previous[2]) / 2)
|
|
258
|
+
}
|
|
259
|
+
// 只认推进方向,**不看外框长宽比**:同一串竖排文字里,单个词的外框可能是宽的
|
|
260
|
+
// (实测 `16` 两字符:宽 36、高 25,仍是沿 y 推进)。用长宽比筛会把这类词漏掉,
|
|
261
|
+
// 于是同一道竖排章里几个词分到两边 —— 一部分当页边材料、一部分留在正文,
|
|
262
|
+
// 那一段正文照样被拆成两截。
|
|
263
|
+
return down > across * 3
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** 一行是不是竖排:这一行里**全部**可判定的词都沿 y 推进。 */
|
|
267
|
+
function rotatedLine(words) {
|
|
268
|
+
let measured = 0
|
|
269
|
+
for (const word of words) {
|
|
270
|
+
if (!Array.isArray(word.chars) || word.chars.length < 2) continue
|
|
271
|
+
measured += 1
|
|
272
|
+
if (!rotatedWord(word)) return false
|
|
273
|
+
}
|
|
274
|
+
return measured > 0
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 一行文字离段落块的最大允许距离(0–1000 归一化坐标)。
|
|
279
|
+
*
|
|
280
|
+
* 实测口径:正常归属的行离自己的块为 0;跨栏续接的段落会把对侧栏的行拉进来,
|
|
281
|
+
* 那批行离块 211–602。取 50 既容得下块框与实际排版之间的常规偏差(实测最大 38),
|
|
282
|
+
* 也挡得住跨栏的那一批。
|
|
283
|
+
*/
|
|
284
|
+
const MAX_BLOCK_DISTANCE = 50
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 内容匹配命中时仍允许的最大距离。比 {@link MAX_BLOCK_DISTANCE} 宽,因为图注、
|
|
288
|
+
* 续行这类同栏文字离块框可以很远(实测回归用例里图注离图框 120)。
|
|
289
|
+
*
|
|
290
|
+
* 上限来自实测:图内标注文字离它被误配的块 596,而真正的图注续行 ≤ 120。
|
|
291
|
+
* 取 200 落在两者中间,两头都分得开。
|
|
292
|
+
*/
|
|
293
|
+
const MAX_MATCHED_DISTANCE = 200
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* 三个**版面尺度**,全部以行高为单位。
|
|
297
|
+
*
|
|
298
|
+
* 🔴 绝对值在这里没有普适性:8pt 的双栏会议模板与 12pt 的单栏讲义,栏间空隙、行距、段距
|
|
299
|
+
* 全都不同;行高是唯一随版面一起缩放的单位,所以阈值只能用它的倍数表达。
|
|
300
|
+
*
|
|
301
|
+
* · {@link COLUMN_GUTTER_UNITS} —— 一道竖向空白至少这么宽才可能是栏间沟;
|
|
302
|
+
* · {@link ROW_GUTTER_UNITS} —— 一道横向空白至少这么高才值得当成上下两段版面;
|
|
303
|
+
* · {@link GUTTER_TOLERANCE} —— 沟里被别的行压住的比例上限。通栏标题、满宽图注会横穿
|
|
304
|
+
* 栏间沟;只按「完全为空」判,带通栏标题的双栏页就再也找不到栏了。
|
|
305
|
+
*/
|
|
306
|
+
/**
|
|
307
|
+
* 兜底档的栏间沟门槛。
|
|
308
|
+
*
|
|
309
|
+
* 🔴 1.6 这个门槛把**真实存在的窄沟**挡在门外:全库 53 页实测,最窄的一道真实栏间沟
|
|
310
|
+
* 只有 1.03 个行高(IEEE 1981 双栏,左栏右沿 484、右栏左沿 498,沟宽 14.1,行高 14.13)。
|
|
311
|
+
* 那种页面上一道沟都切不出来,阅读顺序退化成一维 y 序,DOM 里左右两栏逐行交替 ——
|
|
312
|
+
* 用户从左栏往下拖,右栏几十行跟着涂蓝。
|
|
313
|
+
*
|
|
314
|
+
* 兜底档只在严格档一道栏都切不出来时启用,且**不进递归**(见 orderRegion)。
|
|
315
|
+
*/
|
|
316
|
+
const COLUMN_GUTTER_UNITS = 1.6
|
|
317
|
+
const RELAXED_GUTTER_UNITS = 0.9
|
|
318
|
+
const ROW_GUTTER_UNITS = 0.55
|
|
319
|
+
const GUTTER_TOLERANCE = 0.12
|
|
320
|
+
|
|
321
|
+
/** 拼正文用的两个字面量。写成常量只为让下面那一行不受模板串转义影响。 */
|
|
322
|
+
const SPACE = ' '
|
|
323
|
+
const EMPTY = ''
|
|
324
|
+
|
|
325
|
+
/** y 序比较器:递归切不动时的兜底,也是同一段内从左到右的次序。 */
|
|
326
|
+
const readingTop = (a, b) => a.box[1] - b.box[1] || a.box[0] - b.box[0]
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 把一组物理行按**视觉阅读顺序**排好 —— 递归 XY 切分。
|
|
330
|
+
*
|
|
331
|
+
* 🔴 为什么要重排:浏览器画原生选区只认 DOM 顺序,不看物理位置。两个视觉上相邻、DOM 上
|
|
332
|
+
* 却隔着一整个右栏的块,用户从左栏一路往下拖就会把中间那些行一起涂蓝。
|
|
333
|
+
*
|
|
334
|
+
* 判据只有一条:**先找栏,找不到栏再分段,都没有就按 y 排**。
|
|
335
|
+
* · 栏:投影覆盖为空的那条竖向空白(允许 {@link GUTTER_TOLERANCE} 的例外,见上);
|
|
336
|
+
* · 段:横向空白,判据同理;
|
|
337
|
+
* · 两级都切不动时,这个局部版面就真的是单栏单段,y 序即阅读序。
|
|
338
|
+
*
|
|
339
|
+
* 🔴 不用「通栏行宽度 ≥ 页宽 55%」那个口径:它要求先知道页宽,而页宽会被页边竖排水印、
|
|
340
|
+
* arXiv 章这类边角元素撑大 —— 于是真正的通栏标题反而算不上通栏。实测 GrainGrasp 第 1 页:
|
|
341
|
+
* 左侧 arXiv 章把页宽撑到 889,标题只有 728(不到 55%),整页因此退化成左右交替。
|
|
342
|
+
* 投影覆盖只问「这一条竖线上有没有字」,与页宽无关,也就没有这个失效模式。
|
|
343
|
+
*
|
|
344
|
+
* 递归必然终止:每层把行切成 ≥2 个非空组,组内行数严格减少。
|
|
345
|
+
*
|
|
346
|
+
* @param {Array<{groupId: string, box: number[]}>} lines 物理行
|
|
347
|
+
* @returns {Array<object>} 同一批行,按阅读顺序
|
|
348
|
+
*/
|
|
349
|
+
function lineReadingOrder(lines) {
|
|
350
|
+
if (lines.length <= 1) return lines
|
|
351
|
+
const heights = lines.map(line => line.box[3] - line.box[1]).filter(height => height > 0).sort((a, b) => a - b)
|
|
352
|
+
const unit = heights.length > 0 ? heights[heights.length >> 1] : 0
|
|
353
|
+
return unit > 0 ? orderRegion(lines, unit, true) : [...lines].sort(readingTop)
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** 一个局部版面的递归切分:能切栏就切栏,否则切段,否则按 y。 */
|
|
357
|
+
function orderRegion(lines, unit, allowRelaxed = false) {
|
|
358
|
+
if (lines.length <= 1) return lines
|
|
359
|
+
// 🔴 **严格优先,放宽兜底,且放宽只给顶层。**
|
|
360
|
+
//
|
|
361
|
+
// 放宽档把居中元素(标题折行、作者行、ISSN 页脚)排除出遮挡,于是窄沟也能成立;
|
|
362
|
+
// 但它同样会在**已经切好的一栏内部**凭空切出假栏 —— 全库实测,无条件放宽时
|
|
363
|
+
// 5 页改善、7 页变差,其中把 L:1 R:1 的干净页面切成 L:2 R:3。
|
|
364
|
+
// 所以:先按原门槛严格切;切不出来才退到放宽档,且**栏内不再放宽**。
|
|
365
|
+
const strict = splitAxis(lines, unit, 0, COLUMN_GUTTER_UNITS, false)
|
|
366
|
+
if (strict !== null) return columnOrder(lines, strict, unit, false)
|
|
367
|
+
const relaxed = allowRelaxed ? splitAxis(lines, unit, 0, RELAXED_GUTTER_UNITS, true) : null
|
|
368
|
+
if (relaxed !== null) return columnOrder(lines, relaxed, unit, true)
|
|
369
|
+
const rows = splitAxis(lines, unit, 1, ROW_GUTTER_UNITS, false)
|
|
370
|
+
if (rows !== null) return sliceByCuts(lines, 1, rows.map(gap => (gap.from + gap.to) / 2)).flatMap(row => orderRegion(row, unit))
|
|
371
|
+
return [...lines].sort(readingTop)
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** 按切割中线把行分到各区(按中心点),丢弃空区,保持顺序。 */
|
|
375
|
+
function sliceByCuts(lines, axis, cuts) {
|
|
376
|
+
const groups = cuts.map(() => [])
|
|
377
|
+
groups.push([])
|
|
378
|
+
for (const line of lines) {
|
|
379
|
+
const center = (line.box[axis] + line.box[axis + 2]) / 2
|
|
380
|
+
let index = 0
|
|
381
|
+
while (index < cuts.length && center >= cuts[index]) index += 1
|
|
382
|
+
groups[index].push(line)
|
|
383
|
+
}
|
|
384
|
+
return groups.filter(group => group.length > 0)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* 切出栏之后的排序:**跨栏的行自成一条分隔带**,先把页面分成上下几截,每截内再按栏读。
|
|
389
|
+
*
|
|
390
|
+
* 🔴 这是 XY 切分里最容易被漏掉的一步。只按「中心落在哪一栏」分配是不够的:通栏标题
|
|
391
|
+
* (实测 GrainGrasp 第 1 页 x=136–864、栏间沟在 495)中心落在右栏,于是标题被排到了脚注
|
|
392
|
+
* 之后 —— 用户从摘要拖到 INTRODUCTION 时,选区就把整个右栏一并扫了进去。
|
|
393
|
+
*
|
|
394
|
+
* 分隔带的位置由**行自己的 y** 决定:一条跨栏行把它上方的行与下方的行隔开。于是通栏标题
|
|
395
|
+
* 回到顶部、右栏图注留在右栏该在的高度,左右两栏各自成段,DOM 顺序与视觉顺序一致。
|
|
396
|
+
*
|
|
397
|
+
* 带内的行一律**整行**落在某一侧(跨栏的已经全被抽走),所以这里按左沿分栏不会出错。
|
|
398
|
+
*
|
|
399
|
+
* @param {object[]} lines 同一局部版面的行
|
|
400
|
+
* @param {number[]} cuts 栏间沟的中线(升序)
|
|
401
|
+
* @param {number} unit 行高
|
|
402
|
+
* @returns {object[]} 按阅读顺序
|
|
403
|
+
*/
|
|
404
|
+
function columnOrder(lines, gaps, unit, relaxed = false) {
|
|
405
|
+
// 🔴 **跨栏**的意思是"横穿整道沟",不是"碰到沟"。只按切割中线判会把**探进沟里**但没
|
|
406
|
+
// 穿过去的那一行也算成跨栏 —— 实测夹具里左栏段落右沿到 420、沟是 300–514,它只是
|
|
407
|
+
// 探进沟里 120,却被当成跨栏行抽走,于是整页顺序错位。判据必须是两端都在沟外。
|
|
408
|
+
const spanning = new Set(lines.filter(line => gaps.some(gap => line.box[0] < gap.from && line.box[2] > gap.to)))
|
|
409
|
+
const separators = [...spanning].sort(readingTop)
|
|
410
|
+
const bands = separators.map(() => [])
|
|
411
|
+
bands.push([])
|
|
412
|
+
for (const line of lines) {
|
|
413
|
+
if (spanning.has(line)) continue
|
|
414
|
+
const center = (line.box[1] + line.box[3]) / 2
|
|
415
|
+
let index = 0
|
|
416
|
+
while (index < separators.length && (separators[index].box[1] + separators[index].box[3]) / 2 < center) index += 1
|
|
417
|
+
bands[index].push(line)
|
|
418
|
+
}
|
|
419
|
+
const cuts = gaps.map(gap => (gap.from + gap.to) / 2)
|
|
420
|
+
const ordered = []
|
|
421
|
+
for (let i = 0; i < bands.length; i += 1) {
|
|
422
|
+
for (const column of sliceByCuts(bands[i], 0, cuts)) ordered.push(...orderRegion(column, unit))
|
|
423
|
+
if (i < separators.length) ordered.push(separators[i])
|
|
424
|
+
}
|
|
425
|
+
return ordered
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* 沿 axis 找出可切的空档并合并,返回切割中线(升序);切不开时返回 null。
|
|
430
|
+
*
|
|
431
|
+
* 两侧都得有整行才算切开 —— 横跨过去的那一行本来就不属于任何一侧,不能凭它判定沟成立。
|
|
432
|
+
* 单栏页上由此得到的"沟"两侧必有一侧没有整行(正文横跨整页),于是被这条判据挡掉,
|
|
433
|
+
* 不会把单栏页切成两半。
|
|
434
|
+
*/
|
|
435
|
+
function splitAxis(lines, unit, axis, units, relaxed = false) {
|
|
436
|
+
const gaps = freeIntervals(lines, axis, unit, relaxed)
|
|
437
|
+
.filter(([from, to]) => to - from >= unit * units)
|
|
438
|
+
.filter(([from, to]) => {
|
|
439
|
+
// 严格档沿用原判据(两侧各至少一行):**改造前的行为要逐字节保留**,
|
|
440
|
+
// 否则放宽一档的收益会被严格档的收紧抵消掉(全库实测:收紧后 2 页改善、5 页变差)。
|
|
441
|
+
// 兜底档才要求两侧都有足够的行 —— 那一档的门槛低得多,一个落单的词就能切出假栏。
|
|
442
|
+
const need = relaxed ? Math.max(3, Math.ceil(lines.length * 0.15)) : 1
|
|
443
|
+
const left = lines.filter(line => line.box[axis + 2] <= from)
|
|
444
|
+
const right = lines.filter(line => line.box[axis] >= to)
|
|
445
|
+
if (left.length < need || right.length < need) return false
|
|
446
|
+
// 🔴 兜底档还要求两侧各有**真正的正文行**,不能只是孤字。
|
|
447
|
+
// 实测(4aeb3d6f 第 1 页):x=484–492 处挂着一个孤字 "1"(宽 8、高 11),
|
|
448
|
+
// 它一侧的行数达标,可那一侧其实只有一个字符 —— 于是切出一道假沟,
|
|
449
|
+
// 把一个完整的右栏劈成两段,DOM 段数 0 → 1。
|
|
450
|
+
//
|
|
451
|
+
// 判据用**行的宽度**而不是高度合计:宽度直接说明"这一侧排着正文",
|
|
452
|
+
// 高度合计会被一列落单词堆出来,也容易被孤零零的一行撑够。
|
|
453
|
+
// 门槛取中位行宽的 40%:真正的栏里至少有一行排到这个宽度,孤字永远到不了。
|
|
454
|
+
if (!relaxed) return true
|
|
455
|
+
const widthsOf = side => side.map(line => line.box[axis + 2] - line.box[axis]).filter(width => width > 0).sort((a, b) => a - b)
|
|
456
|
+
const allWidths = widthsOf(lines)
|
|
457
|
+
const typicalWidth = allWidths.length > 0 ? allWidths[allWidths.length >> 1] : 0
|
|
458
|
+
if (!(typicalWidth > 0)) return false
|
|
459
|
+
const hasBody = side => side.some(line => line.box[axis + 2] - line.box[axis] >= typicalWidth * 0.4)
|
|
460
|
+
return hasBody(left) && hasBody(right)
|
|
461
|
+
})
|
|
462
|
+
// 🔴 每层只切**一道**最宽的沟。递归会在每一栏内部继续找下一道,所以多栏页面照样
|
|
463
|
+
// 切得出来;但同一层切好几刀时,一道被落单的字劈开的沟会被当成两道,中间凭空多出
|
|
464
|
+
// 一栏(只装那一个字),两侧正文反而落到更远的栏里。
|
|
465
|
+
.map(([from, to]) => ({ from, to, width: to - from }))
|
|
466
|
+
.sort((a, b) => b.width - a.width)
|
|
467
|
+
.slice(0, 1)
|
|
468
|
+
return gaps.length > 0 ? gaps : null
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* 局部版面里**被字压得足够少**的区间:axis=0 得到栏间沟,axis=1 得到段间带。
|
|
472
|
+
*
|
|
473
|
+
* 覆盖按**另一根轴**的长度加权:一条行在 x 上占的那一格,挡住的是它自己那么高的一条竖线。
|
|
474
|
+
* 于是"这条竖线上一共有多少高度被字压着"才是判据,行宽不参与运算 —— 通栏标题压过栏间沟
|
|
475
|
+
* 时也只贡献它自己那一行的高度,不会像"找最宽空隙"那样把整条沟一票否决。
|
|
476
|
+
*
|
|
477
|
+
* 🔴 两端必须显式入表(`bump(from, 0)` / `bump(to, 0)`)。投影只在**词框边界**上取值,
|
|
478
|
+
* 而一道沟的右沿往往没有词从那里开始(右侧栏的文字从更右边才起头),那个位置就永远
|
|
479
|
+
* 不会成为扫描点,沟的右半截也就丢掉了 —— 实测 GrainGrasp 第 3 页的栏间沟因此只剩左半。
|
|
480
|
+
*
|
|
481
|
+
* 相邻的空档间隔不到半个行高时合并:它们视觉上是同一道沟,被某个落单的字切开而已。
|
|
482
|
+
* 不合并就会在同一道沟里切出两刀,中间凭空多出一栏,那一栏里的行又会变成跨栏行。
|
|
483
|
+
*
|
|
484
|
+
* @param {object[]} lines 同一局部的行
|
|
485
|
+
* @param {0|1} axis 0 = 找竖向空白(栏),1 = 找横向空白(段)
|
|
486
|
+
* @param {number} unit 行高
|
|
487
|
+
* @returns {Array<[number, number]>} 按位置升序的空白区间
|
|
488
|
+
*/
|
|
489
|
+
function freeIntervals(lines, axis, unit, relaxed = false) {
|
|
490
|
+
const across = axis === 0 ? 1 : 0
|
|
491
|
+
const from = Math.min(...lines.map(line => line.box[axis]))
|
|
492
|
+
const to = Math.max(...lines.map(line => line.box[axis + 2]))
|
|
493
|
+
const total = Math.max(...lines.map(line => line.box[across + 2])) - Math.min(...lines.map(line => line.box[across]))
|
|
494
|
+
if (!(to > from) || !(total > 0)) return []
|
|
495
|
+
// 🔴 **判据是「有没有字」,但阈值不能取字面的 0。**
|
|
496
|
+
//
|
|
497
|
+
// 曾经用「被压住的长度 ≤ 页面高度的 12%」当**语义**容差,两处都出错:一列只有 6 行、
|
|
498
|
+
// 却纵向铺开小半页的边缘元素(arXiv 印章)比值很低,于是那一列被判成空白,印章被并进
|
|
499
|
+
// 正文;反过来行数很少的页面上,几行正文的高度占比就能超过 12%,栏间沟又判不出来。
|
|
500
|
+
// 语义容差确实只会引入这两种相反的失效 —— 那部分结论仍然成立。
|
|
501
|
+
//
|
|
502
|
+
// 但阈值也不能是**字面的 0**:depth 是浮点累加量,一道真正空着的沟算出来可能不是 0。
|
|
503
|
+
// 实测(Grounding Agent Memory p2,双栏 A4):沟宽 25.7,depth 停在 2.84e-14 ——
|
|
504
|
+
// 正的,于是 `depth > 0` 把整条沟跳过,freeIntervals 返回空,splitAxis 判本页无栏,
|
|
505
|
+
// 阅读顺序退化成一维 y 序,DOM 里左右两栏逐行交替。用户从左栏往下拖,浏览器按 DOM
|
|
506
|
+
// 线性区间铺选区,右栏几十行跟着涂蓝。
|
|
507
|
+
//
|
|
508
|
+
// 残差从哪来:同一行的 weight 加进去又减出来,两次累加的**顺序不同**(进入按 x 排序、
|
|
509
|
+
// 离开按另一处顺序),IEEE 754 加法不满足结合律,误差就留下了。
|
|
510
|
+
//
|
|
511
|
+
// 所以容差必须是**量纲化的**:取 total(本区另一根轴上字的总跨度)的 1e-9。它远大于
|
|
512
|
+
// 任何浮点残差(上例 total≈836,容差 8.4e-7,是残差的 3000 万倍),又远小于任何一个
|
|
513
|
+
// 真实字形的高度(最小也是 1 单位量级)—— 真有一行压在这条竖线上,depth 至少是它的
|
|
514
|
+
// 高度,不可能被这个容差吃掉。两个方向都留了足够的安全边界。
|
|
515
|
+
//
|
|
516
|
+
// 落单的字横穿沟不吃紧:它只盖住沟的一小截,剩下的仍然够宽;真被劈成两半时,下面
|
|
517
|
+
// 按半个行高合并的那一步会把它们并回去。
|
|
518
|
+
const epsilon = total * 1e-9
|
|
519
|
+
const deltas = new Map()
|
|
520
|
+
const bump = (key, value) => deltas.set(key, (deltas.get(key) ?? 0) + value)
|
|
521
|
+
bump(from, 0)
|
|
522
|
+
bump(to, 0)
|
|
523
|
+
// 🔴 **通栏行不算挡路**:标题、满宽图注、跨栏表格横穿栏间沟都是正常排版。把它们也
|
|
524
|
+
// 算进覆盖,带一个通栏标题的双栏页就再也找不到栏了。
|
|
525
|
+
//
|
|
526
|
+
// 判据取「宽度 ≥ 本区中位行宽的 1.5 倍」,不按整页宽度算。按整页宽度算会在**子区**里
|
|
527
|
+
// 失效:切出左栏之后再找左栏内部的沟时,正文行本身就占了该区八成宽,于是被当成通栏行
|
|
528
|
+
// 豁免掉,左侧竖排章(arXiv 印章)反而切不出来 —— 实测 GrainGrasp 第 1 页的竖排章
|
|
529
|
+
// 因此与摘要交替排布,摘要被拆成 5 段。中位数随区域缩放,没有这个失效模式。
|
|
530
|
+
//
|
|
531
|
+
// 🔴 **中位数只统计正文行,不统计结构性块(图片 / 图表 / 表格 / 代码)里的字。**
|
|
532
|
+
// 这是同一个"判据的输入被它要判的对象污染"的失效,只是绕了一层:
|
|
533
|
+
//
|
|
534
|
+
// 实测(Grounding Agent Memory 第 4 页):页面顶部一张横跨两栏的大图,图内小字 79 行,
|
|
535
|
+
// 行宽中位数 77.6;正文段落 54 行,行宽中位数 366.5。**全部行**的中位数被图内碎字拉到
|
|
536
|
+
// 154.5,通栏门槛于是变成 231.8 —— 正文行(366.5)个个超标,全被当成通栏豁免掉,
|
|
537
|
+
// 图内碎字反倒成了唯一的遮挡源。栏间沟因此找不到,整页 DOM 又退回左右交替。
|
|
538
|
+
//
|
|
539
|
+
// 图里的字不是**正文流**,它不构成版面网格,拿它估计正文的行宽尺度必然失真。按块类型
|
|
540
|
+
// 剔除之后,这一页的 typical 回到 366.5、门槛 549.8,正文行不再被误判。
|
|
541
|
+
//
|
|
542
|
+
// 整页都是结构性块时(纯图页)没有正文可估,退回全部行 —— 那时的"正文尺度"就是它自己。
|
|
543
|
+
const widthsOf = filter => lines.filter(filter).map(line => line.box[axis + 2] - line.box[axis]).filter(width => width > 0).sort((a, b) => a - b)
|
|
544
|
+
const textWidths = widthsOf(line => line.structural !== true)
|
|
545
|
+
const widths = textWidths.length > 0 ? textWidths : widthsOf(() => true)
|
|
546
|
+
const typical = widths.length > 0 ? widths[widths.length >> 1] : 0
|
|
547
|
+
const spanning = line => typical > 0 && line.box[axis + 2] - line.box[axis] >= typical * 1.5
|
|
548
|
+
// 🔴 **居中元素不算挡路**(兜底档才有)。
|
|
549
|
+
//
|
|
550
|
+
// 实测(Robert G. Keys 1981 第 1 页):标题折行 "Processing"、作者 "ROBERT G. KEYS"、
|
|
551
|
+
// 页脚 ISSN 号三行横跨栏间沟,合起来在沟上盖了 58.8 高 —— 正好等于沟上全部覆盖量。
|
|
552
|
+
// 它们既不属于左栏也不属于右栏,可又都**不够宽**,逃不过通栏豁免(中位行宽 410、
|
|
553
|
+
// 门槛 615,它们只有 174 / 129 / 317),于是整道沟被判死。
|
|
554
|
+
//
|
|
555
|
+
// 判据:宽度不到通栏、且中点贴近版面中线。两条都要,缺一条就会把**贴着中线起排的
|
|
556
|
+
// 正文行**也算进来 —— 那会真的把沟让开,属于放行过多。
|
|
557
|
+
const midway = (from + to) / 2
|
|
558
|
+
const centred = line => {
|
|
559
|
+
const width = line.box[axis + 2] - line.box[axis]
|
|
560
|
+
// 🔴 **下界不能省**:一个落在页面中线上的孤字(实测宽度 8,中位行宽 191)也会满足
|
|
561
|
+
// "贴近中线 + 不够通栏"两条,被豁免掉之后反而在它身上切出一道假沟,把一个本来
|
|
562
|
+
// 干净的页面切成两栏。真正的居中元素(标题、作者行)至少占正文宽度的一大截。
|
|
563
|
+
// 上界:够宽就已经是通栏行了,不需要这条豁免。
|
|
564
|
+
if (!(typical > 0) || !(width >= typical * 0.3) || !(width < typical * 1.5)) return false
|
|
565
|
+
return Math.abs((line.box[axis] + line.box[axis + 2]) / 2 - midway) <= typical * 0.35
|
|
566
|
+
}
|
|
567
|
+
for (const line of lines) {
|
|
568
|
+
const start = line.box[axis]
|
|
569
|
+
const end = line.box[axis + 2]
|
|
570
|
+
const weight = line.box[across + 2] - line.box[across]
|
|
571
|
+
if (!(end > start) || !(weight > 0) || spanning(line)) continue
|
|
572
|
+
bump(start, weight)
|
|
573
|
+
bump(end, -weight)
|
|
574
|
+
}
|
|
575
|
+
// 🔴 兜底档另算一条**只累加正文行**的投影:居中元素排除在外。
|
|
576
|
+
// 严格档直接用上面那条原有投影,行为与改造前逐字节一致 —— 放宽只在真的切不出栏时发生。
|
|
577
|
+
const relaxedDeltas = new Map()
|
|
578
|
+
if (relaxed) {
|
|
579
|
+
const relaxedBump = (key, value) => relaxedDeltas.set(key, (relaxedDeltas.get(key) ?? 0) + value)
|
|
580
|
+
relaxedBump(from, 0)
|
|
581
|
+
relaxedBump(to, 0)
|
|
582
|
+
for (const line of lines) {
|
|
583
|
+
const start = line.box[axis]
|
|
584
|
+
const end = line.box[axis + 2]
|
|
585
|
+
const weight = line.box[across + 2] - line.box[across]
|
|
586
|
+
if (!(end > start) || !(weight > 0) || spanning(line) || centred(line)) continue
|
|
587
|
+
relaxedBump(start, weight)
|
|
588
|
+
relaxedBump(end, -weight)
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
const positions = relaxed
|
|
592
|
+
? [...new Set([...deltas.keys(), ...relaxedDeltas.keys()])].sort((a, b) => a - b)
|
|
593
|
+
: [...deltas.keys()].sort((a, b) => a - b)
|
|
594
|
+
const raw = []
|
|
595
|
+
let depth = 0
|
|
596
|
+
let relaxedDepth = 0
|
|
597
|
+
for (let i = 0; i < positions.length - 1; i += 1) {
|
|
598
|
+
depth += deltas.get(positions[i]) ?? 0
|
|
599
|
+
if (relaxed) relaxedDepth += relaxedDeltas.get(positions[i]) ?? 0
|
|
600
|
+
// 容差而不是 0:depth 是浮点累加量,见上面 epsilon 的说明。
|
|
601
|
+
// 兜底档看**排除居中元素之后**的覆盖:那才是真正的正文遮挡。
|
|
602
|
+
if ((relaxed ? relaxedDepth : depth) > epsilon) continue
|
|
603
|
+
const start = positions[i]
|
|
604
|
+
const end = positions[i + 1]
|
|
605
|
+
if (end > start) raw.push([start, end])
|
|
606
|
+
}
|
|
607
|
+
const merged = []
|
|
608
|
+
for (const gap of raw) {
|
|
609
|
+
const last = merged.at(-1)
|
|
610
|
+
if (last !== undefined && gap[0] - last[1] < unit * 0.5) last[1] = gap[1]
|
|
611
|
+
else merged.push([gap[0], gap[1]])
|
|
612
|
+
}
|
|
613
|
+
return merged
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* 一个块在某一页上的**完整正文**(所有 part 合起来)。
|
|
618
|
+
*
|
|
619
|
+
* 块可能被拆成多段 DOM(见 splitRuns),但划线校验、高亮定位、对齐都按**整块**做 ——
|
|
620
|
+
* 只取第一段会漏掉被图或公式隔开的另一半。
|
|
621
|
+
*
|
|
622
|
+
* @param {object[]} groups pdfPageGroups 的产物
|
|
623
|
+
* @param {string} blockId 块 id
|
|
624
|
+
* @returns {string} 整块正文;这一页没有这个块时返回空串
|
|
625
|
+
*/
|
|
626
|
+
export function pdfBlockText(groups, blockId) {
|
|
627
|
+
const parts = (Array.isArray(groups) ? groups : []).filter(group => group?.id === blockId)
|
|
628
|
+
if (parts.length === 0) return ''
|
|
629
|
+
return parts[0].fullText ?? parts.map(part => part.text).join(' ')
|
|
630
|
+
}
|
|
631
|
+
|
|
205
632
|
export function pdfPageGroups(page, blocks) {
|
|
206
633
|
const rows = (Array.isArray(blocks) ? blocks : []).filter(block => block.page === page.page && validBox(block.bbox))
|
|
207
634
|
const texts = new Map(rows.map(row => [row.id, matchingText(row.sourceText ?? '')]))
|
|
208
635
|
// 块类型:结构性块不参与正文词归属(见 STRUCTURAL_KINDS)。
|
|
209
636
|
const styles = new Map(rows.map(row => [row.id, String(row.type ?? '')]))
|
|
637
|
+
// 🔴 切行时**竖排词单独成行**:竖排文字沿 y 推进,同一串字的每个词在 bbox 上纵向
|
|
638
|
+
// 紧挨着、横向完全重合,而横向排版的行是按 x 排的 —— 用 `sameLine` 判会让竖排章被
|
|
639
|
+
// 反复切断或与旁边的正文行合并。先按推进方向决定「能不能接在同一条行上」,
|
|
640
|
+
// 竖排词只接竖排词,横排词只接横排词。
|
|
210
641
|
const lines = []
|
|
211
642
|
for (const word of page.words) {
|
|
212
643
|
const previous = lines.at(-1)?.words.at(-1)
|
|
213
644
|
const gap = previous ? (word.bbox[0] - previous.bbox[2]) * page.width : 0
|
|
214
645
|
const height = (word.bbox[3] - word.bbox[1]) * page.height
|
|
215
|
-
|
|
646
|
+
const sameOrientation = previous !== undefined && rotatedWord(previous) === rotatedWord(word)
|
|
647
|
+
if (!previous || !sameOrientation || !sameLine(previous.bbox, word.bbox) || gap > height * 3) lines.push({ words: [] })
|
|
216
648
|
lines.at(-1).words.push(word)
|
|
217
649
|
}
|
|
650
|
+
// 🔴 竖排文字(侧栏 arXiv 章、水印、书脊)**不参与归属**:它离下面那段正文几何上紧挨着,
|
|
651
|
+
// 兜底分支必然把它吸进最近的正文档,那块因此被拆成"印章段 + 正文段"两截,DOM 里凭空
|
|
652
|
+
// 多出一个跨半页的容器 —— 用户从那段正文首字拖下去就把印章一起涂蓝。
|
|
653
|
+
// 直接丢掉而不是另立一段:这类文字是版面装饰,不该成为标注锚点,而块 id 由上游给定,
|
|
654
|
+
// 这里凭空造一个会和重解析对齐规则冲突。
|
|
655
|
+
//
|
|
656
|
+
// 🔴 整页多数行都竖着时**不这么判**:那是扫描件整页旋转(页面本身横过来了),
|
|
657
|
+
// 全部丢弃会让这一页一个字都选不中。此时按普通版面处理,交给 XY 切分去排。
|
|
658
|
+
for (const line of lines) line.rotated = rotatedLine(line.words)
|
|
659
|
+
if (lines.filter(line => line.rotated).length * 2 > lines.length) {
|
|
660
|
+
for (const line of lines) line.rotated = false
|
|
661
|
+
}
|
|
218
662
|
const grouped = new Map()
|
|
219
663
|
for (const line of lines) {
|
|
664
|
+
if (line.rotated) continue
|
|
220
665
|
const text = matchingText(line.words.map(word => word.text).join(' '))
|
|
221
666
|
const matches = text.length >= 12 ? rows.filter(row => texts.get(row.id).includes(text)) : []
|
|
222
667
|
const x = (line.words[0].bbox[0] + line.words.at(-1).bbox[2]) / 2
|
|
@@ -253,23 +698,70 @@ export function pdfPageGroups(page, blocks) {
|
|
|
253
698
|
&& !STRUCTURAL_KINDS.has(styles.get(other.id))
|
|
254
699
|
&& row.bbox[0] < other.bbox[2] && other.bbox[0] < row.bbox[2]
|
|
255
700
|
&& row.bbox[1] < other.bbox[3] && other.bbox[1] < row.bbox[3])
|
|
256
|
-
|
|
701
|
+
/**
|
|
702
|
+
* 在候选里挑最近的块。
|
|
703
|
+
*
|
|
704
|
+
* 🔴 几何闸门**所有轮次都生效**,内容匹配也不例外。原因是一次实测:
|
|
705
|
+
* 「跨栏续接的段落」MinerU 给的 bbox 只有它起头那一栏的一小截,sourceText 却是整段。
|
|
706
|
+
* 右栏中部的行能在它的 sourceText 里找到,于是内容匹配命中、几何被完全忽略 ——
|
|
707
|
+
* 实测 ResNet 风格的双栏页上,一个 bbox 在左栏底部的块收走了右栏中部 12 行
|
|
708
|
+
* (最远距离 436)。这些行进了同一个 DOM 容器,浏览器按 DOM 顺序铺原生选区,
|
|
709
|
+
* 用户从左栏往下拖就把右栏整片涂蓝(本机第 4 页实测:选区横跨 32 个 DOM 节点,
|
|
710
|
+
* 其中 28 行是右栏)。
|
|
711
|
+
*
|
|
712
|
+
* 闸门只在**最后一轮**放开:三轮都够不着时按纯距离取最近块,
|
|
713
|
+
* 宁可把一行安置到远处,也不能让它掉出分组(掉词会让这一行永远选不中)。
|
|
714
|
+
*/
|
|
715
|
+
/**
|
|
716
|
+
* 内容匹配命中时,这一行还算不算这个块的。两条都要满足:
|
|
717
|
+
*
|
|
718
|
+
* ① **同一栏**(横向重叠 ≥ 较窄者宽度的 25%)。只按距离判断会漏掉跨栏续接的段落:
|
|
719
|
+
* bbox 在左栏、sourceText 却是整段,右栏中部那些行内容全对得上而横向完全不沾边
|
|
720
|
+
* (实测第 4 页:x 507–881 对 x 112–489,中间隔着栏间空隙)—— 收进来就让 DOM
|
|
721
|
+
* 跨栏,选区整片涂蓝。图注续行与图框几乎完全重叠,跨栏的两侧重叠为负,这个判据
|
|
722
|
+
* 两头都分得开。
|
|
723
|
+
* ② **不算隔得太远**({@link MAX_MATCHED_DISTANCE})。同栏也会出问题:实测第 4 页
|
|
724
|
+
* 图内两行 `Coreference Resolution via / Structural Alignment` 在图框里(y≈266),
|
|
725
|
+
* 而 blk-38 的 sourceText 正是以这句开头,于是被内容匹配抓走(距离 596)——
|
|
726
|
+
* 这个块因此纵跨 y=266~921,按首行位置排序就跑到页面中段去了。
|
|
727
|
+
*/
|
|
728
|
+
const lineLeft = line.words[0].bbox[0]
|
|
729
|
+
const lineRight = line.words.at(-1).bbox[2]
|
|
730
|
+
const sameColumn = row => {
|
|
731
|
+
const overlap = Math.min(lineRight, row.bbox[2]) - Math.max(lineLeft, row.bbox[0])
|
|
732
|
+
const narrower = Math.min(lineRight - lineLeft, row.bbox[2] - row.bbox[0])
|
|
733
|
+
return narrower > 0 && overlap >= narrower * 0.25
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* 在候选里挑最近的块。
|
|
737
|
+
*
|
|
738
|
+
* 闸门分两档:
|
|
739
|
+
* · `matched` —— 这一行的内容在候选块里找得到,或用整页块兜底时,只要求**同栏**;
|
|
740
|
+
* · 其余情况(纯几何归属)仍用 {@link MAX_BLOCK_DISTANCE}。
|
|
741
|
+
*/
|
|
742
|
+
const pick = (candidates, { column = false } = {}) => {
|
|
257
743
|
let chosenRow = null, bestScore = Infinity
|
|
258
744
|
for (const row of candidates) {
|
|
259
745
|
const { distance, score } = scoreOf(row)
|
|
260
|
-
if (!
|
|
746
|
+
if (column ? (!sameColumn(row) || distance > MAX_MATCHED_DISTANCE) : distance > MAX_BLOCK_DISTANCE) continue
|
|
261
747
|
if (score < bestScore) { bestScore = score; chosenRow = row }
|
|
262
748
|
}
|
|
263
749
|
return chosenRow
|
|
264
750
|
}
|
|
265
751
|
const pool = matches.length ? matches : rows
|
|
266
752
|
// 第一轮:内容匹配里有没有文字块 —— 有就直接在那些里挑(公式行的 matches 为空,走第二轮)。
|
|
753
|
+
// 这一轮用**同栏**闸门而不是距离:图注类文字离图框可以很远,但它与图框横向重合。
|
|
267
754
|
const textMatched = matches.filter(row => !STRUCTURAL_KINDS.has(styles.get(row.id)))
|
|
268
|
-
let chosen = textMatched.length > 0 ? pick(textMatched) : null
|
|
755
|
+
let chosen = textMatched.length > 0 ? pick(textMatched, { column: true }) : null
|
|
269
756
|
// 第二轮:在**不与文字块重叠**的候选里挑。文字块永远在内,结构性块看它是否压着正文。
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
757
|
+
// 内容匹配过但不同栏(跨栏续接的段落)会在这一轮落到几何上真正罩着它的块。
|
|
758
|
+
if (chosen === null) chosen = pick(pool.filter(row => !STRUCTURAL_KINDS.has(styles.get(row.id)) || !overlapsText(row)), { column: matches.length > 0 })
|
|
759
|
+
// 第三轮:整页候选里挑,闸门口径同上一轮。
|
|
760
|
+
//
|
|
761
|
+
// 🔴 够不着时**宁可丢掉这一行**,不放开闸门。远处的词是页眉、水印、页码一类的版面噪声:
|
|
762
|
+
// 把它们硬塞进某个正文块,会让那个块的 DOM 里混进一批视觉上在别处的节点,选区照样跨栏
|
|
763
|
+
// (这也是回归用例「远处水印不归入正文」钉住的行为)。
|
|
764
|
+
if (chosen === null) chosen = pick(rows, { column: matches.length > 0 })
|
|
273
765
|
if (!chosen) continue
|
|
274
766
|
if (!grouped.has(chosen.id)) grouped.set(chosen.id, { id: chosen.id, words: [] })
|
|
275
767
|
for (const word of line.words) {
|
|
@@ -284,7 +776,7 @@ export function pdfPageGroups(page, blocks) {
|
|
|
284
776
|
})
|
|
285
777
|
}
|
|
286
778
|
}
|
|
287
|
-
|
|
779
|
+
const shaped = [...grouped.values()].filter(group => group.words.length > 0).map(group => {
|
|
288
780
|
// 先把词按物理行归堆,再给**每一行**算一个统一的行带。
|
|
289
781
|
//
|
|
290
782
|
// 🔴 行带的高度**不能**取这一行所有词框的 min/max:行内只要有一个比正文高得多的字形
|
|
@@ -329,6 +821,9 @@ export function pdfPageGroups(page, blocks) {
|
|
|
329
821
|
: word.bbox
|
|
330
822
|
return { text: (joined ? word.text.slice(0, -1) : word.text) + (sameLine && !joined ? ' ' : ''), bbox,
|
|
331
823
|
...(chars ? { chars } : {}),
|
|
824
|
+
// 断词连写:与该词前一个词之间不补任何分隔符。跨 DOM 段重拼正文时要用它
|
|
825
|
+
// (splitRuns 只按行接缝补空格,缺了这条会把 gen- 与 erated 拆成两个词)。
|
|
826
|
+
continuation: joined === true,
|
|
332
827
|
line: [word.line.top, word.line.bottom], gap: sameLine ? Math.max(0, next.bbox[0] - word.bbox[2]) : 0 }
|
|
333
828
|
})
|
|
334
829
|
const bbox = words.reduce((box, word) => [Math.min(box[0], word.bbox[0]), Math.min(box[1], word.line[0]), Math.max(box[2], word.bbox[2]), Math.max(box[3], word.line[1])], [Infinity, Infinity, -Infinity, -Infinity])
|
|
@@ -344,4 +839,101 @@ export function pdfPageGroups(page, blocks) {
|
|
|
344
839
|
}, '')
|
|
345
840
|
return { ...group, bbox, words, text }
|
|
346
841
|
})
|
|
842
|
+
// 🔴 **DOM 顺序必须是视觉阅读顺序**,不能照抄 MinerU 的数组顺序。
|
|
843
|
+
//
|
|
844
|
+
// 浏览器画原生选区只认 DOM 顺序,不看物理位置:两个视觉上相邻、DOM 上隔着一整个
|
|
845
|
+
// 右栏的块,用户从左栏一路往下拖就会把中间那些行一起涂蓝。实测第 4 页的跨栏续接
|
|
846
|
+
// 段落正是这种形状(bbox 在左栏底部,数组位置却在左栏其余段落之前)。
|
|
847
|
+
//
|
|
848
|
+
// 块的位置取它**第一行的阅读顺序位置**。为什么不逐行重排:块是标注的锚点单位
|
|
849
|
+
// (blockId 决定划线落到哪一段),一个块的各行必须留在同一个容器里;而归属修好之后
|
|
850
|
+
// 每个块的行基本已经连续,取首个位置就够准。
|
|
851
|
+
//
|
|
852
|
+
// 用**实际词框**算,不是 MinerU 给的原框:归属修正后两者已经不同,DOM 里真正的落点
|
|
853
|
+
// 由词框决定。
|
|
854
|
+
// 页边材料(页眉 / 页脚 / 脚注 / 页码 / 侧栏批注)单独排到整页正文之后,不参与正文的
|
|
855
|
+
// 阅读顺序。理由见 MARGIN_KINDS:夹在正文中间的脚注会被框选一并涂蓝。
|
|
856
|
+
const isMargin = id => MARGIN_KINDS.has(styles.get(id))
|
|
857
|
+
// 逐行铺开:同一块的同一条物理行归一行,并记下这一行**是不是页边材料**。
|
|
858
|
+
// 判据只有块类型一条 —— 竖排文字在上面的归属阶段就已经被丢掉了,走不到这里。
|
|
859
|
+
const physical = []
|
|
860
|
+
for (const group of shaped) {
|
|
861
|
+
const marginBlock = isMargin(group.id)
|
|
862
|
+
// 🔴 结构性块(图片 / 图表 / 表格 / 代码)里的字不是正文流:它们不构成版面网格,
|
|
863
|
+
// 拿它们估计"正文行宽的中位数"会把通栏判据整体带偏(见 freeIntervals 里的实测)。
|
|
864
|
+
const structuralBlock = STRUCTURAL_KINDS.has(styles.get(group.id))
|
|
865
|
+
for (const word of group.words) {
|
|
866
|
+
const top = word.line?.[0]
|
|
867
|
+
const last = physical.at(-1)
|
|
868
|
+
if (last !== undefined && last.groupId === group.id && last.top === top) {
|
|
869
|
+
last.words.push(word)
|
|
870
|
+
last.box[0] = Math.min(last.box[0], word.bbox[0])
|
|
871
|
+
last.box[2] = Math.max(last.box[2], word.bbox[2])
|
|
872
|
+
} else {
|
|
873
|
+
physical.push({ groupId: group.id, top, margin: marginBlock, structural: structuralBlock,
|
|
874
|
+
box: [word.bbox[0], word.line[0], word.bbox[2], word.line[1]], words: [word] })
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
// 正文按阅读顺序铺开;页边材料自己按 y 排(同一块内从上到下),整段接在正文之后。
|
|
879
|
+
return splitRuns(shaped, [
|
|
880
|
+
...lineReadingOrder(physical.filter(line => !line.margin)),
|
|
881
|
+
...lineReadingOrder(physical.filter(line => line.margin)),
|
|
882
|
+
])
|
|
347
883
|
}
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* 把「块」按阅读顺序重新打包成若干**连续的 DOM 段**。
|
|
888
|
+
*
|
|
889
|
+
* 🔴 这是整个问题的根:浏览器画原生选区只认 DOM 顺序。只要一个块的行在阅读顺序里不是
|
|
890
|
+
* 连续的,它的容器就必须同时罩住中间那些别的块 —— 用户拖选时被夹在中间的内容跟着涂蓝。
|
|
891
|
+
* 光把块排序修好只能保证**块与块之间**不串栏;块内部被拆散(跨栏续接的图注、被公式或
|
|
892
|
+
* 图表隔断的段落)照样会串。
|
|
893
|
+
*
|
|
894
|
+
* 做法:按阅读顺序铺开所有物理行,同一块的行**连续出现**就归一段,断开就另起一段。
|
|
895
|
+
* 于是 DOM 里任何一段连续节点都对应阅读顺序里连续的一段,也就是视觉上连续的一片。
|
|
896
|
+
* 这是排版无关的:单栏、双栏、三栏、横排表格、图文混排一视同仁,因为它只做一件事 ——
|
|
897
|
+
* 让 DOM 顺序就是阅读顺序。
|
|
898
|
+
*
|
|
899
|
+
* 段共用同一个 id(就是 blockId,标注的锚点单位不变),用 part 区分先后。
|
|
900
|
+
* 一个块没被拆散时只产出一段,part 恒为 0,字节形状与拆分前一致。
|
|
901
|
+
*
|
|
902
|
+
* @param {object[]} shaped 每块一组的词
|
|
903
|
+
* @param {object[]} ordered 所有物理行,按视觉阅读顺序
|
|
904
|
+
* @returns {object[]} 按 DOM 顺序排列的段落
|
|
905
|
+
*/
|
|
906
|
+
function splitRuns(shaped, ordered) {
|
|
907
|
+
const runs = []
|
|
908
|
+
for (const line of ordered) {
|
|
909
|
+
const last = runs.at(-1)
|
|
910
|
+
if (last !== undefined && last.id === line.groupId) last.words.push(...line.words)
|
|
911
|
+
else runs.push({ id: line.groupId, words: [...line.words] })
|
|
912
|
+
}
|
|
913
|
+
const sources = new Map(shaped.map(group => [group.id, group]))
|
|
914
|
+
const seen = new Map()
|
|
915
|
+
return runs.filter(run => run.words.length > 0).map(run => {
|
|
916
|
+
const part = seen.get(run.id) ?? 0
|
|
917
|
+
seen.set(run.id, part + 1)
|
|
918
|
+
const source = sources.get(run.id) ?? {}
|
|
919
|
+
const words = run.words
|
|
920
|
+
const bbox = words.reduce((box, word) => [
|
|
921
|
+
Math.min(box[0], word.bbox[0]), Math.min(box[1], word.line[0]),
|
|
922
|
+
Math.max(box[2], word.bbox[2]), Math.max(box[3], word.line[1]),
|
|
923
|
+
], [Infinity, Infinity, -Infinity, -Infinity])
|
|
924
|
+
const text = words.reduce((acc, word, i) => {
|
|
925
|
+
if (i === 0) return word.text
|
|
926
|
+
const previous = words[i - 1]
|
|
927
|
+
// 断词连写(gen- / erated):前一个词带 continuation,两段直接接,不补空格。
|
|
928
|
+
if (previous.continuation === true) return acc + word.text
|
|
929
|
+
const sameLine = previous.line[0] === word.line[0] && previous.line[1] === word.line[1]
|
|
930
|
+
return acc + (sameLine || acc.endsWith(SPACE) ? EMPTY : SPACE) + word.text
|
|
931
|
+
}, String())
|
|
932
|
+
// fullText 是**整块**正文(所有 part 合起来):划线校验、高亮定位、对齐都对整块做,
|
|
933
|
+
// 不能只看某个 part 的那一截。shaped 里那份 text 本来就是按整块拼好的,直接沿用。
|
|
934
|
+
return { ...source, id: run.id, part, words, bbox, text, fullText: source.text ?? text }
|
|
935
|
+
})
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|