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

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


Get every new post delivered to your Inbox.

Join 311 other followers