summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPDF/Object.cpp
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2021-05-08 14:57:49 -0700
committerAndreas Kling <kling@serenityos.org>2021-05-10 10:32:39 +0200
commit8c745ad0d9198db1cd078dff573353457294a840 (patch)
tree05cd0e85a7b49e3e6d57bdd71ae7f97c8bd823be /Userland/Libraries/LibPDF/Object.cpp
parent72f693e9ed6b106e0760a3b1869f92d3267ddd41 (diff)
downloadserenity-8c745ad0d9198db1cd078dff573353457294a840.zip
LibPDF: Parse page structures
This commit introduces the ability to parse the document catalog dict, as well as the page tree and individual pages. Pages obviously aren't fully parsed, as we won't care about most of the fields until we start actually rendering PDFs. One of the primary benefits of the PDF format is laziness. PDFs are not meant to be parsed all at once, and the same is true for pages. When a Document is constructed, it builds a map of page number to object index, but it does not fetch and parse any of the pages. A page is only parsed when a caller requests that particular page (and is cached going forwards). Additionally, this commit also adds an object_cast function which logs bad casts if DEBUG_PDF is set. Additionally, utility functions were added to ArrayObject and DictObject to get all types of objects from the collections to avoid having to manually cast.
Diffstat (limited to 'Userland/Libraries/LibPDF/Object.cpp')
-rw-r--r--Userland/Libraries/LibPDF/Object.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Libraries/LibPDF/Object.cpp b/Userland/Libraries/LibPDF/Object.cpp
index 4fca01a497..3cd861cb61 100644
--- a/Userland/Libraries/LibPDF/Object.cpp
+++ b/Userland/Libraries/LibPDF/Object.cpp
@@ -5,10 +5,34 @@
*/
#include <AK/Hex.h>
+#include <LibPDF/Document.h>
#include <LibPDF/Object.h>
namespace PDF {
+NonnullRefPtr<Object> ArrayObject::get_object_at(Document* document, size_t index) const
+{
+ return document->resolve_to<Object>(m_elements[index]);
+}
+
+NonnullRefPtr<Object> DictObject::get_object(Document* document, const FlyString& key) const
+{
+ return document->resolve_to<Object>(get_value(key));
+}
+
+#define DEFINE_ACCESSORS(class_name, snake_name) \
+ NonnullRefPtr<class_name> ArrayObject::get_##snake_name##_at(Document* document, size_t index) const \
+ { \
+ return document->resolve_to<class_name>(m_elements[index]); \
+ } \
+ \
+ NonnullRefPtr<class_name> DictObject::get_##snake_name(Document* document, const FlyString& key) const \
+ { \
+ return document->resolve_to<class_name>(get(key).value()); \
+ }
+ENUMERATE_DIRECT_OBJECT_TYPES(DEFINE_ACCESSORS)
+#undef DEFINE_INDEXER
+
static void append_indent(StringBuilder& builder, int indent)
{
for (int i = 0; i < indent; i++)