blob: 411343b56cb129bdb5c1f22391db8a24c44bb9c5 [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.com3aa35552013-08-14 18:26:20 +000031class SkPdfNativeDoc {
edisonn@google.com571c70b2013-07-10 17:09:50 +000032private:
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.com3aa35552013-08-14 18:26:20 +000036 SkPdfNativeObject* fObj;
edisonn@google.com571c70b2013-07-10 17:09:50 +000037 // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it!
edisonn@google.com3aa35552013-08-14 18:26:20 +000038 SkPdfNativeObject* fResolvedReference;
edisonn@google.comf68aed32013-08-22 15:37:21 +000039 bool fIsReferenceResolved;
edisonn@google.com571c70b2013-07-10 17:09:50 +000040 };
41
edisonn@google.com3aac1f92013-07-02 22:42:53 +000042public:
edisonn@google.com571c70b2013-07-10 17:09:50 +000043 // 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.com147adb12013-07-24 15:56:19 +000048
edisonn@google.com3aa35552013-08-14 18:26:20 +000049 SkPdfNativeDoc(const char* path);
50 SkPdfNativeDoc(SkStream* stream);
edisonn@google.com147adb12013-07-24 15:56:19 +000051
edisonn@google.com3aa35552013-08-14 18:26:20 +000052 ~SkPdfNativeDoc();
edisonn@google.com571c70b2013-07-10 17:09:50 +000053
54 int pages() const;
55 SkPdfResourceDictionary* pageResources(int page);
edisonn@google.com951d6532013-07-10 23:17:31 +000056 SkRect MediaBox(int page);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000057 SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000058
edisonn@google.com3aa35552013-08-14 18:26:20 +000059 SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000060 SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
61 SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000062
63 size_t objects() const;
edisonn@google.com3aa35552013-08-14 18:26:20 +000064 SkPdfNativeObject* object(int i);
edisonn@google.com88fc03d2013-07-30 13:34:10 +000065 SkPdfPageObjectDictionary* page(int page);
edisonn@google.com571c70b2013-07-10 17:09:50 +000066
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.com2ccc3af2013-07-23 17:43:18 +000073 SkPdfString* createString(const unsigned char* sz, size_t len) const;
edisonn@google.com571c70b2013-07-10 17:09:50 +000074
edisonn@google.com3aa35552013-08-14 18:26:20 +000075 SkPdfNativeObject* resolveReference(SkPdfNativeObject* ref);
edisonn@google.com571c70b2013-07-10 17:09:50 +000076
edisonn@google.coma5aaa792013-07-11 12:27:21 +000077 // Reports an approximation of all the memory usage.
edisonn@google.com7b328fd2013-07-11 12:53:06 +000078 size_t bytesUsed() const;
edisonn@google.coma5aaa792013-07-11 12:27:21 +000079
edisonn@google.com571c70b2013-07-10 17:09:50 +000080private:
81
edisonn@google.com147adb12013-07-24 15:56:19 +000082 // Takes ownership of bytes.
83 void init(const void* bytes, size_t length);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000084 void loadWithoutXRef();
edisonn@google.com147adb12013-07-24 15:56:19 +000085
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000086 const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000087 const unsigned char* readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKeyword);
edisonn@google.com571c70b2013-07-10 17:09:50 +000088
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.comf68aed32013-08-22 15:37:21 +000095 obj->fIsReferenceResolved = false;
edisonn@google.com571c70b2013-07-10 17:09:50 +000096 }
97
edisonn@google.com3aa35552013-08-14 18:26:20 +000098 SkPdfNativeObject* readObject(int id/*, int generation*/);
edisonn@google.com571c70b2013-07-10 17:09:50 +000099
100 void fillPages(SkPdfPageTreeNodeDictionary* tree);
101
102 // private fields
103 SkPdfAllocator* fAllocator;
104 SkPdfMapper* fMapper;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000105 const unsigned char* fFileContent;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000106 size_t fContentLength;
edisonn@google.com3aa35552013-08-14 18:26:20 +0000107 SkPdfNativeObject* fRootCatalogRef;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000108 SkPdfCatalogDictionary* fRootCatalog;
109
110 mutable SkTDArray<PublicObjectEntry> fObjects;
111 SkTDArray<SkPdfPageObjectDictionary*> fPages;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000112};
113
edisonn@google.comcf2cfa12013-08-21 16:31:37 +0000114#endif // SkPdfNativeDoc_DEFINED