blob: c520b0394afca38298dfbc7c8a96eded02cf35b6 [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
edisonn@google.com571c70b2013-07-10 17:09:50 +000020class SkPdfNativeTokenizer;
21
22class SkNativeParsedPDF {
23private:
24 struct PublicObjectEntry {
25 long fOffset;
26 // long endOffset; // TODO(edisonn): determine the end of the object, to be used when the doc is corrupted
27 SkPdfObject* fObj;
28 // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it!
29 SkPdfObject* fResolvedReference;
30 };
31
edisonn@google.com3aac1f92013-07-02 22:42:53 +000032public:
edisonn@google.com571c70b2013-07-10 17:09:50 +000033 // TODO(edisonn): read methods: file, stream, http(s)://url, url with seek?
34 // TODO(edisonn): read first page asap, linearized
35 // TODO(edisonn): read page N asap, read all file
36 // TODO(edisonn): allow corruptions of file (e.g. missing endobj, missing stream length, ...)
37 // TODO(edisonn): encryption
38 SkNativeParsedPDF(const char* path);
39 ~SkNativeParsedPDF();
40
41 int pages() const;
42 SkPdfResourceDictionary* pageResources(int page);
edisonn@google.com951d6532013-07-10 23:17:31 +000043 SkRect MediaBox(int page);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000044 SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000045
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000046 SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream, SkPdfAllocator* allocator);
47 SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
48 SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000049
50 size_t objects() const;
51 SkPdfObject* object(int i);
52
53 const SkPdfMapper* mapper() const;
54 SkPdfAllocator* allocator() const;
55
56 SkPdfReal* createReal(double value) const;
57 SkPdfInteger* createInteger(int value) const;
58 // the string does not own the char*
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000059 SkPdfString* createString(const unsigned char* sz, size_t len) const;
edisonn@google.com571c70b2013-07-10 17:09:50 +000060
edisonn@google.com951d6532013-07-10 23:17:31 +000061 SkPdfObject* resolveReference(const SkPdfObject* ref);
edisonn@google.com571c70b2013-07-10 17:09:50 +000062
edisonn@google.coma5aaa792013-07-11 12:27:21 +000063 // Reports an approximation of all the memory usage.
edisonn@google.com7b328fd2013-07-11 12:53:06 +000064 size_t bytesUsed() const;
edisonn@google.coma5aaa792013-07-11 12:27:21 +000065
edisonn@google.com571c70b2013-07-10 17:09:50 +000066private:
67
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000068 const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd);
69 long readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog);
edisonn@google.com571c70b2013-07-10 17:09:50 +000070
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;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000086 const unsigned char* fFileContent;
edisonn@google.com571c70b2013-07-10 17:09:50 +000087 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_