SkPdfNativeDoc.h revision 7b328fddf94eea5f05ffa36de02d7d9922f504da
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
22571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkNativeParsedPDF {
23571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comprivate:
24571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    struct PublicObjectEntry {
25571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        long fOffset;
26571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        // long endOffset;  // TODO(edisonn): determine the end of the object, to be used when the doc is corrupted
27571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        SkPdfObject* fObj;
28571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it!
29571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        SkPdfObject* fResolvedReference;
30571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    };
313aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com
323aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.compublic:
33571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read methods: file, stream, http(s)://url, url with seek?
34571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read first page asap, linearized
35571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read page N asap, read all file
36571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): allow corruptions of file (e.g. missing endobj, missing stream length, ...)
37571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): encryption
38571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkNativeParsedPDF(const char* path);
39571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    ~SkNativeParsedPDF();
40571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
41571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    int pages() const;
42571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfResourceDictionary* pageResources(int page);
43951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkRect MediaBox(int page);
44951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfPage(int n);
45571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
46951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream);
47951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfBuffer(unsigned char* buffer, size_t len);
48571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
49571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    size_t objects() const;
50571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfObject* object(int i);
51571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
52571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    const SkPdfMapper* mapper() const;
53571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfAllocator* allocator() const;
54571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
55571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfReal* createReal(double value) const;
56571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfInteger* createInteger(int value) const;
57571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // the string does not own the char*
58571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfString* createString(unsigned char* sz, size_t len) const;
59571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
60571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    void drawPage(int page, SkCanvas* canvas);
61571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
62951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkPdfObject* resolveReference(const SkPdfObject* ref);
63571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
64a5aaa7998fc18489701660f781d7daa33ffc6f6eedisonn@google.com    // Reports an approximation of all the memory usage.
657b328fddf94eea5f05ffa36de02d7d9922f504daedisonn@google.com    size_t bytesUsed() const;
66a5aaa7998fc18489701660f781d7daa33ffc6f6eedisonn@google.com
67571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comprivate:
68571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
69571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    unsigned char* readCrossReferenceSection(unsigned char* xrefStart, unsigned char* trailerEnd);
70571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    long readTrailer(unsigned char* trailerStart, unsigned char* trailerEnd, bool storeCatalog);
71571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
72571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): updates not supported right now, generation ignored
73571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    void addCrossSectionInfo(int id, int generation, int offset, bool isFreed);
74571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    static void reset(PublicObjectEntry* obj) {
75571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fObj = NULL;
76571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fResolvedReference = NULL;
77571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fOffset = -1;
78571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    }
79571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
80951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkPdfObject* readObject(int id/*, int generation*/);
81571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
82571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    void fillPages(SkPdfPageTreeNodeDictionary* tree);
83571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
84571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // private fields
85571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfAllocator* fAllocator;
86571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfMapper* fMapper;
87571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    unsigned char* fFileContent;
88571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    size_t fContentLength;
89571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    const SkPdfObject* fRootCatalogRef;
90571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfCatalogDictionary* fRootCatalog;
91571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
92571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    mutable SkTDArray<PublicObjectEntry> fObjects;
93571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkTDArray<SkPdfPageObjectDictionary*> fPages;
943aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com};
953aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com
963aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com#endif  // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_
97