From e1ca10ccc4009aeb79d2098da073fa94a9bb14e4 Mon Sep 17 00:00:00 2001 From: m-holger Date: Mon, 15 Jan 2024 12:01:29 +0000 Subject: [PATCH] In QPDFJob::handleUnderOverlay flatten nested fo maps --- include/qpdf/QPDFJob.hh | 2 +- libqpdf/QPDFJob.cc | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/qpdf/QPDFJob.hh b/include/qpdf/QPDFJob.hh index a89d1503..c78fbc0a 100644 --- a/include/qpdf/QPDFJob.hh +++ b/include/qpdf/QPDFJob.hh @@ -523,7 +523,7 @@ class QPDFJob UnderOverlay& uo, std::map, std::vector> const& pagenos, std::pair pageno_uo_idx, - std::map>& fo, + std::map, QPDFObjectHandle>& fo, std::vector&& pages, QPDFPageObjectHelper& dest_page); void validateUnderOverlay(QPDF& pdf, UnderOverlay* uo); diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index 64de4b68..f68adb91 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -1888,7 +1888,7 @@ QPDFJob::doUnderOverlayForPage( UnderOverlay& uo, std::map, std::vector> const& pagenos, std::pair pageno_uo_idx, - std::map>& fo, + std::map, QPDFObjectHandle>& fo, std::vector&& pages, QPDFPageObjectHelper& dest_page) { @@ -1910,9 +1910,12 @@ QPDFJob::doUnderOverlayForPage( v << " " << uo.filename << " " << uo.which << " " << from_page.no << "\n"; }); auto from_page_ph = pages.at(from_page.idx); - if (fo[from_page.no].count(pageno_uo_idx.second) == 0) { - fo[from_page.no][pageno_uo_idx.second] = - pdf.copyForeignObject(from_page_ph.getFormXObjectForPage()); + std::pair fo_index = {from_page.no, pageno_uo_idx.second}; + auto fo_it = fo.find(fo_index); + if (fo_it == fo.end()) { + fo_it = + fo.insert({fo_index, pdf.copyForeignObject(from_page_ph.getFormXObjectForPage())}) + .first; } // If the same page is overlaid or underlaid multiple times, we'll generate multiple names @@ -1920,16 +1923,13 @@ QPDFJob::doUnderOverlayForPage( std::string name = resources.getUniqueResourceName("/Fx", min_suffix); QPDFMatrix cm; std::string new_content = dest_page.placeFormXObject( - fo[from_page.no][pageno_uo_idx.second], - name, - dest_page.getTrimBox().getArrayAsRectangle(), - cm); + fo_it->second, name, dest_page.getTrimBox().getArrayAsRectangle(), cm); dest_page.copyAnnotations(from_page_ph, cm, dest_afdh, make_afdh(from_page_ph)); if (!new_content.empty()) { resources.mergeResources("<< /XObject << >> >>"_qpdf); auto xobject = resources.getKey("/XObject"); if (xobject.isDictionary()) { - xobject.replaceKey(name, fo[from_page.no][pageno_uo_idx.second]); + xobject.replaceKey(name, fo_it->second); } ++min_suffix; content += new_content; @@ -1992,8 +1992,8 @@ QPDFJob::handleUnderOverlay(QPDF& pdf) }; std::map> afdh; - std::map> underlay_fo; - std::map> overlay_fo; + std::map, QPDFObjectHandle> underlay_fo; + std::map, QPDFObjectHandle> overlay_fo; QPDFPageDocumentHelper main_pdh(pdf); auto main_pages = main_pdh.getAllPages(); size_t main_npages = main_pages.size();