1 // Copyright 2018 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTMANAGER_H_ 6 #define CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTMANAGER_H_ 7 8 #include <stdint.h> 9 10 #include <set> 11 12 #include "core/fxcrt/fx_string_wrappers.h" 13 #include "core/fxcrt/retain_ptr.h" 14 #include "core/fxcrt/unowned_ptr.h" 15 #include "third_party/abseil-cpp/absl/types/variant.h" 16 17 class CPDF_Array; 18 class CPDF_Document; 19 class CPDF_PageObjectHolder; 20 class CPDF_Stream; 21 22 class CPDF_PageContentManager { 23 public: 24 CPDF_PageContentManager(CPDF_PageObjectHolder* page_obj_holder, 25 CPDF_Document* document); 26 ~CPDF_PageContentManager(); 27 28 // Adds a new Content stream. Its index in the array will be returned, or 0 29 // if Contents is not an array, but only a single stream. 30 size_t AddStream(fxcrt::ostringstream* buf); 31 32 // Changes the stream at `stream_index` to contain the data in `buf`. If `buf` 33 // is empty, then schedule the removal of the stream instead. 34 void UpdateStream(size_t stream_index, fxcrt::ostringstream* buf); 35 36 bool HasStreamAtIndex(size_t stream_index); 37 38 private: 39 // Gets the Content stream at a given index. If Contents is a single stream 40 // rather than an array, it is retrievable at index 0. 41 RetainPtr<CPDF_Stream> GetStreamByIndex(size_t stream_index); 42 43 // Schedules the removal of the Content stream at a given index. It will be 44 // removed upon CPDF_PageContentManager destruction. 45 void ScheduleRemoveStreamByIndex(size_t stream_index); 46 47 // Removes all Content streams for which ScheduleRemoveStreamByIndex() was 48 // called. Update the content stream of all page objects with the shifted 49 // indexes. 50 void ExecuteScheduledRemovals(); 51 52 RetainPtr<CPDF_Stream> GetContentsStream(); 53 RetainPtr<CPDF_Array> GetContentsArray(); 54 55 UnownedPtr<CPDF_PageObjectHolder> const page_obj_holder_; 56 UnownedPtr<CPDF_Document> const document_; 57 const std::set<uint32_t> objects_with_multi_refs_; 58 // When holding a CPDF_Stream, the pointer may be null. 59 absl::variant<RetainPtr<CPDF_Stream>, RetainPtr<CPDF_Array>> contents_; 60 std::set<size_t> streams_to_remove_; 61 }; 62 63 #endif // CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTMANAGER_H_ 64