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 | c8fda9d | 2013-10-09 20:23:12 +0000 | [diff] [blame^] | 31 | // TODO(edisonn): Implement a smart stream that can seek, and that can also fall back to reading |
| 32 | // the bytes in order. For example, we can try to read the stream optimistically, but if there |
| 33 | // are issues in the pdf, we must read the pdf from the beginning, and fix whatever errors we can. |
| 34 | // This would be useful to show quickly page 100 in a pdf (www.example.com/foo.pdf#page100) |
| 35 | // But if the pdf is missing the xref, then we will have to read most of pdf to be able to render |
| 36 | // page 100. |
| 37 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 38 | class SkPdfNativeDoc { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 39 | private: |
| 40 | struct PublicObjectEntry { |
| 41 | long fOffset; |
edisonn@google.com | c8fda9d | 2013-10-09 20:23:12 +0000 | [diff] [blame^] | 42 | // long endOffset; // TODO(edisonn): determine the end of the object, |
| 43 | // to be used when the doc is corrupted, for fast failure. |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 44 | SkPdfNativeObject* fObj; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 45 | SkPdfNativeObject* fResolvedReference; |
edisonn@google.com | f68aed3 | 2013-08-22 15:37:21 +0000 | [diff] [blame] | 46 | bool fIsReferenceResolved; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 49 | public: |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 50 | SkPdfNativeDoc(const char* path); |
| 51 | SkPdfNativeDoc(SkStream* stream); |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 52 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 53 | ~SkPdfNativeDoc(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 54 | |
| 55 | int pages() const; |
| 56 | SkPdfResourceDictionary* pageResources(int page); |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 57 | SkRect MediaBox(int page); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 58 | SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 59 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 60 | SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 61 | SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len, |
| 62 | SkPdfAllocator* allocator); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 63 | |
| 64 | size_t objects() const; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 65 | SkPdfNativeObject* object(int i); |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 66 | SkPdfPageObjectDictionary* page(int page); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 67 | |
| 68 | const SkPdfMapper* mapper() const; |
| 69 | SkPdfAllocator* allocator() const; |
| 70 | |
| 71 | SkPdfReal* createReal(double value) const; |
| 72 | SkPdfInteger* createInteger(int value) const; |
| 73 | // the string does not own the char* |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 74 | SkPdfString* createString(const unsigned char* sz, size_t len) const; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 75 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 76 | SkPdfNativeObject* resolveReference(SkPdfNativeObject* ref); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 77 | |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 78 | // Reports an approximation of all the memory usage. |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 79 | size_t bytesUsed() const; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 80 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 81 | private: |
| 82 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 83 | // Takes ownership of bytes. |
| 84 | void init(const void* bytes, size_t length); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 85 | void loadWithoutXRef(); |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 86 | |
edisonn@google.com | c8fda9d | 2013-10-09 20:23:12 +0000 | [diff] [blame^] | 87 | const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, |
| 88 | const unsigned char* trailerEnd); |
| 89 | const unsigned char* readTrailer(const unsigned char* trailerStart, |
| 90 | const unsigned char* trailerEnd, |
| 91 | bool storeCatalog, long* prev, bool skipKeyword); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 92 | |
edisonn@google.com | c8fda9d | 2013-10-09 20:23:12 +0000 | [diff] [blame^] | 93 | // TODO(edisonn): pdfs with updates not supported right now, generation ignored. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 94 | void addCrossSectionInfo(int id, int generation, int offset, bool isFreed); |
| 95 | static void reset(PublicObjectEntry* obj) { |
| 96 | obj->fObj = NULL; |
| 97 | obj->fResolvedReference = NULL; |
| 98 | obj->fOffset = -1; |
edisonn@google.com | f68aed3 | 2013-08-22 15:37:21 +0000 | [diff] [blame] | 99 | obj->fIsReferenceResolved = false; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 100 | } |
| 101 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 102 | SkPdfNativeObject* readObject(int id/*, int generation*/); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 103 | |
| 104 | void fillPages(SkPdfPageTreeNodeDictionary* tree); |
| 105 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 106 | SkPdfAllocator* fAllocator; |
| 107 | SkPdfMapper* fMapper; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 108 | const unsigned char* fFileContent; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 109 | size_t fContentLength; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 110 | SkPdfNativeObject* fRootCatalogRef; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 111 | SkPdfCatalogDictionary* fRootCatalog; |
| 112 | |
| 113 | mutable SkTDArray<PublicObjectEntry> fObjects; |
| 114 | SkTDArray<SkPdfPageObjectDictionary*> fPages; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
edisonn@google.com | cf2cfa1 | 2013-08-21 16:31:37 +0000 | [diff] [blame] | 117 | #endif // SkPdfNativeDoc_DEFINED |