2023-07-12 09:27:26 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibPDF/Document.h>
|
|
|
|
|
#include <LibPDF/ObjectDerivatives.h>
|
|
|
|
|
#include <LibPDF/Page.h>
|
|
|
|
|
|
|
|
|
|
namespace PDF {
|
|
|
|
|
|
|
|
|
|
PDFErrorOr<ByteBuffer> Page::page_contents(Document& document) const
|
|
|
|
|
{
|
2023-07-12 09:30:08 -04:00
|
|
|
// Table 3.27 Entries in a page object on Contents:
|
|
|
|
|
// "If this entry is absent, the page is empty. [...]"
|
2023-07-12 09:27:26 -04:00
|
|
|
if (contents.is_null())
|
|
|
|
|
return ByteBuffer {};
|
|
|
|
|
|
2023-07-12 09:30:08 -04:00
|
|
|
// "The value may be either a single stream or an array of streams. If the value
|
|
|
|
|
// is an array, the effect is as if all the streams in the array were concatenated,
|
2023-10-23 09:28:41 -07:00
|
|
|
// in order, to form a single stream. The division between streams may occur only at
|
|
|
|
|
// the boundaries between lexical tokens"
|
2023-07-12 09:30:08 -04:00
|
|
|
if (contents->is<StreamObject>())
|
|
|
|
|
return TRY(ByteBuffer::copy(contents->cast<StreamObject>()->bytes()));
|
|
|
|
|
|
2023-10-23 09:28:41 -07:00
|
|
|
// If one stream ends with (say) a `Q` and the next starts with `q`, that should be
|
|
|
|
|
// two distinct tokens. Insert spaces between stream contents to ensure that.
|
2023-07-12 09:27:26 -04:00
|
|
|
ByteBuffer byte_buffer;
|
2023-10-23 09:28:41 -07:00
|
|
|
for (auto& ref : *contents->cast<ArrayObject>()) {
|
2023-07-12 09:30:08 -04:00
|
|
|
TRY(byte_buffer.try_append(TRY(document.resolve_to<StreamObject>(ref))->bytes()));
|
2023-10-23 09:28:41 -07:00
|
|
|
TRY(byte_buffer.try_append(' '));
|
|
|
|
|
}
|
2023-07-12 09:27:26 -04:00
|
|
|
return byte_buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|