FX_BOOL not always 0 or 1 in CPDF_PageObjects::m_ParserState
[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 "../../../../third_party/base/nonstd_unique_ptr.h"
13 #include "../../../include/fpdfapi/fpdf_pageobj.h"
14
15 #define PARSE_STEP_LIMIT                100
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_Document* pDoc,
129         CPDF_Dictionary* pPageResources,
130         CPDF_Dictionary* pParentResources,
131         CFX_AffineMatrix* pmtContentToUser,
132         CPDF_PageObjects* pObjList,
133         CPDF_Dictionary* pResources,
134         CFX_FloatRect* pBBox,
135         CPDF_ParseOptions* pOptions,
136         CPDF_AllStates* pAllStates,
137         int level);
138     ~CPDF_StreamContentParser();
139
140     FX_BOOL ShouldAbort() const { return m_bAbort; }
141     CPDF_PageObjects* GetObjectList() const { return m_pObjectList; }
142     CPDF_AllStates* GetCurStates() const { return m_pCurStates.get(); }
143     FX_BOOL IsColored() const { return m_bColored; }
144     const FX_FLOAT* GetType3Data() const { return m_Type3Data; }
145
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_DWORD                    GetParsePos()
162     {
163         return m_pSyntax->GetPos();
164     }
165     void                                AddTextObject(CFX_ByteString* pText, FX_FLOAT fInitKerning, FX_FLOAT* pKerning, int count);
166
167     void                                ConvertUserSpace(FX_FLOAT& x, FX_FLOAT& y);
168     void                                ConvertTextSpace(FX_FLOAT& x, FX_FLOAT& y);
169     void                                OnChangeTextMatrix();
170     FX_DWORD                    Parse(const uint8_t* pData, FX_DWORD dwSize, FX_DWORD max_cost);
171     void                                ParsePathObject();
172     void                                AddPathPoint(FX_FLOAT x, FX_FLOAT y, int flag);
173     void                                AddPathRect(FX_FLOAT x, FX_FLOAT y, FX_FLOAT w, FX_FLOAT h);
174     void                                AddPathObject(int FillType, FX_BOOL bStroke);
175     CPDF_ImageObject*   AddImage(CPDF_Stream* pStream, CPDF_Image* pImage, FX_BOOL bInline);
176     void                                AddDuplicateImage();
177     void                                AddForm(CPDF_Stream*);
178     void                                SetGraphicStates(CPDF_PageObject* pObj, FX_BOOL bColor, FX_BOOL bText, FX_BOOL bGraph);
179     void                                SaveStates(CPDF_AllStates*);
180     void                                RestoreStates(CPDF_AllStates*);
181     CPDF_Font*                  FindFont(const CFX_ByteString& name);
182     CPDF_ColorSpace*    FindColorSpace(const CFX_ByteString& name);
183     CPDF_Pattern*               FindPattern(const CFX_ByteString& name, FX_BOOL bShading);
184     CPDF_Object*                FindResourceObj(const CFX_ByteStringC& type, const CFX_ByteString& name);
185
186 protected:
187     struct OpCode {
188         FX_DWORD m_OpId;
189         void (CPDF_StreamContentParser::*m_OpHandler)();
190     };
191     static const OpCode g_OpCodes[];
192
193     void Handle_CloseFillStrokePath();
194     void Handle_FillStrokePath();
195     void Handle_CloseEOFillStrokePath();
196     void Handle_EOFillStrokePath();
197     void Handle_BeginMarkedContent_Dictionary();
198     void Handle_BeginImage();
199     void Handle_BeginMarkedContent();
200     void Handle_BeginText();
201     void Handle_BeginSectionUndefined();
202     void Handle_CurveTo_123();
203     void Handle_ConcatMatrix();
204     void Handle_SetColorSpace_Fill();
205     void Handle_SetColorSpace_Stroke();
206     void Handle_SetDash();
207     void Handle_SetCharWidth();
208     void Handle_SetCachedDevice();
209     void Handle_ExecuteXObject();
210     void Handle_MarkPlace_Dictionary();
211     void Handle_EndImage();
212     void Handle_EndMarkedContent();
213     void Handle_EndText();
214     void Handle_EndSectionUndefined();
215     void Handle_FillPath();
216     void Handle_FillPathOld();
217     void Handle_EOFillPath();
218     void Handle_SetGray_Fill();
219     void Handle_SetGray_Stroke();
220     void Handle_SetExtendGraphState();
221     void Handle_ClosePath();
222     void Handle_SetFlat();
223     void Handle_BeginImageData();
224     void Handle_SetLineJoin();
225     void Handle_SetLineCap();
226     void Handle_SetCMYKColor_Fill();
227     void Handle_SetCMYKColor_Stroke();
228     void Handle_LineTo();
229     void Handle_MoveTo();
230     void Handle_SetMiterLimit();
231     void Handle_MarkPlace();
232     void Handle_EndPath();
233     void Handle_SaveGraphState();
234     void Handle_RestoreGraphState();
235     void Handle_Rectangle();
236     void Handle_SetRGBColor_Fill();
237     void Handle_SetRGBColor_Stroke();
238     void Handle_SetRenderIntent();
239     void Handle_CloseStrokePath();
240     void Handle_StrokePath();
241     void Handle_SetColor_Fill();
242     void Handle_SetColor_Stroke();
243     void Handle_SetColorPS_Fill();
244     void Handle_SetColorPS_Stroke();
245     void Handle_ShadeFill();
246     void Handle_SetCharSpace();
247     void Handle_MoveTextPoint();
248     void Handle_MoveTextPoint_SetLeading();
249     void Handle_SetFont();
250     void Handle_ShowText();
251     void Handle_ShowText_Positioning();
252     void Handle_SetTextLeading();
253     void Handle_SetTextMatrix();
254     void Handle_SetTextRenderMode();
255     void Handle_SetTextRise();
256     void Handle_SetWordSpace();
257     void Handle_SetHorzScale();
258     void Handle_MoveToNextLine();
259     void Handle_CurveTo_23();
260     void Handle_SetLineWidth();
261     void Handle_Clip();
262     void Handle_EOClip();
263     void Handle_CurveTo_13();
264     void Handle_NextLineShowText();
265     void Handle_NextLineShowText_Space();
266     void Handle_Invalid();
267
268     CPDF_Document* const m_pDocument;
269     CPDF_Dictionary* m_pPageResources;
270     CPDF_Dictionary* m_pParentResources;
271     CPDF_Dictionary* m_pResources;
272     CPDF_PageObjects* m_pObjectList;
273     int m_Level;
274     CFX_AffineMatrix m_mtContentToUser;
275     CFX_FloatRect m_BBox;
276     CPDF_ParseOptions m_Options;
277     _ContentParam m_ParamBuf1[PARAM_BUF_SIZE];
278     FX_DWORD m_ParamStartPos;
279     FX_DWORD m_ParamCount;
280     FX_BOOL m_bAbort;
281     CPDF_StreamParser* m_pSyntax;
282     nonstd::unique_ptr<CPDF_AllStates> m_pCurStates;
283     CPDF_ContentMark m_CurContentMark;
284     CFX_PtrArray m_ClipTextList;
285     CPDF_TextObject* m_pLastTextObject;
286     FX_FLOAT m_DefFontSize;
287     int m_CompatCount;
288     FX_PATHPOINT* m_pPathPoints;
289     int m_PathPointCount;
290     int m_PathAllocSize;
291     FX_FLOAT m_PathStartX;
292     FX_FLOAT m_PathStartY;
293     FX_FLOAT m_PathCurrentX;
294     FX_FLOAT m_PathCurrentY;
295     int m_PathClipType;
296     CFX_ByteString m_LastImageName;
297     CPDF_Image* m_pLastImage;
298     CFX_BinaryBuf m_LastImageDict;
299     CFX_BinaryBuf m_LastImageData;
300     CPDF_Dictionary* m_pLastImageDict;
301     CPDF_Dictionary* m_pLastCloneImageDict;
302     FX_BOOL m_bReleaseLastDict;
303     FX_BOOL m_bSameLastDict;
304     FX_BOOL m_bColored;
305     FX_FLOAT m_Type3Data[6];
306     FX_BOOL m_bResourceMissing;
307     CFX_PtrArray m_StateStack;
308 };
309 class CPDF_ContentParser
310 {
311 public:
312     enum ParseStatus {
313         Ready,
314         ToBeContinued,
315         Done
316     };
317
318     CPDF_ContentParser();
319     ~CPDF_ContentParser();
320
321     ParseStatus                 GetStatus()
322     {
323         return m_Status;
324     }
325     void                                Start(CPDF_Page* pPage, CPDF_ParseOptions* pOptions);
326     void                                Start(CPDF_Form* pForm, CPDF_AllStates* pGraphicStates, CFX_AffineMatrix* pParentMatrix,
327                               CPDF_Type3Char* pType3Char, CPDF_ParseOptions* pOptions, int level);
328     void                                Continue(IFX_Pause* pPause);
329
330 protected:
331     void                                Clear();
332     ParseStatus                 m_Status;
333     CPDF_PageObjects*   m_pObjects;
334     FX_BOOL                             m_bForm;
335     CPDF_ParseOptions   m_Options;
336     CPDF_Type3Char*             m_pType3Char;
337     int                                 m_InternalStage;
338     CPDF_StreamAcc*             m_pSingleStream;
339     CPDF_StreamAcc**    m_pStreamArray;
340     FX_DWORD                    m_nStreams;
341     uint8_t*                    m_pData;
342     FX_DWORD                    m_Size;
343     class CPDF_StreamContentParser*     m_pParser;
344     FX_DWORD                    m_CurrentOffset;
345     CPDF_StreamFilter*  m_pStreamFilter;
346 };
347 class CPDF_AllStates : public CPDF_GraphicStates
348 {
349 public:
350     CPDF_AllStates();
351     ~CPDF_AllStates();
352     void        Copy(const CPDF_AllStates& src);
353     void        ProcessExtGS(CPDF_Dictionary* pGS, CPDF_StreamContentParser* pParser);
354     void        SetLineDash(CPDF_Array*, FX_FLOAT, FX_FLOAT scale);
355     CFX_AffineMatrix            m_TextMatrix, m_CTM, m_ParentMatrix;
356     FX_FLOAT                            m_TextX, m_TextY, m_TextLineX, m_TextLineY;
357     FX_FLOAT                            m_TextLeading, m_TextRise, m_TextHorzScale;
358 };
359
360 class CPDF_DocPageData
361 {
362   public:
363     explicit CPDF_DocPageData(CPDF_Document *pPDFDoc);
364     ~CPDF_DocPageData();
365
366     void                        Clear(FX_BOOL bRelease = FALSE);
367     CPDF_Font*                  GetFont(CPDF_Dictionary* pFontDict, FX_BOOL findOnly);
368     CPDF_Font*                  GetStandardFont(const CFX_ByteStringC& fontName, CPDF_FontEncoding* pEncoding);
369     void                        ReleaseFont(CPDF_Dictionary* pFontDict);
370     CPDF_ColorSpace*            GetColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources);
371     CPDF_ColorSpace*            GetCopiedColorSpace(CPDF_Object* pCSObj);
372     void                        ReleaseColorSpace(CPDF_Object* pColorSpace);
373     CPDF_Pattern*               GetPattern(CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* matrix);
374     void                        ReleasePattern(CPDF_Object* pPatternObj);
375     CPDF_Image*                 GetImage(CPDF_Object* pImageStream);
376     void                        ReleaseImage(CPDF_Object* pImageStream);
377     CPDF_IccProfile*            GetIccProfile(CPDF_Stream* pIccProfileStream);
378     void                        ReleaseIccProfile(CPDF_IccProfile* pIccProfile);
379     CPDF_StreamAcc*             GetFontFileStreamAcc(CPDF_Stream* pFontStream);
380     void                        ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOOL bForce = FALSE);
381     FX_BOOL                     IsForceClear() const {return m_bForceClear;}
382     CPDF_CountedColorSpace*     FindColorSpacePtr(CPDF_Object* pCSObj) const;
383     CPDF_CountedPattern*        FindPatternPtr(CPDF_Object* pPatternObj) const;
384
385   private:
386     using CPDF_CountedFont = CPDF_CountedObject<CPDF_Font>;
387     using CPDF_CountedIccProfile = CPDF_CountedObject<CPDF_IccProfile>;
388     using CPDF_CountedImage = CPDF_CountedObject<CPDF_Image>;
389     using CPDF_CountedStreamAcc = CPDF_CountedObject<CPDF_StreamAcc>;
390
391     using CPDF_ColorSpaceMap = std::map<CPDF_Object*, CPDF_CountedColorSpace*>;
392     using CPDF_FontFileMap = std::map<CPDF_Stream*, CPDF_CountedStreamAcc*>;
393     using CPDF_FontMap = std::map<CPDF_Dictionary*, CPDF_CountedFont*>;
394     using CPDF_IccProfileMap = std::map<CPDF_Stream*, CPDF_CountedIccProfile*>;
395     using CPDF_ImageMap = std::map<FX_DWORD, CPDF_CountedImage*>;
396     using CPDF_PatternMap = std::map<CPDF_Object*, CPDF_CountedPattern*>;
397
398     CPDF_Document* const m_pPDFDoc;
399     CFX_MapByteStringToPtr m_HashProfileMap;
400     FX_BOOL m_bForceClear;
401
402     CPDF_ColorSpaceMap m_ColorSpaceMap;
403     CPDF_FontFileMap m_FontFileMap;
404     CPDF_FontMap m_FontMap;
405     CPDF_IccProfileMap m_IccProfileMap;
406     CPDF_ImageMap m_ImageMap;
407     CPDF_PatternMap m_PatternMap;
408 };
409
410 class CPDF_Function
411 {
412 public:
413     static CPDF_Function*       Load(CPDF_Object* pFuncObj);
414     virtual ~CPDF_Function();
415     FX_BOOL             Call(FX_FLOAT* inputs, int ninputs, FX_FLOAT* results, int& nresults) const;
416     int                 CountInputs()
417     {
418         return m_nInputs;
419     }
420     int                 CountOutputs()
421     {
422         return m_nOutputs;
423     }
424 protected:
425     CPDF_Function();
426     int                 m_nInputs, m_nOutputs;
427     FX_FLOAT*   m_pDomains;
428     FX_FLOAT*   m_pRanges;
429     FX_BOOL             Init(CPDF_Object* pObj);
430     virtual FX_BOOL     v_Init(CPDF_Object* pObj) = 0;
431     virtual FX_BOOL     v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const = 0;
432 };
433 class CPDF_IccProfile
434 {
435 public:
436     CPDF_IccProfile(const uint8_t* pData, FX_DWORD dwSize);
437     ~CPDF_IccProfile();
438     int32_t GetComponents() const { return m_nSrcComponents; }
439     FX_BOOL                                     m_bsRGB;
440     void*                               m_pTransform;
441 private:
442     int32_t                m_nSrcComponents;
443 };
444
445 class CPDF_DeviceCS : public CPDF_ColorSpace
446 {
447 public:
448     CPDF_DeviceCS(CPDF_Document* pDoc, int family);
449
450     FX_BOOL GetRGB(FX_FLOAT* pBuf,
451                    FX_FLOAT& R,
452                    FX_FLOAT& G,
453                    FX_FLOAT& B) const override;
454     FX_BOOL SetRGB(FX_FLOAT* pBuf,
455                    FX_FLOAT R,
456                    FX_FLOAT G,
457                    FX_FLOAT B) const override;
458     FX_BOOL v_GetCMYK(FX_FLOAT* pBuf,
459                       FX_FLOAT& c,
460                       FX_FLOAT& m,
461                       FX_FLOAT& y,
462                       FX_FLOAT& k) const override;
463     FX_BOOL v_SetCMYK(FX_FLOAT* pBuf,
464                       FX_FLOAT c,
465                       FX_FLOAT m,
466                       FX_FLOAT y,
467                       FX_FLOAT k) const override;
468     void TranslateImageLine(uint8_t* pDestBuf,
469                             const uint8_t* pSrcBuf,
470                             int pixels,
471                             int image_width,
472                             int image_height,
473                             FX_BOOL bTransMask = FALSE) const override;
474 };
475
476 class CPDF_PatternCS : public CPDF_ColorSpace
477 {
478 public:
479     explicit CPDF_PatternCS(CPDF_Document* pDoc)
480         : CPDF_ColorSpace(pDoc, PDFCS_PATTERN, 1),
481           m_pBaseCS(nullptr),
482           m_pCountedBaseCS(nullptr) {
483     }
484     ~CPDF_PatternCS() override;
485     FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override;
486     FX_BOOL GetRGB(FX_FLOAT* pBuf,
487                    FX_FLOAT& R,
488                    FX_FLOAT& G,
489                    FX_FLOAT& B) const override;
490     CPDF_ColorSpace* GetBaseCS() const override;
491
492 private:
493     CPDF_ColorSpace* m_pBaseCS;
494     CPDF_CountedColorSpace* m_pCountedBaseCS;
495 };
496
497 #endif  // CORE_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_