Live data from Hacker News

Knuth and Plass line breaking algorithm in JavaScript

bramstein.com

21–30 of 44 posts

Re: Knuth and Plass line breaking algorithm in JavaScript

#21
post #14
post #11

The web is now twenty and browsers are still incapable of something as basic and commonplace as hyphenation and justification. It’s a real shame that this problem has to be solved with JavaScript in 2010. How old is TeX again?

I've been asking for years why browsers do not have this. The only reply I've gotten is for performance considerations, which is a bad answer for several reasons.

It sounds like a difficult thing to do correctly for any given language. I'd probably procrastinate it too.

Re: Knuth and Plass line breaking algorithm in JavaScript

#22
post #14

Earlier quoted context omitted.

I've been asking for years why browsers do not have this. The only reply I've gotten is for performance considerations, which is a bad answer for several reasons.

Internet Explorer actually has this through the (almost standardized) text-justify CSS property. It still doesn't do hyphenation, but Hyphenator.js ( http://code.google.com/p/hyphenator/ ) fills that gap pretty nicely. Performance isn't a good argument in my opinion. The algorithm isn't that expensive. The most expensive part right now is retrieving all the text metrics, but you would get that a lot cheaper in the br…

Is Internet Explorer’s justification done for whole paragraphs using a reasonable algorithm, or just line by line?

Re: Knuth and Plass line breaking algorithm in JavaScript

#24
post #20
post #14

Earlier quoted context omitted.

I've been asking for years why browsers do not have this. The only reply I've gotten is for performance considerations, which is a bad answer for several reasons.

Another reason I've heard before was internationalization. Not sure how many languages have draconian hyphenation rules like English though.

English hyphenation rules are very simple and relaxed by continental standards. It is about the only language where such a simple and straightforward algorithm as the one in TeX (it doesn't even contain a full list of word stems and a rule engine for the (de)construction of composite words) can work.

Re: Knuth and Plass line breaking algorithm in JavaScript

#26
post #24
post #20

Earlier quoted context omitted.

Another reason I've heard before was internationalization. Not sure how many languages have draconian hyphenation rules like English though.

English hyphenation rules are very simple and relaxed by continental standards. It is about the only language where such a simple and straightforward algorithm as the one in TeX (it doesn't even contain a full list of word stems and a rule engine for the (de)construction of composite words) can work.

>> English hyphenation rules are very simple and relaxed by continental standards.

Really? From my experience, ESL students often don't understand the logic behind english syllables.

My point though, is that if different languages hyphenate differently and we're talking about sites with international user generated content (Facebook or Orkut come to mind), then it's not exactly trivial to hyphenate correctly.

Re: Knuth and Plass line breaking algorithm in JavaScript

#27
post #26
post #24

Earlier quoted context omitted.

English hyphenation rules are very simple and relaxed by continental standards. It is about the only language where such a simple and straightforward algorithm as the one in TeX (it doesn't even contain a full list of word stems and a rule engine for the (de)construction of composite words) can work.

>> English hyphenation rules are very simple and relaxed by continental standards. Really? From my experience, ESL students often don't understand the logic behind english syllables. My point though, is that if different languages hyphenate differently and we're talking about sites with international user generated content (Facebook or Orkut come to mind), then it's not exactly trivial to hyphenate correctly.

I was told by my English teacher to just not hyphenate because the rules are crazy and inconsistent. Might be a British English thing, though.

Re: Knuth and Plass line breaking algorithm in JavaScript

#28
post #14

Earlier quoted context omitted.

I've been asking for years why browsers do not have this. The only reply I've gotten is for performance considerations, which is a bad answer for several reasons.

Internet Explorer actually has this through the (almost standardized) text-justify CSS property. It still doesn't do hyphenation, but Hyphenator.js ( http://code.google.com/p/hyphenator/ ) fills that gap pretty nicely. Performance isn't a good argument in my opinion. The algorithm isn't that expensive. The most expensive part right now is retrieving all the text metrics, but you would get that a lot cheaper in the br…

I also looked into it recently, and Damon from the Gnome project attempted it in 2002 or so. Also Adobe+Google are trying to get it into WebKit. The problem (as far as I can tell) is that people have tried to do it all at once, and pushing such a large thunk of code upstream is very hard.

I suspect that if you take it in pieces: first get a decent hyphenation algo into Pango, then get that into FF and WebKit, then work on the line-breaker, and then get a new CSS rule approved by the W3C... well, maybe you could get it done in 3 or 4 years.

Re: Knuth and Plass line breaking algorithm in JavaScript

#29
Great work! The output is beautiful. Looking forward to this being a standard library in the future. Do you know how well it might handle cases of text that already has some formatting?

Also: It blows up in IE9 for me though, many of the lines go on for quite a ways.

Re: Knuth and Plass line breaking algorithm in JavaScript

#30
post #28

Earlier quoted context omitted.

Internet Explorer actually has this through the (almost standardized) text-justify CSS property. It still doesn't do hyphenation, but Hyphenator.js ( http://code.google.com/p/hyphenator/ ) fills that gap pretty nicely. Performance isn't a good argument in my opinion. The algorithm isn't that expensive. The most expensive part right now is retrieving all the text metrics, but you would get that a lot cheaper in the br…

I also looked into it recently, and Damon from the Gnome project attempted it in 2002 or so. Also Adobe+Google are trying to get it into WebKit. The problem (as far as I can tell) is that people have tried to do it all at once, and pushing such a large thunk of code upstream is very hard. I suspect that if you take it in pieces: first get a decent hyphenation algo into Pango, then get that into FF and WebKit, then wo…

W3C [...] 3 or 4 years; don't hold your breath.
Post reply on HN