blob: d073f00a30457e00267b02283ce1409c969f2512 [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
edisonn@google.com147adb12013-07-24 15:56:19 +000022class SkStream;
23
edisonn@google.com571c70b2013-07-10 17:09:50 +000024class 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
edisonn@google.com147adb12013-07-24 15:56:19 +000040
edisonn@google.com571c70b2013-07-10 17:09:50 +000041 SkNativeParsedPDF(const char* path);
edisonn@google.com147adb12013-07-24 15:56:19 +000042 SkNativeParsedPDF(SkStream* stream);
43
edisonn@google.com571c70b2013-07-10 17:09:50 +000044 ~SkNativeParsedPDF();
45
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.com2ccc3af2013-07-23 17:43:18 +000051 SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream, SkPdfAllocator* allocator);
52 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;
56 SkPdfObject* 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.com951d6532013-07-10 23:17:31 +000067 SkPdfObject* resolveReference(const SkPdfObject* 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.com951d6532013-07-10 23:17:31 +000089 SkPdfObject* 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;
98 const SkPdfObject* fRootCatalogRef;
99 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_