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 UI (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.