mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:07:34 +00:00
LibPDF: Make Object::to_deprecated_string() look more like PDF source
- No , between array or dict elements - `stream` goes in front of stream data, _after_ the stream dict Also, print string contents as ASCII if the string data is mostly ASCII.
This commit is contained in:
parent
cf47e130ae
commit
b4c5a7d1a0
1 changed files with 35 additions and 17 deletions
|
@ -85,7 +85,7 @@ DeprecatedString ArrayObject::to_deprecated_string(int indent) const
|
||||||
|
|
||||||
for (auto& element : elements()) {
|
for (auto& element : elements()) {
|
||||||
if (!first)
|
if (!first)
|
||||||
builder.append(",\n"sv);
|
builder.append("\n"sv);
|
||||||
first = false;
|
first = false;
|
||||||
append_indent(builder, indent + 1);
|
append_indent(builder, indent + 1);
|
||||||
builder.appendff("{}", element.to_deprecated_string(indent));
|
builder.appendff("{}", element.to_deprecated_string(indent));
|
||||||
|
@ -100,12 +100,13 @@ DeprecatedString ArrayObject::to_deprecated_string(int indent) const
|
||||||
DeprecatedString DictObject::to_deprecated_string(int indent) const
|
DeprecatedString DictObject::to_deprecated_string(int indent) const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
append_indent(builder, indent);
|
||||||
builder.append("<<\n"sv);
|
builder.append("<<\n"sv);
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
||||||
for (auto& [key, value] : map()) {
|
for (auto& [key, value] : map()) {
|
||||||
if (!first)
|
if (!first)
|
||||||
builder.append(",\n"sv);
|
builder.append("\n"sv);
|
||||||
first = false;
|
first = false;
|
||||||
append_indent(builder, indent + 1);
|
append_indent(builder, indent + 1);
|
||||||
builder.appendff("/{} ", key);
|
builder.appendff("/{} ", key);
|
||||||
|
@ -121,25 +122,42 @@ DeprecatedString DictObject::to_deprecated_string(int indent) const
|
||||||
DeprecatedString StreamObject::to_deprecated_string(int indent) const
|
DeprecatedString StreamObject::to_deprecated_string(int indent) const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
builder.appendff("{}\n", dict()->to_deprecated_string(indent));
|
||||||
builder.append("stream\n"sv);
|
builder.append("stream\n"sv);
|
||||||
append_indent(builder, indent);
|
|
||||||
builder.appendff("{}\n", dict()->to_deprecated_string(indent + 1));
|
|
||||||
append_indent(builder, indent + 1);
|
|
||||||
|
|
||||||
auto string = encode_hex(bytes());
|
size_t ascii_count = 0;
|
||||||
while (true) {
|
for (auto c : bytes()) {
|
||||||
if (string.length() > 60) {
|
if (c < 128)
|
||||||
builder.appendff("{}\n", string.substring(0, 60));
|
++ascii_count;
|
||||||
append_indent(builder, indent);
|
}
|
||||||
string = string.substring(60);
|
|
||||||
continue;
|
size_t percentage_ascii = 100;
|
||||||
}
|
if (bytes().size())
|
||||||
|
percentage_ascii = ascii_count * 100 / bytes().size();
|
||||||
builder.appendff("{}\n", string);
|
bool is_mostly_text = percentage_ascii > 95;
|
||||||
break;
|
|
||||||
|
if (is_mostly_text) {
|
||||||
|
for (auto c : bytes()) {
|
||||||
|
if (c < 128)
|
||||||
|
builder.append(c);
|
||||||
|
else
|
||||||
|
builder.appendff("\\{:03o}", c);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto string = encode_hex(bytes());
|
||||||
|
while (true) {
|
||||||
|
if (string.length() > 60) {
|
||||||
|
builder.appendff("{}\n", string.substring(0, 60));
|
||||||
|
append_indent(builder, indent);
|
||||||
|
string = string.substring(60);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.appendff("{}\n", string);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
append_indent(builder, indent);
|
|
||||||
builder.append("endstream"sv);
|
builder.append("endstream"sv);
|
||||||
return builder.to_deprecated_string();
|
return builder.to_deprecated_string();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue