#335 – How WPF Finds Fonts on a Target System

A WPF application specifies a font that it wants using the properties FontFamily, FontStyle, FontWeight, FontStretch and FontSize.  At runtime, WPF decides the exact font to use on the target system where the application is running. This decision is based on matching the first four properties (ignoring FontSize for the moment) to a physical font file that is installed on that system.

WPF starts by matching the supplied FontFamily against the names of the fonts found on the system.  It then tries to find a font that most closely matches the requested FontStretch, FontStyle and FontWeight property values.  Matching FontStretch is the highest priority, followed by FontStyle and then FontWeight.

If WPF can’t find a matching font, it “falls back” to a default font installed with WPF, C:\Windows\Fonts\GlobalUserInterface.CompositeFont.  This is a composite font that tries to map individual characters to fonts likely to be present on the system.

Advertisement

#265 – Specifying Values for FontStyle

You can specify a number of different values for the FontStyle property of a Control, to indicate whether you want an italic (slanted letters) style.

The different possible values for FontStyle are:

  • Normal – Regular text, not slanted
  • Italic – Standard italic font
  • Oblique – Slant the letters so that they appear italic.

The Oblique choice is typically used when the font being used doesn’t have an italic variant.

The three font style choices are shown below, for the Arial typeface.  (Arial does have an italic variant).

Here’s another example, for the Georgia typeface.  Notice that the Italic version looks a little more professional, while the Oblique variant just slants the letters, relative to the Normal style.

#263 – Specifying Font Properties

In WPF, you specify the font for text elements by specifying values for one or more font-related properties.

There are five main properties for specifying font characteristics, found in the Control class.

  • FontFamily – The typeface, e.g. Arial
  • FontSize – The size of the text, in device independent units, points, inches, or centimeters
  • FontStretch – Optionally stretch/compress the font
  • FontStyle – Set font as italic (e.g. Italic, Oblique)
  • FontWeight – Set font as bold or light (e.g. Bold, Light, Extra-Bold, etc)

Below is a simple example.

	<TextBlock FontFamily="Cambria" FontSize="14 pt" FontStyle="Normal" FontWeight="Normal"
		HorizontalAlignment="Center" VerticalAlignment="Center" Width="300" Height="100" TextWrapping="Wrap">
		A man who carries a cat by the tail learns something he can learn in no other way.
	</TextBlock>