1 // Copyright 2016 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_FPDFAPI_PAGE_CPDF_STREAMCONTENTPARSER_H_ 8 #define CORE_FPDFAPI_PAGE_CPDF_STREAMCONTENTPARSER_H_ 9 10 #include <memory> 11 #include <unordered_map> 12 #include <vector> 13 14 #include "core/fpdfapi/page/cpdf_contentmark.h" 15 #include "core/fpdfapi/parser/cpdf_stream.h" 16 #include "core/fxcrt/fx_string.h" 17 #include "core/fxge/cfx_pathdata.h" 18 19 class CPDF_AllStates; 20 class CPDF_Dictionary; 21 class CPDF_Document; 22 class CPDF_Font; 23 class CPDF_Image; 24 class CPDF_ImageObject; 25 class CPDF_PageObject; 26 class CPDF_PageObjectHolder; 27 class CPDF_StreamParser; 28 class CPDF_TextObject; 29 class CPDF_ColorSpace; 30 class CPDF_Pattern; 31 32 class CPDF_StreamContentParser { 33 public: 34 CPDF_StreamContentParser(CPDF_Document* pDoc, 35 CPDF_Dictionary* pPageResources, 36 CPDF_Dictionary* pParentResources, 37 const CFX_Matrix* pmtContentToUser, 38 CPDF_PageObjectHolder* pObjectHolder, 39 CPDF_Dictionary* pResources, 40 CFX_FloatRect* pBBox, 41 CPDF_AllStates* pAllStates, 42 int level); 43 ~CPDF_StreamContentParser(); 44 45 uint32_t Parse(const uint8_t* pData, uint32_t dwSize, uint32_t max_cost); GetPageObjectHolder()46 CPDF_PageObjectHolder* GetPageObjectHolder() const { return m_pObjectHolder; } GetCurStates()47 CPDF_AllStates* GetCurStates() const { return m_pCurStates.get(); } IsColored()48 bool IsColored() const { return m_bColored; } GetType3Data()49 const FX_FLOAT* GetType3Data() const { return m_Type3Data; } 50 CPDF_Font* FindFont(const CFX_ByteString& name); 51 52 private: 53 struct ContentParam { 54 enum Type { OBJECT = 0, NUMBER, NAME }; 55 56 ContentParam(); 57 ~ContentParam(); 58 59 Type m_Type; 60 std::unique_ptr<CPDF_Object> m_pObject; 61 struct { 62 bool m_bInteger; 63 union { 64 int m_Integer; 65 FX_FLOAT m_Float; 66 }; 67 } m_Number; 68 struct { 69 int m_Len; 70 char m_Buffer[32]; 71 } m_Name; 72 }; 73 74 static const int kParamBufSize = 16; 75 76 using OpCodes = 77 std::unordered_map<uint32_t, void (CPDF_StreamContentParser::*)()>; 78 static OpCodes InitializeOpCodes(); 79 80 void AddNameParam(const CFX_ByteStringC& str); 81 void AddNumberParam(const CFX_ByteStringC& str); 82 void AddObjectParam(std::unique_ptr<CPDF_Object> pObj); 83 int GetNextParamPos(); 84 void ClearAllParams(); 85 CPDF_Object* GetObject(uint32_t index); 86 CFX_ByteString GetString(uint32_t index); 87 FX_FLOAT GetNumber(uint32_t index); GetInteger(uint32_t index)88 int GetInteger(uint32_t index) { return (int32_t)(GetNumber(index)); } 89 void OnOperator(const CFX_ByteStringC& op); 90 void AddTextObject(CFX_ByteString* pText, 91 FX_FLOAT fInitKerning, 92 FX_FLOAT* pKerning, 93 int count); 94 95 void OnChangeTextMatrix(); 96 void ParsePathObject(); 97 void AddPathPoint(FX_FLOAT x, FX_FLOAT y, FXPT_TYPE type, bool close); 98 void AddPathRect(FX_FLOAT x, FX_FLOAT y, FX_FLOAT w, FX_FLOAT h); 99 void AddPathObject(int FillType, bool bStroke); 100 CPDF_ImageObject* AddImage(std::unique_ptr<CPDF_Stream> pStream); 101 CPDF_ImageObject* AddImage(uint32_t streamObjNum); 102 CPDF_ImageObject* AddImage(CPDF_Image* pImage); 103 104 void AddForm(CPDF_Stream* pStream); 105 void SetGraphicStates(CPDF_PageObject* pObj, 106 bool bColor, 107 bool bText, 108 bool bGraph); 109 CPDF_ColorSpace* FindColorSpace(const CFX_ByteString& name); 110 CPDF_Pattern* FindPattern(const CFX_ByteString& name, bool bShading); 111 CPDF_Object* FindResourceObj(const CFX_ByteString& type, 112 const CFX_ByteString& name); 113 114 // Takes ownership of |pImageObj|, returns unowned pointer to it. 115 CPDF_ImageObject* AddImageObject(std::unique_ptr<CPDF_ImageObject> pImageObj); 116 117 void Handle_CloseFillStrokePath(); 118 void Handle_FillStrokePath(); 119 void Handle_CloseEOFillStrokePath(); 120 void Handle_EOFillStrokePath(); 121 void Handle_BeginMarkedContent_Dictionary(); 122 void Handle_BeginImage(); 123 void Handle_BeginMarkedContent(); 124 void Handle_BeginText(); 125 void Handle_CurveTo_123(); 126 void Handle_ConcatMatrix(); 127 void Handle_SetColorSpace_Fill(); 128 void Handle_SetColorSpace_Stroke(); 129 void Handle_SetDash(); 130 void Handle_SetCharWidth(); 131 void Handle_SetCachedDevice(); 132 void Handle_ExecuteXObject(); 133 void Handle_MarkPlace_Dictionary(); 134 void Handle_EndImage(); 135 void Handle_EndMarkedContent(); 136 void Handle_EndText(); 137 void Handle_FillPath(); 138 void Handle_FillPathOld(); 139 void Handle_EOFillPath(); 140 void Handle_SetGray_Fill(); 141 void Handle_SetGray_Stroke(); 142 void Handle_SetExtendGraphState(); 143 void Handle_ClosePath(); 144 void Handle_SetFlat(); 145 void Handle_BeginImageData(); 146 void Handle_SetLineJoin(); 147 void Handle_SetLineCap(); 148 void Handle_SetCMYKColor_Fill(); 149 void Handle_SetCMYKColor_Stroke(); 150 void Handle_LineTo(); 151 void Handle_MoveTo(); 152 void Handle_SetMiterLimit(); 153 void Handle_MarkPlace(); 154 void Handle_EndPath(); 155 void Handle_SaveGraphState(); 156 void Handle_RestoreGraphState(); 157 void Handle_Rectangle(); 158 void Handle_SetRGBColor_Fill(); 159 void Handle_SetRGBColor_Stroke(); 160 void Handle_SetRenderIntent(); 161 void Handle_CloseStrokePath(); 162 void Handle_StrokePath(); 163 void Handle_SetColor_Fill(); 164 void Handle_SetColor_Stroke(); 165 void Handle_SetColorPS_Fill(); 166 void Handle_SetColorPS_Stroke(); 167 void Handle_ShadeFill(); 168 void Handle_SetCharSpace(); 169 void Handle_MoveTextPoint(); 170 void Handle_MoveTextPoint_SetLeading(); 171 void Handle_SetFont(); 172 void Handle_ShowText(); 173 void Handle_ShowText_Positioning(); 174 void Handle_SetTextLeading(); 175 void Handle_SetTextMatrix(); 176 void Handle_SetTextRenderMode(); 177 void Handle_SetTextRise(); 178 void Handle_SetWordSpace(); 179 void Handle_SetHorzScale(); 180 void Handle_MoveToNextLine(); 181 void Handle_CurveTo_23(); 182 void Handle_SetLineWidth(); 183 void Handle_Clip(); 184 void Handle_EOClip(); 185 void Handle_CurveTo_13(); 186 void Handle_NextLineShowText(); 187 void Handle_NextLineShowText_Space(); 188 void Handle_Invalid(); 189 190 CPDF_Document* const m_pDocument; 191 CPDF_Dictionary* m_pPageResources; 192 CPDF_Dictionary* m_pParentResources; 193 CPDF_Dictionary* m_pResources; 194 CPDF_PageObjectHolder* m_pObjectHolder; 195 int m_Level; 196 CFX_Matrix m_mtContentToUser; 197 CFX_FloatRect m_BBox; 198 ContentParam m_ParamBuf[kParamBufSize]; 199 uint32_t m_ParamStartPos; 200 uint32_t m_ParamCount; 201 CPDF_StreamParser* m_pSyntax; 202 std::unique_ptr<CPDF_AllStates> m_pCurStates; 203 CPDF_ContentMark m_CurContentMark; 204 std::vector<std::unique_ptr<CPDF_TextObject>> m_ClipTextList; 205 CPDF_TextObject* m_pLastTextObject; 206 FX_FLOAT m_DefFontSize; 207 FX_PATHPOINT* m_pPathPoints; 208 int m_PathPointCount; 209 int m_PathAllocSize; 210 FX_FLOAT m_PathStartX; 211 FX_FLOAT m_PathStartY; 212 FX_FLOAT m_PathCurrentX; 213 FX_FLOAT m_PathCurrentY; 214 uint8_t m_PathClipType; 215 CFX_ByteString m_LastImageName; 216 CPDF_Image* m_pLastImage; 217 bool m_bColored; 218 FX_FLOAT m_Type3Data[6]; 219 bool m_bResourceMissing; 220 std::vector<std::unique_ptr<CPDF_AllStates>> m_StateStack; 221 }; 222 223 #endif // CORE_FPDFAPI_PAGE_CPDF_STREAMCONTENTPARSER_H_ 224