2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QPDF_Name.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDF_Null.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
QPDF_Dictionary::QPDF_Dictionary(
|
|
|
|
std::map<std::string, QPDFObjectHandle> const& items) :
|
|
|
|
items(items)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-06-06 13:32:08 +00:00
|
|
|
void
|
|
|
|
QPDF_Dictionary::releaseResolved()
|
|
|
|
{
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& iter: this->items) {
|
|
|
|
QPDFObjectHandle::ReleaseResolver::releaseResolved(iter.second);
|
2010-06-06 13:32:08 +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()) {
|
|
|
|
j.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
QPDF_Name::normalizeName(iter.first),
|
|
|
|
iter.second.getJSON(json_version));
|
2022-04-24 14:08:32 +00:00
|
|
|
}
|
2018-12-17 22:40:29 +00:00
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:57:07 +00:00
|
|
|
QPDFObject::object_type_e
|
|
|
|
QPDF_Dictionary::getTypeCode() const
|
|
|
|
{
|
|
|
|
return QPDFObject::ot_dictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const*
|
|
|
|
QPDF_Dictionary::getTypeName() const
|
|
|
|
{
|
|
|
|
return "dictionary";
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:25:27 +00:00
|
|
|
void
|
|
|
|
QPDF_Dictionary::setDescription(QPDF* qpdf, std::string const& description)
|
|
|
|
{
|
|
|
|
this->QPDFObject::setDescription(qpdf, description);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
QPDF* qpdf = 0;
|
|
|
|
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);
|
|
|
|
}
|