Refactor progressive renderer class.
[pdfium.git] / core / include / fpdfapi / fpdf_render.h
1 // Copyright 2014 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_INCLUDE_FPDFAPI_FPDF_RENDER_H_
8 #define CORE_INCLUDE_FPDFAPI_FPDF_RENDER_H_
9
10 #include "../../../third_party/base/nonstd_unique_ptr.h"
11 #include "../../../public/fpdf_progressive.h"
12 #include "../fxge/fx_ge.h"
13 #include "fpdf_page.h"
14
15 class CFX_GraphStateData;
16 class CFX_PathData;
17 class CFX_RenderDevice;
18 class CPDF_FormObject;
19 class CPDF_ImageCache;
20 class CPDF_ImageObject;
21 class CPDF_PathObject;
22 class CPDF_QuickStretcher;
23 class CPDF_RenderContext;
24 class CPDF_RenderOptions;
25 class CPDF_RenderStatus;
26 class CPDF_ShadingObject;
27 class CPDF_TextObject;
28 class IFX_Pause;
29
30 class IPDF_OCContext
31 {
32 public:
33
34     virtual ~IPDF_OCContext() {}
35
36     virtual FX_BOOL     CheckOCGVisible(const CPDF_Dictionary* pOCG) = 0;
37
38     FX_BOOL CheckObjectVisible(const CPDF_PageObject* pObj);
39 };
40 #define RENDER_COLOR_NORMAL             0
41 #define RENDER_COLOR_GRAY               1
42 #define RENDER_COLOR_TWOCOLOR   2
43 #define RENDER_COLOR_ALPHA              3
44 #define RENDER_CLEARTYPE                        0x00000001
45 #define RENDER_PRINTGRAPHICTEXT         0x00000002
46 #define RENDER_FORCE_DOWNSAMPLE         0x00000004
47 #define RENDER_PRINTPREVIEW                     0x00000008
48 #define RENDER_BGR_STRIPE                       0x00000010
49 #define RENDER_NO_NATIVETEXT            0x00000020
50 #define RENDER_FORCE_HALFTONE           0x00000040
51 #define RENDER_RECT_AA                          0x00000080
52 #define RENDER_FILL_FULLCOVER           0x00000100
53 #define RENDER_PRINTIMAGETEXT       0x00000200
54 #define RENDER_OVERPRINT            0x00000400
55 #define RENDER_THINLINE             0x00000800
56 #define RENDER_NOTEXTSMOOTH                     0x10000000
57 #define RENDER_NOPATHSMOOTH                     0x20000000
58 #define RENDER_NOIMAGESMOOTH            0x40000000
59 #define RENDER_LIMITEDIMAGECACHE        0x80000000
60 class CPDF_RenderOptions
61 {
62 public:
63
64     CPDF_RenderOptions();
65
66     int                         m_ColorMode;
67
68     FX_COLORREF         m_BackColor;
69
70     FX_COLORREF         m_ForeColor;
71
72     FX_DWORD            m_Flags;
73
74     int                         m_Interpolation;
75
76     FX_DWORD            m_AddFlags;
77
78     IPDF_OCContext*     m_pOCContext;
79
80     FX_DWORD            m_dwLimitCacheSize;
81
82     int                         m_HalftoneLimit;
83
84     FX_ARGB                     TranslateColor(FX_ARGB argb) const;
85 };
86 class CPDF_RenderContext
87 {
88 public:
89
90     CPDF_RenderContext();
91
92     void                        Create(CPDF_Page* pPage, FX_BOOL bFirstLayer = TRUE);
93
94     void                        Create(CPDF_Document* pDoc = NULL, CPDF_PageRenderCache* pPageCache = NULL,
95                            CPDF_Dictionary* pPageResources = NULL, FX_BOOL bFirstLayer = TRUE);
96
97     ~CPDF_RenderContext();
98
99     void                        Clear();
100
101     void                        AppendObjectList(CPDF_PageObjects* pObjs, const CFX_AffineMatrix* pObject2Device);
102
103     void                        Render(CFX_RenderDevice* pDevice, const CPDF_RenderOptions* pOptions = NULL,
104                            const CFX_AffineMatrix* pFinalMatrix = NULL);
105
106     void                        DrawObjectList(CFX_RenderDevice* pDevice, CPDF_PageObjects* pObjs,
107                                    const CFX_AffineMatrix* pObject2Device, const CPDF_RenderOptions* pOptions);
108
109     void                        GetBackground(CFX_DIBitmap* pBuffer, const CPDF_PageObject* pObj,
110                                   const CPDF_RenderOptions* pOptions, CFX_AffineMatrix* pFinalMatrix);
111
112     CPDF_PageRenderCache*       GetPageCache() const
113     {
114         return m_pPageCache;
115     }
116
117
118
119     CPDF_Document*                      m_pDocument;
120
121     CPDF_Dictionary*            m_pPageResources;
122
123     CPDF_PageRenderCache*       m_pPageCache;
124
125 protected:
126
127     CFX_ArrayTemplate<struct _PDF_RenderItem>   m_ContentList;
128
129     FX_BOOL                                     m_bFirstLayer;
130
131     void                        Render(CFX_RenderDevice* pDevice, const CPDF_PageObject* pStopObj,
132                            const CPDF_RenderOptions* pOptions, const CFX_AffineMatrix* pFinalMatrix);
133     friend class CPDF_RenderStatus;
134     friend class CPDF_ProgressiveRenderer;
135 };
136
137 class CPDF_ProgressiveRenderer
138 {
139 public:
140     // Must match FDF_RENDER_* definitions in fpdf_progressive.h.
141     enum Status {
142         Ready = FPDF_RENDER_READER,
143         ToBeContinued = FPDF_RENDER_TOBECOUNTINUED,
144         Done = FPDF_RENDER_DONE,
145         Failed = FPDF_RENDER_FAILED
146     };
147     static int ToFPDFStatus(Status status) { return static_cast<int>(status); }
148
149     CPDF_ProgressiveRenderer(CPDF_RenderContext* pContext,
150                              CFX_RenderDevice* pDevice,
151                              const CPDF_RenderOptions* pOptions);
152     ~CPDF_ProgressiveRenderer();
153
154     Status GetStatus() const { return m_Status; }
155     void Start(IFX_Pause* pPause);
156     void Continue(IFX_Pause* pPause);
157     int EstimateProgress();
158
159 private:
160     void RenderStep();
161
162     Status m_Status;
163     CPDF_RenderContext* const m_pContext;
164     CFX_RenderDevice* const m_pDevice;
165     const CPDF_RenderOptions* const m_pOptions;
166     nonstd::unique_ptr<CPDF_RenderStatus> m_pRenderStatus;
167     CFX_FloatRect m_ClipRect;
168     FX_DWORD m_LayerIndex;
169     FX_DWORD m_ObjectIndex;
170     FX_POSITION m_ObjectPos;
171     FX_POSITION m_PrevLastPos;
172 };
173
174 class CPDF_TextRenderer
175 {
176 public:
177
178     static void         DrawTextString(CFX_RenderDevice* pDevice, int left, int top,
179                                    CPDF_Font* pFont,
180                                    int height,
181                                    const CFX_ByteString& str,
182                                    FX_ARGB argb);
183
184     static void         DrawTextString(CFX_RenderDevice* pDevice, FX_FLOAT origin_x, FX_FLOAT origin_y,
185                                    CPDF_Font* pFont,
186                                    FX_FLOAT font_size,
187                                    const CFX_AffineMatrix* matrix,
188                                    const CFX_ByteString& str,
189                                    FX_ARGB fill_argb,
190                                    FX_ARGB stroke_argb = 0,
191                                    const CFX_GraphStateData* pGraphState = NULL,
192                                    const CPDF_RenderOptions* pOptions = NULL
193                                );
194
195     static FX_BOOL      DrawTextPath(CFX_RenderDevice* pDevice, int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos,
196                                  CPDF_Font* pFont, FX_FLOAT font_size,
197                                  const CFX_AffineMatrix* pText2User, const CFX_AffineMatrix* pUser2Device,
198                                  const CFX_GraphStateData* pGraphState,
199                                  FX_ARGB fill_argb, FX_ARGB stroke_argb, CFX_PathData* pClippingPath, int nFlag = 0);
200
201     static FX_BOOL      DrawNormalText(CFX_RenderDevice* pDevice, int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos,
202                                    CPDF_Font* pFont, FX_FLOAT font_size, const CFX_AffineMatrix* pText2Device,
203                                    FX_ARGB fill_argb, const CPDF_RenderOptions* pOptions);
204
205     static FX_BOOL      DrawType3Text(CFX_RenderDevice* pDevice, int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos,
206                                   CPDF_Font* pFont, FX_FLOAT font_size, const CFX_AffineMatrix* pText2Device,
207                                   FX_ARGB fill_argb);
208 };
209 class CPDF_PageRenderCache
210 {
211 public:
212     CPDF_PageRenderCache(CPDF_Page* pPage)
213     {
214         m_pPage = pPage;
215         m_nTimeCount = 0;
216         m_nCacheSize = 0;
217         m_pCurImageCache = NULL;
218         m_bCurFindCache = FALSE;
219         m_pCurImageCaches = NULL;
220     }
221     ~CPDF_PageRenderCache()
222     {
223         ClearAll();
224     }
225     void                                ClearAll();
226     void                                ClearImageData();
227
228     FX_DWORD                    EstimateSize();
229     void                                CacheOptimization(int32_t dwLimitCacheSize);
230     FX_DWORD                    GetCachedSize(CPDF_Stream* pStream) const;
231     FX_DWORD                    GetTimeCount() const
232     {
233         return m_nTimeCount;
234     }
235     void                                SetTimeCount(FX_DWORD dwTimeCount)
236     {
237         m_nTimeCount = dwTimeCount;
238     }
239
240     void                                GetCachedBitmap(CPDF_Stream* pStream, CFX_DIBSource*& pBitmap, CFX_DIBSource*& pMask, FX_DWORD& MatteColor,
241                                         FX_BOOL bStdCS = FALSE, FX_DWORD GroupFamily = 0, FX_BOOL bLoadMask = FALSE,
242                                         CPDF_RenderStatus* pRenderStatus = NULL, int32_t downsampleWidth = 0, int32_t downsampleHeight = 0);
243
244     void                                ResetBitmap(CPDF_Stream* pStream, const CFX_DIBitmap* pBitmap);
245     void                                ClearImageCache(CPDF_Stream* pStream);
246     CPDF_Page*                  GetPage()
247     {
248         return m_pPage;
249     }
250     CFX_MapPtrToPtr             m_ImageCaches;
251 public:
252     FX_BOOL                             StartGetCachedBitmap(CPDF_Stream* pStream, FX_BOOL bStdCS = FALSE, FX_DWORD GroupFamily = 0,
253             FX_BOOL bLoadMask = FALSE, CPDF_RenderStatus* pRenderStatus = NULL,
254             int32_t downsampleWidth = 0, int32_t downsampleHeight = 0);
255
256     FX_BOOL                             Continue(IFX_Pause* pPause);
257     CPDF_ImageCache*    m_pCurImageCache;
258     CFX_PtrArray*       m_pCurImageCaches;
259 protected:
260     friend class                CPDF_Page;
261     CPDF_Page*                  m_pPage;
262
263     FX_DWORD                    m_nTimeCount;
264     FX_DWORD                    m_nCacheSize;
265     FX_BOOL                             m_bCurFindCache;
266 };
267 class CPDF_RenderConfig
268 {
269 public:
270     CPDF_RenderConfig();
271     ~CPDF_RenderConfig();
272     int                                 m_HalftoneLimit;
273     int                                 m_RenderStepLimit;
274 };
275
276 #endif  // CORE_INCLUDE_FPDFAPI_FPDF_RENDER_H_