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) :
|
|
|
|
name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return JSON::makeString(normalizeName(this->name));
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:57:07 +00:00
|
|
|
QPDFObject::object_type_e
|
|
|
|
QPDF_Name::getTypeCode() const
|
|
|
|
{
|
|
|
|
return QPDFObject::ot_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const*
|
|
|
|
QPDF_Name::getTypeName() const
|
|
|
|
{
|
|
|
|
return "name";
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Name::getName() const
|
|
|
|
{
|
|
|
|
return this->name;
|
|
|
|
}
|