blob: a378dbbfd811dc58afa99947f1e45200bb1d2c4a [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2016 The PDFium Authors
dsinclairc7a73492016-04-05 12:01:42 -07002// 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 Sepez012b8e32021-04-20 19:29:41 +00007#ifndef CORE_FPDFDOC_CPVT_VARIABLETEXT_H_
8#define CORE_FPDFDOC_CPVT_VARIABLETEXT_H_
dsinclairc7a73492016-04-05 12:01:42 -07009
Tom Sepez78e069b2021-07-27 00:13:22 +000010#include <stdint.h>
11
thestig821d59e2016-05-11 12:59:22 -070012#include <memory>
Tom Sepez32b94552017-03-20 16:06:15 -070013#include <vector>
thestig821d59e2016-05-11 12:59:22 -070014
dsinclairc7a73492016-04-05 12:01:42 -070015#include "core/fpdfdoc/cpvt_floatrect.h"
dsinclair1727aee2016-09-29 13:12:56 -070016#include "core/fpdfdoc/cpvt_line.h"
dsinclairc7a73492016-04-05 12:01:42 -070017#include "core/fpdfdoc/cpvt_lineinfo.h"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpvt_wordplace.h"
19#include "core/fpdfdoc/cpvt_wordrange.h"
Tom Sepezd6839862021-07-26 22:49:03 +000020#include "core/fxcrt/fx_codepage_forward.h"
dsinclaira52ab742016-09-29 13:59:29 -070021#include "core/fxcrt/fx_coordinates.h"
Tom Sepezae386362018-08-21 23:10:51 +000022#include "core/fxcrt/unowned_ptr.h"
Tom Sepez1e151c62021-11-22 23:46:09 +000023#include "core/fxcrt/widestring.h"
dsinclairc7a73492016-04-05 12:01:42 -070024
Lei Zhangf3a1c432021-04-06 20:57:48 +000025class CPVT_Section;
Dan Sinclairf528eee2017-02-14 11:52:07 -050026class CPVT_Word;
dsinclairc7a73492016-04-05 12:01:42 -070027class IPVT_FontMap;
dsinclairc7a73492016-04-05 12:01:42 -070028struct CPVT_WordInfo;
29
Tom Sepez012b8e32021-04-20 19:29:41 +000030class CPVT_VariableText {
dsinclairc7a73492016-04-05 12:01:42 -070031 public:
dsinclairc7a73492016-04-05 12:01:42 -070032 class Iterator {
33 public:
Tom Sepez012b8e32021-04-20 19:29:41 +000034 explicit Iterator(CPVT_VariableText* pVT);
dsinclairc7a73492016-04-05 12:01:42 -070035 ~Iterator();
36
tsepez12f3e4a2016-11-02 15:17:29 -070037 bool NextWord();
tsepez12f3e4a2016-11-02 15:17:29 -070038 bool NextLine();
tsepez12f3e4a2016-11-02 15:17:29 -070039 bool GetWord(CPVT_Word& word) const;
40 bool GetLine(CPVT_Line& line) const;
dsinclairc7a73492016-04-05 12:01:42 -070041 void SetAt(int32_t nWordIndex);
42 void SetAt(const CPVT_WordPlace& place);
Nicolas Penacbcae9b2018-01-03 15:43:25 -050043 const CPVT_WordPlace& GetWordPlace() const { return m_CurPos; }
dsinclairc7a73492016-04-05 12:01:42 -070044
45 private:
46 CPVT_WordPlace m_CurPos;
Tom Sepezfe7dcb02021-04-22 16:53:45 +000047 UnownedPtr<const CPVT_VariableText> const m_pVT;
dsinclairc7a73492016-04-05 12:01:42 -070048 };
49
50 class Provider {
51 public:
thestig2c3a16a2016-05-10 13:24:16 -070052 explicit Provider(IPVT_FontMap* pFontMap);
dsinclairc7a73492016-04-05 12:01:42 -070053 virtual ~Provider();
54
Lei Zhang70e66192020-09-18 20:50:51 +000055 virtual int GetCharWidth(int32_t nFontIndex, uint16_t word);
dsinclairc7a73492016-04-05 12:01:42 -070056 virtual int32_t GetTypeAscent(int32_t nFontIndex);
57 virtual int32_t GetTypeDescent(int32_t nFontIndex);
58 virtual int32_t GetWordFontIndex(uint16_t word,
Tom Sepezd6839862021-07-26 22:49:03 +000059 FX_Charset charset,
dsinclairc7a73492016-04-05 12:01:42 -070060 int32_t nFontIndex);
dsinclairc7a73492016-04-05 12:01:42 -070061 virtual int32_t GetDefaultFontIndex();
62
Tom Sepezbb9bf7d2022-10-28 15:57:03 +000063 IPVT_FontMap* GetFontMap() { return m_pFontMap; }
Lei Zhang0c3b5d02021-04-07 17:42:39 +000064
dsinclairc7a73492016-04-05 12:01:42 -070065 private:
Tom Sepezae386362018-08-21 23:10:51 +000066 UnownedPtr<IPVT_FontMap> const m_pFontMap;
dsinclairc7a73492016-04-05 12:01:42 -070067 };
68
Tom Sepez27b4a3c2022-08-09 01:50:56 +000069 explicit CPVT_VariableText(Provider* Provider);
Tom Sepez012b8e32021-04-20 19:29:41 +000070 ~CPVT_VariableText();
dsinclairc7a73492016-04-05 12:01:42 -070071
Tom Sepez27b4a3c2022-08-09 01:50:56 +000072 void SetProvider(Provider* pProvider);
Tom Sepez012b8e32021-04-20 19:29:41 +000073 CPVT_VariableText::Iterator* GetIterator();
dsinclairc7a73492016-04-05 12:01:42 -070074
dsinclair448c4332016-08-02 12:07:35 -070075 CFX_FloatRect GetContentRect() const;
76 void SetPlateRect(const CFX_FloatRect& rect);
77 const CFX_FloatRect& GetPlateRect() const;
dsinclairc7a73492016-04-05 12:01:42 -070078
dsinclair74a30b72016-07-18 10:18:56 -070079 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; }
tsepez12f3e4a2016-11-02 15:17:29 -070082 void SetMultiLine(bool bMultiLine) { m_bMultiLine = bMultiLine; }
83 void SetAutoReturn(bool bAuto) { m_bLimitWidth = bAuto; }
Dan Sinclair05df0752017-03-14 14:43:42 -040084 void SetFontSize(float fFontSize) { m_fFontSize = fFontSize; }
dsinclair74a30b72016-07-18 10:18:56 -070085 void SetCharArray(int32_t nCharArray) { m_nCharArray = nCharArray; }
tsepez12f3e4a2016-11-02 15:17:29 -070086 void SetAutoFontSize(bool bAuto) { m_bAutoFontSize = bAuto; }
dsinclairc7a73492016-04-05 12:01:42 -070087 void Initialize();
weilif4bb5802016-06-14 17:21:14 -070088
Tom Sepez32b94552017-03-20 16:06:15 -070089 bool IsValid() const { return m_bInitialized; }
weilif4bb5802016-06-14 17:21:14 -070090
dsinclairc7a73492016-04-05 12:01:42 -070091 void RearrangeAll();
92 void RearrangePart(const CPVT_WordRange& PlaceRange);
Ryan Harrison275e2602017-09-18 14:23:18 -040093 void SetText(const WideString& text);
dsinclairc7a73492016-04-05 12:01:42 -070094 CPVT_WordPlace InsertWord(const CPVT_WordPlace& place,
95 uint16_t word,
Tom Sepezd6839862021-07-26 22:49:03 +000096 FX_Charset charset);
Dan Sinclairb01848a2017-10-05 14:27:34 -040097 CPVT_WordPlace InsertSection(const CPVT_WordPlace& place);
dsinclairc7a73492016-04-05 12:01:42 -070098 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 Sinclair05df0752017-03-14 14:43:42 -0400103 float GetFontSize() const { return m_fFontSize; }
dsinclairc7a73492016-04-05 12:01:42 -0700104 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; }
tsepez12f3e4a2016-11-02 15:17:29 -0700108 bool IsMultiLine() const { return m_bMultiLine; }
Dan Sinclair3862d242017-10-05 12:02:15 -0400109 bool IsAutoReturn() const { return m_bLimitWidth; }
110
dsinclairc7a73492016-04-05 12:01:42 -0700111 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 Sinclairf528eee2017-02-14 11:52:07 -0500115 CPVT_WordPlace SearchWordPlace(const CFX_PointF& point) const;
dsinclairc7a73492016-04-05 12:01:42 -0700116 CPVT_WordPlace GetUpWordPlace(const CPVT_WordPlace& place,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500117 const CFX_PointF& point) const;
dsinclairc7a73492016-04-05 12:01:42 -0700118 CPVT_WordPlace GetDownWordPlace(const CPVT_WordPlace& place,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500119 const CFX_PointF& point) const;
dsinclairc7a73492016-04-05 12:01:42 -0700120 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 Sepez892acec2021-05-25 19:12:21 +0000125 CPVT_WordPlace PrevLineHeaderPlace(const CPVT_WordPlace& place) const;
126 CPVT_WordPlace NextLineHeaderPlace(const CPVT_WordPlace& place) const;
dsinclairc7a73492016-04-05 12:01:42 -0700127 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 Sinclair05df0752017-03-14 14:43:42 -0400132 float GetPlateWidth() const { return m_rcPlate.right - m_rcPlate.left; }
133 float GetPlateHeight() const { return m_rcPlate.top - m_rcPlate.bottom; }
Dan Sinclairf528eee2017-02-14 11:52:07 -0500134 CFX_PointF GetBTPoint() const;
135 CFX_PointF GetETPoint() const;
dsinclair448c4332016-08-02 12:07:35 -0700136
Dan Sinclairf528eee2017-02-14 11:52:07 -0500137 CFX_PointF InToOut(const CFX_PointF& point) const;
138 CFX_PointF OutToIn(const CFX_PointF& point) const;
dsinclair448c4332016-08-02 12:07:35 -0700139 CFX_FloatRect InToOut(const CPVT_FloatRect& rect) const;
dsinclair448c4332016-08-02 12:07:35 -0700140
Tom Sepezfe7dcb02021-04-22 16:53:45 +0000141 float GetFontAscent(int32_t nFontIndex, float fFontSize) const;
142 float GetFontDescent(int32_t nFontIndex, float fFontSize) const;
Dan Sinclair3862d242017-10-05 12:02:15 -0400143 int32_t GetDefaultFontIndex();
Dan Sinclairb01848a2017-10-05 14:27:34 -0400144 float GetLineLeading();
Tom Sepezfe7dcb02021-04-22 16:53:45 +0000145 float GetWordWidth(const CPVT_WordInfo& WordInfo) const;
Dan Sinclair3862d242017-10-05 12:02:15 -0400146 float GetWordWidth(int32_t nFontIndex,
147 uint16_t Word,
148 uint16_t SubWord,
Dan Sinclair3862d242017-10-05 12:02:15 -0400149 float fFontSize,
Tom Sepezfe7dcb02021-04-22 16:53:45 +0000150 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 Sinclairb01848a2017-10-05 14:27:34 -0400155 float GetLineAscent();
156 float GetLineDescent();
157 float GetLineIndent();
dsinclairc7a73492016-04-05 12:01:42 -0700158
Dan Sinclair3862d242017-10-05 12:02:15 -0400159 private:
Tom Sepezfe7dcb02021-04-22 16:53:45 +0000160 int GetCharWidth(int32_t nFontIndex, uint16_t Word, uint16_t SubWord) const;
Tom Sepezd6839862021-07-26 22:49:03 +0000161 int32_t GetWordFontIndex(uint16_t word,
162 FX_Charset charset,
163 int32_t nFontIndex);
dsinclairc7a73492016-04-05 12:01:42 -0700164
Dan Sinclaird103b962017-10-05 15:48:39 -0400165 CPVT_WordPlace AddSection(const CPVT_WordPlace& place);
dsinclairc7a73492016-04-05 12:01:42 -0700166 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 Sepezfe7dcb02021-04-22 16:53:45 +0000170 float GetWordFontSize() const;
dsinclairc7a73492016-04-05 12:01:42 -0700171
172 void ClearSectionRightWords(const CPVT_WordPlace& place);
173
tsepez12f3e4a2016-11-02 15:17:29 -0700174 bool ClearEmptySection(const CPVT_WordPlace& place);
dsinclairc7a73492016-04-05 12:01:42 -0700175 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 Zhang38737c62021-04-07 02:05:59 +0000181 void Rearrange(const CPVT_WordRange& PlaceRange);
Dan Sinclair05df0752017-03-14 14:43:42 -0400182 float GetAutoFontSize();
183 bool IsBigger(float fFontSize) const;
dsinclairc7a73492016-04-05 12:01:42 -0700184 CPVT_FloatRect RearrangeSections(const CPVT_WordRange& PlaceRange);
185
Tom Sepezae386362018-08-21 23:10:51 +0000186 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 Sepezae386362018-08-21 23:10:51 +0000194 float m_fLineLeading = 0.0f;
Tom Sepezae386362018-08-21 23:10:51 +0000195 float m_fFontSize = 0.0f;
Lei Zhangf3a1c432021-04-06 20:57:48 +0000196 std::vector<std::unique_ptr<CPVT_Section>> m_SectionArray;
Tom Sepezfe7dcb02021-04-22 16:53:45 +0000197 UnownedPtr<Provider> m_pVTProvider;
198 std::unique_ptr<Iterator> m_pVTIterator;
dsinclair448c4332016-08-02 12:07:35 -0700199 CFX_FloatRect m_rcPlate;
200 CPVT_FloatRect m_rcContent;
dsinclairc7a73492016-04-05 12:01:42 -0700201};
202
Tom Sepez012b8e32021-04-20 19:29:41 +0000203#endif // CORE_FPDFDOC_CPVT_VARIABLETEXT_H_