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