42 points · by Tomte · 8y ago · 16 comments
The author's assertion is true, the example data just doesn't show it although it looks like it does because of the extra marking.
xxxx x xxxx xxxxx xx xx xxxxx x:
xx xxxxxxx xxxxx xxx xxxx xxx :
xxxxx x x xxx xxxx xxx xx xx :
xxxxxxx x xxxx xx xxxx xxx xxx :
xx
xxxx x xxxx xxxxx xx xx xxxxx :
x xx xxxxxxx xxxxx xxx xxxx xxx:
xxxxx x x xxx xxxx xxx xx xx :
xxxxxxx x xxxx xx xxxx xxx xxx :
xx
Note that in reality the paragraphs will not be typeset as above, but the spaces will be stretched or shrunk so that the lines have identical width.The reason the first could be judged as better is that TeX has penalties (\adjdemerits) when consecutive lines belong to "incompatible fitness classes" (e.g. very loose lines following very tight lines), so it may be possible that the first paragraph can count as (tight, loose, loose, loose), and the latter as (loose, tight, loose, loose), which is worse.
https://android.googlesource.com/platform/frameworks/minikin...
PS: I started collecting a few of the more obscure implementations of TeX here (https://github.com/tex-other) — please let me know if there are more that I missed (other than the popular ones: pdfTeX/XeTeX/LuaTeX which are in TeX Live / MiKTeX and don't require archival). Among these, there are JavaScript and C implementations of this algorithm.
The trouble here isn't just the design of the algorithm. The specification itself is bad. The moment you start to mess with the kerning (spacing, both between letters and otherwise) specified by the font designer, you've gone wrong. The font ships with correct kerning. That kerning is what the font designed determined would look best and be most readable. Regular letter spacing also helps with readability. Hyphens are also trouble for readability.
What the Knuth-Plass algorithm is concerned with is merely where to choose line breaks. For justified paragraphs (as in any competently typeset book you can pick up), the space between words necessarily varies from line to line (barring some staggering coincidence). What the algorithm tries to do is break lines such that this inter-word space is as close as possible to the ideal specified in the font (and to minimize the amount of hyphenation required), and at this it does a better job than other algorithms (those that look at only one line at a time).
See Figure 4 in the paper: http://www.eprg.org/G53DOC/pdfs/knuth-plass-breaking.pdf#pag... (page 1130, the 12th page in the PDF).
I wanted to get a simple and fast solution into a one line regex a while back with the simple premise of: breaking on spaces before n chars, i was quite happy with it [1]:
const wrap = (s) => s.replace(
/(?![^\n]{1,32}$)([^\n]{1,32})\s/g, '$1\n'
);
...this looks like a child drawing compared to the article, but then i'm heavily biased towards simplicity over sophistication.[1] https://stackoverflow.com/questions/14484787/wrap-text-in-ja...