将段落分成行的标准算法可能仍然是 Knuth 的排版系统使用的 Knuth & Plass 的算法TeX
。该算法“通过明智地使用动态编程技术避免回溯”在
Donald E. Knuth 和 Michael F. Plass,软件 - 实践和经验11 (1981) 1119-1184 DOI:10.1002/spe.4380111102,也可在Digital Typography中找到,Ch。3,第 67-155 页。
该算法基于考虑每个可能的换行符,从段落的开头开始,并为每个换行符找到之前的换行符序列,以提供迄今为止最好的结果。由于整个序列由序列中的最后一个换行符确定,因此在添加新的潜在断点时,只需考虑当前行的潜在起点,从而产生有效的算法。
该算法的简化版本(例如没有连字符)可以这样描述:
Add start of paragraph to list of active breakpoints
For each possible breakpoint (space) B_n, starting from the beginning:
For each breakpoint in active list as B_a:
If B_a is too far away from B_n:
Delete B_a from active list
else
Calculate badness of line from B_a to B_n
Add B_n to active list
If using B_a minimizes cumulative badness from start to B_n:
Record B_a and cumulative badness as best path to B_n
The result is a linked list of breakpoints to use.
The badness of lines under consideration can be calculated like this:
Each space is assigned a nominal width, a strechability, and a shrinkability.
The badness is then calculated as the ratio of stretching or shrinking used,
relative to what is allowed, raised e.g. to the third power (in order to
ensure that several slightly bad lines are prefered over one really bad one)
可在http://defoe.sourceforge.net/folio/knuth-plass.html找到图解说明
网络上有各种语言的实现,例如Bram Stein 在 Javascript 中的实现:http ://www.bramstein.com/projects/typeset/