2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 19:08:59 +00:00

Merge pull request #959 from m-holger/reserved

Add new method QPDF::newReserved()
This commit is contained in:
Jay Berkenbilt 2023-05-20 15:27:33 -04:00 committed by GitHub
commit 1c14a9e6c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 5 deletions

View File

@ -413,6 +413,24 @@ class QPDF
QPDF_DLL QPDF_DLL
QPDFObjectHandle newStream(std::string const& data); QPDFObjectHandle newStream(std::string const& data);
// A reserved object is a special sentinel used for qpdf to
// reserve a spot for an object that is going to be added to the
// QPDF object. Normally you don't have to use this type since
// you can just call QPDF::makeIndirectObject. However, in some
// cases, if you have to create objects with circular references,
// you may need to create a reserved object so that you can have a
// reference to it and then replace the object later. Reserved
// objects have the special property that they can't be resolved
// to direct objects. This makes it possible to replace a
// reserved object with a new object while preserving existing
// references to them. When you are ready to replace a reserved
// object with its replacement, use QPDF::replaceReserved for this
// purpose rather than the more general QPDF::replaceObject. It
// is an error to try to write a QPDF with QPDFWriter if it has
// any reserved objects in it.
QPDF_DLL
QPDFObjectHandle newReserved();
// Install this object handle as an indirect object and return an // Install this object handle as an indirect object and return an
// indirect reference to it. // indirect reference to it.
QPDF_DLL QPDF_DLL

View File

@ -668,7 +668,8 @@ class QPDFObjectHandle
// object with its replacement, use QPDF::replaceReserved for this // object with its replacement, use QPDF::replaceReserved for this
// purpose rather than the more general QPDF::replaceObject. It // purpose rather than the more general QPDF::replaceObject. It
// is an error to try to write a QPDF with QPDFWriter if it has // is an error to try to write a QPDF with QPDFWriter if it has
// any reserved objects in it. // any reserved objects in it. From QPDF 11.4, you can
// call QPDF::newReserved() instead.
QPDF_DLL QPDF_DLL
static QPDFObjectHandle newReserved(QPDF* qpdf); static QPDFObjectHandle newReserved(QPDF* qpdf);

View File

@ -1984,6 +1984,12 @@ QPDF::makeIndirectObject(QPDFObjectHandle oh)
return makeIndirectFromQPDFObject(oh.getObj()); return makeIndirectFromQPDFObject(oh.getObj());
} }
QPDFObjectHandle
QPDF::newReserved()
{
return makeIndirectFromQPDFObject(QPDF_Reserved::create());
}
QPDFObjectHandle QPDFObjectHandle
QPDF::newStream() QPDF::newStream()
{ {
@ -2207,9 +2213,8 @@ QPDF::reserveObjects(QPDFObjectHandle foreign, ObjCopier& obj_copier, bool top)
QTC::TC("qpdf", "QPDF copy indirect"); QTC::TC("qpdf", "QPDF copy indirect");
if (obj_copier.object_map.count(foreign_og) == 0) { if (obj_copier.object_map.count(foreign_og) == 0) {
obj_copier.to_copy.push_back(foreign); obj_copier.to_copy.push_back(foreign);
obj_copier.object_map[foreign_og] = foreign.isStream() obj_copier.object_map[foreign_og] =
? newStream() foreign.isStream() ? newStream() : newReserved();
: QPDFObjectHandle::newReserved(this);
} }
} }

View File

@ -2128,7 +2128,11 @@ QPDFObjectHandle::newStream(QPDF* qpdf, std::string const& data)
QPDFObjectHandle QPDFObjectHandle
QPDFObjectHandle::newReserved(QPDF* qpdf) QPDFObjectHandle::newReserved(QPDF* qpdf)
{ {
return qpdf->makeIndirectObject(QPDFObjectHandle(QPDF_Reserved::create())); if (qpdf == nullptr) {
throw std::runtime_error(
"attempt to create reserved object in null qpdf object");
}
return qpdf->newReserved();
} }
void void