.NET 字体管理 API

在数字 排版 中,字体定义了用于字符外观的特定样式。大多数情况下,在文档和网页中使用字体来对文本进行风格化。每种字体都在一个文件中描述,该文件包含有关字符大小、重量、样式以及编码的信息。由于字体是各种文件格式的重要组成部分,Aspose 提供了一个专用 API 来处理流行字体类型的操作和渲染,包括 TrueTypeCFFOpenType、和 Type1。在本文中,您将学习如何使用 C# 和 Aspose.Font for .NET 从字体中加载、保存和提取信息。

.NET 字体操作 API - 免费下载

Aspose.Font for .NET 是一个本地 API,可让您在 .NET 应用程序中使用 C# 实现字体管理功能。除了从字体中加载、保存和提取信息外,API 还允许您在需要时呈现字形或文本。您可以 下载 API 或使用 NuGet 将其安装在您的 .NET 应用程序中。

PM> Install-Package Aspose.Font

使用 C# 从文件加载或保存字体

您可以从存储在数字存储中的文件中加载字体,以检索字体信息。 Aspose.Font for .NET 公开了单独的类来处理特定类型的字体,即 CFF、TrueType 等。以下是加载和保存字体的步骤。

  • 使用 FontDefinition 类从文件加载字体(使用文件的路径或字节数组)。
  • 使用 CffFontTtfFontType1Font 类访问字体信息。
  • 保存文件(如果需要)。

加载和保存 CFF 字体

以下代码示例展示了如何使用 C# 加载和保存 CFF 字体。

// 如需完整的示例和数据文件,请访问 https://github.com/aspose-font/Aspose.Font-for-.NET
//要从中加载字体的字节数组
string dataDir = RunExamples.GetDataDir_Data();

byte[] fontMemoryData = File.ReadAllBytes(dataDir + "OpenSans-Regular.cff");
FontDefinition fd = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData)));
CffFont cffFont = Aspose.Font.Font.Open(fd) as CffFont;

//处理来自刚刚加载的 CffFont 对象的数据

//将 CffFont 保存到磁盘
//带有完整路径的输出字体文件名
string outputFile = RunExamples.GetDataDir_Data() + "OpenSans-Regular_out.cff";

cffFont.Save(outputFile);

加载和保存 TrueType 字体

以下代码示例展示了如何使用 C# 加载和保存 TrueType 字体。

// 如需完整的示例和数据文件,请访问 https://github.com/aspose-font/Aspose.Font-for-.NET
//要从中加载字体的字节数组
string dataDir = RunExamples.GetDataDir_Data();
            
byte[] fontMemoryData = File.ReadAllBytes(dataDir + "Montserrat-Regular.ttf");
FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new ByteContentStreamSource(fontMemoryData)));
TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;

//使用刚刚加载的 TtfFont 对象中的数据

//将 TtfFont 保存到磁盘
//带有完整路径的输出字体文件名
string outputFile = RunExamples.GetDataDir_Data() + "Montserrat-Regular_out.ttf";

ttfFont.Save(outputFile);

加载和保存 Type1 字体

以下代码示例显示如何使用 C# 加载和保存 Type1 字体。

// 如需完整的示例和数据文件,请访问 https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;

使用 C# 提取字体度量

Aspose.Font for .NET 还允许您检索字体度量信息,例如 Ascender、Descender、TypoAscender、TypoDescender 和 UnitsPerEm。以下是执行此操作的步骤。

  • 使用 FontDefinition 类从文件中加载 TrueType 或 Type1 字体。
  • 使用相应的字体类型类打开字体,即 Type1Font、TtfFont 等。
  • 访问字体的指标信息。

以下代码示例展示了如何使用 C# 从 TrueType 和 Type1 字体中提取字体规格。

从 TrueType 字体中提取指标

// 如需完整的示例和数据文件,请访问 https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont font = Aspose.Font.Font.Open(fd) as TtfFont;

string name = font.FontName;
Console.WriteLine("Font name: " + name);
Console.WriteLine("Glyph count: " + font.NumGlyphs);
string metrics = string.Format(
    "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
    font.Metrics.Ascender, font.Metrics.Descender,
    font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);

