blob: 04456cc678db0b72a8f4fd945a7c1083732b0c7b [file] [log] [blame]
edisonn@google.comcf2cfa12013-08-21 16:31:37 +00001/*
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.com3aac1f92013-07-02 22:42:53 +000010
edisonn@google.com571c70b2013-07-10 17:09:50 +000011#include "SkRect.h"
12#include "SkTDArray.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000013
edisonn@google.com571c70b2013-07-10 17:09:50 +000014class SkCanvas;
15
16class SkPdfAllocator;
17class SkPdfMapper;
edisonn@google.com3aa35552013-08-14 18:26:20 +000018class SkPdfNativeObject;
edisonn@google.com571c70b2013-07-10 17:09:50 +000019class SkPdfReal;
20class SkPdfInteger;
21class SkPdfString;
22class SkPdfResourceDictionary;
23class SkPdfCatalogDictionary;
24class SkPdfPageObjectDictionary;
25class SkPdfPageTreeNodeDictionary;
26
edisonn@google.com571c70b2013-07-10 17:09:50 +000027class SkPdfNativeTokenizer;
28
edisonn@google.com147adb12013-07-24 15:56:19 +000029class SkStream;
30
edisonn@google.comc8fda9d2013-10-09 20:23:12 +000031// 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.com3aa35552013-08-14 18:26:20 +000038class SkPdfNativeDoc {
edisonn@google.com571c70b2013-07-10 17:09:50 +000039private:
40 struct PublicObjectEntry {
41 long fOffset;
edisonn@google.comc8fda9d2013-10-09 20:23:12 +000042 // long endOffset; // TODO(edisonn): determine the end of the object,
43 // to be used when the doc is corrupted, for fast failure.
edisonn@google.com3aa35552013-08-14 18:26:20 +000044 SkPdfNativeObject* fObj;
edisonn@google.com3aa35552013-08-14 18:26:20 +000045 SkPdfNativeObject* fResolvedReference;
edisonn@google.comf68aed32013-08-22 15:37:21 +000046 bool fIsReferenceResolved;
edisonn@google.com571c70b2013-07-10 17:09:50 +000047 };
48
edisonn@google.com3aac1f92013-07-02 22:42:53 +000049public:
edisonn@google.com3aa35552013-08-14 18:26:20 +000050 SkPdfNativeDoc(const char* path);
51 SkPdfNativeDoc(SkStream* stream);
edisonn@google.com147adb12013-07-24 15:56:19 +000052
edisonn@google.com3aa35552013-08-14 18:26:20 +000053 ~SkPdfNativeDoc();
edisonn@google.com571c70b2013-07-10 17:09:50 +000054
55 int pages() const;
56 SkPdfResourceDictionary* pageResources(int page);
edisonn@google.com951d6532013-07-10 23:17:31 +000057 SkRect MediaBox(int page);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000058 SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000059
edisonn@google.com3aa35552013-08-14 18:26:20 +000060 SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000061 SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
62 SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000063
64 size_t objects() const;
edisonn@google.com3aa35552013-08-14 18:26:20 +000065 SkPdfNativeObject* object(int i);
edisonn@google.com88fc03d2013-07-30 13:34:10 +000066 SkPdfPageObjectDictionary* page(int page);
edisonn@google.com571c70b2013-07-10 17:09:50 +000067
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.com2ccc3af2013-07-23 17:43:18 +000074 SkPdfString* createString(const unsigned char* sz, size_t len) const;
edisonn@google.com571c70b2013-07-10 17:09:50 +000075
edisonn@google.com3aa35552013-08-14 18:26:20 +000076 SkPdfNativeObject* resolveReference(SkPdfNativeObject* ref);
edisonn@google.com571c70b2013-07-10 17:09:50 +000077
edisonn@google.coma5aaa792013-07-11 12:27:21 +000078 // Reports an approximation of all the memory usage.
edisonn@google.com7b328fd2013-07-11 12:53:06 +000079 size_t bytesUsed() const;
edisonn@google.coma5aaa792013-07-11 12:27:21 +000080
edisonn@google.com571c70b2013-07-10 17:09:50 +000081private:
82
edisonn@google.com147adb12013-07-24 15:56:19 +000083 // Takes ownership of bytes.
84 void init(const void* bytes, size_t length);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000085 void loadWithoutXRef();
edisonn@google.com147adb12013-07-24 15:56:19 +000086
edisonn@google.comc8fda9d2013-10-09 20:23:12 +000087 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.com571c70b2013-07-10 17:09:50 +000092
edisonn@google.comc8fda9d2013-10-09 20:23:12 +000093 // TODO(edisonn): pdfs with updates not supported right now, generation ignored.
edisonn@google.com571c70b2013-07-10 17:09:50 +000094 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.comf68aed32013-08-22 15:37:21 +000099 obj->fIsReferenceResolved = false;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000100 }
101
edisonn@google.com3aa35552013-08-14 18:26:20 +0000102 SkPdfNativeObject* readObject(int id/*, int generation*/);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000103
104 void fillPages(SkPdfPageTreeNodeDictionary* tree);
105
edisonn@google.com571c70b2013-07-10 17:09:50 +0000106 SkPdfAllocator* fAllocator;
107 SkPdfMapper* fMapper;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000108 const unsigned char* fFileContent;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000109 size_t fContentLength;
edisonn@google.com3aa35552013-08-14 18:26:20 +0000110 SkPdfNativeObject* fRootCatalogRef;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000111 SkPdfCatalogDictionary* fRootCatalog;
112
113 mutable SkTDArray<PublicObjectEntry> fObjects;
114 SkTDArray<SkPdfPageObjectDictionary*> fPages;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000115};
116
edisonn@google.comcf2cfa12013-08-21 16:31:37 +0000117#endif // SkPdfNativeDoc_DEFINED