FAILURE MAP
← Case archive

FA-80301 / Typography line breaking / Open access

Balanced headline wrapping: final layout width · case 01

The balanced width is computed but lines are laid out at the full width.

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

ROOT CAUSE

The final wrap uses the container width instead of the balanced width.

VERIFIED REPAIR

Wrap at the balanced width found by the search.

Unsuccessful approach: Averaging the two widths is still not the balanced width.

Case contract

Input [words, width]. Let L be the greedy line count at width (["overflow"] if a word is wider). Find the smallest width w (>= longest word) whose greedy wrap needs no more than L lines, then return the greedy wrap at w.

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, width = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= width:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: final layout width', [['x', 'kerning', 'of', 'grid'], 14], ['x kerning', 'of grid']), ('regression: final layout width', [['grid', 'measure', 'baseline', 'grid', 'kerning', 'type', 'type', 'type', 'of'], 24], ['grid measure baseline', 'grid kerning type', 'type type of']), ('regression: final layout width', [['at', 'is', 'baseline', 'on', 'a', 'of', 'measure'], 25], ['at is baseline', 'on a of measure']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['type', 'a', 'the', 'measure', 'x'], 5], ['overflow']), ('control layout', [['is', 'x', 'at', 'baseline', 'type', 'ink'], 30], ['is x at baseline type ink']), ('control layout', [['kerning', 'is', 'x', 'the', 'is', 'type', 'a', 'the'], 16], ['kerning is x the', 'is type a the'])], [('regression: final layout width', [['at', 'grid', 'measure', 'a', 'x', 'is', 'baseline', 'is'], 10], ['at grid', 'measure', 'a x is', 'baseline', 'is']), ('regression: final layout width', [['grid', 'at', 'a', 'measure', 'a', 'at', 'on', 'the'], 27], ['grid at a measure', 'a at on the']), ('regression: final layout width', [['kerning', 'a', 'is', 'the'], 10], ['kerning', 'a is the']), ('regression: final layout width', [['of', 'ink', 'is', 'at', 'a', 'the', 'on', 'ink'], 20], ['of ink is at', 'a the on ink']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'at', 'a', 'baseline', 'is'], 20], ['x at a baseline is']), ('control layout', [['kerning', 'of', 'baseline', 'serif', 'baseline', 'at', 'measure', 'of'], 16], ['kerning of', 'baseline serif', 'baseline at', 'measure of'])], [('regression: final layout width', [['on', 'measure', 'grid', 'a', 'x', 'is', 'kerning', 'kerning'], 27], ['on measure grid a x', 'is kerning kerning']), ('regression: final layout width', [['serif', 'of', 'is', 'on', 'of', 'ink', 'the'], 12], ['serif of', 'is on of', 'ink the']), ('regression: final layout width', [['measure', 'measure', 'the', 'baseline', 'kerning', 'at', 'serif'], 22], ['measure measure', 'the baseline', 'kerning at serif']), ('regression: final layout width', [['on', 'is', 'of', 'serif', 'grid', 'grid', 'on', 'grid'], 30], ['on is of serif', 'grid grid on grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['baseline', 'the', 'type', 'serif', 'x', 'type', 'is', 'a'], 18], ['baseline the type', 'serif x type is a']), ('control layout', [['a', 'serif', 'ink', 'measure', 'a', 'at'], 11], ['a serif ink', 'measure a', 'at'])], [('regression: final layout width', [['on', 'kerning', 'a', 'serif', 'at', 'at'], 18], ['on kerning a', 'serif at at']), ('regression: final layout width', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('regression: final layout width', [['type', 'type', 'the', 'ink', 'of', 'baseline', 'measure'], 29], ['type type the ink', 'of baseline measure']), ('regression: final layout width', [['on', 'of', 'baseline', 'x', 'kerning', 'x', 'type', 'the', 'the'], 29], ['on of baseline x', 'kerning x type the the']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['x', 'serif', 'ink', 'x', 'on', 'the', 'on'], 27], ['x serif ink x on the on']), ('control layout', [['at', 'is', 'serif', 'a', 'kerning', 'serif', 'baseline'], 26], ['at is serif a kerning', 'serif baseline'])], [('regression: final layout width', [['grid', 'kerning', 'ink', 'type', 'grid', 'serif', 'baseline'], 28], ['grid kerning ink type', 'grid serif baseline']), ('regression: final layout width', [['is', 'kerning', 'type', 'x', 'of', 'at', 'baseline', 'grid'], 21], ['is kerning type x', 'of at baseline grid']), ('regression: final layout width', [['baseline', 'a', 'of', 'is', 'kerning', 'ink'], 27], ['baseline a of', 'is kerning ink']), ('regression: final layout width', [['a', 'of', 'at', 'x', 'is', 'of'], 13], ['a of at', 'x is of']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['measure', 'grid', 'baseline', 'of'], 12], ['measure grid', 'baseline of']), ('control layout', [['grid', 'on', 'is', 'kerning', 'a'], 7], ['grid on', 'is', 'kerning', 'a'])]]
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
two balanced lines['the type grid is on', 'serif']['the type grid', 'is on serif']Failed
regression: final layout width['x kerning of', 'grid']['x kerning', 'of grid']Failed
regression: final layout width['grid measure baseline', 'grid kerning type type', 'type of']['grid measure baseline', 'grid kerning type', 'type type of']Failed
regression: final layout width['at is baseline on a of', 'measure']['at is baseline', 'on a of measure']Failed
word exactly the width['baseline', 'x']['baseline', 'x']Passed
control layout['overflow']['overflow']Passed
control layout['is x at baseline type ink']['is x at baseline type ink']Passed
control layout['kerning is x the', 'is type a the']['kerning is x the', 'is type a the']Passed

