views:

63

answers:

3

I have a dialog implementation that receives a length of text from the user and I'd like to format it over multiple lines in a visually pleasing way. The user can include their own line breaks in the string which I would also like to account for.

Is anyone aware of pseudocode or something else publicly available I could use as a reference for coding such an algorithm?

A: 

If you mean basic word wrap, I found this posting an enjoyable read. If you're talking about hyphenation, full justification, and kerning... I'm coming up empty for now.

Jander
A: 

I think this is possibly a duplicate of: this question. If it isn't, then you'll find some good answers there.

Nick Fortescue
This question is a dupe; thanks for the link.
fbrereto
Use comments to indicate duplicates, not answers.
Roger Pate
@Roger, I know that, but at the time the question was vague enough that I wasn't sure it was a duplicate. I left it up to the asker to decide, which he did. And when he did I was the second vote to close.
Nick Fortescue
@Nick: That just sounds like even more reason to make it a comment (on the question) rather than answer.
Roger Pate
A: 

If you okay with random hyphenation, then solution is trivial.

Just cutting a space on word-wrap boundary is easy.

  wordwrap(line_length, input_string, output_string_list):
      offset = backward_search_for_space( input_string + line_length )
      if offset is zero ## a word taking more than a line !!
          offset = forward_search(input_string )
      append  line_length[0:offset] to ouptput_string_list
      if input_string is not null
          wordwrap( line_length, input_string + offset, string_list)

If you want non-random hyphenation ( ie. un-known is allowed, byt unk-own is not ), than you need to keep an hyphenated words list or set of rules, and modify above algorithm

if you want 'equally spaced', than after above algo, you need to take lines less than line_length, and increases spaces in the middle of lines. Easy to do

If your font is variable width, you'll need to implement the algo in physical measurement units rather than character count. It's also easy to do. A 'width' array is to be maintained, and line_length check is to be computed.

Vardhan Varma