2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-22 22:58:33 +00:00

Use replaceKeyAndGet in a few places in existing code

This commit is contained in:
Jay Berkenbilt 2022-04-29 20:21:07 -04:00
parent e80fad86e9
commit d8fdf632a9
7 changed files with 21 additions and 26 deletions

View File

@ -40,9 +40,9 @@ QPDFAcroFormDocumentHelper::getOrCreateAcroForm()
{
auto acroform = this->qpdf.getRoot().getKey("/AcroForm");
if (!acroform.isDictionary()) {
acroform =
this->qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
this->qpdf.getRoot().replaceKey("/AcroForm", acroform);
acroform = this->qpdf.getRoot().replaceKeyAndGet(
"/AcroForm",
this->qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary()));
}
return acroform;
}
@ -53,8 +53,8 @@ QPDFAcroFormDocumentHelper::addFormField(QPDFFormFieldObjectHelper ff)
auto acroform = getOrCreateAcroForm();
auto fields = acroform.getKey("/Fields");
if (!fields.isArray()) {
fields = QPDFObjectHandle::newArray();
acroform.replaceKey("/Fields", fields);
fields =
acroform.replaceKeyAndGet("/Fields", QPDFObjectHandle::newArray());
}
fields.appendItem(ff.getObjectHandle());
std::set<QPDFObjGen> visited;
@ -877,8 +877,8 @@ QPDFAcroFormDocumentHelper::transformAnnotations(
}
dr.makeResourcesIndirect(this->qpdf);
if (!dr.isIndirect()) {
dr = this->qpdf.makeIndirectObject(dr);
acroform.replaceKey("/DR", dr);
dr = acroform.replaceKeyAndGet(
"/DR", this->qpdf.makeIndirectObject(dr));
}
// Merge the other document's /DR, creating a conflict
// map. mergeResources checks to make sure both objects
@ -1099,9 +1099,7 @@ QPDFAcroFormDocumentHelper::transformAnnotations(
auto apdict = ah.getAppearanceDictionary();
std::vector<QPDFObjectHandle> streams;
auto replace_stream = [](auto& dict, auto& key, auto& old) {
auto new_stream = old.copyStream();
dict.replaceKey(key, new_stream);
return new_stream;
return dict.replaceKeyAndGet(key, old.copyStream());
};
if (apdict.isDictionary()) {
for (auto& ap : apdict.ditems()) {

View File

@ -28,8 +28,8 @@ QPDFEFStreamObjectHelper::setParam(
{
auto params = this->oh.getDict().getKey("/Params");
if (!params.isDictionary()) {
params = QPDFObjectHandle::newDictionary();
this->oh.getDict().replaceKey("/Params", params);
params = this->oh.getDict().replaceKeyAndGet(
"/Params", QPDFObjectHandle::newDictionary());
}
params.replaceKey(pkey, pval);
}

View File

@ -62,8 +62,8 @@ QPDFEmbeddedFileDocumentHelper::initEmbeddedFiles()
auto root = qpdf.getRoot();
auto names = root.getKey("/Names");
if (!names.isDictionary()) {
names = QPDFObjectHandle::newDictionary();
root.replaceKey("/Names", names);
names =
root.replaceKeyAndGet("/Names", QPDFObjectHandle::newDictionary());
}
auto embedded_files = names.getKey("/EmbeddedFiles");
if (!embedded_files.isDictionary()) {

View File

@ -1958,8 +1958,8 @@ QPDFJob::doUnderOverlayForPage(
QPDFObjectHandle resources = dest_page.getAttribute("/Resources", true);
if (!resources.isDictionary()) {
QTC::TC("qpdf", "QPDFJob overlay page with no resources");
resources = QPDFObjectHandle::newDictionary();
dest_page.getObjectHandle().replaceKey("/Resources", resources);
resources = dest_page.getObjectHandle().replaceKeyAndGet(
"/Resources", QPDFObjectHandle::newDictionary());
}
for (std::vector<int>::iterator iter = pagenos[pageno].begin();
iter != pagenos[pageno].end();

View File

@ -1172,8 +1172,7 @@ QPDFObjectHandle::mergeResources(
// subdictionaries just to get this shallow copy
// functionality.
QTC::TC("qpdf", "QPDFObjectHandle replace with copy");
this_val = this_val.shallowCopy();
replaceKey(rtype, this_val);
this_val = replaceKeyAndGet(rtype, this_val.shallowCopy());
}
std::map<QPDFObjGen, std::string> og_to_name;
std::set<std::string> rnames;

View File

@ -597,8 +597,7 @@ QPDFPageObjectHelper::removeUnreferencedResourcesHelper(
for (auto const& iter : to_filter) {
QPDFObjectHandle dict = resources.getKey(iter);
if (dict.isDictionary()) {
dict = dict.shallowCopy();
resources.replaceKey(iter, dict);
dict = resources.replaceKeyAndGet(iter, dict.shallowCopy());
rdicts.push_back(dict);
auto keys = dict.getKeys();
known_names.insert(keys.begin(), keys.end());
@ -1113,8 +1112,8 @@ QPDFPageObjectHelper::copyAnnotations(
afdh->addAndRenameFormFields(new_fields);
auto annots = this->oh.getKey("/Annots");
if (!annots.isArray()) {
annots = QPDFObjectHandle::newArray();
this->oh.replaceKey("/Annots", annots);
annots =
this->oh.replaceKeyAndGet("/Annots", QPDFObjectHandle::newArray());
}
for (auto const& annot : new_annots) {
annots.appendItem(annot);

View File

@ -1662,8 +1662,8 @@ QPDFWriter::unparseObject(
"qpdf",
"QPDFWriter create Extensions",
this->m->qdf_mode ? 0 : 1);
extensions = QPDFObjectHandle::newDictionary();
object.replaceKey("/Extensions", extensions);
extensions = object.replaceKeyAndGet(
"/Extensions", QPDFObjectHandle::newDictionary());
}
} else if (!have_extensions_other) {
// We have Extensions dictionary and don't want one.
@ -2387,8 +2387,7 @@ QPDFWriter::prepareFileForWrite()
if (oh.isIndirect()) {
QTC::TC("qpdf", "QPDFWriter make Extensions direct");
extensions_indirect = true;
oh = oh.shallowCopy();
root.replaceKey(key, oh);
oh = root.replaceKeyAndGet(key, oh.shallowCopy());
}
if (oh.hasKey("/ADBE")) {
QPDFObjectHandle adbe = oh.getKey("/ADBE");