blob: 5f22c42659147fa6e9a2a1c057e709ab0ffbcb16 [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;
edisonn@google.com3aa35552013-08-14 18:26:20 +000011class SkPdfNativeObject;
edisonn@google.com571c70b2013-07-10 17:09:50 +000012class 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
edisonn@google.com147adb12013-07-24 15:56:19 +000022class SkStream;
23
edisonn@google.com3aa35552013-08-14 18:26:20 +000024class SkPdfNativeDoc {
edisonn@google.com571c70b2013-07-10 17:09:50 +000025private:
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
edisonn@google.com3aa35552013-08-14 18:26:20 +000029 SkPdfNativeObject* fObj;
edisonn@google.com571c70b2013-07-10 17:09:50 +000030 // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it!
edisonn@google.com3aa35552013-08-14 18:26:20 +000031 SkPdfNativeObject* fResolvedReference;
edisonn@google.com571c70b2013-07-10 17:09:50 +000032 };
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
edisonn@google.com147adb12013-07-24 15:56:19 +000040
edisonn@google.com3aa35552013-08-14 18:26:20 +000041 SkPdfNativeDoc(const char* path);
42 SkPdfNativeDoc(SkStream* stream);
edisonn@google.com147adb12013-07-24 15:56:19 +000043
edisonn@google.com3aa35552013-08-14 18:26:20 +000044 ~SkPdfNativeDoc();
edisonn@google.com571c70b2013-07-10 17:09:50 +000045
46 int pages() const;
47 SkPdfResourceDictionary* pageResources(int page);
edisonn@google.com951d6532013-07-10 23:17:31 +000048 SkRect MediaBox(int page);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000049 SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000050
edisonn@google.com3aa35552013-08-14 18:26:20 +000051 SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000052 SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
53 SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000054
55 size_t objects() const;
edisonn@google.com3aa35552013-08-14 18:26:20 +000056 SkPdfNativeObject* object(int i);
edisonn@google.com88fc03d2013-07-30 13:34:10 +000057 SkPdfPageObjectDictionary* page(int page);
edisonn@google.com571c70b2013-07-10 17:09:50 +000058
59 const SkPdfMapper* mapper() const;
60 SkPdfAllocator* allocator() const;
61
62 SkPdfReal* createReal(double value) const;
63 SkPdfInteger* createInteger(int value) const;
64 // the string does not own the char*
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000065 SkPdfString* createString(const unsigned char* sz, size_t len) const;
edisonn@google.com571c70b2013-07-10 17:09:50 +000066
edisonn@google.com3aa35552013-08-14 18:26:20 +000067 SkPdfNativeObject* resolveReference(SkPdfNativeObject* ref);
edisonn@google.com571c70b2013-07-10 17:09:50 +000068
edisonn@google.coma5aaa792013-07-11 12:27:21 +000069 // Reports an approximation of all the memory usage.
edisonn@google.com7b328fd2013-07-11 12:53:06 +000070 size_t bytesUsed() const;
edisonn@google.coma5aaa792013-07-11 12:27:21 +000071
edisonn@google.com571c70b2013-07-10 17:09:50 +000072private:
73
edisonn@google.com147adb12013-07-24 15:56:19 +000074 // Takes ownership of bytes.
75 void init(const void* bytes, size_t length);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000076 void loadWithoutXRef();
edisonn@google.com147adb12013-07-24 15:56:19 +000077
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000078 const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000079 const unsigned char* readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKeyword);
edisonn@google.com571c70b2013-07-10 17:09:50 +000080
81 // TODO(edisonn): updates not supported right now, generation ignored
82 void addCrossSectionInfo(int id, int generation, int offset, bool isFreed);
83 static void reset(PublicObjectEntry* obj) {
84 obj->fObj = NULL;
85 obj->fResolvedReference = NULL;
86 obj->fOffset = -1;
87 }
88
edisonn@google.com3aa35552013-08-14 18:26:20 +000089 SkPdfNativeObject* readObject(int id/*, int generation*/);
edisonn@google.com571c70b2013-07-10 17:09:50 +000090
91 void fillPages(SkPdfPageTreeNodeDictionary* tree);
92
93 // private fields
94 SkPdfAllocator* fAllocator;
95 SkPdfMapper* fMapper;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000096 const unsigned char* fFileContent;
edisonn@google.com571c70b2013-07-10 17:09:50 +000097 size_t fContentLength;
edisonn@google.com3aa35552013-08-14 18:26:20 +000098 SkPdfNativeObject* fRootCatalogRef;
edisonn@google.com571c70b2013-07-10 17:09:50 +000099 SkPdfCatalogDictionary* fRootCatalog;
100
101 mutable SkTDArray<PublicObjectEntry> fObjects;
102 SkTDArray<SkPdfPageObjectDictionary*> fPages;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000103};
104
105#endif // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_