2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Name.hh>
|
|
|
|
|
2013-01-25 13:59:55 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2008-05-04 16:02:53 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF_Name::QPDF_Name(std::string const& name) :
|
2022-08-02 21:57:33 +00:00
|
|
|
QPDFValue(::ot_name, "name"),
|
2008-04-29 12:55:25 +00:00
|
|
|
name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-06-16 16:45:04 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
|
|
|
QPDF_Name::create(std::string const& name)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Name(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<QPDFObject>
|
|
|
|
QPDF_Name::shallowCopy()
|
|
|
|
{
|
|
|
|
return create(name);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Name::normalizeName(std::string const& name)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (name.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return name;
|
2013-10-05 09:52:42 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string result;
|
2013-10-05 23:42:39 +00:00
|
|
|
result += name.at(0);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 1; i < name.length(); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
char ch = name.at(i);
|
|
|
|
// Don't use locale/ctype here; follow PDF spec guidelines.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (ch == '\0') {
|
2019-08-19 01:26:19 +00:00
|
|
|
// QPDFTokenizer embeds a null character to encode an
|
|
|
|
// invalid #.
|
|
|
|
result += "#";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126)) {
|
2013-01-25 13:59:55 +00:00
|
|
|
result += "#" + QUtil::hex_encode(std::string(&ch, 1));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
result += ch;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF_Name::unparse()
|
|
|
|
{
|
|
|
|
return normalizeName(this->name);
|
|
|
|
}
|
|
|
|
|
2018-12-17 22:40:29 +00:00
|
|
|
JSON
|
2022-05-07 11:53:45 +00:00
|
|
|
QPDF_Name::getJSON(int json_version)
|
2018-12-17 22:40:29 +00:00
|
|
|
{
|
2022-05-17 23:18:02 +00:00
|
|
|
if (json_version == 1) {
|
|
|
|
return JSON::makeString(normalizeName(this->name));
|
|
|
|
} else {
|
|
|
|
return JSON::makeString(this->name);
|
|
|
|
}
|
2018-12-17 22:40:29 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Name::getName() const
|
|
|
|
{
|
|
|
|
return this->name;
|
|
|
|
}
|