PDFプリミティブ

概要

pdf パッケージ(Layer 1)は低レベルのPDFプリミティブを提供します。ほとんどのユーザーはこのパッケージのカラー関数のみを必要とします — その他はレイアウトエンジンが内部的に使用します。

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

カラー

カラーはpdfパッケージで最もよく使われるタイプです。

カラーコンストラクタ

// RGB (float values 0.0 – 1.0)
pdf.RGB(1.0, 0.5, 0.0)     // Orange

// Hex (uint32)
pdf.RGBHex(0xFF6B6B)       // Coral
pdf.RGBHex(0x1A237E)       // Dark blue

// Grayscale (0.0 = black, 1.0 = white)
pdf.Gray(0.0)              // Black
pdf.Gray(0.5)              // Medium gray
pdf.Gray(1.0)              // White

// CMYK (for print)
pdf.CMYK(0, 1, 1, 0)      // Red in CMYK

定義済みカラー

pdf.Black    // Gray(0)
pdf.White    // Gray(1)
pdf.Red      // RGB(1, 0, 0)
pdf.Green    // RGB(0, 1, 0)
pdf.Blue     // RGB(0, 0, 1)
pdf.Yellow   // RGB(1, 1, 0)
pdf.Cyan     // RGB(0, 1, 1)
pdf.Magenta  // RGB(1, 0, 1)

カラーの使用

カラーは TextColor()BgColor() のテキストオプションで使用します:

c.Text("Red on yellow",
    template.TextColor(pdf.Red),
    template.BgColor(pdf.Yellow),
)

テーブル/罫線のオプションでも使用できます:

// Table header
template.TableHeaderStyle(
    template.TextColor(pdf.White),
    template.BgColor(pdf.RGBHex(0x1A237E)),
)

// Line color
template.LineColor(pdf.Blue)

カラースペース

type ColorSpace int

const (
    ColorSpaceRGB  ColorSpace = iota
    ColorSpaceGray
    ColorSpaceCMYK
)

Color型

type Color struct {
    R, G, B float64     // RGB components (0.0 – 1.0)
    A       float64     // Alpha or K (for CMYK)
    Space   ColorSpace
}

Writer(上級者向け)

PDF Writerは低レベルのPDFオブジェクト作成を処理します。ほとんどのユーザーはこれを直接使用する必要はありません。

type Writer struct {}

func NewWriter(w io.Writer) *Writer
func (pw *Writer) AllocObject() ObjectRef
func (pw *Writer) WriteObject(ref ObjectRef, obj Object) error
func (pw *Writer) RegisterFont(name string, data []byte) error
func (pw *Writer) AddPage(page *PageContent) error
func (pw *Writer) Close() error
func (pw *Writer) SetDocumentInfo(info DocumentInfo)

PDFオブジェクト(上級者向け)

低レベルのPDFオブジェクトタイプ:

// Scalar types
type Name string
type LiteralString string
type HexString string
type Integer int64
type Real float64
type Boolean bool
type Null struct{}

// Composite types
type Dict map[string]Object
type Array []Object

// Stream (content, images, fonts)
type Stream struct {
    Data       []byte
    Dictionary Dict
}

// Object reference
type ObjectRef struct {
    Number     int
    Generation int
}

Fontパッケージ(上級者向け)

pdf/font サブパッケージはTrueTypeフォントの解析とサブセッティングを処理します:

import "github.com/gpdf-dev/gpdf/pdf/font"

// Parse a TrueType font
ttf, err := font.ParseTrueType(data)

// Get font name and metrics
name := ttf.Name()
metrics := ttf.Metrics()  // Ascender, Descender, CapHeight, XHeight

// Create a subset with only used glyphs
subset, err := ttf.Subset(runes)

// Measure text width
width := font.MeasureString(ttf, "Hello", 12.0)

// Break text into lines
lines := font.LineBreak(ttf, text, 12.0, maxWidth)

アーキテクチャ

Layer 3: template ─── Builder API, JSON Schema, Components
    │
    ▼
Layer 2: document ─── Nodes, Box Model, Layout Engine
    │
    ▼
Layer 1: pdf ──────── Writer, Streams, Fonts, Images
    │
    ▼
          io.Writer (file, HTTP response, buffer...)

各レイヤーはその下のレイヤーにのみ依存します — 上方向への依存はありません。