aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFOutlineDocumentHelper.cc
blob: 49a767bb6217af3f29797572c172dd6f2cbd0d9f (plain) (blame)
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
#include <qpdf/QPDFOutlineDocumentHelper.hh>

#include <qpdf/QTC.hh>

QPDFOutlineDocumentHelper::Members::~Members()
{
}

QPDFOutlineDocumentHelper::Members::Members()
{
}

QPDFOutlineDocumentHelper::QPDFOutlineDocumentHelper(QPDF& qpdf) :
    QPDFDocumentHelper(qpdf),
    m(new Members())
{
    QPDFObjectHandle root = qpdf.getRoot();
    if (! root.hasKey("/Outlines"))
    {
        return;
    }
    QPDFObjectHandle outlines = root.getKey("/Outlines");
    if (! (outlines.isDictionary() && outlines.hasKey("/First")))
    {
        return;
    }
    QPDFObjectHandle cur = outlines.getKey("/First");
    std::set<QPDFObjGen> seen;
    while (! cur.isNull())
    {
        auto og = cur.getObjGen();
        if (seen.count(og))
        {
            break;
        }
        seen.insert(og);
        this->m->outlines.push_back(
            QPDFOutlineObjectHelper::Accessor::create(cur, *this, 1));
        cur = cur.getKey("/Next");
    }
}

QPDFOutlineDocumentHelper::~QPDFOutlineDocumentHelper()
{
}

bool
QPDFOutlineDocumentHelper::hasOutlines()
{
    return ! this->m->outlines.empty();
}

std::vector<QPDFOutlineObjectHelper>
QPDFOutlineDocumentHelper::getTopLevelOutlines()
{
    return this->m->outlines;
}

void
QPDFOutlineDocumentHelper::initializeByPage()
{
    std::list<QPDFOutlineObjectHelper> queue;
    queue.insert(queue.end(), this->m->outlines.begin(), this->m->outlines.end());

    while (! queue.empty())
    {
        QPDFOutlineObjectHelper oh = queue.front();
        queue.pop_front();
        this->m->by_page[oh.getDestPage().getObjGen()].push_back(oh);
        std::vector<QPDFOutlineObjectHelper> kids = oh.getKids();
        queue.insert(queue.end(), kids.begin(), kids.end());
    }
}

std::vector<QPDFOutlineObjectHelper>
QPDFOutlineDocumentHelper::getOutlinesForPage(QPDFObjGen const& og)
{
    if (this->m->by_page.empty())
    {
        initializeByPage();
    }
    std::vector<QPDFOutlineObjectHelper> result;
    if (this->m->by_page.count(og))
    {
        result = this->m->by_page[og];
    }
    return result;
}

QPDFObjectHandle
QPDFOutlineDocumentHelper::resolveNamedDest(QPDFObjectHandle name)
{
    QPDFObjectHandle result;
    if (name.isName())
    {
        if (! this->m->dest_dict.isInitialized())
        {
            this->m->dest_dict = this->qpdf.getRoot().getKey("/Dests");
        }
        if (this->m->dest_dict.isDictionary())
        {
            QTC::TC("qpdf", "QPDFOutlineDocumentHelper name named dest");
            result = this->m->dest_dict.getKey(name.getName());
        }
    }
    else if (name.isString())
    {
        if (0 == this->m->names_dest.get())
        {
            QPDFObjectHandle names = this->qpdf.getRoot().getKey("/Names");
            if (names.isDictionary())
            {
                QPDFObjectHandle dests = names.getKey("/Dests");
                if (dests.isDictionary())
                {
                    this->m->names_dest =
                        new QPDFNameTreeObjectHelper(dests, this->qpdf);
                }
            }
        }
        if (this->m->names_dest.get())
        {
            if (this->m->names_dest->findObject(name.getUTF8Value(), result))
            {
                QTC::TC("qpdf", "QPDFOutlineDocumentHelper string named dest");
            }
        }
    }
    if (! result.isInitialized())
    {
        result = QPDFObjectHandle::newNull();
    }
    return result;
}

bool
QPDFOutlineDocumentHelper::checkSeen(QPDFObjGen const& og)
{
    if (this->m->seen.count(og) > 0)
    {
        return true;
    }
    this->m->seen.insert(og);
    return false;
}