Page.cpp 1.3 KB

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