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_VT_H_
      8 #define CORE_INCLUDE_FPDFDOC_FPDF_VT_H_
      9 
     10 #include "core/include/fpdfapi/fpdf_parser.h"
     11 #include "core/include/fxge/fx_dib.h"
     12 
     13 class IPDF_VariableText;
     14 class IPDF_VariableText_Iterator;
     15 class IPDF_VariableText_Provider;
     16 struct CPVT_Line;
     17 struct CPVT_Section;
     18 struct CPVT_Word;
     19 struct CPVT_WordPlace;
     20 struct CPVT_WordRange;
     21 
     22 struct CPVT_WordPlace {
     23   CPVT_WordPlace() : nSecIndex(-1), nLineIndex(-1), nWordIndex(-1) {}
     24 
     25   CPVT_WordPlace(int32_t other_nSecIndex,
     26                  int32_t other_nLineIndex,
     27                  int32_t other_nWordIndex) {
     28     nSecIndex = other_nSecIndex;
     29     nLineIndex = other_nLineIndex;
     30     nWordIndex = other_nWordIndex;
     31   }
     32 
     33   void Default() { nSecIndex = nLineIndex = nWordIndex = -1; }
     34 
     35   FX_BOOL operator==(const CPVT_WordPlace& wp) const {
     36     return wp.nSecIndex == nSecIndex && wp.nLineIndex == nLineIndex &&
     37            wp.nWordIndex == nWordIndex;
     38   }
     39 
     40   FX_BOOL operator!=(const CPVT_WordPlace& wp) const {
     41     return wp.nSecIndex != nSecIndex || wp.nLineIndex != nLineIndex ||
     42            wp.nWordIndex != nWordIndex;
     43   }
     44 
     45   inline int32_t WordCmp(const CPVT_WordPlace& wp) const {
     46     if (nSecIndex > wp.nSecIndex) {
     47       return 1;
     48     }
     49     if (nSecIndex < wp.nSecIndex) {
     50       return -1;
     51     }
     52     if (nLineIndex > wp.nLineIndex) {
     53       return 1;
     54     }
     55     if (nLineIndex < wp.nLineIndex) {
     56       return -1;
     57     }
     58     if (nWordIndex > wp.nWordIndex) {
     59       return 1;
     60     }
     61     if (nWordIndex < wp.nWordIndex) {
     62       return -1;
     63     }
     64     return 0;
     65   }
     66 
     67   inline int32_t LineCmp(const CPVT_WordPlace& wp) const {
     68     if (nSecIndex > wp.nSecIndex) {
     69       return 1;
     70     }
     71     if (nSecIndex < wp.nSecIndex) {
     72       return -1;
     73     }
     74     if (nLineIndex > wp.nLineIndex) {
     75       return 1;
     76     }
     77     if (nLineIndex < wp.nLineIndex) {
     78       return -1;
     79     }
     80     return 0;
     81   }
     82 
     83   inline int32_t SecCmp(const CPVT_WordPlace& wp) const {
     84     if (nSecIndex > wp.nSecIndex) {
     85       return 1;
     86     }
     87     if (nSecIndex < wp.nSecIndex) {
     88       return -1;
     89     }
     90     return 0;
     91   }
     92 
     93   int32_t nSecIndex;
     94 
     95   int32_t nLineIndex;
     96 
     97   int32_t nWordIndex;
     98 };
     99 struct CPVT_WordRange {
    100   CPVT_WordRange() {}
    101 
    102   CPVT_WordRange(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) {
    103     Set(begin, end);
    104   }
    105 
    106   void Default() {
    107     BeginPos.Default();
    108     EndPos.Default();
    109   }
    110 
    111   void Set(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) {
    112     BeginPos = begin;
    113     EndPos = end;
    114     SwapWordPlace();
    115   }
    116 
    117   void SetBeginPos(const CPVT_WordPlace& begin) {
    118     BeginPos = begin;
    119     SwapWordPlace();
    120   }
    121 
    122   void SetEndPos(const CPVT_WordPlace& end) {
    123     EndPos = end;
    124     SwapWordPlace();
    125   }
    126 
    127   FX_BOOL IsExist() const { return BeginPos != EndPos; }
    128 
    129   FX_BOOL operator!=(const CPVT_WordRange& wr) const {
    130     return wr.BeginPos != BeginPos || wr.EndPos != EndPos;
    131   }
    132 
    133   void SwapWordPlace() {
    134     if (BeginPos.WordCmp(EndPos) > 0) {
    135       CPVT_WordPlace place = EndPos;
    136       EndPos = BeginPos;
    137       BeginPos = place;
    138     }
    139   }
    140 
    141   CPVT_WordPlace BeginPos;
    142 
    143   CPVT_WordPlace EndPos;
    144 };
    145 struct CPVT_SecProps {
    146   CPVT_SecProps() : fLineLeading(0.0f), fLineIndent(0.0f), nAlignment(0) {}
    147 
    148   CPVT_SecProps(FX_FLOAT lineLeading, FX_FLOAT lineIndent, int32_t alignment)
    149       : fLineLeading(lineLeading),
    150         fLineIndent(lineIndent),
    151         nAlignment(alignment) {}
    152 
    153   CPVT_SecProps(const CPVT_SecProps& other)
    154       : fLineLeading(other.fLineLeading),
    155         fLineIndent(other.fLineIndent),
    156         nAlignment(other.nAlignment) {}
    157 
    158   FX_FLOAT fLineLeading;
    159 
    160   FX_FLOAT fLineIndent;
    161 
    162   int32_t nAlignment;
    163 };
    164 struct CPVT_WordProps {
    165   CPVT_WordProps()
    166       : nFontIndex(-1),
    167         fFontSize(0.0f),
    168         dwWordColor(0),
    169         nScriptType(0),
    170         nWordStyle(0),
    171         fCharSpace(0.0f),
    172         nHorzScale(0) {}
    173 
    174   CPVT_WordProps(int32_t fontIndex,
    175                  FX_FLOAT fontSize,
    176                  FX_COLORREF wordColor = 0,
    177                  int32_t scriptType = 0,
    178                  int32_t wordStyle = 0,
    179                  FX_FLOAT charSpace = 0,
    180                  int32_t horzScale = 100)
    181       : nFontIndex(fontIndex),
    182         fFontSize(fontSize),
    183         dwWordColor(wordColor),
    184         nScriptType(scriptType),
    185         nWordStyle(wordStyle),
    186         fCharSpace(charSpace),
    187         nHorzScale(horzScale) {}
    188 
    189   CPVT_WordProps(const CPVT_WordProps& other)
    190       : nFontIndex(other.nFontIndex),
    191         fFontSize(other.fFontSize),
    192         dwWordColor(other.dwWordColor),
    193         nScriptType(other.nScriptType),
    194         nWordStyle(other.nWordStyle),
    195         fCharSpace(other.fCharSpace),
    196         nHorzScale(other.nHorzScale) {}
    197 
    198   int32_t nFontIndex;
    199 
    200   FX_FLOAT fFontSize;
    201 
    202   FX_COLORREF dwWordColor;
    203 
    204   int32_t nScriptType;
    205 
    206   int32_t nWordStyle;
    207 
    208   FX_FLOAT fCharSpace;
    209 
    210   int32_t nHorzScale;
    211 };
    212 struct CPVT_Word {
    213   CPVT_Word()
    214       : Word(0),
    215         nCharset(0),
    216         ptWord(0, 0),
    217         fAscent(0.0f),
    218         fDescent(0.0f),
    219         fWidth(0.0f),
    220         fFontSize(0),
    221         WordProps() {}
    222 
    223   FX_WORD Word;
    224 
    225   int32_t nCharset;
    226 
    227   CPVT_WordPlace WordPlace;
    228 
    229   CPDF_Point ptWord;
    230 
    231   FX_FLOAT fAscent;
    232 
    233   FX_FLOAT fDescent;
    234 
    235   FX_FLOAT fWidth;
    236 
    237   int32_t nFontIndex;
    238 
    239   FX_FLOAT fFontSize;
    240 
    241   CPVT_WordProps WordProps;
    242 };
    243 struct CPVT_Line {
    244   CPVT_Line()
    245       : ptLine(0, 0), fLineWidth(0.0f), fLineAscent(0.0f), fLineDescent(0.0f) {}
    246 
    247   CPVT_WordPlace lineplace;
    248 
    249   CPVT_WordPlace lineEnd;
    250 
    251   CPDF_Point ptLine;
    252 
    253   FX_FLOAT fLineWidth;
    254 
    255   FX_FLOAT fLineAscent;
    256 
    257   FX_FLOAT fLineDescent;
    258 };
    259 struct CPVT_Section {
    260   CPVT_WordPlace secplace;
    261 
    262   CPDF_Rect rcSection;
    263 
    264   CPVT_SecProps SecProps;
    265 
    266   CPVT_WordProps WordProps;
    267 };
    268 class IPDF_VariableText_Provider {
    269  public:
    270   virtual ~IPDF_VariableText_Provider() {}
    271 
    272   virtual int32_t GetCharWidth(int32_t nFontIndex,
    273                                FX_WORD word,
    274                                int32_t nWordStyle) = 0;
    275 
    276   virtual int32_t GetTypeAscent(int32_t nFontIndex) = 0;
    277 
    278   virtual int32_t GetTypeDescent(int32_t nFontIndex) = 0;
    279 
    280   virtual int32_t GetWordFontIndex(FX_WORD word,
    281                                    int32_t charset,
    282                                    int32_t nFontIndex) = 0;
    283 
    284   virtual FX_BOOL IsLatinWord(FX_WORD word) = 0;
    285 
    286   virtual int32_t GetDefaultFontIndex() = 0;
    287 };
    288 class IPDF_VariableText_Iterator {
    289  public:
    290   virtual ~IPDF_VariableText_Iterator() {}
    291 
    292   virtual FX_BOOL NextWord() = 0;
    293 
    294   virtual FX_BOOL PrevWord() = 0;
    295 
    296   virtual FX_BOOL NextLine() = 0;
    297 
    298   virtual FX_BOOL PrevLine() = 0;
    299 
    300   virtual FX_BOOL NextSection() = 0;
    301 
    302   virtual FX_BOOL PrevSection() = 0;
    303 
    304   virtual FX_BOOL GetWord(CPVT_Word& word) const = 0;
    305 
    306   virtual FX_BOOL SetWord(const CPVT_Word& word) = 0;
    307 
    308   virtual FX_BOOL GetLine(CPVT_Line& line) const = 0;
    309 
    310   virtual FX_BOOL GetSection(CPVT_Section& section) const = 0;
    311 
    312   virtual FX_BOOL SetSection(const CPVT_Section& section) = 0;
    313 
    314   virtual void SetAt(int32_t nWordIndex) = 0;
    315 
    316   virtual void SetAt(const CPVT_WordPlace& place) = 0;
    317 
    318   virtual const CPVT_WordPlace& GetAt() const = 0;
    319 };
    320 class IPDF_VariableText {
    321  public:
    322   static IPDF_VariableText* NewVariableText();
    323 
    324   static void DelVariableText(IPDF_VariableText* pVT);
    325 
    326   virtual IPDF_VariableText_Provider* SetProvider(
    327       IPDF_VariableText_Provider* pProvider) = 0;
    328 
    329   virtual IPDF_VariableText_Iterator* GetIterator() = 0;
    330 
    331   virtual void SetPlateRect(const CPDF_Rect& rect) = 0;
    332 
    333   virtual void SetAlignment(int32_t nFormat = 0) = 0;
    334 
    335   virtual void SetPasswordChar(FX_WORD wSubWord = '*') = 0;
    336 
    337   virtual void SetLimitChar(int32_t nLimitChar = 0) = 0;
    338 
    339   virtual void SetCharArray(int32_t nCharArray = 0) = 0;
    340 
    341   virtual void SetCharSpace(FX_FLOAT fCharSpace = 0.0f) = 0;
    342 
    343   virtual void SetHorzScale(int32_t nHorzScale = 100) = 0;
    344 
    345   virtual void SetMultiLine(FX_BOOL bMultiLine = TRUE) = 0;
    346 
    347   virtual void SetAutoReturn(FX_BOOL bAuto = TRUE) = 0;
    348 
    349   virtual void SetAutoFontSize(FX_BOOL bAuto = TRUE) = 0;
    350 
    351   virtual void SetFontSize(FX_FLOAT fFontSize) = 0;
    352 
    353   virtual void SetLineLeading(FX_FLOAT fLineLeading) = 0;
    354 
    355   virtual void SetRichText(FX_BOOL bRichText) = 0;
    356 
    357   virtual void Initialize() = 0;
    358 
    359   virtual FX_BOOL IsValid() const = 0;
    360 
    361   virtual FX_BOOL IsRichText() const = 0;
    362 
    363   virtual void RearrangeAll() = 0;
    364 
    365   virtual void RearrangePart(const CPVT_WordRange& PlaceRange) = 0;
    366 
    367   virtual void ResetAll() = 0;
    368 
    369   virtual void SetText(const FX_WCHAR* text,
    370                        int32_t charset = 1,
    371                        const CPVT_SecProps* pSecProps = NULL,
    372                        const CPVT_WordProps* pWordProps = NULL) = 0;
    373 
    374   virtual CPVT_WordPlace InsertWord(
    375       const CPVT_WordPlace& place,
    376       FX_WORD word,
    377       int32_t charset = 1,
    378       const CPVT_WordProps* pWordProps = NULL) = 0;
    379 
    380   virtual CPVT_WordPlace InsertSection(
    381       const CPVT_WordPlace& place,
    382       const CPVT_SecProps* pSecProps = NULL,
    383       const CPVT_WordProps* pWordProps = NULL) = 0;
    384 
    385   virtual CPVT_WordPlace InsertText(
    386       const CPVT_WordPlace& place,
    387       const FX_WCHAR* text,
    388       int32_t charset = 1,
    389       const CPVT_SecProps* pSecProps = NULL,
    390       const CPVT_WordProps* pWordProps = NULL) = 0;
    391 
    392   virtual CPVT_WordPlace DeleteWords(const CPVT_WordRange& PlaceRange) = 0;
    393 
    394   virtual CPVT_WordPlace DeleteWord(const CPVT_WordPlace& place) = 0;
    395 
    396   virtual CPVT_WordPlace BackSpaceWord(const CPVT_WordPlace& place) = 0;
    397 
    398   virtual const CPDF_Rect& GetPlateRect() const = 0;
    399 
    400   virtual CPDF_Rect GetContentRect() const = 0;
    401 
    402   virtual int32_t GetTotalWords() const = 0;
    403 
    404   virtual FX_FLOAT GetFontSize() const = 0;
    405 
    406   virtual int32_t GetAlignment() const = 0;
    407 
    408   virtual FX_WORD GetPasswordChar() const = 0;
    409 
    410   virtual int32_t GetCharArray() const = 0;
    411 
    412   virtual int32_t GetLimitChar() const = 0;
    413 
    414   virtual FX_BOOL IsMultiLine() const = 0;
    415 
    416   virtual int32_t GetHorzScale() const = 0;
    417 
    418   virtual FX_FLOAT GetCharSpace() const = 0;
    419 
    420   virtual CPVT_WordPlace GetBeginWordPlace() const = 0;
    421 
    422   virtual CPVT_WordPlace GetEndWordPlace() const = 0;
    423 
    424   virtual CPVT_WordPlace GetPrevWordPlace(
    425       const CPVT_WordPlace& place) const = 0;
    426 
    427   virtual CPVT_WordPlace GetNextWordPlace(
    428       const CPVT_WordPlace& place) const = 0;
    429 
    430   virtual CPVT_WordPlace SearchWordPlace(const CPDF_Point& point) const = 0;
    431 
    432   virtual CPVT_WordPlace GetUpWordPlace(const CPVT_WordPlace& place,
    433                                         const CPDF_Point& point) const = 0;
    434 
    435   virtual CPVT_WordPlace GetDownWordPlace(const CPVT_WordPlace& place,
    436                                           const CPDF_Point& point) const = 0;
    437 
    438   virtual CPVT_WordPlace GetLineBeginPlace(
    439       const CPVT_WordPlace& place) const = 0;
    440 
    441   virtual CPVT_WordPlace GetLineEndPlace(const CPVT_WordPlace& place) const = 0;
    442 
    443   virtual CPVT_WordPlace GetSectionBeginPlace(
    444       const CPVT_WordPlace& place) const = 0;
    445 
    446   virtual CPVT_WordPlace GetSectionEndPlace(
    447       const CPVT_WordPlace& place) const = 0;
    448 
    449   virtual void UpdateWordPlace(CPVT_WordPlace& place) const = 0;
    450 
    451   virtual CPVT_WordPlace AdjustLineHeader(const CPVT_WordPlace& place,
    452                                           FX_BOOL bPrevOrNext) const = 0;
    453 
    454   virtual int32_t WordPlaceToWordIndex(const CPVT_WordPlace& place) const = 0;
    455 
    456   virtual CPVT_WordPlace WordIndexToWordPlace(int32_t index) const = 0;
    457 
    458  protected:
    459   ~IPDF_VariableText() {}
    460 };
    461 
    462 #endif  // CORE_INCLUDE_FPDFDOC_FPDF_VT_H_
    463