2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_String.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QUtil.hh>
|
2008-11-23 18:49:13 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
|
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.
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// See above about ctype.
|
2019-06-21 03:35:23 +00:00
|
|
|
static bool is_ascii_printable(char ch)
|
2009-10-17 17:31:52 +00:00
|
|
|
{
|
|
|
|
return ((ch >= 32) && (ch <= 126));
|
|
|
|
}
|
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) :
|
|
|
|
val(val)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDF_String::~QPDF_String()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-05 17:54:41 +00:00
|
|
|
QPDF_String*
|
|
|
|
QPDF_String::new_utf16(std::string const& utf8_val)
|
|
|
|
{
|
2021-01-23 22:58:23 +00:00
|
|
|
std::string result;
|
|
|
|
if (! (QUtil::utf8_to_ascii(utf8_val, result, '?') ||
|
|
|
|
QUtil::utf8_to_pdf_doc(utf8_val, result, '?')))
|
|
|
|
{
|
|
|
|
result = QUtil::utf8_to_utf16(utf8_val);
|
|
|
|
}
|
|
|
|
return new QPDF_String(result);
|
2018-06-21 18:03:45 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_String::unparse()
|
|
|
|
{
|
|
|
|
return unparse(false);
|
|
|
|
}
|
|
|
|
|
2018-12-17 22:40:29 +00:00
|
|
|
JSON
|
|
|
|
QPDF_String::getJSON()
|
|
|
|
{
|
|
|
|
return JSON::makeString(getUTF8Val());
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:57:07 +00:00
|
|
|
QPDFObject::object_type_e
|
|
|
|
QPDF_String::getTypeCode() const
|
|
|
|
{
|
|
|
|
return QPDFObject::ot_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const*
|
|
|
|
QPDF_String::getTypeName() const
|
|
|
|
{
|
|
|
|
return "string";
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_String::unparse(bool force_binary)
|
|
|
|
{
|
|
|
|
bool use_hexstring = force_binary;
|
|
|
|
if (! use_hexstring)
|
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
unsigned int nonprintable = 0;
|
|
|
|
int consecutive_printable = 0;
|
|
|
|
for (unsigned int i = 0; i < this->val.length(); ++i)
|
|
|
|
{
|
|
|
|
char ch = this->val.at(i);
|
|
|
|
// Note: do not use locale to determine printability. The
|
|
|
|
// PDF specification accepts arbitrary binary data. Some
|
|
|
|
// locales imply multibyte characters. We'll consider
|
|
|
|
// something printable if it is printable in 7-bit ASCII.
|
|
|
|
// We'll code this manually rather than being rude and
|
|
|
|
// setting locale.
|
|
|
|
if ((ch == 0) || (! (is_ascii_printable(ch) ||
|
|
|
|
strchr("\n\r\t\b\f", ch))))
|
|
|
|
{
|
|
|
|
++nonprintable;
|
|
|
|
consecutive_printable = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (++consecutive_printable > 5)
|
|
|
|
{
|
|
|
|
// If there are more than 5 consecutive printable
|
|
|
|
// characters, I want to see them as such.
|
|
|
|
nonprintable = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use hex notation if more than 20% of the characters are not
|
|
|
|
// printable in plain ASCII.
|
|
|
|
if (5 * nonprintable > val.length())
|
|
|
|
{
|
|
|
|
use_hexstring = true;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
std::string result;
|
|
|
|
if (use_hexstring)
|
|
|
|
{
|
2022-02-08 14:18:08 +00:00
|
|
|
result += "<" + QUtil::hex_encode(this->val) + ">";
|
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::getVal() const
|
|
|
|
{
|
|
|
|
return this->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF_String::getUTF8Val() const
|
|
|
|
{
|
2019-01-13 13:00:14 +00:00
|
|
|
if (QUtil::is_utf16(this->val))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-01-13 13:00:14 +00:00
|
|
|
return QUtil::utf16_to_utf8(this->val);
|
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
|
|
|
}
|
|
|
|
}
|