#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.

#264 – Specifying Values for FontWeight

You can specify a number of different values for the FontWeight property of a Control, to specify the thickness, or “weight” of a font.  Each possible value is a request to the font for a different thickness.

Listed in order from lightest weight to heaviest, the possible choices for FontWeight are:

  • Thin
  • ExtraLight
  • Light
  • Normal
  • Medium
  • DemiBold
  • Bold
  • ExtraBold
  • Black
  • ExtraBlack

The number of distinct weights that you’ll get will depend on the font.  Many fonts have just two weights–Normal and Bold.  The weight value specified in the FontWeight setting is just a request.  Windows will return a font whose weight best matches the requested weight.

Below is an example with the Arial typeface.  We request all ten different weights, but we see that there are only three different weights for Arial in Windows.

The Segoe UI font has four different weights:

#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>

Follow

Get every new post delivered to your Inbox.

Join 312 other followers