ContentNormalizer.cc
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <qpdf/ContentNormalizer.hh>
#include <qpdf/QPDFObjectHandle_private.hh>
#include <qpdf/QUtil.hh>
using namespace qpdf;
ContentNormalizer::ContentNormalizer() :
any_bad_tokens(false),
last_token_was_bad(false)
{
}
void
ContentNormalizer::handleToken(QPDFTokenizer::Token const& token)
{
QPDFTokenizer::token_type_e token_type = token.getType();
if (token_type == QPDFTokenizer::tt_bad) {
this->any_bad_tokens = true;
this->last_token_was_bad = true;
} else if (token_type != QPDFTokenizer::tt_eof) {
this->last_token_was_bad = false;
}
switch (token_type) {
case QPDFTokenizer::tt_space:
{
std::string const& value = token.getRawValue();
auto size = value.size();
size_t pos = 0;
auto r_pos = value.find('\r');
while (r_pos != std::string::npos) {
if (pos != r_pos) {
write(&value[pos], r_pos - pos);
}
if (++r_pos >= size) {
write("\n");
return;
}
if (value[r_pos] != '\n') {
write("\n");
}
pos = r_pos;
r_pos = value.find('\r', pos);
}
if (pos < size) {
write(&value[pos], size - pos);
}
}
return;
case QPDFTokenizer::tt_string:
// Replacing string and name tokens in this way normalizes their representation as this will
// automatically handle quoting of unprintable characters, etc.
write(QPDFObjectHandle::newString(token.getValue()).unparse());
break;
case QPDFTokenizer::tt_name:
write(Name::normalize(token.getValue()));
break;
default:
writeToken(token);
return;
}
// tt_string or tt_name
std::string const& value = token.getRawValue();
if (value.find('\r') != std::string::npos || value.find('\n') != std::string::npos) {
write("\n");
}
}
bool
ContentNormalizer::anyBadTokens() const
{
return this->any_bad_tokens;
}
bool
ContentNormalizer::lastTokenWasBad() const
{
return this->last_token_was_bad;
}