在 PdfSharp 中使用私有字体
在 PdfSharp 1.5 中提供了在 Web 服务器上使用私有字体的示例,见:http://www.pdfsharp.net/wiki/(X(1)S(mg0wojiafv2wdqhklvachrti))/FontResolver-sample.ashx。
注意:FontResolver 是一个全局对象,应用于 PdfSharp 库的所有消费者。也被 MigraDoc 库用于创建 PDF 文档。
1. 私有字体
JetBrain 是一套适合程序员使用的字体,我从 https://www.jetbrains.com/lp/mono/ 下载了它,下载下来的文件是一个 Zip 包,解压之后,里面是多个 TrueType 格式的字体文件,文件名的氛围两部分组成,以中划线分隔,前面都是 JetBrainsMono,后面是字体的风格。例如,对于普通的字体来说,就是 JetBrainsMono-Regular.ttf 这个字体文件。粗体对应的字体文件是 JetBrainsMono-Bold.ttf,而斜体对应的字体文件是 JetBrains-Mono-Italic.ttf。
我还下载了一个微软雅慧的 TrueType 字体文件。名称为 YaHei.ttf。通过它来使用中文字体。
字体族和字体
这里有两个概念:字体家族 Font Family 和 字体 Font Face。
Font Family 是一套字体的名称,例如,对于 JetBrains Mono 字体家族来说,这就是一套字体的名称。而一套字体内部,有包括了普通字体,粗体,斜体等等多种具体的字体。这些最终的字体称为 Font Face。
当使用字体的时候,我们需要提供字体家族的名称,具体对应的样式,它们组合起来,我们可以得到实际使用的字体文件,最终得到 TrueType 字体数据。
下面的示例将展示如何使用这两种字体。
2. IFontResolver 接口
PdfSharp 通过 IFontResolver 接口定义了如何获取字体。该接口定义如下。见:https://github.com/empira/PDFsharp/blob/master/src/PdfSharp/Fonts/IFontResolver.cs
namespace PdfSharp.Fonts
{
/// <summary>
/// Provides functionality that converts a requested typeface into a physical font.
/// </summary>
public interface IFontResolver
{
/// <summary>
/// Converts specified information about a required typeface into a specific font.
/// </summary>
/// <param name="familyName">Name of the font family.</param>
/// <param name="isBold">Set to <c>true</c> when a bold fontface is required.</param>
/// <param name="isItalic">Set to <c>true</c> when an italic fontface is required.</param>
/// <returns>Information about the physical font, or null if the request cannot be satisfied.</returns>
FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic);
//FontResolverInfo ResolveTypeface(Typeface); TODO in PDFsharp 2.0
/// <summary>
/// Gets the bytes of a physical font with specified face name.
/// </summary>
/// <param name="faceName">A face name previously retrieved by ResolveTypeface.</param>
byte[] GetFont(string faceName);
}
}
接口定义了两个方法。
2.1 ResolveTypeface
获得字体信息。将需要使用的特定字体信息表示为一个 FontResolverInfo 对象。需要提供字体家族名称,是否粗体,是否泄题。
对于字体来说,我们需要提供一个字体族的名称来代表一类字体,在内部,我们需要针对每种具体的字形,定义出针对每种字形的字符串名称。通过字符串