From d6a9b41bacc3be065111e2244eeab96e78c16fbf Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Mon, 10 May 2021 10:44:22 -0700 Subject: [PATCH] LibPDF: Parse page crop box and user units --- Userland/Libraries/LibPDF/Document.cpp | 20 ++++++++++++++++++-- Userland/Libraries/LibPDF/Document.h | 13 ++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibPDF/Document.cpp b/Userland/Libraries/LibPDF/Document.cpp index 7d7d01c79a6..c95966c8671 100644 --- a/Userland/Libraries/LibPDF/Document.cpp +++ b/Userland/Libraries/LibPDF/Document.cpp @@ -63,6 +63,8 @@ Page Document::get_page(u32 index) auto raw_page_object = resolve_to(get_or_load_value(page_object_index)); auto resources = raw_page_object->get_dict(this, "Resources"); + auto contents = raw_page_object->get_object(this, "Contents"); + auto media_box_array = raw_page_object->get_array(this, "MediaBox"); auto media_box = Rectangle { media_box_array->at(0).to_float(), @@ -70,9 +72,23 @@ Page Document::get_page(u32 index) media_box_array->at(2).to_float(), media_box_array->at(3).to_float(), }; - auto contents = raw_page_object->get_object(this, "Contents"); - Page page { resources, media_box, contents }; + auto crop_box = media_box; + if (raw_page_object->contains("CropBox")) { + auto crop_box_array = raw_page_object->get_array(this, "CropBox"); + crop_box = Rectangle { + crop_box_array->at(0).to_float(), + crop_box_array->at(1).to_float(), + crop_box_array->at(2).to_float(), + crop_box_array->at(3).to_float(), + }; + } + + float user_unit = 1.0f; + if (raw_page_object->contains("UserUnit")) + user_unit = raw_page_object->get_value("UserUnit").to_float(); + + Page page { move(resources), move(contents), media_box, crop_box, user_unit }; m_pages.set(index, page); return page; } diff --git a/Userland/Libraries/LibPDF/Document.h b/Userland/Libraries/LibPDF/Document.h index f5210250879..5adc24a3ecb 100644 --- a/Userland/Libraries/LibPDF/Document.h +++ b/Userland/Libraries/LibPDF/Document.h @@ -24,8 +24,10 @@ struct Rectangle { struct Page { NonnullRefPtr resources; - Rectangle media_box; NonnullRefPtr contents; + Rectangle media_box; + Rectangle crop_box; + float user_unit; }; class Document final : public RefCounted { @@ -105,8 +107,13 @@ template<> struct Formatter : Formatter { void format(FormatBuilder& builder, const PDF::Page& page) { - constexpr auto fmt_string = "Page {{\n resources={}\n contents={}\n media_box={}\n}}"; - auto str = String::formatted(fmt_string, page.resources->to_string(1), page.contents->to_string(1), page.media_box); + constexpr auto fmt_string = "Page {{\n resources={}\n contents={}\n media_box={}\n crop_box={}\n user_unit={}\n}}"; + auto str = String::formatted(fmt_string, + page.resources->to_string(1), + page.contents->to_string(1), + page.media_box, + page.crop_box, + page.user_unit); Formatter::format(builder, str); } };