はじめに
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>=1.0.0 でインストールしてください。
主な機能
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(GID)を PostScript グリフ名にマップします。標準の ISO Adobe charset を取得するには standard() を使用し、バイナリ CFF データから charset を解析するには from_reader() を使用します。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 エンコーディング — Unicode から GID へのマッピング
CffEncoding は CFF エンコーディングテーブルを処理し、文字コードをグリフIDにマップします。standard() は標準エンコーディングを返し、expert() はエキスパートエンコーディングを返し、from_reader() は CFF バイナリデータからカスタムエンコーディングを解析します。Unicode コードポイントで GID を検索するには unicode_to_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 辞書の解析
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 インデックス構造
CffIndex は CFF INDEX データ構造を表します — charstrings、subrs、名前テーブルで使用される可変長バイト文字列の長さプレフィックス付き配列です。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)}")
タイプ 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)
クイックスタート
git clone https://github.com/aspose-font-foss/Aspose.Font-FOSS-for-Python.git
cd Aspose.Font-FOSS-for-Python
pip install -e .from 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 for Python は MIT ライセンスです。ソースコードは GitHub にあり、aspose-font-foss/Aspose.Font-FOSS-for-Python にあります。PyPI から aspose-font としてインストールしてください。