Athena: Word Wrap

Athena: Word WrapOne thing I hate doing is writing classes that will output text, as well as handle special character sequences that get replaced by images. And while all this is happening, having it word wrap as well.

So I wrote a quick word wrap template class today that will do this. It doesn’t give line spacing yet, which I’m still unsure whether or not it should be this.

The way it works is when you create the word wrap object, you can pass to it’s constructor a value for the maximum width a line can be, this can be a floating point value, or an integer (it defaults to integer).

Then you call Parse() and give it a string, it will go through that string, checking the width of characters until the accumulated character widths are greater than the maximum width, or a new line is found. It will output this line of characters and move onto the next bit.

If it finds a word that is longer than maximum width, it will do one of two things. First if it is at the start of a newline, it will cut the word at the point it goes over. If it is already part way into a line, it will cause a new line and put the word on the next line.

I’ve included with this blog entry the header for the word wrap class, it comes as part of a test project in Xcode (it uses the Lorem Ipsum text), but the source and headers should work in another C++ IDE.

Athena Word Wrap Source (Version 1.0.0) (358 downloads)