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