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()) {
|
|
|
|
m->dest_dict = this->qpdf.getRoot().getKey("/Dests");
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->dest_dict.isDictionary()) {
|
2018-12-19 02:08:53 +00:00
|
|
|
QTC::TC("qpdf", "QPDFOutlineDocumentHelper name named dest");
|
2023-05-21 13:42:34 +00:00
|
|
|
result = m->dest_dict.getKey(name.getName());
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (name.isString()) {
|
2023-05-21 13:42:34 +00:00
|
|
|
if (nullptr == m->names_dest) {
|
2018-12-19 02:08:53 +00:00
|
|
|
QPDFObjectHandle names = this->qpdf.getRoot().getKey("/Names");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (names.isDictionary()) {
|
2018-12-19 02:08:53 +00:00
|
|
|
QPDFObjectHandle dests = names.getKey("/Dests");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (dests.isDictionary()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
m->names_dest = std::make_shared<QPDFNameTreeObjectHelper>(dests, this->qpdf);
|
2018-12-19 02:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->names_dest.get()) {
|
|
|
|
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()) {
|
2018-12-19 02:08:53 +00:00
|
|
|
result = QPDFObjectHandle::newNull();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|