ObjectDerivatives.h 6.4 KB

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