7.25.0 splits letter-spaced CJK runs of two to four characters into one word per character (`股本重組` -> `股 本 重 組`): `infer_unspaced_gap_profile()` needs four gaps before it sees tracking

Aperta
#483 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@jjohnnystwsss ci sta già lavorando.

Dal 22/11/2026.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
backend, data

Direzione di ricerca

Start with cells.h and infer_unspaced_gap_profile(), added in 7.25.0, then inspect the existing CJK gap tests in tests/test_unit_word_gap_unspaced_runs.py. Reproduce the reported two to four character cases and compare word and textline cells with the earlier release behavior. Done means letter spaced CJK runs remain whole while the #383 spaced word gap fixes still pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

What happens

#383 is closed by #437 (7.25.0), and the rework does what it set out to do. On the 99 files of #383 the lost word spaces go from 1,142 seams in 7.22.1 to 328 in 7.26.0 (same method: 7.20.0 and pdfium have a space, the textline cell has none), and 52 of the 328 are real word gaps; the rest are raised or lowered glyphs of inline mathematics and tracked Latin text that pdfium splits and 7.26.0 rightly joins. The lines quoted in #383 (2 Related Work, from both, system must, ModelScope Model) are right from 7.25.0 on, and justified Chinese paragraphs that 7.22.1 returned one word per character are whole again. This report is about a residue of that fix.

Since 7.25.0 a short run of CJK characters that is set with letter spacing comes out one word per character, in the word cells and in the textline cell. It has two shapes:

  • a run of two to four characters: section headings, table labels, defined terms, and the four-character lines of a narrow justified table column. This is most of it;
  • a longer line in which the Han characters carry the letter spacing while digits, Latin letters or punctuation are set tight.

All examples are from two public PDFs. 7.22.1 to 7.24.2 and pdfium's text layer agree on every row; 7.20.0 agrees on the first seven (it cuts the last two lines into different cells, without spaces between the characters).

PDF page 7.22.1 to 7.24.2 7.25.0, 7.26.0
circular 8 緒言 緒 言
circular 9 股本重組 股 本 重 組
circular 4 「董事」 「董 事」
circular 10 可行日期 可 行 日 期
circular 15 責任聲明 責 任 聲 明
manual 14 前立即採 前 立 即 採
manual 16 臨床診斷 臨 床 診 斷
circular 1 (股份代號:897) (股 份 代 號:897)
circular 9 緊隨股本重組後,本公司之法定股本為600,000,000.00港元,分為60,000,000,000 緊 隨 股 本 重 組 後,本 公 司 之 法 定 股 本 為600,000,000.00港 元,分 為60,000,000,000

Downstream, a word-segmenting Chinese tokenizer makes one token of 香港 and two single-character tokens of 香 港, so a keyword search for the word no longer matches the cell.

The two PDFs (both URLs served these bytes on 2026-11-19):

Reproduce

docling-parse only, default DecodeConfig and ContentConfig:

import sys
from importlib.metadata import version

from docling_parse.pdf_parser import DoclingPdfParser

TARGETS = {
    # circular: headings, defined terms, table labels
    "緒言", "股本重組", "警告", "推薦意見", "責任聲明", "備查文件", "「董事」", "「香港」", "可行日期", "現有股份", "新股份",
    # manual: four-character lines of a narrow justified table column
    "前立即採", "臨床診斷", "之發燒期",
}

print("docling-parse", version("docling-parse"))
doc = DoclingPdfParser(loglevel="fatal").load(path_or_stream=sys.argv[1])
for page_no in [int(arg) for arg in sys.argv[2:]] or range(1, doc.number_of_pages() + 1):
    for cell in doc.get_page(page_no).textline_cells:
        if "".join(cell.text.split()) in TARGETS:
            print(f"p{page_no}: {cell.text!r}")
doc.unload()

python repro.py 20160218_cw0897_circular.pdf

docling-parse 7.24.2
p4: '「董事」'
p4: '「香港」'
p8: '緒言'
p9: '股本重組'
p10: '可行日期'
p10: '現有股份'
p10: '新股份'
p14: '警告'
p14: '推薦意見'
p15: '責任聲明'
p15: '備查文件'
docling-parse 7.26.0
p4: '「董 事」'
p4: '「香 港」'
p8: '緒 言'
p9: '股 本 重 組'
p10: '可 行 日 期'
p10: '現 有 股 份'
p10: '新 股 份'
p14: '警 告'
p14: '推 薦 意 見'
p15: '責 任 聲 明'
p15: '備 查 文 件'

