2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
|
|
|
|
2023-02-17 13:58:21 +00:00
|
|
|
#include <qpdf/QPDFObject_private.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Name.hh>
|
2023-02-17 14:59:33 +00:00
|
|
|
#include <qpdf/QPDF_Null.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-02-17 13:58:21 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items) :
|
2022-08-02 21:57:33 +00:00
|
|
|
QPDFValue(::ot_dictionary, "dictionary"),
|
2008-04-29 12:55:25 +00:00
|
|
|
items(items)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-19 19:20:36 +00:00
|
|
|
QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items) :
|
|
|
|
QPDFValue(::ot_dictionary, "dictionary"),
|
|
|
|
items(items)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 16:45:04 +00:00
|
|
|
QPDF_Dictionary::create(std::map<std::string, QPDFObjectHandle> const& items)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Dictionary(items));
|
|
|
|
}
|
|
|
|
|
2022-12-19 19:20:36 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
|
|
|
QPDF_Dictionary::create(std::map<std::string, QPDFObjectHandle>&& items)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Dictionary(items));
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-11-14 17:54:12 +00:00
|
|
|
QPDF_Dictionary::copy(bool shallow)
|
2022-06-16 16:45:04 +00:00
|
|
|
{
|
2022-11-14 22:06:04 +00:00
|
|
|
if (shallow) {
|
|
|
|
return create(items);
|
|
|
|
} else {
|
|
|
|
std::map<std::string, QPDFObjectHandle> new_items;
|
|
|
|
for (auto const& item: this->items) {
|
|
|
|
auto value = item.second;
|
|
|
|
new_items[item.first] = value.isIndirect() ? value : value.shallowCopy();
|
|
|
|
}
|
|
|
|
return create(new_items);
|
|
|
|
}
|
2022-06-16 16:45:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:31 +00:00
|
|
|
void
|
2022-09-08 15:06:15 +00:00
|
|
|
QPDF_Dictionary::disconnect()
|
2022-09-07 20:49:31 +00:00
|
|
|
{
|
|
|
|
for (auto& iter: this->items) {
|
2022-09-08 15:06:15 +00:00
|
|
|
QPDFObjectHandle::DisconnectAccess::disconnect(iter.second);
|
2022-09-07 20:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Dictionary::unparse()
|
|
|
|
{
|
|
|
|
std::string result = "<< ";
|
2022-04-24 14:08:32 +00:00
|
|
|
for (auto& iter: this->items) {
|
|
|
|
if (!iter.second.isNull()) {
|
|
|
|
result += QPDF_Name::normalizeName(iter.first) + " " + iter.second.unparse() + " ";
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
result += ">>";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-17 22:40:29 +00:00
|
|
|
JSON
|
2022-05-07 11:53:45 +00:00
|
|
|
QPDF_Dictionary::getJSON(int json_version)
|
2018-12-17 22:40:29 +00:00
|
|
|
{
|
|
|
|
JSON j = JSON::makeDictionary();
|
2022-04-24 14:08:32 +00:00
|
|
|
for (auto& iter: this->items) {
|
|
|
|
if (!iter.second.isNull()) {
|
2022-05-17 23:18:02 +00:00
|
|
|
std::string key =
|
|
|
|
(json_version == 1 ? QPDF_Name::normalizeName(iter.first) : iter.first);
|
|
|
|
j.addDictionaryMember(key, iter.second.getJSON(json_version));
|
2022-04-24 14:08:32 +00:00
|
|
|
}
|
2018-12-17 22:40:29 +00:00
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
bool
|
|
|
|
QPDF_Dictionary::hasKey(std::string const& key)
|
|
|
|
{
|
|
|
|
return ((this->items.count(key) > 0) && (!this->items[key].isNull()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF_Dictionary::getKey(std::string const& key)
|
|
|
|
{
|
|
|
|
// PDF spec says fetching a non-existent key from a dictionary returns the null object.
|
2022-04-24 14:08:32 +00:00
|
|
|
auto item = this->items.find(key);
|
|
|
|
if (item != this->items.end()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// May be a null object
|
2022-04-24 14:08:32 +00:00
|
|
|
return item->second;
|
2008-04-29 12:55:25 +00:00
|
|
|
} else {
|
2023-02-17 13:58:21 +00:00
|
|
|
static auto constexpr msg = " -> dictionary key $VD"sv;
|
2023-02-17 14:59:33 +00:00
|
|
|
return QPDF_Null::create(shared_from_this(), msg, key);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::string>
|
|
|
|
QPDF_Dictionary::getKeys()
|
|
|
|
{
|
|
|
|
std::set<std::string> result;
|
2022-04-24 14:08:32 +00:00
|
|
|
for (auto& iter: this->items) {
|
|
|
|
if (!iter.second.isNull()) {
|
|
|
|
result.insert(iter.first);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-08-10 17:33:58 +00:00
|
|
|
std::map<std::string, QPDFObjectHandle> const&
|
|
|
|
QPDF_Dictionary::getAsMap() const
|
|
|
|
{
|
|
|
|
return this->items;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2022-04-24 13:31:32 +00:00
|
|
|
QPDF_Dictionary::replaceKey(std::string const& key, QPDFObjectHandle value)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-08-25 11:21:25 +00:00
|
|
|
if (value.isNull() && !value.isIndirect()) {
|
2023-08-25 11:25:06 +00:00
|
|
|
// The PDF spec doesn't distinguish between keys with null values and missing keys. Allow
|
|
|
|
// indirect nulls which are equivalent to a dangling reference, which is permitted by the
|
|
|
|
// spec.
|
2022-04-24 13:31:32 +00:00
|
|
|
removeKey(key);
|
|
|
|
} else {
|
|
|
|
// add or replace value
|
|
|
|
this->items[key] = value;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF_Dictionary::removeKey(std::string const& key)
|
|
|
|
{
|
|
|
|
// no-op if key does not exist
|
|
|
|
this->items.erase(key);
|
|
|
|
}
|