Home

Accessories for Controls: Fonts

 

Introduction to Fonts

 

Introduction

A font is a technique of representing symbols drawn on an object. A font is designed by an artist but usually follows a specific pattern. For example a font designed to produce symbols readable in the English language must be designed by a set of predetermined and agreed upon symbols. These English symbols are grouped in an entity called the English alphabet.

When designing such a font, the symbols created must conform to that language. This also implies that one font can be significantly different from another and a font is not necessarily a series of readable symbols.

Just like everything else in the computer, a font must have a name. To accommodate the visual needs, a font is also designed to assume different sizes.

Before using a font, it must have been installed. Microsoft Windows installs many fonts during setup. To handle its various assignments, the operating system uses a particular font known as the System Font. This is the font used to display the menu items and other labels for resources in applications. If you want to use a different font to draw text in your application, you must select it.

 

 

Font Creation or Selection

The Win32 library makes fonts available through the HFONT handle. The VCL provides font support through the TFont class. The TFont class is derived from the TGraphicsObject class. Both classes are members of the Graphics.hpp header file. The TGraphicsObject class is derived from TPersistent.

To create a font, you can declare a TFont variable. If you want to use a temporary font in an event, you can declare the variable locally. Like every TObject descendent, a TFont variable must be declared using the new operator. Here is an example:

procedure TForm1.btnFontClick(Sender: TObject);
var NewFont : TFont;
begin
    NewFont := TFont.Create;

    Font := NewFont;
end;

If you plan to refer to the same font object in more than one event, you should declare it globally in the header file of the parent object that will make it available to necessary controls.

After declaring a TFont variable, you must initialize it. This can be done by assigning the desired values to its member variables. You do not have to specify a value for each characteristic of the font. If you omit a property, its default value would be used.

If the system or you has (have) created or selected a font, you can use it to initialize another font variable. To support this, the TFont class inherits the Assign() method from TPersistent. Its syntax is:

procedure Assign(Source: TPersistent); virtual;

The Source parameter is the returned new font. After the font variable calls it, it would hold the same characteristics of the existing font.

Characteristics of Fonts

 

Introduction

Although sometimes represented as if it were one entity object, a font can be a complex concept made of various characteristics such as its width, weight, and height, etc. Therefore, to make better use of fonts, you should be familiar with their appearance, especially if you plan to perform any artistic text drawing.

The Name of a Font

The name of a font is the most commonly used characteristic. It is used by the operating system and the application to identify it. The names of fonts installed on your computer can be seen in the Fonts window accessible from Control Panel:

To identify a font, the TFont class is equipped with a property named Name:

property Name: TFontName read GetName write SetName;

To use a particular font, assign its name to the Name property of your TFont object. Here is an example:

procedure TForm1.btnFontClick(Sender: TObject);
var NewFont : TFont;
begin
    NewFont := TFont.Create;

    NewFont.Name := 'Garamond';
end;

If you are specifying a font other than the default to use in your application, you should use only the most popular fonts that are more likely to be found on your user's computers.

The Height of a Font

The height of a font is a measure of the height used to represent its characters. It is represented by the Height property of the TFont class:

property Height: Integer read GetHeight write SetHeight;

The Size of a Font

The font size is the dimension of characters used to represent the font on a device context. It is specify using the TFont.Size property:

property Size: Integer read GetSize write SetSize;

The Style of a Font

The style of a font controls how the font displays, in normal, italicized, underlined, stroke out, some of these characteristics or all of them. The VCL manages these properties as a set; meaning you can build them, add those you want or retract those you do not need. To support font styles, the TFont class is equipped with a property named Style:

property Style: TFontStyles read GetStyle write SetStyle;

Font styles are implemented through the TFontStyles property. The available characteristics are as follows:

Characteristic Value Example
Bold fsBold This text is bold
Italic fsItalic Italicized section
Underline fsUnderline The words are underlined
Strikeout fsStrikeOut Stroke out but happy

To control the style of font, you must call TFontStyles and add or subtract a style. Here is an example:

procedure TForm1.btnFontClick(Sender: TObject);
var fntVerdana : TFont;
begin
    	fntVerdana := TFont.Create;

	fntVerdana.Name := 'Verdana';
	fntVerdana.Size := 10;
	fntVerdana.Style := [fsBold];
end;

The TFont class provides support for colors as it is equipped with a property named Color:

property Color: TColor read FColor write SetColor;

Therefore, to specify the color of a font, assign the desired value to the TFont.Color property. Here is an example:

procedure TForm1.btnFontClick(Sender: TObject);
var fntVerdana : TFont;
begin
    fntVerdana := TFont.Create;

	fntVerdana.Name := 'Verdana';
	fntVerdana.Size := 10;
	fntVerdana.Color := 7027305;
	fntVerdana.Style := [fsBold];
end;
 

The Win32 Handle of a Font

As the main library of Windows applications, Win32 provides font support through various functions. Most of these functions return an HFONT value. To use them, call any of the Win32 functions and make sure you retrieve its return value. To support the Win32 HFONT, the TFont class is equipped with a property named Handle:

__property HFONT__ * Handle = {read=GetHandle,write=SetHandle};
 

Creating a Font in Win32

As the main library of Windows applications, Win32 provides font support through various functions. Most of these functions return an HFONT value. To use them, call any of the Win32 functions and make sure you retrieve its return value. To support the Win32 HFONT, the TFont class is equipped with a property named Handle:

__property HFONT__ * Handle = {read=GetHandle,write=SetHandle};

One of the most complete means of creating a font is by using the CreateFont() function. Its syntax is:

