Core C# and .NET
< 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.
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
Constructors: public FontFamily (string name); public FontFamily (GenericFontFamilies genfamilies);
Parameters:
FontFamily ff = new FontFamily("Arial"); // Arial 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: 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);
Parameters:
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
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.
|
< Day Day Up > |