python repro.py 24b07a0d-65a9-4724-b3fd-f04eeb9ff458.pdf 14 16

docling-parse 7.24.2
p14: '前立即採'
p16: '臨床診斷'
p16: '之發燒期'
docling-parse 7.26.0
p14: '前 立 即 採'
p16: '臨 床 診 斷'
p16: '之 發 燒 期'

Expected: the 7.24.2 output. pdfium (pypdfium2 5.15.2, get_textpage().get_text_bounded() on the cell's box) reads 緒言, 股本重組, 可行日期, 責任聲明, 前立即採, 臨床診斷 and the other targets without a space.

In the whole circular 59 textline cells gain a space between two Han characters (131 spaces, on 15 of the 18 pages); in the manual 151 cells (393 spaces, on 40 of the 130 pages).

Without a download

The same thing on a page built in memory, with the non-embedded GBK font of tests/test_unit_word_gap_unspaced_runs.py. Each line is one text object, <spacing> Tc (<characters>) Tj at 12 pt:

from importlib.metadata import version
from io import BytesIO

from docling_parse.pdf_parser import DoclingPdfParser

TEXT, SIZE, SPACINGS_EM = "加勒比海地圖", 12, [0.10, 0.15, 0.20, 0.25, 0.30]

# one text object per line: `<spacing> Tc (<GBK bytes>) Tj`, so every gap of the line is <spacing> / 12 em
lines, y = [], 20
for count in range(2, 7):
    for em in SPACINGS_EM:
        gbk = TEXT[:count].encode("gbk").decode("latin-1")
        lines.append((count, y, f"BT /F1 {SIZE} Tf 1 0 0 1 20 {y} Tm {em * SIZE:.2f} Tc ({gbk}) Tj ET"))
        y += 20
content = "\n".join(line[2] for line in lines).encode("latin-1")
objects = [
    b"<< /Type /Catalog /Pages 2 0 R >>",
    b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
    b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 %d] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>" % y,
    b"<< /Length %d >>\nstream\n" % len(content) + content + b"\nendstream",
    # the non-embedded GBK font of tests/test_unit_word_gap_unspaced_runs.py
    b"<< /Type /Font /Subtype /Type0 /BaseFont /SimSun /Encoding /GBK-EUC-H /DescendantFonts [6 0 R] >>",
    b"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /SimSun /CIDSystemInfo << /Registry (Adobe) /Ordering (GB1) "
    b"/Supplement 2 >> /FontDescriptor 7 0 R /DW 1000 >>",
    b"<< /Type /FontDescriptor /FontName /SimSun /Flags 4 /FontBBox [0 0 1000 1000] /ItalicAngle 0 /Ascent 859 "
    b"/Descent -141 /CapHeight 674 /StemV 91 >>",
]
pdf, offsets = b"%PDF-1.7\n", []
for number, obj in enumerate(objects, start=1):
    offsets.append(len(pdf))
    pdf += b"%d 0 obj\n" % number + obj + b"\nendobj\n"
xref = len(pdf)
pdf += b"xref\n0 %d\n0000000000 65535 f \n" % (len(objects) + 1)
pdf += b"".join(b"%010d 00000 n \n" % offset for offset in offsets)
pdf += b"trailer\n<< /Size %d /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n" % (len(objects) + 1, xref)

print("docling-parse", version("docling-parse"))
page = DoclingPdfParser(loglevel="fatal").load(path_or_stream=BytesIO(pdf)).get_page(1)
print("letter spacing:", " | ".join(f"{em:.2f} em" for em in SPACINGS_EM))
for count in range(2, 7):
    cells = [
        " / ".join(cell.text for cell in page.textline_cells if abs(cell.rect.r_y0 - y) < 3)
        for line_count, y, _ in lines
        if line_count == count
    ]
    print(f"{count} characters:", " | ".join(cells))
