diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index 9ea329ff..6ae6dcda 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -759,6 +759,10 @@ class QPDFObjectHandle QPDF_DLL QPDFDictItems ditems(); + class DictItems; + QPDF_DLL + DictItems dItems(); + // Return true if key is present. Keys with null values are treated as if they are not present. // This is as per the PDF spec. QPDF_DLL @@ -1418,6 +1422,41 @@ QPDFObjectHandle operator ""_qpdf(char const* v, size_t len); #endif // QPDF_NO_QPDF_STRING +class QPDFObjectHandle::DictItems +{ + friend class QPDFObjectHandle; + + public: + class iterator: public std::map::iterator + { + public: + iterator(std::map::iterator it) : + std::map::iterator(it) + { + } + }; + + iterator begin() + { + return dict.begin(); + } + + iterator end() + { + return dict.end(); + } + + private: + DictItems(QPDFObjectHandle& oh, std::map& dict) : + oh(oh), + dict(dict) + { + } + + QPDFObjectHandle oh; + std::map& dict; +}; + class QPDFObjectHandle::QPDFDictItems { // This class allows C++-style iteration, including range-for iteration, around dictionaries. diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 163ebe88..55394faf 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -962,6 +962,19 @@ QPDFObjectHandle::ditems() return {*this}; } +QPDFObjectHandle::DictItems +QPDFObjectHandle::dItems() +{ + static std::map empty; + if (auto dict = asDictionary()) { + return {*this, dict->getAsMap()}; + } else { + typeWarning("dictionary", "treating as empty"); + // QTC::TC("qpdf", "QPDFObjectHandle dictionary empty map for dItems"); + return {*this, empty}; + } +} + bool QPDFObjectHandle::hasKey(std::string const& key) { diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 981fc755..af67c45a 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -1129,7 +1129,7 @@ QPDFWriter::enqueueObject(QPDFObjectHandle object) enqueueObject(item); } } else if (object.isDictionary()) { - for (auto& item: object.getDictAsMap()) { + for (auto& item: object.dItems()) { if (!item.second.isNull()) { enqueueObject(item.second); } @@ -1487,7 +1487,7 @@ QPDFWriter::unparseObject( writeString("<<"); - for (auto& item: object.getDictAsMap()) { + for (auto& item: object.dItems()) { if (!item.second.isNull()) { auto const& key = item.first; writeString(indent); diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index 9332b1d3..ade61fbc 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -126,8 +126,8 @@ QPDF_Dictionary::getKeys() return result; } -std::map const& -QPDF_Dictionary::getAsMap() const +std::map& +QPDF_Dictionary::getAsMap() { return this->items; } @@ -152,3 +152,5 @@ QPDF_Dictionary::removeKey(std::string const& key) // no-op if key does not exist this->items.erase(key); } + + diff --git a/libqpdf/qpdf/QPDF_Dictionary.hh b/libqpdf/qpdf/QPDF_Dictionary.hh index 8713a450..5ebea959 100644 --- a/libqpdf/qpdf/QPDF_Dictionary.hh +++ b/libqpdf/qpdf/QPDF_Dictionary.hh @@ -24,8 +24,7 @@ class QPDF_Dictionary: public QPDFValue bool hasKey(std::string const&); QPDFObjectHandle getKey(std::string const&); std::set getKeys(); - std::map const& getAsMap() const; - + std::map& getAsMap(); // If value is null, remove key; otherwise, replace the value of key, adding it if it does not // exist. void replaceKey(std::string const& key, QPDFObjectHandle value);