edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1 | #ifndef EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_ |
| 2 | #define EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_ |
| 3 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 4 | #include "SkRect.h" |
| 5 | #include "SkTDArray.h" |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 6 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 7 | class SkCanvas; |
| 8 | |
| 9 | class SkPdfAllocator; |
| 10 | class SkPdfMapper; |
| 11 | class SkPdfObject; |
| 12 | class SkPdfReal; |
| 13 | class SkPdfInteger; |
| 14 | class SkPdfString; |
| 15 | class SkPdfResourceDictionary; |
| 16 | class SkPdfCatalogDictionary; |
| 17 | class SkPdfPageObjectDictionary; |
| 18 | class SkPdfPageTreeNodeDictionary; |
| 19 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 20 | class SkPdfNativeTokenizer; |
| 21 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 22 | class SkStream; |
| 23 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 24 | class SkNativeParsedPDF { |
| 25 | private: |
| 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.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 34 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 35 | // 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.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 40 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 41 | SkNativeParsedPDF(const char* path); |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 42 | SkNativeParsedPDF(SkStream* stream); |
| 43 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 44 | ~SkNativeParsedPDF(); |
| 45 | |
| 46 | int pages() const; |
| 47 | SkPdfResourceDictionary* pageResources(int page); |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 48 | SkRect MediaBox(int page); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 49 | SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 50 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 51 | SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream, SkPdfAllocator* allocator); |
| 52 | SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len, |
| 53 | SkPdfAllocator* allocator); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 54 | |
| 55 | size_t objects() const; |
| 56 | SkPdfObject* object(int i); |
| 57 | |
| 58 | const SkPdfMapper* mapper() const; |
| 59 | SkPdfAllocator* allocator() const; |
| 60 | |
| 61 | SkPdfReal* createReal(double value) const; |
| 62 | SkPdfInteger* createInteger(int value) const; |
| 63 | // the string does not own the char* |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 64 | SkPdfString* createString(const unsigned char* sz, size_t len) const; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 65 | |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 66 | SkPdfObject* resolveReference(const SkPdfObject* ref); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 67 | |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 68 | // Reports an approximation of all the memory usage. |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 69 | size_t bytesUsed() const; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 70 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 71 | private: |
| 72 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 73 | // Takes ownership of bytes. |
| 74 | void init(const void* bytes, size_t length); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 75 | void loadWithoutXRef(); |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 76 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 77 | const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 78 | const unsigned char* readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKeyword); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 79 | |
| 80 | // TODO(edisonn): updates not supported right now, generation ignored |
| 81 | void addCrossSectionInfo(int id, int generation, int offset, bool isFreed); |
| 82 | static void reset(PublicObjectEntry* obj) { |
| 83 | obj->fObj = NULL; |
| 84 | obj->fResolvedReference = NULL; |
| 85 | obj->fOffset = -1; |
| 86 | } |
| 87 | |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 88 | SkPdfObject* readObject(int id/*, int generation*/); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 89 | |
| 90 | void fillPages(SkPdfPageTreeNodeDictionary* tree); |
| 91 | |
| 92 | // private fields |
| 93 | SkPdfAllocator* fAllocator; |
| 94 | SkPdfMapper* fMapper; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 95 | const unsigned char* fFileContent; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 96 | size_t fContentLength; |
| 97 | const SkPdfObject* fRootCatalogRef; |
| 98 | SkPdfCatalogDictionary* fRootCatalog; |
| 99 | |
| 100 | mutable SkTDArray<PublicObjectEntry> fObjects; |
| 101 | SkTDArray<SkPdfPageObjectDictionary*> fPages; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | #endif // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKNATIVEPARSEDPDF_H_ |