FAILURE MAP
← Case archive

FA-79881 / Typography line breaking / Open access

Total-fit paragraph demerits: demerit squaring · case 01

Break choices favour many mediocre lines over fewer good ones.

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

ROOT CAUSE

Only the badness is squared; the line penalty is added outside the square.

THE FAILURE

Only the badness is squared; the line penalty is added outside the square.

Unsuccessful approach: Doubling instead of squaring flattens the demerit curve.

Case contract

Input [word widths, [space, stretch, shrink] per gap, line width, line penalty]. A line i..j has gaps=j-i-1. The last line has zero badness when not overfull. Other lines use ratio shortfall/(stretch*gaps) or /(shrink*gaps); zero stretch with slack or compression past shrink is infeasible; badness=min(10000, floor(100|r|^3+1/2)) and must be <=1000. Fitness 0 tight (r<-1/2), 1 decent (r<=1/2), 2 loose (r<=1), 3 very loose; last line decent. Demerits (lp+b)^2 plus 3000 when adjacent fitness classes differ by more than 1, starting from decent. Return [min total demerits, break list] (ties: first found) or ["infeasible"].

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
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    words, glue, width, lp = x
    sp, st, sh = glue
    n = len(words)
    def rate(i, j):
        gaps = j - i - 1
        nat = sum(words[i:j]) + sp * gaps
        short = width - nat
        if j == n and short >= 0:
            return 0, 1
        if short > 0:
            if st * gaps == 0:
                return None
            r = Fraction(short, st * gaps)
        elif short < 0:
            if -short > sh * gaps:
                return None
            r = Fraction(short, sh * gaps)
        else:
            r = Fraction(0)
        b = min(10000, math.floor(100 * abs(r) ** 3 + Fraction(1, 2)))
        if b > 1000:
            return None
        fit = 0 if r < Fraction(-1, 2) else 1 if r <= Fraction(1, 2) else 2 if r <= 1 else 3
        return b, fit
    best = {(0, 1): (0, [])}
    for j in range(1, n + 1):
        for i in range(j):
            rated = rate(i, j)
            if rated is None:
                continue
            b, fit = rated
            for (pos, pfit), (dem, brk) in sorted(best.items()):
                if pos != i:
                    continue
                d = dem + lp + b ** 2
                if abs(fit - pfit) > 1:
                    d += 3000
                key = (j, fit)
                if key not in best or d < best[key][0]:
                    best[key] = (d, brk + [j])
    finals = [v for (pos, f), v in best.items() if pos == n]
    if not finals:
        return ['infeasible']
    d, brk = min(finals)
    return [d, brk]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('regression: demerit squaring', [[1, 5, 5], [1, 3, 2], 14, 10], [100, [3]]), ('regression: demerit squaring', [[4, 2, 4, 4, 5, 1, 1], [2, 3, 0], 14, 50], [7500, [3, 6, 7]]), ('regression: demerit squaring', [[2, 5, 1, 1, 6, 7], [1, 2, 2], 15, 10], [629, [4, 6]]), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('control layout', [[6, 1, 7, 2, 3, 2, 3, 3], [3, 3, 0], 9, 1], ['infeasible']), ('control layout', [[6, 6, 6, 1, 4, 3, 1, 7], [3, 1, 1], 10, 1], ['infeasible'])], [('regression: demerit squaring', [[4, 6, 5, 1, 1, 1, 5, 2], [1, 1, 2], 18, 10], [200, [4, 8]]), ('regression: demerit squaring', [[1, 4, 6, 3, 3, 2, 3, 4], [3, 2, 0], 20, 50], [13464, [3, 7, 8]]), ('partial-repair probe', [[4, 5, 4, 4], [1, 3, 2], 23, 1], [1, [4]]), ('regression: demerit squaring', [[2, 2, 5, 3, 6, 2, 5, 3], [2, 1, 2], 17, 50], [7500, [4, 7, 8]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('control layout', [[2, 6, 5, 7, 3, 6], [3, 3, 1], 13, 10], ['infeasible']), ('control layout', [[2, 6, 7, 3, 1, 5, 5, 1], [1, 1, 1], 9, 1], ['infeasible'])], [('regression: demerit squaring', [[1, 5, 5], [1, 3, 2], 14, 10], [100, [3]]), ('regression: demerit squaring', [[5, 6, 4, 5, 5, 2], [1, 2, 2], 11, 10], [1158, [2, 4, 6]]), ('regression: demerit squaring', [[4, 1, 4], [3, 2, 1], 24, 50], [2500, [3]]), ('regression: demerit squaring', [[6, 3, 4, 2, 2], [3, 3, 2], 11, 1], [1158, [2, 4, 5]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('control layout', [[3, 2, 6, 5, 1], [1, 1, 0], 9, 50], ['infeasible']), ('control layout', [[6, 4, 6, 5, 2, 3], [3, 1, 0], 9, 50], ['infeasible'])], [('regression: demerit squaring', [[2, 5, 1, 1, 6, 7], [1, 2, 2], 15, 10], [629, [4, 6]]), ('regression: demerit squaring', [[1, 7, 7, 2, 4, 1, 3, 4], [1, 1, 2], 13, 50], [27500, [3, 7, 8]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('regression: demerit squaring', [[3, 2, 5, 2], [2, 1, 2], 20, 50], [2500, [4]]), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('control layout', [[6, 3, 6, 5, 7], [3, 1, 2], 9, 1], ['infeasible']), ('control layout', [[3, 2, 4, 1, 4, 4, 1, 7], [3, 1, 0], 9, 10], ['infeasible'])], [('regression: demerit squaring', [[7, 1, 4, 7, 1, 4], [3, 3, 0], 24, 1], [10202, [3, 6]]), ('regression: demerit squaring', [[3, 5, 7, 2, 2, 1], [2, 3, 1], 11, 1], [27, [2, 4, 6]]), ('regression: demerit squaring', [[1, 2, 4, 4, 5], [2, 2, 1], 21, 1], [962, [4, 5]]), ('regression: demerit squaring', [[1, 3, 1, 2, 2], [3, 2, 1], 10, 10], [629, [3, 5]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('control layout', [[2, 7, 6, 1, 7, 4, 6, 2], [2, 3, 0], 17, 10], ['infeasible']), ('control layout', [[4, 3, 7, 7, 7], [1, 1, 1], 11, 10], ['infeasible'])]]
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-line paragraph with loose first line[10020, [3, 4]][12200, [3, 4]]Failed
regression: demerit squaring[10, [3]][100, [3]]Failed
regression: demerit squaring[150, [3, 6, 7]][7500, [3, 6, 7]]Failed
regression: demerit squaring[189, [4, 6]][629, [4, 6]]Failed
tight then loose lines[20003, [3, 6, 7]][20403, [3, 6, 7]]Failed
last line overfull['infeasible']['infeasible']Passed
control layout['infeasible']['infeasible']Passed
control layout['infeasible']['infeasible']Passed

SHA-256 / 197e5e6c8f0c242b1b66f46472f85e025b5753cbec6935f6796dc03b9c208f14

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    words, glue, width, lp = x
    sp, st, sh = glue
    n = len(words)
    def rate(i, j):
        gaps = j - i - 1
        nat = sum(words[i:j]) + sp * gaps
        short = width - nat
        if j == n and short >= 0:
            return 0, 1
        if short > 0:
            if st * gaps == 0:
                return None
            r = Fraction(short, st * gaps)
        elif short < 0:
            if -short > sh * gaps:
                return None
            r = Fraction(short, sh * gaps)
        else:
            r = Fraction(0)
        b = min(10000, math.floor(100 * abs(r) ** 3 + Fraction(1, 2)))
        if b > 1000:
            return None
        fit = 0 if r < Fraction(-1, 2) else 1 if r <= Fraction(1, 2) else 2 if r <= 1 else 3
        return b, fit
    best = {(0, 1): (0, [])}
    for j in range(1, n + 1):
        for i in range(j):
            rated = rate(i, j)
            if rated is None:
                continue
            b, fit = rated
            for (pos, pfit), (dem, brk) in sorted(best.items()):
                if pos != i:
                    continue
                d = dem + (lp + b) * 2
                if abs(fit - pfit) > 1:
                    d += 3000
                key = (j, fit)
                if key not in best or d < best[key][0]:
                    best[key] = (d, brk + [j])
    finals = [v for (pos, f), v in best.items() if pos == n]
    if not finals:
        return ['infeasible']
    d, brk = min(finals)
    return [d, brk]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('regression: demerit squaring', [[1, 5, 5], [1, 3, 2], 14, 10], [100, [3]]), ('regression: demerit squaring', [[4, 2, 4, 4, 5, 1, 1], [2, 3, 0], 14, 50], [7500, [3, 6, 7]]), ('regression: demerit squaring', [[2, 5, 1, 1, 6, 7], [1, 2, 2], 15, 10], [629, [4, 6]]), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('control layout', [[6, 1, 7, 2, 3, 2, 3, 3], [3, 3, 0], 9, 1], ['infeasible']), ('control layout', [[6, 6, 6, 1, 4, 3, 1, 7], [3, 1, 1], 10, 1], ['infeasible'])], [('regression: demerit squaring', [[4, 6, 5, 1, 1, 1, 5, 2], [1, 1, 2], 18, 10], [200, [4, 8]]), ('regression: demerit squaring', [[1, 4, 6, 3, 3, 2, 3, 4], [3, 2, 0], 20, 50], [13464, [3, 7, 8]]), ('partial-repair probe', [[4, 5, 4, 4], [1, 3, 2], 23, 1], [1, [4]]), ('regression: demerit squaring', [[2, 2, 5, 3, 6, 2, 5, 3], [2, 1, 2], 17, 50], [7500, [4, 7, 8]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('control layout', [[2, 6, 5, 7, 3, 6], [3, 3, 1], 13, 10], ['infeasible']), ('control layout', [[2, 6, 7, 3, 1, 5, 5, 1], [1, 1, 1], 9, 1], ['infeasible'])], [('regression: demerit squaring', [[1, 5, 5], [1, 3, 2], 14, 10], [100, [3]]), ('regression: demerit squaring', [[5, 6, 4, 5, 5, 2], [1, 2, 2], 11, 10], [1158, [2, 4, 6]]), ('regression: demerit squaring', [[4, 1, 4], [3, 2, 1], 24, 50], [2500, [3]]), ('regression: demerit squaring', [[6, 3, 4, 2, 2], [3, 3, 2], 11, 1], [1158, [2, 4, 5]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('control layout', [[3, 2, 6, 5, 1], [1, 1, 0], 9, 50], ['infeasible']), ('control layout', [[6, 4, 6, 5, 2, 3], [3, 1, 0], 9, 50], ['infeasible'])], [('regression: demerit squaring', [[2, 5, 1, 1, 6, 7], [1, 2, 2], 15, 10], [629, [4, 6]]), ('regression: demerit squaring', [[1, 7, 7, 2, 4, 1, 3, 4], [1, 1, 2], 13, 50], [27500, [3, 7, 8]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('regression: demerit squaring', [[3, 2, 5, 2], [2, 1, 2], 20, 50], [2500, [4]]), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('control layout', [[6, 3, 6, 5, 7], [3, 1, 2], 9, 1], ['infeasible']), ('control layout', [[3, 2, 4, 1, 4, 4, 1, 7], [3, 1, 0], 9, 10], ['infeasible'])], [('regression: demerit squaring', [[7, 1, 4, 7, 1, 4], [3, 3, 0], 24, 1], [10202, [3, 6]]), ('regression: demerit squaring', [[3, 5, 7, 2, 2, 1], [2, 3, 1], 11, 1], [27, [2, 4, 6]]), ('regression: demerit squaring', [[1, 2, 4, 4, 5], [2, 2, 1], 21, 1], [962, [4, 5]]), ('regression: demerit squaring', [[1, 3, 1, 2, 2], [3, 2, 1], 10, 10], [629, [3, 5]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('control layout', [[2, 7, 6, 1, 7, 4, 6, 2], [2, 3, 0], 17, 10], ['infeasible']), ('control layout', [[4, 3, 7, 7, 7], [1, 1, 1], 11, 10], ['infeasible'])]]
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-line paragraph with loose first line[240, [3, 4]][12200, [3, 4]]Failed
regression: demerit squaring[20, [3]][100, [3]]Failed
regression: demerit squaring[300, [3, 6, 7]][7500, [3, 6, 7]]Failed
regression: demerit squaring[66, [4, 6]][629, [4, 6]]Failed
tight then loose lines[406, [3, 6, 7]][20403, [3, 6, 7]]Failed
last line overfull['infeasible']['infeasible']Passed
control layout['infeasible']['infeasible']Passed
control layout['infeasible']['infeasible']Passed

SHA-256 / 856842c9e97f083203250315b36229f5b14d4422f85b26c758e1b6deacc6a34f

HELD IN THE MEMBER ARCHIVE

The verified repair and its recorded checks are member-only.

This mechanism has 8 recorded checks per implementation. The open-access tier publishes the failure and the unsuccessful fix; the repaired source that passes every check, and the observations that prove it, are available to members.

Every case sharing this mechanism uses the same contract and the same repair, so this one record is held back for all of them.

Member access is invitation-based. Sign in with your invited account to inspect the repair.

Sign in to the archive ↗

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

Case digest / c546c9d16a976d0ac70708691d47a92c44e076628cc8a339e033acb15258c606