Documentation
¶
Overview ¶
Package content provides a builder for PDF content streams — the sequences of operators that render text, graphics, and images on a page (ISO 32000 §7.8.2).
The Stream type accumulates operators into bytes via type-safe methods covering:
- Text rendering: BeginText, SetFont, ShowText, ShowTextArray, MoveTextWithLeading, SetHorizontalScaling (§9.4)
- Path construction: MoveTo, LineTo, CurveTo, Rectangle, ClosePath (§8.5)
- Graphics state: SaveState, RestoreState, SetLineWidth, SetDashPattern (§8.4)
- Color: SetFillColorRGB, SetStrokeColorCMYK, SetFillColorGray (§8.6)
- Shading: ShadingFill (§8.7.4)
- XObjects: Do (§8.8)
- Marked content: BeginMarkedContent, EndMarkedContent, MarkedPoint, MarkedPointWithID (§14.6)
Convenience methods such as Stream.Circle, Stream.Ellipse, and Stream.RoundedRectPerCorner build complex paths from primitives. Call Stream.Bytes or Stream.ToPdfStream to obtain the result.
ISO 32000 operators intentionally not implemented ¶
A few operator families from ISO 32000 are deliberately omitted from this builder:
- Inline images (BI / ID / EI, §8.9.7) — inline images require composing an inline image dictionary followed by raw sample data, which does not fit a linear operator-by-operator builder. Use Stream.Do with an image XObject instead (see the image package).
- Type 3 font glyph metrics (d0 / d1, §9.6.4) — these operators may only appear inside a Type 3 font glyph description, which is out of scope for a general page-content builder.
- Compatibility section markers (BX / EX, §14.9) — these delimit regions where unknown operators must be tolerated by consumers; they serve no purpose when producing content.
Index ¶
- Constants
- type Stream
- func (s *Stream) AppendBytes(data []byte)
- func (s *Stream) BeginArtifact()
- func (s *Stream) BeginMarkedContent(tag string)
- func (s *Stream) BeginMarkedContentActualText(text string)
- func (s *Stream) BeginMarkedContentWithID(tag string, mcid int)
- func (s *Stream) BeginText()
- func (s *Stream) Bytes() []byte
- func (s *Stream) Circle(cx, cy, r float64)
- func (s *Stream) ClipEvenOdd()
- func (s *Stream) ClipNonZero()
- func (s *Stream) ClosePath()
- func (s *Stream) ClosePathFillAndStroke()
- func (s *Stream) ClosePathFillEvenOddAndStroke()
- func (s *Stream) ClosePathStroke()
- func (s *Stream) ConcatMatrix(a, b, c, d, e, f float64)
- func (s *Stream) CurveTo(x1, y1, x2, y2, x3, y3 float64)
- func (s *Stream) CurveToV(x2, y2, x3, y3 float64)
- func (s *Stream) CurveToY(x1, y1, x3, y3 float64)
- func (s *Stream) Do(name string)
- func (s *Stream) Ellipse(cx, cy, rx, ry float64)
- func (s *Stream) EndMarkedContent()
- func (s *Stream) EndPath()
- func (s *Stream) EndText()
- func (s *Stream) Fill()
- func (s *Stream) FillAndStroke()
- func (s *Stream) FillEvenOdd()
- func (s *Stream) FillEvenOddAndStroke()
- func (s *Stream) LineTo(x, y float64)
- func (s *Stream) MarkedPoint(tag string)
- func (s *Stream) MarkedPointWithID(tag string, mcid int)
- func (s *Stream) MoveText(tx, ty float64)
- func (s *Stream) MoveTextWithLeading(tx, ty float64)
- func (s *Stream) MoveTo(x, y float64)
- func (s *Stream) MoveToNextLine()
- func (s *Stream) PrependBytes(data []byte)
- func (s *Stream) Rectangle(x, y, w, h float64)
- func (s *Stream) ReplaceInBytes(old, new string)
- func (s *Stream) RestoreState()
- func (s *Stream) RoundedRect(x, y, w, h, r float64)
- func (s *Stream) RoundedRectPerCorner(x, y, w, h, rTL, rTR, rBR, rBL float64)
- func (s *Stream) RoundedRectPerCornerXY(x, y, w, h float64, rx, ry [4]float64)
- func (s *Stream) SaveState()
- func (s *Stream) SetCharSpacing(charSpace float64)
- func (s *Stream) SetDashPattern(dashArray []float64, phase float64)
- func (s *Stream) SetExtGState(name string)
- func (s *Stream) SetFillColor(components ...float64)
- func (s *Stream) SetFillColorCMYK(c, m, y, k float64)
- func (s *Stream) SetFillColorGray(gray float64)
- func (s *Stream) SetFillColorPattern(patternName string, components ...float64)
- func (s *Stream) SetFillColorRGB(r, g, b float64)
- func (s *Stream) SetFillColorSpace(name string)
- func (s *Stream) SetFlatness(tolerance float64)
- func (s *Stream) SetFont(fontName string, size float64)
- func (s *Stream) SetHorizontalScaling(scale float64)
- func (s *Stream) SetLeading(leading float64)
- func (s *Stream) SetLineCap(style int)
- func (s *Stream) SetLineJoin(style int)
- func (s *Stream) SetLineWidth(width float64)
- func (s *Stream) SetMiterLimit(limit float64)
- func (s *Stream) SetRenderingIntent(name string)
- func (s *Stream) SetStrokeColor(components ...float64)
- func (s *Stream) SetStrokeColorCMYK(c, m, y, k float64)
- func (s *Stream) SetStrokeColorGray(gray float64)
- func (s *Stream) SetStrokeColorPattern(patternName string, components ...float64)
- func (s *Stream) SetStrokeColorRGB(r, g, b float64)
- func (s *Stream) SetStrokeColorSpace(name string)
- func (s *Stream) SetTextMatrix(a, b, c, d, e, f float64)
- func (s *Stream) SetTextRenderingMode(mode int)
- func (s *Stream) SetTextRise(rise float64)
- func (s *Stream) SetWordSpacing(wordSpace float64)
- func (s *Stream) ShadingFill(name string)
- func (s *Stream) ShowText(text string)
- func (s *Stream) ShowTextArray(elements []TextArrayElement)
- func (s *Stream) ShowTextArrayHex(elements []TextArrayElement)
- func (s *Stream) ShowTextHex(data []byte)
- func (s *Stream) ShowTextNextLine(text string)
- func (s *Stream) ShowTextWithSpacing(wordSpace, charSpace float64, text string)
- func (s *Stream) Stroke()
- func (s *Stream) ToPdfStream() *core.PdfStream
- type TextArrayElement
Constants ¶
const ( TextRenderFill = 0 // Fill text (default) TextRenderStroke = 1 // Stroke text TextRenderFillStroke = 2 // Fill then stroke text TextRenderInvisible = 3 // Invisible text (for searchable OCR layers) TextRenderFillClip = 4 // Fill text and add to clipping path TextRenderStrokeClip = 5 // Stroke text and add to clipping path TextRenderFillStrokeClip = 6 // Fill, stroke, and clip TextRenderClip = 7 // Add text to clipping path only )
TextRenderingMode constants (ISO 32000 §9.3.6).
const ( LineCapButt = 0 // Butt cap (default) — stroke ends at endpoint LineCapRound = 1 // Round cap — semicircle at endpoint LineCapSquare = 2 // Square cap — half-square at endpoint )
Line cap style constants (ISO 32000 §8.4.3.3).
const ( LineJoinMiter = 0 // Miter join (default) — sharp corners LineJoinRound = 1 // Round join — rounded corners LineJoinBevel = 2 // Bevel join — flat corners )
Line join style constants (ISO 32000 §8.4.3.4).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream builds a PDF content stream from a sequence of operators. The resulting bytes go inside a PdfStream on a page's /Contents.
func (*Stream) AppendBytes ¶
AppendBytes appends raw content stream bytes after the existing content.
func (*Stream) BeginArtifact ¶ added in v0.9.1
func (s *Stream) BeginArtifact()
BeginArtifact writes the BDC operator opening an /Artifact marked-content sequence. Artifacts are page elements that are not part of the document's logical content — pagination headers/footers, backgrounds, and layout decorations — and are excluded from the structure tree (ISO 32000-2 §14.8.2.2, PDF/UA ISO 14289-1 §7.1). Pair every call with EndMarkedContent.
func (*Stream) BeginMarkedContent ¶
BeginMarkedContent writes the BMC operator: begin a marked content sequence. tag is the structure type (e.g. "P", "Span", "Figure").
func (*Stream) BeginMarkedContentActualText ¶ added in v0.7.0
BeginMarkedContentActualText writes the BDC operator with a /Span tag and a property list containing /ActualText. Per ISO 32000-2 §14.9.4, the /ActualText entry overrides the displayed text for accessibility, search, and copy/paste, while the rendered glyphs remain unchanged. The text is encoded as a UTF-16BE PDF text string with the byte-order mark FEFF, per ISO 32000-2 §7.9.2.2. Pair every call with a single EndMarkedContent; nested ActualText sequences are not supported by this helper.
func (*Stream) BeginMarkedContentWithID ¶
BeginMarkedContentWithID writes the BDC operator: begin a marked content sequence with a property dictionary containing an MCID. tag is the structure type; mcid links this content to the structure tree.
func (*Stream) BeginText ¶
func (s *Stream) BeginText()
BeginText writes the BT (begin text object) operator.
func (*Stream) Bytes ¶
Bytes returns the content stream as raw bytes, suitable for embedding in a PdfStream.
func (*Stream) Circle ¶
Circle appends a circular path centered at (cx, cy) with the given radius. Uses four cubic Bézier curves (the standard approximation). Does NOT stroke or fill — call Stroke(), Fill(), etc. after.
func (*Stream) ClipEvenOdd ¶
func (s *Stream) ClipEvenOdd()
ClipEvenOdd writes the W* operator: set the clipping path using even-odd rule.
func (*Stream) ClipNonZero ¶
func (s *Stream) ClipNonZero()
ClipNonZero writes the W operator: set the clipping path using non-zero winding rule. Must be followed by a path painting operator (n for no-op paint).
func (*Stream) ClosePath ¶
func (s *Stream) ClosePath()
ClosePath writes the h operator: close the current subpath.
func (*Stream) ClosePathFillAndStroke ¶
func (s *Stream) ClosePathFillAndStroke()
ClosePathFillAndStroke writes the b operator: close path, fill, and stroke.
func (*Stream) ClosePathFillEvenOddAndStroke ¶ added in v0.7.0
func (s *Stream) ClosePathFillEvenOddAndStroke()
ClosePathFillEvenOddAndStroke writes the b* operator: close the path, fill it using the even-odd rule, and stroke it.
func (*Stream) ClosePathStroke ¶
func (s *Stream) ClosePathStroke()
ClosePathStroke writes the s operator: close path and stroke.
func (*Stream) ConcatMatrix ¶
ConcatMatrix writes the cm operator: concatenate a transformation matrix. The six values define the matrix [a b c d e f]. Common use: cm(width, 0, 0, height, x, y) to place an image.
func (*Stream) CurveTo ¶
CurveTo writes the c operator: append a cubic Bézier curve. (x1, y1) and (x2, y2) are control points; (x3, y3) is the endpoint.
func (*Stream) CurveToV ¶
CurveToV writes the v operator: append a cubic Bézier curve with the first control point at the current point. (x2, y2) is the second control point; (x3, y3) is the endpoint.
func (*Stream) CurveToY ¶
CurveToY writes the y operator: append a cubic Bézier curve with the second control point at the endpoint. (x1, y1) is the first control point; (x3, y3) is the endpoint.
func (*Stream) Do ¶
Do writes the Do operator: paint the named XObject. name is the resource name (e.g. "Im1").
func (*Stream) Ellipse ¶
Ellipse appends an elliptical path centered at (cx, cy) with radii rx, ry. Uses four cubic Bézier curves.
func (*Stream) EndMarkedContent ¶
func (s *Stream) EndMarkedContent()
EndMarkedContent writes the EMC operator: end the current marked content sequence.
func (*Stream) EndPath ¶
func (s *Stream) EndPath()
EndPath writes the n operator: end the path without filling or stroking. Often used after clipping operators.
func (*Stream) EndText ¶
func (s *Stream) EndText()
EndText writes the ET (end text object) operator.
func (*Stream) Fill ¶
func (s *Stream) Fill()
Fill writes the f operator: fill the current path (non-zero winding).
func (*Stream) FillAndStroke ¶
func (s *Stream) FillAndStroke()
FillAndStroke writes the B operator: fill and stroke the current path.
func (*Stream) FillEvenOdd ¶
func (s *Stream) FillEvenOdd()
FillEvenOdd writes the f* operator: fill using even-odd rule.
func (*Stream) FillEvenOddAndStroke ¶ added in v0.7.0
func (s *Stream) FillEvenOddAndStroke()
FillEvenOddAndStroke writes the B* operator: fill the current path using the even-odd rule, then stroke it.
func (*Stream) MarkedPoint ¶ added in v0.7.0
MarkedPoint writes the MP operator: designate a marked-content point. tag is the marked-content tag (structure type).
func (*Stream) MarkedPointWithID ¶ added in v0.7.0
MarkedPointWithID writes the DP operator: designate a marked-content point with an MCID property that links to the structure tree.
func (*Stream) MoveText ¶
MoveText writes the Td operator: move to (tx, ty) relative to the start of the current line.
func (*Stream) MoveTextWithLeading ¶ added in v0.7.0
MoveTextWithLeading writes the TD operator: move to (tx, ty) relative to the start of the current line and set the leading to -ty. Equivalent to calling SetLeading(-ty) followed by MoveText(tx, ty), but in a single operator.
func (*Stream) MoveToNextLine ¶
func (s *Stream) MoveToNextLine()
MoveToNextLine writes the T* operator: move to the start of the next line.
func (*Stream) PrependBytes ¶
PrependBytes inserts raw content stream bytes before any existing content. This is used for watermarks and other background elements that must be drawn before the page's main content.
func (*Stream) Rectangle ¶
Rectangle writes the re operator: append a rectangle. (x, y) is the lower-left corner; w and h are width and height.
func (*Stream) ReplaceInBytes ¶
ReplaceInBytes performs a byte-level replacement in the content stream. Used for second-pass substitutions like total page count placeholders.
func (*Stream) RestoreState ¶
func (s *Stream) RestoreState()
RestoreState writes the Q operator: restore the graphics state.
func (*Stream) RoundedRect ¶
RoundedRect appends a rounded rectangle path. (x, y) is the lower-left corner; w and h are width and height; r is the corner radius (clamped to half the smaller dimension).
func (*Stream) RoundedRectPerCorner ¶ added in v0.6.0
RoundedRectPerCorner draws a rounded rectangle with different (circular) radii per corner. The radii are rTL (top-left), rTR (top-right), rBR (bottom-right), rBL (bottom-left). In PDF coordinates y increases upward, so (x, y) is the bottom-left of the rect.
Radii are proportionally reduced so that no edge is over-subscribed by its two adjacent corners (the CSS border-radius algorithm, CSS Backgrounds and Borders Module Level 3 §5.5). Negative radii are treated as 0.
This is a circular special case of RoundedRectPerCornerXY: it delegates with rx == ry per corner so there is a single drawing implementation.
func (*Stream) RoundedRectPerCornerXY ¶ added in v0.9.1
RoundedRectPerCornerXY draws a rounded rectangle with independent horizontal (rx) and vertical (ry) radii per corner, producing elliptical corners. The arrays are indexed [TL, TR, BR, BL]. A corner with rx == ry is circular. In PDF coordinates y increases upward, so (x, y) is the bottom-left of the rect.
Radii are proportionally reduced so that no edge is over-subscribed by its two adjacent corners (the generalized CSS border-radius algorithm, CSS Backgrounds and Borders Module Level 3 §5.5): the horizontal radii constrain the top/bottom edges against w, and the vertical radii constrain the left/right edges against h. A single reduction factor f scales every rx and ry. Negative radii are treated as 0.
func (*Stream) SaveState ¶
func (s *Stream) SaveState()
SaveState writes the q operator: save the current graphics state.
func (*Stream) SetCharSpacing ¶
SetCharSpacing writes the Tc operator: set character spacing. charSpace is extra spacing added between each character, in text space units.
func (*Stream) SetDashPattern ¶
SetDashPattern writes the d operator: set line dash pattern. dashArray defines the pattern (e.g. [3 2] for 3-on, 2-off). phase is the offset into the pattern to start at.
func (*Stream) SetExtGState ¶
SetExtGState writes the gs operator: set graphics state from an ExtGState resource dictionary. name is the resource name (e.g. "GS1").
func (*Stream) SetFillColor ¶ added in v0.7.0
SetFillColor writes the sc operator: set the fill color using the current fill color space. The number of components depends on the color space (1 for Gray, 3 for RGB, 4 for CMYK). For ICCBased or Pattern spaces use SetFillColorPattern.
func (*Stream) SetFillColorCMYK ¶
SetFillColorCMYK writes the k operator: set fill color in DeviceCMYK. c, m, y, k are in [0, 1].
func (*Stream) SetFillColorGray ¶
SetFillColorGray writes the g operator: set fill color in DeviceGray.
func (*Stream) SetFillColorPattern ¶ added in v0.7.0
SetFillColorPattern writes the scn operator: set the fill color using a pattern from a Pattern color space, optionally preceded by tint components for uncolored patterns. Pass empty patternName to emit scn with components only (useful for DeviceN / ICCBased / Lab color spaces that sc does not support).
func (*Stream) SetFillColorRGB ¶
SetFillColorRGB writes the rg operator: set fill color in DeviceRGB. r, g, b are in [0, 1].
func (*Stream) SetFillColorSpace ¶ added in v0.7.0
SetFillColorSpace writes the cs operator: set the current color space for filling and non-stroking paint operations.
func (*Stream) SetFlatness ¶ added in v0.7.0
SetFlatness writes the i operator: set the flatness tolerance for curve rendering. Valid range is 0 to 100; 0 uses the output device's default. Higher values allow coarser curve approximation (faster but less accurate).
func (*Stream) SetFont ¶
SetFont writes the Tf operator: set font and size. fontName is the resource name (e.g. "F1"), size is in points.
func (*Stream) SetHorizontalScaling ¶ added in v0.7.0
SetHorizontalScaling writes the Tz operator: set the horizontal scaling, as a percentage of normal width. 100 means normal width; 50 means half width (condensed); 200 means double width (expanded).
func (*Stream) SetLeading ¶
SetLeading writes the TL operator: set the text leading (line spacing).
func (*Stream) SetLineCap ¶
SetLineCap writes the J operator: set line cap style (0-2).
func (*Stream) SetLineJoin ¶
SetLineJoin writes the j operator: set line join style (0-2).
func (*Stream) SetLineWidth ¶
SetLineWidth writes the w operator: set the line width. width must be >= 0; a value of 0 denotes the thinnest line the output device can render.
func (*Stream) SetMiterLimit ¶
SetMiterLimit writes the M operator: set miter limit for line joins. limit must be >= 1 (ISO 32000 §8.4.3.5).
func (*Stream) SetRenderingIntent ¶ added in v0.7.0
SetRenderingIntent writes the ri operator: set the color rendering intent. Standard values are "AbsoluteColorimetric", "RelativeColorimetric", "Saturation", and "Perceptual".
func (*Stream) SetStrokeColor ¶ added in v0.7.0
SetStrokeColor writes the SC operator: set the stroke color using the current stroke color space. The number of components depends on the color space (1 for Gray, 3 for RGB, 4 for CMYK). For ICCBased or Pattern spaces use SetStrokeColorPattern.
func (*Stream) SetStrokeColorCMYK ¶
SetStrokeColorCMYK writes the K operator: set stroke color in DeviceCMYK. c, m, y, k are in [0, 1].
func (*Stream) SetStrokeColorGray ¶
SetStrokeColorGray writes the G operator: set stroke color in DeviceGray. gray is in [0, 1] where 0=black, 1=white.
func (*Stream) SetStrokeColorPattern ¶ added in v0.7.0
SetStrokeColorPattern writes the SCN operator: set the stroke color using a pattern from a Pattern color space, optionally preceded by tint components for uncolored patterns. Pass empty patternName to emit SCN with components only (useful for DeviceN / ICCBased / Lab color spaces that SC does not support).
func (*Stream) SetStrokeColorRGB ¶
SetStrokeColorRGB writes the RG operator: set stroke color in DeviceRGB. r, g, b are in [0, 1].
func (*Stream) SetStrokeColorSpace ¶ added in v0.7.0
SetStrokeColorSpace writes the CS operator: set the current color space for stroking. name is a device color space ("DeviceRGB", "DeviceGray", "DeviceCMYK", "Pattern") or the resource name of a color space in the current resource dictionary.
func (*Stream) SetTextMatrix ¶
SetTextMatrix writes the Tm operator: set the text matrix. The six values [a b c d e f] define the text position and transformation. Common use: Tm(1, 0, 0, 1, x, y) positions text at (x, y). For rotated text: Tm(cos, sin, -sin, cos, x, y).
func (*Stream) SetTextRenderingMode ¶
SetTextRenderingMode writes the Tr operator: set text rendering mode. mode is one of the TextRender* constants (0-7).
func (*Stream) SetTextRise ¶
SetTextRise writes the Ts operator: set text rise. rise shifts text up (positive) or down (negative) from the baseline, in text space units. Used for superscript and subscript.
func (*Stream) SetWordSpacing ¶
SetWordSpacing writes the Tw operator: set word spacing. wordSpace is extra spacing added after each ASCII space character (0x20), in text space units.
func (*Stream) ShadingFill ¶ added in v0.7.0
ShadingFill writes the sh operator: paint the shape and color defined by a shading dictionary within the current clipping region. name is the resource name of a shading in the current Pattern resource dictionary.
func (*Stream) ShowTextArray ¶
func (s *Stream) ShowTextArray(elements []TextArrayElement)
ShowTextArray writes the TJ operator: show text with per-glyph positioning. Each element in the array is either a string (shown as text) or a numeric adjustment (in thousandths of a unit of text space — negative values move right). This is used for kerning: [(H) -80 (ello)] TJ shifts "ello" 80 units right.
func (*Stream) ShowTextArrayHex ¶
func (s *Stream) ShowTextArrayHex(elements []TextArrayElement)
ShowTextArrayHex writes the TJ operator with hex-encoded strings. Used for CIDFont text with kerning adjustments.
func (*Stream) ShowTextHex ¶
ShowTextHex writes the Tj operator with a hex-encoded string. Used for CIDFont text where each glyph ID is encoded as a big-endian uint16 pair.
func (*Stream) ShowTextNextLine ¶
ShowTextNextLine writes the ' operator: move to next line and show text.
func (*Stream) ShowTextWithSpacing ¶ added in v0.7.0
ShowTextWithSpacing writes the " operator: set word spacing, set character spacing, move to the next line, and show the given text. Equivalent to SetWordSpacing(wordSpace) + SetCharSpacing(charSpace) + ShowTextNextLine(text).
func (*Stream) Stroke ¶
func (s *Stream) Stroke()
Stroke writes the S operator: stroke the current path.
func (*Stream) ToPdfStream ¶
ToPdfStream wraps the content stream bytes in a compressed core.PdfStream. FlateDecode compression typically reduces content stream size by 60-80%.
type TextArrayElement ¶
type TextArrayElement struct {
Text string // non-empty for text segments
HexData []byte // non-nil for hex-encoded segments (CIDFont)
Adjustment float64 // kerning adjustment (thousandths of text space unit)
IsAdjustment bool // true if this is a numeric adjustment, not text
}
TextArrayElement is a single element in a TJ text array. Either a text string or a numeric positioning adjustment.