ドキュメントモデル

概要

document パッケージ(Layer 2)はページサイズ、単位、スタイル、ドキュメントノードツリーの型を提供します。ほとんどのユーザーは、ツリーの直接操作ではなく設定のためにこれらの型を使用します。

import "github.com/gpdf-dev/gpdf/document"

ページサイズ

var (
    A4     = Size{Width: 595.28, Height: 841.89}   // 210mm x 297mm
    A3     = Size{Width: 841.89, Height: 1190.55}   // 297mm x 420mm
    Letter = Size{Width: 612, Height: 792}           // 8.5" x 11"
    Legal  = Size{Width: 612, Height: 1008}          // 8.5" x 14"
)

Size

type Size struct {
    Width, Height float64  // in points (1pt = 1/72 inch)
}

単位と値

Value

単位付きの寸法:

type Value struct {
    Amount float64
    Unit   Unit
}

単位コンストラクタ

関数単位
Pt(v)ポイント (1/72 inch)Pt(12) — ネイティブPDF単位
Mm(v)ミリメートルMm(20) — 20mmマージン
Cm(v)センチメートルCm(2.5) — 2.5cm
In(v)インチIn(1) — 1インチ
Em(v)フォントサイズ相対Em(2) — フォントサイズの2倍
Pct(v)親要素の割合Pct(50) — 50%
// Usage examples
document.Mm(20)   // 20 millimeters
document.Pt(12)   // 12 points
document.In(0.5)  // half inch
document.Pct(50)  // 50% of parent

Value メソッド

func (v Value) Resolve(parentSize, fontSize float64) float64
func (v Value) IsAuto() bool

Resolve() は任意の単位をポイントに変換します。IsAuto() は自動サイズの値かどうかをチェックします。

Edges(ボックスモデル)

Edges

type Edges struct {
    Top, Right, Bottom, Left Value
}

UniformEdges

func UniformEdges(v Value) Edges

すべての辺に同じ値のEdgesを作成します:

document.UniformEdges(document.Mm(20))  // 20mm on all sides

カスタムEdges

document.Edges{
    Top:    document.Mm(25),
    Right:  document.Mm(15),
    Bottom: document.Mm(25),
    Left:   document.Mm(15),
}

Style

Style 構造体はすべての視覚的プロパティを制御します:

type Style struct {
    // Font
    FontFamily     string
    FontSize       float64
    FontWeight     FontWeight    // WeightNormal (400) or WeightBold (700)
    FontStyle      FontStyle     // StyleNormal or StyleItalic

    // Color
    Color          pdf.Color
    Background     *pdf.Color

    // Text layout
    TextAlign      TextAlign     // AlignLeft, AlignCenter, AlignRight, AlignJustify
    LineHeight     float64
    LetterSpacing  float64
    TextIndent     Value
    TextDecoration TextDecoration
    VerticalAlign  VerticalAlign

    // Box model
    Margin  Edges
    Padding Edges
    Border  BorderEdges
}

TextAlign

const (
    AlignLeft    TextAlign = iota  // default
    AlignCenter
    AlignRight
    AlignJustify
)

FontWeight

const (
    WeightNormal FontWeight = 400
    WeightBold   FontWeight = 700
)

TextDecoration

const (
    DecorationNone          TextDecoration = 0
    DecorationUnderline     TextDecoration = 1
    DecorationStrikethrough TextDecoration = 2
    DecorationOverline      TextDecoration = 4
)

装飾はビット演算ORで組み合わせることができます。

VerticalAlign

const (
    VAlignTop    VerticalAlign = iota  // default
    VAlignMiddle
    VAlignBottom
)

テーブルセルの垂直方向の配置に使用します:

template.TableCellVAlign(document.VAlignMiddle)

ドキュメントメタデータ

type DocumentMetadata struct {
    Title    string
    Author   string
    Subject  string
    Creator  string
    Producer string
}
template.WithMetadata(document.DocumentMetadata{
    Title:  "Quarterly Report",
    Author: "ACME Corp",
})

画像タイプ

ImageFitMode

const (
    FitContain  ImageFitMode = iota  // 境界内に収まるようスケール(比率維持)
    FitCover                          // 境界を埋めるようスケール(切り取りの可能性あり)
    FitStretch                        // 境界を埋めるよう引き伸ばし(歪みあり)
    FitOriginal                       // 元の画像サイズを使用
)

ImageFormat

const (
    ImageFormatJPEG ImageFormat = iota
    ImageFormatPNG
)

ノードタイプ

ドキュメントツリーはノードで構成されます:

const (
    NodeDocument NodeType = iota
    NodePage
    NodeBox
    NodeText
    NodeImage
    NodeTable
    NodeList
    NodeRichText
)

ほとんどのユーザーはノードを直接操作しません — テンプレートビルダーが自動的に作成します。

ジオメトリタイプ

type Rectangle struct {
    X, Y, Width, Height float64
}

type Point struct {
    X, Y float64
}