.
In Depth Banner
Skip Navigation Links

Select your preferred language

TextFormatter.

Previously we looked at general principles of typography and saw how to roughly break down a sentence or paragraph into it's component words. In order to lay-out and format text correctly the whitespace and formatting such as tabs and carriage returns have to be preserved too so in this article the business of analysing the text and recognizing that important information will be explored.

The whitespace in a piece of text is often as important as the words, so each portion of the text has to be preserved, even the spaces, tabs and carriage returns. To do this, the text needs to be broken down into a list containing all the original elements of the text but in a form that enables each element to be measured and treated as a separate entity.

In this implementation of the TextFormatter, words and whitespace are analysed and stored in a collection of word-position objects. The objects maintain the word or the whitespace, it's position in the text both relative to other text entities and physically on a page and it's physical size which is important for deciding how a paragraph is to be laid out.

This Listing shows the WordPos class.

The WordPos objects are stored in a WordPosCollection that is used to maintain a block of text in the broken-down and analysed form.

This listing shows the WordPosCollection

Having a class that stores words and whitespace and a collection in which to store them, the next task is to analyse a block of text to transform a paragraph into the word and whitespace data.  The method shown in this listing breaks down a string of text preserving spaces, tabs and carriage returns while treating any group of non white-space characters as words.  Each of these entities is stored in a WordPos class and the text content measured using the current font so that the physical size of the portion of text can be ascertained. 

 

Text layout. 

At this stage it's possible to begin reconstructing the text so that it can be laid out on screen or printer.  Text is always laid out between two margins that provide the physical column-width and so, depending upon font size, a particular piece of text may produce a very number of lines containing word and white space entities that fit between the boundary of these margins.  The first task of any layout operation is to take text objects and one at a time and collect them together into lines that, without any other formatting, will fit between the margins.  This method performs this task.  As each line is gathered, it is justified according to the current style. Effectively, justification involves adjusting the white-space sizes so that words are spaced appropriately. The DoJustify method calls one of four routines that perform the actual justification of a line. In this part, only the left justification is implemented.

The simplest layout task is to reconstruct the text line by line and place it between the margins with left-justification.  Left justification is accomplished by placing the first entity on the left margin and placing all subsequent objects in the line immediately after each other using the measurements provided when the text was analyzed as seen in this code snippet.

You'll notice that whenever a line is justified, the page positions are re-calculated so that each word and white-space entity can store it's own lateral page position. This is accomplished using this method.

Control the functionality.

Now everything is in-place to enable the analysis and re-positioning of text all that's needed is a visual control that can use the functionality on a form. The TextPanel class maintains a Formatter object that manages the word list and provides a simple interface which is design-time enabled. The class is based on the standard Panel object and treats the edges of the control as the text margins thereby formatting text within it's own boundaries. It also has the abiliy to display the extents of the white-space so that you can see how layout is being performed. This will come in handy later when we get to the full justification system.

This listing shows the TextFormatter class.

Wrapping up.

The demonstration application uses the TextPanel control on a form, filling the Text property with the good old "Lorem ipsum..." test paragraph. This application demonstrates the use of the TextPanel object and figure 1 shows the application in action.

Figure 1. The TextFormatterTest application in action.

Summary.

In this article you've seen how to break a sentence or paragraph into it's component parts, analyse them using the chosen font as a guideline and create a data structure that manages this information as a collection of word position entities. Then you saw how this information can be used to reconstitute the text as a collection of lines that fit within a given set of page-margins.

In the next part you will see how to extend this functionality to include centered text, right justified and fully justified paragraphs with flexible word-spacing.

You can find the Source Code files here.

Back to Part1.

Read on....

Return to the main index.

Copyright © Bob Powell 2000-.  All rights reserved.