的標準算法打破段成線很可能還是克努特&賽普拉斯的算法,由Knuth的排版系統TeX
使用。的算法,該算法「通過明智地使用動態編程的技術避免了回溯」在
描述
唐納德·E Knuth的和Michael F.賽普拉斯,軟件 - 實踐與經驗 11(1981)1119- 1184 DOI: 10.1002/spe.4380111102, 也有貨數字版式,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/
如果分數空間可以分佈,那麼不均勻空間分佈是不可避免的嗎?它們最多不同於一個像素,肉眼難以察覺。 – IVlad
請記住,我們需要確保每行中的最後一個單詞觸及正確的邊界!當然,除了最後一行。 –
是的。設'd'爲最後一個詞到右邊界的距離。讓'x'爲單詞之間的空格數('單詞 - 1')。爲每個空格添加'd/x',最後一個單詞將觸及右邊緣。如果'd'是以像素爲單位的,則必須將其餘的'd%x'像素添加到第一個'd%x'空間或隨機添加。要麼應該看起來不錯。 – IVlad