blob: f61114b9e152e783ae46fefaa8eefa9e12e18cf0 [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);
edisonn@google.com951d6532013-07-10 23:17:31 +000045 SkRect MediaBox(int page);
46 SkPdfNativeTokenizer* tokenizerOfPage(int n);
edisonn@google.com571c70b2013-07-10 17:09:50 +000047
edisonn@google.com951d6532013-07-10 23:17:31 +000048 SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream);
49 SkPdfNativeTokenizer* tokenizerOfBuffer(unsigned char* buffer, size_t len);
edisonn@google.com571c70b2013-07-10 17:09:50 +000050
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
edisonn@google.com951d6532013-07-10 23:17:31 +000064 SkPdfObject* resolveReference(const SkPdfObject* ref);
edisonn@google.com571c70b2013-07-10 17:09:50 +000065
66private:
67
68 unsigned char* readCrossReferenceSection(unsigned char* xrefStart, unsigned char* trailerEnd);
69 long readTrailer(unsigned char* trailerStart, unsigned char* trailerEnd, bool storeCatalog);
70
71 // TODO(edisonn): updates not supported right now, generation ignored
72 void addCrossSectionInfo(int id, int generation, int offset, bool isFreed);
73 static void reset(PublicObjectEntry* obj) {
74 obj->fObj = NULL;
75 obj->fResolvedReference = NULL;
76 obj->fOffset = -1;
77 }
78
edisonn@google.com951d6532013-07-10 23:17:31 +000079 SkPdfObject* readObject(int id/*, int generation*/);
edisonn@google.com571c70b2013-07-10 17:09:50 +000080
81 void fillPages(SkPdfPageTreeNodeDictionary* tree);
82
83 // private fields
84 SkPdfAllocator* fAllocator;
85 SkPdfMapper* fMapper;
86 unsigned char* fFileContent;
87 size_t fContentLength;
88 const SkPdfObject* fRootCatalogRef;
89 SkPdfCatalogDictionary* fRootCatalog;
90
91 mutable SkTDArray<PublicObjectEntry> fObjects;
92 SkTDArray<SkPdfPageObjectDictionary*> fPages;
edisonn@google.com3aac1f92013-07-02 22:42:53 +000093};
94
95#endif // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_