From d9c46a5ec329079da7acee05a14cc97fa154d23e Mon Sep 17 00:00:00 2001 From: m-holger Date: Wed, 3 Sep 2025 12:06:31 +0100 Subject: [PATCH] Refactor `QPDFAcroFormDocumentHelper::transformAnnotations`: update type handling with `Dictionary` and `Array`. --- libqpdf/QPDFAcroFormDocumentHelper.cc | 29 ++++++++++++++--------------- qpdf/qpdf.testcov | 1 - 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/libqpdf/QPDFAcroFormDocumentHelper.cc b/libqpdf/QPDFAcroFormDocumentHelper.cc index a27d23b..849e8b2 100644 --- a/libqpdf/QPDFAcroFormDocumentHelper.cc +++ b/libqpdf/QPDFAcroFormDocumentHelper.cc @@ -992,17 +992,18 @@ QPDFAcroFormDocumentHelper::transformAnnotations( // Identify and copy any appearance streams auto ah = QPDFAnnotationObjectHelper(annot); - auto apdict = ah.getAppearanceDictionary(); + Dictionary apdict = ah.getAppearanceDictionary(); std::vector streams; auto replace_stream = [](auto& dict, auto& key, auto& old) { - return dict.replaceKeyAndGetNew(key, old.copyStream()); + dict.replaceKey(key, old.copyStream()); + return dict[key]; }; - for (auto& [key1, value1]: apdict.as_dictionary()) { + for (auto& [key1, value1]: apdict) { if (value1.isStream()) { streams.emplace_back(replace_stream(apdict, key1, value1)); } else { - for (auto& [key2, value2]: value1.as_dictionary()) { + for (auto& [key2, value2]: Dictionary(value1)) { if (value2.isStream()) { streams.emplace_back(replace_stream(value1, key2, value2)); } @@ -1012,21 +1013,19 @@ QPDFAcroFormDocumentHelper::transformAnnotations( // Now we can safely mutate the annotation and its appearance streams. for (auto& stream: streams) { - auto dict = stream.getDict(); - auto omatrix = dict["/Matrix"]; + Dictionary dict = stream.getDict(); + QPDFMatrix apcm; - if (omatrix.isArray()) { - QTC::TC("qpdf", "QPDFAcroFormDocumentHelper modify ap matrix"); - auto m1 = omatrix.getArrayAsMatrix(); - apcm = QPDFMatrix(m1); + Array omatrix = dict["/Matrix"]; + if (omatrix) { + apcm = QPDFMatrix(QPDFObjectHandle(omatrix).getArrayAsMatrix()); } apcm.concat(cm); - auto new_matrix = QPDFObjectHandle::newFromMatrix(apcm); - if (omatrix.isArray() || (apcm != QPDFMatrix())) { - dict.replaceKey("/Matrix", new_matrix); + if (omatrix || apcm != QPDFMatrix()) { + dict.replaceKey("/Matrix", QPDFObjectHandle::newFromMatrix(apcm)); } - auto resources = dict["/Resources"]; - if ((!dr_map.empty()) && resources.isDictionary()) { + Dictionary resources = dict["/Resources"]; + if (!dr_map.empty() && resources) { adjustAppearanceStream(stream, dr_map); } } diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index 6a14817..4fff849 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -487,7 +487,6 @@ QPDFFileSpecObjectHelper empty compat_name 0 QPDFFileSpecObjectHelper non-empty compat_name 0 QPDFAcroFormDocumentHelper copy annotation 3 QPDFAcroFormDocumentHelper field with parent 3 -QPDFAcroFormDocumentHelper modify ap matrix 0 QPDFJob pages keeping field from original 0 QPDFObjectHandle merge reuse 0 QPDFObjectHandle merge generate 0 -- libgit2 0.21.4