blob: 5b0c259952944a8fd02861e10b358bcc13b263aa [file] [log] [blame]
dsinclairc7a73492016-04-05 12:01:42 -07001// 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
dsinclair1727aee2016-09-29 13:12:56 -07007#ifndef CORE_FPDFDOC_CPDF_VARIABLETEXT_H_
8#define CORE_FPDFDOC_CPDF_VARIABLETEXT_H_
dsinclairc7a73492016-04-05 12:01:42 -07009
thestig821d59e2016-05-11 12:59:22 -070010#include <memory>
Tom Sepez32b94552017-03-20 16:06:15 -070011#include <vector>
thestig821d59e2016-05-11 12:59:22 -070012
dsinclairc7a73492016-04-05 12:01:42 -070013#include "core/fpdfdoc/cpvt_floatrect.h"
dsinclair1727aee2016-09-29 13:12:56 -070014#include "core/fpdfdoc/cpvt_line.h"
dsinclairc7a73492016-04-05 12:01:42 -070015#include "core/fpdfdoc/cpvt_lineinfo.h"
dsinclair1727aee2016-09-29 13:12:56 -070016#include "core/fpdfdoc/cpvt_wordplace.h"
17#include "core/fpdfdoc/cpvt_wordrange.h"
dsinclaira52ab742016-09-29 13:59:29 -070018#include "core/fxcrt/fx_coordinates.h"
19#include "core/fxcrt/fx_string.h"
20#include "core/fxcrt/fx_system.h"
dsinclair74a34fc2016-09-29 16:41:42 -070021#include "core/fxge/fx_font.h"
dsinclairc7a73492016-04-05 12:01:42 -070022
Dan Sinclairf528eee2017-02-14 11:52:07 -050023class CPVT_Word;
dsinclairc7a73492016-04-05 12:01:42 -070024class CSection;
25class IPVT_FontMap;
dsinclairc7a73492016-04-05 12:01:42 -070026struct CPVT_SecProps;
27struct CPVT_Section;
28struct CPVT_SectionInfo;
Dan Sinclairf528eee2017-02-14 11:52:07 -050029
dsinclairc7a73492016-04-05 12:01:42 -070030struct CPVT_WordInfo;
thestig2c3a16a2016-05-10 13:24:16 -070031struct CPVT_WordProps;
dsinclairc7a73492016-04-05 12:01:42 -070032
33#define VARIABLETEXT_HALF 0.5f
34
dsinclair448c4332016-08-02 12:07:35 -070035class CPDF_VariableText {
dsinclairc7a73492016-04-05 12:01:42 -070036 public:
37 enum class ScriptType { Normal, Super, Sub };
38
39 class Iterator {
40 public:
41 explicit Iterator(CPDF_VariableText* pVT);
42 ~Iterator();
43
tsepez12f3e4a2016-11-02 15:17:29 -070044 bool NextWord();
45 bool PrevWord();
46 bool NextLine();
47 bool PrevLine();
48 bool NextSection();
49 bool PrevSection();
50 bool SetWord(const CPVT_Word& word);
51 bool GetWord(CPVT_Word& word) const;
52 bool GetLine(CPVT_Line& line) const;
53 bool GetSection(CPVT_Section& section) const;
54 bool SetSection(const CPVT_Section& section);
dsinclairc7a73492016-04-05 12:01:42 -070055 void SetAt(int32_t nWordIndex);
56 void SetAt(const CPVT_WordPlace& place);
57 const CPVT_WordPlace& GetAt() const { return m_CurPos; }
58
59 private:
60 CPVT_WordPlace m_CurPos;
Dan Sinclairaee0db02017-09-21 16:53:58 -040061 UnownedPtr<CPDF_VariableText> const m_pVT;
dsinclairc7a73492016-04-05 12:01:42 -070062 };
63
64 class Provider {
65 public:
thestig2c3a16a2016-05-10 13:24:16 -070066 explicit Provider(IPVT_FontMap* pFontMap);
dsinclairc7a73492016-04-05 12:01:42 -070067 virtual ~Provider();
68
npm41d6bbe2016-09-14 11:54:44 -070069 virtual int32_t GetCharWidth(int32_t nFontIndex, uint16_t word);
dsinclairc7a73492016-04-05 12:01:42 -070070 virtual int32_t GetTypeAscent(int32_t nFontIndex);
71 virtual int32_t GetTypeDescent(int32_t nFontIndex);
72 virtual int32_t GetWordFontIndex(uint16_t word,
73 int32_t charset,
74 int32_t nFontIndex);
tsepez12f3e4a2016-11-02 15:17:29 -070075 virtual bool IsLatinWord(uint16_t word);
dsinclairc7a73492016-04-05 12:01:42 -070076 virtual int32_t GetDefaultFontIndex();
77
78 private:
thestig594b20b2016-05-12 21:56:43 -070079 IPVT_FontMap* const m_pFontMap;
dsinclairc7a73492016-04-05 12:01:42 -070080 };
81
82 CPDF_VariableText();
dsinclair448c4332016-08-02 12:07:35 -070083 ~CPDF_VariableText();
dsinclairc7a73492016-04-05 12:01:42 -070084
thestig2c3a16a2016-05-10 13:24:16 -070085 void SetProvider(CPDF_VariableText::Provider* pProvider);
dsinclairc7a73492016-04-05 12:01:42 -070086 CPDF_VariableText::Iterator* GetIterator();
87
dsinclair448c4332016-08-02 12:07:35 -070088 void SetContentRect(const CPVT_FloatRect& rect);
89 CFX_FloatRect GetContentRect() const;
90 void SetPlateRect(const CFX_FloatRect& rect);
91 const CFX_FloatRect& GetPlateRect() const;
dsinclairc7a73492016-04-05 12:01:42 -070092
dsinclair74a30b72016-07-18 10:18:56 -070093 void SetAlignment(int32_t nFormat) { m_nAlignment = nFormat; }
94 void SetPasswordChar(uint16_t wSubWord) { m_wSubWord = wSubWord; }
95 void SetLimitChar(int32_t nLimitChar) { m_nLimitChar = nLimitChar; }
Dan Sinclair05df0752017-03-14 14:43:42 -040096 void SetCharSpace(float fCharSpace) { m_fCharSpace = fCharSpace; }
tsepez12f3e4a2016-11-02 15:17:29 -070097 void SetMultiLine(bool bMultiLine) { m_bMultiLine = bMultiLine; }
98 void SetAutoReturn(bool bAuto) { m_bLimitWidth = bAuto; }
Dan Sinclair05df0752017-03-14 14:43:42 -040099 void SetFontSize(float fFontSize) { m_fFontSize = fFontSize; }
dsinclair74a30b72016-07-18 10:18:56 -0700100 void SetCharArray(int32_t nCharArray) { m_nCharArray = nCharArray; }
tsepez12f3e4a2016-11-02 15:17:29 -0700101 void SetAutoFontSize(bool bAuto) { m_bAutoFontSize = bAuto; }
dsinclairc7a73492016-04-05 12:01:42 -0700102 void Initialize();
weilif4bb5802016-06-14 17:21:14 -0700103
Tom Sepez32b94552017-03-20 16:06:15 -0700104 bool IsValid() const { return m_bInitialized; }
weilif4bb5802016-06-14 17:21:14 -0700105
dsinclairc7a73492016-04-05 12:01:42 -0700106 void RearrangeAll();
107 void RearrangePart(const CPVT_WordRange& PlaceRange);
108 void ResetAll();
Ryan Harrison275e2602017-09-18 14:23:18 -0400109 void SetText(const WideString& text);
dsinclairc7a73492016-04-05 12:01:42 -0700110 CPVT_WordPlace InsertWord(const CPVT_WordPlace& place,
111 uint16_t word,
dsinclair74a30b72016-07-18 10:18:56 -0700112 int32_t charset,
113 const CPVT_WordProps* pWordProps);
dsinclairc7a73492016-04-05 12:01:42 -0700114 CPVT_WordPlace InsertSection(const CPVT_WordPlace& place,
dsinclair74a30b72016-07-18 10:18:56 -0700115 const CPVT_SecProps* pSecProps,
116 const CPVT_WordProps* pWordProps);
Dan Sinclair812e96c2017-03-13 16:43:37 -0400117 CPVT_WordPlace InsertText(const CPVT_WordPlace& place, const wchar_t* text);
dsinclairc7a73492016-04-05 12:01:42 -0700118 CPVT_WordPlace DeleteWords(const CPVT_WordRange& PlaceRange);
119 CPVT_WordPlace DeleteWord(const CPVT_WordPlace& place);
120 CPVT_WordPlace BackSpaceWord(const CPVT_WordPlace& place);
121
122 int32_t GetTotalWords() const;
Dan Sinclair05df0752017-03-14 14:43:42 -0400123 float GetFontSize() const { return m_fFontSize; }
dsinclairc7a73492016-04-05 12:01:42 -0700124 int32_t GetAlignment() const { return m_nAlignment; }
125 uint16_t GetPasswordChar() const { return GetSubWord(); }
126 int32_t GetCharArray() const { return m_nCharArray; }
127 int32_t GetLimitChar() const { return m_nLimitChar; }
tsepez12f3e4a2016-11-02 15:17:29 -0700128 bool IsMultiLine() const { return m_bMultiLine; }
dsinclairc7a73492016-04-05 12:01:42 -0700129 int32_t GetHorzScale() const { return m_nHorzScale; }
Dan Sinclair05df0752017-03-14 14:43:42 -0400130 float GetCharSpace() const { return m_fCharSpace; }
dsinclairc7a73492016-04-05 12:01:42 -0700131 CPVT_WordPlace GetBeginWordPlace() const;
132 CPVT_WordPlace GetEndWordPlace() const;
133 CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace& place) const;
134 CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace& place) const;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500135 CPVT_WordPlace SearchWordPlace(const CFX_PointF& point) const;
dsinclairc7a73492016-04-05 12:01:42 -0700136 CPVT_WordPlace GetUpWordPlace(const CPVT_WordPlace& place,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500137 const CFX_PointF& point) const;
dsinclairc7a73492016-04-05 12:01:42 -0700138 CPVT_WordPlace GetDownWordPlace(const CPVT_WordPlace& place,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500139 const CFX_PointF& point) const;
dsinclairc7a73492016-04-05 12:01:42 -0700140 CPVT_WordPlace GetLineBeginPlace(const CPVT_WordPlace& place) const;
141 CPVT_WordPlace GetLineEndPlace(const CPVT_WordPlace& place) const;
142 CPVT_WordPlace GetSectionBeginPlace(const CPVT_WordPlace& place) const;
143 CPVT_WordPlace GetSectionEndPlace(const CPVT_WordPlace& place) const;
144 void UpdateWordPlace(CPVT_WordPlace& place) const;
145 CPVT_WordPlace AdjustLineHeader(const CPVT_WordPlace& place,
tsepez12f3e4a2016-11-02 15:17:29 -0700146 bool bPrevOrNext) const;
dsinclairc7a73492016-04-05 12:01:42 -0700147 int32_t WordPlaceToWordIndex(const CPVT_WordPlace& place) const;
148 CPVT_WordPlace WordIndexToWordPlace(int32_t index) const;
149
150 uint16_t GetSubWord() const { return m_wSubWord; }
151
Dan Sinclair05df0752017-03-14 14:43:42 -0400152 float GetPlateWidth() const { return m_rcPlate.right - m_rcPlate.left; }
153 float GetPlateHeight() const { return m_rcPlate.top - m_rcPlate.bottom; }
dsinclair448c4332016-08-02 12:07:35 -0700154 CFX_SizeF GetPlateSize() const;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500155 CFX_PointF GetBTPoint() const;
156 CFX_PointF GetETPoint() const;
dsinclair448c4332016-08-02 12:07:35 -0700157
Dan Sinclairf528eee2017-02-14 11:52:07 -0500158 CFX_PointF InToOut(const CFX_PointF& point) const;
159 CFX_PointF OutToIn(const CFX_PointF& point) const;
dsinclair448c4332016-08-02 12:07:35 -0700160 CFX_FloatRect InToOut(const CPVT_FloatRect& rect) const;
161 CPVT_FloatRect OutToIn(const CFX_FloatRect& rect) const;
162
dsinclairc7a73492016-04-05 12:01:42 -0700163 private:
164 friend class CTypeset;
165 friend class CSection;
166
npm41d6bbe2016-09-14 11:54:44 -0700167 int32_t GetCharWidth(int32_t nFontIndex, uint16_t Word, uint16_t SubWord);
dsinclairc7a73492016-04-05 12:01:42 -0700168 int32_t GetTypeAscent(int32_t nFontIndex);
169 int32_t GetTypeDescent(int32_t nFontIndex);
170 int32_t GetWordFontIndex(uint16_t word, int32_t charset, int32_t nFontIndex);
171 int32_t GetDefaultFontIndex();
tsepez12f3e4a2016-11-02 15:17:29 -0700172 bool IsLatinWord(uint16_t word);
dsinclairc7a73492016-04-05 12:01:42 -0700173
174 CPVT_WordPlace AddSection(const CPVT_WordPlace& place,
175 const CPVT_SectionInfo& secinfo);
176 CPVT_WordPlace AddLine(const CPVT_WordPlace& place,
177 const CPVT_LineInfo& lineinfo);
178 CPVT_WordPlace AddWord(const CPVT_WordPlace& place,
179 const CPVT_WordInfo& wordinfo);
tsepez12f3e4a2016-11-02 15:17:29 -0700180 bool GetWordInfo(const CPVT_WordPlace& place, CPVT_WordInfo& wordinfo);
181 bool SetWordInfo(const CPVT_WordPlace& place, const CPVT_WordInfo& wordinfo);
182 bool GetLineInfo(const CPVT_WordPlace& place, CPVT_LineInfo& lineinfo);
183 bool GetSectionInfo(const CPVT_WordPlace& place, CPVT_SectionInfo& secinfo);
Dan Sinclair05df0752017-03-14 14:43:42 -0400184 float GetWordFontSize(const CPVT_WordInfo& WordInfo);
185 float GetWordWidth(int32_t nFontIndex,
186 uint16_t Word,
187 uint16_t SubWord,
188 float fCharSpace,
189 int32_t nHorzScale,
190 float fFontSize,
191 float fWordTail);
192 float GetWordWidth(const CPVT_WordInfo& WordInfo);
193 float GetWordAscent(const CPVT_WordInfo& WordInfo, float fFontSize);
194 float GetWordDescent(const CPVT_WordInfo& WordInfo, float fFontSize);
195 float GetWordAscent(const CPVT_WordInfo& WordInfo);
196 float GetWordDescent(const CPVT_WordInfo& WordInfo);
197 float GetLineAscent(const CPVT_SectionInfo& SecInfo);
198 float GetLineDescent(const CPVT_SectionInfo& SecInfo);
199 float GetFontAscent(int32_t nFontIndex, float fFontSize);
200 float GetFontDescent(int32_t nFontIndex, float fFontSize);
dsinclairc7a73492016-04-05 12:01:42 -0700201 int32_t GetWordFontIndex(const CPVT_WordInfo& WordInfo);
Dan Sinclair05df0752017-03-14 14:43:42 -0400202 float GetCharSpace(const CPVT_WordInfo& WordInfo);
dsinclairc7a73492016-04-05 12:01:42 -0700203 int32_t GetHorzScale(const CPVT_WordInfo& WordInfo);
Dan Sinclair05df0752017-03-14 14:43:42 -0400204 float GetLineLeading(const CPVT_SectionInfo& SecInfo);
205 float GetLineIndent(const CPVT_SectionInfo& SecInfo);
dsinclairc7a73492016-04-05 12:01:42 -0700206 int32_t GetAlignment(const CPVT_SectionInfo& SecInfo);
207
208 void ClearSectionRightWords(const CPVT_WordPlace& place);
209
tsepez12f3e4a2016-11-02 15:17:29 -0700210 bool ClearEmptySection(const CPVT_WordPlace& place);
dsinclairc7a73492016-04-05 12:01:42 -0700211 void ClearEmptySections(const CPVT_WordRange& PlaceRange);
212 void LinkLatterSection(const CPVT_WordPlace& place);
213 void ClearWords(const CPVT_WordRange& PlaceRange);
214 CPVT_WordPlace ClearLeftWord(const CPVT_WordPlace& place);
215 CPVT_WordPlace ClearRightWord(const CPVT_WordPlace& place);
216
217 CPVT_FloatRect Rearrange(const CPVT_WordRange& PlaceRange);
Dan Sinclair05df0752017-03-14 14:43:42 -0400218 float GetAutoFontSize();
219 bool IsBigger(float fFontSize) const;
dsinclairc7a73492016-04-05 12:01:42 -0700220 CPVT_FloatRect RearrangeSections(const CPVT_WordRange& PlaceRange);
221
Tom Sepez32b94552017-03-20 16:06:15 -0700222 std::vector<std::unique_ptr<CSection>> m_SectionArray;
dsinclairc7a73492016-04-05 12:01:42 -0700223 int32_t m_nLimitChar;
224 int32_t m_nCharArray;
tsepez12f3e4a2016-11-02 15:17:29 -0700225 bool m_bMultiLine;
226 bool m_bLimitWidth;
227 bool m_bAutoFontSize;
dsinclairc7a73492016-04-05 12:01:42 -0700228 int32_t m_nAlignment;
Dan Sinclair05df0752017-03-14 14:43:42 -0400229 float m_fLineLeading;
230 float m_fCharSpace;
dsinclairc7a73492016-04-05 12:01:42 -0700231 int32_t m_nHorzScale;
232 uint16_t m_wSubWord;
Dan Sinclair05df0752017-03-14 14:43:42 -0400233 float m_fFontSize;
Tom Sepez32b94552017-03-20 16:06:15 -0700234 bool m_bInitialized;
dsinclairc7a73492016-04-05 12:01:42 -0700235 CPDF_VariableText::Provider* m_pVTProvider;
thestig821d59e2016-05-11 12:59:22 -0700236 std::unique_ptr<CPDF_VariableText::Iterator> m_pVTIterator;
dsinclair448c4332016-08-02 12:07:35 -0700237 CFX_FloatRect m_rcPlate;
238 CPVT_FloatRect m_rcContent;
dsinclairc7a73492016-04-05 12:01:42 -0700239};
240
dsinclair1727aee2016-09-29 13:12:56 -0700241#endif // CORE_FPDFDOC_CPDF_VARIABLETEXT_H_