2018-12-19 02:08:53 +00:00
|
|
|
#include <qpdf/QPDFOutlineDocumentHelper.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2018-12-19 02:08:53 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
|
|
|
|
QPDFOutlineDocumentHelper::QPDFOutlineDocumentHelper(QPDF& qpdf) :
|
|
|
|
QPDFDocumentHelper(qpdf),
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
QPDFObjectHandle root = qpdf.getRoot();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!root.hasKey("/Outlines")) {
|
2018-12-19 02:08:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPDFObjectHandle outlines = root.getKey("/Outlines");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!(outlines.isDictionary() && outlines.hasKey("/First"))) {
|
2018-12-19 02:08:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPDFObjectHandle cur = outlines.getKey("/First");
|
2023-01-05 12:20:37 +00:00
|
|
|
QPDFObjGen::set seen;
|
|
|
|
while (!cur.isNull() && seen.add(cur)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
m->outlines.push_back(QPDFOutlineObjectHelper::Accessor::create(cur, *this, 1));
|
2018-12-19 02:08:53 +00:00
|
|
|
cur = cur.getKey("/Next");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFOutlineDocumentHelper::hasOutlines()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return !m->outlines.empty();
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 17:34:02 +00:00
|
|
|
std::vector<QPDFOutlineObjectHelper>
|
2018-12-19 02:08:53 +00:00
|
|
|
QPDFOutlineDocumentHelper::getTopLevelOutlines()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return m->outlines;
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFOutlineDocumentHelper::initializeByPage()
|
|
|
|
{
|
|
|
|
std::list<QPDFOutlineObjectHelper> queue;
|
2023-05-21 13:42:34 +00:00
|
|
|
queue.insert(queue.end(), m->outlines.begin(), m->outlines.end());
|
2018-12-19 02:08:53 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
while (!queue.empty()) {
|
2018-12-19 02:08:53 +00:00
|
|
|
QPDFOutlineObjectHelper oh = queue.front();
|
|
|
|
queue.pop_front();
|
2023-05-21 13:42:34 +00:00
|
|
|
m->by_page[oh.getDestPage().getObjGen()].push_back(oh);
|
2019-07-03 17:34:02 +00:00
|
|
|
std::vector<QPDFOutlineObjectHelper> kids = oh.getKids();
|
2018-12-19 02:08:53 +00:00
|
|
|
queue.insert(queue.end(), kids.begin(), kids.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 17:34:02 +00:00
|
|
|
std::vector<QPDFOutlineObjectHelper>
|
2018-12-19 02:08:53 +00:00
|
|
|
QPDFOutlineDocumentHelper::getOutlinesForPage(QPDFObjGen const& og)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->by_page.empty()) {
|
2018-12-19 02:08:53 +00:00
|
|
|
initializeByPage();
|
|
|
|
}
|
2019-07-03 17:34:02 +00:00
|
|
|
std::vector<QPDFOutlineObjectHelper> result;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->by_page.count(og)) {
|
|
|
|
result = m->by_page[og];
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFOutlineDocumentHelper::resolveNamedDest(QPDFObjectHandle name)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle result;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (name.isName()) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->dest_dict.isInitialized()) {
|
2024-07-13 10:00:19 +00:00
|
|
|
m->dest_dict = qpdf.getRoot().getKey("/Dests");
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
2024-07-13 10:00:19 +00:00
|
|
|
QTC::TC("qpdf", "QPDFOutlineDocumentHelper name named dest");
|
2024-07-25 09:22:35 +00:00
|
|
|
result = m->dest_dict.getKeyIfDict(name.getName());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (name.isString()) {
|
2024-07-13 10:00:19 +00:00
|
|
|
if (!m->names_dest) {
|
|
|
|
auto dests = qpdf.getRoot().getKey("/Names").getKeyIfDict("/Dests");
|
|
|
|
if (dests.isDictionary()) {
|
|
|
|
m->names_dest = std::make_shared<QPDFNameTreeObjectHelper>(dests, qpdf);
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-13 10:00:19 +00:00
|
|
|
if (m->names_dest) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->names_dest->findObject(name.getUTF8Value(), result)) {
|
2018-12-19 02:08:53 +00:00
|
|
|
QTC::TC("qpdf", "QPDFOutlineDocumentHelper string named dest");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!result.isInitialized()) {
|
2024-07-13 10:00:19 +00:00
|
|
|
return QPDFObjectHandle::newNull();
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
2024-07-13 11:14:51 +00:00
|
|
|
if (result.isDictionary()) {
|
|
|
|
QTC::TC("qpdf", "QPDFOutlineDocumentHelper named dest dictionary");
|
|
|
|
return result.getKey("/D");
|
|
|
|
}
|
2018-12-19 02:08:53 +00:00
|
|
|
return result;
|
|
|
|
}
|