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-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 16:45:04 +00:00
|
|
|
QPDF_Name::create(std::string const& name)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Name(name));
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-11-14 17:54:12 +00:00
|
|
|
QPDF_Name::copy(bool shallow)
|
2022-06-16 16:45:04 +00:00
|
|
|
{
|
|
|
|
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 += "#";
|
2023-01-30 15:56:29 +00:00
|
|
|
} else if (
|
|
|
|
ch < 33 || 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-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
|
|
|
}
|