aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.h
blob: 411343b56cb129bdb5c1f22391db8a24c44bb9c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkPdfNativeDoc_DEFINED
#define SkPdfNativeDoc_DEFINED

#include "SkRect.h"
#include "SkTDArray.h"

class SkCanvas;

class SkPdfAllocator;
class SkPdfMapper;
class SkPdfNativeObject;
class SkPdfReal;
class SkPdfInteger;
class SkPdfString;
class SkPdfResourceDictionary;
class SkPdfCatalogDictionary;
class SkPdfPageObjectDictionary;
class SkPdfPageTreeNodeDictionary;

class SkPdfNativeTokenizer;

class SkStream;

class SkPdfNativeDoc {
private:
    struct PublicObjectEntry {
        long fOffset;
        // long endOffset;  // TODO(edisonn): determine the end of the object, to be used when the doc is corrupted
        SkPdfNativeObject* fObj;
        // TODO(edisonn): perf ... probably it does not make sense to cache the ref. test it!
        SkPdfNativeObject* fResolvedReference;
        bool fIsReferenceResolved;
    };

public:
    // TODO(edisonn): read methods: file, stream, http(s)://url, url with seek?
    // TODO(edisonn): read first page asap, linearized
    // TODO(edisonn): read page N asap, read all file
    // TODO(edisonn): allow corruptions of file (e.g. missing endobj, missing stream length, ...)
    // TODO(edisonn): encryption

    SkPdfNativeDoc(const char* path);
    SkPdfNativeDoc(SkStream* stream);

    ~SkPdfNativeDoc();

    int pages() const;
    SkPdfResourceDictionary* pageResources(int page);
    SkRect MediaBox(int page);
    SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);

    SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator);
    SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
                                            SkPdfAllocator* allocator);

    size_t objects() const;
    SkPdfNativeObject* object(int i);
    SkPdfPageObjectDictionary* page(int page);

    const SkPdfMapper* mapper() const;
    SkPdfAllocator* allocator() const;

    SkPdfReal* createReal(double value) const;
    SkPdfInteger* createInteger(int value) const;
    // the string does not own the char*
    SkPdfString* createString(const unsigned char* sz, size_t len) const;

    SkPdfNativeObject* resolveReference(SkPdfNativeObject* ref);

    // Reports an approximation of all the memory usage.
    size_t bytesUsed() const;

private:

    // Takes ownership of bytes.
    void init(const void* bytes, size_t length);
    void loadWithoutXRef();

    const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd);
    const unsigned char* readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKeyword);

    // TODO(edisonn): updates not supported right now, generation ignored
    void addCrossSectionInfo(int id, int generation, int offset, bool isFreed);
    static void reset(PublicObjectEntry* obj) {
        obj->fObj = NULL;
        obj->fResolvedReference = NULL;
        obj->fOffset = -1;
        obj->fIsReferenceResolved = false;
    }

    SkPdfNativeObject* readObject(int id/*, int generation*/);

    void fillPages(SkPdfPageTreeNodeDictionary* tree);

    // private fields
    SkPdfAllocator* fAllocator;
    SkPdfMapper* fMapper;
    const unsigned char* fFileContent;
    size_t fContentLength;
    SkPdfNativeObject* fRootCatalogRef;
    SkPdfCatalogDictionary* fRootCatalog;

    mutable SkTDArray<PublicObjectEntry> fObjects;
    SkTDArray<SkPdfPageObjectDictionary*> fPages;
};

#endif  // SkPdfNativeDoc_DEFINED