edisonn@google.com | cf2cfa1 | 2013-08-21 16:31:37 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkPdfNativeDoc_DEFINED |
| 9 | #define SkPdfNativeDoc_DEFINED |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 10 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 11 | #include "SkRect.h" |
| 12 | #include "SkTDArray.h" |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 13 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 14 | class SkCanvas; |
| 15 | |
| 16 | class SkPdfAllocator; |
| 17 | class SkPdfMapper; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 18 | class SkPdfNativeObject; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 19 | class SkPdfReal; |
| 20 | class SkPdfInteger; |
| 21 | class SkPdfString; |
| 22 | class SkPdfResourceDictionary; |
| 23 | class SkPdfCatalogDictionary; |
| 24 | class SkPdfPageObjectDictionary; |
| 25 | class SkPdfPageTreeNodeDictionary; |
| 26 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 27 | class SkPdfNativeTokenizer; |
| 28 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 29 | class SkStream; |
| 30 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 31 | class SkPdfNativeDoc { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 32 | private: |
| 33 | struct PublicObjectEntry { |
| 34 | long fOffset; |
| 35 | // long endOffset; // TODO(edisonn): determine the end of the object, to be used when the doc is corrupted |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 36 | SkPdfNativeObject* fObj; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 37 | // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it! |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 38 | SkPdfNativeObject* fResolvedReference; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 41 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 42 | // TODO(edisonn): read methods: file, stream, http(s)://url, url with seek? |
| 43 | // TODO(edisonn): read first page asap, linearized |
| 44 | // TODO(edisonn): read page N asap, read all file |
| 45 | // TODO(edisonn): allow corruptions of file (e.g. missing endobj, missing stream length, ...) |
| 46 | // TODO(edisonn): encryption |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 47 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 48 | SkPdfNativeDoc(const char* path); |
| 49 | SkPdfNativeDoc(SkStream* stream); |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 50 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 51 | ~SkPdfNativeDoc(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 52 | |
| 53 | int pages() const; |
| 54 | SkPdfResourceDictionary* pageResources(int page); |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 55 | SkRect MediaBox(int page); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 56 | SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 57 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 58 | SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 59 | SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len, |
| 60 | SkPdfAllocator* allocator); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 61 | |
| 62 | size_t objects() const; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 63 | SkPdfNativeObject* object(int i); |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 64 | SkPdfPageObjectDictionary* page(int page); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 65 | |
| 66 | const SkPdfMapper* mapper() const; |
| 67 | SkPdfAllocator* allocator() const; |
| 68 | |
| 69 | SkPdfReal* createReal(double value) const; |
| 70 | SkPdfInteger* createInteger(int value) const; |
| 71 | // the string does not own the char* |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 72 | SkPdfString* createString(const unsigned char* sz, size_t len) const; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 73 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 74 | SkPdfNativeObject* resolveReference(SkPdfNativeObject* ref); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 75 | |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 76 | // Reports an approximation of all the memory usage. |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 77 | size_t bytesUsed() const; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 78 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 79 | private: |
| 80 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 81 | // Takes ownership of bytes. |
| 82 | void init(const void* bytes, size_t length); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 83 | void loadWithoutXRef(); |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 84 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 85 | 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] | 86 | 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] | 87 | |
| 88 | // TODO(edisonn): updates not supported right now, generation ignored |
| 89 | void addCrossSectionInfo(int id, int generation, int offset, bool isFreed); |
| 90 | static void reset(PublicObjectEntry* obj) { |
| 91 | obj->fObj = NULL; |
| 92 | obj->fResolvedReference = NULL; |
| 93 | obj->fOffset = -1; |
| 94 | } |
| 95 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 96 | SkPdfNativeObject* readObject(int id/*, int generation*/); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 97 | |
| 98 | void fillPages(SkPdfPageTreeNodeDictionary* tree); |
| 99 | |
| 100 | // private fields |
| 101 | SkPdfAllocator* fAllocator; |
| 102 | SkPdfMapper* fMapper; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 103 | const unsigned char* fFileContent; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 104 | size_t fContentLength; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 105 | SkPdfNativeObject* fRootCatalogRef; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 106 | SkPdfCatalogDictionary* fRootCatalog; |
| 107 | |
| 108 | mutable SkTDArray<PublicObjectEntry> fObjects; |
| 109 | SkTDArray<SkPdfPageObjectDictionary*> fPages; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
edisonn@google.com | cf2cfa1 | 2013-08-21 16:31:37 +0000 | [diff] [blame^] | 112 | #endif // SkPdfNativeDoc_DEFINED |