1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:47:44 +00:00

LibPDF: Make pdf --dump-contents handle \r line endings better

Previously, all page contents ended up overprinting a single line
over and over for PDFs that used only `\r` as line ending.

This is for example useful for 0000364.pdf.
This commit is contained in:
Nico Weber 2024-01-10 11:23:06 -05:00 committed by Andrew Kaster
parent 7fcce6b6c4
commit b34509edd2

View file

@ -137,11 +137,17 @@ ByteString StreamObject::to_byte_string(int indent) const
bool is_mostly_text = percentage_ascii > 95;
if (is_mostly_text) {
for (auto c : bytes()) {
if (c < 128)
builder.append(c);
else
for (size_t i = 0; i < bytes().size(); ++i) {
auto c = bytes()[i];
if (c < 128) {
bool next_is_newline = i + 1 < bytes().size() && bytes()[i + 1] == '\n';
if (c == '\r' && !next_is_newline)
builder.append('\n');
else
builder.append(c);
} else {
builder.appendff("\\{:03o}", c);
}
}
} else {
auto string = encode_hex(bytes());