Home | History | Annotate | Download | only in fpdfdoc
      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_FPDFDOC_CPDF_VARIABLETEXT_H_
      8 #define CORE_FPDFDOC_CPDF_VARIABLETEXT_H_
      9 
     10 #include <memory>
     11 #include <vector>
     12 
     13 #include "core/fpdfdoc/cpvt_floatrect.h"
     14 #include "core/fpdfdoc/cpvt_line.h"
     15 #include "core/fpdfdoc/cpvt_lineinfo.h"
     16 #include "core/fpdfdoc/cpvt_wordplace.h"
     17 #include "core/fpdfdoc/cpvt_wordrange.h"
     18 #include "core/fxcrt/fx_coordinates.h"
     19 #include "core/fxcrt/fx_string.h"
     20 #include "core/fxcrt/fx_system.h"
     21 
     22 class CPVT_Word;
     23 class CSection;
     24 class IPVT_FontMap;
     25 struct CPVT_WordInfo;
     26 
     27 #define VARIABLETEXT_HALF 0.5f
     28 
     29 class CPDF_VariableText {
     30  public:
     31 
     32   class Iterator {
     33    public:
     34     explicit Iterator(CPDF_VariableText* pVT);
     35     ~Iterator();
     36 
     37     bool NextWord();
     38     bool PrevWord();
     39     bool NextLine();
     40     bool GetWord(CPVT_Word& word) const;
     41     bool GetLine(CPVT_Line& line) const;
     42     void SetAt(int32_t nWordIndex);
     43     void SetAt(const CPVT_WordPlace& place);
     44     const CPVT_WordPlace& GetWordPlace() const { return m_CurPos; }
     45 
     46    private:
     47     CPVT_WordPlace m_CurPos;
     48     UnownedPtr<CPDF_VariableText> const m_pVT;
     49   };
     50 
     51   class Provider {
     52    public:
     53     explicit Provider(IPVT_FontMap* pFontMap);
     54     virtual ~Provider();
     55 
     56     virtual int32_t GetCharWidth(int32_t nFontIndex, uint16_t word);
     57     virtual int32_t GetTypeAscent(int32_t nFontIndex);
     58     virtual int32_t GetTypeDescent(int32_t nFontIndex);
     59     virtual int32_t GetWordFontIndex(uint16_t word,
     60                                      int32_t charset,
     61                                      int32_t nFontIndex);
     62     virtual bool IsLatinWord(uint16_t word);
     63     virtual int32_t GetDefaultFontIndex();
     64 
     65    private:
     66     IPVT_FontMap* const m_pFontMap;
     67   };
     68 
     69   CPDF_VariableText();
     70   ~CPDF_VariableText();
     71 
     72   void SetProvider(CPDF_VariableText::Provider* pProvider);
     73   CPDF_VariableText::Iterator* GetIterator();
     74 
     75   void SetContentRect(const CPVT_FloatRect& rect);
     76   CFX_FloatRect GetContentRect() const;
     77   void SetPlateRect(const CFX_FloatRect& rect);
     78   const CFX_FloatRect& GetPlateRect() const;
     79 
     80   void SetAlignment(int32_t nFormat) { m_nAlignment = nFormat; }
     81   void SetPasswordChar(uint16_t wSubWord) { m_wSubWord = wSubWord; }
     82   void SetLimitChar(int32_t nLimitChar) { m_nLimitChar = nLimitChar; }
     83   void SetCharSpace(float fCharSpace) { m_fCharSpace = fCharSpace; }
     84   void SetMultiLine(bool bMultiLine) { m_bMultiLine = bMultiLine; }
     85   void SetAutoReturn(bool bAuto) { m_bLimitWidth = bAuto; }
     86   void SetFontSize(float fFontSize) { m_fFontSize = fFontSize; }
     87   void SetCharArray(int32_t nCharArray) { m_nCharArray = nCharArray; }
     88   void SetAutoFontSize(bool bAuto) { m_bAutoFontSize = bAuto; }
     89   void Initialize();
     90 
     91   bool IsValid() const { return m_bInitialized; }
     92 
     93   void RearrangeAll();
     94   void RearrangePart(const CPVT_WordRange& PlaceRange);
     95   void SetText(const WideString& text);
     96   CPVT_WordPlace InsertWord(const CPVT_WordPlace& place,
     97                             uint16_t word,
     98                             int32_t charset);
     99   CPVT_WordPlace InsertSection(const CPVT_WordPlace& place);
    100   CPVT_WordPlace DeleteWords(const CPVT_WordRange& PlaceRange);
    101   CPVT_WordPlace DeleteWord(const CPVT_WordPlace& place);
    102   CPVT_WordPlace BackSpaceWord(const CPVT_WordPlace& place);
    103 
    104   int32_t GetTotalWords() const;
    105   float GetFontSize() const { return m_fFontSize; }
    106   int32_t GetAlignment() const { return m_nAlignment; }
    107   uint16_t GetPasswordChar() const { return GetSubWord(); }
    108   int32_t GetCharArray() const { return m_nCharArray; }
    109   int32_t GetLimitChar() const { return m_nLimitChar; }
    110   bool IsMultiLine() const { return m_bMultiLine; }
    111   int32_t GetHorzScale() const { return m_nHorzScale; }
    112   float GetCharSpace() const { return m_fCharSpace; }
    113   bool IsAutoReturn() const { return m_bLimitWidth; }
    114 
    115   CPVT_WordPlace GetBeginWordPlace() const;
    116   CPVT_WordPlace GetEndWordPlace() const;
    117   CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace& place) const;
    118   CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace& place) const;
    119   CPVT_WordPlace SearchWordPlace(const CFX_PointF& point) const;
    120   CPVT_WordPlace GetUpWordPlace(const CPVT_WordPlace& place,
    121                                 const CFX_PointF& point) const;
    122   CPVT_WordPlace GetDownWordPlace(const CPVT_WordPlace& place,
    123                                   const CFX_PointF& point) const;
    124   CPVT_WordPlace GetLineBeginPlace(const CPVT_WordPlace& place) const;
    125   CPVT_WordPlace GetLineEndPlace(const CPVT_WordPlace& place) const;
    126   CPVT_WordPlace GetSectionBeginPlace(const CPVT_WordPlace& place) const;
    127   CPVT_WordPlace GetSectionEndPlace(const CPVT_WordPlace& place) const;
    128   void UpdateWordPlace(CPVT_WordPlace& place) const;
    129   CPVT_WordPlace AdjustLineHeader(const CPVT_WordPlace& place,
    130                                   bool bPrevOrNext) const;
    131   int32_t WordPlaceToWordIndex(const CPVT_WordPlace& place) const;
    132   CPVT_WordPlace WordIndexToWordPlace(int32_t index) const;
    133 
    134   uint16_t GetSubWord() const { return m_wSubWord; }
    135 
    136   float GetPlateWidth() const { return m_rcPlate.right - m_rcPlate.left; }
    137   float GetPlateHeight() const { return m_rcPlate.top - m_rcPlate.bottom; }
    138   CFX_PointF GetBTPoint() const;
    139   CFX_PointF GetETPoint() const;
    140 
    141   CFX_PointF InToOut(const CFX_PointF& point) const;
    142   CFX_PointF OutToIn(const CFX_PointF& point) const;
    143   CFX_FloatRect InToOut(const CPVT_FloatRect& rect) const;
    144   CPVT_FloatRect OutToIn(const CFX_FloatRect& rect) const;
    145 
    146   float GetFontAscent(int32_t nFontIndex, float fFontSize);
    147   float GetFontDescent(int32_t nFontIndex, float fFontSize);
    148   int32_t GetDefaultFontIndex();
    149   float GetLineLeading();
    150   int32_t GetAlignment();
    151   float GetWordWidth(const CPVT_WordInfo& WordInfo);
    152   float GetWordWidth(int32_t nFontIndex,
    153                      uint16_t Word,
    154                      uint16_t SubWord,
    155                      float fCharSpace,
    156                      int32_t nHorzScale,
    157                      float fFontSize,
    158                      float fWordTail);
    159   float GetWordAscent(const CPVT_WordInfo& WordInfo);
    160   float GetWordDescent(const CPVT_WordInfo& WordInfo);
    161   float GetWordAscent(const CPVT_WordInfo& WordInfo, float fFontSize);
    162   float GetWordDescent(const CPVT_WordInfo& WordInfo, float fFontSize);
    163   float GetLineAscent();
    164   float GetLineDescent();
    165   float GetLineIndent();
    166 
    167  private:
    168   int32_t GetCharWidth(int32_t nFontIndex, uint16_t Word, uint16_t SubWord);
    169   int32_t GetTypeAscent(int32_t nFontIndex);
    170   int32_t GetTypeDescent(int32_t nFontIndex);
    171   int32_t GetWordFontIndex(uint16_t word, int32_t charset, int32_t nFontIndex);
    172   bool IsLatinWord(uint16_t word);
    173 
    174   CPVT_WordPlace AddSection(const CPVT_WordPlace& place);
    175   CPVT_WordPlace AddLine(const CPVT_WordPlace& place,
    176                          const CPVT_LineInfo& lineinfo);
    177   CPVT_WordPlace AddWord(const CPVT_WordPlace& place,
    178                          const CPVT_WordInfo& wordinfo);
    179   bool GetWordInfo(const CPVT_WordPlace& place, CPVT_WordInfo& wordinfo);
    180   bool SetWordInfo(const CPVT_WordPlace& place, const CPVT_WordInfo& wordinfo);
    181   bool GetLineInfo(const CPVT_WordPlace& place, CPVT_LineInfo& lineinfo);
    182   float GetWordFontSize();
    183   int32_t GetWordFontIndex(const CPVT_WordInfo& WordInfo);
    184 
    185   void ClearSectionRightWords(const CPVT_WordPlace& place);
    186 
    187   bool ClearEmptySection(const CPVT_WordPlace& place);
    188   void ClearEmptySections(const CPVT_WordRange& PlaceRange);
    189   void LinkLatterSection(const CPVT_WordPlace& place);
    190   void ClearWords(const CPVT_WordRange& PlaceRange);
    191   CPVT_WordPlace ClearLeftWord(const CPVT_WordPlace& place);
    192   CPVT_WordPlace ClearRightWord(const CPVT_WordPlace& place);
    193 
    194   CPVT_FloatRect Rearrange(const CPVT_WordRange& PlaceRange);
    195   float GetAutoFontSize();
    196   bool IsBigger(float fFontSize) const;
    197   CPVT_FloatRect RearrangeSections(const CPVT_WordRange& PlaceRange);
    198 
    199   std::vector<std::unique_ptr<CSection>> m_SectionArray;
    200   int32_t m_nLimitChar;
    201   int32_t m_nCharArray;
    202   bool m_bMultiLine;
    203   bool m_bLimitWidth;
    204   bool m_bAutoFontSize;
    205   int32_t m_nAlignment;
    206   float m_fLineLeading;
    207   float m_fCharSpace;
    208   int32_t m_nHorzScale;
    209   uint16_t m_wSubWord;
    210   float m_fFontSize;
    211   bool m_bInitialized;
    212   CPDF_VariableText::Provider* m_pVTProvider;
    213   std::unique_ptr<CPDF_VariableText::Iterator> m_pVTIterator;
    214   CFX_FloatRect m_rcPlate;
    215   CPVT_FloatRect m_rcContent;
    216 };
    217 
    218 #endif  // CORE_FPDFDOC_CPDF_VARIABLETEXT_H_
    219