diff --git a/pdf/core/crossrefs.go b/pdf/core/crossrefs.go index 70f15b31..cefa1de6 100644 --- a/pdf/core/crossrefs.go +++ b/pdf/core/crossrefs.go @@ -25,10 +25,10 @@ const ( XREF_OBJECT_STREAM = iota ) -// Either can be in a normal xref table, or in an xref stream. -// Can point either to a file offset, or to an object stream. -// XrefFileOffset or XrefObjectStream... - +// XrefObject defines a cross reference entry which is a map between object number (with generation number) and the +// location of the actual object, either as a file offset (xref table entry), or as a location within an xref +// stream object (xref object stream). +// TODO (v3): Unexport. type XrefObject struct { xtype int objectNumber int @@ -40,16 +40,26 @@ type XrefObject struct { osObjIndex int } +// XrefTable is a map between object number and corresponding XrefObject. +// TODO (v3): Unexport. type XrefTable map[int]XrefObject +// ObjectStream represents an object stream's information which can contain multiple indirect objects. +// The information specifies the number of objects and has information about offset locations for +// each object. +// TODO (v3): Unexport. type ObjectStream struct { - N int + N int // TODO (v3): Unexport. ds []byte offsets map[int]int64 } +// ObjectStreams defines a map between object numbers (object streams only) and underlying ObjectStream information. type ObjectStreams map[int]ObjectStream +// ObjectCache defines a map between object numbers and corresponding PdfObject. Serves as a cache for PdfObjects that +// have already been parsed. +// TODO (v3): Unexport. type ObjectCache map[int]PdfObject // Get an object from an object stream. @@ -114,7 +124,7 @@ func (this *PdfParser) lookupObjectViaOS(sobjNumber int, objNum int) (PdfObject, common.Log.Trace("Parsing offset map") // Load the offset map (relative to the beginning of the stream...) - var offsets map[int]int64 = make(map[int]int64) + offsets := map[int]int64{} // Object list and offsets. for i := 0; i < int(*N); i++ { this.skipSpaces() @@ -182,11 +192,10 @@ func (this *PdfParser) lookupObjectViaOS(sobjNumber int, objNum int) (PdfObject, return &io, nil } -// Currently a bit messy.. multiple wrappers. Can we clean up? - -// Outside interface for lookupByNumberWrapper. Default attempts -// repairs of bad xref tables. +// LookupByNumber looks up a PdfObject by object number. Returns an error on failure. +// TODO (v3): Unexport. func (this *PdfParser) LookupByNumber(objNumber int) (PdfObject, error) { + // Outside interface for lookupByNumberWrapper. Default attempts repairs of bad xref tables. obj, _, err := this.lookupByNumberWrapper(objNumber, true) return obj, err } @@ -318,13 +327,14 @@ func (this *PdfParser) lookupByNumber(objNumber int, attemptRepairs bool) (PdfOb return nil, false, errors.New("Unknown xref type") } -// LookupByReference +// LookupByReference looks up a PdfObject by a reference. func (this *PdfParser) LookupByReference(ref PdfObjectReference) (PdfObject, error) { common.Log.Trace("Looking up reference %s", ref.String()) return this.LookupByNumber(int(ref.ObjectNumber)) } -// Trace to direct object. +// Trace traces a PdfObject to direct object, looking up and resolving references as needed (unlike TraceToDirect). +// TODO (v3): Unexport. func (this *PdfParser) Trace(obj PdfObject) (PdfObject, error) { ref, isRef := obj.(*PdfObjectReference) if !isRef { diff --git a/pdf/core/stream.go b/pdf/core/stream.go index fea5e667..fa1a8e30 100644 --- a/pdf/core/stream.go +++ b/pdf/core/stream.go @@ -11,7 +11,7 @@ import ( "github.com/unidoc/unidoc/common" ) -// Creates the encoder from the stream's dictionary. +// NewEncoderFromStream creates a StreamEncoder based on the stream's dictionary. func NewEncoderFromStream(streamObj *PdfObjectStream) (StreamEncoder, error) { filterObj := streamObj.PdfObjectDictionary.Get("Filter") if filterObj == nil { @@ -71,8 +71,8 @@ func NewEncoderFromStream(streamObj *PdfObjectStream) (StreamEncoder, error) { } } -// Decodes the stream. -// Supports FlateDecode, ASCIIHexDecode, LZW. +// DecodeStream decodes the stream data and returns the decoded data. +// An error is returned upon failure. func DecodeStream(streamObj *PdfObjectStream) ([]byte, error) { common.Log.Trace("Decode stream") @@ -92,8 +92,7 @@ func DecodeStream(streamObj *PdfObjectStream) ([]byte, error) { return decoded, nil } -// Encodes the stream. -// Uses the encoding specified by the object. +// EncodeStream encodes the stream data using the encoded specified by the stream's dictionary. func EncodeStream(streamObj *PdfObjectStream) error { common.Log.Trace("Encode stream") diff --git a/pdf/core/symbols.go b/pdf/core/symbols.go index d664eecc..e076a7a9 100644 --- a/pdf/core/symbols.go +++ b/pdf/core/symbols.go @@ -5,6 +5,8 @@ package core +// IsWhiteSpace checks if byte represents a white space character. +// TODO (v3): Unexport. func IsWhiteSpace(ch byte) bool { // Table 1 white-space characters (7.2.2 Character Set) // spaceCharacters := string([]byte{0x00, 0x09, 0x0A, 0x0C, 0x0D, 0x20}) @@ -15,10 +17,14 @@ func IsWhiteSpace(ch byte) bool { } } +// IsFloatDigit checks if a character can be a part of a float number string. +// TODO (v3): Unexport. func IsFloatDigit(c byte) bool { return ('0' <= c && c <= '9') || c == '.' } +// IsDecimalDigit checks if the character is a part of a decimal number string. +// TODO (v3): Unexport. func IsDecimalDigit(c byte) bool { if c >= '0' && c <= '9' { return true @@ -27,6 +33,8 @@ func IsDecimalDigit(c byte) bool { } } +// IsOctalDigit checks if a character can be part of an octal digit string. +// TODO (v3): Unexport. func IsOctalDigit(c byte) bool { if c >= '0' && c <= '7' { return true @@ -35,8 +43,10 @@ func IsOctalDigit(c byte) bool { } } +// IsPrintable checks if a character is printable. // Regular characters that are outside the range EXCLAMATION MARK(21h) // (!) to TILDE (7Eh) (~) should be written using the hexadecimal notation. +// TODO (v3): Unexport. func IsPrintable(char byte) bool { if char < 0x21 || char > 0x7E { return false @@ -44,6 +54,8 @@ func IsPrintable(char byte) bool { return true } +// IsDelimiter checks if a character represents a delimiter. +// TODO (v3): Unexport. func IsDelimiter(char byte) bool { if char == '(' || char == ')' { return true