docling-parse 7.24.2
letter spacing: 0.10 em | 0.15 em | 0.20 em | 0.25 em | 0.30 em
2 characters: 加勒 | 加勒 | 加勒 | 加勒 | 加 勒
3 characters: 加勒比 | 加勒比 | 加勒比 | 加勒比 | 加 勒 比
4 characters: 加勒比海 | 加勒比海 | 加勒比海 | 加勒比海 | 加 勒 比 海
5 characters: 加勒比海地 | 加勒比海地 | 加勒比海地 | 加勒比海地 | 加 勒 比 海 地
6 characters: 加勒比海地圖 | 加勒比海地圖 | 加勒比海地圖 | 加勒比海地圖 | 加 勒 比 海 地 圖
docling-parse 7.26.0
letter spacing: 0.10 em | 0.15 em | 0.20 em | 0.25 em | 0.30 em
2 characters: 加勒 | 加 勒 | 加 勒 | 加 勒 | 加 勒
3 characters: 加勒比 | 加 勒 比 | 加 勒 比 | 加 勒 比 | 加 勒 比
4 characters: 加勒比海 | 加 勒 比 海 | 加 勒 比 海 | 加 勒 比 海 | 加 勒 比 海
5 characters: 加勒比海地 | 加勒比海地 | 加勒比海地 | 加勒比海地 | 加勒比海地
6 characters: 加勒比海地圖 | 加勒比海地圖 | 加勒比海地圖 | 加勒比海地圖 | 加勒比海地圖

7.20.0 returns every line whole. So 7.25.0 repairs the runs of five and more characters at 0.30 em, and runs of two to four characters now split from 0.15 em instead of from 0.30 em.

The existing test has the same edge: test_justified_cjk_text_contracts_to_one_cell passes with its six characters, and with 中文, 中文排 or 中文排版 and the same adjustments ([300] * (n - 1), or 150) 7.26.0 returns one word per character.

First affected version

7.25.0. I ran both scripts on every PyPI release from 7.20.0 to 7.26.0 (7.20.0, 7.21.0, 7.22.0 to 7.22.4, 7.23.0, 7.23.1, 7.24.0 to 7.24.2, 7.25.0, 7.26.0; Python 3.12):

  • the circular: 7.20.0 to 7.24.2 print the eleven cells whole, 7.25.0 and 7.26.0 print all eleven split;
  • the synthetic page: 7.21.0 to 7.24.2 print the 7.24.2 grid above, 7.25.0 and 7.26.0 the 7.26.0 grid.

7.27.0 (released 2026-11-19) is the same: the synthetic page prints the 7.26.0 grid. I have not run the other measurements of this report on it.

On the 14 affected files of my corpus that I dumped with every release from 7.22.1 on (232 pages), the word and textline cells of 7.22.1 to 7.24.2 are identical, text and boxes; 7.25.0 changes 116 of the 232 pages, and the cells that gain spaces are the same in 7.25.0 and 7.26.0. 7.25.0 is also the release in which the #383 lines come right, and its release notes carry #437; infer_unspaced_gap_profile() is in cells.h at v7.25.0 and not at v7.24.2. I did not bisect commits.

How much

7.26.0 against 7.24.2 (the same numbers come out against 7.22.1, and for 7.25.0), default DecodeConfig; shapes and bitmaps are switched off in the ContentConfig to make the run affordable, which leaves the word and textline cells identical on the 218 pages where I compared the two settings. A cell counts when both versions return it (same characters, same place) and 7.26.0's text has a space between two Han characters where 7.24.2's has none; each new space is then checked against the text pdfium returns for the cell's box. A cell whose extent changed between the versions is not compared, so these are lower bounds.

the 100 PDFs of #383, first 60 pages (2,035 pages) 285 Traditional Chinese PDFs, first 20 pages (1,743 pages)
cells that gain a space between two Han characters 111, on 37 pages in 5 files 135, on 37 pages in 13 files
new spaces 298 283
pdfium has no space there 282 (106 cells, 35 pages, 4 files) 202 (99 cells, 26 pages, 9 files)
pdfium has a space there too 16 36
cell not found in pdfium's text (one scan with an OCR text layer) 0 45
cells pdfium contradicts that are two to four characters long 95 of 106 83 of 99
spaces between Han characters that 7.26.0 removes, same comparison 768, in 98 cells 1,494, in 101 cells

Pages with a cell that pdfium contradicts are 1.7% and 1.5% of the pages. The second corpus is a sample of public-sector documents, newsletters, company filings and reports; 101 of the 111 cells of the first are an earlier edition of the manual above. The last row is the other side of the same change: in the cells both versions return, 7.26.0 removes about four times as many spaces between Han characters as it adds.

Geometry of an affected seam

