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 refere开发者_C百科nce for coding such an algorithm?
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.
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.
精彩评论