はじめに
Compact Font Format (CFF) は、OpenType フォント内で使用されるバイナリグリフ記述フォーマットで、PostScript 互換のアウトラインエンジンを備えています。Python 用の Aspose.Font FOSS は、aspose_font.cff 名前空間の下に、CFF データ構造を読み取り・検査するための専用クラス群を提供します:CffFont、CffCharset、CffEncoding、CffDict、CffIndex、および Type2Interpreter。この記事では、ライブラリを使用して CFF フォントをロードし、内部テーブルをナビゲートし、charstring プログラムを解釈する方法を解説します。
このパッケージは MIT ライセンスです。以下の手順でインストールしてください:
pip install aspose-font-foss主な機能
CFF フォントのロード
FontLoader.open() は CFF フォーマットを自動的に検出します — スタンドアロンの .cff ファイルまたは CFF アウトラインテーブルを埋め込んだ OTF ファイルのいずれかを渡してください。返されるオブジェクトは CffFont です(OTF ファイルの場合は TtfFont の cff_font プロパティからアクセスできます)。
from aspose_font.loader import FontLoader
# Load a standalone CFF file
font = FontLoader.open("OpenSans.cff")
# Load a standalone CFF file (auto-detected from format)
font = FontLoader.open("OpenSans.cff")
print(font.font_name, font.num_glyphs)
CFF Charset — グリフ ID を名前にマッピング
CffCharset はグリフ ID (GIDs) を PostScript グリフ名にマッピングします。standard() を使用して標準の ISO Adobe charset を取得するか、from_reader() を使用してバイナリ CFF データから charset を解析します。name_for() メソッドは GID の PostScript 名を返し、gid_for() は逆引きを実行します。
from aspose_font.cff.charset import CffCharset
charset = CffCharset.standard()
print(charset.name_for(1)) # ".notdef" or similar
print(charset.gid_for("A")) # GID for the PostScript name "A"
CFF Encoding — Unicode から GID へのマッピング
CffEncoding は CFF エンコーディングテーブルを処理し、文字コードをグリフ ID にマッピングします。standard() は標準エンコーディングを返し、expert() はエキスパートエンコーディングを返し、from_reader() は CFF バイナリデータからカスタムエンコーディングを解析します。unicode_to_gid() を使用して Unicode コードポイントから GID を検索し、get_all_codepoints() を使用してマッピングされたすべてのコードポイントを列挙します。
from aspose_font.cff.encoding import CffEncoding
encoding = CffEncoding.standard()
gid = encoding.unicode_to_gid(0x41) # Unicode for 'A'
all_codepoints = encoding.get_all_codepoints()
print(f"Standard encoding covers {len(all_codepoints)} code points")
CFF Dictionary 解析
CFF フォントはトップレベルとプライベートのメタデータを辞書に格納します。CffDict.from_bytes() は生の CFF DICT ブロブを解析し、オペレータキーによる get() と set() のアクセスを提供します。TopDict.from_dict() はこれをトップレベルの CFF 辞書用に特化し、PrivateDict.from_dict() はプライベート DICT 用に同様の処理を行います。
from aspose_font.cff.dict_ import CffDict
# Parse a raw CFF DICT from bytes
raw_dict_bytes = b"..." # CFF-encoded DICT bytes
d = CffDict.from_bytes(raw_dict_bytes)
vals = d.get(21) # retrieve values for operator 21 (PostScript CharstringType)
CFF Index 構造
CffIndex は CFF INDEX データ構造を表します — charstrings、subrs、name tables に使用される可変長バイト文字列の長さプリフィックス付き配列です。from_reader() はバイナリリーダーから INDEX を解析し、結果は整数インデックスで添字付け可能です。
from aspose_font.cff.index import CffIndex
from aspose_font._io import BinaryReader # internal API — may change between releases
import io
raw_index = b"..." # raw CFF INDEX bytes
reader = BinaryReader(io.BytesIO(raw_index))
index = CffIndex.from_reader(reader)
print(f"INDEX entries: {len(index)}")
Type 2 Charstring 解釈
Type2Interpreter は Type 2 charstring プログラム(CFF フォントで使用されるコンパクトな PostScript ライク形式)を実行し、アウトラインコマンドの GlyphPath を生成します。charstring バイトと関連するプライベート辞書を使用して interpret() を呼び出してください。これにより、完全な PostScript インタプリタを使用せずに CFF charstring から生のグリフアウトラインを抽出できます。
from aspose_font.cff.type2 import Type2Interpreter
interpreter = Type2Interpreter()
glyph_path = interpreter.interpret(charstring_bytes, private_dict)
for cmd in glyph_path.commands:
print(type(cmd).__name__, cmd)
クイックスタート
pip install aspose-font-fossfrom aspose_font.loader import FontLoader
# Load an OTF with embedded CFF outlines
font = FontLoader.open("OpenSans-Regular.otf")
glyph_id = font.encoding.unicode_to_gid(0x41)
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)
print(f"Path commands for 'A': {len(glyph.path.commands)}")
サポートされているフォーマット
| フォーマット | 拡張子 | 読み取り | 書き込み |
|---|---|---|---|
| CFF | .cff | ✓ | ✓ |
| OTF | .otf | ✓ | ✓ |
| TTF | .ttf | ✓ | ✓ |
| WOFF | .woff | ✓ | ✓ |
| WOFF2 | .woff2 | ✓ | ✓ |
| EOT | .eot | ✓ | ✓ |
| TYPE1 | .pfb / .pfa | ✓ | ✓ |
オープンソースとライセンス
Aspose.Font FOSS は Python 用に MIT ライセンスです。ソースコードは GitHub にあり、aspose-font-foss/Aspose.Font-FOSS-for-Python にあります。aspose-font-foss としてパッケージ化されています。