views:

490

answers:

4

How can I fully justify a block of text (like MS Word does, not only on the right and not only on the left but on both sides)?

I want to justify some texts (mainly arabic text) adjusted to certain screen size (some handheld device screen actually, and its text viewer doesn't have this function) and save this text as justified. So I can reload and reuse it again elsewhere.

(The problem with MS word is, that if you copy the justified text from MS Word and paste it to another editor it'll copy it un-justified).

Update : for now I'm thinking of doing it like this:

  1. get-a-word

  2. get-word-width

  3. add-word-to-total-Word and add-Word-width-to-total-word-width

  4. check if total-Word-width = myscreen-width then continue

else if total-Word-width is between myscree-wdith and (myscreen-width -3) then

add-spaces-To-total-word until it = myscreen-width

This is what I'm thinking now, but I put this question up and hope to see if there is a better solution, or somebody else already implemented it.

PS: I hope I have made my question clear and I'm sorry for bad expression if there is.

edit1 : changed the title to make it more clear.

+3  A: 

If you want to justify plain text, you can only add extra spaces to the lines to get them align on the left and right. Unfortunately the character widths differ in fonts; so doing it this way will only work for a certain font, unless you limit yourself to monospaced fonts where all characters have the same size.

If you want a result like in Word, adding spaces won't cut it. Word will not add spaces, but stretch and shrink the existing spaces. This information is lost when you copy and paste it into another app.

Either way, justifying is an optimization problem. If you are interested in a good solution and its implementation: have a look a TeX. For an implementation that works on plain text with monospaced fonts have a look at par

f3lix
im going to work only with tahome and at size of 10, nothing else.im also aware of characters size differences so im checking every characters width.
avar
TeX shows that this is not an optimization problem, but one that can be solved algorithmically. TeX justifies text lines by treating all characters as boxes and calculating the positions of these boxes according to the algorithm. In any case it's a problem of presentation, so adding spaces (altering the data) isn't really a solution.
mghie
I did not say it cannot be solved algorithmically. TeX uses dynamic programming to do it. But that does not make it less of a optimization problem, does it? And I agree: it's problem of presentation. But the questions states that the view "doesn't have this function". So the only option here is to change the data that's given to the viewer. And: yes, adding spaces is an ugly thing to do...
f3lix
A: 

The problem you are going to face is always going to be differences in the rendering of the font. Word handles full justification by adjusting kerning as well as adjusting the number of pixels between words by a few (either way). The end result is lined up both margins. This pixel adjustment is done BOTH ways, and as evenly as possible.

To properly handle this in your portable device you will have to also perform the same algorithm for the display of the text there.

If this is not possible, then the ONLY way you can even get somewhat close would be to add whitespace between words.

skamradt
+1  A: 

There are some API calls that may help:

ExtTextOut and GetCharacterPlacement

Look at the GCP_JUSTIFY flag for GetCharacterPlacement

ExtTextOut is used by Canvas.TextRect

Gerry
A: 

As has been pointed out in other answers Word does full justification by stretching the existing spaces often by very small amounts. This is only possible if you have full control over how your text is drawn on the screen (which word - or any other windows program has).

You only real option in this regard would be to implement your own text viewer on the platform you are targeting. Eg you would need to draw the text on the screen yourself (any platform that allows games should allow you to draw on the screen). However this seems like an awful lot of trouble to get justified text.

Sorry couldn't be of more help.

Toby Allen