2021-09-17 03:28:52 +03:00
|
|
|
/*
|
2022-03-06 03:30:55 +03:00
|
|
|
* Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
|
2021-09-17 03:28:52 +03:00
|
|
|
* Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-01-09 03:23:00 +03:00
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2021-09-17 03:28:52 +03:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
#include <AK/SourceLocation.h>
|
|
|
|
#include <LibPDF/Forward.h>
|
|
|
|
#include <LibPDF/Object.h>
|
|
|
|
#include <LibPDF/Value.h>
|
|
|
|
|
|
|
|
namespace PDF {
|
|
|
|
|
|
|
|
class StringObject final : public Object {
|
|
|
|
public:
|
2022-12-04 21:02:33 +03:00
|
|
|
StringObject(DeprecatedString string, bool is_binary)
|
2021-09-17 03:28:52 +03:00
|
|
|
: m_string(move(string))
|
|
|
|
, m_is_binary(is_binary)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~StringObject() override = default;
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE DeprecatedString const& string() const { return m_string; }
|
2021-09-17 03:28:52 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_binary() const { return m_is_binary; }
|
2022-12-04 21:02:33 +03:00
|
|
|
void set_string(DeprecatedString string) { m_string = move(string); }
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* type_name() const override { return "string"; }
|
2022-12-06 04:12:49 +03:00
|
|
|
DeprecatedString to_deprecated_string(int indent) const override;
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-03-06 03:30:55 +03:00
|
|
|
protected:
|
|
|
|
bool is_string() const override { return true; }
|
|
|
|
|
2021-09-17 03:28:52 +03:00
|
|
|
private:
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString m_string;
|
2021-09-17 03:28:52 +03:00
|
|
|
bool m_is_binary;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NameObject final : public Object {
|
|
|
|
public:
|
2023-01-09 03:23:00 +03:00
|
|
|
explicit NameObject(DeprecatedFlyString name)
|
2021-09-17 03:28:52 +03:00
|
|
|
: m_name(move(name))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~NameObject() override = default;
|
|
|
|
|
2023-01-09 03:23:00 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE DeprecatedFlyString const& name() const { return m_name; }
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* type_name() const override { return "name"; }
|
2022-12-06 04:12:49 +03:00
|
|
|
DeprecatedString to_deprecated_string(int indent) const override;
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-03-06 03:30:55 +03:00
|
|
|
protected:
|
|
|
|
bool is_name() const override { return true; }
|
|
|
|
|
2021-09-17 03:28:52 +03:00
|
|
|
private:
|
2023-01-09 03:23:00 +03:00
|
|
|
DeprecatedFlyString m_name;
|
2021-09-17 03:28:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class ArrayObject final : public Object {
|
|
|
|
public:
|
|
|
|
explicit ArrayObject(Vector<Value> elements)
|
|
|
|
: m_elements(move(elements))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ArrayObject() override = default;
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE size_t size() const { return m_elements.size(); }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE Vector<Value> elements() const { return m_elements; }
|
2022-11-25 11:34:06 +03:00
|
|
|
[[nodiscard]] Vector<float> float_elements() const;
|
2021-09-17 03:28:52 +03:00
|
|
|
|
|
|
|
ALWAYS_INLINE auto begin() const { return m_elements.begin(); }
|
|
|
|
ALWAYS_INLINE auto end() const { return m_elements.end(); }
|
|
|
|
|
|
|
|
ALWAYS_INLINE Value const& operator[](size_t index) const { return at(index); }
|
|
|
|
ALWAYS_INLINE Value const& at(size_t index) const { return m_elements[index]; }
|
|
|
|
|
LibPDF: Add more utility methods to {Dict,Array}Object
Being both of them containers, these classes already offered a set of
methods to retrieve an inner element by key or index, respectively, with
different methods for the different subtypes of the PDF::Object type
returning the element cast to the correct type pointer. On top of
that, DictObject offered an additional method to obtain an element as an
Object pointer.
While these methods were useful, they have some shortcomings:
* They always take a Document pointer to first perform an object
resolution, in case the element is a Reference. This is not always
necessary though, as there are values that are always meant to be
immediate, and hence the resolution lookup adds overhead.
* There was no easy way to get an individual Object element from an
ArrayObject like there is in DictObject. This makes it difficult to
obtain such values, as one first needs to call dict.get() to get a
Value, then cast it manually to a NonnullRefPtr<Object>.
This commit fixes these two issues by:
* Adding a new method that returns an Object for a given index.
* Adding overloads for this new method, and all the existing methods
described above, that do *not* take a Document, and therefore do
*not* perform an object resolution lookup.
2023-01-05 19:19:12 +03:00
|
|
|
PDFErrorOr<NonnullRefPtr<Object>> get_object_at(Document* document, size_t index) const;
|
2023-07-08 05:48:11 +03:00
|
|
|
NonnullRefPtr<Object> get_object_at(size_t index) const { return at(index).get<NonnullRefPtr<Object>>(); }
|
LibPDF: Add more utility methods to {Dict,Array}Object
Being both of them containers, these classes already offered a set of
methods to retrieve an inner element by key or index, respectively, with
different methods for the different subtypes of the PDF::Object type
returning the element cast to the correct type pointer. On top of
that, DictObject offered an additional method to obtain an element as an
Object pointer.
While these methods were useful, they have some shortcomings:
* They always take a Document pointer to first perform an object
resolution, in case the element is a Reference. This is not always
necessary though, as there are values that are always meant to be
immediate, and hence the resolution lookup adds overhead.
* There was no easy way to get an individual Object element from an
ArrayObject like there is in DictObject. This makes it difficult to
obtain such values, as one first needs to call dict.get() to get a
Value, then cast it manually to a NonnullRefPtr<Object>.
This commit fixes these two issues by:
* Adding a new method that returns an Object for a given index.
* Adding overloads for this new method, and all the existing methods
described above, that do *not* take a Document, and therefore do
*not* perform an object resolution lookup.
2023-01-05 19:19:12 +03:00
|
|
|
|
|
|
|
#define DEFINE_INDEXER(class_name, snake_name) \
|
|
|
|
PDFErrorOr<NonnullRefPtr<class_name>> get_##snake_name##_at(Document*, size_t index) const; \
|
|
|
|
NonnullRefPtr<class_name> get_##snake_name##_at(size_t index) const;
|
2021-09-17 03:28:52 +03:00
|
|
|
ENUMERATE_OBJECT_TYPES(DEFINE_INDEXER)
|
|
|
|
#undef DEFINE_INDEXER
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* type_name() const override
|
2021-09-17 03:28:52 +03:00
|
|
|
{
|
2022-03-06 03:30:55 +03:00
|
|
|
return "array";
|
2021-09-17 03:28:52 +03:00
|
|
|
}
|
2022-12-06 04:12:49 +03:00
|
|
|
DeprecatedString to_deprecated_string(int indent) const override;
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-03-06 03:30:55 +03:00
|
|
|
protected:
|
|
|
|
bool is_array() const override { return true; }
|
|
|
|
|
2021-09-17 03:28:52 +03:00
|
|
|
private:
|
|
|
|
Vector<Value> m_elements;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DictObject final : public Object {
|
|
|
|
public:
|
2023-01-09 03:23:00 +03:00
|
|
|
explicit DictObject(HashMap<DeprecatedFlyString, Value> map)
|
2021-09-17 03:28:52 +03:00
|
|
|
: m_map(move(map))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~DictObject() override = default;
|
|
|
|
|
2023-01-09 03:23:00 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE HashMap<DeprecatedFlyString, Value> const& map() const { return m_map; }
|
2021-09-17 03:28:52 +03:00
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
bool contains(Args&&... keys) const { return (m_map.contains(keys) && ...); }
|
|
|
|
|
2022-08-20 16:17:15 +03:00
|
|
|
template<typename... Args>
|
|
|
|
bool contains_any_of(Args&&... keys) const { return (m_map.contains(keys) || ...); }
|
|
|
|
|
2023-01-09 03:23:00 +03:00
|
|
|
ALWAYS_INLINE Optional<Value> get(DeprecatedFlyString const& key) const { return m_map.get(key); }
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2023-01-09 03:23:00 +03:00
|
|
|
Value get_value(DeprecatedFlyString const& key) const
|
2021-09-17 03:28:52 +03:00
|
|
|
{
|
|
|
|
auto value = get(key);
|
|
|
|
VERIFY(value.has_value());
|
|
|
|
return value.value();
|
|
|
|
}
|
|
|
|
|
2023-01-09 03:23:00 +03:00
|
|
|
PDFErrorOr<NonnullRefPtr<Object>> get_object(Document*, DeprecatedFlyString const& key) const;
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2023-01-09 03:23:00 +03:00
|
|
|
#define DEFINE_GETTER(class_name, snake_name) \
|
|
|
|
PDFErrorOr<NonnullRefPtr<class_name>> get_##snake_name(Document*, DeprecatedFlyString const& key) const; \
|
|
|
|
NonnullRefPtr<class_name> get_##snake_name(DeprecatedFlyString const& key) const;
|
2021-09-17 03:28:52 +03:00
|
|
|
ENUMERATE_OBJECT_TYPES(DEFINE_GETTER)
|
|
|
|
#undef DEFINE_GETTER
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* type_name() const override
|
2021-09-17 03:28:52 +03:00
|
|
|
{
|
2022-03-06 03:30:55 +03:00
|
|
|
return "dict";
|
2021-09-17 03:28:52 +03:00
|
|
|
}
|
2022-12-06 04:12:49 +03:00
|
|
|
DeprecatedString to_deprecated_string(int indent) const override;
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-03-06 03:30:55 +03:00
|
|
|
protected:
|
|
|
|
bool is_dict() const override { return true; }
|
|
|
|
|
2021-09-17 03:28:52 +03:00
|
|
|
private:
|
2023-01-09 03:23:00 +03:00
|
|
|
HashMap<DeprecatedFlyString, Value> m_map;
|
2021-09-17 03:28:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class StreamObject : public Object {
|
|
|
|
public:
|
2022-03-20 21:07:47 +03:00
|
|
|
explicit StreamObject(NonnullRefPtr<DictObject> const& dict, ByteBuffer const& bytes)
|
2021-09-17 03:28:52 +03:00
|
|
|
: m_dict(dict)
|
2022-03-20 21:07:47 +03:00
|
|
|
, m_buffer(bytes)
|
2021-09-17 03:28:52 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~StreamObject() override = default;
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE NonnullRefPtr<DictObject> dict() const { return m_dict; }
|
2023-07-08 05:48:11 +03:00
|
|
|
[[nodiscard]] ReadonlyBytes bytes() const { return m_buffer.bytes(); }
|
|
|
|
[[nodiscard]] ByteBuffer& buffer() { return m_buffer; }
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* type_name() const override { return "stream"; }
|
2022-12-06 04:12:49 +03:00
|
|
|
DeprecatedString to_deprecated_string(int indent) const override;
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-03-20 21:07:47 +03:00
|
|
|
private:
|
2022-03-06 03:30:55 +03:00
|
|
|
bool is_stream() const override { return true; }
|
|
|
|
|
2021-09-17 03:28:52 +03:00
|
|
|
NonnullRefPtr<DictObject> m_dict;
|
|
|
|
ByteBuffer m_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class IndirectValue final : public Object {
|
|
|
|
public:
|
|
|
|
IndirectValue(u32 index, u32 generation_index, Value const& value)
|
|
|
|
: m_index(index)
|
|
|
|
, m_value(value)
|
|
|
|
{
|
|
|
|
set_generation_index(generation_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
~IndirectValue() override = default;
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE u32 index() const { return m_index; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE Value const& value() const { return m_value; }
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* type_name() const override { return "indirect_object"; }
|
2022-12-06 04:12:49 +03:00
|
|
|
DeprecatedString to_deprecated_string(int indent) const override;
|
2021-09-17 03:28:52 +03:00
|
|
|
|
2022-03-06 03:30:55 +03:00
|
|
|
protected:
|
|
|
|
bool is_indirect_value() const override { return true; }
|
|
|
|
|
2021-09-17 03:28:52 +03:00
|
|
|
private:
|
|
|
|
u32 m_index;
|
|
|
|
Value m_value;
|
|
|
|
};
|
|
|
|
|
2023-01-05 18:55:25 +03:00
|
|
|
template<IsValueType T>
|
|
|
|
UnwrappedValueType<T> cast_to(Value const& value)
|
|
|
|
{
|
|
|
|
if constexpr (IsSame<T, bool>)
|
|
|
|
return value.get<bool>();
|
|
|
|
else if constexpr (IsSame<T, int>)
|
|
|
|
return value.get<int>();
|
|
|
|
else if constexpr (IsSame<T, float>)
|
|
|
|
return value.get<float>();
|
|
|
|
else if constexpr (IsSame<T, Object>)
|
|
|
|
return value.get<NonnullRefPtr<Object>>();
|
|
|
|
else if constexpr (IsObject<T>)
|
|
|
|
return value.get<NonnullRefPtr<Object>>()->cast<T>();
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2021-09-17 03:28:52 +03:00
|
|
|
}
|