2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QPDF_Name.hh>
|
|
|
|
|
|
|
|
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-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-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-30 13:43:07 +00:00
|
|
|
for (auto& iter: this->items) {
|
2022-04-24 14:08:32 +00:00
|
|
|
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-30 13:43:07 +00:00
|
|
|
for (auto& iter: this->items) {
|
2022-04-24 14:08:32 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return ((this->items.count(key) > 0) && (!this->items[key].isNull()));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2018-02-16 22:25:27 +00:00
|
|
|
QPDFObjectHandle null = QPDFObjectHandle::newNull();
|
2022-07-26 11:37:50 +00:00
|
|
|
QPDF* qpdf = nullptr;
|
2018-02-16 22:25:27 +00:00
|
|
|
std::string description;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (getDescription(qpdf, description)) {
|
2018-02-16 22:25:27 +00:00
|
|
|
null.setObjectDescription(
|
|
|
|
qpdf, description + " -> dictionary key " + key);
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
return null;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::string>
|
|
|
|
QPDF_Dictionary::getKeys()
|
|
|
|
{
|
|
|
|
std::set<std::string> result;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto& iter: this->items) {
|
2022-04-24 14:08:32 +00:00
|
|
|
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
|
|
|
{
|
2022-04-24 13:31:32 +00:00
|
|
|
if (value.isNull()) {
|
|
|
|
// The PDF spec doesn't distinguish between keys with null
|
|
|
|
// values and missing keys.
|
|
|
|
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);
|
|
|
|
}
|