Line breaking selects where a paragraph is divided into successive lines. The choice combines the widths of words and spaces with a measure of acceptable layout. A glyph advance supplies part of the underlying geometry, while the chosen breaks determine which content shares a CSS line box in a web layout.
A greedy algorithm can put as much content as possible on each line before proceeding. Greedy line breaking is easy to compute, but an early choice can leave an awkward short line later. Line breaking with squared slack cost gives a small, explicit example. Suppose four word widths are , each internal space has width 1, and the permitted line width is 7, all in arbitrary consistent units. Greedy grouping produces line lengths . An alternative grouping produces . Charging squared unused width on every line except the last gives
An immediately fuller first line can therefore produce a worse paragraph under this particular cost.
Figure 1.
Greedy line breaking and a globally optimal choice under squared slack cost
. Original toy example with word widths 3,2,2,5, fixed spaces of width 1 and line width 7. Squared unused-width cost excludes the final line. The generator computes both the greedy grouping and the optimum by dynamic programming. Details.
Dynamic programming makes the global choice systematic. Let be the minimum cost for the first words, and let include words through and their internal spaces. For feasible lines, set , except that for the final line. Then
Possible break positions form a directed acyclic graph; a feasible line is an edge, so the same calculation finds a shortest path from the start to the end of the paragraph.
Donald E. Knuth and Michael F. Plass developed a richer paragraph-wide method using boxes, adjustable glue and penalties. Their Knuth-Plass line-breaking algorithm accommodates typographic choices beyond the fixed spaces and squared costs in the illustrative model above.[ref]

Articles by others on the same topic (0)

There are currently no matching articles.