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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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