N.05 / METHOD

No oracle: how I know the line breaks are right

A cost model, a brute-force reference, one hard invariant, one CSV baseline and byte-identical output: what this proves, and what it does not.

The hard part of a line breaker is not getting it right once, but knowing when it is right and when it is not. Nothing here treats Typst or TeX output as the answer; the reference is a solver that can be re-runAn oracle here is a reference output that can decide right from wrong. Line breaking has no unique right answer, only an optimum inside a cost model—so the reference has to be brute-forceable and re-runnable..

Another engine is not an oracle

Comparing against another typesetter is the cheapest and least reliable option. Two engines break the same paragraph differently and there is no way to say who is wrong: they do not optimise the same thing. TeX's demeritsKnuth & Plass, “Breaking Paragraphs into Lines” (1981): break the whole paragraph as one minimum-cost path. This engine keeps the objective but uses an additive cost instead of squared demerits. are squared; this cost is an additive weighted sum; punctuation compression, line-edge trimming and short last lines are each written into their own rules.

So the reference here is not another engine but our own cost model plus brute force. That narrows the question and makes it harder: how do we know the cost model is implemented correctly?

The cost is a sum, not a score

A line's cost is six terms added: line count, badness, boundary deviation, edge trimming, short last line and height. Each is normalised first, then weighted; there is no squared term and no coupling between terms. What is lost is TeX's decades of tuning priors; what is gained is that every decision can be read back from the trace.

Addition also means the model can be tested piecewise: for a given set of breakpoints the total must equal the sum of the six terms. A wrong normalisation is caught by a unit test instead of by a paragraph that looks odd.

md · html · tex · docxFOUR FRONT ENDSDocIRsoma.doc/2 · SEMANTIC盒树ATOM · BOUNDARYK-P → 分页INTEGER DPpdf · htmlsvg · jsonRULES禁则 · 标点类 · 行边裁剪上限 · ruleIdMETRICS宽度 · 墨迹 · MATH 常数 · 面选择THE BREAKER READS NEITHER BAND: ONLY ATOM AND BOUNDARYFigure 1 · One pipeline, two seams. The rules layer turns Chinese into Atom and Boundary attributes; the metrics layer turns fonts into widths and ink. Line breaking and pagination read only those two layers.

Chinese lives in the boundary, not in a branch

The breaker does not know Chinese. It sees a sequence of atoms and the boundaries between them: whether a break is allowed, how far the glue stretches, what the penalty is. Forbidden breaks, punctuation compression, edge trimming and CJK/Latin spacing are written onto those boundaries by the rules layer; there is no “if language is Chinese” branch inside the kernel.

The constraint has a testable consequence: changing the font, the template or the language never touches the DP. A wrong Chinese rule changes the rules table; a wrong DP changes the DP. Neither can hide behind the other.

中文排版,不是事后补丁。禁则 / FORBIDDEN断点 / CHOSEN可断 / ALLOWEDLINEBADNESSBOUNDARYTRIMSHORTHEIGHTBOUNDARY = BREAKABILITY · GLUE · PENALTY · CJK/LATIN DEVIATIONRULES WRITE THE BOUNDARY; THE DP ONLY ADDS.Figure 2 · Chinese never enters the DP. Forbidden breaks, glue, penalties and CJK/Latin deviation are written onto the boundary; six cost terms are added. Changing the language changes only the rules layer.

Brute force answers one question

For n ≤ 16, enumerate every set of breakpoints and take the cheapest. It is far too slow for production; its job is to give the dynamic program a reference: both must return the same breakpoint sequence, or the DP is wrong.

Brute force does not depend on any belief about the DP. It knows nothing about intermediate states, pruning or tie-breaks; it enumerates outcomes. A wrong pruning rule shows up immediately.

One invariant: K-P ≤ Greedy-C

Greedy and the DP read the same cost. Greedy looks at the current line, the DP at the whole paragraph, so on the same candidates the DP cannot be more expensive. That is a property of the model, not an observation, which is why it can be checked as a hard invariant across the corpus.

The converse does not hold: the DP is usually better, but not necessarily on every paragraph; when the two tie, the paragraph is simply insensitive to where it breaks.

The real regression test is one CSV line

Layout changes rarely turn a test red; they change where one paragraph breaks. So the corpus baseline is compressed into one line:

ALL,48,599,1213,0,0,3,0,0,3,486,1580,377,9

That line carries 48 documents, 599 pages and 1,213 paragraphs; zero over-compressed lines, zero emergency fallbacks, three continuation blocks, zero widows and zero orphans, followed by p95 columns for in-page whitespace and line-to-line jitter. Any change in layout behaviour has to explain why that line moved. If it cannot, it does not go in.

Table 1 · The nameable values in the Gate Q′ baseline
FieldValueMeaning
Documents / pages / paragraphs48 / 599 / 1 213Corpus size; pagination and breaking use one pipeline
Over-compressed lines0Lines whose glue was pushed below its lower bound
Emergency fallbacks0Lines that reached the fallback ladder
Continuation blocks3Paragraphs crossing a page that need a continuation band
Widows / orphans0 / 0Short first and last lines across page breaks

Determinism is a constraint, not an optimisation

All geometry sits on a 1/4096 em integer grid and the core contains no f32. Output is canonical JSON: field order, integer rounding and iteration order are fixed. The hash covers doc, template, styles, param, rules, metrics, engine and layout; native and wasm32-wasip1 must produce the same hash.

It also explains why renderers never re-break: PDF, HTML, SVG and JSON consume one LayoutResult. A backend that re-lays-out the text would break the equality in the next figure.

native wasm32-wasip1 LayoutResult 1/4096 em · CANONICAL JSON pdf html svg canonical json NO RE-BREAKNO RE-BREAKNO RE-BREAKNO RE-BREAK HASH · fnv(doc · template · styles · param · rules · metrics · engine · layout) SAME HASH ON BOTH TARGETS Figure 3 · Same bytes for the same input. Both targets emit one LayoutResult and one hash; the four renderers consume it and never re-break.

The same kernel in your browser

The paragraph below is not a screenshot and not a precomputed result: it is broken in your browser by the same inline K-P kernel, with the rules layer compiled in. The module is 32 KB gzip with zero imports, and it loads only when this section scrolls into view.

K-P / GLOBAL OPTIMUM

ATOMS –LINES –BREAK – MSFALLBACK –PARITY –

Widths come from the browser's measureText, which differs from the PDF's .smt table by 1–2% (D95), so the breaks can differ. On fallback the D94 policy switches to Greedy-C and the readout says so.

What this does not prove

  • It proves the breaks are optimal within this cost model, not that the cost model equals good typography. That is judged by the corpus and by reading.
  • Brute force stops at n ≤ 16. Longer paragraphs have invariants and the corpus, not a reference solution.
  • The corpus is a set of documents, not every Chinese layout situation; vertical writing, bidi and multi-column floats are outside this pipeline.
  • Performance is separate: mixed 2,000 atoms takes 20.23 ms, 1.2% over the 20 ms budget. Correctness does not imply the budget holds.

Saying clearly when a conclusion is not available is worth more than one more assertion. It is also why the gates include clean-room and dependency-boundary checks: they guard against drift in provenance and scope, not against layout bugs.

Correctness is not "it looks right"; it is the same bytes from the same input on every target.