github.com/andybalholm/giopdf@v0.0.0-20220317170119-aad9a095ad48/pdf/content.go (about)

     1  package pdf
     2  
     3  import "io"
     4  
     5  // A ContentStream is a sequence of instructions (operators and operands)
     6  // describing the content of a page.
     7  type ContentStream struct {
     8  	b *buffer
     9  }
    10  
    11  // NewContentStream creates a ContentStream with the contents of r.
    12  func NewContentStream(r io.Reader) *ContentStream {
    13  	b := newBuffer(r, 0)
    14  	b.allowEOF = true
    15  	b.allowObjptr = false
    16  	b.allowStream = false
    17  
    18  	return &ContentStream{b: b}
    19  }
    20  
    21  // ReadInstruction returns the next instruction.
    22  // If the end of the stream is reached, the operator will be the empty string.
    23  func (cs *ContentStream) ReadInstruction() (operands []Value, operator string) {
    24  	for {
    25  		tok := cs.b.readToken()
    26  		if tok == io.EOF {
    27  			return operands, ""
    28  		}
    29  		if kw, ok := tok.(keyword); ok {
    30  			switch kw {
    31  			case "null", "[", "]", "<<", ">>":
    32  				break
    33  			default:
    34  				return operands, string(kw)
    35  			}
    36  		}
    37  		cs.b.unreadToken(tok)
    38  		obj := cs.b.readObject()
    39  		operands = append(operands, Value{nil, objptr{}, obj})
    40  	}
    41  }