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