SkPdfNativeDoc.h revision cf2cfa174ca878c144e17e9fc60ca8e9070d7ded
1cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com/*
2cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * Copyright 2013 Google Inc.
3cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com *
4cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * Use of this source code is governed by a BSD-style license that can be
5cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * found in the LICENSE file.
6cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com */
7cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com
8cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#ifndef SkPdfNativeDoc_DEFINED
9cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#define SkPdfNativeDoc_DEFINED
103aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com
11571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com#include "SkRect.h"
12571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com#include "SkTDArray.h"
13571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
14571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkCanvas;
15571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
16571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfAllocator;
17571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfMapper;
183aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comclass SkPdfNativeObject;
19571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfReal;
20571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfInteger;
21571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfString;
22571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfResourceDictionary;
23571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfCatalogDictionary;
24571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfPageObjectDictionary;
25571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfPageTreeNodeDictionary;
26571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
27571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comclass SkPdfNativeTokenizer;
28571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
29147adb10f7f80ae721879e08474fd575e719487cedisonn@google.comclass SkStream;
30147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com
313aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comclass SkPdfNativeDoc {
32571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comprivate:
33571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    struct PublicObjectEntry {
34571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        long fOffset;
35571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        // long endOffset;  // TODO(edisonn): determine the end of the object, to be used when the doc is corrupted
363aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        SkPdfNativeObject* fObj;
37571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it!
383aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        SkPdfNativeObject* fResolvedReference;
39571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    };
403aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com
413aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.compublic:
42571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read methods: file, stream, http(s)://url, url with seek?
43571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read first page asap, linearized
44571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): read page N asap, read all file
45571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): allow corruptions of file (e.g. missing endobj, missing stream length, ...)
46571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): encryption
47147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com
483aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    SkPdfNativeDoc(const char* path);
493aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    SkPdfNativeDoc(SkStream* stream);
50147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com
513aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    ~SkPdfNativeDoc();
52571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
53571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    int pages() const;
54571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfResourceDictionary* pageResources(int page);
55951d6532de49003cd5a43f57caf91dd6d3efc33eedisonn@google.com    SkRect MediaBox(int page);
562ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);
57571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
583aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator);
592ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
602ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com                                            SkPdfAllocator* allocator);
61571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
62571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    size_t objects() const;
633aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    SkPdfNativeObject* object(int i);
6488fc03dd1f47ad33b08d60c47013de860f8fa69fedisonn@google.com    SkPdfPageObjectDictionary* page(int page);
65571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
66571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    const SkPdfMapper* mapper() const;
67571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfAllocator* allocator() const;
68571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
69571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfReal* createReal(double value) const;
70571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfInteger* createInteger(int value) const;
71571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // the string does not own the char*
722ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    SkPdfString* createString(const unsigned char* sz, size_t len) const;
73571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
743aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    SkPdfNativeObject* resolveReference(SkPdfNativeObject* ref);
75571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
76a5aaa7998fc18489701660f781d7daa33ffc6f6eedisonn@google.com    // Reports an approximation of all the memory usage.
777b328fddf94eea5f05ffa36de02d7d9922f504daedisonn@google.com    size_t bytesUsed() const;
78a5aaa7998fc18489701660f781d7daa33ffc6f6eedisonn@google.com
79571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.comprivate:
80571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
81147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com    // Takes ownership of bytes.
82147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com    void init(const void* bytes, size_t length);
834ef4bed00efd247a0ea005b95b7239a9d4c14c68edisonn@google.com    void loadWithoutXRef();
84147adb10f7f80ae721879e08474fd575e719487cedisonn@google.com
852ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd);
864ef4bed00efd247a0ea005b95b7239a9d4c14c68edisonn@google.com    const unsigned char* readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKeyword);
87571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
88571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // TODO(edisonn): updates not supported right now, generation ignored
89571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    void addCrossSectionInfo(int id, int generation, int offset, bool isFreed);
90571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    static void reset(PublicObjectEntry* obj) {
91571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fObj = NULL;
92571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fResolvedReference = NULL;
93571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com        obj->fOffset = -1;
94571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    }
95571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
963aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    SkPdfNativeObject* readObject(int id/*, int generation*/);
97571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
98571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    void fillPages(SkPdfPageTreeNodeDictionary* tree);
99571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
100571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    // private fields
101571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfAllocator* fAllocator;
102571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfMapper* fMapper;
1032ccc3afa474f9485c39c2e863252ddaa3f35724bedisonn@google.com    const unsigned char* fFileContent;
104571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    size_t fContentLength;
1053aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    SkPdfNativeObject* fRootCatalogRef;
106571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkPdfCatalogDictionary* fRootCatalog;
107571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com
108571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    mutable SkTDArray<PublicObjectEntry> fObjects;
109571c70b95f56e22b5a7d6f4f288aa6c9a925a64fedisonn@google.com    SkTDArray<SkPdfPageObjectDictionary*> fPages;
1103aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com};
1113aac1f9f308192f3787265830fe86ce8874e7382edisonn@google.com
112cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#endif  // SkPdfNativeDoc_DEFINED
113