xref: /aosp_15_r20/external/pdfium/core/fpdfapi/edit/cpdf_pagecontentmanager.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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  private:
37   // Gets the Content stream at a given index. If Contents is a single stream
38   // rather than an array, it is retrievable at index 0.
39   RetainPtr<CPDF_Stream> GetStreamByIndex(size_t stream_index);
40 
41   // Schedules the removal of the Content stream at a given index. It will be
42   // removed upon CPDF_PageContentManager destruction.
43   void ScheduleRemoveStreamByIndex(size_t stream_index);
44 
45   // Removes all Content streams for which ScheduleRemoveStreamByIndex() was
46   // called. Update the content stream of all page objects with the shifted
47   // indexes.
48   void ExecuteScheduledRemovals();
49 
50   RetainPtr<CPDF_Stream> GetContentsStream();
51   RetainPtr<CPDF_Array> GetContentsArray();
52 
53   UnownedPtr<CPDF_PageObjectHolder> const page_obj_holder_;
54   UnownedPtr<CPDF_Document> const document_;
55   const std::set<uint32_t> objects_with_multi_refs_;
56   // When holding a CPDF_Stream, the pointer may be null.
57   absl::variant<RetainPtr<CPDF_Stream>, RetainPtr<CPDF_Array>> contents_;
58   std::set<size_t> streams_to_remove_;
59 };
60 
61 #endif  // CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTMANAGER_H_
62