Type-1 (PostScript) フォントの読み込み | .NET
Contents
[
Hide
Show
]Aspose.Font for .NETでのType‑1フォントの読み込み
なぜType‑1フォントなのか?
- Type‑1(PostScript)フォント – .pfb(バイナリ)と*.pfa*(ASCII) – は、多くのレガシー出版やCADワークフローで依然として使用されています。
- Aspose.Fontは、外部依存なしでこれらのフォントを読み取り、解析、レンダリング、変換でき、サーバー側の処理やドキュメント生成に最適です。
必要なもの
| 項目 | 最低バージョン |
|---|---|
| .NET Framework | 4.6 (or .NET Core 2.x / .NET 5+) |
| Aspose.Font for .NET | 23.12 or later |
| License | A valid .lic file (optional for trial – otherwise a 30‑day evaluation works). |
プロジェクトにNuGetパッケージを追加します:
1dotnet add package Aspose.FontコアAPIの概念
| クラス / 列挙型 | 目的 |
|---|---|
FontDefinition | 開きたいフォントの種類(FontType.Type1)を記述します。 |
FontFileDefinition | フォントが存在する場所(ファイルシステム (FileSystemStreamSource)、メモリ (ByteContentStreamSource)、またはカスタムストリームソース)を記述します。 |
FontType | Asposeに期待するフォーマット(CFF、TTF、Type1、…)を示す列挙型です。 |
Aspose.Font.Font.Open(fd) | 定義を読み取り、IFont実装(Type1Font)を返す静的メソッドです。 |
Type1Font | Type‑1フォント向けにグリフ、メトリクス、エンコーディング、レンダリング機能を提供する具体クラスです。 |
ディスクからType‑1フォントをロードする
1using System;
2using System.IO;
3using Aspose.Font;
4using Aspose.Font.Sources;
5
6// Path to the .pfb or .pfa file
7string fileName = @"C:\Fonts\courier.pfb"; // change to your file
8
9// Ensure the file exists
10if (!File.Exists(fileName))
11 throw new FileNotFoundException($"Font file not found: {fileName}");
12
13// Build a FontDefinition that tells Aspose we are dealing with a Type‑1 font.
14FontDefinition fd = new FontDefinition(
15 FontType.Type1, // format hint
16 new FontFileDefinition(
17 "pfb", // extension hint (pfb or pfa)
18 new FileSystemStreamSource(fileName))); // reads directly from the file system
19
20// Load it
21Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
22
23// Verify that the font really loaded
24if (font == null)
25 throw new InvalidOperationException("Unable to load the Type‑1 font.");メモリからType‑1をロードする
1byte[] fontBytes = File.ReadAllBytes(fileName);
2FontDefinition fd = new FontDefinition(
3 FontType.Type1,
4 new FontFileDefinition("pfb", new ByteContentStreamSource(fontBytes)));
5Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;ヒント:
"pfb"文字列は単なるヒントです。ASCIIエンコードのType‑1ファイルには"pfa"を使用してください。
結論
- Aspose.Fontは、レガシーなType‑1フォントのロードと操作をシンプルにします。ソース(
FileSystemStreamSource、ByteContentStreamSource、…)を定義し、Aspose.Font.Font.Openで開くことで、メトリクス、エンコーディングテーブル、強力なレンダリングAPIに直ちにアクセスできます。 - 同じパターンがCFFやTrueType/OpenTypeフォントにも適用されます。
FontType.Type1を適切な列挙値に変更するだけです。