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