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 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 ファイルをロードしています
ステップバイステップ
- フォント ファイルを見つけます (例:
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");を呼び出します。 - サブセット/マージ – 複数のフォントを 1 つのコレクションに結合します。
- ライセンス情報を抽出 – WOFF ヘッダーに存在する場合。
これらはすべて、公式サンプル リポジトリで実証されています。
📂 https://github.com/aspose-font/Aspose.Font-Documentation/tree/master/net-examples
クイックチェックリスト (ドキュメントページ用)
- ✅ NuGet バッジを追加します:
[](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拡張子の両方を渡したことを確認してください。