Home

The Characteristics of a Font

 
 

Introduction

A font is a GDI+ object, like a pen or a brush. Like any other object, the primary information of a font is its name. To see the names of fonts installed in a computer that is running Microsoft Windows, in the Control Panel, you can double-click Fonts. Here is an example:

Because is something that must be installed on, and can be removed from, the computer, it is likely that two computers would not have the same (list of) fonts. Still, because Microsoft Windows installs some default or popular fonts by default, there are fonts that are likely to be found on every regular installation of Microsoft Windows. This implies that, if you create a (commercial) application that you intend to distribute to people (customers) you do not know, you should assume that your audience would not have most or any of the fancy fonts you have in your computer.

You have two alternatives when selecting the fonts used in your application you intend to distribute. If you own the font, you can distribute it with your application. Another solution, easier solution, is to use only the most likely fonts to be found on most computers.

Creating a Font

In the .NET Framework, the primary piece of information about a font is held by a class named FontFamily. The FontFamly class is equipped with three constructors that each provides a different means of initializing a font.

The list of fonts installed in a a computer is stored in a property of the FontFamily class and that property is named Families. The FontFamily.Families property is collection member declared as a static property.

Like every GDI+ object, before using a font, you must create and initialize it.  To support fonts, the .NET Framework provides the Font class. This class is equipped with various constructors that each provides a particular means of initializing a font.

In order to use a font in an application, you must identify the FontFamily object you want to use and associate it with a Font.

The Name of a Font

The primary piece of information about a font is its name. As seen about, the names of fonts can be seen in the Fonts window of Control Panel. As mentioned already, the name of a font is held by the FontFamily class. If you know the name of the font you want to use, to create a font object (using that name), you can use the following constructor of the FontFamily class:

public FontFamily(string fontName);

This constructor takes as argument the name of the font you want to use. Here is an example of using it:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    Button btnWrite;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        btnWrite = new Button();
        btnWrite.Text = "Write";
        btnWrite.Location = new Point(12, 12);
        btnWrite.Click += new EventHandler(btnWriteClick);

        Controls.Add(btnWrite);
    }

    void btnWriteClick(object sender, EventArgs e)
    {
        FontFamily fntWrite = new FontFamily("Times New Roman");
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

When using this constructor, you should make sure the font you use exists in the computer that will run the application. If you provide a name of a font that does not exist in the computer that is running the application, the compiler would throw an ArgumentException exception.

To get the name that a FontFamily object is using, get the value of its Name property. As you can guess, the name of a font is a string.

The Size of a Font

The size of a font is the width and the height that will be used to draw a character. It is important to know that this size refers to the visual 2-dimensional size and not to the memory size. This means that the letters i, N, g, M, l, and W obviously use different visual space but all of them use the same amount of memory space.

When creating a font, to specify its size, you can use the following constructor of the Font class:

public Font(string familyName, float emSize);

The first argument is the name of the font you want to use. The second argument is a floating-point number. Here is an example:

void btnWriteClick(object sender, EventArgs e)
{
      Font fntWrite = new Font("Verdana", 16.0F);
}

If you had stored the name of the font in a FontFamily variable and want to create a font, you can use the following constructor of the Font class:

public Font(FontFamily family, float emSize);

Here is an example of using this constructor:

void btnWriteClick(object sender, EventArgs e)
{
      FontFamily ffmWrite = new FontFamily("Times New Roman");
      Font fntWrite = new Font(ffmWrite, 16.0F);
}

To find out the size of an existing font, you can get the value of the Size property of a Font object.

The Style of a Font

To enhance the appearance of a characters, a font can be underlined, italicized, or bolded, etc. These are referred to as a style. To specify a style when creating a font, you can use the following constructor of the Font class:

public Font(string familyName, float emSize, FontStyle style);

The first argument is the name of the font. The second argument would be the size of the font. The third argument is of type FontStyle, which is an enumeration. The members of this enumeration are:

  • Regular: The character(s) would not have a particular style
  • Bold: The character(s) would be bolded
  • Italic: The character(s) would be italicized
  • Strikeout: A line would traverse the the character(s)
  • Underline: The character(s) would be underlined

Here is an example of initializing a font by specifying its name, its size, and a style:

void btnWriteClick(object sender, EventArgs e)
{
        Font fntWrite = new Font("Verdana", 16.0F, FontStyle.Italic);
}

The above constructor is used to directly specify the name of the font. If you had stored the name of a font in a FontFamily object and you want to specify its style when initializing it, you can use the following constructor when declaring a Font variable:

public Font(FontFamily family, float emSize, FontStyle style);

Here is an example of using this constructor:

void btnWriteClick(object sender, EventArgs e)
{
        FontFamily ffmWrite = new FontFamily("Times New Roman");
        Font fntWrite = new Font(ffmWrite, 16.0F, FontStyle.Strikeout);
}

If you already have an existing font but want to apply a style to it, change its style, or apply a different style to it, you can use the following constructor of the Font class:

public Font(Font prototype, FontStyle newStyle);

Here is an example:

void btnWriteClick(object sender, EventArgs e)
{
        FontFamily ffmWrite = new FontFamily("Times New Roman");
        Font fntWrite = new Font(ffmWrite, 16.0F, FontStyle.Strikeout);
        
        . . . Use or don't use the font here

        fntWrite = new Font(fntWrite, FontStyle.Bold);

        . . . Use the new font here
}

You can also create a new font based on the previous one but with a different style. Here is an example:

void btnWriteClick(object sender, EventArgs e)
{
        FontFamily ffmWrite = new FontFamily("Times New Roman");
        Font fntWrite = new Font(ffmWrite, 16.0F, FontStyle.Strikeout);
        
        . . . Use or don't use the font here

        Font fntAnother = new Font(fntWrite, FontStyle.Bold);

        . . . Use the new font here
}

You can combine two or more styles to get a fancier effect. To combine the styles, you use the OR bit operator "|". Here are two examples:

void btnWriteClick(object sender, EventArgs e)
{
    FontFamily ffmWrite = new FontFamily("Times New Roman");
    Font fntWrite = new Font(ffmWrite, 16.0F,
		 FontStyle.Strikeout | FontStyle.Italic);

    . . . Use or don't use the font here
        
    fntWrite = new Font(fntWrite,
		FontStyle.Italic | FontStyle.Bold | FontStyle.Underline);

    . . . Use the new font here
}

As opposed to applying a style, you may be interested to get the style that a font object holds:

  • To find out whether a character or a word is bolded, get the Boolean value of the Bold property of its font
  • To find out whether a character or a word is italicized, get the Boolean value of the Italic property of its font
  • To find out whether a character or a word in underlined, get the Boolean value of the Underline property of its font
  • To find out whether a character or a word is stricken through, get the Boolean value of the Strikeout property of its font
  • To get all the styles applied to a character, a word, or a group of words, get its Style property
 

Previous Copyright © 2007 FunctionX, Inc. Next