1 // Copyright 2016 PDFium Authors. All rights reserved.
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_RENDER_CPDF_RENDERSTATUS_H_
8 #define CORE_FPDFAPI_RENDER_CPDF_RENDERSTATUS_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fpdfapi/page/cpdf_clippath.h"
14 #include "core/fpdfapi/page/cpdf_graphicstates.h"
15 #include "core/fpdfapi/page/cpdf_transparency.h"
16 #include "core/fpdfapi/parser/cpdf_dictionary.h"
17 #include "core/fpdfapi/render/cpdf_renderoptions.h"
18 #include "core/fxcrt/retain_ptr.h"
19 #include "core/fxcrt/unowned_ptr.h"
20 #include "core/fxge/dib/fx_dib.h"
21 
22 class CFX_DIBitmap;
23 class CFX_PathData;
24 class CFX_RenderDevice;
25 class CPDF_Color;
26 class CPDF_Font;
27 class CPDF_FormObject;
28 class CPDF_ImageCacheEntry;
29 class CPDF_ImageObject;
30 class CPDF_ImageRenderer;
31 class CPDF_Object;
32 class CPDF_PageObject;
33 class CPDF_PageObjectHolder;
34 class CPDF_PathObject;
35 class CPDF_RenderContext;
36 class CPDF_ShadingObject;
37 class CPDF_ShadingPattern;
38 class CPDF_TilingPattern;
39 class CPDF_TransferFunc;
40 class CPDF_Type3Cache;
41 class CPDF_Type3Char;
42 class CPDF_Type3Font;
43 class PauseIndicatorIface;
44 
45 class CPDF_RenderStatus {
46  public:
47   CPDF_RenderStatus(CPDF_RenderContext* pContext, CFX_RenderDevice* pDevice);
48   ~CPDF_RenderStatus();
49 
50   // Called prior to Initialize().
SetOptions(const CPDF_RenderOptions & options)51   void SetOptions(const CPDF_RenderOptions& options) { m_Options = options; }
SetDeviceMatrix(const CFX_Matrix & matrix)52   void SetDeviceMatrix(const CFX_Matrix& matrix) { m_DeviceMatrix = matrix; }
SetStopObject(const CPDF_PageObject * pStopObj)53   void SetStopObject(const CPDF_PageObject* pStopObj) { m_pStopObj = pStopObj; }
SetFormResource(const CPDF_Dictionary * pRes)54   void SetFormResource(const CPDF_Dictionary* pRes) {
55     m_pFormResource.Reset(pRes);
56   }
SetType3Char(CPDF_Type3Char * pType3Char)57   void SetType3Char(CPDF_Type3Char* pType3Char) { m_pType3Char = pType3Char; }
SetFillColor(FX_ARGB color)58   void SetFillColor(FX_ARGB color) { m_T3FillColor = color; }
SetDropObjects(bool bDropObjects)59   void SetDropObjects(bool bDropObjects) { m_bDropObjects = bDropObjects; }
SetLoadMask(bool bLoadMask)60   void SetLoadMask(bool bLoadMask) { m_bLoadMask = bLoadMask; }
SetStdCS(bool bStdCS)61   void SetStdCS(bool bStdCS) { m_bStdCS = bStdCS; }
SetGroupFamily(uint32_t family)62   void SetGroupFamily(uint32_t family) { m_GroupFamily = family; }
SetTransparency(const CPDF_Transparency & transparency)63   void SetTransparency(const CPDF_Transparency& transparency) {
64     m_Transparency = transparency;
65   }
66 
67   void Initialize(const CPDF_RenderStatus* pParentStatus,
68                   const CPDF_GraphicStates* pInitialStates);
69 
70   void RenderObjectList(const CPDF_PageObjectHolder* pObjectHolder,
71                         const CFX_Matrix& mtObj2Device);
72   void RenderSingleObject(CPDF_PageObject* pObj,
73                           const CFX_Matrix& mtObj2Device);
74   bool ContinueSingleObject(CPDF_PageObject* pObj,
75                             const CFX_Matrix& mtObj2Device,
76                             PauseIndicatorIface* pPause);
77   void ProcessClipPath(const CPDF_ClipPath& ClipPath,
78                        const CFX_Matrix& mtObj2Device);
79 
GetGroupFamily()80   uint32_t GetGroupFamily() const { return m_GroupFamily; }
GetLoadMask()81   bool GetLoadMask() const { return m_bLoadMask; }
GetDropObjects()82   bool GetDropObjects() const { return m_bDropObjects; }
IsPrint()83   bool IsPrint() const { return m_bPrint; }
IsStopped()84   bool IsStopped() const { return m_bStopped; }
GetContext()85   CPDF_RenderContext* GetContext() const { return m_pContext.Get(); }
GetFormResource()86   const CPDF_Dictionary* GetFormResource() const {
87     return m_pFormResource.Get();
88   }
GetPageResource()89   const CPDF_Dictionary* GetPageResource() const {
90     return m_pPageResource.Get();
91   }
GetRenderDevice()92   CFX_RenderDevice* GetRenderDevice() const { return m_pDevice; }
GetRenderOptions()93   const CPDF_RenderOptions& GetRenderOptions() const { return m_Options; }
94 
95 #if defined(_SKIA_SUPPORT_)
96   void DebugVerifyDeviceIsPreMultiplied() const;
97 #endif
98 
99   RetainPtr<CPDF_TransferFunc> GetTransferFunc(
100       const CPDF_Object* pObject) const;
101 
GetFillArgb(CPDF_PageObject * pObj)102   FX_ARGB GetFillArgb(CPDF_PageObject* pObj) const {
103     return GetFillArgbInternal(pObj, false);
104   }
GetFillArgbForType3(CPDF_PageObject * pObj)105   FX_ARGB GetFillArgbForType3(CPDF_PageObject* pObj) const {
106     return GetFillArgbInternal(pObj, true);
107   }
108 
109   void DrawTilingPattern(CPDF_TilingPattern* pPattern,
110                          CPDF_PageObject* pPageObj,
111                          const CFX_Matrix& mtObj2Device,
112                          bool stroke);
113   void DrawShadingPattern(CPDF_ShadingPattern* pPattern,
114                           const CPDF_PageObject* pPageObj,
115                           const CFX_Matrix& mtObj2Device,
116                           bool stroke);
117   void CompositeDIBitmap(const RetainPtr<CFX_DIBitmap>& pDIBitmap,
118                          int left,
119                          int top,
120                          FX_ARGB mask_argb,
121                          int bitmap_alpha,
122                          BlendMode blend_mode,
123                          const CPDF_Transparency& transparency);
124 
125   static std::unique_ptr<CPDF_GraphicStates> CloneObjStates(
126       const CPDF_GraphicStates* pSrcStates,
127       bool stroke);
128 
129  private:
130   FX_ARGB GetFillArgbInternal(CPDF_PageObject* pObj, bool bType3) const;
131   bool ProcessTransparency(CPDF_PageObject* PageObj,
132                            const CFX_Matrix& mtObj2Device);
133   void ProcessObjectNoClip(CPDF_PageObject* pObj,
134                            const CFX_Matrix& mtObj2Device);
135   void DrawObjWithBackground(CPDF_PageObject* pObj,
136                              const CFX_Matrix& mtObj2Device);
137   bool DrawObjWithBlend(CPDF_PageObject* pObj, const CFX_Matrix& mtObj2Device);
138   bool ProcessPath(CPDF_PathObject* path_obj, const CFX_Matrix& mtObj2Device);
139   void ProcessPathPattern(CPDF_PathObject* path_obj,
140                           const CFX_Matrix& mtObj2Device,
141                           CFX_FillRenderOptions::FillType* fill_type,
142                           bool* stroke);
143   void DrawPathWithPattern(CPDF_PathObject* path_obj,
144                            const CFX_Matrix& mtObj2Device,
145                            const CPDF_Color* pColor,
146                            bool stroke);
147   bool ClipPattern(const CPDF_PageObject* page_obj,
148                    const CFX_Matrix& mtObj2Device,
149                    bool stroke);
150   bool SelectClipPath(const CPDF_PathObject* path_obj,
151                       const CFX_Matrix& mtObj2Device,
152                       bool stroke);
153   bool ProcessImage(CPDF_ImageObject* pImageObj,
154                     const CFX_Matrix& mtObj2Device);
155   void ProcessShading(const CPDF_ShadingObject* pShadingObj,
156                       const CFX_Matrix& mtObj2Device);
157   bool ProcessType3Text(CPDF_TextObject* textobj,
158                         const CFX_Matrix& mtObj2Device);
159   bool ProcessText(CPDF_TextObject* textobj,
160                    const CFX_Matrix& mtObj2Device,
161                    CFX_PathData* clipping_path);
162   void DrawTextPathWithPattern(const CPDF_TextObject* textobj,
163                                const CFX_Matrix& mtObj2Device,
164                                CPDF_Font* pFont,
165                                float font_size,
166                                const CFX_Matrix* pTextMatrix,
167                                bool fill,
168                                bool stroke);
169   bool ProcessForm(const CPDF_FormObject* pFormObj,
170                    const CFX_Matrix& mtObj2Device);
171   RetainPtr<CFX_DIBitmap> GetBackdrop(const CPDF_PageObject* pObj,
172                                       const FX_RECT& rect,
173                                       bool bBackAlphaRequired,
174                                       int* left,
175                                       int* top);
176   RetainPtr<CFX_DIBitmap> LoadSMask(CPDF_Dictionary* pSMaskDict,
177                                     FX_RECT* pClipRect,
178                                     const CFX_Matrix* pMatrix);
179   // Optionally write the colorspace family value into |pCSFamily|.
180   FX_ARGB GetBackColor(const CPDF_Dictionary* pSMaskDict,
181                        const CPDF_Dictionary* pGroupDict,
182                        int* pCSFamily);
183   FX_ARGB GetStrokeArgb(CPDF_PageObject* pObj) const;
184   FX_RECT GetObjectClippedRect(const CPDF_PageObject* pObj,
185                                const CFX_Matrix& mtObj2Device) const;
186 
187   CPDF_RenderOptions m_Options;
188   RetainPtr<const CPDF_Dictionary> m_pFormResource;
189   RetainPtr<CPDF_Dictionary> m_pPageResource;
190   std::vector<CPDF_Type3Font*> m_Type3FontCache;
191   UnownedPtr<CPDF_RenderContext> const m_pContext;
192   bool m_bStopped = false;
193   CFX_RenderDevice* const m_pDevice;
194   CFX_Matrix m_DeviceMatrix;
195   CPDF_ClipPath m_LastClipPath;
196   UnownedPtr<const CPDF_PageObject> m_pCurObj;
197   UnownedPtr<const CPDF_PageObject> m_pStopObj;
198   CPDF_GraphicStates m_InitialStates;
199   std::unique_ptr<CPDF_ImageRenderer> m_pImageRenderer;
200   CPDF_Transparency m_Transparency;
201   bool m_bPrint = false;
202   bool m_bDropObjects = false;
203   bool m_bStdCS = false;
204   bool m_bLoadMask = false;
205   uint32_t m_GroupFamily = 0;
206   UnownedPtr<CPDF_Type3Char> m_pType3Char;
207   FX_ARGB m_T3FillColor = 0;
208   BlendMode m_curBlend = BlendMode::kNormal;
209 };
210 
211 #endif  // CORE_FPDFAPI_RENDER_CPDF_RENDERSTATUS_H_
212