K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2016 The PDFium Authors |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 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 | |
Tom Sepez | 012b8e3 | 2021-04-20 19:29:41 +0000 | [diff] [blame] | 7 | #ifndef CORE_FPDFDOC_CPVT_VARIABLETEXT_H_ |
| 8 | #define CORE_FPDFDOC_CPVT_VARIABLETEXT_H_ |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 9 | |
Tom Sepez | 78e069b | 2021-07-27 00:13:22 +0000 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 12 | #include <memory> |
Tom Sepez | 32b9455 | 2017-03-20 16:06:15 -0700 | [diff] [blame] | 13 | #include <vector> |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 14 | |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 15 | #include "core/fpdfdoc/cpvt_floatrect.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 16 | #include "core/fpdfdoc/cpvt_line.h" |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 17 | #include "core/fpdfdoc/cpvt_lineinfo.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 18 | #include "core/fpdfdoc/cpvt_wordplace.h" |
| 19 | #include "core/fpdfdoc/cpvt_wordrange.h" |
Tom Sepez | d683986 | 2021-07-26 22:49:03 +0000 | [diff] [blame] | 20 | #include "core/fxcrt/fx_codepage_forward.h" |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 21 | #include "core/fxcrt/fx_coordinates.h" |
Tom Sepez | ae38636 | 2018-08-21 23:10:51 +0000 | [diff] [blame] | 22 | #include "core/fxcrt/unowned_ptr.h" |
Tom Sepez | 1e151c6 | 2021-11-22 23:46:09 +0000 | [diff] [blame] | 23 | #include "core/fxcrt/widestring.h" |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 24 | |
Lei Zhang | f3a1c43 | 2021-04-06 20:57:48 +0000 | [diff] [blame] | 25 | class CPVT_Section; |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 26 | class CPVT_Word; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 27 | class IPVT_FontMap; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 28 | struct CPVT_WordInfo; |
| 29 | |
Tom Sepez | 012b8e3 | 2021-04-20 19:29:41 +0000 | [diff] [blame] | 30 | class CPVT_VariableText { |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 31 | public: |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 32 | class Iterator { |
| 33 | public: |
Tom Sepez | 012b8e3 | 2021-04-20 19:29:41 +0000 | [diff] [blame] | 34 | explicit Iterator(CPVT_VariableText* pVT); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 35 | ~Iterator(); |
| 36 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 37 | bool NextWord(); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 38 | bool NextLine(); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 39 | bool GetWord(CPVT_Word& word) const; |
| 40 | bool GetLine(CPVT_Line& line) const; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 41 | void SetAt(int32_t nWordIndex); |
| 42 | void SetAt(const CPVT_WordPlace& place); |
Nicolas Pena | cbcae9b | 2018-01-03 15:43:25 -0500 | [diff] [blame] | 43 | const CPVT_WordPlace& GetWordPlace() const { return m_CurPos; } |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | CPVT_WordPlace m_CurPos; |
Tom Sepez | fe7dcb0 | 2021-04-22 16:53:45 +0000 | [diff] [blame] | 47 | UnownedPtr<const CPVT_VariableText> const m_pVT; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | class Provider { |
| 51 | public: |
thestig | 2c3a16a | 2016-05-10 13:24:16 -0700 | [diff] [blame] | 52 | explicit Provider(IPVT_FontMap* pFontMap); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 53 | virtual ~Provider(); |
| 54 | |
Lei Zhang | 70e6619 | 2020-09-18 20:50:51 +0000 | [diff] [blame] | 55 | virtual int GetCharWidth(int32_t nFontIndex, uint16_t word); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 56 | virtual int32_t GetTypeAscent(int32_t nFontIndex); |
| 57 | virtual int32_t GetTypeDescent(int32_t nFontIndex); |
| 58 | virtual int32_t GetWordFontIndex(uint16_t word, |
Tom Sepez | d683986 | 2021-07-26 22:49:03 +0000 | [diff] [blame] | 59 | FX_Charset charset, |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 60 | int32_t nFontIndex); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 61 | virtual int32_t GetDefaultFontIndex(); |
| 62 | |
Tom Sepez | bb9bf7d | 2022-10-28 15:57:03 +0000 | [diff] [blame] | 63 | IPVT_FontMap* GetFontMap() { return m_pFontMap; } |
Lei Zhang | 0c3b5d0 | 2021-04-07 17:42:39 +0000 | [diff] [blame] | 64 | |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 65 | private: |
Tom Sepez | ae38636 | 2018-08-21 23:10:51 +0000 | [diff] [blame] | 66 | UnownedPtr<IPVT_FontMap> const m_pFontMap; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Tom Sepez | 27b4a3c | 2022-08-09 01:50:56 +0000 | [diff] [blame] | 69 | explicit CPVT_VariableText(Provider* Provider); |
Tom Sepez | 012b8e3 | 2021-04-20 19:29:41 +0000 | [diff] [blame] | 70 | ~CPVT_VariableText(); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 71 | |
Tom Sepez | 27b4a3c | 2022-08-09 01:50:56 +0000 | [diff] [blame] | 72 | void SetProvider(Provider* pProvider); |
Tom Sepez | 012b8e3 | 2021-04-20 19:29:41 +0000 | [diff] [blame] | 73 | CPVT_VariableText::Iterator* GetIterator(); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 74 | |
dsinclair | 448c433 | 2016-08-02 12:07:35 -0700 | [diff] [blame] | 75 | CFX_FloatRect GetContentRect() const; |
| 76 | void SetPlateRect(const CFX_FloatRect& rect); |
| 77 | const CFX_FloatRect& GetPlateRect() const; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 78 | |
dsinclair | 74a30b7 | 2016-07-18 10:18:56 -0700 | [diff] [blame] | 79 | void SetAlignment(int32_t nFormat) { m_nAlignment = nFormat; } |
| 80 | void SetPasswordChar(uint16_t wSubWord) { m_wSubWord = wSubWord; } |
| 81 | void SetLimitChar(int32_t nLimitChar) { m_nLimitChar = nLimitChar; } |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 82 | void SetMultiLine(bool bMultiLine) { m_bMultiLine = bMultiLine; } |
| 83 | void SetAutoReturn(bool bAuto) { m_bLimitWidth = bAuto; } |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 84 | void SetFontSize(float fFontSize) { m_fFontSize = fFontSize; } |
dsinclair | 74a30b7 | 2016-07-18 10:18:56 -0700 | [diff] [blame] | 85 | void SetCharArray(int32_t nCharArray) { m_nCharArray = nCharArray; } |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 86 | void SetAutoFontSize(bool bAuto) { m_bAutoFontSize = bAuto; } |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 87 | void Initialize(); |
weili | f4bb580 | 2016-06-14 17:21:14 -0700 | [diff] [blame] | 88 | |
Tom Sepez | 32b9455 | 2017-03-20 16:06:15 -0700 | [diff] [blame] | 89 | bool IsValid() const { return m_bInitialized; } |
weili | f4bb580 | 2016-06-14 17:21:14 -0700 | [diff] [blame] | 90 | |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 91 | void RearrangeAll(); |
| 92 | void RearrangePart(const CPVT_WordRange& PlaceRange); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 93 | void SetText(const WideString& text); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 94 | CPVT_WordPlace InsertWord(const CPVT_WordPlace& place, |
| 95 | uint16_t word, |
Tom Sepez | d683986 | 2021-07-26 22:49:03 +0000 | [diff] [blame] | 96 | FX_Charset charset); |
Dan Sinclair | b01848a | 2017-10-05 14:27:34 -0400 | [diff] [blame] | 97 | CPVT_WordPlace InsertSection(const CPVT_WordPlace& place); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 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; |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 103 | float GetFontSize() const { return m_fFontSize; } |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 104 | int32_t GetAlignment() const { return m_nAlignment; } |
| 105 | uint16_t GetPasswordChar() const { return GetSubWord(); } |
| 106 | int32_t GetCharArray() const { return m_nCharArray; } |
| 107 | int32_t GetLimitChar() const { return m_nLimitChar; } |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 108 | bool IsMultiLine() const { return m_bMultiLine; } |
Dan Sinclair | 3862d24 | 2017-10-05 12:02:15 -0400 | [diff] [blame] | 109 | bool IsAutoReturn() const { return m_bLimitWidth; } |
| 110 | |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 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; |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 115 | CPVT_WordPlace SearchWordPlace(const CFX_PointF& point) const; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 116 | CPVT_WordPlace GetUpWordPlace(const CPVT_WordPlace& place, |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 117 | const CFX_PointF& point) const; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 118 | CPVT_WordPlace GetDownWordPlace(const CPVT_WordPlace& place, |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 119 | const CFX_PointF& point) const; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 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; |
Tom Sepez | 892acec | 2021-05-25 19:12:21 +0000 | [diff] [blame] | 125 | CPVT_WordPlace PrevLineHeaderPlace(const CPVT_WordPlace& place) const; |
| 126 | CPVT_WordPlace NextLineHeaderPlace(const CPVT_WordPlace& place) const; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 127 | int32_t WordPlaceToWordIndex(const CPVT_WordPlace& place) const; |
| 128 | CPVT_WordPlace WordIndexToWordPlace(int32_t index) const; |
| 129 | |
| 130 | uint16_t GetSubWord() const { return m_wSubWord; } |
| 131 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 132 | float GetPlateWidth() const { return m_rcPlate.right - m_rcPlate.left; } |
| 133 | float GetPlateHeight() const { return m_rcPlate.top - m_rcPlate.bottom; } |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 134 | CFX_PointF GetBTPoint() const; |
| 135 | CFX_PointF GetETPoint() const; |
dsinclair | 448c433 | 2016-08-02 12:07:35 -0700 | [diff] [blame] | 136 | |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 137 | CFX_PointF InToOut(const CFX_PointF& point) const; |
| 138 | CFX_PointF OutToIn(const CFX_PointF& point) const; |
dsinclair | 448c433 | 2016-08-02 12:07:35 -0700 | [diff] [blame] | 139 | CFX_FloatRect InToOut(const CPVT_FloatRect& rect) const; |
dsinclair | 448c433 | 2016-08-02 12:07:35 -0700 | [diff] [blame] | 140 | |
Tom Sepez | fe7dcb0 | 2021-04-22 16:53:45 +0000 | [diff] [blame] | 141 | float GetFontAscent(int32_t nFontIndex, float fFontSize) const; |
| 142 | float GetFontDescent(int32_t nFontIndex, float fFontSize) const; |
Dan Sinclair | 3862d24 | 2017-10-05 12:02:15 -0400 | [diff] [blame] | 143 | int32_t GetDefaultFontIndex(); |
Dan Sinclair | b01848a | 2017-10-05 14:27:34 -0400 | [diff] [blame] | 144 | float GetLineLeading(); |
Tom Sepez | fe7dcb0 | 2021-04-22 16:53:45 +0000 | [diff] [blame] | 145 | float GetWordWidth(const CPVT_WordInfo& WordInfo) const; |
Dan Sinclair | 3862d24 | 2017-10-05 12:02:15 -0400 | [diff] [blame] | 146 | float GetWordWidth(int32_t nFontIndex, |
| 147 | uint16_t Word, |
| 148 | uint16_t SubWord, |
Dan Sinclair | 3862d24 | 2017-10-05 12:02:15 -0400 | [diff] [blame] | 149 | float fFontSize, |
Tom Sepez | fe7dcb0 | 2021-04-22 16:53:45 +0000 | [diff] [blame] | 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; |
Dan Sinclair | b01848a | 2017-10-05 14:27:34 -0400 | [diff] [blame] | 155 | float GetLineAscent(); |
| 156 | float GetLineDescent(); |
| 157 | float GetLineIndent(); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 158 | |
Dan Sinclair | 3862d24 | 2017-10-05 12:02:15 -0400 | [diff] [blame] | 159 | private: |
Tom Sepez | fe7dcb0 | 2021-04-22 16:53:45 +0000 | [diff] [blame] | 160 | int GetCharWidth(int32_t nFontIndex, uint16_t Word, uint16_t SubWord) const; |
Tom Sepez | d683986 | 2021-07-26 22:49:03 +0000 | [diff] [blame] | 161 | int32_t GetWordFontIndex(uint16_t word, |
| 162 | FX_Charset charset, |
| 163 | int32_t nFontIndex); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 164 | |
Dan Sinclair | d103b96 | 2017-10-05 15:48:39 -0400 | [diff] [blame] | 165 | CPVT_WordPlace AddSection(const CPVT_WordPlace& place); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 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); |
Tom Sepez | fe7dcb0 | 2021-04-22 16:53:45 +0000 | [diff] [blame] | 170 | float GetWordFontSize() const; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 171 | |
| 172 | void ClearSectionRightWords(const CPVT_WordPlace& place); |
| 173 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 174 | bool ClearEmptySection(const CPVT_WordPlace& place); |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 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 | |
Lei Zhang | 38737c6 | 2021-04-07 02:05:59 +0000 | [diff] [blame] | 181 | void Rearrange(const CPVT_WordRange& PlaceRange); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 182 | float GetAutoFontSize(); |
| 183 | bool IsBigger(float fFontSize) const; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 184 | CPVT_FloatRect RearrangeSections(const CPVT_WordRange& PlaceRange); |
| 185 | |
Tom Sepez | ae38636 | 2018-08-21 23:10:51 +0000 | [diff] [blame] | 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; |
Tom Sepez | ae38636 | 2018-08-21 23:10:51 +0000 | [diff] [blame] | 194 | float m_fLineLeading = 0.0f; |
Tom Sepez | ae38636 | 2018-08-21 23:10:51 +0000 | [diff] [blame] | 195 | float m_fFontSize = 0.0f; |
Lei Zhang | f3a1c43 | 2021-04-06 20:57:48 +0000 | [diff] [blame] | 196 | std::vector<std::unique_ptr<CPVT_Section>> m_SectionArray; |
Tom Sepez | fe7dcb0 | 2021-04-22 16:53:45 +0000 | [diff] [blame] | 197 | UnownedPtr<Provider> m_pVTProvider; |
| 198 | std::unique_ptr<Iterator> m_pVTIterator; |
dsinclair | 448c433 | 2016-08-02 12:07:35 -0700 | [diff] [blame] | 199 | CFX_FloatRect m_rcPlate; |
| 200 | CPVT_FloatRect m_rcContent; |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Tom Sepez | 012b8e3 | 2021-04-20 19:29:41 +0000 | [diff] [blame] | 203 | #endif // CORE_FPDFDOC_CPVT_VARIABLETEXT_H_ |