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 #include "core/fxcrt/unowned_ptr.h" 22 23 class CPVT_Word; 24 class CSection; 25 class IPVT_FontMap; 26 struct CPVT_WordInfo; 27 28 #define VARIABLETEXT_HALF 0.5f 29 30 class CPDF_VariableText { 31 public: 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); GetWordPlace()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 uint32_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 UnownedPtr<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 SetAlignment(int32_t nFormat)80 void SetAlignment(int32_t nFormat) { m_nAlignment = nFormat; } SetPasswordChar(uint16_t wSubWord)81 void SetPasswordChar(uint16_t wSubWord) { m_wSubWord = wSubWord; } SetLimitChar(int32_t nLimitChar)82 void SetLimitChar(int32_t nLimitChar) { m_nLimitChar = nLimitChar; } SetCharSpace(float fCharSpace)83 void SetCharSpace(float fCharSpace) { m_fCharSpace = fCharSpace; } SetMultiLine(bool bMultiLine)84 void SetMultiLine(bool bMultiLine) { m_bMultiLine = bMultiLine; } SetAutoReturn(bool bAuto)85 void SetAutoReturn(bool bAuto) { m_bLimitWidth = bAuto; } SetFontSize(float fFontSize)86 void SetFontSize(float fFontSize) { m_fFontSize = fFontSize; } SetCharArray(int32_t nCharArray)87 void SetCharArray(int32_t nCharArray) { m_nCharArray = nCharArray; } SetAutoFontSize(bool bAuto)88 void SetAutoFontSize(bool bAuto) { m_bAutoFontSize = bAuto; } 89 void Initialize(); 90 IsValid()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; GetFontSize()105 float GetFontSize() const { return m_fFontSize; } GetAlignment()106 int32_t GetAlignment() const { return m_nAlignment; } GetPasswordChar()107 uint16_t GetPasswordChar() const { return GetSubWord(); } GetCharArray()108 int32_t GetCharArray() const { return m_nCharArray; } GetLimitChar()109 int32_t GetLimitChar() const { return m_nLimitChar; } IsMultiLine()110 bool IsMultiLine() const { return m_bMultiLine; } GetCharSpace()111 float GetCharSpace() const { return m_fCharSpace; } IsAutoReturn()112 bool IsAutoReturn() const { return m_bLimitWidth; } 113 114 CPVT_WordPlace GetBeginWordPlace() const; 115 CPVT_WordPlace GetEndWordPlace() const; 116 CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace& place) const; 117 CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace& place) const; 118 CPVT_WordPlace SearchWordPlace(const CFX_PointF& point) const; 119 CPVT_WordPlace GetUpWordPlace(const CPVT_WordPlace& place, 120 const CFX_PointF& point) const; 121 CPVT_WordPlace GetDownWordPlace(const CPVT_WordPlace& place, 122 const CFX_PointF& point) const; 123 CPVT_WordPlace GetLineBeginPlace(const CPVT_WordPlace& place) const; 124 CPVT_WordPlace GetLineEndPlace(const CPVT_WordPlace& place) const; 125 CPVT_WordPlace GetSectionBeginPlace(const CPVT_WordPlace& place) const; 126 CPVT_WordPlace GetSectionEndPlace(const CPVT_WordPlace& place) const; 127 void UpdateWordPlace(CPVT_WordPlace& place) const; 128 CPVT_WordPlace AdjustLineHeader(const CPVT_WordPlace& place, 129 bool bPrevOrNext) const; 130 int32_t WordPlaceToWordIndex(const CPVT_WordPlace& place) const; 131 CPVT_WordPlace WordIndexToWordPlace(int32_t index) const; 132 GetSubWord()133 uint16_t GetSubWord() const { return m_wSubWord; } 134 GetPlateWidth()135 float GetPlateWidth() const { return m_rcPlate.right - m_rcPlate.left; } GetPlateHeight()136 float GetPlateHeight() const { return m_rcPlate.top - m_rcPlate.bottom; } 137 CFX_PointF GetBTPoint() const; 138 CFX_PointF GetETPoint() const; 139 140 CFX_PointF InToOut(const CFX_PointF& point) const; 141 CFX_PointF OutToIn(const CFX_PointF& point) const; 142 CFX_FloatRect InToOut(const CPVT_FloatRect& rect) const; 143 CPVT_FloatRect OutToIn(const CFX_FloatRect& rect) const; 144 145 float GetFontAscent(int32_t nFontIndex, float fFontSize); 146 float GetFontDescent(int32_t nFontIndex, float fFontSize); 147 int32_t GetDefaultFontIndex(); 148 float GetLineLeading(); 149 int32_t GetAlignment(); 150 float GetWordWidth(const CPVT_WordInfo& WordInfo); 151 float GetWordWidth(int32_t nFontIndex, 152 uint16_t Word, 153 uint16_t SubWord, 154 float fCharSpace, 155 float fFontSize, 156 float fWordTail); 157 float GetWordAscent(const CPVT_WordInfo& WordInfo); 158 float GetWordDescent(const CPVT_WordInfo& WordInfo); 159 float GetWordAscent(const CPVT_WordInfo& WordInfo, float fFontSize); 160 float GetWordDescent(const CPVT_WordInfo& WordInfo, float fFontSize); 161 float GetLineAscent(); 162 float GetLineDescent(); 163 float GetLineIndent(); 164 165 private: 166 uint32_t GetCharWidth(int32_t nFontIndex, uint16_t Word, uint16_t SubWord); 167 int32_t GetTypeAscent(int32_t nFontIndex); 168 int32_t GetTypeDescent(int32_t nFontIndex); 169 int32_t GetWordFontIndex(uint16_t word, int32_t charset, int32_t nFontIndex); 170 bool IsLatinWord(uint16_t word); 171 172 CPVT_WordPlace AddSection(const CPVT_WordPlace& place); 173 CPVT_WordPlace AddLine(const CPVT_WordPlace& place, 174 const CPVT_LineInfo& lineinfo); 175 CPVT_WordPlace AddWord(const CPVT_WordPlace& place, 176 const CPVT_WordInfo& wordinfo); 177 float GetWordFontSize(); 178 int32_t GetWordFontIndex(const CPVT_WordInfo& WordInfo); 179 180 void ClearSectionRightWords(const CPVT_WordPlace& place); 181 182 bool ClearEmptySection(const CPVT_WordPlace& place); 183 void ClearEmptySections(const CPVT_WordRange& PlaceRange); 184 void LinkLatterSection(const CPVT_WordPlace& place); 185 void ClearWords(const CPVT_WordRange& PlaceRange); 186 CPVT_WordPlace ClearLeftWord(const CPVT_WordPlace& place); 187 CPVT_WordPlace ClearRightWord(const CPVT_WordPlace& place); 188 189 CPVT_FloatRect Rearrange(const CPVT_WordRange& PlaceRange); 190 float GetAutoFontSize(); 191 bool IsBigger(float fFontSize) const; 192 CPVT_FloatRect RearrangeSections(const CPVT_WordRange& PlaceRange); 193 194 bool m_bInitialized = false; 195 bool m_bMultiLine = false; 196 bool m_bLimitWidth = false; 197 bool m_bAutoFontSize = false; 198 uint16_t m_wSubWord = 0; 199 int32_t m_nLimitChar = 0; 200 int32_t m_nCharArray = 0; 201 int32_t m_nAlignment = 0; 202 float m_fLineLeading = 0.0f; 203 float m_fCharSpace = 0.0f; 204 float m_fFontSize = 0.0f; 205 std::vector<std::unique_ptr<CSection>> m_SectionArray; 206 UnownedPtr<CPDF_VariableText::Provider> m_pVTProvider; 207 std::unique_ptr<CPDF_VariableText::Iterator> m_pVTIterator; 208 CFX_FloatRect m_rcPlate; 209 CPVT_FloatRect m_rcContent; 210 }; 211 212 #endif // CORE_FPDFDOC_CPDF_VARIABLETEXT_H_ 213