加载 Web-Open-Font (WOFF) 文件 | .NET
Contents
[
Hide
Show
]使用 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 文件
一步一步
- 找到字体文件(例如“C:\Fonts\MyWebFont.woff”)。
- **创建一个“FileSystemStreamSource”,为库提供该文件的流。
- 将流源包装在“FontFileDefinition”中 – 指定扩展名“woff”(库使用它来选择正确的解析器)。
- 创建一个
FontDefinition– 传递适当的FontType(Woff) 和FontFileDefinition。 - 通过
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}结果:
woffFont是Aspose.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}两种方法都使用相同的底层类,仅字节源发生变化(磁盘文件与内存与自定义流)。
加载后——你能做什么?
- 渲染文本 – 使用“Aspose.Font.Rendering”子系统或导出字形轮廓。
- 读取字体规格 – 例如
((WoffFont)woffFont).Metrics。 - 转换为另一种格式 – 在转换为适当的子类或使用通用 API 后调用
font.Save("output.ttf");。 - 子集/合并 – 将多种字体合并到一个集合中。
- 提取许可信息 – 如果存在于 WOFF 标头中。
所有这些都在官方示例存储库中进行了演示:
📂 https://github.com/aspose-font/Aspose.Font-Documentation/tree/master/net-examples
快速清单(用于文档页面)
- ✅ 添加 NuGet 徽章:
[NuGet](https://img.shields.io/nuget/v/Aspose.Font.svg)](https://www.nuget.org/packages/Aspose.Font/) - ✅ 包含具有正确语法突出显示的示例代码块(如上所示)。
- ✅ 提及支持的扩展表(从上面第 2 节复制)。
- ✅ 链接到 API 参考:
Aspose.Font.Font.OpenAspose.Font.Sources.FileSystemStreamSourceAspose.Font.Sources.ByteContentStreamSourceAspose.Font.FontFileDefinitionAspose.Font.FontDefinition
- ✅ 提供“进一步阅读”链接:
- Getting started: https://docs.aspose.com/font/net/getting-started/
- Full API reference: https://reference.aspose.com/font/net/
- Free online demo: https://products.aspose.app/font/conversion
- ✅ 添加故障排除提示:如果您收到“不支持的字体格式”,请验证您是否通过了
FontType.WOFF和"woff"扩展名。