2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_String.hh>
|
|
|
|
|
2024-02-09 13:09:08 +00:00
|
|
|
#include <qpdf/JSON_writer.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2008-11-23 18:49:13 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// DO NOT USE ctype -- it is locale dependent for some things, and it's not worth the risk of
|
|
|
|
// including it in case it may accidentally be used.
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
static bool
|
|
|
|
is_iso_latin1_printable(char ch)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
return (((ch >= 32) && (ch <= 126)) || (static_cast<unsigned char>(ch) >= 160));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDF_String::QPDF_String(std::string const& val) :
|
2022-08-02 21:57:33 +00:00
|
|
|
QPDFValue(::ot_string, "string"),
|
2008-04-29 12:55:25 +00:00
|
|
|
val(val)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 16:45:04 +00:00
|
|
|
QPDF_String::create(std::string const& val)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_String(val));
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 16:45:04 +00:00
|
|
|
QPDF_String::create_utf16(std::string const& utf8_val)
|
2019-01-05 17:54:41 +00:00
|
|
|
{
|
2021-01-23 22:58:23 +00:00
|
|
|
std::string result;
|
2022-02-16 00:22:35 +00:00
|
|
|
if (!QUtil::utf8_to_pdf_doc(utf8_val, result, '?')) {
|
2021-01-23 22:58:23 +00:00
|
|
|
result = QUtil::utf8_to_utf16(utf8_val);
|
|
|
|
}
|
2022-06-16 16:45:04 +00:00
|
|
|
return do_create(new QPDF_String(result));
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-11-14 17:54:12 +00:00
|
|
|
QPDF_String::copy(bool shallow)
|
2022-06-16 16:45:04 +00:00
|
|
|
{
|
|
|
|
return create(val);
|
2018-06-21 18:03:45 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_String::unparse()
|
|
|
|
{
|
|
|
|
return unparse(false);
|
|
|
|
}
|
|
|
|
|
2024-02-09 13:09:08 +00:00
|
|
|
void
|
|
|
|
QPDF_String::writeJSON(int json_version, JSON::Writer& p)
|
|
|
|
{
|
|
|
|
auto candidate = getUTF8Val();
|
|
|
|
if (json_version == 1) {
|
|
|
|
|
|
|
|
p << "\"" << JSON::Writer::encode_string(candidate) << "\"";
|
|
|
|
} else {
|
|
|
|
// See if we can unambiguously represent as Unicode.
|
|
|
|
if (QUtil::is_utf16(this->val) || QUtil::is_explicit_utf8(this->val)) {
|
|
|
|
p << "\"u:" << JSON::Writer::encode_string(candidate) <<"\"";
|
|
|
|
return;
|
|
|
|
} else if (!useHexString()) {
|
|
|
|
std::string test;
|
|
|
|
if (QUtil::utf8_to_pdf_doc(candidate, test, '?') && (test == this->val)) {
|
|
|
|
// This is a PDF-doc string that can be losslessly encoded as Unicode.
|
|
|
|
p << "\"u:" << JSON::Writer::encode_string(candidate) <<"\"";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p << "\"b:" << QUtil::hex_encode(val) <<"\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 12:20:09 +00:00
|
|
|
bool
|
|
|
|
QPDF_String::useHexString() const
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-05-07 12:20:09 +00:00
|
|
|
// Heuristic: use the hexadecimal representation of a string if there are any non-printable (in
|
|
|
|
// PDF Doc encoding) characters or if too large of a proportion of the string consists of
|
|
|
|
// non-ASCII characters.
|
|
|
|
unsigned int non_ascii = 0;
|
2022-09-23 17:56:07 +00:00
|
|
|
for (auto const ch: this->val) {
|
|
|
|
if (ch > 126) {
|
|
|
|
++non_ascii;
|
|
|
|
} else if (ch >= 32) {
|
|
|
|
continue;
|
|
|
|
} else if (ch < 0 || ch >= 24) {
|
2022-05-07 12:20:09 +00:00
|
|
|
++non_ascii;
|
2022-09-23 17:56:07 +00:00
|
|
|
} else if (!(ch == '\n' || ch == '\r' || ch == '\t' || ch == '\b' || ch == '\f')) {
|
|
|
|
return true;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-09-23 17:56:07 +00:00
|
|
|
return 5 * non_ascii > val.length();
|
2022-05-07 12:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF_String::unparse(bool force_binary)
|
|
|
|
{
|
|
|
|
bool use_hexstring = force_binary || useHexString();
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string result;
|
|
|
|
if (use_hexstring) {
|
2022-09-21 19:21:17 +00:00
|
|
|
static auto constexpr hexchars = "0123456789abcdef";
|
|
|
|
result.reserve(2 * this->val.length() + 2);
|
|
|
|
result += '<';
|
|
|
|
for (const char c: this->val) {
|
|
|
|
result += hexchars[static_cast<unsigned char>(c) >> 4];
|
|
|
|
result += hexchars[c & 0x0f];
|
|
|
|
}
|
|
|
|
result += '>';
|
2008-04-29 12:55:25 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
result += "(";
|
|
|
|
for (unsigned int i = 0; i < this->val.length(); ++i) {
|
|
|
|
char ch = this->val.at(i);
|
|
|
|
switch (ch) {
|
|
|
|
case '\n':
|
|
|
|
result += "\\n";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
result += "\\r";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\t':
|
|
|
|
result += "\\t";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\b':
|
|
|
|
result += "\\b";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\f':
|
|
|
|
result += "\\f";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
result += "\\(";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
result += "\\)";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
result += "\\\\";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (is_iso_latin1_printable(ch)) {
|
|
|
|
result += this->val.at(i);
|
|
|
|
} else {
|
|
|
|
result += "\\" +
|
|
|
|
QUtil::int_to_string_base(
|
2013-02-28 21:20:45 +00:00
|
|
|
static_cast<int>(static_cast<unsigned char>(ch)), 8, 3);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += ")";
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF_String::getUTF8Val() const
|
|
|
|
{
|
2019-01-13 13:00:14 +00:00
|
|
|
if (QUtil::is_utf16(this->val)) {
|
|
|
|
return QUtil::utf16_to_utf8(this->val);
|
2022-04-23 20:39:27 +00:00
|
|
|
} else if (QUtil::is_explicit_utf8(this->val)) {
|
2022-02-22 13:04:11 +00:00
|
|
|
// PDF 2.0 allows UTF-8 strings when explicitly prefixed with the three-byte representation
|
2022-04-23 20:39:27 +00:00
|
|
|
// of U+FEFF.
|
2022-02-22 13:04:11 +00:00
|
|
|
return this->val.substr(3);
|
2008-04-29 12:55:25 +00:00
|
|
|
} else {
|
2019-01-13 13:00:14 +00:00
|
|
|
return QUtil::pdf_doc_to_utf8(this->val);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|