blob: ae89e8f58565cb5991efa158afc96a75781d0199 [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.com571c70b2013-07-10 17:09:50 +000039 };
40
edisonn@google.com3aac1f92013-07-02 22:42:53 +000041public:
edisonn@google.com571c70b2013-07-10 17:09:50 +000042 // 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.com147adb12013-07-24 15:56:19 +000047
edisonn@google.com3aa35552013-08-14 18:26:20 +000048 SkPdfNativeDoc(const char* path);
49 SkPdfNativeDoc(SkStream* stream);
edisonn@google.com147adb12013-07-24 15:56:19 +000050
edisonn@google.com3aa35552013-08-14 18:26:20 +000051 ~SkPdfNativeDoc();
edisonn@google.com571c70b2013-07-10 17:09:50 +000052
53 int pages() const;
54 SkPdfResourceDictionary* pageResources(int page);
edisonn@google.com951d6532013-07-10 23:17:31 +000055 SkRect MediaBox(int page);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000056 SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000057
edisonn@google.com3aa35552013-08-14 18:26:20 +000058 SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000059 SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
60 SkPdfAllocator* allocator);
edisonn@google.com571c70b2013-07-10 17:09:50 +000061
62 size_t objects() const;
edisonn@google.com3aa35552013-08-14 18:26:20 +000063 SkPdfNativeObject* object(int i);
edisonn@google.com88fc03d2013-07-30 13:34:10 +000064 SkPdfPageObjectDictionary* page(int page);
edisonn@google.com571c70b2013-07-10 17:09:50 +000065
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.com2ccc3af2013-07-23 17:43:18 +000072 SkPdfString* createString(const unsigned char* sz, size_t len) const;
edisonn@google.com571c70b2013-07-10 17:09:50 +000073
edisonn@google.com3aa35552013-08-14 18:26:20 +000074 SkPdfNativeObject* resolveReference(SkPdfNativeObject* ref);
edisonn@google.com571c70b2013-07-10 17:09:50 +000075
edisonn@google.coma5aaa792013-07-11 12:27:21 +000076 // Reports an approximation of all the memory usage.
edisonn@google.com7b328fd2013-07-11 12:53:06 +000077 size_t bytesUsed() const;
edisonn@google.coma5aaa792013-07-11 12:27:21 +000078
edisonn@google.com571c70b2013-07-10 17:09:50 +000079private:
80
edisonn@google.com147adb12013-07-24 15:56:19 +000081 // Takes ownership of bytes.
82 void init(const void* bytes, size_t length);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000083 void loadWithoutXRef();
edisonn@google.com147adb12013-07-24 15:56:19 +000084
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000085 const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000086 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 +000087
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.com3aa35552013-08-14 18:26:20 +000096 SkPdfNativeObject* readObject(int id/*, int generation*/);
edisonn@google.com571c70b2013-07-10 17:09:50 +000097
98 void fillPages(SkPdfPageTreeNodeDictionary* tree);
99
100 // private fields
101 SkPdfAllocator* fAllocator;
102 SkPdfMapper* fMapper;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000103 const unsigned char* fFileContent;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000104 size_t fContentLength;
edisonn@google.com3aa35552013-08-14 18:26:20 +0000105 SkPdfNativeObject* fRootCatalogRef;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000106 SkPdfCatalogDictionary* fRootCatalog;
107
108 mutable SkTDArray<PublicObjectEntry> fObjects;
109 SkTDArray<SkPdfPageObjectDictionary*> fPages;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000110};
111
edisonn@google.comcf2cfa12013-08-21 16:31:37 +0000112#endif // SkPdfNativeDoc_DEFINED