SHA-256 / 2f5e7f549db9ea69e9ef9e8622bc232382329d91662959bcd45c5a2660a7fc82

2 / The unsuccessful fix

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

N = 1
observations = []
def solve(x):
    words, width = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= (lo + width) // 2:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: final layout width', [['x', 'kerning', 'of', 'grid'], 14], ['x kerning', 'of grid']), ('regression: final layout width', [['grid', 'measure', 'baseline', 'grid', 'kerning', 'type', 'type', 'type', 'of'], 24], ['grid measure baseline', 'grid kerning type', 'type type of']), ('regression: final layout width', [['at', 'is', 'baseline', 'on', 'a', 'of', 'measure'], 25], ['at is baseline', 'on a of measure']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['type', 'a', 'the', 'measure', 'x'], 5], ['overflow']), ('control layout', [['is', 'x', 'at', 'baseline', 'type', 'ink'], 30], ['is x at baseline type ink']), ('control layout', [['kerning', 'is', 'x', 'the', 'is', 'type', 'a', 'the'], 16], ['kerning is x the', 'is type a the'])], [('regression: final layout width', [['at', 'grid', 'measure', 'a', 'x', 'is', 'baseline', 'is'], 10], ['at grid', 'measure', 'a x is', 'baseline', 'is']), ('regression: final layout width', [['grid', 'at', 'a', 'measure', 'a', 'at', 'on', 'the'], 27], ['grid at a measure', 'a at on the']), ('regression: final layout width', [['kerning', 'a', 'is', 'the'], 10], ['kerning', 'a is the']), ('regression: final layout width', [['of', 'ink', 'is', 'at', 'a', 'the', 'on', 'ink'], 20], ['of ink is at', 'a the on ink']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'at', 'a', 'baseline', 'is'], 20], ['x at a baseline is']), ('control layout', [['kerning', 'of', 'baseline', 'serif', 'baseline', 'at', 'measure', 'of'], 16], ['kerning of', 'baseline serif', 'baseline at', 'measure of'])], [('regression: final layout width', [['on', 'measure', 'grid', 'a', 'x', 'is', 'kerning', 'kerning'], 27], ['on measure grid a x', 'is kerning kerning']), ('regression: final layout width', [['serif', 'of', 'is', 'on', 'of', 'ink', 'the'], 12], ['serif of', 'is on of', 'ink the']), ('regression: final layout width', [['measure', 'measure', 'the', 'baseline', 'kerning', 'at', 'serif'], 22], ['measure measure', 'the baseline', 'kerning at serif']), ('regression: final layout width', [['on', 'is', 'of', 'serif', 'grid', 'grid', 'on', 'grid'], 30], ['on is of serif', 'grid grid on grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['baseline', 'the', 'type', 'serif', 'x', 'type', 'is', 'a'], 18], ['baseline the type', 'serif x type is a']), ('control layout', [['a', 'serif', 'ink', 'measure', 'a', 'at'], 11], ['a serif ink', 'measure a', 'at'])], [('regression: final layout width', [['on', 'kerning', 'a', 'serif', 'at', 'at'], 18], ['on kerning a', 'serif at at']), ('regression: final layout width', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('regression: final layout width', [['type', 'type', 'the', 'ink', 'of', 'baseline', 'measure'], 29], ['type type the ink', 'of baseline measure']), ('regression: final layout width', [['on', 'of', 'baseline', 'x', 'kerning', 'x', 'type', 'the', 'the'], 29], ['on of baseline x', 'kerning x type the the']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['x', 'serif', 'ink', 'x', 'on', 'the', 'on'], 27], ['x serif ink x on the on']), ('control layout', [['at', 'is', 'serif', 'a', 'kerning', 'serif', 'baseline'], 26], ['at is serif a kerning', 'serif baseline'])], [('regression: final layout width', [['grid', 'kerning', 'ink', 'type', 'grid', 'serif', 'baseline'], 28], ['grid kerning ink type', 'grid serif baseline']), ('regression: final layout width', [['is', 'kerning', 'type', 'x', 'of', 'at', 'baseline', 'grid'], 21], ['is kerning type x', 'of at baseline grid']), ('regression: final layout width', [['baseline', 'a', 'of', 'is', 'kerning', 'ink'], 27], ['baseline a of', 'is kerning ink']), ('regression: final layout width', [['a', 'of', 'at', 'x', 'is', 'of'], 13], ['a of at', 'x is of']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['measure', 'grid', 'baseline', 'of'], 12], ['measure grid', 'baseline of']), ('control layout', [['grid', 'on', 'is', 'kerning', 'a'], 7], ['grid on', 'is', 'kerning', 'a'])]]
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
two balanced lines['the type grid is', 'on serif']['the type grid', 'is on serif']Failed
regression: final layout width['x kerning', 'of grid']['x kerning', 'of grid']Passed
regression: final layout width['grid measure baseline', 'grid kerning type type', 'type of']['grid measure baseline', 'grid kerning type', 'type type of']Failed
regression: final layout width['at is baseline on a', 'of measure']['at is baseline', 'on a of measure']Failed
word exactly the width['baseline', 'x']['baseline', 'x']Passed
control layout['overflow']['overflow']Passed
control layout['is x at baseline type ink']['is x at baseline type ink']Passed
control layout['kerning is x the', 'is type a the']['kerning is x the', 'is type a the']Passed

