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