SkPdfNativeDoc.h revision 4ef4bed00efd247a0ea005b95b7239a9d4c14c68
13aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com#ifndef EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_
23aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com#define EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_
33aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com
4571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com#include "SkRect.h"
5571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com#include "SkTDArray.h"
6571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
7571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkCanvas;
8571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
9571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfAllocator;
10571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfMapper;
11571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfObject;
12571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfReal;
13571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfInteger;
14571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfString;
15571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfResourceDictionary;
16571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfCatalogDictionary;
17571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfPageObjectDictionary;
18571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfPageTreeNodeDictionary;
19571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
20571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfNativeTokenizer;
21571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
22147adb10f7f80ae721879e08474fd575e719487cedisonn@google.comclass SkStream;
23147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com
24571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkNativeParsedPDF {
25571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comprivate:
26571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    struct PublicObjectEntry {
27571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        long fOffset;
28571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        // long endOffset;  // TODO(edisonn): determine the end of the object, to be used when the doc is corrupted
29571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        SkPdfObject* fObj;
30571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it!
31571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        SkPdfObject* fResolvedReference;
32571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    };
333aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com
343aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.compublic:
35571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read methods: file, stream, http(s)://url, url with seek?
36571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read first page asap, linearized
37571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read page N asap, read all file
38571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): allow corruptions of file (e.g. missing endobj, missing stream length, ...)
39571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): encryption
40147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com
41571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkNativeParsedPDF(const char* path);
42147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com    SkNativeParsedPDF(SkStream* stream);
43147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com
44571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    ~SkNativeParsedPDF();
45571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
46571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    int pages() const;
47571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfResourceDictionary* pageResources(int page);
48951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkRect MediaBox(int page);
492ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);
50571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
512ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream, SkPdfAllocator* allocator);
522ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
532ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com                                            SkPdfAllocator* allocator);
54571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
55571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    size_t objects() const;
56571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfObject* object(int i);
57571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
58571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    const SkPdfMapper* mapper() const;
59571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfAllocator* allocator() const;
60571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
61571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfReal* createReal(double value) const;
62571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfInteger* createInteger(int value) const;
63571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // the string does not own the char*
642ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    SkPdfString* createString(const unsigned char* sz, size_t len) const;
65571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
66951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkPdfObject* resolveReference(const SkPdfObject* ref);
67571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
68a5aaa7998fc18489701660f781d7daa33ffc6f6eedisonn@google.com    // Reports an approximation of all the memory usage.
697b328fddf94eea5f05ffa36de02d7d9922f504daedisonn@google.com    size_t bytesUsed() const;
70a5aaa7998fc18489701660f781d7daa33ffc6f6eedisonn@google.com
71571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comprivate:
72571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
73147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com    // Takes ownership of bytes.
74147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com    void init(const void* bytes, size_t length);
754ef4bed00efd247a0ea005b95b7239a9d4c14c68edisonn@google.com    void loadWithoutXRef();
76147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com
772ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd);
784ef4bed00efd247a0ea005b95b7239a9d4c14c68edisonn@google.com    const unsigned char* readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKeyword);
79571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
80571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): updates not supported right now, generation ignored
81571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    void addCrossSectionInfo(int id, int generation, int offset, bool isFreed);
82571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    static void reset(PublicObjectEntry* obj) {
83571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fObj = NULL;
84571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fResolvedReference = NULL;
85571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fOffset = -1;
86571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    }
87571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
88951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkPdfObject* readObject(int id/*, int generation*/);
89571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
90571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    void fillPages(SkPdfPageTreeNodeDictionary* tree);
91571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
92571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // private fields
93571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfAllocator* fAllocator;
94571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfMapper* fMapper;
952ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    const unsigned char* fFileContent;
96571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    size_t fContentLength;
97571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    const SkPdfObject* fRootCatalogRef;
98571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfCatalogDictionary* fRootCatalog;
99571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
100571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    mutable SkTDArray<PublicObjectEntry> fObjects;
101571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkTDArray<SkPdfPageObjectDictionary*> fPages;
1023aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com};
1033aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com
1043aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com#endif  // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_
105