HFONT CreateFont(int nHeight,
		 int nWidth,
		 int nEscapement,
		 int nOrientation,
		 int fnWeight,
		 DWORD fdwItalic,
		 DWORD fdwUnderline,
		 DWORD fdwStrikeOut,
		 DWORD fdwCharSet,
		 DWORD fdwOutputPrecision,
		 DWORD fdwClipPrecision,
		 DWORD fdwQuality,
		 DWORD fdwPitchAndFamily,
		 LPCTSTR lpszFace);

The nHeight parameter is the height of a small rectangle in which a character of this font would fit.

The nWidth value is the average width of characters of this font. If you know the width to apply, then you can pass it as this argument. If not, pass it as 0. In this case, the system will choose the closest value to be applied on the text.

The nEscapement parameter is the angle used to orient the text. The angle is calculated as a multiple of 0.1, oriented counterclockwise and provided in degrees.

The nOrientation parameter is the angular orientation of the text with regards to the horizontal axis.

The fnWeight parameter is used to attempt to control the font weight of the text because it is affected by the characteristics of the font as set by the designer. It holds values that displays text from thin to heavy bold. The possible values are:

Constant Value   Constant Value
FW_DONTCARE 0   FW_THIN 100
FW_EXTRALIGHT 200   FW_ULTRALIGHT 200
FW_LIGHT 300      
FW_NORMAL 400   FW_REGULAR 400
FW_MEDIUM 500      
FW_SEMIBOLD 600   FW_DEMIBOLD 600
FW_BOLD 700      
FW_EXTRABOLD 800   FW_ULTRABOLD 800
FW_BLACK 900   FW_HEAVY 900

The fdwItalic value specifies whether the font will be italicized (TRUE) or not (FALSE).

The dwbUnderline value is used to underline (TRUE) or not underline (FALSE) the text.

The fdwStrikeOut value is specifies whether the text should be stroke out (TRUE) or not (FALSE) with a (horizontal) line.

The fdwCharSet parameter specifies the character set used. The possible values are: ANSI_CHARSET, BALTIC_CHARSET, CHINESEBIG5_CHARSET, DEFAULT_CHARSET, EASTEUROPE_CHARSET, GB2312_CHARSET, GREEK_CHARSET, HANGUL_CHARSET, MAC_CHARSET, OEM_CHARSET, RUSSIAN_CHARSET, SHIFTJIS_CHARSET, SYMBOL_CHARSET,
TURKISH_CHARSET, HEBREW_CHARSET, and THAI_CHARSET.

The fdwOutPrecision parameter controls the amount of precision used to evaluate the numeric values used on this function for the height, the width, and angles. It can have one of the following values: OUT_CHARACTER_PRECIS, OUT_DEFAULT_PRECIS, OUT_DEVICE_PRECIS, OUT_OUTLINE_PRECIS, OUT_RASTER_PRECIS, OUT_STRING_PRECIS, OUT_STROKE_PRECIS, OUT_TT_ONLY_PRECIS, and OUT_TT_PRECIS.

The fdwClipPrecision parameter is used to specify how some characters may be drawn outside of the area in which they are intended. The possible values used are CLIP_DEFAULT_PRECIS, CLIP_CHARACTER_PRECIS, CLIP_STROKE_PRECIS, CLIP_MASK, CLIP_EMBEDED, CLIP_LH_ANGLES, and CLIP_TT_ALWAYS.

The fdwQuality parameter specifies how the function will attempt to match the font's characteristics. The possible values are ANTIALIASED_QUALITY, DEFAULT_QUALITY, DRAFT_QUALITY, NONANTIALIASED_QUALITY, and PROOF_QUALITY.

The fdwPitchAndFamily parameter specifies the category of the font used. It combines the pitch and the family the intended font belongs to. The pitch can be specified with DEFAULT_PITCH, VARIABLE_PITCH, or FIXED_PITCH. The pitch is combined using the bitwise OR operator with one of the following values:

Value Description
FF_DECORATIVE Used for a decorative or fancy fonts
FF_DONTCARE Let the compiler specify
FF_MODERN Modern fonts that have a constant width
FF_ROMAN Serif fonts with variable width
FF_SCRIPT Script-like fonts
FF_SWISS Sans serif fonts with variable width

The lpszFace string is the name of the font used.

Once you have created a font, you can assign its return HFONT value to the handle of the TCanvas::Font member variable and then use it as you see fit. Here is an example:

procedure TForm1.btnFontClick(Sender: TObject);
var
    font : HFONT;
    fnt : TFont;
begin
    font := CreateFont(46, 28, 215, 0,
			  FW_NORMAL, 0, 0, 0, ANSI_CHARSET,
			  OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
			  DEFAULT_QUALITY, DEFAULT_PITCH Or FF_ROMAN,
			  'Times New Roman');

    fnt := TFont.Create;
    fnt.Handle := font;

    // Now you can use the font however you want
end;
 

Font Retrieval

At any specific time, a font is selected. This font could be the default font set by the operating system, which is usually MS Sans Serif. You may have changed it because of the requirements of your application. If you want to find out what font is currently selected on an object, simple declare a TFont variable and get the Font property of the control. This could be done as follows:

procedure TForm1.btnFontClick(Sender: TObject);
var
    fntFontUsedByThisForm : TFont;
begin
    fntFontUsedByThisForm := Self.Font;

	// Now you can do what you want with the font
end;

After this initialization, your variable can provide you with any type of valid information related to the currently used font by the control. The information includes the font's name, its size, its style(s), character set, etc.

 
 
 
 

Home Copyright © 2010 FunctionX, Inc.