Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support single-item array in page Contents key #32

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ func (p Page) GetPlainText(fonts map[string]*Font) (result string, err error) {
}
}()

strm := p.V.Key("Contents")
strm := p.contentStream()

var enc TextEncoding = &nopEncoder{}

if fonts == nil {
Expand Down Expand Up @@ -708,7 +709,7 @@ func (p Page) GetTextByRow() (Rows, error) {
}

func (p Page) walkTextBlocks(walker func(enc TextEncoding, x, y float64, s string)) {
strm := p.V.Key("Contents")
strm := p.contentStream()

fonts := make(map[string]*Font)
for _, font := range p.Fonts() {
Expand Down Expand Up @@ -778,7 +779,7 @@ func (p Page) walkTextBlocks(walker func(enc TextEncoding, x, y float64, s strin

// Content returns the page's content.
func (p Page) Content() Content {
strm := p.V.Key("Contents")
strm := p.contentStream()
var enc TextEncoding = &nopEncoder{}

var g = gstate{
Expand Down Expand Up @@ -998,6 +999,24 @@ func (p Page) Content() Content {
return Content{text, rect}
}

func (p Page) contentStream() Value {
strm := p.V.Key("Contents")

// There are simple cases, in which the Contents key holds an array with
// a single value, we can deal with that. If it contains multiple
// arrays, we would need to concatenate the streams and this is
// something we do not yet support.
if rr, ok := strm.data.(array); ok {
if len(rr) == 1 {
strm = p.V.r.resolve(p.V.ptr, rr[0])
} else {
panic("PDF page contains multiple streams in Contents key, this is not yet supported")
}
}

return strm
}

// TextVertical implements sort.Interface for sorting
// a slice of Text values in vertical order, top to bottom,
// and then left to right within a line.
Expand Down