9.1. Fonts

 < Day Day Up > 

GDI+ supports only OpenType and TrueType fonts. Both of these font types are defined by mathematical representations that allow them to be scaled and rotated easily. This is in contrast to raster fonts that represent characters by a bitmap of a predetermined size. Although prevalent a few years ago, these font types are now only a historical footnote in the .NET world.

The term font is often used as a catchall term to describe what are in fact typefaces and font families. In .NET, it is important to have a precise definition for these terms because both Font and FontFamily are .NET classes. Before discussing how to implement these classes, let's look at the proper definition of these terms.

  • A font family is at the top of the hierarchy and consists of a group of typefaces that may be of any style, but share the same basic appearance. Tahoma is a font family.

  • A typeface is one step up the hierarchy and refers to a set of fonts that share the same family and style but can be any size. Tahoma bold is a typeface.

  • A font defines how a character is represented in terms of font family, style, and size. For example (see Figure 9-1), Tahoma Regular10 describes a font in the Tahoma font family having a regular style as opposed to bold or italic and a size of 10 points.

    Figure 9-1. Relationship of fonts, typefaces, and font families

Font Families

The FontFamily class has two primary purposes: to create a FontFamily object that is later used to create an instance of a Font, or to provide the names of all font families available on the user's computer system.

Creating a FontFamily Object


 public FontFamily (string name); public FontFamily (GenericFontFamilies genfamilies); 



A font family name such as Arial or Tahoma.

 FontFamily ff = new FontFamily("Arial");  // Arial family 


An enumeration (in the System.Drawing.Text namespace) that has three values: Monospace, Serif, and SansSerif. This constructor is useful when you are interested in a generic font family rather than a specific one. The following constructor specifies a font family in which each character takes an equal amount of space horizontally. (Courier New is a common monospace family.)

 FontFamily ff = new FontFamily(GenericFontFamilies.Monospace); 

As a rule, the more specific you are about the font family, the more control you have over the appearance of the application, which makes the first constructor preferable in most cases. However, use a generic font family if you are unsure about the availability of a specific font on a user's computer.

Listing Font Families

