File size: 10,674 Bytes
a9694d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//========================================================================
//
// Catalog.h
//
// Copyright 1996-2007 Glyph & Cog, LLC
//
//========================================================================

//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005 Kristian Høgsberg <[email protected]>
// Copyright (C) 2005, 2007, 2009-2011, 2013, 2017-2022 Albert Astals Cid <[email protected]>
// Copyright (C) 2005 Jonathan Blandford <[email protected]>
// Copyright (C) 2005, 2006, 2008 Brad Hards <[email protected]>
// Copyright (C) 2007 Julien Rebetez <[email protected]>
// Copyright (C) 2008, 2011 Pino Toscano <[email protected]>
// Copyright (C) 2010 Hib Eris <[email protected]>
// Copyright (C) 2012 Fabio D'Urso <[email protected]>
// Copyright (C) 2013 Thomas Freitag <[email protected]>
// Copyright (C) 2013 Adrian Perez de Castro <[email protected]>
// Copyright (C) 2013, 2017 Adrian Johnson <[email protected]>
// Copyright (C) 2013 José Aliste <[email protected]>
// Copyright (C) 2016 Masamichi Hosoda <[email protected]>
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich
// Copyright (C) 2018 Adam Reichold <[email protected]>
// Copyright (C) 2020 Oliver Sander <[email protected]>
// Copyright (C) 2020 Katarina Behrens <[email protected]>
// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by Technische Universität Dresden
// Copyright (C) 2021 RM <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

#ifndef CATALOG_H
#define CATALOG_H

#include "poppler-config.h"
#include "poppler_private_export.h"
#include "Object.h"
#include "Link.h"

#include <memory>
#include <optional>
#include <vector>

class PDFDoc;
class XRef;
class Object;
class Page;
class PageAttrs;
struct Ref;
class PageLabelInfo;
class Form;
class OCGs;
class ViewerPreferences;
class FileSpec;
class StructTreeRoot;

//------------------------------------------------------------------------
// NameTree
//------------------------------------------------------------------------

class POPPLER_PRIVATE_EXPORT NameTree
{
public:
    NameTree();
    ~NameTree();

    NameTree(const NameTree &) = delete;
    NameTree &operator=(const NameTree &) = delete;

    void init(XRef *xref, Object *tree);
    Object lookup(const GooString *name);
    int numEntries() { return length; };
    // iterator accessor, note it returns a pointer to the internal object, do not free nor delete it
    Object *getValue(int i);
    const GooString *getName(int i) const;

private:
    struct Entry
    {
        Entry(Array *array, int index);
        ~Entry();
        GooString name;
        Object value;
        static int cmpEntry(const void *voidEntry, const void *voidOtherEntry);
        static int cmp(const void *key, const void *entry);
    };

    void parse(const Object *tree, std::set<int> &seen);
    void addEntry(Entry *entry);

    XRef *xref;
    Entry **entries;
    int size, length; // size is the number of entries in
                      // the array of Entry*
                      // length is the number of real Entry
};

//------------------------------------------------------------------------
// Catalog
//------------------------------------------------------------------------

class POPPLER_PRIVATE_EXPORT Catalog
{
public:
    // Constructor.
    explicit Catalog(PDFDoc *docA);

    // Destructor.
    ~Catalog();

    Catalog(const Catalog &) = delete;
    Catalog &operator=(const Catalog &) = delete;

    // Is catalog valid?
    bool isOk() { return ok; }

    // Get number of pages.
    int getNumPages();

    // Get a page.
    Page *getPage(int i);

    // Get the reference for a page object.
    Ref *getPageRef(int i);

    // Return base URI, or NULL if none.
    const std::optional<std::string> &getBaseURI() const { return baseURI; }

    // Return the contents of the metadata stream, or NULL if there is
    // no metadata.
    std::unique_ptr<GooString> readMetadata();

    // Return the structure tree root object.
    StructTreeRoot *getStructTreeRoot();

    // Return values from the MarkInfo dictionary as flags in a bitfield.
    enum MarkInfoFlags
    {
        markInfoNull = 1 << 0,
        markInfoMarked = 1 << 1,
        markInfoUserProperties = 1 << 2,
        markInfoSuspects = 1 << 3,
    };
    unsigned int getMarkInfo();

    // Find a page, given its object ID.  Returns page number, or 0 if
    // not found.
    int findPage(const Ref pageRef);

    // Find a named destination.  Returns the link destination, or
    // NULL if <name> is not a destination.
    std::unique_ptr<LinkDest> findDest(const GooString *name);

    Object *getDests();

    // Get the number of named destinations in name-dict
    int numDests();

    // Get the i'th named destination name in name-dict
    const char *getDestsName(int i);

    // Get the i'th named destination link destination in name-dict
    std::unique_ptr<LinkDest> getDestsDest(int i);

    // Get the number of named destinations in name-tree
    int numDestNameTree() { return getDestNameTree()->numEntries(); }

    // Get the i'th named destination name in name-tree
    const GooString *getDestNameTreeName(int i) { return getDestNameTree()->getName(i); }

