Convert CPDF_ImageMap to std::map.
[pdfium.git] / core / src / fpdfapi / fpdf_page / pageint.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_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_
8 #define CORE_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_
9
10 #include <map>
11
12 #include "../../../include/fpdfapi/fpdf_pageobj.h"
13
14 #define PARSE_STEP_LIMIT                100
15
16 class CPDF_StreamParser
17 {
18 public:
19
20     CPDF_StreamParser(const uint8_t* pData, FX_DWORD dwSize);
21     ~CPDF_StreamParser();
22
23     CPDF_Stream*                ReadInlineStream(CPDF_Document* pDoc, CPDF_Dictionary* pDict, CPDF_Object* pCSObj, FX_BOOL bDecode);
24     typedef enum { EndOfData, Number, Keyword, Name, Others } SyntaxType;
25
26     SyntaxType                  ParseNextElement();
27     uint8_t*                    GetWordBuf()
28     {
29         return m_WordBuffer;
30     }
31     FX_DWORD                    GetWordSize()
32     {
33         return m_WordSize;
34     }
35     CPDF_Object*                GetObject()
36     {
37         CPDF_Object* pObj = m_pLastObj;
38         m_pLastObj = NULL;
39         return pObj;
40     }
41     FX_DWORD                    GetPos()
42     {
43         return m_Pos;
44     }
45     void                                SetPos(FX_DWORD pos)
46     {
47         m_Pos = pos;
48     }
49
50     CPDF_Object*                ReadNextObject(FX_BOOL bAllowNestedArray = FALSE, FX_BOOL bInArray = FALSE);
51     void                                SkipPathObject();
52 protected:
53     void                                GetNextWord(FX_BOOL& bIsNumber);
54     CFX_ByteString              ReadString();
55     CFX_ByteString              ReadHexString();
56     const uint8_t*              m_pBuf;
57     FX_DWORD                    m_Size;
58     FX_DWORD                    m_Pos;
59     uint8_t                             m_WordBuffer[256];
60     FX_DWORD                    m_WordSize;
61     CPDF_Object*                m_pLastObj;
62 };
63 typedef enum {
64     PDFOP_CloseFillStrokePath = 0, PDFOP_FillStrokePath,
65     PDFOP_CloseEOFillStrokePath, PDFOP_EOFillStrokePath,
66     PDFOP_BeginMarkedContent_Dictionary, PDFOP_BeginImage,
67     PDFOP_BeginMarkedContent, PDFOP_BeginText,
68     PDFOP_BeginSectionUndefined, PDFOP_CurveTo_123,
69     PDFOP_ConcatMatrix, PDFOP_SetColorSpace_Fill,
70     PDFOP_SetColorSpace_Stroke, PDFOP_SetDash,
71     PDFOP_SetCharWidth, PDFOP_SetCachedDevice,
72     PDFOP_ExecuteXObject, PDFOP_MarkPlace_Dictionary,
73     PDFOP_EndImage, PDFOP_EndMarkedContent,
74     PDFOP_EndText, PDFOP_EndSectionUndefined,
75     PDFOP_FillPath, PDFOP_FillPathOld,
76     PDFOP_EOFillPath, PDFOP_SetGray_Fill,
77     PDFOP_SetGray_Stroke, PDFOP_SetExtendGraphState,
78     PDFOP_ClosePath, PDFOP_SetFlat,
79     PDFOP_BeginImageData, PDFOP_SetLineJoin,
80     PDFOP_SetLineCap, PDFOP_SetCMYKColor_Fill,
81     PDFOP_SetCMYKColor_Stroke, PDFOP_LineTo,
82     PDFOP_MoveTo, PDFOP_SetMiterLimit,
83     PDFOP_MarkPlace, PDFOP_EndPath,
84     PDFOP_SaveGraphState, PDFOP_RestoreGraphState,
85     PDFOP_Rectangle, PDFOP_SetRGBColor_Fill,
86     PDFOP_SetRGBColor_Stroke, PDFOP_SetRenderIntent,
87     PDFOP_CloseStrokePath, PDFOP_StrokePath,
88     PDFOP_SetColor_Fill, PDFOP_SetColor_Stroke,
89     PDFOP_SetColorPS_Fill, PDFOP_SetColorPS_Stroke,
90     PDFOP_ShadeFill, PDFOP_SetCharSpace,
91     PDFOP_MoveTextPoint, PDFOP_MoveTextPoint_SetLeading,
92     PDFOP_SetFont, PDFOP_ShowText,
93     PDFOP_ShowText_Positioning, PDFOP_SetTextLeading,
94     PDFOP_SetTextMatrix, PDFOP_SetTextRenderMode,
95     PDFOP_SetTextRise, PDFOP_SetWordSpace,
96     PDFOP_SetHorzScale, PDFOP_MoveToNextLine,
97     PDFOP_CurveTo_23, PDFOP_SetLineWidth,
98     PDFOP_Clip, PDFOP_EOClip,
99     PDFOP_CurveTo_13, PDFOP_NextLineShowText,
100     PDFOP_NextLineShowText_Space, PDFOP_Invalid
101 } PDFOP;
102 #define PARAM_BUF_SIZE  16
103 typedef struct {
104     int                 m_Type;
105     union {
106         struct {
107             FX_BOOL             m_bInteger;
108             union {
109                 int             m_Integer;
110                 FX_FLOAT m_Float;
111             };
112         } m_Number;
113         CPDF_Object*    m_pObject;
114         struct {
115             int                 m_Len;
116             char                m_Buffer[32];
117         } m_Name;
118     };
119 } _ContentParam;
120 #define _FPDF_MAX_FORM_LEVEL_           30
121 #define _FPDF_MAX_TYPE3_FORM_LEVEL_     4
122 #define _FPDF_MAX_OBJECT_STACK_SIZE_ 512
123 class CPDF_StreamContentParser
124 {
125 public:
126     CPDF_StreamContentParser();
127     ~CPDF_StreamContentParser();
128     FX_BOOL Initialize();
129     void        PrepareParse(CPDF_Document* pDoc, CPDF_Dictionary* pPageResources, CPDF_Dictionary* pParentResources,
130                          CFX_AffineMatrix* pmtContentToUser,
131                          CPDF_PageObjects* pObjList, CPDF_Dictionary* pResources,
132                          CFX_FloatRect* pBBox, CPDF_ParseOptions* pOptions,
133                          CPDF_AllStates* pAllStates, int level);
134     CPDF_Document*              m_pDocument;
135     CPDF_Dictionary*    m_pPageResources;
136     CPDF_Dictionary*    m_pParentResources;
137     CPDF_PageObjects*   m_pObjectList;
138     CPDF_Dictionary*    m_pResources;
139     int                                 m_Level;
140     CFX_AffineMatrix    m_mtContentToUser;
141     CFX_FloatRect               m_BBox;
142     CPDF_ParseOptions   m_Options;
143     _ContentParam               m_ParamBuf1[PARAM_BUF_SIZE];
144     FX_DWORD                    m_ParamStartPos;
145     FX_DWORD                    m_ParamCount;
146     void                                AddNumberParam(const FX_CHAR* str, int len);
147     void                                AddObjectParam(CPDF_Object* pObj);
148     void                                AddNameParam(const FX_CHAR* name, int size);
149     int                                 GetNextParamPos();
150     void                                ClearAllParams();
151     CPDF_Object*                GetObject(FX_DWORD index);
152     CFX_ByteString              GetString(FX_DWORD index);
153     FX_FLOAT                    GetNumber(FX_DWORD index);
154     FX_FLOAT            GetNumber16(FX_DWORD index);
155     int                                 GetInteger(FX_DWORD index)
156     {
157         return (int32_t)(GetNumber(index));
158     }
159     FX_BOOL                             OnOperator(const FX_CHAR* op);
160     void                                BigCaseCaller(int index);
161     FX_BOOL                             m_bAbort;
162     CPDF_StreamParser*  m_pSyntax;
163     FX_DWORD                    GetParsePos()
164     {
165         return m_pSyntax->GetPos();
166     }
167     CPDF_AllStates*             m_pCurStates;
168     CPDF_ContentMark    m_CurContentMark;
169     CFX_PtrArray                m_ClipTextList;
170     CPDF_TextObject*    m_pLastTextObject;
171     FX_FLOAT                    m_DefFontSize;
172     void                                AddTextObject(CFX_ByteString* pText, FX_FLOAT fInitKerning, FX_FLOAT* pKerning, int count);
173
174     void                                ConvertUserSpace(FX_FLOAT& x, FX_FLOAT& y);
175     void                                ConvertTextSpace(FX_FLOAT& x, FX_FLOAT& y);
176     void                                OnChangeTextMatrix();
177     FX_DWORD                    Parse(const uint8_t* pData, FX_DWORD dwSize, FX_DWORD max_cost);
178     void                                ParsePathObject();
179     int                                 m_CompatCount;
180     FX_PATHPOINT*               m_pPathPoints;
181     int                                 m_PathPointCount;
182     int                                 m_PathAllocSize;
183     FX_FLOAT                    m_PathStartX, m_PathStartY;
184     FX_FLOAT                    m_PathCurrentX, m_PathCurrentY;
185     int                                 m_PathClipType;
186     void                                AddPathPoint(FX_FLOAT x, FX_FLOAT y, int flag);
187     void                                AddPathRect(FX_FLOAT x, FX_FLOAT y, FX_FLOAT w, FX_FLOAT h);
188     void                                AddPathObject(int FillType, FX_BOOL bStroke);
189     CPDF_ImageObject*   AddImage(CPDF_Stream* pStream, CPDF_Image* pImage, FX_BOOL bInline);
190     void                                AddDuplicateImage();
191     void                                AddForm(CPDF_Stream*);
192     CFX_ByteString              m_LastImageName;
193     CPDF_Image*                 m_pLastImage;
194     CFX_BinaryBuf               m_LastImageDict, m_LastImageData;
195     CPDF_Dictionary*    m_pLastImageDict;
196     CPDF_Dictionary*    m_pLastCloneImageDict;
197     FX_BOOL                             m_bReleaseLastDict;
198     FX_BOOL                             m_bSameLastDict;
199     void                                SetGraphicStates(CPDF_PageObject* pObj, FX_BOOL bColor, FX_BOOL bText, FX_BOOL bGraph);
200     FX_BOOL                             m_bColored;
201     FX_FLOAT                    m_Type3Data[6];
202     FX_BOOL                             m_bResourceMissing;
203     CFX_PtrArray                m_StateStack;
204     void                                SaveStates(CPDF_AllStates*);
205     void                                RestoreStates(CPDF_AllStates*);
206     CPDF_Font*                  FindFont(const CFX_ByteString& name);
207     CPDF_ColorSpace*    FindColorSpace(const CFX_ByteString& name);
208     CPDF_Pattern*               FindPattern(const CFX_ByteString& name, FX_BOOL bShading);
209     CPDF_Object*                FindResourceObj(FX_BSTR type, const CFX_ByteString& name);
210     void Handle_CloseFillStrokePath();
211     void Handle_FillStrokePath();
212     void Handle_CloseEOFillStrokePath();
213     void Handle_EOFillStrokePath();
214     void Handle_BeginMarkedContent_Dictionary();
215     void Handle_BeginImage();
216     void Handle_BeginMarkedContent();
217     void Handle_BeginText();
218     void Handle_BeginSectionUndefined();
219     void Handle_CurveTo_123();
220     void Handle_ConcatMatrix();
221     void Handle_SetColorSpace_Fill();
222     void Handle_SetColorSpace_Stroke();
223     void Handle_SetDash();
224     void Handle_SetCharWidth();
225     void Handle_SetCachedDevice();
226     void Handle_ExecuteXObject();
227     void Handle_MarkPlace_Dictionary();
228     void Handle_EndImage();
229     void Handle_EndMarkedContent();
230     void Handle_EndText();
231     void Handle_EndSectionUndefined();
232     void Handle_FillPath();
233     void Handle_FillPathOld();
234     void Handle_EOFillPath();
235     void Handle_SetGray_Fill();
236     void Handle_SetGray_Stroke();
237     void Handle_SetExtendGraphState();
238     void Handle_ClosePath();
239     void Handle_SetFlat();
240     void Handle_BeginImageData();
241     void Handle_SetLineJoin();
242     void Handle_SetLineCap();
243     void Handle_SetCMYKColor_Fill();
244     void Handle_SetCMYKColor_Stroke();
245     void Handle_LineTo();
246     void Handle_MoveTo();
247     void Handle_SetMiterLimit();
248     void Handle_MarkPlace();
249     void Handle_EndPath();
250     void Handle_SaveGraphState();
251     void Handle_RestoreGraphState();
252     void Handle_Rectangle();
253     void Handle_SetRGBColor_Fill();
254     void Handle_SetRGBColor_Stroke();
255     void Handle_SetRenderIntent();
256     void Handle_CloseStrokePath();
257     void Handle_StrokePath();
258     void Handle_SetColor_Fill();
259     void Handle_SetColor_Stroke();
260     void Handle_SetColorPS_Fill();
261     void Handle_SetColorPS_Stroke();
262     void Handle_ShadeFill();
263     void Handle_SetCharSpace();
264     void Handle_MoveTextPoint();
265     void Handle_MoveTextPoint_SetLeading();
266     void Handle_SetFont();
267     void Handle_ShowText();
268     void Handle_ShowText_Positioning();
269     void Handle_SetTextLeading();
270     void Handle_SetTextMatrix();
271     void Handle_SetTextRenderMode();
272     void Handle_SetTextRise();
273     void Handle_SetWordSpace();
274     void Handle_SetHorzScale();
275     void Handle_MoveToNextLine();
276     void Handle_CurveTo_23();
277     void Handle_SetLineWidth();
278     void Handle_Clip();
279     void Handle_EOClip();
280     void Handle_CurveTo_13();
281     void Handle_NextLineShowText();
282     void Handle_NextLineShowText_Space();
283     void Handle_Invalid();
284 };
285 class CPDF_ContentParser
286 {
287 public:
288     CPDF_ContentParser();
289     ~CPDF_ContentParser();
290     typedef enum { Ready, ToBeContinued, Done } ParseStatus;
291     ParseStatus                 GetStatus()
292     {
293         return m_Status;
294     }
295     void                                Start(CPDF_Page* pPage, CPDF_ParseOptions* pOptions);
296     void                                Start(CPDF_Form* pForm, CPDF_AllStates* pGraphicStates, CFX_AffineMatrix* pParentMatrix,
297                               CPDF_Type3Char* pType3Char, CPDF_ParseOptions* pOptions, int level);
298     void                                Continue(IFX_Pause* pPause);
299     int                                 EstimateProgress();
300 protected:
301     void                                Clear();
302     ParseStatus                 m_Status;
303     CPDF_PageObjects*   m_pObjects;
304     FX_BOOL                             m_bForm;
305     CPDF_ParseOptions   m_Options;
306     CPDF_Type3Char*             m_pType3Char;
307     int                                 m_InternalStage;
308     CPDF_StreamAcc*             m_pSingleStream;
309     CPDF_StreamAcc**    m_pStreamArray;
310     FX_DWORD                    m_nStreams;
311     uint8_t*                    m_pData;
312     FX_DWORD                    m_Size;
313     class CPDF_StreamContentParser*     m_pParser;
314     FX_DWORD                    m_CurrentOffset;
315     CPDF_StreamFilter*  m_pStreamFilter;
316 };
317 class CPDF_AllStates : public CPDF_GraphicStates
318 {
319 public:
320     CPDF_AllStates();
321     ~CPDF_AllStates();
322     void        Copy(const CPDF_AllStates& src);
323     void        ProcessExtGS(CPDF_Dictionary* pGS, CPDF_StreamContentParser* pParser);
324     void        SetLineDash(CPDF_Array*, FX_FLOAT, FX_FLOAT scale);
325     CFX_AffineMatrix            m_TextMatrix, m_CTM, m_ParentMatrix;
326     FX_FLOAT                            m_TextX, m_TextY, m_TextLineX, m_TextLineY;
327     FX_FLOAT                            m_TextLeading, m_TextRise, m_TextHorzScale;
328 };
329
330 class CPDF_DocPageData
331 {
332   public:
333     explicit CPDF_DocPageData(CPDF_Document *pPDFDoc);
334     ~CPDF_DocPageData();
335
336     void                        Clear(FX_BOOL bRelease = FALSE);
337     CPDF_Font*                  GetFont(CPDF_Dictionary* pFontDict, FX_BOOL findOnly);
338     CPDF_Font*                  GetStandardFont(FX_BSTR fontName, CPDF_FontEncoding* pEncoding);
339     void                        ReleaseFont(CPDF_Dictionary* pFontDict);
340     CPDF_ColorSpace*            GetColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources);
341     CPDF_ColorSpace*            GetCopiedColorSpace(CPDF_Object* pCSObj);
342     void                        ReleaseColorSpace(CPDF_Object* pColorSpace);
343     CPDF_Pattern*               GetPattern(CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* matrix);
344     void                        ReleasePattern(CPDF_Object* pPatternObj);
345     CPDF_Image*                 GetImage(CPDF_Object* pImageStream);
346     void                        ReleaseImage(CPDF_Object* pImageStream);
347     CPDF_IccProfile*            GetIccProfile(CPDF_Stream* pIccProfileStream);
348     void                        ReleaseIccProfile(CPDF_IccProfile* pIccProfile);
349     CPDF_StreamAcc*             GetFontFileStreamAcc(CPDF_Stream* pFontStream);
350     void                        ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOOL bForce = FALSE);
351     FX_BOOL                     IsForceClear() const {return m_bForceClear;}
352     CPDF_CountedColorSpace*     FindColorSpacePtr(CPDF_Object* pCSObj) const;
353     CPDF_CountedPattern*        FindPatternPtr(CPDF_Object* pPatternObj) const;
354
355     CPDF_Document*              m_pPDFDoc;
356     CPDF_IccProfileMap          m_IccProfileMap;
357     CFX_MapByteStringToPtr      m_HashProfileMap;
358     CPDF_FontFileMap            m_FontFileMap;
359     FX_BOOL                     m_bForceClear;
360
361   private:
362     using CPDF_CountedFont = CPDF_CountedObject<CPDF_Font*>;
363     using CPDF_CountedImage = CPDF_CountedObject<CPDF_Image*>;
364
365     using CPDF_ColorSpaceMap = std::map<CPDF_Object*, CPDF_CountedColorSpace*>;
366     using CPDF_FontMap = std::map<CPDF_Dictionary*, CPDF_CountedFont*>;
367     using CPDF_ImageMap = std::map<FX_DWORD, CPDF_CountedImage*>;
368     using CPDF_PatternMap = std::map<CPDF_Object*, CPDF_CountedPattern*>;
369
370     CPDF_ColorSpaceMap          m_ColorSpaceMap;
371     CPDF_FontMap                m_FontMap;
372     CPDF_ImageMap               m_ImageMap;
373     CPDF_PatternMap             m_PatternMap;
374 };
375
376 class CPDF_Function
377 {
378 public:
379     static CPDF_Function*       Load(CPDF_Object* pFuncObj);
380     virtual ~CPDF_Function();
381     FX_BOOL             Call(FX_FLOAT* inputs, int ninputs, FX_FLOAT* results, int& nresults) const;
382     int                 CountInputs()
383     {
384         return m_nInputs;
385     }
386     int                 CountOutputs()
387     {
388         return m_nOutputs;
389     }
390 protected:
391     CPDF_Function();
392     int                 m_nInputs, m_nOutputs;
393     FX_FLOAT*   m_pDomains;
394     FX_FLOAT*   m_pRanges;
395     FX_BOOL             Init(CPDF_Object* pObj);
396     virtual FX_BOOL     v_Init(CPDF_Object* pObj) = 0;
397     virtual FX_BOOL     v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const = 0;
398 };
399 class CPDF_IccProfile
400 {
401 public:
402     CPDF_IccProfile(const uint8_t* pData, FX_DWORD dwSize);
403     ~CPDF_IccProfile();
404     int32_t GetComponents() const { return m_nSrcComponents; }
405     FX_BOOL                                     m_bsRGB;
406     void*                               m_pTransform;
407 private:
408     int32_t                m_nSrcComponents;
409 };
410 class CPDF_DeviceCS : public CPDF_ColorSpace
411 {
412 public:
413     CPDF_DeviceCS(int family);
414     virtual FX_BOOL     GetRGB(FX_FLOAT* pBuf, FX_FLOAT& R, FX_FLOAT& G, FX_FLOAT& B) const;
415     FX_BOOL     SetRGB(FX_FLOAT* pBuf, FX_FLOAT R, FX_FLOAT G, FX_FLOAT B) const;
416     FX_BOOL     v_GetCMYK(FX_FLOAT* pBuf, FX_FLOAT& c, FX_FLOAT& m, FX_FLOAT& y, FX_FLOAT& k) const;
417     FX_BOOL     v_SetCMYK(FX_FLOAT* pBuf, FX_FLOAT c, FX_FLOAT m, FX_FLOAT y, FX_FLOAT k) const;
418     virtual void        TranslateImageLine(uint8_t* pDestBuf, const uint8_t* pSrcBuf, int pixels, int image_width, int image_height, FX_BOOL bTransMask = FALSE) const;
419 };
420 class CPDF_PatternCS : public CPDF_ColorSpace
421 {
422 public:
423     CPDF_PatternCS();
424     ~CPDF_PatternCS();
425     virtual FX_BOOL             v_Load(CPDF_Document* pDoc, CPDF_Array* pArray);
426     virtual FX_BOOL             GetRGB(FX_FLOAT* pBuf, FX_FLOAT& R, FX_FLOAT& G, FX_FLOAT& B) const;
427     virtual CPDF_ColorSpace*    GetBaseCS() const
428     {
429         return m_pBaseCS;
430     }
431     CPDF_ColorSpace*    m_pBaseCS;
432     CPDF_CountedColorSpace*     m_pCountedBaseCS;
433 };
434 #define MAX_PAGE_OBJECTS_UNIFY_NAMING                           4096
435 class CPDF_ResourceNaming
436 {
437 public:
438     struct _NamingState  {
439         CFX_ByteString  m_Prefix;
440         int                             m_nIndex;
441     };
442     ~CPDF_ResourceNaming();
443     CFX_ByteString              GetName(const CPDF_Dictionary* pResList, const FX_CHAR* szType);
444 protected:
445     CFX_MapByteStringToPtr      m_NamingCache;
446 };
447
448 #endif  // CORE_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_