Loading SVG fonts | .NET

使用 Aspose.Font for .NET 加载 SVG 字体

为什么选择 SVG 字体?

Aspose.Font 提供了一个统一的 API,可加载任何受支持的字体类型(TTF、OTF、WOFF、WOFF2、SVG 等),并将其作为强类型的 Aspose.Font.Font 对象进行操作。

What you need

ItemDescriptionHow to obtain
Aspose.Font for .NET核心库,可读取/写入所有受支持的字体格式(包括 SVG)。dotnet add package Aspose.Font
or via the NuGet UI: https://www.nuget.org/packages/Aspose.Font/
Target framework.NET Framework 4.x / .NET Core 3.x / .NET 5/6/7 – 任意 Aspose.Font 支持的平台。No extra runtime needed.
Namespace importscsharp<br>using Aspose.Font;<br>using Aspose.Font.Sources;<br>using System.IO;<br>

Add the NuGet package

1dotnet add package Aspose.Font

Core API Concepts

Class / EnumPurpose
FontDefinition描述 想要加载的内容(字体类型 + 来源)。
FontFileDefinition流源FileSystemStreamSourceByteContentStreamSource 等)与 文件扩展名"svg")绑定在一起。
FileSystemStreamSource / ByteContentStreamSource提供底层字节的可读流 —— 可以来自磁盘上的文件或内存缓冲区。
Font.Open(FontDefinition)工厂方法,解析来源并返回具体的 Aspose.Font.Font 实现(SvgFont)。

唯一变化的只有 FontType 枚举值(Svg)以及文件扩展名字符串("svg")。

Loading an SVG font – three common scenarios

从文件系统加载

 1using System;
 2using System.IO;
 3using Aspose.Font;
 4using Aspose.Font.Sources;
 5
 6class LoadSvgFromFile
 7{
 8    static void Main()
 9    {
10        // 1️⃣ Path to the *.svg* file
11        string svgPath = @"C:\Fonts\MyWebFont.svg";
12
13        // Verify the file exists
14        if (!File.Exists(svgPath))
15            throw new FileNotFoundException($"SVG file not found: {svgPath}");
16
17        // 2️⃣ Stream source that reads directly from the file
18        FileSystemStreamSource streamSrc = new FileSystemStreamSource(svgPath);
19
20        // 3️⃣ FontFileDefinition – we tell Aspose the extension is "svg"
21        FontFileDefinition fileDef = new FontFileDefinition("svg", streamSrc);
22
23        // 4️⃣ FontDefinition – specify the exact font type
24        FontDefinition fontDef = new FontDefinition(FontType.Svg, fileDef);
25
26        // 5️⃣ Load the font
27        Font svgFont = Font.Open(fontDef);
28
29        Console.WriteLine($"Loaded font: {svgFont.GetType().Name}");   // → SvgFont
30    }
31}

结果: svgFontAspose.Font.Svg.SvgFont 的实例。从这里您可以查询字形、度量,或将其转换为其他格式。

字节数组 加载

 1using System;
 2using System.IO;
 3using Aspose.Font;
 4using Aspose.Font.Sources;
 5
 6class LoadSvgFromBytes
 7{
 8    static void Main()
 9    {
10        // Assume the SVG font was bundled as an embedded resource
11        byte[] svgBytes = File.ReadAllBytes(@"C:\Fonts\MyWebFont.svg");
12
13        // Wrap the raw bytes into a stream source
14        ByteContentStreamSource byteSrc = new ByteContentStreamSource(svgBytes);
15
16        // Build the definition – extension still must be “svg”
17        FontDefinition fd = new FontDefinition(
18            FontType.Svg,
19            "svg",
20            byteSrc);
21
22        // Load
23        Font svgFont = Font.Open(fd);
24
25        Console.WriteLine($"Glyph count: {((Aspose.Font.Svg.SvgFont)svgFont).Glyphs.Count}");
26    }
27}

使用场景:

直接从 打开的流 加载

 1using System;
 2using System.IO;
 3using Aspose.Font;
 4using Aspose.Font.Sources;
 5
 6class LoadSvgFromOpenStream
 7{
 8    static void Main()
 9    {
10        using (FileStream fs = File.OpenRead(@"C:\Fonts\MyWebFont.svg"))
11        {
12            // The stream source can be constructed from any Stream object
13            FileSystemStreamSource src = new FileSystemStreamSource(fs);
14
15            // Pass stream source + extension straight into FontDefinition
16            FontDefinition fd = new FontDefinition(
17                FontType.Svg,
18                "svg",
19                src);
20
21            Font svgFont = Font.Open(fd);
22            Console.WriteLine($"Family name: {((Aspose.Font.Svg.SvgFont)svgFont).FamilyNames[0]}");
23        }
24    }
25}

为何重要:
如果您已经拥有一个 Stream(例如来自 ASP.NET 上传),则无需先将其写入磁盘——只需包装后直接打开。

所有这些场景都在官方示例仓库中演示:

结论

只需三行代码,您即可从磁盘、内存或任何自定义流中加载 SVG 字体,并立即获得对其字形路径、度量和转换功能的完整编程访问——这要归功于 Aspose.Font 的统一模型。如果需要将文本直接光栅化到图像或 PDF 上,可结合 Aspose.SVG 的渲染引擎,这就形成了在 .NET 中针对现代 Web 为中心的排版的完整流水线。

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.