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