加载 Web-Open-Font (WOFF) 文件 | .NET

使用 Aspose.Font for .NET 加载 Web-Open-Font (WOFF) 文件

你需要什么

项目描述获取方式
Aspose.Font for .NET用于读取、写入和转换字体文件(包括 WOFF/WOFF2)的核心库。可通过 dotnet add package Aspose.Font 命令或通过 NuGet 用户界面 (https://www.nuget.org/packages/Aspose.Font) 安装。
目标框架.NETFramework4.x / .NETCore3.x / .NET5/6/7 – Aspose.Font 支持的任何平台。无需额外的运行时环境。
命名空间导入csharp\nusing Aspose.Font;\nusing Aspose.Font.Sources;\nusing System.IO;\n

**提示:**所有 API 文档位于 https://reference.aspose.com/font/net/

从文件系统加载 WOFF 文件

一步一步

  1. 找到字体文件(例如“C:\Fonts\MyWebFont.woff”)。
  2. **创建一个“FileSystemStreamSource”,为库提供该文件的流。
  3. 将流源包装在“FontFileDefinition”中 – 指定扩展名“woff”(库使用它来选择正确的解析器)。
  4. 创建一个 FontDefinition – 传递适当的 FontType (Woff) 和 FontFileDefinition
  5. 通过 Font.Open() 打开字体 – 您获得从 Aspose.Font.Font 派生的对象(例如 WoffFont)。
 1using System;
 2using System.IO;
 3using Aspose.Font;
 4using Aspose.Font.Sources;
 5
 6class LoadWoffExample
 7{
 8    static void Main()
 9    {
10        // Path to the .woff file
11        string woffPath = @"C:\Fonts\MyWebFont.woff";
12
13        // Stream source that reads directly from the file system
14        FileSystemStreamSource streamSrc = new FileSystemStreamSource(woffPath);
15
16        // FontFileDefinition – we tell Aspose it is a “woff” file
17        FontFileDefinition fileDef = new FontFileDefinition("woff", streamSrc);
18
19        // FontDefinition – specify the font type (Woff)
20        FontDefinition fontDef = new FontDefinition(FontType.WOFF, fileDef);
21
22        // Load the font
23        Font woffFont = Font.Open(fontDef);
24
25        // woffFont now gives you access to glyphs, metrics, encoding, etc.
26        Console.WriteLine($"Loaded font: {woffFont.GetType().Name}");
27    }
28}

结果:woffFontAspose.Font.Woff.WoffFont 的一个实例。现在您可以渲染文本、检查字形度量或将其转换为其他格式(TTF、SVG 等)。

加载 WOFF 字体的替代方法

字节数组加载(例如,当字体嵌入到资源中或通过 HTTP 下载时)

 1byte[] woffBytes = File.ReadAllBytes(@"C:\Fonts\MyWebFont.woff");
 2
 3// ByteContentStreamSource wraps the byte[] as a readable stream
 4ByteContentStreamSource byteSrc = new ByteContentStreamSource(woffBytes);
 5
 6// Create definition – note we still supply "woff" as extension
 7FontDefinition fd = new FontDefinition(FontType.WOFF,
 8                                      "woff",
 9                                      byteSrc);
10
11Font woffFont = Font.Open(fd);

直接从流加载,无需中间 FileDefinition

如果您已经有一个打开的“FileStream”:

1using (FileStream fs = File.OpenRead(@"C:\Fonts\MyWebFont.woff"))
2{
3    // Wrap the live FileStream in FileSystemStreamSource
4    var streamSrc = new FileSystemStreamSource(fs);
5
6    // Directly pass stream source + extension into FontDefinition
7    var fd = new FontDefinition(FontType.WOFF, "woff", streamSrc);
8    var woffFont = Font.Open(fd);
9}

两种方法都使用相同的底层类,仅字节源发生变化(磁盘文件与内存与自定义流)。

加载后——你能做什么?

所有这些都在官方示例存储库中进行了演示:

📂 https://github.com/aspose-font/Aspose.Font-Documentation/tree/master/net-examples

快速清单(用于文档页面)

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.