Documentation
¶
Overview ¶
Package content defines the domain model for PDF page content.
This package contains the Content interface and related types that represent elements that can be placed on PDF pages (text, images, shapes, etc.).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Content ¶
type Content interface {
// Render writes the PDF content stream operators for this element.
//
// The implementation should write valid PDF operators to the writer.
// For example, text might write: BT, Tf, Tm, Tj, ET
//
// Returns an error if rendering fails.
Render(w io.Writer) error
// Bounds returns the bounding rectangle of the content in page coordinates.
//
// The rectangle defines the area occupied by this content element.
// This is useful for:
// - Layout calculations
// - Collision detection
// - Content positioning
Bounds() types.Rectangle
// Validate checks if the content is valid and ready to be rendered.
//
// Returns an error if:
// - Required fields are missing (e.g., text without font)
// - Values are out of valid range
// - Content is malformed
Validate() error
// Type returns the content type identifier.
//
// This is primarily for debugging, logging, and type discrimination.
// Examples: "text", "image", "path", "table"
Type() ContentType
}
Content represents any element that can be placed on a PDF page.
This is the core interface in the content domain. All renderable elements (text, images, shapes, tables, forms, etc.) must implement this interface.
Design principles: - Each content element knows how to render itself (Rich Domain Model) - Content is position-aware (has bounds) - Content is validatable - Content provides metadata for debugging/logging
Example implementations: - TextContent: Rendered text with font, size, position - ImageContent: Embedded images with scaling - PathContent: Vector graphics (lines, rectangles, etc.) - TableContent: Complex table layouts
type ContentType ¶
type ContentType string
ContentType identifies the type of content.
This is used for: - Debugging and logging - Type assertions when needed - Content statistics
const ( // ContentTypeText represents text content. ContentTypeText ContentType = "text" // ContentTypeImage represents image content. ContentTypeImage ContentType = "image" // ContentTypePath represents vector graphics (paths, lines, shapes). ContentTypePath ContentType = "path" // ContentTypeTable represents table content. ContentTypeTable ContentType = "table" // ContentTypeForm represents form field content. ContentTypeForm ContentType = "form" // ContentTypeAnnotation represents annotations and markup. ContentTypeAnnotation ContentType = "annotation" )
func (ContentType) IsValid ¶
func (ct ContentType) IsValid() bool
IsValid checks if the content type is valid.
func (ContentType) String ¶
func (ct ContentType) String() string
String returns the string representation of the content type.
type MockContent ¶
type MockContent struct {
// RenderFunc is called when Render() is invoked.
// If nil, a default implementation writes "mock content".
RenderFunc func(w io.Writer) error
// BoundsValue is returned by Bounds().
// If zero, a default 100x100 rectangle is returned.
BoundsValue types.Rectangle
// ValidateFunc is called when Validate() is invoked.
// If nil, returns no error.
ValidateFunc func() error
// TypeValue is returned by Type().
// If empty, returns ContentTypeText.
TypeValue ContentType
}
MockContent is a mock implementation of Content for testing.
This is exported so other packages can use it in their tests. It provides a simple way to test code that depends on the Content interface.
Example usage:
mock := &content.MockContent{
BoundsValue: types.MustRectangle(0, 0, 100, 100),
TypeValue: content.ContentTypeText,
}
page.AddContent(mock)
Example ¶
Example test showing how to use MockContent in other packages
// Create a mock content element
mock := &MockContent{
BoundsValue: types.MustRectangle(100, 200, 300, 400),
TypeValue: ContentTypeText,
}
// Use it as Content interface
var content Content = mock
// Test rendering
var buf bytes.Buffer
_ = content.Render(&buf)
// Get bounds
bounds := content.Bounds()
_ = bounds.Width() // 200
_ = bounds.Height() // 200
// Validate
_ = content.Validate()
// Get type
_ = content.Type() // ContentTypeText
func (*MockContent) Bounds ¶
func (m *MockContent) Bounds() types.Rectangle
Bounds implements Content.Bounds().
func (*MockContent) Render ¶
func (m *MockContent) Render(w io.Writer) error
Render implements Content.Render().
func (*MockContent) Validate ¶
func (m *MockContent) Validate() error
Validate implements Content.Validate().