FAILURE MAP
← Case archive

FA-80066 / Typography line breaking / Open access

Justified interword spacing: last-line justification · case 01

The final line of a paragraph is stretched across the measure.

Verified by executionVariant 1 · 8 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

The ragged path is only taken for single-word lines, not for the last line.

VERIFIED REPAIR

Set the last line and single-word lines ragged.

Unsuccessful approach: Requiring both conditions together still justifies ordinary last lines.

Case contract

Input [word widths, target width, is_last, natural space, min space, max space]. Last lines and single-word lines are set ragged with natural spaces. Otherwise distribute target-sum(words) over the gaps: equal share with the remainder units added to the rightmost gaps; a share below min space is ["overfull"]; if any gap would exceed max space fall back to ragged. Return word x positions.

Why this case matters

Line breaking decides where paragraphs wrap on screen and in print; a wrong decision point shifts every following line.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    words, target, is_last, sp, lo, hi = x
    n = len(words)
    gaps = n - 1
    def natural():
        pos, xx = [], 0
        for w in words:
            pos.append(xx)
            xx += w + sp
        return pos
    if gaps == 0:
        return natural()
    space = target - sum(words)
    per, rem = divmod(space, gaps)
    if per < lo:
        return ['overfull']
    if per + (1 if rem else 0) > hi:
        return natural()
    pos, xx = [], 0
    for k, w in enumerate(words):
        pos.append(xx)
        if k < gaps:
            xx += w + per + (1 if k >= gaps - rem else 0)
    return pos
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('regression: last-line justification', [[8, 5, 2], 16, True, 3, 1, 5], [0, 11, 19]), ('regression: last-line justification', [[3, 9, 2, 2, 7], 43, True, 3, 1, 7], [0, 6, 18, 23, 28]), ('regression: last-line justification', [[5, 9], 15, True, 2, 1, 6], [0, 7]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('control layout', [[9, 3, 2], 15, False, 3, 3, 4], ['overfull']), ('control layout', [[7, 6, 5, 6, 2], 42, False, 2, 2, 3], [0, 9, 17, 24, 32])], [('regression: last-line justification', [[4, 5, 3, 6, 3], 41, True, 3, 3, 7], [0, 7, 15, 21, 30]), ('regression: last-line justification', [[9, 2, 8], 20, True, 2, 1, 3], [0, 11, 15]), ('regression: last-line justification', [[5, 9, 5, 7, 2], 32, True, 3, 3, 4], [0, 8, 20, 28, 38]), ('regression: last-line justification', [[7, 5, 4, 6, 9], 43, True, 2, 2, 4], [0, 9, 16, 22, 30]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('control layout', [[2, 9, 9, 3, 8], 36, False, 3, 3, 7], ['overfull']), ('control layout', [[6, 4, 6, 4, 8], 51, False, 2, 2, 4], [0, 8, 14, 22, 28])], [('regression: last-line justification', [[7, 8, 3, 7, 9], 37, True, 2, 2, 5], [0, 9, 19, 24, 33]), ('regression: last-line justification', [[4, 5, 3, 4], 28, True, 3, 3, 6], [0, 7, 15, 21]), ('regression: last-line justification', [[9, 8, 3], 21, True, 2, 1, 5], [0, 11, 21]), ('regression: last-line justification', [[4, 8, 4, 8], 26, True, 2, 1, 5], [0, 6, 16, 22]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('control layout', [[4, 3, 2, 3, 5, 4], 55, True, 2, 2, 6], [0, 6, 11, 15, 20, 27]), ('control layout', [[7, 4], 17, False, 3, 1, 7], [0, 13])], [('regression: last-line justification', [[2, 8, 3, 2, 2, 4], 27, True, 2, 2, 6], [0, 4, 14, 19, 23, 27]), ('regression: last-line justification', [[7, 5], 14, True, 3, 2, 7], [0, 10]), ('regression: last-line justification', [[7, 3, 8], 21, True, 3, 3, 6], [0, 10, 16]), ('regression: last-line justification', [[5, 9, 5, 7, 2], 32, True, 3, 3, 4], [0, 8, 20, 28, 38]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('control layout', [[7, 2], 15, False, 3, 2, 4], [0, 10]), ('control layout', [[7, 3, 9, 5, 8], 40, False, 2, 2, 3], [0, 9, 14, 25, 32])], [('regression: last-line justification', [[2, 2], 4, True, 3, 2, 7], [0, 5]), ('regression: last-line justification', [[9, 8, 8, 7, 5], 53, True, 3, 3, 4], [0, 12, 23, 34, 44]), ('regression: last-line justification', [[5, 2, 2, 8, 9], 35, True, 3, 3, 4], [0, 8, 13, 18, 29]), ('regression: last-line justification', [[3, 8], 14, True, 2, 1, 6], [0, 5]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('control layout', [[4, 3, 9], 24, False, 2, 2, 6], [0, 8, 15]), ('control layout', [[4, 9, 9, 7], 45, False, 3, 1, 4], [0, 7, 19, 31])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
last line ragged[0, 7, 15][0, 5, 11]Failed
regression: last-line justification['overfull'][0, 11, 19]Failed
regression: last-line justification[0, 8, 22, 29, 36][0, 6, 18, 23, 28]Failed
regression: last-line justification[0, 6][0, 7]Failed
remainder goes to right gaps[0, 5, 11, 18][0, 5, 11, 18]Passed
exactly minimum share[0, 4, 8][0, 4, 8]Passed
control layout['overfull']['overfull']Passed
control layout[0, 9, 17, 24, 32][0, 9, 17, 24, 32]Passed

SHA-256 / 221b2b5b6c45aa709552d5373336559a26d171af6e31a6ad3b10c209a588dafb

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    words, target, is_last, sp, lo, hi = x
    n = len(words)
    gaps = n - 1
    def natural():
        pos, xx = [], 0
        for w in words:
            pos.append(xx)
            xx += w + sp
        return pos
    if is_last and gaps == 0:
        return natural()
    space = target - sum(words)
    per, rem = divmod(space, gaps)
    if per < lo:
        return ['overfull']
    if per + (1 if rem else 0) > hi:
        return natural()
    pos, xx = [], 0
    for k, w in enumerate(words):
        pos.append(xx)
        if k < gaps:
            xx += w + per + (1 if k >= gaps - rem else 0)
    return pos
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('regression: last-line justification', [[8, 5, 2], 16, True, 3, 1, 5], [0, 11, 19]), ('regression: last-line justification', [[3, 9, 2, 2, 7], 43, True, 3, 1, 7], [0, 6, 18, 23, 28]), ('regression: last-line justification', [[5, 9], 15, True, 2, 1, 6], [0, 7]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('control layout', [[9, 3, 2], 15, False, 3, 3, 4], ['overfull']), ('control layout', [[7, 6, 5, 6, 2], 42, False, 2, 2, 3], [0, 9, 17, 24, 32])], [('regression: last-line justification', [[4, 5, 3, 6, 3], 41, True, 3, 3, 7], [0, 7, 15, 21, 30]), ('regression: last-line justification', [[9, 2, 8], 20, True, 2, 1, 3], [0, 11, 15]), ('regression: last-line justification', [[5, 9, 5, 7, 2], 32, True, 3, 3, 4], [0, 8, 20, 28, 38]), ('regression: last-line justification', [[7, 5, 4, 6, 9], 43, True, 2, 2, 4], [0, 9, 16, 22, 30]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('control layout', [[2, 9, 9, 3, 8], 36, False, 3, 3, 7], ['overfull']), ('control layout', [[6, 4, 6, 4, 8], 51, False, 2, 2, 4], [0, 8, 14, 22, 28])], [('regression: last-line justification', [[7, 8, 3, 7, 9], 37, True, 2, 2, 5], [0, 9, 19, 24, 33]), ('regression: last-line justification', [[4, 5, 3, 4], 28, True, 3, 3, 6], [0, 7, 15, 21]), ('regression: last-line justification', [[9, 8, 3], 21, True, 2, 1, 5], [0, 11, 21]), ('regression: last-line justification', [[4, 8, 4, 8], 26, True, 2, 1, 5], [0, 6, 16, 22]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('control layout', [[4, 3, 2, 3, 5, 4], 55, True, 2, 2, 6], [0, 6, 11, 15, 20, 27]), ('control layout', [[7, 4], 17, False, 3, 1, 7], [0, 13])], [('regression: last-line justification', [[2, 8, 3, 2, 2, 4], 27, True, 2, 2, 6], [0, 4, 14, 19, 23, 27]), ('regression: last-line justification', [[7, 5], 14, True, 3, 2, 7], [0, 10]), ('regression: last-line justification', [[7, 3, 8], 21, True, 3, 3, 6], [0, 10, 16]), ('regression: last-line justification', [[5, 9, 5, 7, 2], 32, True, 3, 3, 4], [0, 8, 20, 28, 38]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('control layout', [[7, 2], 15, False, 3, 2, 4], [0, 10]), ('control layout', [[7, 3, 9, 5, 8], 40, False, 2, 2, 3], [0, 9, 14, 25, 32])], [('regression: last-line justification', [[2, 2], 4, True, 3, 2, 7], [0, 5]), ('regression: last-line justification', [[9, 8, 8, 7, 5], 53, True, 3, 3, 4], [0, 12, 23, 34, 44]), ('regression: last-line justification', [[5, 2, 2, 8, 9], 35, True, 3, 3, 4], [0, 8, 13, 18, 29]), ('regression: last-line justification', [[3, 8], 14, True, 2, 1, 6], [0, 5]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('control layout', [[4, 3, 9], 24, False, 2, 2, 6], [0, 8, 15]), ('control layout', [[4, 9, 9, 7], 45, False, 3, 1, 4], [0, 7, 19, 31])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
last line ragged[0, 7, 15][0, 5, 11]Failed
regression: last-line justification['overfull'][0, 11, 19]Failed
regression: last-line justification[0, 8, 22, 29, 36][0, 6, 18, 23, 28]Failed
regression: last-line justification[0, 6][0, 7]Failed
remainder goes to right gaps[0, 5, 11, 18][0, 5, 11, 18]Passed
exactly minimum share[0, 4, 8][0, 4, 8]Passed
control layout['overfull']['overfull']Passed
control layout[0, 9, 17, 24, 32][0, 9, 17, 24, 32]Passed

SHA-256 / 899a22b1c7f2ccb353264f2655ea1acfbb4d3d2690cdd14525c0c22666f9d3ed

3 / The verified repair

Exit 0
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    words, target, is_last, sp, lo, hi = x
    n = len(words)
    gaps = n - 1
    def natural():
        pos, xx = [], 0
        for w in words:
            pos.append(xx)
            xx += w + sp
        return pos
    if is_last or gaps == 0:
        return natural()
    space = target - sum(words)
    per, rem = divmod(space, gaps)
    if per < lo:
        return ['overfull']
    if per + (1 if rem else 0) > hi:
        return natural()
    pos, xx = [], 0
    for k, w in enumerate(words):
        pos.append(xx)
        if k < gaps:
            xx += w + per + (1 if k >= gaps - rem else 0)
    return pos
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('regression: last-line justification', [[8, 5, 2], 16, True, 3, 1, 5], [0, 11, 19]), ('regression: last-line justification', [[3, 9, 2, 2, 7], 43, True, 3, 1, 7], [0, 6, 18, 23, 28]), ('regression: last-line justification', [[5, 9], 15, True, 2, 1, 6], [0, 7]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('control layout', [[9, 3, 2], 15, False, 3, 3, 4], ['overfull']), ('control layout', [[7, 6, 5, 6, 2], 42, False, 2, 2, 3], [0, 9, 17, 24, 32])], [('regression: last-line justification', [[4, 5, 3, 6, 3], 41, True, 3, 3, 7], [0, 7, 15, 21, 30]), ('regression: last-line justification', [[9, 2, 8], 20, True, 2, 1, 3], [0, 11, 15]), ('regression: last-line justification', [[5, 9, 5, 7, 2], 32, True, 3, 3, 4], [0, 8, 20, 28, 38]), ('regression: last-line justification', [[7, 5, 4, 6, 9], 43, True, 2, 2, 4], [0, 9, 16, 22, 30]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('control layout', [[2, 9, 9, 3, 8], 36, False, 3, 3, 7], ['overfull']), ('control layout', [[6, 4, 6, 4, 8], 51, False, 2, 2, 4], [0, 8, 14, 22, 28])], [('regression: last-line justification', [[7, 8, 3, 7, 9], 37, True, 2, 2, 5], [0, 9, 19, 24, 33]), ('regression: last-line justification', [[4, 5, 3, 4], 28, True, 3, 3, 6], [0, 7, 15, 21]), ('regression: last-line justification', [[9, 8, 3], 21, True, 2, 1, 5], [0, 11, 21]), ('regression: last-line justification', [[4, 8, 4, 8], 26, True, 2, 1, 5], [0, 6, 16, 22]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('control layout', [[4, 3, 2, 3, 5, 4], 55, True, 2, 2, 6], [0, 6, 11, 15, 20, 27]), ('control layout', [[7, 4], 17, False, 3, 1, 7], [0, 13])], [('regression: last-line justification', [[2, 8, 3, 2, 2, 4], 27, True, 2, 2, 6], [0, 4, 14, 19, 23, 27]), ('regression: last-line justification', [[7, 5], 14, True, 3, 2, 7], [0, 10]), ('regression: last-line justification', [[7, 3, 8], 21, True, 3, 3, 6], [0, 10, 16]), ('regression: last-line justification', [[5, 9, 5, 7, 2], 32, True, 3, 3, 4], [0, 8, 20, 28, 38]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('remainder goes to right gaps', [[3, 4, 5, 2], 20, False, 2, 1, 4], [0, 5, 11, 18]), ('control layout', [[7, 2], 15, False, 3, 2, 4], [0, 10]), ('control layout', [[7, 3, 9, 5, 8], 40, False, 2, 2, 3], [0, 9, 14, 25, 32])], [('regression: last-line justification', [[2, 2], 4, True, 3, 2, 7], [0, 5]), ('regression: last-line justification', [[9, 8, 8, 7, 5], 53, True, 3, 3, 4], [0, 12, 23, 34, 44]), ('regression: last-line justification', [[5, 2, 2, 8, 9], 35, True, 3, 3, 4], [0, 8, 13, 18, 29]), ('regression: last-line justification', [[3, 8], 14, True, 2, 1, 6], [0, 5]), ('exactly minimum share', [[3, 3, 3], 11, False, 2, 1, 4], [0, 4, 8]), ('last line ragged', [[3, 4, 5], 20, True, 2, 1, 6], [0, 5, 11]), ('control layout', [[4, 3, 9], 24, False, 2, 2, 6], [0, 8, 15]), ('control layout', [[4, 9, 9, 7], 45, False, 3, 1, 4], [0, 7, 19, 31])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
last line ragged[0, 5, 11][0, 5, 11]Passed
regression: last-line justification[0, 11, 19][0, 11, 19]Passed
regression: last-line justification[0, 6, 18, 23, 28][0, 6, 18, 23, 28]Passed
regression: last-line justification[0, 7][0, 7]Passed
remainder goes to right gaps[0, 5, 11, 18][0, 5, 11, 18]Passed
exactly minimum share[0, 4, 8][0, 4, 8]Passed
control layout['overfull']['overfull']Passed
control layout[0, 9, 17, 24, 32][0, 9, 17, 24, 32]Passed

SHA-256 / f023ece3db4436f8721dc4914dba5192abe9b5c9efb73eb0145231c9810071bf

Verification & scope

A deterministic toy typesetting model with integer widths and a stipulated rule set; it does not claim conformance to any engine. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:49:50.337688+00:00.

Case digest / 9aaf361b7cedc6a0f2283866f92ae30b1e3e51cd4dfa052476af47a216883ca9