content

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package content scans PDF content streams without allocating per operand.

A Scanner walks a decoded content stream one operation at a time. Operands are kept in a buffer the Scanner reuses: numbers are parsed in place, names and strings are slices of the stream (escapes are resolved on demand), and arrays and dictionaries are flattened into the same buffer. Operands are therefore only valid until the next call to Next.

Like go-pdfkit/reader's ContentScanner, broken content does not stop the scan: a malformed token is stepped over, counted, and the operations around it are still returned.

Index

Constants

View Source
const MaxNesting = 32

MaxNesting bounds nested arrays and dictionaries in one operand.

View Source
const MaxOperands = 1 << 12

MaxOperands bounds the operands kept for one operator; producers that emit more (a runaway TJ array is one operand) are truncated from the front, since operators take their operands from the end.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kind

type Kind uint8

Kind is the type of an operand.

const (
	Number Kind = iota
	Name
	String    // (literal); Raw holds the bytes between the parentheses
	HexString // <hex>; Raw holds the bytes between the angle brackets
	Array
	Dict
	Bool
	Null
)

type Operand

type Operand struct {
	Kind Kind
	// Int reports that Num was written without a decimal point.
	Int bool
	// Num is the value of a Number, 1 or 0 for a Bool.
	Num float64
	// Raw is the unresolved text of a Name (without the slash), String or
	// HexString. It points into the content stream.
	Raw []byte
	// contains filtered or unexported fields
}

Operand is one operand of an operation, or an element of an array or a dictionary operand.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner walks a content stream.

func (*Scanner) Arg

func (s *Scanner) Arg(i int) *Operand

Arg returns operand i of the current operation.

func (*Scanner) DictGet

func (s *Scanner) DictGet(o *Operand, key string) *Operand

DictGet returns the value of key in a Dict operand, or nil.

func (*Scanner) Elems

func (s *Scanner) Elems(o *Operand, f func(e *Operand) bool)

Elems calls f for each element of an Array operand (or each key and value of a Dict operand, alternately) until f returns false.

func (*Scanner) Errors

func (s *Scanner) Errors() int

Errors returns the number of malformed tokens stepped over so far.

func (*Scanner) FromEnd

func (s *Scanner) FromEnd(i int) *Operand

FromEnd returns operand i counted from the end (0 is the last), or nil.

func (*Scanner) Image

func (s *Scanner) Image() (dict *Operand, data []byte)

Image returns the dictionary and the still-encoded data of the inline image of a BI operation.

func (*Scanner) Last

func (s *Scanner) Last() *Operand

Last returns the last operand, or nil if there is none.

func (*Scanner) Len

func (s *Scanner) Len() int

Len returns the number of operands of the current operation.

func (*Scanner) NameIs

func (s *Scanner) NameIs(o *Operand, n string) bool

NameIs reports whether o is the name n, with #xx escapes resolved.

func (*Scanner) Next

func (s *Scanner) Next() (op []byte, ok bool)

Next advances to the next operation and returns its operator, or false at the end of the stream. The operator points into the stream.

func (*Scanner) Nums

func (s *Scanner) Nums(v []float64) bool

Nums reads the last len(v) operands, which must all be finite numbers, into v. Operators take their operands from the end, so surplus operands in front are ignored.

func (*Scanner) Op

func (s *Scanner) Op() []byte

Op returns the operator of the current operation.

func (*Scanner) Reset

func (s *Scanner) Reset(data []byte)

Reset starts scanning data, keeping the buffers.

func (*Scanner) Span added in v0.10.0

func (s *Scanner) Span() (start, end int)

Span returns where in the stream the current operation is: from its first operand to past its operator, or past the data of an inline image. A rewriter of content copies or replaces these bytes.

func (*Scanner) Text

func (s *Scanner) Text(o *Operand) []byte

Text returns the resolved bytes of a Name, String or HexString: escapes and #xx sequences decoded. The result is only valid until the next call to Text or Next.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL