xref: /aosp_15_r20/external/pdfium/core/fpdfapi/edit/cpdf_pagecontentgenerator.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTGENERATOR_H_
8 #define CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTGENERATOR_H_
9 
10 #include <stdint.h>
11 
12 #include <map>
13 #include <vector>
14 
15 #include "core/fxcrt/bytestring.h"
16 #include "core/fxcrt/fx_string_wrappers.h"
17 #include "core/fxcrt/unowned_ptr.h"
18 
19 class CPDF_ContentMarks;
20 class CPDF_Document;
21 class CPDF_FormObject;
22 class CPDF_ImageObject;
23 class CPDF_Object;
24 class CPDF_PageObject;
25 class CPDF_PageObjectHolder;
26 class CPDF_Path;
27 class CPDF_PathObject;
28 class CPDF_TextObject;
29 
30 class CPDF_PageContentGenerator {
31  public:
32   explicit CPDF_PageContentGenerator(CPDF_PageObjectHolder* pObjHolder);
33   ~CPDF_PageContentGenerator();
34 
35   void GenerateContent();
36   bool ProcessPageObjects(fxcrt::ostringstream* buf);
37 
38  private:
39   friend class CPDF_PageContentGeneratorTest;
40 
41   void ProcessPageObject(fxcrt::ostringstream* buf, CPDF_PageObject* pPageObj);
42   void ProcessPathPoints(fxcrt::ostringstream* buf, CPDF_Path* pPath);
43   void ProcessPath(fxcrt::ostringstream* buf, CPDF_PathObject* pPathObj);
44   void ProcessForm(fxcrt::ostringstream* buf, CPDF_FormObject* pFormObj);
45   void ProcessImage(fxcrt::ostringstream* buf, CPDF_ImageObject* pImageObj);
46   void ProcessGraphics(fxcrt::ostringstream* buf, CPDF_PageObject* pPageObj);
47   void ProcessDefaultGraphics(fxcrt::ostringstream* buf);
48   void ProcessText(fxcrt::ostringstream* buf, CPDF_TextObject* pTextObj);
49   ByteString GetOrCreateDefaultGraphics() const;
50   ByteString RealizeResource(const CPDF_Object* pResource,
51                              const ByteString& bsType) const;
52   const CPDF_ContentMarks* ProcessContentMarks(fxcrt::ostringstream* buf,
53                                                const CPDF_PageObject* pPageObj,
54                                                const CPDF_ContentMarks* pPrev);
55   void FinishMarks(fxcrt::ostringstream* buf,
56                    const CPDF_ContentMarks* pContentMarks);
57 
58   // Returns a map from content stream index to new stream data. Unmodified
59   // streams are not touched.
60   std::map<int32_t, fxcrt::ostringstream> GenerateModifiedStreams();
61 
62   // For each entry in `new_stream_data`, adds the string buffer to the page's
63   // content stream.
64   void UpdateContentStreams(
65       std::map<int32_t, fxcrt::ostringstream>&& new_stream_data);
66 
67   // Sets the stream index of all page objects with stream index ==
68   // |CPDF_PageObject::kNoContentStream|. These are new objects that had not
69   // been parsed from or written to any content stream yet.
70   void UpdateStreamlessPageObjects(int new_content_stream_index);
71 
72   // Updates the resource dictionary for `m_pObjHolder` to account for all the
73   // changes.
74   void UpdateResourcesDict();
75 
76   UnownedPtr<CPDF_PageObjectHolder> const m_pObjHolder;
77   UnownedPtr<CPDF_Document> const m_pDocument;
78   std::vector<UnownedPtr<CPDF_PageObject>> m_pageObjects;
79   ByteString m_DefaultGraphicsName;
80 };
81 
82 #endif  // CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTGENERATOR_H_
83