0

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?

5
  • What kind of text? English prose? Commented Oct 18, 2010 at 5:25
  • @belisarius: Yes, English prose. Commented Oct 18, 2010 at 5:28
  • If you google for "Automated paragraph layout patent" you will get a few patents, some with inspiring pseudocode included. I'm not posting this as an answer because a patent is a patent, and one should not mess with it :) Commented Oct 18, 2010 at 5:54
  • c.snippets.org/browser.php#29. For some entertainment, look at the version of gcc I originally tested it with! Commented Oct 18, 2010 at 7:17
  • possible duplicate of Best word wrap algorithm? Commented Oct 18, 2010 at 16:16

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.