类型1字体的字形和指标| .NET

获取字体指标

字体指标包含诸如ascenderdescendertypoAscendertypodescender和’unitsperem’之类的信息。 适用于 .NET 的 Aspose.Font API可以使用以下示例代码从Type1字体文件中读取字体指标信息。

  1. 创建 FontDefinition – 构建 Type1 字体文件的完整路径(例如,string fontPath = Path.Combine(dataDir, "courier.pfb");),并为 .pfb 格式创建 FontFileDefinition,然后使用 FontType.Type1 和文件定义构建 FontDefinition
  2. 打开字体并访问字体指标 – 使用 Aspose.Font.Font.Open(fd) 打开字体,该函数返回一个 Font 对象。将其转换为 Type1Font 以访问 Type1 特定的属性,然后读取字体指标属性,例如 AscenderDescenderTypoAscenderTypoDescenderUnitsPerEm
  3. 使用指标 – 这些值可用于布局计算、缩放或应用程序所需的任何排版处理。
 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2string fileName = dataDir + "courier.pfb"; //Font file name with full path
 3
 4FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
 5Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
 6
 7string name = font.FontName;
 8Console.WriteLine("Font name: " + name);
 9Console.WriteLine("Glyph count: " + font.NumGlyphs);
10string metrics = string.Format(
11    "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
12    font.Metrics.Ascender, font.Metrics.Descender,
13    font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);
14
15Console.WriteLine(metrics);

检测拉丁符号

适用于 .NET 的 Aspose.Font可让您从Type1字体文件中检测拉丁符号。这可以使用以下示例代码来实现。

  1. 加载 Type1 字体 – 为 .pfb 文件创建一个 FontFileDefinition(例如 string fontPath = Path.Combine(dataDir, "courier.pfb");),然后使用 FontType.Type1 和文件定义构建一个 FontDefinition

  2. 检测支持的拉丁字符 – 遍历字体的字形集合,并使用 DecodeToGid() 将每个 Unicode 字符转换为其内部的字形 ID。如果该方法返回非零 ID,则表示该字形存在于字体中,表明支持该拉丁字符。

 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2string fileName = dataDir + "courier.pfb"; //Font file name with full path
 3
 4FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
 5Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
 6
 7bool latinText = true;
 8
 9
10for (uint code = 65; code < 123; code++)
11{
12    GlyphId gid = font.Encoding.DecodeToGid(code);
13    if (gid == null || gid == GlyphUInt32Id.NotDefId)
14    {
15        latinText = false;
16    }
17}
18
19if (latinText)
20{
21    Console.WriteLine(string.Format("Font {0} supports latin symbols.", font.FontName));
22}
23else
24{
25    Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", font.FontName));
26}

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.