func skipComment(f fancy.Reader) { for { c, err := f.ReadByte() if err != nil || c == 13 || c == 10 { break } } }
// xrefStart() queries the start of the xref-table in a PDF file. func xrefStart(f fancy.Reader) int { s := int(f.Size()) pdf := make([]byte, min(s, 1024)) f.ReadAt(pdf, int64(max(0, s-1024))) ps := xref.AllMatches(pdf, 0) if ps == nil { return -1 } return num(xref.MatchSlices(ps[len(ps)-1])[2]) }
func skipSpaces(f fancy.Reader) byte { for { c, err := f.ReadByte() if err != nil { break } if c > 32 { return c } } return 0 }
func skipString(f fancy.Reader) { for depth := 1; depth > 0; { c, err := f.ReadByte() if err != nil { break } switch c { case '(': depth++ case ')': depth-- case '\\': f.ReadByte() } } }
func skipToDelim(f fancy.Reader) byte { for { c, err := f.ReadByte() if err != nil { break } if c < 33 { return c } switch c { case '<', '>', '(', ')', '[', ']', '/', '%', '{', '}': return c } } return 255 }
// xrefSkip() queries the start of the trailer for a (partial) xref-table. func xrefSkip(f fancy.Reader, xref int) int { f.Seek(int64(xref), 0) t, p := ps.Token(f) if string(t) != "xref" { return -1 } for { t, p = ps.Token(f) if t[0] < '0' || t[0] > '9' { f.Seek(p, 0) break } t, _ = ps.Token(f) ps.SkipLE(f) f.Seek(int64(num(t)*20), 1) } r, _ := f.Seek(0, 1) return int(r) }
func refToken(f fancy.Reader) ([]byte, int64) { tok, p := ps.Token(f) if len(tok) > 0 && tok[0] >= '0' && tok[0] <= '9' { ps.Token(f) r, q := ps.Token(f) if string(r) == "R" { f.Seek(p, 0) tok = f.Slice(int(1 + q - p)) } else { f.Seek(p+int64(len(tok)), 0) } } return tok, p }
func SkipLE(f fancy.Reader) { for { c, err := f.ReadByte() if err != nil { return } if c > 32 { f.UnreadByte() return } if c == 13 { c, err = f.ReadByte() if err == nil && c != 10 { f.UnreadByte() } return } if c == 10 { return } } }
// xrefRead() reads the xref table(s) of a PDF file. This is not recursive // in favour of not to have to keep track of already used starting points // for xrefs. func xrefRead(f fancy.Reader, p int) map[int]int { var back [MAX_PDF_UPDATES]int b := 0 s := _Bytes for ok := true; ok; { back[b] = p b++ p = xrefSkip(f, p) f.Seek(int64(p), 0) s, _ = ps.Token(f) if string(s) != "trailer" { return nil } s, _ = ps.Token(f) s, ok = Dictionary(s)["/Prev"] p = num(s) } r := make(map[int]int) for b != 0 { b-- f.Seek(int64(back[b]), 0) ps.Token(f) // skip "xref" for { m := tupel(f, 2) if string(m[0]) == "trailer" { break } ps.SkipLE(f) o := num(m[0]) dat := f.Slice(num(m[1]) * 20) for i := 0; i < len(dat); i += 20 { if dat[i+17] != 'n' { r[o] = 0, false } else { r[o] = num(dat[i : i+10]) } o++ } } } return r }
func Token(f fancy.Reader) ([]byte, int64) { again: c := skipSpaces(f) if c == 0 { return []byte{}, -1 } p := fpos(f) - 1 switch c { case '%': skipComment(f) goto again case '<', '[', '{': skipComposite(f) case '(': skipString(f) default: if skipToDelim(f) != 255 { f.UnreadByte() } } n := int(fpos(f) - p) f.Seek(p, 0) return f.Slice(n), p }
func fpos(f fancy.Reader) int64 { r, _ := f.Seek(0, 1) return r }