    // Get the i'th named destination link destination in name-tree
    std::unique_ptr<LinkDest> getDestNameTreeDest(int i);

    // Get the number of embedded files
    int numEmbeddedFiles() { return getEmbeddedFileNameTree()->numEntries(); }

    // Get the i'th file embedded (at the Document level) in the document
    std::unique_ptr<FileSpec> embeddedFile(int i);

    // Is there an embedded file with the given name?
    bool hasEmbeddedFile(const std::string &fileName);

    // Adds and embeddedFile
    // If there is already an existing embedded file with the given fileName
    // it gets replaced, if that's not what you want check hasEmbeddedFile first
    void addEmbeddedFile(GooFile *file, const std::string &fileName);

    // Get the number of javascript scripts
    int numJS() { return getJSNameTree()->numEntries(); }
    const GooString *getJSName(int i) { return getJSNameTree()->getName(i); }

    // Get the i'th JavaScript script (at the Document level) in the document
    GooString *getJS(int i);

    // Convert between page indices and page labels.
    bool labelToIndex(GooString *label, int *index);
    bool indexToLabel(int index, GooString *label);

    Object *getOutline();
    // returns the existing outline or new one if it doesn't exist
    Object *getCreateOutline();

    Object *getAcroForm() { return &acroForm; }
    void addFormToAcroForm(const Ref formRef);
    void removeFormFromAcroForm(const Ref formRef);
    void setAcroFormModified();

    OCGs *getOptContentConfig() { return optContent; }

    int getPDFMajorVersion() const { return catalogPdfMajorVersion; }
    int getPDFMinorVersion() const { return catalogPdfMinorVersion; }

    enum FormType
    {
        NoForm,
        AcroForm,
        XfaForm
    };

    FormType getFormType();
    // This can return nullptr if the document is in a very damaged state
    Form *getCreateForm();
    Form *getForm();

    ViewerPreferences *getViewerPreferences();

    enum PageMode
    {
        pageModeNone,
        pageModeOutlines,
        pageModeThumbs,
        pageModeFullScreen,
        pageModeOC,
        pageModeAttach,
        pageModeNull
    };
    enum PageLayout
    {
        pageLayoutNone,
        pageLayoutSinglePage,
        pageLayoutOneColumn,
        pageLayoutTwoColumnLeft,
        pageLayoutTwoColumnRight,
        pageLayoutTwoPageLeft,
        pageLayoutTwoPageRight,
        pageLayoutNull
    };

    // Returns the page mode.
    PageMode getPageMode();
    PageLayout getPageLayout();

    enum DocumentAdditionalActionsType
    {
        actionCloseDocument, ///< Performed before closing the document
        actionSaveDocumentStart, ///< Performed before saving the document
        actionSaveDocumentFinish, ///< Performed after saving the document
        actionPrintDocumentStart, ///< Performed before printing the document
        actionPrintDocumentFinish, ///< Performed after printing the document
    };

    std::unique_ptr<LinkAction> getAdditionalAction(DocumentAdditionalActionsType type);

private:
    // Get page label info.
    PageLabelInfo *getPageLabelInfo();

    PDFDoc *doc;
    XRef *xref; // the xref table for this PDF file
    std::vector<std::pair<std::unique_ptr<Page>, Ref>> pages;
    std::vector<Object> *pagesList;
    std::vector<Ref> *pagesRefList;
    std::vector<PageAttrs *> *attrsList;
    std::vector<int> *kidsIdxList;
    Form *form;
    ViewerPreferences *viewerPrefs;
    int numPages; // number of pages
    Object dests; // named destination dictionary
    Object names; // named names dictionary
    NameTree *destNameTree; // named destination name-tree
    NameTree *embeddedFileNameTree; // embedded file name-tree
    NameTree *jsNameTree; // Java Script name-tree
    std::optional<std::string> baseURI; // base URI for URI-type links
    Object metadata; // metadata stream
    StructTreeRoot *structTreeRoot; // structure tree root
    unsigned int markInfo; // Flags from MarkInfo dictionary
    Object outline; // outline dictionary
    Object acroForm; // AcroForm dictionary
    Object viewerPreferences; // ViewerPreference dictionary
    OCGs *optContent; // Optional Content groups
    bool ok; // true if catalog is valid
    PageLabelInfo *pageLabelInfo; // info about page labels
    PageMode pageMode; // page mode
    PageLayout pageLayout; // page layout
    Object additionalActions; // page additional actions

    bool cachePageTree(int page); // Cache first <page> pages.
    Object *findDestInTree(Object *tree, GooString *name, Object *obj);

    Object *getNames();
    NameTree *getDestNameTree();
    NameTree *getEmbeddedFileNameTree();
    NameTree *getJSNameTree();
    std::unique_ptr<LinkDest> createLinkDest(Object *obj);

    int catalogPdfMajorVersion = -1;
    int catalogPdfMinorVersion = -1;

    mutable std::recursive_mutex mutex;
};

#endif