ObjectDerivatives.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/SourceLocation.h>
  12. #include <LibPDF/Forward.h>
  13. #include <LibPDF/Object.h>
  14. #include <LibPDF/Value.h>
  15. namespace PDF {
  16. class StringObject final : public Object {
  17. public:
  18. StringObject(String string, bool is_binary)
  19. : m_string(move(string))
  20. , m_is_binary(is_binary)
  21. {
  22. }
  23. ~StringObject() override = default;
  24. [[nodiscard]] ALWAYS_INLINE String const& string() const { return m_string; }
  25. [[nodiscard]] ALWAYS_INLINE bool is_binary() const { return m_is_binary; }
  26. void set_string(String string) { m_string = move(string); }
  27. char const* type_name() const override { return "string"; }
  28. String to_string(int indent) const override;
  29. protected:
  30. bool is_string() const override { return true; }
  31. private:
  32. String m_string;
  33. bool m_is_binary;
  34. };
  35. class NameObject final : public Object {
  36. public:
  37. explicit NameObject(FlyString name)
  38. : m_name(move(name))
  39. {
  40. }
  41. ~NameObject() override = default;
  42. [[nodiscard]] ALWAYS_INLINE FlyString const& name() const { return m_name; }
  43. char const* type_name() const override { return "name"; }
  44. String to_string(int indent) const override;
  45. protected:
  46. bool is_name() const override { return true; }
  47. private:
  48. FlyString m_name;
  49. };
  50. class ArrayObject final : public Object {
  51. public:
  52. explicit ArrayObject(Vector<Value> elements)
  53. : m_elements(move(elements))
  54. {
  55. }
  56. ~ArrayObject() override = default;
  57. [[nodiscard]] ALWAYS_INLINE size_t size() const { return m_elements.size(); }
  58. [[nodiscard]] ALWAYS_INLINE Vector<Value> elements() const { return m_elements; }
  59. ALWAYS_INLINE auto begin() const { return m_elements.begin(); }
  60. ALWAYS_INLINE auto end() const { return m_elements.end(); }
  61. ALWAYS_INLINE Value const& operator[](size_t index) const { return at(index); }
  62. ALWAYS_INLINE Value const& at(size_t index) const { return m_elements[index]; }
  63. #define DEFINE_INDEXER(class_name, snake_name) \
  64. PDFErrorOr<NonnullRefPtr<class_name>> get_##snake_name##_at(Document*, size_t index) const;
  65. ENUMERATE_OBJECT_TYPES(DEFINE_INDEXER)
  66. #undef DEFINE_INDEXER
  67. char const* type_name() const override
  68. {
  69. return "array";
  70. }
  71. String to_string(int indent) const override;
  72. protected:
  73. bool is_array() const override { return true; }
  74. private:
  75. Vector<Value> m_elements;
  76. };
  77. class DictObject final : public Object {
  78. public:
  79. explicit DictObject(HashMap<FlyString, Value> map)
  80. : m_map(move(map))
  81. {
  82. }
  83. ~DictObject() override = default;
  84. [[nodiscard]] ALWAYS_INLINE HashMap<FlyString, Value> const& map() const { return m_map; }
  85. template<typename... Args>
  86. bool contains(Args&&... keys) const { return (m_map.contains(keys) && ...); }
  87. ALWAYS_INLINE Optional<Value> get(FlyString const& key) const { return m_map.get(key); }
  88. Value get_value(FlyString const& key) const
  89. {
  90. auto value = get(key);
  91. VERIFY(value.has_value());
  92. return value.value();
  93. }
  94. PDFErrorOr<NonnullRefPtr<Object>> get_object(Document*, FlyString const& key) const;
  95. #define DEFINE_GETTER(class_name, snake_name) \
  96. PDFErrorOr<NonnullRefPtr<class_name>> get_##snake_name(Document*, FlyString const& key) const;
  97. ENUMERATE_OBJECT_TYPES(DEFINE_GETTER)
  98. #undef DEFINE_GETTER
  99. char const* type_name() const override
  100. {
  101. return "dict";
  102. }
  103. String to_string(int indent) const override;
  104. protected:
  105. bool is_dict() const override { return true; }
  106. private:
  107. HashMap<FlyString, Value> m_map;
  108. };
  109. class StreamObject : public Object {
  110. public:
  111. explicit StreamObject(NonnullRefPtr<DictObject> const& dict, ByteBuffer const& bytes)
  112. : m_dict(dict)
  113. , m_buffer(bytes)
  114. {
  115. }
  116. virtual ~StreamObject() override = default;
  117. [[nodiscard]] ALWAYS_INLINE NonnullRefPtr<DictObject> dict() const { return m_dict; }
  118. [[nodiscard]] ReadonlyBytes bytes() const { return m_buffer.bytes(); };
  119. [[nodiscard]] ByteBuffer& buffer() { return m_buffer; };
  120. char const* type_name() const override { return "stream"; }
  121. String to_string(int indent) const override;
  122. private:
  123. bool is_stream() const override { return true; }
  124. NonnullRefPtr<DictObject> m_dict;
  125. ByteBuffer m_buffer;
  126. };
  127. class IndirectValue final : public Object {
  128. public:
  129. IndirectValue(u32 index, u32 generation_index, Value const& value)
  130. : m_index(index)
  131. , m_value(value)
  132. {
  133. set_generation_index(generation_index);
  134. }
  135. ~IndirectValue() override = default;
  136. [[nodiscard]] ALWAYS_INLINE u32 index() const { return m_index; }
  137. [[nodiscard]] ALWAYS_INLINE Value const& value() const { return m_value; }
  138. char const* type_name() const override { return "indirect_object"; }
  139. String to_string(int indent) const override;
  140. protected:
  141. bool is_indirect_value() const override { return true; }
  142. private:
  143. u32 m_index;
  144. Value m_value;
  145. };
  146. }