@li-canghai/dsh-paper 0.8.3 → 0.8.7

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.
@@ -0,0 +1,93 @@
1
+ Google Inc.
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ http://scripts.sil.org/OFL
6
+
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION & CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.
package/lib/gloss.js CHANGED
@@ -1031,14 +1031,29 @@ export async function translateSelection(input = {}, deps = {}) {
1031
1031
  // 🔴 但**不能一律要求「只抄第一句」**(2026-11-20 实测,用户截图):选区跨三句时模型照办只抄
1032
1032
  // 一句,另一列只亮起开头 44 字(English 298 字 → 中文 44 字,而对应内容有 101 字)。
1033
1033
  // 选区的句数由用户决定,提示词不能替它拍板。现在两头都点明:该抄的整段,抄到内容结束为止。
1034
+ //
1035
+ // 🔴 **「答案只能来自 Paragraph」必须单独成句**(2026-11-27 实测 Hy-MT3)。
1036
+ //
1037
+ // 原措辞是 `Output the English text from the paragraph that corresponds to the Chinese.`,
1038
+ // 在 tencent/Hy-MT3-7B-GGUF 上两个方向都**原样回显输入**:英文选区回英文、中文选区回中文。
1039
+ // 逐条 A/B 之后根因不是「没说清语言」,而是这一句式被读成了「把选区转写一遍」——
1040
+ // 只要把约束换成 `Your answer must be copied from the Paragraph, never from the target.`
1041
+ // 单独占一行,再用 `The Paragraph is in X and the target is its Y translation.` 交代两侧语言,
1042
+ // 整句、多句、表格行三个方向全部命中(4/4);仅把语言名换个位置(`The Paragraph is Chinese
1043
+ // and the Target is English. Copy the Chinese portion…`)仍会回显,可见起作用的是这一句约束。
1044
+ //
1045
+ // ⚠️ 剩下的已知短板:选区是**单个词**时(`注意力`)Hy-MT3 仍会把它当翻译任务而不是定位任务。
1046
+ // 结构档 {@link structuralSpan} 在能给出跨度时并不依赖这一档的回复,所以影响面只剩
1047
+ // 「结构档给不出跨度、且用户只划了一个词」的降级路径。
1034
1048
  system: '',
1035
- user: 'Output the ' + otherLanguageName(counterpart, counterpartKind) + ' text from the paragraph that corresponds to the '
1036
- + quoteLanguageName(counterpart, counterpartKind) + '. Copy it exactly.\n\n'
1037
- + 'Copy the WHOLE matching span: the ' + quoteLanguageName(counterpart, counterpartKind)
1038
- + ' text may cover several sentences, so copy all of them and stop where the matching content ends.'
1039
- + ' Do not stop after the first sentence, and do not continue into the text after it.\n\n'
1049
+ user: 'Do not translate. Copy verbatim.\n\n'
1050
+ + 'Your answer must be copied from the Paragraph, never from the target.\n'
1051
+ + 'The Paragraph is in ' + otherLanguageName(counterpart, counterpartKind) + ' and the target is its '
1052
+ + quoteLanguageName(counterpart, counterpartKind) + ' translation.\n'
1053
+ + 'Output the ' + otherLanguageName(counterpart, counterpartKind) + ' substring of the Paragraph that the target translates — '
1054
+ + 'the whole span, which may cover several sentences. Copy it character for character.\n\n'
1040
1055
  + 'Paragraph:\n' + counterpart + '\n\n'
1041
- + quoteLanguageName(counterpart, counterpartKind) + ':\n' + quote,
1056
+ + quoteLanguageName(counterpart, counterpartKind) + ' target:\n' + quote,
1042
1057
  } : buildSelectionPrompt({
1043
1058
  quote,
1044
1059
  context: context.slice(0, GLOSS_CONTEXT_LIMIT),
package/lib/index.js CHANGED
@@ -3,6 +3,7 @@ import { randomUUID } from 'node:crypto'
3
3
  import { mkdir, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises'
4
4
  import { homedir } from 'node:os'
5
5
  import { basename, dirname, extname, isAbsolute, join, resolve } from 'node:path'
6
+ import { fileURLToPath } from 'node:url'
6
7
  import { visibleHeadingIndexes } from './blocks.js'
7
8
  import { normalizeBlockId } from './slicing.js'
8
9
  import { buildContextMessage, deliveredPaperState, isProducerSource } from './context.js'
@@ -562,6 +563,23 @@ export function apply(ctx, config = {}) {
562
563
  const document = hydrateDocument(raw)
563
564
  return { markdownPath: document.markdownPath, pdfPath: document.pdfPath }
564
565
  }
566
+ /**
567
+ * 随包自带的中文字体分片目录(D207)。
568
+ *
569
+ * `lib/fonts/` 与 `lib/index.js` 是**同一个发布包里的兄弟目录**,所以用
570
+ * `import.meta.url` 反推而不是 `process.cwd()` —— 后者是宿主进程的工作目录,
571
+ * 与插件包在哪毫无关系。桌面端把插件装在 `~/.dsh/profiles/<面>/local-plugins/<名>/`,
572
+ * 开发目录则指向仓库里那份,两种部署都靠这条相对关系找到字体。
573
+ */
574
+ function bundledFontDir() {
575
+ try {
576
+ return fileURLToPath(new URL('./fonts', import.meta.url))
577
+ } catch {
578
+ // 拿不到自身 URL 的打包形态(罕见):返回空串,路由回 503。
579
+ // 字体缺席只会让正文落到系统字体,绝不阻塞阅读。
580
+ return ''
581
+ }
582
+ }
565
583
  // 与 tool 那一路同样**惰性构造**:apply 期 ctx.get('jobs') 之类还没注册,构造时做断言会污染日志。
566
584
  let assetRoutesInstance
567
585
  function assetRoutesFor() {
@@ -570,6 +588,7 @@ export function apply(ctx, config = {}) {
570
588
  renderPageImages,
571
589
  cacheDir: pageImageCacheDir,
572
590
  command: config.mineruCommand,
591
+ fontDir: bundledFontDir(),
573
592
  })
574
593
  return assetRoutesInstance
575
594
  }
@@ -3975,7 +3994,7 @@ function hasMeaningfulValue(value) {
3975
3994
  : path === '/marks' ? request => serializeMarks(() => dispatch(request)) : dispatch,
3976
3995
  })
3977
3996
  // 阅读器资源(字节响应,不经 JSON dispatch)。
3978
- for (const [path, name] of [['/analysis-asset', 'analysis-asset'], ['/page-image', 'page-image']]) {
3997
+ for (const [path, name] of [['/analysis-asset', 'analysis-asset'], ['/page-image', 'page-image'], ['/font-file', 'font-file']]) {
3979
3998
  connectionCtx.connection.registerFetchRoute(ctx, {
3980
3999
  path: API_BASE + path,
3981
4000
  methods: ['GET'],
package/lib/pdf-page.js CHANGED
@@ -303,7 +303,18 @@ const MAX_MATCHED_DISTANCE = 200
303
303
  * · {@link GUTTER_TOLERANCE} —— 沟里被别的行压住的比例上限。通栏标题、满宽图注会横穿
304
304
  * 栏间沟;只按「完全为空」判,带通栏标题的双栏页就再也找不到栏了。
305
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
+ */
306
316
  const COLUMN_GUTTER_UNITS = 1.6
317
+ const RELAXED_GUTTER_UNITS = 0.9
307
318
  const ROW_GUTTER_UNITS = 0.55
308
319
  const GUTTER_TOLERANCE = 0.12
309
320
 
@@ -339,15 +350,23 @@ function lineReadingOrder(lines) {
339
350
  if (lines.length <= 1) return lines
340
351
  const heights = lines.map(line => line.box[3] - line.box[1]).filter(height => height > 0).sort((a, b) => a - b)
341
352
  const unit = heights.length > 0 ? heights[heights.length >> 1] : 0
342
- return unit > 0 ? orderRegion(lines, unit) : [...lines].sort(readingTop)
353
+ return unit > 0 ? orderRegion(lines, unit, true) : [...lines].sort(readingTop)
343
354
  }
344
355
 
345
356
  /** 一个局部版面的递归切分:能切栏就切栏,否则切段,否则按 y。 */
346
- function orderRegion(lines, unit) {
357
+ function orderRegion(lines, unit, allowRelaxed = false) {
347
358
  if (lines.length <= 1) return lines
348
- const columns = splitAxis(lines, unit, 0, COLUMN_GUTTER_UNITS)
349
- if (columns !== null) return columnOrder(lines, columns, unit)
350
- const rows = splitAxis(lines, unit, 1, ROW_GUTTER_UNITS)
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)
351
370
  if (rows !== null) return sliceByCuts(lines, 1, rows.map(gap => (gap.from + gap.to) / 2)).flatMap(row => orderRegion(row, unit))
352
371
  return [...lines].sort(readingTop)
353
372
  }
@@ -382,7 +401,7 @@ function sliceByCuts(lines, axis, cuts) {
382
401
  * @param {number} unit 行高
383
402
  * @returns {object[]} 按阅读顺序
384
403
  */
385
- function columnOrder(lines, gaps, unit) {
404
+ function columnOrder(lines, gaps, unit, relaxed = false) {
386
405
  // 🔴 **跨栏**的意思是"横穿整道沟",不是"碰到沟"。只按切割中线判会把**探进沟里**但没
387
406
  // 穿过去的那一行也算成跨栏 —— 实测夹具里左栏段落右沿到 420、沟是 300–514,它只是
388
407
  // 探进沟里 120,却被当成跨栏行抽走,于是整页顺序错位。判据必须是两端都在沟外。
@@ -413,10 +432,33 @@ function columnOrder(lines, gaps, unit) {
413
432
  * 单栏页上由此得到的"沟"两侧必有一侧没有整行(正文横跨整页),于是被这条判据挡掉,
414
433
  * 不会把单栏页切成两半。
415
434
  */
416
- function splitAxis(lines, unit, axis, units) {
417
- const gaps = freeIntervals(lines, axis, unit)
435
+ function splitAxis(lines, unit, axis, units, relaxed = false) {
436
+ const gaps = freeIntervals(lines, axis, unit, relaxed)
418
437
  .filter(([from, to]) => to - from >= unit * units)
419
- .filter(([from, to]) => lines.some(line => line.box[axis + 2] <= from) && lines.some(line => line.box[axis] >= to))
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
+ })
420
462
  // 🔴 每层只切**一道**最宽的沟。递归会在每一栏内部继续找下一道,所以多栏页面照样
421
463
  // 切得出来;但同一层切好几刀时,一道被落单的字劈开的沟会被当成两道,中间凭空多出
422
464
  // 一栏(只装那一个字),两侧正文反而落到更远的栏里。
@@ -444,7 +486,7 @@ function splitAxis(lines, unit, axis, units) {
444
486
  * @param {number} unit 行高
445
487
  * @returns {Array<[number, number]>} 按位置升序的空白区间
446
488
  */
447
- function freeIntervals(lines, axis, unit) {
489
+ function freeIntervals(lines, axis, unit, relaxed = false) {
448
490
  const across = axis === 0 ? 1 : 0
449
491
  const from = Math.min(...lines.map(line => line.box[axis]))
450
492
  const to = Math.max(...lines.map(line => line.box[axis + 2]))
@@ -503,6 +545,25 @@ function freeIntervals(lines, axis, unit) {
503
545
  const widths = textWidths.length > 0 ? textWidths : widthsOf(() => true)
504
546
  const typical = widths.length > 0 ? widths[widths.length >> 1] : 0
505
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
+ }
506
567
  for (const line of lines) {
507
568
  const start = line.box[axis]
508
569
  const end = line.box[axis + 2]
@@ -511,13 +572,34 @@ function freeIntervals(lines, axis, unit) {
511
572
  bump(start, weight)
512
573
  bump(end, -weight)
513
574
  }
514
- const positions = [...deltas.keys()].sort((a, b) => a - b)
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)
515
594
  const raw = []
516
595
  let depth = 0
596
+ let relaxedDepth = 0
517
597
  for (let i = 0; i < positions.length - 1; i += 1) {
518
- depth += deltas.get(positions[i])
598
+ depth += deltas.get(positions[i]) ?? 0
599
+ if (relaxed) relaxedDepth += relaxedDeltas.get(positions[i]) ?? 0
519
600
  // 容差而不是 0:depth 是浮点累加量,见上面 epsilon 的说明。
520
- if (depth > epsilon) continue
601
+ // 兜底档看**排除居中元素之后**的覆盖:那才是真正的正文遮挡。
602
+ if ((relaxed ? relaxedDepth : depth) > epsilon) continue
521
603
  const start = positions[i]
522
604
  const end = positions[i + 1]
523
605
  if (end > start) raw.push([start, end])