### Constructing Fonts

A font is a collection of characters (both textual and symbolic) that share common visual elements between all of the characters of one size of one type style. These visual elements can be broken up into several categories which uniquely identify the font. The major categories are the name of the typeface, the size of type, the style of type, and the weight of type.

A typeface is a unique name, usually registered, which identifies a font by the attributes of the characters in the font. Different parts of a type character such as stroke width, presence or lack of serifs, vertex shaping, are all used to distinguish one font from another. The size of type is measured in points. A point is 1/72 of an inch, so there are 72 points in one inch. The style of type typically indicates whether a font is upright or slanted, while the weight of type refers to how light or heavy the type characters are.

Most operating systems provide a default set of fonts for you to use in your applications. These fonts are shipped with the system but they vary from one operating system type to another. It is important to keep this in mind when writing portable applications because the font you select to use on one operating system may not be available on another.

The Open Class Library provides the IFont class to allow you to create and manipulate fonts within your application. This section demonstrates how you can construct and modify fonts using IFont.

There are four ways you can construct IFont objects in your application:

  Create default IFont objects
  Create IFont objects using an existing window's font
  Create IFont objects with a specific font facename and optional attributes
  Create IFont objects using an existing graphic context

#### Creating a Default IFont Object

The first way to create an IFont object is using the default constructor. This causes an object to be created that represents the system default font for the operating system.

Note: | On the Windows operating systems, this represents the stock font object SYSTEM_FONT. On the OS/2 operating systems, this represents the System Proportional font. For the AIX operating system, Courier is the default font loaded.

The following example shows how to construct a default IFont object:

       IFont defaultFont();

#### Creating an IFont Object Using a Window's Font

The second way to create an IFont object is to use a font that is currently in use by an existing window. The window containing the font is queried for the font that is in use, and the characteristics of that font are used to create another font with the same name, size and attributes. The following example demonstrates this by first creating an information area object for a frame window and then creating an IFont object from that info area. Finally, the maximum character height of the font is retrieved for this font and is used to set the height of the information area.

       IInfoArea* infoArea = new IInfoArea( this, WND_INFO );
       IFont infoFont( infoArea );
       setExtensionSize( &infoArea, infoFont.maxCharHeight() );

#### Creating an IFont Object with a Specific Name

The third way to create an IFont object is to specify an exact facename of a font along with some optional characteristics of the font such as size, type information, and graphic context. Because individual font facenames may not be available on all operating systems, this approach may not be portable across operating systems.

The highlighted lines in the following example show you how to create a font with a specific name and point size, and then how to change the point size of the text associated with visible Open Class Library objects. A Helvetica 8 point font is initially constructed. It is then modified to be a Helvetica 12 point font and applied to the title. It is then modified again to be a Helvetica 20 point font and is applied to the checkbox.

    #include <ifont.hpp>

      IFont font("Helvetica",8);

      title1.setAlignment( IStaticText::centerLeft );
      title1.setText( STR_TITLE1 );
      font.setPointSize(12);
      title1.setFont(font);

      check1.setText( STR_CHECK1 );
      font.setPointSize(20);
      check1.setFont(font);

To test the font statements, include the highlighted lines in the amcelcv.cpp file. The amcelcv.cpp file is located in the `\samples\ioc` directory.

Note that the example above demonstrates dynamically creating new fonts from existing fonts using different IFont member functions to change the characteristics of the font. There are a wide range of member functions available to use to change attributes of the font. The following is a list of the more common font attributes that can be modified:

Font facename
  The setName member function is used to modify the exact facename of the IFont
Font pointsize
  The setPointSize member function is used to modify the point size of the font.
Font characteristics
  The setUnderscore, setStrikeout, setBold, setItalic and setOutline member functions are used to modify a specific attribute of the font. These functions underline the text, draw a line through the text, change the font to have more weight, slant the font, and only draw the outline of the characters, respectfully.

For portable applications, consider using a font dialog.

#### Creating an IFont Object from a Graphic Context

The final way to construct an IFont object is to use an existing graphic context, represented by an IPresSpaceHandle. The graphic context is queried to determine the font that is currently in use and that font is then used to construct an IFont object with the same name and characteristics. The following example demonstrates this:

       IPresSpaceHandle ps = IPresSpaceHandle();
       IFont tempFont( ps );

Controls

Using Common Dialogs

IFont
IFontDialog
IMGraphic
IText