SHA-256 / 1690e99a5e827c6a6509cb98330b83215f0dfd7c1dcbbbfff86b46a3021c61c6

3 / The verified repair

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

N = 1
observations = []
def solve(x):
    words, width = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: final layout width', [['x', 'kerning', 'of', 'grid'], 14], ['x kerning', 'of grid']), ('regression: final layout width', [['grid', 'measure', 'baseline', 'grid', 'kerning', 'type', 'type', 'type', 'of'], 24], ['grid measure baseline', 'grid kerning type', 'type type of']), ('regression: final layout width', [['at', 'is', 'baseline', 'on', 'a', 'of', 'measure'], 25], ['at is baseline', 'on a of measure']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['type', 'a', 'the', 'measure', 'x'], 5], ['overflow']), ('control layout', [['is', 'x', 'at', 'baseline', 'type', 'ink'], 30], ['is x at baseline type ink']), ('control layout', [['kerning', 'is', 'x', 'the', 'is', 'type', 'a', 'the'], 16], ['kerning is x the', 'is type a the'])], [('regression: final layout width', [['at', 'grid', 'measure', 'a', 'x', 'is', 'baseline', 'is'], 10], ['at grid', 'measure', 'a x is', 'baseline', 'is']), ('regression: final layout width', [['grid', 'at', 'a', 'measure', 'a', 'at', 'on', 'the'], 27], ['grid at a measure', 'a at on the']), ('regression: final layout width', [['kerning', 'a', 'is', 'the'], 10], ['kerning', 'a is the']), ('regression: final layout width', [['of', 'ink', 'is', 'at', 'a', 'the', 'on', 'ink'], 20], ['of ink is at', 'a the on ink']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'at', 'a', 'baseline', 'is'], 20], ['x at a baseline is']), ('control layout', [['kerning', 'of', 'baseline', 'serif', 'baseline', 'at', 'measure', 'of'], 16], ['kerning of', 'baseline serif', 'baseline at', 'measure of'])], [('regression: final layout width', [['on', 'measure', 'grid', 'a', 'x', 'is', 'kerning', 'kerning'], 27], ['on measure grid a x', 'is kerning kerning']), ('regression: final layout width', [['serif', 'of', 'is', 'on', 'of', 'ink', 'the'], 12], ['serif of', 'is on of', 'ink the']), ('regression: final layout width', [['measure', 'measure', 'the', 'baseline', 'kerning', 'at', 'serif'], 22], ['measure measure', 'the baseline', 'kerning at serif']), ('regression: final layout width', [['on', 'is', 'of', 'serif', 'grid', 'grid', 'on', 'grid'], 30], ['on is of serif', 'grid grid on grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['baseline', 'the', 'type', 'serif', 'x', 'type', 'is', 'a'], 18], ['baseline the type', 'serif x type is a']), ('control layout', [['a', 'serif', 'ink', 'measure', 'a', 'at'], 11], ['a serif ink', 'measure a', 'at'])], [('regression: final layout width', [['on', 'kerning', 'a', 'serif', 'at', 'at'], 18], ['on kerning a', 'serif at at']), ('regression: final layout width', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('regression: final layout width', [['type', 'type', 'the', 'ink', 'of', 'baseline', 'measure'], 29], ['type type the ink', 'of baseline measure']), ('regression: final layout width', [['on', 'of', 'baseline', 'x', 'kerning', 'x', 'type', 'the', 'the'], 29], ['on of baseline x', 'kerning x type the the']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['x', 'serif', 'ink', 'x', 'on', 'the', 'on'], 27], ['x serif ink x on the on']), ('control layout', [['at', 'is', 'serif', 'a', 'kerning', 'serif', 'baseline'], 26], ['at is serif a kerning', 'serif baseline'])], [('regression: final layout width', [['grid', 'kerning', 'ink', 'type', 'grid', 'serif', 'baseline'], 28], ['grid kerning ink type', 'grid serif baseline']), ('regression: final layout width', [['is', 'kerning', 'type', 'x', 'of', 'at', 'baseline', 'grid'], 21], ['is kerning type x', 'of at baseline grid']), ('regression: final layout width', [['baseline', 'a', 'of', 'is', 'kerning', 'ink'], 27], ['baseline a of', 'is kerning ink']), ('regression: final layout width', [['a', 'of', 'at', 'x', 'is', 'of'], 13], ['a of at', 'x is of']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['measure', 'grid', 'baseline', 'of'], 12], ['measure grid', 'baseline of']), ('control layout', [['grid', 'on', 'is', 'kerning', 'a'], 7], ['grid on', 'is', 'kerning', 'a'])]]
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
two balanced lines['the type grid', 'is on serif']['the type grid', 'is on serif']Passed
regression: final layout width['x kerning', 'of grid']['x kerning', 'of grid']Passed
regression: final layout width['grid measure baseline', 'grid kerning type', 'type type of']['grid measure baseline', 'grid kerning type', 'type type of']Passed
regression: final layout width['at is baseline', 'on a of measure']['at is baseline', 'on a of measure']Passed
word exactly the width['baseline', 'x']['baseline', 'x']Passed
control layout['overflow']['overflow']Passed
control layout['is x at baseline type ink']['is x at baseline type ink']Passed
control layout['kerning is x the', 'is type a the']['kerning is x the', 'is type a the']Passed

SHA-256 / 1412a95ce1ec54fd37131d35e94832f3a1afbda542facf81bb0dd1ed8d62de40

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:52.599433+00:00.

Case digest / bfa286257bc7b5dcd253967e7ca5d7940a5901175e1118ad6baf69c5ce8c63e6