2012-06-21 13:18:49 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <qpdf/QPDFExc.hh>
|
|
|
|
|
2012-06-21 14:42:18 +00:00
|
|
|
// In support of page manipulation APIs, these methods internally
|
|
|
|
// maintain state about pages in a pair of data structures: all_pages,
|
|
|
|
// which is a vector of page objects, and pageobj_to_pages_pos, which
|
|
|
|
// maps a page object to its position in the all_pages array.
|
|
|
|
// Unfortunately, the getAllPages() method returns a const reference
|
|
|
|
// to all_pages and has been in the public API long before the
|
|
|
|
// introduction of mutation APIs, so we're pretty much stuck with it.
|
|
|
|
// Anyway, there are lots of calls to it in the library, so the
|
|
|
|
// efficiency of having it cached is probably worth keeping it.
|
|
|
|
|
|
|
|
// The goal of this code is to ensure that the all_pages vector, which
|
|
|
|
// users may have a reference to, and the pageobj_to_pages_pos map,
|
|
|
|
// which users will not have access to, remain consistent outside of
|
|
|
|
// any call to the library. As long as users only touch the /Pages
|
|
|
|
// structure through page-specific API calls, they never have to worry
|
|
|
|
// about anything, and this will also stay consistent. If a user
|
|
|
|
// touches anything about the /Pages structure outside of these calls
|
|
|
|
// (such as by directly looking up and manipulating the underlying
|
|
|
|
// objects), they can call updatePagesCache() to bring things back in
|
|
|
|
// sync.
|
|
|
|
|
|
|
|
// If the user doesn't ever use the page manipulation APIs, then qpdf
|
|
|
|
// leaves the /Pages structure alone. If the user does use the APIs,
|
|
|
|
// then we push all inheritable objects down and flatten the /Pages
|
|
|
|
// tree. This makes it easier for us to keep /Pages, all_pages, and
|
|
|
|
// pageobj_to_pages_pos internally consistent at all times.
|
|
|
|
|
|
|
|
// Responsibility for keeping all_pages, pageobj_to_pages_pos, and the
|
|
|
|
// Pages structure consistent should remain in as few places as
|
|
|
|
// possible. As of initial writing, only flattenPagesTree,
|
|
|
|
// insertPage, and removePage, along with methods they call, are
|
|
|
|
// concerned with it. Everything else goes through one of those
|
|
|
|
// methods.
|
|
|
|
|
2012-06-21 13:18:49 +00:00
|
|
|
std::vector<QPDFObjectHandle> const&
|
|
|
|
QPDF::getAllPages()
|
|
|
|
{
|
2012-06-22 16:11:25 +00:00
|
|
|
// Note that pushInheritedAttributesToPage may also be used to
|
2017-08-22 01:33:44 +00:00
|
|
|
// initialize this->m->all_pages.
|
|
|
|
if (this->m->all_pages.empty())
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2019-01-30 03:04:52 +00:00
|
|
|
std::set<QPDFObjGen> visited;
|
|
|
|
std::set<QPDFObjGen> seen;
|
|
|
|
getAllPagesInternal(getRoot().getKey("/Pages"), this->m->all_pages,
|
|
|
|
visited, seen);
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->all_pages;
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::getAllPagesInternal(QPDFObjectHandle cur_pages,
|
2019-01-30 03:04:52 +00:00
|
|
|
std::vector<QPDFObjectHandle>& result,
|
|
|
|
std::set<QPDFObjGen>& visited,
|
|
|
|
std::set<QPDFObjGen>& seen)
|
2015-02-21 23:22:32 +00:00
|
|
|
{
|
|
|
|
QPDFObjGen this_og = cur_pages.getObjGen();
|
|
|
|
if (visited.count(this_og) > 0)
|
|
|
|
{
|
|
|
|
throw QPDFExc(
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf_e_pages, this->m->file->getName(),
|
|
|
|
this->m->last_object_description, 0,
|
2015-02-21 23:22:32 +00:00
|
|
|
"Loop detected in /Pages structure (getAllPages)");
|
|
|
|
}
|
|
|
|
visited.insert(this_og);
|
2014-12-01 19:44:47 +00:00
|
|
|
std::string type;
|
|
|
|
QPDFObjectHandle type_key = cur_pages.getKey("/Type");
|
|
|
|
if (type_key.isName())
|
|
|
|
{
|
|
|
|
type = type_key.getName();
|
|
|
|
}
|
|
|
|
else if (cur_pages.hasKey("/Kids"))
|
|
|
|
{
|
|
|
|
type = "/Pages";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type = "/Page";
|
|
|
|
}
|
2012-06-21 13:18:49 +00:00
|
|
|
if (type == "/Pages")
|
|
|
|
{
|
|
|
|
QPDFObjectHandle kids = cur_pages.getKey("/Kids");
|
|
|
|
int n = kids.getArrayNItems();
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
2019-01-29 01:13:10 +00:00
|
|
|
QPDFObjectHandle kid = kids.getArrayItem(i);
|
2019-01-29 22:01:36 +00:00
|
|
|
if (! kid.isIndirect())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF handle direct page object");
|
|
|
|
kid = makeIndirectObject(kid);
|
|
|
|
kids.setArrayItem(i, kid);
|
|
|
|
}
|
|
|
|
else if (seen.count(kid.getObjGen()))
|
2019-01-29 01:13:10 +00:00
|
|
|
{
|
|
|
|
// Make a copy of the page. This does the same as
|
|
|
|
// shallowCopyPage in QPDFPageObjectHelper.
|
|
|
|
QTC::TC("qpdf", "QPDF resolve duplicated page object");
|
|
|
|
kid = makeIndirectObject(QPDFObjectHandle(kid).shallowCopy());
|
|
|
|
kids.setArrayItem(i, kid);
|
|
|
|
}
|
2019-01-30 03:04:52 +00:00
|
|
|
getAllPagesInternal(kid, result, visited, seen);
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == "/Page")
|
|
|
|
{
|
2019-01-29 01:13:10 +00:00
|
|
|
seen.insert(this_og);
|
2012-06-21 13:18:49 +00:00
|
|
|
result.push_back(cur_pages);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2012-06-22 03:06:48 +00:00
|
|
|
"invalid Type " + type + " in page tree");
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
2015-02-21 23:22:32 +00:00
|
|
|
visited.erase(this_og);
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-21 14:42:18 +00:00
|
|
|
QPDF::updateAllPagesCache()
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2012-06-21 14:42:18 +00:00
|
|
|
// Force regeneration of the pages cache. We force immediate
|
|
|
|
// recalculation of all_pages since users may have references to
|
|
|
|
// it that they got from calls to getAllPages(). We can defer
|
|
|
|
// recalculation of pageobj_to_pages_pos until needed.
|
|
|
|
QTC::TC("qpdf", "QPDF updateAllPagesCache");
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->all_pages.clear();
|
|
|
|
this->m->pageobj_to_pages_pos.clear();
|
|
|
|
this->m->pushed_inherited_attributes_to_pages = false;
|
2012-06-21 14:42:18 +00:00
|
|
|
getAllPages();
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::flattenPagesTree()
|
|
|
|
{
|
2012-06-21 14:42:18 +00:00
|
|
|
// If not already done, flatten the /Pages structure and
|
|
|
|
// initialize pageobj_to_pages_pos.
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->pageobj_to_pages_pos.empty())
|
2012-06-21 14:42:18 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2012-06-22 16:11:25 +00:00
|
|
|
// Push inherited objects down to the /Page level. As a side
|
2017-08-22 01:33:44 +00:00
|
|
|
// effect this->m->all_pages will also be generated.
|
2012-06-22 16:52:13 +00:00
|
|
|
pushInheritedAttributesToPage(true, true);
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2012-06-21 20:05:02 +00:00
|
|
|
QPDFObjectHandle pages = getRoot().getKey("/Pages");
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t const len = this->m->all_pages.size();
|
|
|
|
for (size_t pos = 0; pos < len; ++pos)
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2012-06-21 14:42:18 +00:00
|
|
|
// populate pageobj_to_pages_pos and fix parent pointer
|
2019-06-21 03:35:23 +00:00
|
|
|
insertPageobjToPage(this->m->all_pages.at(pos), toI(pos), true);
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->all_pages.at(pos).replaceKey("/Parent", pages);
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
pages.replaceKey("/Kids", QPDFObjectHandle::newArray(this->m->all_pages));
|
2012-06-21 13:18:49 +00:00
|
|
|
// /Count has not changed
|
2019-06-21 03:35:23 +00:00
|
|
|
if (pages.getKey("/Count").getUIntValue() != len)
|
2013-10-05 20:37:27 +00:00
|
|
|
{
|
|
|
|
throw std::logic_error("/Count is wrong after flattening pages tree");
|
|
|
|
}
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
|
2012-06-21 14:42:18 +00:00
|
|
|
void
|
|
|
|
QPDF::insertPageobjToPage(QPDFObjectHandle const& obj, int pos,
|
|
|
|
bool check_duplicate)
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen og(obj.getObjGen());
|
2012-06-21 16:21:34 +00:00
|
|
|
if (check_duplicate)
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->pageobj_to_pages_pos.insert(
|
|
|
|
std::make_pair(og, pos)).second)
|
2012-06-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF duplicate page reference");
|
|
|
|
setLastObjectDescription("page " + QUtil::int_to_string(pos) +
|
|
|
|
" (numbered from zero)",
|
2013-06-14 15:22:04 +00:00
|
|
|
og.getObj(), og.getGen());
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_pages, this->m->file->getName(),
|
|
|
|
this->m->last_object_description, 0,
|
2012-06-21 16:21:34 +00:00
|
|
|
"duplicate page reference found;"
|
|
|
|
" this would cause loss of data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->pageobj_to_pages_pos[og] = pos;
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-21 14:42:18 +00:00
|
|
|
QPDF::insertPage(QPDFObjectHandle newpage, int pos)
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2012-07-29 18:32:54 +00:00
|
|
|
// pos is numbered from 0, so pos = 0 inserts at the beginning and
|
2012-06-21 14:42:18 +00:00
|
|
|
// pos = npages adds to the end.
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2012-06-21 14:42:18 +00:00
|
|
|
flattenPagesTree();
|
|
|
|
|
2012-06-21 16:21:34 +00:00
|
|
|
if (! newpage.isIndirect())
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF insert non-indirect page");
|
2017-08-22 01:33:44 +00:00
|
|
|
newpage = makeIndirectObject(newpage);
|
2012-06-21 16:21:34 +00:00
|
|
|
}
|
2012-07-11 19:29:41 +00:00
|
|
|
else if (newpage.getOwningQPDF() != this)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF insert foreign page");
|
|
|
|
newpage.getOwningQPDF()->pushInheritedAttributesToPage();
|
2019-01-29 02:53:55 +00:00
|
|
|
newpage = copyForeignObject(newpage);
|
2012-07-11 19:29:41 +00:00
|
|
|
}
|
2012-06-21 16:21:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF insert indirect page");
|
|
|
|
}
|
|
|
|
|
2012-06-21 14:42:18 +00:00
|
|
|
QTC::TC("qpdf", "QPDF insert page",
|
|
|
|
(pos == 0) ? 0 : // insert at beginning
|
2019-06-21 03:35:23 +00:00
|
|
|
(pos == QIntC::to_int(this->m->all_pages.size())) ? 1 : // at end
|
2012-06-21 14:42:18 +00:00
|
|
|
2); // insert in middle
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2012-06-21 20:05:02 +00:00
|
|
|
QPDFObjectHandle pages = getRoot().getKey("/Pages");
|
2012-06-21 13:18:49 +00:00
|
|
|
QPDFObjectHandle kids = pages.getKey("/Kids");
|
2019-06-21 03:35:23 +00:00
|
|
|
assert ((pos >= 0) && (QIntC::to_size(pos) <= this->m->all_pages.size()));
|
2012-06-21 13:18:49 +00:00
|
|
|
|
|
|
|
newpage.replaceKey("/Parent", pages);
|
2012-06-21 14:42:18 +00:00
|
|
|
kids.insertItem(pos, newpage);
|
|
|
|
int npages = kids.getArrayNItems();
|
|
|
|
pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages));
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->all_pages.insert(this->m->all_pages.begin() + pos, newpage);
|
2019-06-21 03:35:23 +00:00
|
|
|
assert(this->m->all_pages.size() == QIntC::to_size(npages));
|
2012-06-21 14:42:18 +00:00
|
|
|
for (int i = pos + 1; i < npages; ++i)
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
insertPageobjToPage(this->m->all_pages.at(toS(i)), i, false);
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
2012-06-21 14:42:18 +00:00
|
|
|
insertPageobjToPage(newpage, pos, true);
|
2019-06-21 03:35:23 +00:00
|
|
|
assert(this->m->pageobj_to_pages_pos.size() == QIntC::to_size(npages));
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-21 14:42:18 +00:00
|
|
|
QPDF::removePage(QPDFObjectHandle page)
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2012-06-21 14:42:18 +00:00
|
|
|
int pos = findPage(page); // also ensures flat /Pages
|
|
|
|
QTC::TC("qpdf", "QPDF remove page",
|
2013-02-24 02:46:21 +00:00
|
|
|
(pos == 0) ? 0 : // remove at beginning
|
2019-06-21 03:35:23 +00:00
|
|
|
(pos == QIntC::to_int(this->m->all_pages.size() - 1)) ? 1 : // end
|
2013-02-24 02:46:21 +00:00
|
|
|
2); // remove in middle
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2012-06-21 20:05:02 +00:00
|
|
|
QPDFObjectHandle pages = getRoot().getKey("/Pages");
|
2012-06-21 13:18:49 +00:00
|
|
|
QPDFObjectHandle kids = pages.getKey("/Kids");
|
|
|
|
|
2012-06-21 14:42:18 +00:00
|
|
|
kids.eraseItem(pos);
|
|
|
|
int npages = kids.getArrayNItems();
|
|
|
|
pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages));
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->all_pages.erase(this->m->all_pages.begin() + pos);
|
2019-06-21 03:35:23 +00:00
|
|
|
assert(this->m->all_pages.size() == QIntC::to_size(npages));
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->pageobj_to_pages_pos.erase(page.getObjGen());
|
2019-06-21 03:35:23 +00:00
|
|
|
assert(this->m->pageobj_to_pages_pos.size() == QIntC::to_size(npages));
|
2012-06-21 14:42:18 +00:00
|
|
|
for (int i = pos; i < npages; ++i)
|
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
insertPageobjToPage(this->m->all_pages.at(toS(i)), i, false);
|
2012-06-21 14:42:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::addPageAt(QPDFObjectHandle newpage, bool before,
|
|
|
|
QPDFObjectHandle refpage)
|
|
|
|
{
|
|
|
|
int refpos = findPage(refpage);
|
2012-06-21 13:18:49 +00:00
|
|
|
if (! before)
|
|
|
|
{
|
|
|
|
++refpos;
|
|
|
|
}
|
2012-06-21 14:42:18 +00:00
|
|
|
insertPage(newpage, refpos);
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-21 14:42:18 +00:00
|
|
|
QPDF::addPage(QPDFObjectHandle newpage, bool first)
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2012-06-21 14:42:18 +00:00
|
|
|
if (first)
|
2012-06-21 13:18:49 +00:00
|
|
|
{
|
2012-06-21 14:42:18 +00:00
|
|
|
insertPage(newpage, 0);
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|
2012-06-21 14:42:18 +00:00
|
|
|
else
|
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
insertPage(
|
|
|
|
newpage,
|
|
|
|
getRoot().getKey("/Pages").getKey("/Count").getIntValueAsInt());
|
2012-06-21 14:42:18 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2012-06-21 14:42:18 +00:00
|
|
|
int
|
|
|
|
QPDF::findPage(QPDFObjectHandle& page)
|
|
|
|
{
|
2013-06-14 15:58:37 +00:00
|
|
|
return findPage(page.getObjGen());
|
2012-06-21 14:42:18 +00:00
|
|
|
}
|
2012-06-21 13:18:49 +00:00
|
|
|
|
2012-06-21 14:42:18 +00:00
|
|
|
int
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDF::findPage(QPDFObjGen const& og)
|
2012-06-21 14:42:18 +00:00
|
|
|
{
|
|
|
|
flattenPagesTree();
|
2013-06-14 15:22:04 +00:00
|
|
|
std::map<QPDFObjGen, int>::iterator it =
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->pageobj_to_pages_pos.find(og);
|
|
|
|
if (it == this->m->pageobj_to_pages_pos.end())
|
2012-06-21 14:42:18 +00:00
|
|
|
{
|
2013-06-14 15:58:37 +00:00
|
|
|
setLastObjectDescription("page object", og.getObj(), og.getGen());
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_pages, this->m->file->getName(),
|
|
|
|
this->m->last_object_description, 0,
|
2012-06-22 20:25:29 +00:00
|
|
|
"page object not referenced in /Pages tree");
|
2012-06-21 14:42:18 +00:00
|
|
|
}
|
|
|
|
return (*it).second;
|
2012-06-21 13:18:49 +00:00
|
|
|
}
|