Home | History | Annotate | Download | only in fpdfdoc
      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_INCLUDE_FPDFDOC_FPDF_DOC_H_
      8 #define CORE_INCLUDE_FPDFDOC_FPDF_DOC_H_
      9 
     10 #include <map>
     11 #include <memory>
     12 #include <vector>
     13 
     14 #include "core/include/fpdfapi/fpdf_parser.h"
     15 #include "core/include/fpdfapi/fpdf_render.h"
     16 
     17 class CFieldTree;
     18 class CPDF_AAction;
     19 class CPDF_Action;
     20 class CPDF_ActionFields;
     21 class CPDF_Annot;
     22 class CPDF_AnnotList;
     23 class CPDF_ApSettings;
     24 class CPDF_Bookmark;
     25 class CPDF_BookmarkTree;
     26 class CPDF_DefaultAppearance;
     27 class CPDF_Dest;
     28 class CPDF_DocJSActions;
     29 class CPDF_FileSpec;
     30 class CPDF_FormControl;
     31 class CPDF_FormField;
     32 class CPDF_FormNotify;
     33 class CPDF_IconFit;
     34 class CPDF_InterForm;
     35 class CPDF_Link;
     36 class CPDF_Metadata;
     37 class CPDF_NumberTree;
     38 class CPDF_OCContext;
     39 class CPDF_Page;
     40 class CPDF_RenderOptions;
     41 class CPDF_TextObject;
     42 class CPDF_ViewerPreferences;
     43 class CXML_Element;
     44 
     45 class CPDF_NameTree {
     46  public:
     47   explicit CPDF_NameTree(CPDF_Dictionary* pRoot) { m_pRoot = pRoot; }
     48 
     49   CPDF_NameTree(CPDF_Document* pDoc, const CFX_ByteStringC& category);
     50 
     51   CPDF_Object* LookupValue(int nIndex, CFX_ByteString& csName) const;
     52 
     53   CPDF_Object* LookupValue(const CFX_ByteString& csName) const;
     54 
     55   CPDF_Array* LookupNamedDest(CPDF_Document* pDoc,
     56                               const CFX_ByteStringC& sName);
     57 
     58   int GetIndex(const CFX_ByteString& csName) const;
     59 
     60   int GetCount() const;
     61 
     62   CPDF_Dictionary* GetRoot() const { return m_pRoot; }
     63 
     64  protected:
     65   CPDF_Dictionary* m_pRoot;
     66 };
     67 class CPDF_BookmarkTree {
     68  public:
     69   explicit CPDF_BookmarkTree(CPDF_Document* pDoc) : m_pDocument(pDoc) {}
     70 
     71   CPDF_Bookmark GetFirstChild(const CPDF_Bookmark& parent) const;
     72 
     73   CPDF_Bookmark GetNextSibling(const CPDF_Bookmark& bookmark) const;
     74 
     75   CPDF_Document* GetDocument() const { return m_pDocument; }
     76 
     77  protected:
     78   CPDF_Document* m_pDocument;
     79 };
     80 #define PDFBOOKMARK_ITALIC 1
     81 #define PDFBOOKMARK_BOLD 2
     82 class CPDF_Bookmark {
     83  public:
     84   CPDF_Bookmark() : m_pDict(NULL) {}
     85 
     86   explicit CPDF_Bookmark(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
     87 
     88   CPDF_Dictionary* GetDict() const { return m_pDict; }
     89 
     90   operator bool() const { return m_pDict != NULL; }
     91 
     92   FX_DWORD GetColorRef() const;
     93 
     94   FX_DWORD GetFontStyle() const;
     95 
     96   CFX_WideString GetTitle() const;
     97 
     98   CPDF_Dest GetDest(CPDF_Document* pDocument) const;
     99 
    100   CPDF_Action GetAction() const;
    101 
    102   CPDF_Dictionary* m_pDict;
    103 };
    104 #define PDFZOOM_XYZ 1
    105 #define PDFZOOM_FITPAGE 2
    106 #define PDFZOOM_FITHORZ 3
    107 #define PDFZOOM_FITVERT 4
    108 #define PDFZOOM_FITRECT 5
    109 #define PDFZOOM_FITBBOX 6
    110 #define PDFZOOM_FITBHORZ 7
    111 #define PDFZOOM_FITBVERT 8
    112 class CPDF_Dest {
    113  public:
    114   CPDF_Dest() : m_pObj(nullptr) {}
    115   explicit CPDF_Dest(CPDF_Object* pObj) : m_pObj(pObj) {}
    116 
    117   operator bool() const { return m_pObj != NULL; }
    118   CPDF_Object* GetObject() const { return m_pObj; }
    119 
    120   CFX_ByteString GetRemoteName();
    121   int GetPageIndex(CPDF_Document* pDoc);
    122   FX_DWORD GetPageObjNum();
    123   int GetZoomMode();
    124   FX_FLOAT GetParam(int index);
    125 
    126  protected:
    127   CPDF_Object* m_pObj;
    128 };
    129 class CPDF_OCContext : public IPDF_OCContext {
    130  public:
    131   enum UsageType { View = 0, Design, Print, Export };
    132 
    133   explicit CPDF_OCContext(CPDF_Document* pDoc, UsageType eUsageType = View);
    134   ~CPDF_OCContext() override;
    135 
    136   CPDF_Document* GetDocument() const { return m_pDocument; }
    137 
    138   UsageType GetUsageType() const { return m_eUsageType; }
    139 
    140   FX_BOOL CheckOCGVisible(const CPDF_Dictionary* pOCGDict) override;
    141 
    142   void ResetOCContext();
    143 
    144  protected:
    145   FX_BOOL LoadOCGStateFromConfig(const CFX_ByteStringC& csConfig,
    146                                  const CPDF_Dictionary* pOCGDict,
    147                                  FX_BOOL& bValidConfig) const;
    148 
    149   FX_BOOL LoadOCGState(const CPDF_Dictionary* pOCGDict) const;
    150 
    151   FX_BOOL GetOCGVisible(const CPDF_Dictionary* pOCGDict);
    152 
    153   FX_BOOL GetOCGVE(CPDF_Array* pExpression,
    154                    FX_BOOL bFromConfig,
    155                    int nLevel = 0);
    156 
    157   FX_BOOL LoadOCMDState(const CPDF_Dictionary* pOCMDDict, FX_BOOL bFromConfig);
    158 
    159   CPDF_Document* m_pDocument;
    160 
    161   UsageType m_eUsageType;
    162 
    163   std::map<const CPDF_Dictionary*, FX_BOOL> m_OCGStates;
    164 };
    165 
    166 class CPDF_ActionFields {
    167  public:
    168   // TODO(thestig): Examine why this cannot be explicit.
    169   CPDF_ActionFields(const CPDF_Action* pAction) {
    170     m_pAction = (CPDF_Action*)pAction;
    171   }
    172 
    173   // TODO(thestig): Replace this.
    174   operator CPDF_Action*() const { return m_pAction; }
    175 
    176   FX_DWORD GetFieldsCount() const;
    177 
    178   std::vector<CPDF_Object*> GetAllFields() const;
    179 
    180   CPDF_Object* GetField(FX_DWORD iIndex) const;
    181 
    182   CPDF_Action* m_pAction;
    183 };
    184 
    185 #define PDFNAMED_NEXTPAGE 1
    186 #define PDFNAMED_PREVPAGE 2
    187 #define PDFNAMED_FIRSTPAGE 3
    188 #define PDFNAMED_LASTPAGE 4
    189 #define PDFJS_MAXLENGTH 64
    190 class CPDF_Action {
    191  public:
    192   enum ActionType {
    193     Unknown = 0,
    194     GoTo,
    195     GoToR,
    196     GoToE,
    197     Launch,
    198     Thread,
    199     URI,
    200     Sound,
    201     Movie,
    202     Hide,
    203     Named,
    204     SubmitForm,
    205     ResetForm,
    206     ImportData,
    207     JavaScript,
    208     SetOCGState,
    209     Rendition,
    210     Trans,
    211     GoTo3DView
    212   };
    213 
    214   CPDF_Action() : m_pDict(nullptr) {}
    215   explicit CPDF_Action(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
    216 
    217   operator bool() const { return m_pDict != NULL; }
    218 
    219   CPDF_Dictionary* GetDict() const { return m_pDict; }
    220 
    221   CFX_ByteString GetTypeName() const { return m_pDict->GetString("S"); }
    222 
    223   ActionType GetType() const;
    224 
    225   CPDF_Dest GetDest(CPDF_Document* pDoc) const;
    226 
    227   CFX_WideString GetFilePath() const;
    228 
    229   FX_BOOL GetNewWindow() const { return m_pDict->GetBoolean("NewWindow"); }
    230 
    231   CFX_ByteString GetURI(CPDF_Document* pDoc) const;
    232 
    233   FX_BOOL GetMouseMap() const { return m_pDict->GetBoolean("IsMap"); }
    234 
    235   CPDF_ActionFields GetWidgets() const { return this; }
    236 
    237   FX_BOOL GetHideStatus() const { return m_pDict->GetBoolean("H", TRUE); }
    238 
    239   CFX_ByteString GetNamedAction() const { return m_pDict->GetString("N"); }
    240 
    241   FX_DWORD GetFlags() const { return m_pDict->GetInteger("Flags"); }
    242 
    243   CFX_WideString GetJavaScript() const;
    244 
    245   CPDF_Dictionary* GetAnnot() const;
    246 
    247   int32_t GetOperationType() const;
    248 
    249   CPDF_Stream* GetSoundStream() const { return m_pDict->GetStream("Sound"); }
    250 
    251   FX_FLOAT GetVolume() const { return m_pDict->GetNumber("Volume"); }
    252 
    253   FX_BOOL IsSynchronous() const { return m_pDict->GetBoolean("Synchronous"); }
    254 
    255   FX_BOOL IsRepeat() const { return m_pDict->GetBoolean("Repeat"); }
    256 
    257   FX_BOOL IsMixPlay() const { return m_pDict->GetBoolean("Mix"); }
    258 
    259   FX_DWORD GetSubActionsCount() const;
    260 
    261   CPDF_Action GetSubAction(FX_DWORD iIndex) const;
    262 
    263  protected:
    264   CPDF_Dictionary* m_pDict;
    265 };
    266 class CPDF_AAction {
    267  public:
    268   // TODO(thestig): Examine why this cannot be explicit.
    269   CPDF_AAction(CPDF_Dictionary* pDict = NULL) { m_pDict = pDict; }
    270 
    271   operator CPDF_Dictionary*() const { return m_pDict; }
    272 
    273   enum AActionType {
    274     CursorEnter = 0,
    275     CursorExit,
    276     ButtonDown,
    277     ButtonUp,
    278     GetFocus,
    279     LoseFocus,
    280     PageOpen,
    281     PageClose,
    282     PageVisible,
    283     PageInvisible,
    284     OpenPage,
    285     ClosePage,
    286     KeyStroke,
    287     Format,
    288     Validate,
    289     Calculate,
    290     CloseDocument,
    291     SaveDocument,
    292     DocumentSaved,
    293     PrintDocument,
    294     DocumentPrinted
    295   };
    296 
    297   FX_BOOL ActionExist(AActionType eType) const;
    298 
    299   CPDF_Action GetAction(AActionType eType) const;
    300 
    301   CPDF_Dictionary* m_pDict;
    302 };
    303 class CPDF_DocJSActions {
    304  public:
    305   explicit CPDF_DocJSActions(CPDF_Document* pDoc);
    306 
    307   int CountJSActions() const;
    308 
    309   CPDF_Action GetJSAction(int index, CFX_ByteString& csName) const;
    310 
    311   CPDF_Action GetJSAction(const CFX_ByteString& csName) const;
    312 
    313   int FindJSAction(const CFX_ByteString& csName) const;
    314 
    315   CPDF_Document* GetDocument() const { return m_pDocument; }
    316 
    317  protected:
    318   CPDF_Document* const m_pDocument;
    319 };
    320 class CPDF_FileSpec {
    321  public:
    322   CPDF_FileSpec();
    323 
    324   explicit CPDF_FileSpec(CPDF_Object* pObj) { m_pObj = pObj; }
    325 
    326   operator CPDF_Object*() const { return m_pObj; }
    327 
    328   FX_BOOL IsURL() const;
    329 
    330   FX_BOOL GetFileName(CFX_WideString& wsFileName) const;
    331 
    332   CPDF_Stream* GetFileStream() const;
    333 
    334   void SetFileName(const CFX_WideStringC& wsFileName, FX_BOOL bURL = FALSE);
    335 
    336  protected:
    337   CPDF_Object* m_pObj;
    338 };
    339 
    340 class CPDF_LinkList {
    341  public:
    342   CPDF_LinkList();
    343   ~CPDF_LinkList();
    344 
    345   CPDF_Link GetLinkAtPoint(CPDF_Page* pPage,
    346                            FX_FLOAT pdf_x,
    347                            FX_FLOAT pdf_y,
    348                            int* z_order);
    349 
    350  private:
    351   const std::vector<CPDF_Dictionary*>* GetPageLinks(CPDF_Page* pPage);
    352 
    353   void LoadPageLinks(CPDF_Page* pPage, std::vector<CPDF_Dictionary*>* pList);
    354 
    355   std::map<FX_DWORD, std::vector<CPDF_Dictionary*>> m_PageMap;
    356 };
    357 
    358 class CPDF_Link {
    359  public:
    360   CPDF_Link() : m_pDict(nullptr) {}
    361   explicit CPDF_Link(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
    362 
    363   CPDF_Dictionary* GetDict() const { return m_pDict; }
    364 
    365   CFX_FloatRect GetRect();
    366   CPDF_Dest GetDest(CPDF_Document* pDoc);
    367   CPDF_Action GetAction();
    368 
    369  protected:
    370   CPDF_Dictionary* m_pDict;
    371 };
    372 
    373 #define ANNOTFLAG_INVISIBLE 0x0001
    374 #define ANNOTFLAG_HIDDEN 0x0002
    375 #define ANNOTFLAG_PRINT 0x0004
    376 #define ANNOTFLAG_NOZOOM 0x0008
    377 #define ANNOTFLAG_NOROTATE 0x0010
    378 #define ANNOTFLAG_NOVIEW 0x0020
    379 #define ANNOTFLAG_READONLY 0x0040
    380 #define ANNOTFLAG_LOCKED 0x0080
    381 #define ANNOTFLAG_TOGGLENOVIEW 0x0100
    382 
    383 class CPDF_Annot : public CFX_PrivateData {
    384  public:
    385   enum AppearanceMode { Normal, Rollover, Down };
    386 
    387   CPDF_Annot(CPDF_Dictionary* pDict, CPDF_AnnotList* pList);
    388   ~CPDF_Annot();
    389 
    390   CFX_ByteString GetSubType() const;
    391 
    392   FX_DWORD GetFlags() const;
    393 
    394   void GetRect(CFX_FloatRect& rect) const;
    395 
    396   const CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict; }
    397   CPDF_Dictionary* GetAnnotDict() { return m_pAnnotDict; }
    398 
    399   FX_BOOL DrawAppearance(CPDF_Page* pPage,
    400                          CFX_RenderDevice* pDevice,
    401                          const CFX_Matrix* pUser2Device,
    402                          AppearanceMode mode,
    403                          const CPDF_RenderOptions* pOptions);
    404 
    405   FX_BOOL DrawInContext(const CPDF_Page* pPage,
    406                         CPDF_RenderContext* pContext,
    407                         const CFX_Matrix* pUser2Device,
    408                         AppearanceMode mode);
    409 
    410   void ClearCachedAP();
    411 
    412   void DrawBorder(CFX_RenderDevice* pDevice,
    413                   const CFX_Matrix* pUser2Device,
    414                   const CPDF_RenderOptions* pOptions);
    415 
    416   CPDF_Form* GetAPForm(const CPDF_Page* pPage, AppearanceMode mode);
    417 
    418  private:
    419   CPDF_Dictionary* const m_pAnnotDict;
    420   CPDF_AnnotList* const m_pList;
    421   const CFX_ByteString m_sSubtype;
    422   std::map<CPDF_Stream*, CPDF_Form*> m_APMap;
    423 };
    424 
    425 class CPDF_AnnotList {
    426  public:
    427   explicit CPDF_AnnotList(CPDF_Page* pPage);
    428   ~CPDF_AnnotList();
    429 
    430   void DisplayAnnots(CPDF_Page* pPage,
    431                      CPDF_RenderContext* pContext,
    432                      FX_BOOL bPrinting,
    433                      CFX_Matrix* pMatrix,
    434                      FX_BOOL bShowWidget,
    435                      CPDF_RenderOptions* pOptions) {
    436     DisplayAnnots(pPage, nullptr, pContext, bPrinting, pMatrix,
    437                   bShowWidget ? 3 : 1, pOptions, nullptr);
    438   }
    439   void DisplayAnnots(CPDF_Page* pPage,
    440                      CFX_RenderDevice* pDevice,
    441                      CPDF_RenderContext* pContext,
    442                      FX_BOOL bPrinting,
    443                      CFX_Matrix* pMatrix,
    444                      FX_DWORD dwAnnotFlags,
    445                      CPDF_RenderOptions* pOptions,
    446                      FX_RECT* pClipRect);
    447   size_t Count() const { return m_AnnotList.size(); }
    448   CPDF_Annot* GetAt(size_t index) const { return m_AnnotList[index]; }
    449   const std::vector<CPDF_Annot*>& All() const { return m_AnnotList; }
    450   CPDF_Document* GetDocument() const { return m_pDocument; }
    451 
    452  protected:
    453   void DisplayPass(CPDF_Page* pPage,
    454                    CFX_RenderDevice* pDevice,
    455                    CPDF_RenderContext* pContext,
    456                    FX_BOOL bPrinting,
    457                    CFX_Matrix* pMatrix,
    458                    FX_BOOL bWidget,
    459                    CPDF_RenderOptions* pOptions,
    460                    FX_RECT* clip_rect);
    461 
    462   CPDF_Document* const m_pDocument;
    463   std::vector<CPDF_Annot*> m_AnnotList;
    464 };
    465 
    466 #define COLORTYPE_TRANSPARENT 0
    467 #define COLORTYPE_GRAY 1
    468 #define COLORTYPE_RGB 2
    469 #define COLORTYPE_CMYK 3
    470 class CPDF_DefaultAppearance {
    471  public:
    472   // TODO(thestig): Examine why this cannot be explicit.
    473   CPDF_DefaultAppearance(const CFX_ByteString& csDA = "") { m_csDA = csDA; }
    474 
    475   CPDF_DefaultAppearance(const CPDF_DefaultAppearance& cDA) {
    476     m_csDA = (CFX_ByteString)(CPDF_DefaultAppearance&)cDA;
    477   }
    478 
    479   operator CFX_ByteString() const { return m_csDA; }
    480 
    481   const CPDF_DefaultAppearance& operator=(const CFX_ByteString& csDA) {
    482     m_csDA = csDA;
    483     return *this;
    484   }
    485 
    486   const CPDF_DefaultAppearance& operator=(const CPDF_DefaultAppearance& cDA) {
    487     m_csDA = (CFX_ByteString)(CPDF_DefaultAppearance&)cDA;
    488     return *this;
    489   }
    490 
    491   FX_BOOL HasFont();
    492 
    493   CFX_ByteString GetFontString();
    494 
    495   void GetFont(CFX_ByteString& csFontNameTag, FX_FLOAT& fFontSize);
    496 
    497   FX_BOOL HasColor(FX_BOOL bStrokingOperation = FALSE);
    498 
    499   CFX_ByteString GetColorString(FX_BOOL bStrokingOperation = FALSE);
    500 
    501   void GetColor(int& iColorType,
    502                 FX_FLOAT fc[4],
    503                 FX_BOOL bStrokingOperation = FALSE);
    504 
    505   void GetColor(FX_ARGB& color,
    506                 int& iColorType,
    507                 FX_BOOL bStrokingOperation = FALSE);
    508 
    509   FX_BOOL HasTextMatrix();
    510 
    511   CFX_ByteString GetTextMatrixString();
    512 
    513   CFX_Matrix GetTextMatrix();
    514 
    515  protected:
    516   CFX_ByteString m_csDA;
    517 };
    518 #define FIELDTYPE_UNKNOWN 0
    519 #define FIELDTYPE_PUSHBUTTON 1
    520 #define FIELDTYPE_CHECKBOX 2
    521 #define FIELDTYPE_RADIOBUTTON 3
    522 #define FIELDTYPE_COMBOBOX 4
    523 #define FIELDTYPE_LISTBOX 5
    524 #define FIELDTYPE_TEXTFIELD 6
    525 #define FIELDTYPE_SIGNATURE 7
    526 class CPDF_InterForm : public CFX_PrivateData {
    527  public:
    528   CPDF_InterForm(CPDF_Document* pDocument, FX_BOOL bUpdateAP);
    529   ~CPDF_InterForm();
    530 
    531   static void EnableUpdateAP(FX_BOOL bUpdateAP);
    532 
    533   static FX_BOOL UpdatingAPEnabled();
    534 
    535   static CFX_ByteString GenerateNewResourceName(const CPDF_Dictionary* pResDict,
    536                                                 const FX_CHAR* csType,
    537                                                 int iMinLen = 2,
    538                                                 const FX_CHAR* csPrefix = "");
    539 
    540   static CPDF_Font* AddStandardFont(CPDF_Document* pDocument,
    541                                     CFX_ByteString csFontName);
    542 
    543   static CFX_ByteString GetNativeFont(uint8_t iCharSet, void* pLogFont = NULL);
    544 
    545   static CFX_ByteString GetNativeFont(void* pLogFont = NULL);
    546 
    547   static uint8_t GetNativeCharSet();
    548 
    549   static CPDF_Font* AddNativeFont(uint8_t iCharSet, CPDF_Document* pDocument);
    550 
    551   static CPDF_Font* AddNativeFont(CPDF_Document* pDocument);
    552 
    553   FX_BOOL ValidateFieldName(CFX_WideString& csNewFieldName, int iType);
    554 
    555   FX_BOOL ValidateFieldName(const CPDF_FormField* pField,
    556                             CFX_WideString& csNewFieldName);
    557 
    558   FX_BOOL ValidateFieldName(const CPDF_FormControl* pControl,
    559                             CFX_WideString& csNewFieldName);
    560 
    561   FX_DWORD CountFields(const CFX_WideString& csFieldName = L"");
    562 
    563   CPDF_FormField* GetField(FX_DWORD index,
    564                            const CFX_WideString& csFieldName = L"");
    565 
    566   void GetAllFieldNames(CFX_WideStringArray& allFieldNames);
    567 
    568   CPDF_FormField* GetFieldByDict(CPDF_Dictionary* pFieldDict) const;
    569 
    570   CPDF_FormControl* GetControlAtPoint(CPDF_Page* pPage,
    571                                       FX_FLOAT pdf_x,
    572                                       FX_FLOAT pdf_y,
    573                                       int* z_order) const;
    574 
    575   CPDF_FormControl* GetControlByDict(const CPDF_Dictionary* pWidgetDict) const;
    576 
    577   CPDF_Document* GetDocument() const { return m_pDocument; }
    578 
    579   CPDF_Dictionary* GetFormDict() const { return m_pFormDict; }
    580 
    581   FX_BOOL NeedConstructAP();
    582 
    583   void NeedConstructAP(FX_BOOL bNeedAP);
    584 
    585   int CountFieldsInCalculationOrder();
    586 
    587   CPDF_FormField* GetFieldInCalculationOrder(int index);
    588 
    589   int FindFieldInCalculationOrder(const CPDF_FormField* pField);
    590 
    591   FX_DWORD CountFormFonts();
    592 
    593   CPDF_Font* GetFormFont(FX_DWORD index, CFX_ByteString& csNameTag);
    594 
    595   CPDF_Font* GetFormFont(CFX_ByteString csNameTag);
    596 
    597   CPDF_Font* GetFormFont(CFX_ByteString csFontName, CFX_ByteString& csNameTag);
    598 
    599   CPDF_Font* GetNativeFormFont(uint8_t iCharSet, CFX_ByteString& csNameTag);
    600 
    601   CPDF_Font* GetNativeFormFont(CFX_ByteString& csNameTag);
    602 
    603   FX_BOOL FindFormFont(const CPDF_Font* pFont, CFX_ByteString& csNameTag);
    604 
    605   FX_BOOL FindFormFont(CFX_ByteString csFontName,
    606                        CPDF_Font*& pFont,
    607                        CFX_ByteString& csNameTag);
    608 
    609   inline FX_BOOL FindFormFont(CFX_WideString csFontName,
    610                               CPDF_Font*& pFont,
    611                               CFX_ByteString& csNameTag) {
    612     return FindFormFont(PDF_EncodeText(csFontName), pFont, csNameTag);
    613   }
    614 
    615   void AddFormFont(const CPDF_Font* pFont, CFX_ByteString& csNameTag);
    616 
    617   CPDF_Font* AddNativeFormFont(uint8_t iCharSet, CFX_ByteString& csNameTag);
    618 
    619   CPDF_Font* AddNativeFormFont(CFX_ByteString& csNameTag);
    620 
    621   void RemoveFormFont(const CPDF_Font* pFont);
    622 
    623   void RemoveFormFont(CFX_ByteString csNameTag);
    624 
    625   CPDF_DefaultAppearance GetDefaultAppearance();
    626 
    627   CPDF_Font* GetDefaultFormFont();
    628 
    629   int GetFormAlignment();
    630 
    631   CPDF_FormField* CheckRequiredFields(
    632       const std::vector<CPDF_FormField*>* fields,
    633       bool bIncludeOrExclude) const;
    634 
    635   CFDF_Document* ExportToFDF(const CFX_WideStringC& pdf_path,
    636                              bool bSimpleFileSpec = false) const;
    637 
    638   CFDF_Document* ExportToFDF(const CFX_WideStringC& pdf_path,
    639                              const std::vector<CPDF_FormField*>& fields,
    640                              bool bIncludeOrExclude = true,
    641                              bool bSimpleFileSpec = false) const;
    642 
    643   FX_BOOL ImportFromFDF(const CFDF_Document* pFDFDoc, FX_BOOL bNotify = FALSE);
    644 
    645   bool ResetForm(const std::vector<CPDF_FormField*>& fields,
    646                  bool bIncludeOrExclude = true,
    647                  bool bNotify = false);
    648 
    649   bool ResetForm(bool bNotify = false);
    650 
    651   CPDF_FormNotify* GetFormNotify() const { return m_pFormNotify; }
    652 
    653   void SetFormNotify(const CPDF_FormNotify* pNotify);
    654 
    655   FX_BOOL IsUpdated() { return m_bUpdated; }
    656 
    657   void ClearUpdatedFlag() { m_bUpdated = FALSE; }
    658 
    659   FX_BOOL HasXFAForm() const;
    660 
    661   void FixPageFields(const CPDF_Page* pPage);
    662 
    663  protected:
    664   static FX_BOOL m_bUpdateAP;
    665 
    666   void LoadField(CPDF_Dictionary* pFieldDict, int nLevel = 0);
    667 
    668   CPDF_Object* GetFieldAttr(CPDF_Dictionary* pFieldDict, const FX_CHAR* name);
    669 
    670   CPDF_FormField* AddTerminalField(CPDF_Dictionary* pFieldDict);
    671 
    672   CPDF_FormControl* AddControl(const CPDF_FormField* pField,
    673                                CPDF_Dictionary* pWidgetDict);
    674 
    675   void FDF_ImportField(CPDF_Dictionary* pField,
    676                        const CFX_WideString& parent_name,
    677                        FX_BOOL bNotify = FALSE,
    678                        int nLevel = 0);
    679 
    680   FX_BOOL ValidateFieldName(CFX_WideString& csNewFieldName,
    681                             int iType,
    682                             const CPDF_FormField* pExcludedField,
    683                             const CPDF_FormControl* pExcludedControl);
    684 
    685   int CompareFieldName(const CFX_WideString& name1,
    686                        const CFX_WideString& name2);
    687 
    688   int CompareFieldName(const CFX_ByteString& name1,
    689                        const CFX_ByteString& name2);
    690 
    691   CPDF_Document* const m_pDocument;
    692 
    693   FX_BOOL m_bGenerateAP;
    694 
    695   CPDF_Dictionary* m_pFormDict;
    696 
    697   std::map<const CPDF_Dictionary*, CPDF_FormControl*> m_ControlMap;
    698 
    699   std::unique_ptr<CFieldTree> m_pFieldTree;
    700 
    701   CFX_ByteString m_bsEncoding;
    702 
    703   CPDF_FormNotify* m_pFormNotify;
    704 
    705   FX_BOOL m_bUpdated;
    706   friend class CPDF_FormControl;
    707   friend class CPDF_FormField;
    708 };
    709 #define FORMFIELD_READONLY 0x01
    710 #define FORMFIELD_REQUIRED 0x02
    711 #define FORMFIELD_NOEXPORT 0x04
    712 #define FORMRADIO_NOTOGGLEOFF 0x100
    713 #define FORMRADIO_UNISON 0x200
    714 #define FORMTEXT_MULTILINE 0x100
    715 #define FORMTEXT_PASSWORD 0x200
    716 #define FORMTEXT_NOSCROLL 0x400
    717 #define FORMTEXT_COMB 0x800
    718 #define FORMCOMBO_EDIT 0x100
    719 #define FORMLIST_MULTISELECT 0x100
    720 class CPDF_FormField {
    721  public:
    722   enum Type {
    723     Unknown,
    724     PushButton,
    725     RadioButton,
    726     CheckBox,
    727     Text,
    728     RichText,
    729     File,
    730     ListBox,
    731     ComboBox,
    732     Sign
    733   };
    734 
    735   CFX_WideString GetFullName();
    736 
    737   Type GetType() { return m_Type; }
    738 
    739   FX_DWORD GetFlags() { return m_Flags; }
    740 
    741   CPDF_Dictionary* GetFieldDict() const { return m_pDict; }
    742 
    743   void SetFieldDict(CPDF_Dictionary* pDict) { m_pDict = pDict; }
    744 
    745   FX_BOOL ResetField(FX_BOOL bNotify = FALSE);
    746 
    747   int CountControls() { return m_ControlList.GetSize(); }
    748 
    749   CPDF_FormControl* GetControl(int index) { return m_ControlList.GetAt(index); }
    750 
    751   int GetControlIndex(const CPDF_FormControl* pControl);
    752 
    753   int GetFieldType();
    754 
    755   CPDF_AAction GetAdditionalAction();
    756 
    757   CFX_WideString GetAlternateName();
    758 
    759   CFX_WideString GetMappingName();
    760 
    761   FX_DWORD GetFieldFlags();
    762 
    763   CFX_ByteString GetDefaultStyle();
    764 
    765   CFX_WideString GetRichTextString();
    766 
    767   CFX_WideString GetValue();
    768 
    769   CFX_WideString GetDefaultValue();
    770 
    771   FX_BOOL SetValue(const CFX_WideString& value, FX_BOOL bNotify = FALSE);
    772 
    773   int GetMaxLen();
    774 
    775   int CountSelectedItems();
    776 
    777   int GetSelectedIndex(int index);
    778 
    779   FX_BOOL ClearSelection(FX_BOOL bNotify = FALSE);
    780 
    781   FX_BOOL IsItemSelected(int index);
    782 
    783   FX_BOOL SetItemSelection(int index,
    784                            FX_BOOL bSelected,
    785                            FX_BOOL bNotify = FALSE);
    786 
    787   FX_BOOL IsItemDefaultSelected(int index);
    788 
    789   int GetDefaultSelectedItem();
    790 
    791   int CountOptions();
    792 
    793   CFX_WideString GetOptionLabel(int index);
    794 
    795   CFX_WideString GetOptionValue(int index);
    796 
    797   int FindOption(CFX_WideString csOptLabel);
    798 
    799   int FindOptionValue(const CFX_WideString& csOptValue, int iStartIndex = 0);
    800 
    801   FX_BOOL CheckControl(int iControlIndex,
    802                        FX_BOOL bChecked,
    803                        FX_BOOL bNotify = FALSE);
    804 
    805   int GetTopVisibleIndex();
    806 
    807   int CountSelectedOptions();
    808 
    809   int GetSelectedOptionIndex(int index);
    810 
    811   FX_BOOL IsOptionSelected(int iOptIndex);
    812 
    813   FX_BOOL SelectOption(int iOptIndex,
    814                        FX_BOOL bSelected,
    815                        FX_BOOL bNotify = FALSE);
    816 
    817   FX_BOOL ClearSelectedOptions(FX_BOOL bNotify = FALSE);
    818 
    819 #ifdef PDF_ENABLE_XFA
    820   FX_BOOL ClearOptions(FX_BOOL bNotify = FALSE);
    821 
    822   int InsertOption(CFX_WideString csOptLabel,
    823                    int index = -1,
    824                    FX_BOOL bNotify = FALSE);
    825 #endif  // PDF_ENABLE_XFA
    826 
    827   FX_FLOAT GetFontSize() { return m_FontSize; }
    828 
    829   CPDF_Font* GetFont() { return m_pFont; }
    830 
    831  protected:
    832   CPDF_FormField(CPDF_InterForm* pForm, CPDF_Dictionary* pDict);
    833   ~CPDF_FormField();
    834 
    835   CFX_WideString GetValue(FX_BOOL bDefault);
    836 
    837   FX_BOOL SetValue(const CFX_WideString& value,
    838                    FX_BOOL bDefault,
    839                    FX_BOOL bNotify);
    840 
    841   void SyncFieldFlags();
    842 
    843   int FindListSel(CPDF_String* str);
    844 
    845   CFX_WideString GetOptionText(int index, int sub_index);
    846 
    847   void LoadDA();
    848 
    849   void UpdateAP(CPDF_FormControl* pControl);
    850 
    851   CFX_WideString GetCheckValue(FX_BOOL bDefault);
    852 
    853   FX_BOOL SetCheckValue(const CFX_WideString& value,
    854                         FX_BOOL bDefault,
    855                         FX_BOOL bNotify);
    856 
    857   CPDF_FormField::Type m_Type;
    858   FX_DWORD m_Flags;
    859   CPDF_InterForm* m_pForm;
    860   CPDF_Dictionary* m_pDict;
    861   CFX_ArrayTemplate<CPDF_FormControl*> m_ControlList;
    862   FX_FLOAT m_FontSize;
    863   CPDF_Font* m_pFont;
    864 
    865   friend class CPDF_InterForm;
    866   friend class CPDF_FormControl;
    867 };
    868 CPDF_Object* FPDF_GetFieldAttr(CPDF_Dictionary* pFieldDict,
    869                                const FX_CHAR* name,
    870                                int nLevel = 0);
    871 class CPDF_IconFit {
    872  public:
    873   // TODO(thestig): Examine why this cannot be explicit.
    874   CPDF_IconFit(CPDF_Dictionary* pDict = NULL) { m_pDict = pDict; }
    875 
    876   operator CPDF_Dictionary*() const { return m_pDict; }
    877 
    878   enum ScaleMethod { Always = 0, Bigger, Smaller, Never };
    879 
    880   ScaleMethod GetScaleMethod();
    881 
    882   FX_BOOL IsProportionalScale();
    883 
    884   void GetIconPosition(FX_FLOAT& fLeft, FX_FLOAT& fBottom);
    885 
    886   FX_BOOL GetFittingBounds();
    887 
    888   CPDF_Dictionary* m_pDict;
    889 };
    890 
    891 #define TEXTPOS_CAPTION 0
    892 #define TEXTPOS_ICON 1
    893 #define TEXTPOS_BELOW 2
    894 #define TEXTPOS_ABOVE 3
    895 #define TEXTPOS_RIGHT 4
    896 #define TEXTPOS_LEFT 5
    897 #define TEXTPOS_OVERLAID 6
    898 class CPDF_FormControl {
    899  public:
    900   CPDF_FormField::Type GetType() { return m_pField->GetType(); }
    901 
    902   CPDF_InterForm* GetInterForm() const { return m_pForm; }
    903 
    904   CPDF_FormField* GetField() const { return m_pField; }
    905 
    906   CPDF_Dictionary* GetWidget() const { return m_pWidgetDict; }
    907 
    908   CFX_FloatRect GetRect() const;
    909 
    910   void DrawControl(CFX_RenderDevice* pDevice,
    911                    CFX_Matrix* pMatrix,
    912                    CPDF_Page* pPage,
    913                    CPDF_Annot::AppearanceMode mode,
    914                    const CPDF_RenderOptions* pOptions = NULL);
    915 
    916   CFX_ByteString GetCheckedAPState();
    917 
    918   CFX_WideString GetExportValue();
    919 
    920   FX_BOOL IsChecked();
    921 
    922   FX_BOOL IsDefaultChecked();
    923 
    924   enum HighlightingMode { None = 0, Invert, Outline, Push, Toggle };
    925 
    926   HighlightingMode GetHighlightingMode();
    927 
    928   bool HasMKEntry(CFX_ByteString csEntry) const;
    929 
    930   int GetRotation();
    931 
    932   inline FX_ARGB GetBorderColor(int& iColorType) {
    933     return GetColor(iColorType, "BC");
    934   }
    935 
    936   FX_FLOAT GetOriginalBorderColor(int index) {
    937     return GetOriginalColor(index, "BC");
    938   }
    939 
    940   void GetOriginalBorderColor(int& iColorType, FX_FLOAT fc[4]) {
    941     GetOriginalColor(iColorType, fc, "BC");
    942   }
    943 
    944   FX_ARGB GetBackgroundColor(int& iColorType) {
    945     return GetColor(iColorType, "BG");
    946   }
    947 
    948   FX_FLOAT GetOriginalBackgroundColor(int index) {
    949     return GetOriginalColor(index, "BG");
    950   }
    951 
    952   void GetOriginalBackgroundColor(int& iColorType, FX_FLOAT fc[4]) {
    953     GetOriginalColor(iColorType, fc, "BG");
    954   }
    955 
    956   CFX_WideString GetNormalCaption() { return GetCaption("CA"); }
    957 
    958   CFX_WideString GetRolloverCaption() { return GetCaption("RC"); }
    959 
    960   CFX_WideString GetDownCaption() { return GetCaption("AC"); }
    961 
    962   CPDF_Stream* GetNormalIcon() { return GetIcon("I"); }
    963 
    964   CPDF_Stream* GetRolloverIcon() { return GetIcon("RI"); }
    965 
    966   CPDF_Stream* GetDownIcon() { return GetIcon("IX"); }
    967 
    968   CPDF_IconFit GetIconFit();
    969 
    970   int GetTextPosition();
    971 
    972   CPDF_Action GetAction();
    973 
    974   CPDF_AAction GetAdditionalAction();
    975 
    976   CPDF_DefaultAppearance GetDefaultAppearance();
    977 
    978   CPDF_Font* GetDefaultControlFont();
    979 
    980   int GetControlAlignment();
    981 
    982  protected:
    983   CPDF_FormControl(CPDF_FormField* pField, CPDF_Dictionary* pWidgetDict);
    984 
    985   CFX_ByteString GetOnStateName();
    986 
    987   void SetOnStateName(const CFX_ByteString& csOn);
    988 
    989   void CheckControl(FX_BOOL bChecked);
    990 
    991   FX_ARGB GetColor(int& iColorType, CFX_ByteString csEntry);
    992 
    993   FX_FLOAT GetOriginalColor(int index, CFX_ByteString csEntry);
    994 
    995   void GetOriginalColor(int& iColorType,
    996                         FX_FLOAT fc[4],
    997                         CFX_ByteString csEntry);
    998 
    999   CFX_WideString GetCaption(CFX_ByteString csEntry);
   1000 
   1001   CPDF_Stream* GetIcon(CFX_ByteString csEntry);
   1002 
   1003   CPDF_ApSettings GetMK() const;
   1004 
   1005   CPDF_InterForm* m_pForm;
   1006 
   1007   CPDF_FormField* m_pField;
   1008 
   1009   CPDF_Dictionary* m_pWidgetDict;
   1010   friend class CPDF_InterForm;
   1011   friend class CPDF_FormField;
   1012 };
   1013 class CPDF_FormNotify {
   1014  public:
   1015   virtual ~CPDF_FormNotify() {}
   1016 
   1017   virtual int BeforeValueChange(const CPDF_FormField* pField,
   1018                                 CFX_WideString& csValue) {
   1019     return 0;
   1020   }
   1021 
   1022   virtual int AfterValueChange(const CPDF_FormField* pField) { return 0; }
   1023 
   1024   virtual int BeforeSelectionChange(const CPDF_FormField* pField,
   1025                                     CFX_WideString& csValue) {
   1026     return 0;
   1027   }
   1028 
   1029   virtual int AfterSelectionChange(const CPDF_FormField* pField) { return 0; }
   1030 
   1031   virtual int AfterCheckedStatusChange(const CPDF_FormField* pField,
   1032                                        const CFX_ByteArray& statusArray) {
   1033     return 0;
   1034   }
   1035 
   1036   virtual int BeforeFormReset(const CPDF_InterForm* pForm) { return 0; }
   1037 
   1038   virtual int AfterFormReset(const CPDF_InterForm* pForm) { return 0; }
   1039 
   1040   virtual int BeforeFormImportData(const CPDF_InterForm* pForm) { return 0; }
   1041 
   1042   virtual int AfterFormImportData(const CPDF_InterForm* pForm) { return 0; }
   1043 };
   1044 FX_BOOL FPDF_GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict);
   1045 class CPDF_PageLabel {
   1046  public:
   1047   explicit CPDF_PageLabel(CPDF_Document* pDocument) { m_pDocument = pDocument; }
   1048 
   1049   CFX_WideString GetLabel(int nPage) const;
   1050 
   1051   int32_t GetPageByLabel(const CFX_ByteStringC& bsLabel) const;
   1052 
   1053   int32_t GetPageByLabel(const CFX_WideStringC& wsLabel) const;
   1054 
   1055  protected:
   1056   CPDF_Document* m_pDocument;
   1057 };
   1058 
   1059 class CPDF_Metadata {
   1060  public:
   1061   explicit CPDF_Metadata(CPDF_Document* pDoc);
   1062   ~CPDF_Metadata();
   1063 
   1064   const CXML_Element* GetRoot() const;
   1065 
   1066  private:
   1067   std::unique_ptr<CXML_Element> m_pXmlElement;
   1068 };
   1069 
   1070 class CPDF_ViewerPreferences {
   1071  public:
   1072   explicit CPDF_ViewerPreferences(CPDF_Document* pDoc);
   1073 
   1074   ~CPDF_ViewerPreferences();
   1075 
   1076   FX_BOOL IsDirectionR2L() const;
   1077 
   1078   FX_BOOL PrintScaling() const;
   1079 
   1080   int32_t NumCopies() const;
   1081 
   1082   CPDF_Array* PrintPageRange() const;
   1083 
   1084   CFX_ByteString Duplex() const;
   1085 
   1086  protected:
   1087   CPDF_Document* const m_pDoc;
   1088 };
   1089 
   1090 class CPDF_ApSettings {
   1091  public:
   1092   explicit CPDF_ApSettings(CPDF_Dictionary* pDict);
   1093 
   1094   bool HasMKEntry(const CFX_ByteStringC& csEntry) const;
   1095   int GetRotation() const;
   1096 
   1097   FX_ARGB GetBorderColor(int& iColorType) const {
   1098     return GetColor(iColorType, "BC");
   1099   }
   1100 
   1101   FX_FLOAT GetOriginalBorderColor(int index) const {
   1102     return GetOriginalColor(index, "BC");
   1103   }
   1104 
   1105   void GetOriginalBorderColor(int& iColorType, FX_FLOAT fc[4]) const {
   1106     GetOriginalColor(iColorType, fc, "BC");
   1107   }
   1108 
   1109   FX_ARGB GetBackgroundColor(int& iColorType) const {
   1110     return GetColor(iColorType, "BG");
   1111   }
   1112 
   1113   FX_FLOAT GetOriginalBackgroundColor(int index) const {
   1114     return GetOriginalColor(index, "BG");
   1115   }
   1116 
   1117   void GetOriginalBackgroundColor(int& iColorType, FX_FLOAT fc[4]) const {
   1118     GetOriginalColor(iColorType, fc, "BG");
   1119   }
   1120 
   1121   CFX_WideString GetNormalCaption() const { return GetCaption("CA"); }
   1122 
   1123   CFX_WideString GetRolloverCaption() const { return GetCaption("RC"); }
   1124 
   1125   CFX_WideString GetDownCaption() const { return GetCaption("AC"); }
   1126 
   1127   CPDF_Stream* GetNormalIcon() const { return GetIcon("I"); }
   1128 
   1129   CPDF_Stream* GetRolloverIcon() const { return GetIcon("RI"); }
   1130 
   1131   CPDF_Stream* GetDownIcon() const { return GetIcon("IX"); }
   1132 
   1133   CPDF_IconFit GetIconFit() const;
   1134 
   1135   int GetTextPosition() const;
   1136 
   1137  protected:
   1138   friend class CPDF_FormControl;
   1139 
   1140   FX_ARGB GetColor(int& iColorType, const CFX_ByteStringC& csEntry) const;
   1141 
   1142   FX_FLOAT GetOriginalColor(int index, const CFX_ByteStringC& csEntry) const;
   1143 
   1144   void GetOriginalColor(int& iColorType,
   1145                         FX_FLOAT fc[4],
   1146                         const CFX_ByteStringC& csEntry) const;
   1147 
   1148   CFX_WideString GetCaption(const CFX_ByteStringC& csEntry) const;
   1149 
   1150   CPDF_Stream* GetIcon(const CFX_ByteStringC& csEntry) const;
   1151 
   1152   CPDF_Dictionary* const m_pDict;
   1153 };
   1154 
   1155 CFX_WideString FILESPEC_EncodeFileName(const CFX_WideStringC& filepath);
   1156 
   1157 #endif  // CORE_INCLUDE_FPDFDOC_FPDF_DOC_H_
   1158