Skip to content

タイポグラフィ(HasTypography)

HasTypography トレイトは、テキストの外観を制御します:フォントファミリー、スタイル、サイズ、間隔、伸縮、色、影、レンダリングモード。すべてのメソッドはフルエントなチェーン用に static を返します。

フォントの設定

setFont()

フォントファミリー、スタイル、サイズを一度の呼び出しで設定します。

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Normal text', newLine: true)
    ->setFont('Helvetica', 'B', 14)
    ->cell(0, 10, 'Bold text', newLine: true)
    ->setFont('Helvetica', 'BI', 16)
    ->cell(0, 10, 'Bold Italic text', newLine: true);

フォントスタイル

スタイル文字を組み合わせて複数の効果を適用します:

コードスタイル
''レギュラー
'B'ボールド
'I'イタリック
'BI'ボールドイタリック
'U'アンダーライン
'D'取り消し線
'O'オーバーライン

スタイルは自由に組み合わせられます:'BIU' はボールド、イタリック、アンダーライン付きのテキストになります。

php
$pdf->setFont('Helvetica', 'BU', 12)
    ->cell(0, 10, 'Bold + Underline', newLine: true)
    ->setFont('Helvetica', 'ID', 12)
    ->cell(0, 10, 'Italic + Strikethrough', newLine: true)
    ->setFont('Helvetica', 'BO', 12)
    ->cell(0, 10, 'Bold + Overline', newLine: true);

setFontSize()

ファミリーやスタイルを変更せずにフォントサイズを変更します。

php
$pdf->setFont('Helvetica', 'B', 12)
    ->cell(0, 10, '12pt heading', newLine: true)
    ->setFontSize(10)
    ->cell(0, 10, '10pt body text', newLine: true)
    ->setFontSize(8)
    ->cell(0, 10, '8pt footnote', newLine: true);

間隔と伸縮

setFontSpacing()

すべての文字ペア間に挿入される追加スペース(ポイント単位)を調整します。

php
$pdf->setFont('Helvetica', '', 12)
    ->setFontSpacing(0)
    ->cell(0, 10, 'Normal spacing', newLine: true)
    ->setFontSpacing(1.5)
    ->cell(0, 10, 'Expanded spacing', newLine: true)
    ->setFontSpacing(-0.5)
    ->cell(0, 10, 'Tight spacing', newLine: true)
    ->setFontSpacing(0);

setFontStretching()

グリフに水平スケーリングを適用します。値 100 が通常の幅です。

php
$pdf->setFont('Helvetica', '', 14)
    ->setFontStretching(100)
    ->cell(0, 10, 'Normal width (100%)', newLine: true)
    ->setFontStretching(130)
    ->cell(0, 10, 'Stretched (130%)', newLine: true)
    ->setFontStretching(75)
    ->cell(0, 10, 'Condensed (75%)', newLine: true)
    ->setFontStretching(100);

テキストの測定

getStringWidth()

現在のフォントとサイズでレンダリングされた文字列の幅(ユーザー単位)を返します。描画前のレイアウト計算に役立ちます。

php
$pdf->setFont('Helvetica', '', 12);
$width = $pdf->getStringWidth('Invoice Total: $1,250.00');

// 測定した幅を使用してセルを右揃えにする
$pageWidth = $pdf->getPageWidth();
$rightMargin = $pdf->getRightMargin();
$pdf->setX($pageWidth - $rightMargin - $width)
    ->cell($width, 10, 'Invoice Total: $1,250.00');

テキストレンダリングモード

PDFは TextRenderer 列挙型を通じて8つのテキストレンダリングモードを定義しています。これらは、テキストが塗りつぶされるか、ストロークされるか、クリッピングパスとして使用されるか、非表示にされるかを制御します。

モード効果
Fill0通常の塗りつぶしテキスト(デフォルト)
Stroke1アウトラインのみ
FillStroke2アウトライン付き塗りつぶし
Invisible3非表示だが選択/検索可能
FillClip4塗りつぶし後、クリッピングパスに追加
StrokeClip5ストローク後、クリッピングパスに追加
FillStrokeClip6塗りつぶし+ストローク後、クリップ
Clip7クリッピングパスのみに追加
php
$pdf->setFont('Helvetica', 'B', 36)
    ->setTextRenderingMode(0)
    ->cell(0, 15, 'Filled text', newLine: true)
    ->setTextRenderingMode(1)
    ->cell(0, 15, 'Outlined text', newLine: true)
    ->setTextRenderingMode(2)
    ->cell(0, 15, 'Fill + Stroke', newLine: true)
    ->setTextRenderingMode(3)
    ->cell(0, 15, 'Invisible (but searchable)', newLine: true)
    ->setTextRenderingMode(0);

Invisibleモード(3)はOCRオーバーレイに特に有用です — スキャン画像の上に認識されたテキストを配置することで、画像が表示されたままPDFを検索可能にします。

テキストカラー

setTextColor()

RGB値を使用してテキストカラーを設定します。

php
$pdf->setTextColor(0, 0, 0)          // 黒
    ->cell(0, 10, 'Black text', newLine: true)
    ->setTextColor(220, 50, 50)       // 赤
    ->cell(0, 10, 'Red text', newLine: true)
    ->setTextColor(0, 102, 204)       // 青
    ->cell(0, 10, 'Blue text', newLine: true)
    ->setTextColor(0, 0, 0);          // 黒にリセット

単一の引数で呼び出した場合、グレースケール値として設定されます(0 = 黒、255 = 白)。

php
$pdf->setTextColor(128)
    ->cell(0, 10, 'Gray text', newLine: true);

テキストシャドウ

setTextShadow()

テキストの下に影効果を適用します。影は連想配列として定義されます。

php
$pdf->setFont('Helvetica', 'B', 28)
    ->setTextShadow([
        'enabled'    => true,
        'depth_w'    => 0.4,    // 水平オフセット(mm)
        'depth_h'    => 0.4,    // 垂直オフセット(mm)
        'color'      => [180, 180, 180],
        'opacity'    => 0.5,
        'blend_mode' => 'Normal',
    ])
    ->cell(0, 15, 'Heading with Shadow', newLine: true)
    ->setTextShadow(['enabled' => false]);

完全な例

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()

    // タイトル — 大きく、太字、青色
    ->setFont('Helvetica', 'B', 24)
    ->setTextColor(0, 51, 102)
    ->cell(0, 14, 'Monthly Report', newLine: true)

    // サブタイトル — レギュラー、グレー、間隔を広めに
    ->setFont('Helvetica', '', 12)
    ->setTextColor(100, 100, 100)
    ->setFontSpacing(1.0)
    ->cell(0, 10, 'February 2026', newLine: true)
    ->setFontSpacing(0)
    ->ln(5)

    // 本文テキスト — 黒、通常
    ->setFont('Times', '', 11)
    ->setTextColor(0, 0, 0)
    ->multiCell(0, 6, 'Revenue increased 12% compared to the previous quarter. Operating costs remained stable, and net margin improved by 3 percentage points.')
    ->ln(3)

    // 脚注 — 小さく、イタリック、グレー
    ->setFont('Times', 'I', 8)
    ->setTextColor(150, 150, 150)
    ->cell(0, 5, '* All figures are unaudited.')

    ->save('report.pdf');

LGPL-3.0-or-later ライセンスの下で公開されています。