Share via


Packaging Fonts with Applications

This topic provides an overview of how to package fonts with your Windows Presentation Foundation (WPF) application.

NoteNote:

As with most types of software, font files are licensed, rather than sold. Licenses that govern the use of fonts vary from vendor to vendor but in general most licenses, including those covering the fonts Microsoft supplies with applications and Windows, do not allow the fonts to be embedded within applications or otherwise redistributed. Therefore, as a developer it is your responsibility to ensure that you have the required license rights for any font you embed within an application or otherwise redistribute.

This topic contains the following sections.

  • Introduction to Packaging Fonts
  • Adding Fonts as Content Items
  • Adding Fonts as Resource Items
  • Creating a Font Resource Library
  • Limitations on Font Usage
  • Related Topics

Introduction to Packaging Fonts

You can easily package fonts as resources within your WPF applications to display user interface text and other types of text based content. The fonts can be separate from or embedded within the application's assembly files. You can also can create a resource-only font library, which your application can reference.

OpenType and TrueType® fonts contain a type flag, fsType, that indicates font embedding licensing rights for the font. However, this type flag only refers to embedded fonts stored in a document–it does not refer to fonts embedded in an application. You can retrieve the font embedding rights for a font by creating a GlyphTypeFace object and referencing its EmbeddingRights property. Refer to the "OS/2 and Windows Metrics" section of the OpenType Specification for more information on the fsType flag.

The Microsoft Typography Web site includes contact information that can help you locate a particular font vendor or find a font vendor for custom work.

Adding Fonts as Content Items

You can add fonts to your application as project content items that are separate from the application's assembly files. This means that content items are not embedded as resources within an assembly. The following project file example shows how to define content items.

<Project DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Other project build settings ... -->

  <ItemGroup>
    <Content Include="Peric.ttf" />
    <Content Include="Pericl.ttf" />
  </ItemGroup>
</Project>

In order to ensure that the application can use the fonts at run time, the fonts must be accessible in the application's deployment directory. The <CopyToOutputDirectory> element in the application's project file allows you to automatically copy the fonts to the application deployment directory during the build process. The following project file example shows how to copy fonts to the deployment directory.

<ItemGroup>
  <Content Include="Peric.ttf">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
  <Content Include="Pericl.ttf">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

The following code example shows how to reference the application's font as a content item—the referenced content item must be in the same directory as the application's assembly files.

<TextBlock FontFamily="./#Pericles Light">
  Aegean Sea
</TextBlock>

Adding Fonts as Resource Items

You can add fonts to your application as project resource items that are embedded within the application's assembly files. Using a separate subdirectory for resources helps to organize the application's project files. The following project file example shows how to define fonts as resource items in a separate subdirectory.

<Project DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Other project build settings ... -->

  <ItemGroup>
    <Resource Include="resources\Peric.ttf" />
    <Resource Include="resources\Pericl.ttf" />
  </ItemGroup>
</Project>
NoteNote:

When you add fonts as resources to your application, make sure you are setting the <Resource> element, and not the <EmbeddedResource> element in your application's project file. The <EmbeddedResource> element for the build action is not supported.

The following markup example shows how to reference the application's font resources.

<TextBlock FontFamily="./resources/#Pericles Light">
  Aegean Sea
</TextBlock>

Referencing Font Resource Items from Code

In order to reference font resource items from code, you must supply a two-part font resource reference: the base uniform resource identifier (URI); and the font location reference. These values are used as the parameters for the FontFamily method. The following code example shows how to reference the application's font resources in the project subdirectory called resources.

// The font resource reference includes the base URI reference (application directory level),
// and a relative URI reference.
myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./resources/#Pericles Light");

The base uniform resource identifier (URI) can include the application subdirectory where the font resource resides. In this case, the font location reference would not need to specify a directory, but would have to include a leading "./", which indicates the font resource is in the same directory specified by the base uniform resource identifier (URI). The following code example shows an alternate way of referencing the font resource item—it is equivalent to the previous code example.

