2018-12-18 16:29:00 +00:00
|
|
|
#include <qpdf/QPDFPageLabelDocumentHelper.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2018-12-18 16:29:00 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
|
|
|
|
QPDFPageLabelDocumentHelper::QPDFPageLabelDocumentHelper(QPDF& qpdf) :
|
|
|
|
QPDFDocumentHelper(qpdf),
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
QPDFObjectHandle root = qpdf.getRoot();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (root.hasKey("/PageLabels")) {
|
2022-04-09 18:35:56 +00:00
|
|
|
this->m->labels = std::make_shared<QPDFNumberTreeObjectHelper>(
|
2021-02-10 20:01:59 +00:00
|
|
|
root.getKey("/PageLabels"), this->qpdf);
|
2018-12-18 16:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFPageLabelDocumentHelper::hasPageLabels()
|
|
|
|
{
|
2022-07-26 11:37:50 +00:00
|
|
|
return nullptr != this->m->labels.get();
|
2018-12-18 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFPageLabelDocumentHelper::getLabelForPage(long long page_idx)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle result(QPDFObjectHandle::newNull());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!hasPageLabels()) {
|
2018-12-18 16:29:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
QPDFNumberTreeObjectHelper::numtree_number offset = 0;
|
|
|
|
QPDFObjectHandle label;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!this->m->labels->findObjectAtOrBelow(page_idx, label, offset)) {
|
2018-12-18 16:29:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!label.isDictionary()) {
|
2018-12-18 16:29:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
QPDFObjectHandle S = label.getKey("/S"); // type (D, R, r, A, a)
|
|
|
|
QPDFObjectHandle P = label.getKey("/P"); // prefix
|
|
|
|
QPDFObjectHandle St = label.getKey("/St"); // starting number
|
|
|
|
long long start = 1;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (St.isInteger()) {
|
2018-12-18 16:29:00 +00:00
|
|
|
start = St.getIntValue();
|
|
|
|
}
|
2020-10-22 09:45:01 +00:00
|
|
|
QIntC::range_check(start, offset);
|
2018-12-18 16:29:00 +00:00
|
|
|
start += offset;
|
|
|
|
result = QPDFObjectHandle::newDictionary();
|
2022-05-17 22:28:50 +00:00
|
|
|
result.replaceKey("/S", S);
|
|
|
|
result.replaceKey("/P", P);
|
|
|
|
result.replaceKey("/St", QPDFObjectHandle::newInteger(start));
|
2018-12-18 16:29:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFPageLabelDocumentHelper::getLabelsForPageRange(
|
2022-04-02 21:14:10 +00:00
|
|
|
long long start_idx,
|
|
|
|
long long end_idx,
|
|
|
|
long long new_start_idx,
|
2018-12-18 16:29:00 +00:00
|
|
|
std::vector<QPDFObjectHandle>& new_labels)
|
|
|
|
{
|
|
|
|
// Start off with a suitable label for the first page. For every
|
|
|
|
// remaining page, if that page has an explicit entry, copy it.
|
|
|
|
// Otherwise, let the subsequent page just sequence from the prior
|
|
|
|
// entry. If there is no entry for the first page, fabricate one
|
|
|
|
// that would match how the page would look in a new file in which
|
|
|
|
// it also didn't have an explicit label.
|
|
|
|
QPDFObjectHandle label = getLabelForPage(start_idx);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (label.isNull()) {
|
2018-12-18 16:29:00 +00:00
|
|
|
label = QPDFObjectHandle::newDictionary();
|
|
|
|
label.replaceKey(
|
|
|
|
"/St", QPDFObjectHandle::newInteger(1 + new_start_idx));
|
|
|
|
}
|
|
|
|
// See if the new label is redundant based on the previous entry
|
|
|
|
// in the vector. If so, don't add it.
|
|
|
|
size_t size = new_labels.size();
|
|
|
|
bool skip_first = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (size >= 2) {
|
2018-12-19 13:19:06 +00:00
|
|
|
QPDFObjectHandle last = new_labels.at(size - 1);
|
|
|
|
QPDFObjectHandle last_idx = new_labels.at(size - 2);
|
2018-12-18 16:29:00 +00:00
|
|
|
if (last_idx.isInteger() && last.isDictionary() &&
|
|
|
|
(label.getKey("/S").unparse() == last.getKey("/S").unparse()) &&
|
|
|
|
(label.getKey("/P").unparse() == last.getKey("/P").unparse()) &&
|
2022-04-02 21:14:10 +00:00
|
|
|
label.getKey("/St").isInteger() && last.getKey("/St").isInteger()) {
|
|
|
|
long long int st_delta = label.getKey("/St").getIntValue() -
|
2018-12-18 16:29:00 +00:00
|
|
|
last.getKey("/St").getIntValue();
|
2022-04-02 21:14:10 +00:00
|
|
|
long long int idx_delta = new_start_idx - last_idx.getIntValue();
|
|
|
|
if (st_delta == idx_delta) {
|
2018-12-18 16:29:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFPageLabelDocumentHelper skip first");
|
|
|
|
skip_first = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!skip_first) {
|
2018-12-18 16:29:00 +00:00
|
|
|
new_labels.push_back(QPDFObjectHandle::newInteger(new_start_idx));
|
|
|
|
new_labels.push_back(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
long long int idx_offset = new_start_idx - start_idx;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (long long i = start_idx + 1; i <= end_idx; ++i) {
|
2018-12-18 16:29:00 +00:00
|
|
|
if (this->m->labels->hasIndex(i) &&
|
2022-04-02 21:14:10 +00:00
|
|
|
(label = getLabelForPage(i)).isDictionary()) {
|
2018-12-18 16:29:00 +00:00
|
|
|
new_labels.push_back(QPDFObjectHandle::newInteger(i + idx_offset));
|
|
|
|
new_labels.push_back(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|