문서 모델
개요
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 인치) | 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
모든 면에 동일한 값의 여백을 생성합니다:
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
}