Make ContentStreamParser members private, fix constructor
[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     CPDF_ContentParser();
313     ~CPDF_ContentParser();
314     typedef enum { Ready, ToBeContinued, Done } ParseStatus;
315     ParseStatus                 GetStatus()
316     {
317         return m_Status;
318     }
319     void                                Start(CPDF_Page* pPage, CPDF_ParseOptions* pOptions);
320     void                                Start(CPDF_Form* pForm, CPDF_AllStates* pGraphicStates, CFX_AffineMatrix* pParentMatrix,
321                               CPDF_Type3Char* pType3Char, CPDF_ParseOptions* pOptions, int level);
322     void                                Continue(IFX_Pause* pPause);
323     int                                 EstimateProgress();
324 protected:
325     void                                Clear();
326     ParseStatus                 m_Status;
327     CPDF_PageObjects*   m_pObjects;
328     FX_BOOL                             m_bForm;
329     CPDF_ParseOptions   m_Options;
330     CPDF_Type3Char*             m_pType3Char;
331     int                                 m_InternalStage;
332     CPDF_StreamAcc*             m_pSingleStream;
333     CPDF_StreamAcc**    m_pStreamArray;
334     FX_DWORD                    m_nStreams;
335     uint8_t*                    m_pData;
336     FX_DWORD                    m_Size;
337     class CPDF_StreamContentParser*     m_pParser;
338     FX_DWORD                    m_CurrentOffset;
339     CPDF_StreamFilter*  m_pStreamFilter;
340 };
341 class CPDF_AllStates : public CPDF_GraphicStates
342 {
343 public:
344     CPDF_AllStates();
345     ~CPDF_AllStates();
346     void        Copy(const CPDF_AllStates& src);
347     void        ProcessExtGS(CPDF_Dictionary* pGS, CPDF_StreamContentParser* pParser);
348     void        SetLineDash(CPDF_Array*, FX_FLOAT, FX_FLOAT scale);
349     CFX_AffineMatrix            m_TextMatrix, m_CTM, m_ParentMatrix;
350     FX_FLOAT                            m_TextX, m_TextY, m_TextLineX, m_TextLineY;
351     FX_FLOAT                            m_TextLeading, m_TextRise, m_TextHorzScale;
352 };
353
354 class CPDF_DocPageData
355 {
356   public:
357     explicit CPDF_DocPageData(CPDF_Document *pPDFDoc);
358     ~CPDF_DocPageData();
359
360     void                        Clear(FX_BOOL bRelease = FALSE);
361     CPDF_Font*                  GetFont(CPDF_Dictionary* pFontDict, FX_BOOL findOnly);
362     CPDF_Font*                  GetStandardFont(const CFX_ByteStringC& fontName, CPDF_FontEncoding* pEncoding);
363     void                        ReleaseFont(CPDF_Dictionary* pFontDict);
364     CPDF_ColorSpace*            GetColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources);
365     CPDF_ColorSpace*            GetCopiedColorSpace(CPDF_Object* pCSObj);
366     void                        ReleaseColorSpace(CPDF_Object* pColorSpace);
367     CPDF_Pattern*               GetPattern(CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* matrix);
368     void                        ReleasePattern(CPDF_Object* pPatternObj);
369     CPDF_Image*                 GetImage(CPDF_Object* pImageStream);
370     void                        ReleaseImage(CPDF_Object* pImageStream);
371     CPDF_IccProfile*            GetIccProfile(CPDF_Stream* pIccProfileStream);
372     void                        ReleaseIccProfile(CPDF_IccProfile* pIccProfile);
373     CPDF_StreamAcc*             GetFontFileStreamAcc(CPDF_Stream* pFontStream);
374     void                        ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOOL bForce = FALSE);
375     FX_BOOL                     IsForceClear() const {return m_bForceClear;}
376     CPDF_CountedColorSpace*     FindColorSpacePtr(CPDF_Object* pCSObj) const;
377     CPDF_CountedPattern*        FindPatternPtr(CPDF_Object* pPatternObj) const;
378
379   private:
380     using CPDF_CountedFont = CPDF_CountedObject<CPDF_Font>;
381     using CPDF_CountedIccProfile = CPDF_CountedObject<CPDF_IccProfile>;
382     using CPDF_CountedImage = CPDF_CountedObject<CPDF_Image>;
383     using CPDF_CountedStreamAcc = CPDF_CountedObject<CPDF_StreamAcc>;
384
385     using CPDF_ColorSpaceMap = std::map<CPDF_Object*, CPDF_CountedColorSpace*>;
386     using CPDF_FontFileMap = std::map<CPDF_Stream*, CPDF_CountedStreamAcc*>;
387     using CPDF_FontMap = std::map<CPDF_Dictionary*, CPDF_CountedFont*>;
388     using CPDF_IccProfileMap = std::map<CPDF_Stream*, CPDF_CountedIccProfile*>;
389     using CPDF_ImageMap = std::map<FX_DWORD, CPDF_CountedImage*>;
390     using CPDF_PatternMap = std::map<CPDF_Object*, CPDF_CountedPattern*>;
391
392     CPDF_Document* const m_pPDFDoc;
393     CFX_MapByteStringToPtr m_HashProfileMap;
394     FX_BOOL m_bForceClear;
395
396     CPDF_ColorSpaceMap m_ColorSpaceMap;
397     CPDF_FontFileMap m_FontFileMap;
398     CPDF_FontMap m_FontMap;
399     CPDF_IccProfileMap m_IccProfileMap;
400     CPDF_ImageMap m_ImageMap;
401     CPDF_PatternMap m_PatternMap;
402 };
403
404 class CPDF_Function
405 {
406 public:
407     static CPDF_Function*       Load(CPDF_Object* pFuncObj);
408     virtual ~CPDF_Function();
409     FX_BOOL             Call(FX_FLOAT* inputs, int ninputs, FX_FLOAT* results, int& nresults) const;
410     int                 CountInputs()
411     {
412         return m_nInputs;
413     }
414     int                 CountOutputs()
415     {
416         return m_nOutputs;
417     }
418 protected:
419     CPDF_Function();
420     int                 m_nInputs, m_nOutputs;
421     FX_FLOAT*   m_pDomains;
422     FX_FLOAT*   m_pRanges;
423     FX_BOOL             Init(CPDF_Object* pObj);
424     virtual FX_BOOL     v_Init(CPDF_Object* pObj) = 0;
425     virtual FX_BOOL     v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const = 0;
426 };
427 class CPDF_IccProfile
428 {
429 public:
430     CPDF_IccProfile(const uint8_t* pData, FX_DWORD dwSize);
431     ~CPDF_IccProfile();
432     int32_t GetComponents() const { return m_nSrcComponents; }
433     FX_BOOL                                     m_bsRGB;
434     void*                               m_pTransform;
435 private:
436     int32_t                m_nSrcComponents;
437 };
438
439 class CPDF_DeviceCS : public CPDF_ColorSpace
440 {
441 public:
442     CPDF_DeviceCS(CPDF_Document* pDoc, int family);
443
444     FX_BOOL GetRGB(FX_FLOAT* pBuf,
445                    FX_FLOAT& R,
446                    FX_FLOAT& G,
447                    FX_FLOAT& B) const override;
448     FX_BOOL SetRGB(FX_FLOAT* pBuf,
449                    FX_FLOAT R,
450                    FX_FLOAT G,
451                    FX_FLOAT B) const override;
452     FX_BOOL v_GetCMYK(FX_FLOAT* pBuf,
453                       FX_FLOAT& c,
454                       FX_FLOAT& m,
455                       FX_FLOAT& y,
456                       FX_FLOAT& k) const override;
457     FX_BOOL v_SetCMYK(FX_FLOAT* pBuf,
458                       FX_FLOAT c,
459                       FX_FLOAT m,
460                       FX_FLOAT y,
461                       FX_FLOAT k) const override;
462     void TranslateImageLine(uint8_t* pDestBuf,
463                             const uint8_t* pSrcBuf,
464                             int pixels,
465                             int image_width,
466                             int image_height,
467                             FX_BOOL bTransMask = FALSE) const override;
468 };
469
470 class CPDF_PatternCS : public CPDF_ColorSpace
471 {
472 public:
473     explicit CPDF_PatternCS(CPDF_Document* pDoc)
474         : CPDF_ColorSpace(pDoc, PDFCS_PATTERN, 1),
475           m_pBaseCS(nullptr),
476           m_pCountedBaseCS(nullptr) {
477     }
478     ~CPDF_PatternCS() override;
479     FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override;
480     FX_BOOL GetRGB(FX_FLOAT* pBuf,
481                    FX_FLOAT& R,
482                    FX_FLOAT& G,
483                    FX_FLOAT& B) const override;
484     CPDF_ColorSpace* GetBaseCS() const override;
485
486 private:
487     CPDF_ColorSpace* m_pBaseCS;
488     CPDF_CountedColorSpace* m_pCountedBaseCS;
489 };
490
491 #endif  // CORE_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_