Tuesday, March 8, 2011

Fonts in wpf

There is no such thing as a Font class in the WPF. But we can have a reference to a FontFamily object. A font family (also known as a type family) is a collection of related typefaces. Under Windows, font families have familiar names such as Courier New, Times New Roman, Arial, Palatino Linotype, Verdana, and, of course, Comic Sans MS.

FontFamily = new FontFamily("Comic Sans MS");
FontSize = 48;




A typeface (also known as a face name) is the combination of a font family and a possible variation, such as Times New Roman Bold, Times New Roman Italic, and Times New Roman Bold Italic. Not every variation is available in every font family, and some font families have variations that affect the widths of individual characters, such as Arial Narrow.

The term font is generally used to denote a combination of a particular typeface with a particular size. The common measurement of fonts is the em size. (The term comes from the size of the square piece of metal type used in olden days for the capital M.) The em size is commonly described as the height of the characters in the Latin alphabetthe uppercase and lowercase letters A through Z without diacritical marksfrom the very top of the ascenders to the bottom of the descenders. However, the em size is not a metrical concept. It is a typographical design concept. The actual size of characters in a particular font could be somewhat greater than or less than what the em size implies.

Commonly, the em size is specified in a unit of measurement known as the point. In traditional typography, a point is 0.01384 inch, but in computer typography, the point is assumed to be exactly 1/72 inch. Thus, a 36-point em size (often abbreviated as a 36-point font) refers to characters that are about 1/2 inch tall.

In the Windows Presentation Foundation, you set the em size you want by using the FontSize property. But you don't use points. Like every measurement in the WPF, you specify the FontSize in device-independent units, which are 1/96 inch. Setting the FontSize property to 48 results in an em size of 1/2 inch, which is equivalent to 36 points.

If you're accustomed to specifying em sizes in points, just multiply the point size by 4/3 (or divide by 0.75) when setting the FontSize property. If you're not accustomed to specifying em sizes in points, you should get accustomed to it, and just multiply the point size by 4/3 when setting the FontSize property.

The default FontSize property is 11, which is 8.25 points. Much of The New York Times is printed in an 8-point type. Newsweek uses a 9-point type. This book has 10-point type.

You can use a full typeface name in the FontFamily constructor:

FontFamily = new FontFamily("Times New Roman Bold Italic");
FontSize = 32;


That's a 24-point Times New Roman Bold Italic font. However, it's more common to use the family name in the FontFamily constructor and indicate bold and italic by setting the FontStyle and FontWeight properties:

FontFamily = new FontFamily("Times New Roman");
FontSize = 32;
FontStyle = FontStyles.Italic;
FontWeight = FontWeights.Bold;

Notice that the FontStyle and FontWeight properties are set to static read-only properties of the FontStyles (plural) and FontWeights (plural) classes. These static properties return objects of type FontStyle and FontWeight, which are structures that have limited use by themselves.

Here's an interesting little variation:

FontStyle = FontStyles.Oblique;




An italic typeface is often stylistically a bit different from the non-italic (or roman) typeface. Look at the lowercase "a" to see the difference. But an oblique typeface simply slants all the letters of the roman typeface to the right. For some font families, you can set a FontStretch property to a static property of the FontStretches class.

No comments:

Post a Comment