Hi,

I am looking for a text justification algorithm that fully justifies the text. I suppose that I could design one of my own. However, why reinvent the wheel?

I tried googling and doing other stuff, but it surprisingly was unable to find anything useful.

Any help will be greatly appreciated.

Recommended Answers

All 4 Replies

I have a very basic solution:

Let's say you have a text field that supports 50 characters and your text is only 20 characters. You could get the text length, subtract that from the amount of characters the text field supports, and divide by two. Once you have that, then you can add that number of spaces at the beginnning.


Of course that solution doesn't work for everything, and if possible it's better to use some other idea.

There are various text justification algorithms, some better than others. For example, one simply asks, "How many words can I fit on the first line?" and then spreads those words evenly on the first line. Then it looks at the rest of the words.

A smart text justification algorithm does more to prevent abnormally poorly spaced lines, using a dynamic programming algorithm. I have never looked at the algorithm; I just know that's what it uses.

TeX uses the 'smart' kind of algorithm, so take a look at http://tex.loria.fr/tex-source/tex-source.html

This is a HUGE topic. I work in printing/publishing, and code PostScript, so I've written my share of text justificaiton routines.

Simple approach: adjust the whitespace between words

Better approach: adjust the majority of the whitespace between words, the rest between individual spaces. I use 80/20, it seems to be the most balanced.

Sophisticated approach: add hyphenation. <-- may take years to get right!

Sophisticated approach: add hyphenation. <-- may take years to get right!

Heh. If I ever see hyphenation like quantifier-
s again, well, I guess nothing bad will happen, but we'll see :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.