.
In Depth Banner
Skip Navigation Links

Select your preferred language

TextFormatter Part 3.

Perviously, you saw how a paragraph of text was broken down into its constituent parts namely, white space and words and how that information was stored in a list that was then subsequently used to reconstruct paragraph by arranging it between two defined margins.  You also saw how individual lines could be laid out in a left justified style.  This time we'll wrap up the article by examining how the lines we've already analyzed can be laid out with the other three justification formats such as right, centered and fully justified. 

The software has already provided us with a collection of lines containing words and white space which, when measured altogether, wide as many text entities as are required to fill the specified column. in the case of two of the three required formats, right and center justified, the line just needs to be positioned in the correct place on the page. in the case of full justification each word and white space must be dealt with separately to ensure that the beginning of the first word is aligned with the left margin at the end of the last word is aligned with the right margin and any white space interposed between them is expanded to fill the gap and pad-out the lines.  Figure 1 illustrates this principle with four format styles shown. The red boxes indicate where whitespace is added or expanded.

Figure 1. Formatting is a matter of space.

Centered text.

This is a simple matter of measuring the line length of all text and starting the line at 1/2 the difference between the line length and the column width from the left edge of the page. To ensure the line is measured properly, leading and trailing whitespace is removed by the TrimStart and TrimEnd methods. Once a line is lean and trim it can be measured and the difference between the length of the line and the width of the column ascertained. This method is called by the DoJustify method to center a line.

Right justified text

Write justification is as easy as subtracting the line length from the column width.  The resulting figure is the amount by which the first word in the line must be offset to ensure that the last word in the line rests neatly on the right margin. Once again, unneeded white space is trimmed from the lines.  The method which performs right justification is shown here.   

Fully justified text.

This is the subject that everyone asks about.  The principle however is very simple. All you have to do to fully justify a line is measure the length of the line and its white space characters, subtract this value from the width of the column and the resulting figure is divided by the number of white space characters in line and then each of those white space characters is made larger by this value.  Effectively, each of the white space characters is padded out to ensure that the last non white space character in the line just touches the right margin.  This listing shows the line justification code.  Note how the code checks to see if the line contains a new line character and left justifies if it does.  This creates a nicely formatted paragraph and does not allow the final line to have words that are two widely spaced.  Without this a paragraph having three words on the final line looks very odd indeed. 

Centered text.

This is a simple matter of measuring the line length of all text and starting the line at 1/2 the difference between the line length and the column width from the left edge of the page. To ensure the line is measured properly, leading and trailing whitespace is removed by the TrimStart and TrimEnd methods. Once a line is lean and trim it can be measured and the difference between the length of the line and the width of the column ascertained. This method is called by the DoJustify method to center a line.

Right justified text

Write justification is as easy as subtracting the line length from the column width.  The resulting figure is the amount by which the first word in the line must be offset to ensure that the last word in the line rests neatly on the right margin. Once again, unneeded white space is trimmed from the lines.  The method which performs right justification is shown here.   

Fully justified text.

This is the subject that everyone asks about.  The principle however is very simple. All you have to do to fully justify a line is measure the length of the line and its white space characters, subtract this value from the width of the column and the resulting figure is divided by the number of white space characters in line and then each of those white space characters is made larger by this value.  Effectively, each of the white space characters is padded out to ensure that the last non white space character in the line just touches the right margin.  This listing shows the line justification code.  Note how the code checks to see if the line contains a new line character and left justifies if it does.  This creates a nicely formatted paragraph and does not allow the final line to have words that are two widely spaced.  Without this a paragraph having three words on the final line looks very odd indeed. 

Summary

This just about wraps up the TextFormatter articles.  The code presented here which encapsulates the functionality described is all that's needed to display text in the four formats.  You will find that placing a TextPanel on a form and docking it to fill the form produces a nice demonstration of how the TextFormatter is able to analyze the lines and rearrange them very quickly.  This code has been tested in production situations for several years and has been used to format text in many applications and has always performed well, even with large amounts of text to reformat. 

You can find the Source Code files here.

Back to Part2.

Return to the main index

Copyright © Bob Powell 2000-.  All rights reserved.