Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best word wrap algorithm? [closed]

Word wrap is one of the must-have features in a modern text editor.

How word wrap be handled? What is the best algorithm for word-wrap?

If text is several million lines, how can I make word-wrap very fast?

Why do I need the solution? Because my projects must draw text with various zoom level and simultaneously beautiful appearance.

The running environment is Windows Mobile devices. The maximum 600 MHz speed with very small memory size.

How should I handle line information? Let's assume original data has three lines.

THIS IS LINE 1. THIS IS LINE 2. THIS IS LINE 3. 

Afterwards, the break text will be shown like this:

THIS IS LINE 1. THIS IS LINE 2. THIS IS LINE 3. 

Should I allocate three lines more? Or any other suggestions? ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

like image 763
popopome Avatar asked Aug 20 '08 08:08

popopome


People also ask

What is word wrap 6?

: a word processing feature that automatically transfers a word for which there is insufficient space from the end of one line of text to the beginning of the next.

What is hard wrapping?

A soft return or soft wrap is the break resulting from line wrap or word wrap (whether automatic or manual), whereas a hard return or hard wrap is an intentional break, creating a new paragraph. With a hard return, paragraph-break formatting can (and should) be applied (either indenting or vertical whitespace).


1 Answers

Here is a word-wrap algorithm I've written in C#. It should be fairly easy to translate into other languages (except perhaps for IndexOfAny).

static char[] splitChars = new char[] { ' ', '-', '\t' };  private static string WordWrap(string str, int width) {     string[] words = Explode(str, splitChars);      int curLineLength = 0;     StringBuilder strBuilder = new StringBuilder();     for(int i = 0; i < words.Length; i += 1)     {         string word = words[i];         // If adding the new word to the current line would be too long,         // then put it on a new line (and split it up if it's too long).         if (curLineLength + word.Length > width)         {             // Only move down to a new line if we have text on the current line.             // Avoids situation where wrapped whitespace causes emptylines in text.             if (curLineLength > 0)             {                 strBuilder.Append(Environment.NewLine);                 curLineLength = 0;             }              // If the current word is too long to fit on a line even on it's own then             // split the word up.             while (word.Length > width)             {                 strBuilder.Append(word.Substring(0, width - 1) + "-");                 word = word.Substring(width - 1);                  strBuilder.Append(Environment.NewLine);             }              // Remove leading whitespace from the word so the new line starts flush to the left.             word = word.TrimStart();         }         strBuilder.Append(word);         curLineLength += word.Length;     }      return strBuilder.ToString(); }  private static string[] Explode(string str, char[] splitChars) {     List<string> parts = new List<string>();     int startIndex = 0;     while (true)     {         int index = str.IndexOfAny(splitChars, startIndex);          if (index == -1)         {             parts.Add(str.Substring(startIndex));             return parts.ToArray();         }          string word = str.Substring(startIndex, index - startIndex);         char nextChar = str.Substring(index, 1)[0];         // Dashes and the likes should stick to the word occuring before it. Whitespace doesn't have to.         if (char.IsWhiteSpace(nextChar))         {             parts.Add(word);             parts.Add(nextChar.ToString());         }         else         {             parts.Add(word + nextChar);         }          startIndex = index + 1;     } } 

It's fairly primitive - it splits on spaces, tabs and dashes. It does make sure that dashes stick to the word before it (so you don't end up with stack\n-overflow) though it doesn't favour moving small hyphenated words to a newline rather than splitting them. It does split up words if they are too long for a line.

It's also fairly culturally specific, as I don't know much about the word-wrapping rules of other cultures.

like image 136
ICR Avatar answered Sep 20 '22 18:09

ICR