SVG フォントの読み込み | .NET

Loading SVG Fonts with Aspose.Font for .NET

なぜ SVG フォントなのか?

Aspose.Font は、TTF、OTF、WOFF、WOFF2、SVG など、すべてのサポートされているフォントタイプを読み込む単一の統一 API を提供し、Aspose.Font.Font オブジェクトとして扱えます。

必要なもの

項目説明取得方法
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>

NuGet パッケージの追加

1dotnet add package Aspose.Font

Core API Concepts

クラス / 列挙型目的
FontDefinition読み込む対象(フォントタイプ + ソース)を記述します。
FontFileDefinitionストリーム ソースFileSystemStreamSourceByteContentStreamSource など)と ファイル拡張子"svg")を結び付けます。
FileSystemStreamSource / ByteContentStreamSource基礎バイトの読み取り可能なストリームを提供します – ディスク上のファイルまたはメモリ バッファからです。
Font.Open(FontDefinition)ソースを解析し、具体的な Aspose.Font.Font 実装(SvgFont)を返すファクトリ メソッドです。

フォーマット間で変わる唯一の要素は、FontType 列挙値(Svg)とファイル拡張子文字列("svg")です。

SVG フォントの読み込み – 3 つの一般的シナリオ

ファイルシステムからの読み込み

 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 のアップロード)を持っている場合、まずディスクに書き込む必要はなく、直接ラップして開くだけで済みます。

これらすべてのシナリオは公式サンプルリポジトリで実演されています:

結論

たった 3 行のコードで、ディスク、メモリ、または任意のカスタムストリームから 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.