2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
|
|
|
|
2024-02-09 13:09:08 +00:00
|
|
|
#include <qpdf/JSON_writer.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>
|
2024-01-29 13:22:58 +00:00
|
|
|
#include <qpdf/QUtil.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) :
|
2024-08-12 16:52:42 +00:00
|
|
|
QPDFValue(::ot_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) :
|
2024-08-12 16:52:42 +00:00
|
|
|
QPDFValue(::ot_dictionary),
|
2022-12-19 19:20:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-09 13:09:08 +00:00
|
|
|
void
|
|
|
|
QPDF_Dictionary::writeJSON(int json_version, JSON::Writer& p)
|
|
|
|
{
|
|
|
|
p.writeStart('{');
|
|
|
|
for (auto& iter: this->items) {
|
|
|
|
if (!iter.second.isNull()) {
|
|
|
|
p.writeNext();
|
|
|
|
if (json_version == 1) {
|
2024-02-10 12:03:28 +00:00
|
|
|
p << "\"" << JSON::Writer::encode_string(QPDF_Name::normalizeName(iter.first))
|
|
|
|
<< "\": ";
|
|
|
|
} else if (auto res = QPDF_Name::analyzeJSONEncoding(iter.first); res.first) {
|
|
|
|
if (res.second) {
|
|
|
|
p << "\"" << iter.first << "\": ";
|
2024-02-09 13:09:08 +00:00
|
|
|
} else {
|
2024-02-10 12:03:28 +00:00
|
|
|
p << "\"" << JSON::Writer::encode_string(iter.first) << "\": ";
|
2024-02-09 13:09:08 +00:00
|
|
|
}
|
2024-02-10 12:03:28 +00:00
|
|
|
} else {
|
|
|
|
p << "\"n:" << JSON::Writer::encode_string(QPDF_Name::normalizeName(iter.first))
|
|
|
|
<< "\": ";
|
2024-02-09 13:09:08 +00:00
|
|
|
}
|
|
|
|
iter.second.writeJSON(json_version, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.writeEnd('}');
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|