Fonts R Us!

by Kelvin R. Lawrence

OS/2 2.1 comes with support for, and provides a selection of, both image (bitmap) fonts and Adobe type 1 outline fonts. In this article, we look at some of the considerations that you should make when using these fonts in a Presentation Manager application. We will take an overall look at the issues of dealing with fonts and explain some of the most commonly used terminology.

Plan Ahead!
Before adding font support to an application, stop and consider what type of application you are producing. You must decide whether the application is a screen-only application or if it will support other devices such as printers and plotters. This decision will affect the way that you write font-handling code in your application. Also, if your application demands WYSIWYG (what-you-see-is-what-you-get) support, then it should be using the outline fonts, which are much more flexible than image fonts.

System Image vs. Outline Fonts - Which to Use?
The image fonts that come as part of the OS/2 system should, ideally, only be used for displaying text on the screen. They consist of a set of fixed point sizes that are not scaleable and, thus, have limited flexibility. Image fonts cannot be sheared, sized, and rotated in the way that outline fonts can. For applications that just want to write to the screen they might be ideal; however, outline fonts are far better suited for more general uses.

The outline fonts, on the other hand. are far more flexible and can be used in a wide variety of ways. You can use them effectively both on the screen and hardcopy devices because they can be sized accurately with graphics, such that words and pictures appear in the same scale. In addition, they have more flexible uses such as producing outlines, constructing fills, using paths, and constructing clip paths. Outline fonts also can be transformed just like any other graphical primitives; they can be accurately rotated, scaled, and sheared.

Some General Font and Typeface Terminology
When discussing fonts, and typefaces in general, it is useful to define some of the common terminology. This terminology is not unique to the computer industry; almost all of the basic font-related terminology comes from the printing and typesetting world. You can open almost any book in the art section of your local bookstore that deals with typesetting and find a good explanation of the basic terms. In fact, I would recommend people new to working with fonts do just that.

The Font Metrics Structure
Given this diagram, we can look at the font metrics (FONTMETRICS) structure as used by the GpiQueryFonts function. This structure definition can be found in the OS2DEF.H header file that is part of the Developer's Toolkit for OS/2. As you can see, this structure contains all of the relevant information, or metrics. about a particular font. ''Figure 1. Font Metrics''

So, What Fonts Do I Have?
To determine the fonts that currently are loaded in the system, an application can call the GpiQueryFonts function. Use this function to query how many fonts are available and to retrieve the metrics for some, or all, of these fonts. The function, MyQueryFonts, calculates how many fonts are loaded in the system, allocates space to store the font metrics, and retrieves the metrics. An application can query information about just the public (that is, system-wide) fonts or private fonts (that is, local to the application) or both. Do this by using one or both of the QF_PUBLIC and QF_PRIVATE flags on the call. ''Sample Code 1. My Query Fonts function''

Having queried the list of available fonts, the application can present the user with a list and have them choose a font to use for a particular task. However, as the following section indicates, OS/2 provides a standard dialog interface for choosing fonts that all applications should use. This lets each program offer a consistent interface for selecting fonts. More importantly than using the metric information to generate lists of the available fonts in the system, applications can use the information in the FONTMETRICS structure to calculate such things as line and character spacing.

Another Structure
The font attributes (FATTRS) structure as used by the GpiCreateLogFont</tt> and the WinFontDlg</tt> functions display the font selection dialog. The FATTRS</tt> structure also is defined in the OS2DEF.H header file. ''Figure 2. The Font Attributes structure''

A Consistent Dialog
Offering a list of fonts to choose from is something that many Presentation Manager applications must do. Rather than have each application programmer invent a new set of dialogs for doing this, we provide a standard interface as part of the base system. Use the WinFontDlg</tt> function to display the dialog. Although can be a very complex function (at first look), it is actually very flexible. The best way to get familiar with the power offered by the font dialog is to experiment with it in your applications.

However, at a minimum, the function DisplayFontDialog</tt> shows how to set up a call to WinFontDlg</tt> having specified only a few of the many parameters that are available. The WinFontDlg</tt> call expects to receive a FONTDLG</tt> structure as one of its parameters. This is a large structure; therefore, only those parts of the structure needed in the example are filled in by the DisplayFontDlg</tt> function. ''Figure 3. The Font Dialog structure All of the flags and the FONTDLG structure are defined in the PMSTDDLG.H'' header file, which is part of the Developer's Toolkit for OS/2 2.1. The following function displays the font dialog: ''Sample Code 2. The DisplayFontDlg function''

Upon return from a call to WinFontDlg</tt>, the fAttrs</tt> field of the font dialog structure contains the font attributes for the font that was selected by the user. These attributes, then, can be passed to the GpiCreateLogFont</tt> function to create a logical font representing the font chosen by the user.

Creating a Logical Font and Displaying Text
Having established the font attributes structure for the font that we wish to display text in, before we can actually display the text, create a logical font for the selected font and establish that font as the current character set for our presentation s pace. The function CreateFontAndDisplayIt</tt> creates a logical font, using the fAttrs</tt> structure returned from the call to WinFontDlg</tt> and displays the string "Hello!" using that font. ''Sample Code 3. A function to create a font, then display it.''

If the Font Fits...
The CreateFontAndDisplayIt</tt> function uses a somewhat inaccurate calculation to approximate the size that the character box should be set to if the user selects an outline font. For much more exact sizing of an outline font for display on a specific device, you can use the following algorithm:
 * 1) Choose the point size required
 * 2) Apply the following formula:
 * 3) * pt = selected point size
 * 4) * dev = device graphics resolution in pels per meter
 * 5) * mi = 0.0254 (meters/inch)
 * 6) * ip = 0.9961/72 (inches/point)
 * 7) * character box = pt * dev * mi * ip

Then, use the <tt>GpiSetCharBox</tt> function to set the character box accordingly.

The formula uses the following basic rules from the typesetting world:
 * 12 points = 1 pica
 * 6 picas = 1 inch (actually 0.9961 inches)
 * 72 points = 1 inch
 * 1 inch = 2.54 cm
 * 1 inch = 0.0254 m

And, That's Not All!
In this article, we have only touched on some of the many features that OS/2 Presentation Manager provides for manipulating and displaying text characters in different fonts. As always, the best way to get familiar with these interfaces is to try them out in your own programs. Complete sample programs can be found on your accompanying Developer Connection for OS/2 CD-ROM.

In a future article, we will look at some more complex ways to use fonts in your applications.