ObjectDerivatives.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2021, 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. ALWAYS_INLINE bool is_string() const override { return true; }
  27. ALWAYS_INLINE const char* type_name() const override { return "string"; }
  28. String to_string(int indent) const override;
  29. private:
  30. String m_string;
  31. bool m_is_binary;
  32. };
  33. class NameObject final : public Object {
  34. public:
  35. explicit NameObject(FlyString name)
  36. : m_name(move(name))
  37. {
  38. }
  39. ~NameObject() override = default;
  40. [[nodiscard]] ALWAYS_INLINE FlyString const& name() const { return m_name; }
  41. ALWAYS_INLINE bool is_name() const override { return true; }
  42. ALWAYS_INLINE const char* type_name() const override { return "name"; }
  43. String to_string(int indent) const override;
  44. private:
  45. FlyString m_name;
  46. };
  47. class ArrayObject final : public Object {
  48. public:
  49. explicit ArrayObject(Vector<Value> elements)
  50. : m_elements(move(elements))
  51. {
  52. }
  53. ~ArrayObject() override = default;
  54. [[nodiscard]] ALWAYS_INLINE size_t size() const { return m_elements.size(); }
  55. [[nodiscard]] ALWAYS_INLINE Vector<Value> elements() const { return m_elements; }
  56. ALWAYS_INLINE auto begin() const { return m_elements.begin(); }
  57. ALWAYS_INLINE auto end() const { return m_elements.end(); }
  58. ALWAYS_INLINE Value const& operator[](size_t index) const { return at(index); }
  59. ALWAYS_INLINE Value const& at(size_t index) const { return m_elements[index]; }
  60. NonnullRefPtr<Object> get_object_at(Document*, size_t index) const;
  61. #define DEFINE_INDEXER(class_name, snake_name) \
  62. NonnullRefPtr<class_name> get_##snake_name##_at(Document*, size_t index) const;
  63. ENUMERATE_OBJECT_TYPES(DEFINE_INDEXER)
  64. #undef DEFINE_INDEXER
  65. ALWAYS_INLINE bool is_array() const override
  66. {
  67. return true;
  68. }
  69. ALWAYS_INLINE const char* type_name() const override { return "array"; }
  70. String to_string(int indent) const override;
  71. private:
  72. Vector<Value> m_elements;
  73. };
  74. class DictObject final : public Object {
  75. public:
  76. explicit DictObject(HashMap<FlyString, Value> map)
  77. : m_map(move(map))
  78. {
  79. }
  80. ~DictObject() override = default;
  81. [[nodiscard]] ALWAYS_INLINE HashMap<FlyString, Value> const& map() const { return m_map; }
  82. template<typename... Args>
  83. bool contains(Args&&... keys) const { return (m_map.contains(keys) && ...); }
  84. ALWAYS_INLINE Optional<Value> get(FlyString const& key) const { return m_map.get(key); }
  85. Value get_value(FlyString const& key) const
  86. {
  87. auto value = get(key);
  88. VERIFY(value.has_value());
  89. return value.value();
  90. }
  91. NonnullRefPtr<Object> get_object(Document*, FlyString const& key) const;
  92. #define DEFINE_GETTER(class_name, snake_name) \
  93. NonnullRefPtr<class_name> get_##snake_name(Document*, FlyString const& key) const;
  94. ENUMERATE_OBJECT_TYPES(DEFINE_GETTER)
  95. #undef DEFINE_GETTER
  96. ALWAYS_INLINE bool is_dict() const override
  97. {
  98. return true;
  99. }
  100. ALWAYS_INLINE const char* type_name() const override { return "dict"; }
  101. String to_string(int indent) const override;
  102. private:
  103. HashMap<FlyString, Value> m_map;
  104. };
  105. class StreamObject : public Object {
  106. public:
  107. explicit StreamObject(NonnullRefPtr<DictObject> const& dict)
  108. : m_dict(dict)
  109. {
  110. }
  111. virtual ~StreamObject() override = default;
  112. [[nodiscard]] ALWAYS_INLINE NonnullRefPtr<DictObject> dict() const { return m_dict; }
  113. [[nodiscard]] virtual ReadonlyBytes bytes() const = 0;
  114. ALWAYS_INLINE bool is_stream() const override { return true; }
  115. ALWAYS_INLINE const char* type_name() const override { return "stream"; }
  116. String to_string(int indent) const override;
  117. private:
  118. NonnullRefPtr<DictObject> m_dict;
  119. };
  120. class PlainTextStreamObject final : public StreamObject {
  121. public:
  122. PlainTextStreamObject(NonnullRefPtr<DictObject> const& dict, ReadonlyBytes bytes)
  123. : StreamObject(dict)
  124. , m_bytes(bytes)
  125. {
  126. }
  127. virtual ~PlainTextStreamObject() override = default;
  128. [[nodiscard]] ALWAYS_INLINE virtual ReadonlyBytes bytes() const override { return m_bytes; }
  129. private:
  130. ReadonlyBytes m_bytes;
  131. };
  132. class EncodedStreamObject final : public StreamObject {
  133. public:
  134. EncodedStreamObject(NonnullRefPtr<DictObject> const& dict, ByteBuffer&& buffer)
  135. : StreamObject(dict)
  136. , m_buffer(buffer)
  137. {
  138. }
  139. virtual ~EncodedStreamObject() override = default;
  140. [[nodiscard]] ALWAYS_INLINE virtual ReadonlyBytes bytes() const override { return m_buffer.bytes(); }
  141. private:
  142. ByteBuffer m_buffer;
  143. };
  144. class IndirectValue final : public Object {
  145. public:
  146. IndirectValue(u32 index, u32 generation_index, Value const& value)
  147. : m_index(index)
  148. , m_value(value)
  149. {
  150. set_generation_index(generation_index);
  151. }
  152. ~IndirectValue() override = default;
  153. [[nodiscard]] ALWAYS_INLINE u32 index() const { return m_index; }
  154. [[nodiscard]] ALWAYS_INLINE Value const& value() const { return m_value; }
  155. ALWAYS_INLINE bool is_indirect_value() const override { return true; }
  156. ALWAYS_INLINE const char* type_name() const override { return "indirect_object"; }
  157. String to_string(int indent) const override;
  158. private:
  159. u32 m_index;
  160. Value m_value;
  161. };
  162. }