Character cells are the same in 7.24.2 and 7.26.0, text and boxes, on all 18 pages of the circular and on the seven pages of the manual I compared (13, 14, 16 to 20); only the contraction differs. There is no space glyph in any of these runs. The gap is the distance between the two glyph boxes, the font size is the size in page units (pdfium's font size times the scale of the text matrix), and the operator was read from the content stream with pikepdf.

PDF, page cell split seams gap font size in em glyphs in the run what makes the gap
circular p8 緒言 緒 | 言 2.01 pt 11.5 pt 0.175 2 0.175 Tc under /G1 1 Tf, 11.5 0 0 11.5 … Tm
circular p9 股本重組 all three 2.01 pt 11.5 pt 0.175 4 the same; one Tj with the four characters
circular p10 可行日期 all three 1.56 pt 8.93 pt 0.175 4 0.175 Tc
circular p4 「董事」 董 | 事 1.84 pt 10.5 pt 0.175 4; the brackets sit 0.26 pt (0.025 em) away 0.175 Tc, taken back next to the brackets by a TJ adjustment of 150
manual p14 前立即採 all three 2.64 pt 11.04 pt 0.239 4 2.64 Tc under /F2 11.04 Tf; one string of four characters
manual p16 臨床診斷 all three 1.68 pt 12 pt 0.140 4 1.68 Tc under /F2 12 Tf
circular p1 (股份代號:897) the three between Han characters 1.84 pt 10.5 pt 0.175 10; the six other gaps are 0.26 pt (0.025 em) 0.175 Tc on the Han string, 0.025 Tc on the digits
circular p9 緊隨股本重組後,…60,000,000,000 the sixteen between Han characters 1.52 pt 10.5 pt 0.145 50; thirty gaps of 0.025 em, three of 0.075 em Tc, 0.145 on the Han strings

Next to the other seams between two Han characters on the same page (page 9 of the circular, 431 seams inside textline cells):

gap where seams 7.24.2 7.26.0 pdfium
0.170 to 0.178 em body lines, no space glyph in the run 134 joined joined no space
0.163 to 0.191 em body lines whose run carries a space glyph 129 joined joined no space
0.183 to 0.375 em justified body lines 124 split joined no space at 85, a generated space at 39
0.175 em 股本重組, 併股份; 5 joined split no space
0.145 to 0.175 em two lines with digits (緊隨股本重組後,…, 本通函第SGM-1至SGM-3頁內。) 20 joined split no space
0.19 em one line with digits 17 split split no space
1.05, 1.07 em the honorific space before 閣下, a space glyph 2 split split a space

The same 0.175 em is joined 134 times in the long lines of the page and split in the heading. The third row is the repair of 7.25.0; it also shows that pdfium is not a clean reference in the other direction, since it puts generated spaces into wider letter-spaced lines.

pdfium at the affected seams themselves (its char stream between the two glyphs): no space at 127 of the 131 in the circular (the four others are one line on page 4, 買賣(股份代號:897)) and at 391 of the 393 in the manual.

docling-parse-7.26.0-cjk-letter-spaced-runs-seams.csv (attached) has one row per such seam of the two PDFs: file, page (1-based), the textline cell in 7.24.2 and in 7.26.0, the two characters, the gap in pt and in em, the font size, the number of glyphs in the run, whether the run carries a space glyph, the Tc in force in em where I located the operator, and what pdfium has. 520 rows; the two signature lines of the circular are left out. In seven rows the text is oblique (頁次 on page 2: 10.5 0 2.4241 10.5 … Tm, 0.175 Tc), the axis-aligned boxes overlap and the measured gap is negative.

Mechanism (src/parse/page_item_sanitators/cells.h at v7.26.0)

For a run without a space glyph contract() decides

starts_word =
  em_gap(*previous_visible, cell) - profile.tracking >
  profile.word_gap;

and infer_unspaced_gap_profile() starts from tracking = 0.0, word_gap = 0.13 and returns that when the run is short:

if(gaps.size() < 4)
  {
    return profile;
  }
...
if(2 * cjk_seams > seams)
  {
    profile.tracking = std::min(0.5, median_of(gaps));
    return profile;
  }

Replaying this function and the line above on the char cells of each textline cell (em as in the table, since the Python cells do not expose nominal_text_height) gives the word boundaries 7.26.0 returns at 7,142 of 7,142 seams between Han characters on the 40 affected pages of the manual and at 5,287 of 5,302 in the circular (runs without a space glyph; seven of the fifteen misses are the oblique seams, I did not chase the other eight). The lines above come out as follows:

cell gaps of the run, em branch taken tracking threshold splits
股本重組 0.175 × 3 fewer than four gaps 0 0.13 3
「董事」 0.025, 0.175, 0.025 fewer than four gaps 0 0.13 1
前立即採 0.239 × 3 fewer than four gaps 0 0.13 3
臨床診斷 0.140 × 3 fewer than four gaps 0 0.13 3
(股份代號:897) 0.025 × 6, 0.175 × 3 CJK majority (7 of 9 seams), median 0.025 0.13 3 (0.175 − 0.025 = 0.15)
緊隨股本重組後,…60,000,000,000 0.025 × 30, 0.075 × 3, 0.145 × 16 not a CJK majority (23 of 49 seams); the 20th-percentile gap, 0.025, is under the 0.03 floor; the tight-line rule gives 0.70 × 0.145 0 0.10 16

So there are three ways to the same result:

  1. One to three gaps. The run gets no tracking, and 0.13 em is applied to the letter spacing itself. A two-character heading has one gap; a four-character line has three.
  2. A CJK-majority run whose tight seams outnumber the letter-spaced ones. The median is taken over all seams, and the seams next to punctuation are tight, so the tracking comes out at 0.025 em.
  3. A line that is not CJK-majority. The 20th percentile sits on the tight digit seams, no tracking is recognised, and the Han seams are then read as the line's word gaps, which lowers the threshold further.

7.22.1 to 7.24.2 judged these runs at 0.25 of the neighbouring advances (infer_word_gap() returned 0.25 for a single gap, for uniform gaps, and when the widest gap was under 0.18), which is 0.25 em between full-width glyphs. That is the 0.30 em column of the synthetic grid, and why it is mostly letter spacing of 0.14 to 0.25 em that newly splits.

What distinguishes these seams

Measured on 15 files of the two corpora: the 12 that have newly split seams (every file with a cell that pdfium contradicts, bar one with a single such cell) and three whose letter-spaced runs 7.24.2 split already. 546 seams between Han characters are joined in 7.24.2 and split in 7.26.0. 399 of them are in runs of two to four glyphs (median gap 0.16 em, largest 0.31 em; pdfium has no space at 395), and 147 in longer runs (45 of those in the scan with an OCR text layer, where the glyph positions are irregular).

The gap is the character spacing of the text state. At 461 of the 546 seams the gap equals the Tc in force for that string (within 0.012 em of Tc divided by the Tf size); in the two public PDFs at 511 of 524. Tc is added after every glyph of the string, so it is letter spacing by construction, at any run length. Over all the seams between Han characters that 7.26.0 splits on the 18 pages of the circular and the 40 affected pages of the manual (the 524 new ones and the 351 that 7.24.2 split as well):

pdfium: no space pdfium: a space
gap equal to the Tc in force 625 7
anything else (a TJ adjustment, a new text position, or an operator I did not locate) 12 231

On the 15 files the same table reads 721 and 54 in the first row, 26 and 786 in the second (53 more seams could not be matched in pdfium).

Two things this suggests. Neither has been run against your ground truth.

  • The operator. Taking the Tc of the text state out of the gap before the 0.13 em test would join the 461 seams above, in all three ways, without inferring anything from the run. It would not cover a producer that writes tracking as TJ adjustments; one Quartz-made file in my corpus does.
  • The run length. Letting a CJK-majority run with one to three gaps take its median gap as tracking (at most 0.5 em), as a longer run does, joins 394 of the 399 short-run seams when replayed on the char cells. It also joins 119 wider seams of such runs (median 0.5 em, up to 0.60 em) that 7.24.2 split as well, 34 of which pdfium separates with a space, and it does nothing for the two other ways. For a two-character run it amounts to a higher threshold for a single seam between two Han characters.

Whatever shape a fix takes, 股本重組 (three gaps), 緒言 (one gap) and (股份代號:897) (letter-spaced Han between tight punctuation and digits) would be worth having next to the six-character test.

Environment

  • docling-parse 7.25.0 and 7.26.0 (PyPI wheels), compared with the 7.20.0 to 7.24.2 wheels
  • Python 3.12.16, Linux x86_64
  • pypdfium2 5.15.2 as the independent text-layer reference, pikepdf 10.18.2 to read the content-stream operators

Related: #383 (fixed by #437), #409 (the em-unit rule, consolidated in #437), #358. The corpus lists (file, page, cell in both versions) are available on request.

docling-parse-7.26.0-cjk-letter-spaced-runs-seams.csv

Lingua principale
C++
Stelle
352
Fork
99
Merge medio
1g 21h
PR unite (30g)
37

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di docling-project/docling-parse

Tutte le issue di docling-project/docling-parse

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.