2021-04-30 18:33:13 -07:00
|
|
|
/*
|
2022-03-05 17:30:55 -07:00
|
|
|
* Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
|
2021-04-30 18:33:13 -07:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-05-08 14:57:49 -07:00
|
|
|
#include <AK/Format.h>
|
|
|
|
|
#include <AK/HashMap.h>
|
2021-04-30 18:33:13 -07:00
|
|
|
#include <AK/RefCounted.h>
|
2021-11-28 11:52:48 +01:00
|
|
|
#include <AK/Weakable.h>
|
2021-05-23 16:37:33 -07:00
|
|
|
#include <LibGfx/Color.h>
|
2022-08-15 11:45:24 +02:00
|
|
|
#include <LibPDF/DocumentParser.h>
|
2022-03-20 14:24:23 -07:00
|
|
|
#include <LibPDF/Encryption.h>
|
2022-03-05 17:30:55 -07:00
|
|
|
#include <LibPDF/Error.h>
|
2021-09-17 02:28:52 +02:00
|
|
|
#include <LibPDF/ObjectDerivatives.h>
|
2023-07-12 09:13:12 -04:00
|
|
|
#include <LibPDF/Page.h>
|
2021-04-30 18:33:13 -07:00
|
|
|
|
|
|
|
|
namespace PDF {
|
|
|
|
|
|
2021-05-23 16:37:33 -07:00
|
|
|
struct Destination {
|
|
|
|
|
enum class Type {
|
|
|
|
|
XYZ,
|
|
|
|
|
Fit,
|
|
|
|
|
FitH,
|
|
|
|
|
FitV,
|
|
|
|
|
FitR,
|
|
|
|
|
FitB,
|
|
|
|
|
FitBH,
|
|
|
|
|
FitBV,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Type type;
|
2022-12-17 13:32:35 +08:00
|
|
|
Optional<u32> page;
|
2023-01-05 23:49:40 +08:00
|
|
|
Vector<Optional<float>> parameters;
|
2021-05-23 16:37:33 -07:00
|
|
|
};
|
|
|
|
|
|
2023-12-02 12:58:39 +02:00
|
|
|
struct OutlineItem final : public RefCounted<OutlineItem>
|
|
|
|
|
, public Weakable<OutlineItem> {
|
|
|
|
|
WeakPtr<OutlineItem> parent;
|
2023-03-06 14:17:01 +01:00
|
|
|
Vector<NonnullRefPtr<OutlineItem>> children;
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString title; // Already converted to UTF-8.
|
2021-05-23 16:37:33 -07:00
|
|
|
i32 count { 0 };
|
|
|
|
|
Destination dest;
|
|
|
|
|
Gfx::Color color { Color::NamedColor::Black }; // 'C' in the PDF spec
|
|
|
|
|
bool italic { false }; // bit 0 of 'F' in the PDF spec
|
|
|
|
|
bool bold { false }; // bit 0 of 'F' in the PDF spec
|
|
|
|
|
|
|
|
|
|
OutlineItem() = default;
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString to_byte_string(int indent) const;
|
2021-05-23 16:37:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct OutlineDict final : public RefCounted<OutlineDict> {
|
2023-03-06 14:17:01 +01:00
|
|
|
Vector<NonnullRefPtr<OutlineItem>> children;
|
2021-05-23 16:37:33 -07:00
|
|
|
u32 count { 0 };
|
|
|
|
|
|
|
|
|
|
OutlineDict() = default;
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-10 08:59:03 -04:00
|
|
|
class InfoDict {
|
|
|
|
|
public:
|
|
|
|
|
InfoDict(Document* document, NonnullRefPtr<DictObject> dict)
|
|
|
|
|
: m_document(document)
|
|
|
|
|
, m_info_dict(move(dict))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 21:04:31 -05:00
|
|
|
// These all return strings that are already converted to UTF-8.
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
PDFErrorOr<Optional<ByteString>> title() const;
|
|
|
|
|
PDFErrorOr<Optional<ByteString>> author() const;
|
|
|
|
|
PDFErrorOr<Optional<ByteString>> subject() const;
|
|
|
|
|
PDFErrorOr<Optional<ByteString>> keywords() const;
|
2023-07-10 08:59:03 -04:00
|
|
|
|
|
|
|
|
// Name of the program that created the original, non-PDF file.
|
2023-12-16 17:49:34 +03:30
|
|
|
PDFErrorOr<Optional<ByteString>> creator() const;
|
2023-07-10 08:59:03 -04:00
|
|
|
|
|
|
|
|
// Name of the program that converted the file to PDF.
|
2023-12-16 17:49:34 +03:30
|
|
|
PDFErrorOr<Optional<ByteString>> producer() const;
|
2023-07-10 08:59:03 -04:00
|
|
|
|
|
|
|
|
// FIXME: Provide some helper for parsing the date strings returned by these two methods.
|
2023-12-16 17:49:34 +03:30
|
|
|
PDFErrorOr<Optional<ByteString>> creation_date() const;
|
|
|
|
|
PDFErrorOr<Optional<ByteString>> modification_date() const;
|
2023-07-10 08:59:03 -04:00
|
|
|
|
|
|
|
|
private:
|
2023-12-16 17:49:34 +03:30
|
|
|
PDFErrorOr<Optional<ByteString>> get(DeprecatedFlyString const& name) const
|
2023-07-10 08:59:03 -04:00
|
|
|
{
|
|
|
|
|
if (!m_info_dict->contains(name))
|
|
|
|
|
return OptionalNone {};
|
|
|
|
|
return TRY(m_info_dict->get_string(m_document, name))->string();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
PDFErrorOr<Optional<ByteString>> get_text(DeprecatedFlyString const& name) const;
|
2023-11-20 21:04:31 -05:00
|
|
|
|
2023-07-10 08:59:03 -04:00
|
|
|
WeakPtr<Document> m_document;
|
|
|
|
|
NonnullRefPtr<DictObject> m_info_dict;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-28 11:52:48 +01:00
|
|
|
class Document final
|
|
|
|
|
: public RefCounted<Document>
|
|
|
|
|
, public Weakable<Document> {
|
2021-04-30 18:33:13 -07:00
|
|
|
public:
|
2023-11-20 21:04:31 -05:00
|
|
|
// Converts a text string (PDF 1.7 spec, 3.8.1. "String Types") to UTF-8.
|
2023-12-16 17:49:34 +03:30
|
|
|
static ByteString text_string_to_utf8(ByteString const&);
|
2023-11-20 21:04:31 -05:00
|
|
|
|
2022-03-05 17:30:55 -07:00
|
|
|
static PDFErrorOr<NonnullRefPtr<Document>> create(ReadonlyBytes bytes);
|
2021-04-30 18:33:13 -07:00
|
|
|
|
2022-03-20 14:24:23 -07:00
|
|
|
// If a security handler is present, it is the caller's responsibility to ensure
|
|
|
|
|
// this document is unencrypted before calling this function. The user does not
|
|
|
|
|
// need to handle the case where the user password is the empty string.
|
|
|
|
|
PDFErrorOr<void> initialize();
|
|
|
|
|
|
2023-07-11 12:37:13 -04:00
|
|
|
Version version() const { return m_version; }
|
|
|
|
|
|
2022-03-20 14:24:23 -07:00
|
|
|
ALWAYS_INLINE RefPtr<SecurityHandler> const& security_handler() const { return m_security_handler; }
|
|
|
|
|
|
2021-06-01 11:16:11 -07:00
|
|
|
ALWAYS_INLINE RefPtr<OutlineDict> const& outline() const { return m_outline; }
|
2021-04-30 18:33:13 -07:00
|
|
|
|
2022-03-20 14:24:23 -07:00
|
|
|
ALWAYS_INLINE RefPtr<DictObject> const& trailer() const { return m_trailer; }
|
|
|
|
|
|
2022-03-05 17:30:55 -07:00
|
|
|
[[nodiscard]] PDFErrorOr<Value> get_or_load_value(u32 index);
|
2021-05-08 14:57:49 -07:00
|
|
|
|
|
|
|
|
[[nodiscard]] u32 get_first_page_index() const;
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] u32 get_page_count() const;
|
|
|
|
|
|
2023-07-12 12:21:41 -04:00
|
|
|
PDFErrorOr<void> dump_page(u32 index);
|
2022-03-05 17:30:55 -07:00
|
|
|
[[nodiscard]] PDFErrorOr<Page> get_page(u32 index);
|
2021-05-08 14:57:49 -07:00
|
|
|
|
2021-04-30 18:33:13 -07:00
|
|
|
ALWAYS_INLINE Value get_value(u32 index) const
|
|
|
|
|
{
|
|
|
|
|
return m_values.get(index).value_or({});
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 14:57:49 -07:00
|
|
|
// Strips away the layer of indirection by turning indirect value
|
|
|
|
|
// refs into the value they reference, and indirect values into
|
|
|
|
|
// the value being wrapped.
|
2022-03-05 17:30:55 -07:00
|
|
|
PDFErrorOr<Value> resolve(Value const& value);
|
2021-05-08 14:57:49 -07:00
|
|
|
|
|
|
|
|
// Like resolve, but unwraps the Value into the given type. Accepts
|
|
|
|
|
// any object type, and the three primitive Value types.
|
|
|
|
|
template<IsValueType T>
|
2022-03-05 17:30:55 -07:00
|
|
|
PDFErrorOr<UnwrappedValueType<T>> resolve_to(Value const& value)
|
2021-05-10 11:50:15 -07:00
|
|
|
{
|
2023-01-05 23:55:25 +08:00
|
|
|
return cast_to<T>(TRY(resolve(value)));
|
2021-05-10 11:50:15 -07:00
|
|
|
}
|
2021-05-08 14:57:49 -07:00
|
|
|
|
2023-07-05 13:39:53 -04:00
|
|
|
/// Whether this Document is ready to resolve references, which is usually
|
2023-02-06 00:05:33 +08:00
|
|
|
/// true, except just before the XRef table is parsed (and while the linearization
|
|
|
|
|
/// dict is being read).
|
2023-07-05 13:39:53 -04:00
|
|
|
bool can_resolve_references() { return m_parser->can_resolve_references(); }
|
2023-02-06 00:05:33 +08:00
|
|
|
|
2023-07-10 08:59:03 -04:00
|
|
|
PDFErrorOr<Optional<InfoDict>> info_dict();
|
|
|
|
|
|
2023-07-23 22:45:39 -04:00
|
|
|
PDFErrorOr<Vector<DeprecatedFlyString>> read_filters(NonnullRefPtr<DictObject>);
|
|
|
|
|
|
2023-12-18 21:13:03 -05:00
|
|
|
PDFErrorOr<void> unfilter_stream(NonnullRefPtr<StreamObject> stream) { return m_parser->unfilter_stream(move(stream)); }
|
|
|
|
|
|
2021-04-30 18:33:13 -07:00
|
|
|
private:
|
2022-08-15 11:45:24 +02:00
|
|
|
explicit Document(NonnullRefPtr<DocumentParser> const& parser);
|
2021-05-24 13:57:16 -07:00
|
|
|
|
2021-05-02 18:53:07 -07:00
|
|
|
// FIXME: Currently, to improve performance, we don't load any pages at Document
|
|
|
|
|
// construction, rather we just load the page structure and populate
|
|
|
|
|
// m_page_object_indices. However, we can be even lazier and defer page tree node
|
|
|
|
|
// parsing, as good PDF writers will layout the page tree in a balanced tree to
|
|
|
|
|
// improve lookup time. This would reduce the initial overhead by not loading
|
|
|
|
|
// every page tree node of, say, a 1000+ page PDF file.
|
2022-03-05 17:30:55 -07:00
|
|
|
PDFErrorOr<void> build_page_tree();
|
|
|
|
|
PDFErrorOr<void> add_page_tree_node_to_page_tree(NonnullRefPtr<DictObject> const& page_tree);
|
2021-05-08 14:57:49 -07:00
|
|
|
|
2022-03-05 17:30:55 -07:00
|
|
|
PDFErrorOr<void> build_outline();
|
2022-12-17 13:32:35 +08:00
|
|
|
PDFErrorOr<NonnullRefPtr<OutlineItem>> build_outline_item(NonnullRefPtr<DictObject> const& outline_item_dict, HashMap<u32, u32> const&);
|
2023-03-06 14:17:01 +01:00
|
|
|
PDFErrorOr<Vector<NonnullRefPtr<OutlineItem>>> build_outline_item_chain(Value const& first_ref, HashMap<u32, u32> const&);
|
2021-05-23 16:37:33 -07:00
|
|
|
|
2022-12-17 13:32:35 +08:00
|
|
|
PDFErrorOr<Destination> create_destination_from_parameters(NonnullRefPtr<ArrayObject>, HashMap<u32, u32> const&);
|
2023-01-06 00:41:44 +08:00
|
|
|
PDFErrorOr<Destination> create_destination_from_dictionary_entry(NonnullRefPtr<Object> const& entry, HashMap<u32, u32> const& page_number_by_index_ref);
|
2023-10-16 21:18:01 -04:00
|
|
|
PDFErrorOr<Destination> create_destination_from_object(NonnullRefPtr<Object> const& dest_obj, HashMap<u32, u32> const& page_number_by_index_ref);
|
2022-03-05 18:25:33 -07:00
|
|
|
|
2023-03-24 22:07:03 +01:00
|
|
|
PDFErrorOr<Optional<NonnullRefPtr<Object>>> get_inheritable_object(DeprecatedFlyString const& name, NonnullRefPtr<DictObject>);
|
2023-03-24 22:15:17 +01:00
|
|
|
PDFErrorOr<Optional<Value>> get_inheritable_value(DeprecatedFlyString const& name, NonnullRefPtr<DictObject>);
|
2022-08-25 10:36:36 +02:00
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
PDFErrorOr<NonnullRefPtr<Object>> find_in_name_tree(NonnullRefPtr<DictObject> root, DeprecatedFlyString name);
|
|
|
|
|
PDFErrorOr<NonnullRefPtr<Object>> find_in_name_tree_nodes(NonnullRefPtr<ArrayObject> siblings, DeprecatedFlyString name);
|
|
|
|
|
PDFErrorOr<NonnullRefPtr<Object>> find_in_key_value_array(NonnullRefPtr<ArrayObject> key_value_array, DeprecatedFlyString name);
|
2023-01-06 00:33:24 +08:00
|
|
|
|
2022-08-15 11:45:24 +02:00
|
|
|
NonnullRefPtr<DocumentParser> m_parser;
|
2023-07-11 12:37:13 -04:00
|
|
|
Version m_version;
|
2021-05-08 14:57:49 -07:00
|
|
|
RefPtr<DictObject> m_catalog;
|
2022-03-20 14:24:23 -07:00
|
|
|
RefPtr<DictObject> m_trailer;
|
2021-05-08 14:57:49 -07:00
|
|
|
Vector<u32> m_page_object_indices;
|
|
|
|
|
HashMap<u32, Page> m_pages;
|
2021-04-30 18:33:13 -07:00
|
|
|
HashMap<u32, Value> m_values;
|
2021-05-23 16:37:33 -07:00
|
|
|
RefPtr<OutlineDict> m_outline;
|
2022-03-20 14:24:23 -07:00
|
|
|
RefPtr<SecurityHandler> m_security_handler;
|
2021-04-30 18:33:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2021-05-08 14:57:49 -07:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2021-05-23 16:37:33 -07:00
|
|
|
template<>
|
2022-07-11 20:23:24 +00:00
|
|
|
struct Formatter<PDF::Destination> : Formatter<FormatString> {
|
2021-11-16 01:15:21 +01:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, PDF::Destination const& destination)
|
2021-05-23 16:37:33 -07:00
|
|
|
{
|
2022-07-11 20:23:24 +00:00
|
|
|
StringView type_str;
|
2021-05-23 16:37:33 -07:00
|
|
|
switch (destination.type) {
|
|
|
|
|
case PDF::Destination::Type::XYZ:
|
2022-07-11 20:23:24 +00:00
|
|
|
type_str = "XYZ"sv;
|
2021-05-23 16:37:33 -07:00
|
|
|
break;
|
|
|
|
|
case PDF::Destination::Type::Fit:
|
2022-07-11 20:23:24 +00:00
|
|
|
type_str = "Fit"sv;
|
2021-05-23 16:37:33 -07:00
|
|
|
break;
|
|
|
|
|
case PDF::Destination::Type::FitH:
|
2022-07-11 20:23:24 +00:00
|
|
|
type_str = "FitH"sv;
|
2021-05-23 16:37:33 -07:00
|
|
|
break;
|
|
|
|
|
case PDF::Destination::Type::FitV:
|
2022-07-11 20:23:24 +00:00
|
|
|
type_str = "FitV"sv;
|
2021-05-23 16:37:33 -07:00
|
|
|
break;
|
|
|
|
|
case PDF::Destination::Type::FitR:
|
2022-07-11 20:23:24 +00:00
|
|
|
type_str = "FitR"sv;
|
2021-05-23 16:37:33 -07:00
|
|
|
break;
|
|
|
|
|
case PDF::Destination::Type::FitB:
|
2022-07-11 20:23:24 +00:00
|
|
|
type_str = "FitB"sv;
|
2021-05-23 16:37:33 -07:00
|
|
|
break;
|
|
|
|
|
case PDF::Destination::Type::FitBH:
|
2022-07-11 20:23:24 +00:00
|
|
|
type_str = "FitBH"sv;
|
2021-05-23 16:37:33 -07:00
|
|
|
break;
|
|
|
|
|
case PDF::Destination::Type::FitBV:
|
2022-07-11 20:23:24 +00:00
|
|
|
type_str = "FitBV"sv;
|
2021-05-23 16:37:33 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder param_builder;
|
2023-01-06 00:31:53 +08:00
|
|
|
builder.builder().appendff("{{ type={} page="sv, type_str);
|
|
|
|
|
if (!destination.page.has_value())
|
|
|
|
|
TRY(builder.put_literal("{{}}"sv));
|
2022-12-17 13:32:35 +08:00
|
|
|
else
|
|
|
|
|
TRY(builder.put_u64(destination.page.value()));
|
2023-01-05 23:49:40 +08:00
|
|
|
if (!destination.parameters.is_empty()) {
|
|
|
|
|
TRY(builder.put_literal(" parameters="sv));
|
|
|
|
|
for (auto const& param : destination.parameters) {
|
|
|
|
|
if (param.has_value())
|
2023-10-30 23:31:47 +03:30
|
|
|
TRY(builder.put_f32_or_f64(param.value()));
|
2023-01-05 23:49:40 +08:00
|
|
|
else
|
|
|
|
|
TRY(builder.put_literal("{{}}"sv));
|
|
|
|
|
TRY(builder.put_literal(" "sv));
|
|
|
|
|
}
|
2022-12-17 13:32:35 +08:00
|
|
|
}
|
2023-01-06 00:31:53 +08:00
|
|
|
return builder.put_literal(" }}"sv);
|
2021-05-23 16:37:33 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<>
|
2022-07-11 20:23:24 +00:00
|
|
|
struct Formatter<PDF::OutlineItem> : Formatter<FormatString> {
|
2021-11-16 01:15:21 +01:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, PDF::OutlineItem const& item)
|
2021-05-23 16:37:33 -07:00
|
|
|
{
|
2023-12-16 17:49:34 +03:30
|
|
|
return builder.put_string(item.to_byte_string(0));
|
2021-05-23 16:37:33 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<>
|
2022-07-11 20:23:24 +00:00
|
|
|
struct Formatter<PDF::OutlineDict> : Formatter<FormatString> {
|
2021-11-16 01:15:21 +01:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, PDF::OutlineDict const& dict)
|
2021-05-23 16:37:33 -07:00
|
|
|
{
|
|
|
|
|
StringBuilder child_builder;
|
|
|
|
|
child_builder.append('[');
|
|
|
|
|
for (auto& child : dict.children)
|
2023-12-16 17:49:34 +03:30
|
|
|
child_builder.appendff("{}\n", child->to_byte_string(2));
|
2022-07-11 17:32:29 +00:00
|
|
|
child_builder.append(" ]"sv);
|
2021-05-23 16:37:33 -07:00
|
|
|
|
2022-07-11 20:23:24 +00:00
|
|
|
return Formatter<FormatString>::format(builder,
|
2023-12-16 17:49:34 +03:30
|
|
|
"OutlineDict {{\n count={}\n children={}\n}}"sv, dict.count, child_builder.to_byte_string());
|
2021-05-23 16:37:33 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-08 14:57:49 -07:00
|
|
|
}
|