The FontFamily.Families property returns an array of available font families. This is useful for allowing a program to select a font internally to use for displaying fonts to users, so they can select one. The following code enumerates the array of font families and displays the name of the family using the Name property; it also uses the IsStyleAvailable method to determine if the family supports a bold style font.

 string txt = ""; foreach (FontFamily ff in FontFamily.Families) {    txt += ff.Name;    if (ff.IsStyleAvailable(FontStyle.Bold))txt= += " (B)";    txt += "\r\n"; // Line feed } textBox1.Text = txt;   // Place font family names in textbox 

The Font Class

Although several constructors are available for creating fonts, they fall into two categories based on whether their first parameter is a FontFamily type or a string containing the name of a FontFamily. Other parameters specify the size of the font, its style, and the units to be used for sizing. Here are the most commonly used constructors:


 public Font(FontFamily ff, Float emSize); public Font(FontFamily ff, Float emSize, FontStyle style); public Font(FontFamily ff, Float emSize, GraphicsUnit unit); public Font(FontFamily ff, Float emSize, FontStyle style,             GraphicsUnit unit); public Font(String famname, Float emSize); public Font(String famname, Float emSize, FontStyle style); public Font(String famname, Float emSize, GraphicsUnit unit); public Font(String famname, Float emSize, FontStyle style,             GraphicsUnit unit);  



The size of the font in terms of the GraphicsUnit. The default GraphicsUnit is Point.


A FontStyle enumeration value: Bold, Italic, Regular, Strikeout, or Underline. All font families do not support all styles.


The units in which the font is measured. This is one of the GraphicsUnit enumeration values:

Point 1/72nd inch


Display 1/96th inch


Document 1/300th inch

Pixel (based on device resolution)

GraphicsUnits and sizing are discussed later in this section.

Creating a Font

Creating fonts is easy; the difficulty lies in deciding which of the many constructors to use. If you have already created a font family, the simplest constructor is

 FontFamily ff = new FontFamily("Arial"); Font normFont = new Font(ff,10);      // Arial Regular 10 pt. 

The simplest approach, without using a font family, is to pass the typeface name and size to a constructor:

 Font normFont = new Font("Arial",10)  // Arial Regular 10 point 

By default, a Regular font style is provided. To override this, pass a FontStyle enumeration value to the constructor:

 Font boldFont = new Font("Arial",10,FontStyle.Bold); Font bldItFont = new Font("Arial",10,       FontStyle.Bold | FontStyle.Italic); // Arial bold italic 10 

The second example illustrates how to combine styles. Note that if a style is specified that is not supported by the font family, an exception will occur.

The Font class implements the IDisposable interface, which means that a font's Dispose method should be called when the font is no longer needed. As we did in the previous chapter with the Graphics object, we can create the Font object inside a using construct, to ensure the font resources are freed even if an exception occurs.

 using (Font normFont = new Font("Arial",12)) { 

Using Font Metrics to Determine Font Height

The term font metrics refers to the characteristics that define the height of a font family. .NET provides both Font and FontClass methods to expose these values. To best understand them, it is useful to look at the legacy of typesetting and the terms that have been carried forward from the days of Gutenberg's printing press to the .NET Framework Class Library.

In typography, the square grid (imagine graph paper) used to lay out the outline (glyph) of a font is referred to as the em square. It consists of thousands of cells each measured as one design unit. The outline of each character is created in an em square and consists of three parts: a descent, which is the part of the character below the established baseline for all characters; the ascent, which is the part above the baseline; and the leading, which provides the vertical spacing between characters on adjacent lines (see Figure 9-2).

Figure 9-2. Font metrics: the components of a font

Table 9-1 lists the methods and properties used to retrieve the metrics associated with the em height, descent, ascent, and total line space for a font family. Most of the values are returned as design units, which are quite easy to convert to a GraphicsUnit. The key is to remember that the total number of design units in the em is equivalent to the base size argument passed to the Font constructor. Here is an example of retrieving metrics for an Arial 20-point font:

 FontFamily ff = new FontFamily("Arial"); Font myFont   = new Font(ff,20);   // Height is 20 points int emHeight  = ff.GetEmHeight(FontStyle.Regular);     // 2048 int ascHeight = ff.GetCellAscent(FontStyle.Regular);   // 1854 int desHeight = ff.GetCellDescent(FontStyle.Regular);  // 434 int lineSpace = ff.GetLineSpacing(FontStyle.Regular);  // 2355 // Get Line Height in Points (20 x (2355/2048)) float guHeight = myFont.Size * (lineSpace / emHeight); // 22.99 float guHeight2 = myFont.GetHeight(); // 30.66 pixels 

Table 9-1. Using Font and FontFamily to Obtain Font Metrics





Design units

The height of the em square used to design the font family. TrueType fonts usually have a value of 2,048.


Design units

The height of a character above the base line.


Design units

The height of a character below the base line.


Design units

The total height reserved for a character plus line spacing. The sum of CellAscent, CellDescent, and Leading (see Figure 9-2). This value is usually 12 to 15 percent greater than the em height.


Graphics unit

The base size (size passed to constructor).



The base size in points.



The total height of a line. Calculated by converting LineSpacing value to pixels.

The primary value of this exercise is to establish familiarity with the terms and units used to express font metrics. Most applications that print or display lines of text are interested primarily in the height of a line. This value is returned by the Font.GetHeight method and is also available through the Graphics.MeasureString method described in the next section.


cell height


ascent + descent

em height


cell height internal leading

line spacing


cell height + external leading

     < Day Day Up > 

    Core C# and  .NET
    Core C# and .NET
    ISBN: 131472275
    EAN: N/A
    Year: 2005
    Pages: 219

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net