It seems like hours, days, and months…since I last worked on a bug. My last release was for Bug #509492. A few days ago I acquired myself a new Bug #542524. So What is the problem? Not necessary a problem but a feature.The folks at Bespin want to implement word wrap feature into the Bespin Editor. Now what the heck is word wrap? Word wrap is a feature that allows an editor to automatically shift words to the next line if the words are too large to fit onto the current line.
First step
Thanks to David Humphrey for pointing out how I should get started with this. Initially I began reading about various Algorithms used to perform word wrap and the most important factor is optimization. As much as we love to get a piece of code to do what we want it do, we do have to ask our self is it the most efficient way ? Even after speaking to Kevin Dangoor, his only requirement was to make sure that this feature did not slow down the editor!
Greedy Algorithm
SpaceLeft := LineWidth
for each Word in Text
if Width(Word) > SpaceLeft
insert line break before Word in Text
SpaceLeft := LineWidth - Width(Word)
else
SpaceLeft := SpaceLeft - (Width(Word) + SpaceWidth)
This Algorithm is called the “Greedy Algorithm” and it used by popular word processors like Microsoft Word and Open Office. This pseudo code as described goes through each word and tries to fit as many words it can on a line and continues this routine by going to the next line until there are no words left to insert.
Getting Started…?
So I get the idea of how to do this but I’m unclear as to where the code would go, I’m guessing it should be a function that should called when the editor is redrawn after each character is typed. I will ask the folks on #Bespin IRC channel for help.
In the mean time I will start playing around with the code but I’m very hungry and I need to have my dinner…So I’ll continue from here, bye:)