blob: 245bdfb133115f4df0557d950b7025d44e499b0e [file] [log] [blame]
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001#ifndef EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_
2#define EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_
3
edisonn@google.com571c70b2013-07-10 17:09:50 +00004#include "SkRect.h"
5#include "SkTDArray.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +00006
edisonn@google.com571c70b2013-07-10 17:09:50 +00007class SkCanvas;
8
9class SkPdfAllocator;
10class SkPdfMapper;
11class SkPdfObject;
12class SkPdfReal;
13class SkPdfInteger;
14class SkPdfString;
15class SkPdfResourceDictionary;
16class SkPdfCatalogDictionary;
17class SkPdfPageObjectDictionary;
18class SkPdfPageTreeNodeDictionary;
19
20
21
22class SkPdfNativeTokenizer;
23
24class SkNativeParsedPDF {
25private:
26 struct PublicObjectEntry {
27 long fOffset;
28 // long endOffset; // TODO(edisonn): determine the end of the object, to be used when the doc is corrupted
29 SkPdfObject* fObj;
30 // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it!
31 SkPdfObject* fResolvedReference;
32 };
33
edisonn@google.com3aac1f92013-07-02 22:42:53 +000034public:
edisonn@google.com571c70b2013-07-10 17:09:50 +000035 // TODO(edisonn): read methods: file, stream, http(s)://url, url with seek?
36 // TODO(edisonn): read first page asap, linearized
37 // TODO(edisonn): read page N asap, read all file
38 // TODO(edisonn): allow corruptions of file (e.g. missing endobj, missing stream length, ...)
39 // TODO(edisonn): encryption
40 SkNativeParsedPDF(const char* path);
41 ~SkNativeParsedPDF();
42
43 int pages() const;
44 SkPdfResourceDictionary* pageResources(int page);
45 SkRect MediaBox(int page) const;
46 SkPdfNativeTokenizer* tokenizerOfPage(int n) const;
47
48 SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream) const;
49 SkPdfNativeTokenizer* tokenizerOfBuffer(unsigned char* buffer, size_t len) const;
50
51 size_t objects() const;
52 SkPdfObject* object(int i);
53
54 const SkPdfMapper* mapper() const;
55 SkPdfAllocator* allocator() const;
56
57 SkPdfReal* createReal(double value) const;
58 SkPdfInteger* createInteger(int value) const;
59 // the string does not own the char*
60 SkPdfString* createString(unsigned char* sz, size_t len) const;
61
62 void drawPage(int page, SkCanvas* canvas);
63
64 SkPdfObject* resolveReference(SkPdfObject* ref) const;
65 SkPdfObject* resolveReference(const SkPdfObject* ref) const;
66
67private:
68
69 unsigned char* readCrossReferenceSection(unsigned char* xrefStart, unsigned char* trailerEnd);
70 long readTrailer(unsigned char* trailerStart, unsigned char* trailerEnd, bool storeCatalog);
71
72 // TODO(edisonn): updates not supported right now, generation ignored
73 void addCrossSectionInfo(int id, int generation, int offset, bool isFreed);
74 static void reset(PublicObjectEntry* obj) {
75 obj->fObj = NULL;
76 obj->fResolvedReference = NULL;
77 obj->fOffset = -1;
78 }
79
80 SkPdfObject* readObject(int id/*, int generation*/) const;
81
82 void fillPages(SkPdfPageTreeNodeDictionary* tree);
83
84 // private fields
85 SkPdfAllocator* fAllocator;
86 SkPdfMapper* fMapper;
87 unsigned char* fFileContent;
88 size_t fContentLength;
89 const SkPdfObject* fRootCatalogRef;
90 SkPdfCatalogDictionary* fRootCatalog;
91
92 mutable SkTDArray<PublicObjectEntry> fObjects;
93 SkTDArray<SkPdfPageObjectDictionary*> fPages;
edisonn@google.com3aac1f92013-07-02 22:42:53 +000094};
95
96#endif // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_