// The base URI reference can include an application subdirectory.
myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/resources/"), "./#Pericles Light");

Referencing Fonts from the Same Application Subdirectory

You can place both application content and resource files within the same user-defined subdirectory of your application project. The following project file example shows a content page and font resources defined in the same subdirectory.

<ItemGroup>
  <Page Include="pages\HomePage.xaml" />
</ItemGroup>
<ItemGroup>
  <Resource Include="pages\Peric.ttf" />
  <Resource Include="pages\Pericl.ttf" />
</ItemGroup>

Since the application content and font are in the same subdirectory, the font reference is relative to the application content. The following examples show how to reference the application's font resource when the font is in the same directory as the application.

<TextBlock FontFamily="./#Pericles Light">
  Aegean Sea
</TextBlock>
// The font resource reference includes the base Uri (application directory level),
// and the file resource location, which is relative to the base Uri.
myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "/pages/#Pericles Light");

Enumerating Fonts in an Application

To enumerate fonts as resource items in your application, use either the GetFontFamilies or GetTypefaces method. The following example shows how to use the GetFontFamilies method to return the collection of FontFamily objects from the application font location. In this case, the application contains a subdirectory named "resources".

foreach (FontFamily fontFamily in Fonts.GetFontFamilies(new Uri("pack://application:,,,/"), "./resources/"))
{
    // Perform action.
}

The following example shows how to use the GetTypefaces method to return the collection of Typeface objects from the application font location. In this case, the application contains a subdirectory named "resources".

foreach (Typeface typeface in Fonts.GetTypefaces(new Uri("pack://application:,,,/"), "./resources/"))
{
    // Perform action.
}

Creating a Font Resource Library

You can create a resource-only library that contains only fonts—no code is part of this type of library project. Creating a resource-only library is a common technique for decoupling resources from the application code that uses them. This also allows the library assembly to be included with multiple application projects. The following project file example shows the key portions of a resource-only library project.

<PropertyGroup>
  <AssemblyName>FontLibrary</AssemblyName>
  <OutputType>library</OutputType>
  ...
</PropertyGroup>
...
<ItemGroup>
  <Resource Include="Kooten.ttf" />
  <Resource Include="Pesca.ttf" />
</ItemGroup

Referencing a Font in a Resource Library

To reference a font in a resource library from your application, you must prefix the font reference with the name of the library assembly. In this case, the font resource assembly is "FontLibrary". To separate the assembly name from the reference within the assembly, use a ';' character. Adding the "Component" keyword followed by the reference to the font name completes the full reference to the font library's resource. The following code example shows how to reference a font in a resource library assembly.

<Run FontFamily="/FontLibrary;Component/#Kootenay" FontSize="36">
  ABCDEFGHIJKLMNOPQRSTUVWXYZ
</Run>
NoteNote:

This SDK contains a set of sample OpenType fonts that you can use with WPF applications. The fonts are defined in a resource-only library. For more information, see Sample OpenType Font Pack.

Limitations on Font Usage

The following list describes several limitations on the packaging and use of fonts in WPF applications:

  • Font embedding permission bits: WPF applications do not check or enforce any font embedding permission bits. See the Introduction_to_Packing Fonts section for more information.

  • Site of origin fonts: WPF applications do not allow a font reference to an http or ftp uniform resource identifier (URI).

  • Absolute URI using the pack: notation: WPF applications do not allow you to create a FontFamily object programmatically using "pack:" as part of the absolute uniform resource identifier (URI) reference to a font. For example, "pack://application:,,,/resources/#Pericles Light" is an invalid font reference.

  • Automatic font embedding: During design time, there is no support for searching an application's use of fonts and automatically embedding the fonts in the application's resources.

  • Font subsets: WPF applications do not support the creation of font subsets for non-fixed documents.

  • In cases where there is an incorrect reference, the application falls back to using an available font.

See Also

Reference

Typography
FontFamily

Concepts

OpenType Font Features
Sample OpenType Font Pack

Other Resources

https://www.microsoft.com/typography/links/
https://www.microsoft.com/typography/otspec/