Description and implementation of the core Knuth-Plass line-wrapping algorithm
github.com
github.com
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).
1. The opinion is certainly a radical one, i.e. at odds with almost every typographer. Walk into a library and pick up a random book or magazine (by a good publisher), and you'll almost surely find justified paragraphs. It's only some self-published books or typewriter / computer printouts that tend not to justify. It is an interesting exercise to try to find a physical book (that you have access to) that contains non-justified paragraphs — I looked at 65 books at home (that have paragraphs) and all of them (across multiple languages) use justification. (In the poetry books I had to look at the preface...)
I checked previews online of some books by typographers and font designers, and (of course) they also contain justified paragraphs: Robert Bringhurst's The Elements of Typographic Style, etc. I am aware of Eric Gill's An Essay on Typography that is not, and contains a section called ”The Procrustean Bed”, and that's about it.
2. The problem being solved, for which the algorithms are being compared, is how to generate justified paragraphs. The top-level comment (by pgtan@) that started this thread, was also about justified paragraphs (that's what I was pointing out in the comment you replied to). As an analogy, when discussing algorithms for the traveling salesman problem, one can reject the problem by declaring that sales must be done online, which... seems somewhat a change of topic.
3. Even if what you want is non-justified (ragged-right) paragraphs, you still need to choose line breaks. The choice is not always unique. The Knuth–Plass algorithm covers this case too (with the right choice of penalties and glue), and has something to offer here too. It's in the paper :-) http://eprg.org/G53DOC/pdfs/knuth-plass-breaking.pdf (Search for "ragged")
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...