はじめに

フォントファイルはテーブルを中心に構成されており、各テーブルはグリフのアウトライン、文字マッピング、命名メタデータ、水平メトリクスなど、特定のデータカテゴリを格納します。これらのテーブルを理解しアクセスすることは、フォント分析、メタデータ編集、品質保証、フォーマット変換といった作業に不可欠です。

Aspose.Font FOSS for Python (aspose-font-foss) は、クリーンな Python API を通じて、OpenType と TrueType テーブルの全セットに直接アクセスできるようにします。このライブラリは MIT ライセンスでリリースされており、外部依存がゼロなので、ライセンス上の懸念なく任意のプロジェクトに組み込むことができます。

この記事では、ライブラリで利用可能な主要なテーブルクラスを順に解説します。ヘッダーメタデータの読み取りからグリフデータの検査、名前レコードの変更まで、すべては TtfFont 上の TtfTableSet コンテナに支えられています。


主な機能

フォントの読み込みとテーブルへのアクセス

FontLoader で読み込まれたすべてのフォントは、TtfFont 上の ttf_tables プロパティを通じて解析されたテーブルを公開します。TtfTableSet オブジェクトは、各標準テーブルへの型付けされたアクセスを提供するとともに、カスタムまたは未認識のテーブルに対しては get_raw() を介した生バイトアクセスも可能にします。

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
tables = font.ttf_tables

# Typed table access
head = tables.head
name = tables.name
cmap = tables.cmap

# Raw bytes for any table tag
raw_os2 = tables.get_raw("OS/2")

ヘッドテーブルの読み取り

HeadTable はフォント全体の基本パラメータを格納します:units per em、リビジョン、バウンディングボックス、作成日、スタイルフラグ。これらの値はレンダリングエンジンやフォントバリデータにとって重要です。

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
head = font.ttf_tables.head

print(f"Units per em: {head.units_per_em}")
print(f"Revision: {head.font_revision}")
print(f"Bounding box: ({head.x_min}, {head.y_min}) to ({head.x_max}, {head.y_max})")
print(f"Mac style flags: {head.mac_style}")
print(f"Glyph data format: {head.glyph_data_format}")

ネームテーブルの操作

NameTable にはすべての人間が読めるメタデータが格納されています:フォントファミリ名、スタイル、著作権、ライセンス、デザイナー、説明。名前IDでレコードを照会したり、ローカライズされた名前を取得したり、リブランディングのためにエントリを変更したりできます。

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
name_table = font.ttf_tables.name

# Read the font family name (name ID 1)
family = name_table.best_name(1, ["en"])
print(f"Family: {family}")

# List all localized versions of the full name (name ID 4)
localized = name_table.localized_names(4)
for lang, value in localized.items():
    print(f"  {lang}: {value}")

# Replace the font family name
name_table.replace_name(1, "MyCustomFamily")

CMapテーブルの検査

CmapTable はUnicodeコードポイントをグリフIDにマッピングします。さまざまなプラットフォームとエンコーディングの組み合わせに対応する複数のサブテーブルを含む場合があります。best_subtable() を使用して、用途に最も適したマッピングを取得してください。

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
cmap = font.ttf_tables.cmap

# Get the best Unicode subtable
subtable = cmap.best_subtable()
print(f"Platform: {subtable.platform_id}, Encoding: {subtable.encoding_id}")

# Look up the glyph ID for 'A'
gid = subtable.get_gid(ord("A"))
print(f"Glyph ID for 'A': {gid}")

OS/2 と水平メトリクスの読み取り

Os2Table は OS/2 と、タイポグラフィ的なアセンダー/ディセンダー、埋め込みフラグ、Panose分類を含む Windows 固有のメトリクスを提供します。HheaTable と HmtxTable はテキストシェイピングに使用される水平レイアウトメトリクスを供給します。

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
os2 = font.ttf_tables.os2
hhea = font.ttf_tables.hhea

print(f"Typo ascender: {os2.s_typo_ascender}")
print(f"Typo descender: {os2.s_typo_descender}")
print(f"Embedding flags (fsType): {os2.fs_type}")
print(f"Panose: {os2.panose.hex()}")

print(f"Hhea ascender: {hhea.ascender}")
print(f"Max advance width: {hhea.advance_width_max}")
print(f"Number of hmetrics: {hhea.number_of_hmetrics}")

カーニングとグリフデータ

KernTable は水平間隔調整のためのカーニングペアを格納します。GlyfTable には TrueType フォント用の実際のグリフアウトラインデータが含まれています。また、PostTable を使用してグリフ名を解決することもできます。

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")

# Kerning lookup
kern = font.ttf_tables.kern
if kern:
    pairs = kern.pairs
    print(f"Total kern pairs: {len(pairs)}")
    # Look up a specific pair by glyph IDs
    adjustment = kern.get(36, 57)
    print(f"Kern adjustment: {adjustment}")

# Glyph name from post table
post = font.ttf_tables.post
if post:
    glyph_name = post.glyph_name(0)
    print(f"Glyph 0 name: {glyph_name}")

クイックスタート

pip install aspose-font-foss
import aspose_font

# Load any TTF or OTF font
font = aspose_font.FontLoader.open("MyFont.ttf")
tables = font.ttf_tables

# Print basic table info
print(f"Font: {font.font_name}")
print(f"Family: {font.font_family}")
print(f"Glyphs: {font.num_glyphs}")
print(f"Units/em: {tables.head.units_per_em}")

# Read name table entries
name_table = tables.name
for name_id in [0, 1, 2, 4, 5, 6]:
    value = name_table.best_name(name_id, ["en"])
    if value:
        print(f"  Name ID {name_id}: {value}")

# Check character coverage
cmap = tables.cmap
subtable = cmap.best_subtable()
for char in "ABCabc012":
    gid = subtable.get_gid(ord(char))
    print(f"  '{char}' -> glyph {gid}")

サポートされているフォーマット

フォーマット拡張子読み取り書き込み
TTF.ttf✓✓
OTF.otf✓✓
CFF.cff✓✓
WOFF.woff✓✓
WOFF2.woff2✓✓
EOT.eot✓—
TYPE1.pfb/.pfa✓—

オープンソースとライセンス

Aspose.Font FOSS for Python は MIT ライセンスの下でリリースされています。ソースコードは GitHub で入手可能で、aspose-font-foss としてパッケージ化されています。個人・商用プロジェクトを問わず、制限なく使用、改変、配布することが自由です。


はじめに

関連リソース