Console.WriteLine(metrics);

//从字体中获取 cmap unicode 编码表作为对象 TtfCMapFormatBaseTable 以访问有关符号“A”的字体字形的信息。
//还要检查字体是否有对象 TtfGlyfTable(表'glyf')来访问字形。
Aspose.Font.TtfCMapFormats.TtfCMapFormatBaseTable cmapTable = null;
if (font.TtfTables.CMapTable != null)
{
    cmapTable = font.TtfTables.CMapTable.FindUnicodeTable();
}
if (cmapTable != null && font.TtfTables.GlyfTable != null)
{
    Console.WriteLine("Font cmap unicode table: PlatformID = " + cmapTable.PlatformId + ", PlatformSpecificID = " + cmapTable.PlatformSpecificId);

    //“A”符号的代码
    char unicode = (char)65;

    //“A”的字形索引
    uint glIndex = cmapTable.GetGlyphIndex(unicode);

    if (glIndex != 0)
    {
        //“A”字形
        Glyph glyph = font.GetGlyphById(glIndex);
        if (glyph != null)
        {
            //打印字形指标
            Console.WriteLine("Glyph metrics for 'A' symbol:");
            string bbox = string.Format(
                "Glyph BBox: Xmin = {0}, Xmax = {1}" + ", Ymin = {2}, Ymax = {3}",
                glyph.GlyphBBox.XMin, glyph.GlyphBBox.XMax,
                glyph.GlyphBBox.YMin, glyph.GlyphBBox.YMax);
            Console.WriteLine(bbox);
            Console.WriteLine("Width:" + font.Metrics.GetGlyphWidth(new GlyphUInt32Id(glIndex)));
        }
    }
}

从 Type1 字体中提取指标

// 如需完整的示例和数据文件,请访问 https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;

string name = font.FontName;
Console.WriteLine("Font name: " + name);
Console.WriteLine("Glyph count: " + font.NumGlyphs);
string metrics = string.Format(
    "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
    font.Metrics.Ascender, font.Metrics.Descender,
    font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);

Console.WriteLine(metrics);

使用 C# 检测字体中的拉丁符号

您还可以通过使用 Aspose.Font for .NET 解码字形代码来检测字体中的拉丁文本。以下是执行此操作的步骤。

  • 使用 FontDefinition 类从文件中加载字体。
  • 使用相应字体类的 DecodeToGid() 方法解码 GlyphId。

以下代码示例展示了如何使用 C# 解码 TrueType 和 Type1 字体中的拉丁符号。

检测 TrueType 字体中的拉丁符号

// 如需完整的示例和数据文件,请访问 https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;

bool latinText = true;


for (uint code = 65; code < 123; code++)
{
    GlyphId gid = ttfFont.Encoding.DecodeToGid(code);
    if (gid == null || gid == GlyphUInt32Id.NotDefId)
    {
        latinText = false;
    }
}

if (latinText)
{
    Console.WriteLine(string.Format("Font {0} supports latin symbols.", ttfFont.FontName));
}
else
{
    Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", ttfFont.FontName));
}

检测 Type1 字体中的拉丁符号

// 如需完整的示例和数据文件,请访问 https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;

bool latinText = true;


for (uint code = 65; code < 123; code++)
{
    GlyphId gid = font.Encoding.DecodeToGid(code);
    if (gid == null || gid == GlyphUInt32Id.NotDefId)
    {
        latinText = false;
    }
}

if (latinText)
{
    Console.WriteLine(string.Format("Font {0} supports latin symbols.", font.FontName));
}
else
{
    Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", font.FontName));
}

结论

在本文中,您了解了如何使用 C# 以编程方式加载和保存 CFF、TrueType 和 Type1 字体。此外,您还学习了如何从 .NET 应用程序中的 TrueType 和 Type1 字体中提取字体度量信息。您可以在 文档 中探索 Aspose.Font 为 .NET 提供的更多有趣功能。如需更多更新,请继续访问 Aspose.Font 博客