First Fit
As a browser breaks it, one line at a time
Measuring the face
One paragraph, set twice, every line priced
In olden times when wishing still helped one, there lived a king whose daughters were all beautiful; and the youngest was so beautiful that the sun itself, which has seen so much, was astonished whenever it shone in her face. Close by the king’s castle lay a great dark forest, and under an old lime-tree in the forest was a well, and when the day was very warm, the king’s child went out into the forest and sat down by the side of the cool fountain; and when she was bored she took a golden ball, and threw it up on high and caught it; and this ball was her favorite plaything.
As a browser breaks it, one line at a time
Measuring the face
As Knuth and Plass break it, all at once
Measuring the face
Ratio, badness and demerits for each line
| Line | Ends | Ratio | Badness | Demerits | Note |
|---|---|---|---|---|---|
| Measuring the face | |||||
| Line | Ends | Ratio | Badness | Demerits | Note |
|---|---|---|---|---|---|
| Measuring the face | |||||
Knuth and Plass’s prices, as plain TeX sets them
Both pages break the same paragraph in the same face at the same measure. The words are boxes, measured in your browser in Old Standard TT at 18 px before anything is broken. Each space is glue: the face’s own space, which may stretch by half its width and shrink by a third, the proportions of the paper’s example (Knuth and Plass 1981, p. 1124). Each hyphenation point from Liang’s patterns is a penalty of 50.
A line’s adjustment ratio r is the share of its stretch (or shrink) it must use to reach the measure: the line’s shortfall divided by its total stretch, or its excess divided by its total shrink (p. 1126). Its badness is 100|r|³, so a line stretched twice as far is eight times as bad; a line squeezed past r = −1 is overfull (p. 1127). TeX caps badness at 10,000 (tex.web §108).
Each line’s demerits are (10 + badness)² plus the square of its penalty, as TeX82 counts them (tex.web §859), with 10,000 more when two lines in a row end in hyphens, 5,000 when the second-last line does, and 10,000 when a tight line meets a very loose one. These are plain TeX’s settings (plain.tex: \linepenalty=10, \hyphenpenalty=50, \doublehyphendemerits=10000, \finalhyphendemerits=5000, \adjdemerits=10000). A page’s demerits are the sum of its lines’.
First fit is how browsers justify: put words on the line while they fit at their natural spacing, break before the first that does not (or inside it, at a hyphenation point, when hyphenation is on), then stretch the spaces to fill. Each line is final the moment it is set. Total fit tries every legal break against every line start still in reach, keeps the cheapest way to arrive at each, and returns the paragraph with the fewest demerits by these prices (p. 1159). When no setting keeps every line within the tolerance, it takes any line that is not overfull, as \tolerance=10000 does.
The bar in each margin is the line’s ratio; the tick across the margin is your tolerance, so a bar that crosses it is a loose line. The figure beside the bar is the badness, a whole number as TeX prints it.
The paper’s own numbers, re-run by this engine
The engine that sets your paragraph is tested against the paper it comes from. Thirteen character widths are not printed in the paper; they are fitted by least squares from the paper’s printed ratios, and with them every one of the paper’s six settings breaks exactly where the paper’s does.
| Receipt | The source | This engine |
|---|---|---|
| Legal breakpoints in the paragraph | 129 (p. 1125) | 129 |
| Items, one box per character | 601 (p. 1124) | 601 |
| Figure 1, first fit, measure 421 | 11 lines | same 11 lines |
| Figure 2, best fit, measure 421 | 11 lines | same 11 lines |
| Figure 3, total fit, measure 421 | 11 lines | same 11 lines |
| Figure 4, all three methods, measure 500 | 9, 10 and 10 lines | same lines |
| Figure 1 badness from its printed ratios | 0 7 68 18 5 0 69 72 90 49 0 (p. 1127) | 0 7 68 18 5 0 69 72 90 49 0 |
| Demerits of all 62 printed lines | pp. 1128–1129 | 61 follow from the printed ratio; Figure 4(b) line 9 does not, in the paper itself |
| Total demerits, Figures 1, 2, 3 | 26,304 · 10,491 · 7,063 | 31,199 · 9,749 · 7,134 |
| Total demerits, Figure 4(a), (b), (c) | 13,218 · 10,143 · 4,030 | 14,381 · 10,449 · 4,781 |
| Worst ratio gap on any printed line | — | 0.065 |
| Feasible ways, Figure 12, tolerance 1 | 81 (p. 1163) | 86 |
| Feasible ways, Figure 14, tolerance 10 | 836,272,858 (p. 1163) | 567,763,849 |
| Liang’s eight worked words | com-put-er … su-per-cal-ifrag-ilis-tic-ex-pi-ali-do-cious (pp. 37–38) | all eight |
| Total fit against first and best fit | never worse within the tolerance (p. 1159) | never worse at 300 measures, 12 settings each |
| Total fit against this page’s first fit | — | never worse in 800 settings, measures 250 to 649 px |
The totals differ from the paper’s by up to 19 percent because the fitted widths are not the paper’s widths; the breaks do not differ. The paper’s Figure 4(b) prints a ratio of −.257 on its ninth line and 10 demerits for it, and −.257 gives 7. Both gaps are in the tests as expected failures, so a closer fit would announce itself.
One paragraph, English hyphens, spaces only