* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
13 KiB
title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Packaging Fonts with Applications | Learn how to package fonts with a Windows Presentation Foundation application, including adding fonts as content and resource items, and limits on font usage. | 03/30/2017 |
|
|
db15ee48-4d24-49f5-8b9d-a64460865286 |
Packaging Fonts with Applications
This topic provides an overview of how to package fonts with your [!INCLUDETLA#tla_winclient] application.
Note
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.
Introduction to Packaging Fonts
You can easily package fonts as resources within your [!INCLUDETLA2#tla_winclient] 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 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 xref:System.Windows.Media.GlyphTypeface object and referencing its xref:System.Windows.Media.GlyphTypeface.EmbeddingRights%2A 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="http://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.
[!code-xamlFontSnippets#FontPackageSnippet8]
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="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Other project build settings ... -->
<ItemGroup>
<Resource Include="resources\Peric.ttf" />
<Resource Include="resources\Pericl.ttf" />
</ItemGroup>
</Project>
Note
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.
[!code-xamlFontSnippets#FontPackageSnippet1]
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 xref:System.Windows.Media.FontFamily.%23ctor%2A method. The following code example shows how to reference the application's font resources in the project subdirectory called resources.
[!code-csharpFontSnippets#FontPackageSnippet2] [!code-vbFontSnippets#FontPackageSnippet2]
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.
[!code-csharpFontSnippets#FontPackageSnippet5] [!code-vbFontSnippets#FontPackageSnippet5]
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.
[!code-xamlFontSnippets#FontPackageSnippet3]
[!code-csharpFontSnippets#FontPackageSnippet4] [!code-vbFontSnippets#FontPackageSnippet4]
Enumerating Fonts in an Application
To enumerate fonts as resource items in your application, use either the xref:System.Windows.Media.Fonts.GetFontFamilies%2A or xref:System.Windows.Media.Fonts.GetTypefaces%2A method. The following example shows how to use the xref:System.Windows.Media.Fonts.GetFontFamilies%2A method to return the collection of xref:System.Windows.Media.FontFamily objects from the application font location. In this case, the application contains a subdirectory named "resources".
[!code-csharpFontSnippets#FontsSnippet3] [!code-vbFontSnippets#FontsSnippet3]
The following example shows how to use the xref:System.Windows.Media.Fonts.GetTypefaces%2A method to return the collection of xref:System.Windows.Media.Typeface objects from the application font location. In this case, the application contains a subdirectory named "resources".
[!code-csharpFontSnippets#FontsSnippet7] [!code-vbFontSnippets#FontsSnippet7]
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.
[!code-xamlOpenTypeFontsSample#OpenTypeFontsSample1]
Note
This SDK contains a set of sample OpenType fonts that you can use with [!INCLUDETLA2#tla_winclient] 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 [!INCLUDETLA2#tla_winclient] applications:
-
Font embedding permission bits: [!INCLUDETLA2#tla_winclient] 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: [!INCLUDETLA2#tla_winclient] applications do not allow a font reference to an http or ftp uniform resource identifier (URI).
-
Absolute URI using the pack: notation: [!INCLUDETLA2#tla_winclient] applications do not allow you to create a xref:System.Windows.Media.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: [!INCLUDETLA2#tla_winclient] 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.