1 // Copyright 2014 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 FPDFSDK_INCLUDE_FXEDIT_FXET_EDIT_H_
8 #define FPDFSDK_INCLUDE_FXEDIT_FXET_EDIT_H_
9 
10 #include "core/include/fpdfdoc/fpdf_vt.h"
11 #include "fpdfsdk/include/fxedit/fx_edit.h"
12 
13 class CFX_Edit;
14 class CFX_Edit_Iterator;
15 class CFX_Edit_Provider;
16 
17 #define FX_EDIT_IsFloatZero(f) (f < 0.0001 && f > -0.0001)
18 #define FX_EDIT_IsFloatEqual(fa, fb) FX_EDIT_IsFloatZero(fa - fb)
19 #define FX_EDIT_IsFloatBigger(fa, fb) (fa > fb && !FX_EDIT_IsFloatEqual(fa, fb))
20 #define FX_EDIT_IsFloatSmaller(fa, fb) \
21   (fa < fb && !FX_EDIT_IsFloatEqual(fa, fb))
22 
23 /* ------------------------- CFX_Edit_Refresh ---------------------------- */
24 
25 enum REFRESH_PLAN_E { RP_ANALYSE, RP_NOANALYSE, RP_OPTIONAL };
26 
27 enum EDIT_PROPS_E {
28   EP_LINELEADING,
29   EP_LINEINDENT,
30   EP_ALIGNMENT,
31   EP_FONTINDEX,
32   EP_FONTSIZE,
33   EP_WORDCOLOR,
34   EP_SCRIPTTYPE,
35   EP_UNDERLINE,
36   EP_CROSSOUT,
37   EP_CHARSPACE,
38   EP_HORZSCALE,
39   EP_BOLD,
40   EP_ITALIC
41 };
42 
43 struct CFX_Edit_LineRect {
CFX_Edit_LineRectCFX_Edit_LineRect44   CFX_Edit_LineRect(const CPVT_WordRange& wrLine, const CPDF_Rect& rcLine)
45       : m_wrLine(wrLine), m_rcLine(rcLine) {}
46 
47   FX_BOOL operator!=(const CFX_Edit_LineRect& linerect) const {
48     return FXSYS_memcmp(this, &linerect, sizeof(CFX_Edit_LineRect)) != 0;
49   }
50 
IsSameHeightCFX_Edit_LineRect51   FX_BOOL IsSameHeight(const CFX_Edit_LineRect& linerect) const {
52     return FX_EDIT_IsFloatZero(
53         (m_rcLine.top - m_rcLine.bottom) -
54         (linerect.m_rcLine.top - linerect.m_rcLine.bottom));
55   }
56 
IsSameTopCFX_Edit_LineRect57   FX_BOOL IsSameTop(const CFX_Edit_LineRect& linerect) const {
58     return FX_EDIT_IsFloatZero(m_rcLine.top - linerect.m_rcLine.top);
59   }
60 
IsSameLeftCFX_Edit_LineRect61   FX_BOOL IsSameLeft(const CFX_Edit_LineRect& linerect) const {
62     return FX_EDIT_IsFloatZero(m_rcLine.left - linerect.m_rcLine.left);
63   }
64 
IsSameRightCFX_Edit_LineRect65   FX_BOOL IsSameRight(const CFX_Edit_LineRect& linerect) const {
66     return FX_EDIT_IsFloatZero(m_rcLine.right - linerect.m_rcLine.right);
67   }
68 
69   CPVT_WordRange m_wrLine;
70   CPDF_Rect m_rcLine;
71 };
72 
73 class CFX_Edit_LineRectArray {
74  public:
CFX_Edit_LineRectArray()75   CFX_Edit_LineRectArray() {}
76 
~CFX_Edit_LineRectArray()77   virtual ~CFX_Edit_LineRectArray() { Empty(); }
78 
Empty()79   void Empty() {
80     for (int32_t i = 0, sz = m_LineRects.GetSize(); i < sz; i++)
81       delete m_LineRects.GetAt(i);
82 
83     m_LineRects.RemoveAll();
84   }
85 
RemoveAll()86   void RemoveAll() { m_LineRects.RemoveAll(); }
87 
88   void operator=(CFX_Edit_LineRectArray& rects) {
89     Empty();
90     for (int32_t i = 0, sz = rects.GetSize(); i < sz; i++)
91       m_LineRects.Add(rects.GetAt(i));
92 
93     rects.RemoveAll();
94   }
95 
Add(const CPVT_WordRange & wrLine,const CPDF_Rect & rcLine)96   void Add(const CPVT_WordRange& wrLine, const CPDF_Rect& rcLine) {
97     m_LineRects.Add(new CFX_Edit_LineRect(wrLine, rcLine));
98   }
99 
GetSize()100   int32_t GetSize() const { return m_LineRects.GetSize(); }
101 
GetAt(int32_t nIndex)102   CFX_Edit_LineRect* GetAt(int32_t nIndex) const {
103     if (nIndex < 0 || nIndex >= m_LineRects.GetSize())
104       return NULL;
105 
106     return m_LineRects.GetAt(nIndex);
107   }
108 
109   CFX_ArrayTemplate<CFX_Edit_LineRect*> m_LineRects;
110 };
111 
112 class CFX_Edit_RectArray {
113  public:
CFX_Edit_RectArray()114   CFX_Edit_RectArray() {}
115 
~CFX_Edit_RectArray()116   virtual ~CFX_Edit_RectArray() { Empty(); }
117 
Empty()118   void Empty() {
119     for (int32_t i = 0, sz = m_Rects.GetSize(); i < sz; i++)
120       delete m_Rects.GetAt(i);
121 
122     m_Rects.RemoveAll();
123   }
124 
Add(const CPDF_Rect & rect)125   void Add(const CPDF_Rect& rect) {
126     // check for overlapped area
127     for (int32_t i = 0, sz = m_Rects.GetSize(); i < sz; i++) {
128       CPDF_Rect* pRect = m_Rects.GetAt(i);
129       if (pRect && pRect->Contains(rect))
130         return;
131     }
132 
133     m_Rects.Add(new CPDF_Rect(rect));
134   }
135 
GetSize()136   int32_t GetSize() const { return m_Rects.GetSize(); }
137 
GetAt(int32_t nIndex)138   CPDF_Rect* GetAt(int32_t nIndex) const {
139     if (nIndex < 0 || nIndex >= m_Rects.GetSize())
140       return NULL;
141 
142     return m_Rects.GetAt(nIndex);
143   }
144 
145   CFX_ArrayTemplate<CPDF_Rect*> m_Rects;
146 };
147 
148 class CFX_Edit_Refresh {
149  public:
150   CFX_Edit_Refresh();
151   virtual ~CFX_Edit_Refresh();
152 
153   void BeginRefresh();
154   void Push(const CPVT_WordRange& linerange, const CPDF_Rect& rect);
155   void NoAnalyse();
156   void Analyse(int32_t nAlignment);
157   void AddRefresh(const CPDF_Rect& rect);
158   const CFX_Edit_RectArray* GetRefreshRects() const;
159   void EndRefresh();
160 
161  private:
162   CFX_Edit_LineRectArray m_NewLineRects;
163   CFX_Edit_LineRectArray m_OldLineRects;
164   CFX_Edit_RectArray m_RefreshRects;
165 };
166 
167 /* ------------------------- CFX_Edit_Select ---------------------------- */
168 
169 class CFX_Edit_Select {
170  public:
CFX_Edit_Select()171   CFX_Edit_Select() {}
172 
CFX_Edit_Select(const CPVT_WordPlace & begin,const CPVT_WordPlace & end)173   CFX_Edit_Select(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) {
174     Set(begin, end);
175   }
176 
CFX_Edit_Select(const CPVT_WordRange & range)177   explicit CFX_Edit_Select(const CPVT_WordRange& range) {
178     Set(range.BeginPos, range.EndPos);
179   }
180 
ConvertToWordRange()181   CPVT_WordRange ConvertToWordRange() const {
182     return CPVT_WordRange(BeginPos, EndPos);
183   }
184 
Default()185   void Default() {
186     BeginPos.Default();
187     EndPos.Default();
188   }
189 
Set(const CPVT_WordPlace & begin,const CPVT_WordPlace & end)190   void Set(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) {
191     BeginPos = begin;
192     EndPos = end;
193   }
194 
SetBeginPos(const CPVT_WordPlace & begin)195   void SetBeginPos(const CPVT_WordPlace& begin) { BeginPos = begin; }
196 
SetEndPos(const CPVT_WordPlace & end)197   void SetEndPos(const CPVT_WordPlace& end) { EndPos = end; }
198 
IsExist()199   FX_BOOL IsExist() const { return BeginPos != EndPos; }
200 
201   FX_BOOL operator!=(const CPVT_WordRange& wr) const {
202     return wr.BeginPos != BeginPos || wr.EndPos != EndPos;
203   }
204 
205   CPVT_WordPlace BeginPos, EndPos;
206 };
207 
208 /* ------------------------- CFX_Edit_Undo ---------------------------- */
209 
210 class CFX_Edit_Undo {
211  public:
212   explicit CFX_Edit_Undo(int32_t nBufsize);
213   virtual ~CFX_Edit_Undo();
214 
215   void Undo();
216   void Redo();
217 
218   void AddItem(IFX_Edit_UndoItem* pItem);
219 
220   FX_BOOL CanUndo() const;
221   FX_BOOL CanRedo() const;
222   FX_BOOL IsModified() const;
223   FX_BOOL IsWorking() const;
224 
225   void Reset();
226 
227   IFX_Edit_UndoItem* GetItem(int32_t nIndex);
GetItemCount()228   int32_t GetItemCount() { return m_UndoItemStack.GetSize(); }
GetCurUndoPos()229   int32_t GetCurUndoPos() { return m_nCurUndoPos; }
230 
231  private:
SetBufSize(int32_t nSize)232   void SetBufSize(int32_t nSize) { m_nBufSize = nSize; }
GetBufSize()233   int32_t GetBufSize() { return m_nBufSize; }
234 
235   void RemoveHeads();
236   void RemoveTails();
237 
238  private:
239   CFX_ArrayTemplate<IFX_Edit_UndoItem*> m_UndoItemStack;
240 
241   int32_t m_nCurUndoPos;
242   int32_t m_nBufSize;
243   FX_BOOL m_bModified;
244   FX_BOOL m_bVirgin;
245   FX_BOOL m_bWorking;
246 };
247 
248 class CFX_Edit_UndoItem : public IFX_Edit_UndoItem {
249  public:
CFX_Edit_UndoItem()250   CFX_Edit_UndoItem() : m_bFirst(TRUE), m_bLast(TRUE) {}
~CFX_Edit_UndoItem()251   ~CFX_Edit_UndoItem() override {}
252 
GetUndoTitle()253   CFX_WideString GetUndoTitle() override { return L""; }
254 
SetFirst(FX_BOOL bFirst)255   void SetFirst(FX_BOOL bFirst) { m_bFirst = bFirst; }
IsFirst()256   FX_BOOL IsFirst() { return m_bFirst; }
SetLast(FX_BOOL bLast)257   void SetLast(FX_BOOL bLast) { m_bLast = bLast; }
IsLast()258   FX_BOOL IsLast() { return m_bLast; }
259 
260  private:
261   FX_BOOL m_bFirst;
262   FX_BOOL m_bLast;
263 };
264 
265 class CFX_Edit_GroupUndoItem : public IFX_Edit_UndoItem {
266  public:
267   explicit CFX_Edit_GroupUndoItem(const CFX_WideString& sTitle);
268   ~CFX_Edit_GroupUndoItem() override;
269 
270   void Undo() override;
271   void Redo() override;
272   CFX_WideString GetUndoTitle() override;
273 
274   void AddUndoItem(CFX_Edit_UndoItem* pUndoItem);
275   void UpdateItems();
276 
277  private:
278   CFX_WideString m_sTitle;
279   CFX_ArrayTemplate<CFX_Edit_UndoItem*> m_Items;
280 };
281 
282 /* ------------------------- CFX_Edit_UndoItem derived classes
283  * ---------------------------- */
284 
285 class CFXEU_InsertWord : public CFX_Edit_UndoItem {
286  public:
287   CFXEU_InsertWord(CFX_Edit* pEdit,
288                    const CPVT_WordPlace& wpOldPlace,
289                    const CPVT_WordPlace& wpNewPlace,
290                    FX_WORD word,
291                    int32_t charset,
292                    const CPVT_WordProps* pWordProps);
293   ~CFXEU_InsertWord() override;
294 
295   // CFX_Edit_UndoItem
296   void Redo() override;
297   void Undo() override;
298 
299  private:
300   CFX_Edit* m_pEdit;
301 
302   CPVT_WordPlace m_wpOld;
303   CPVT_WordPlace m_wpNew;
304   FX_WORD m_Word;
305   int32_t m_nCharset;
306   CPVT_WordProps m_WordProps;
307 };
308 
309 class CFXEU_InsertReturn : public CFX_Edit_UndoItem {
310  public:
311   CFXEU_InsertReturn(CFX_Edit* pEdit,
312                      const CPVT_WordPlace& wpOldPlace,
313                      const CPVT_WordPlace& wpNewPlace,
314                      const CPVT_SecProps* pSecProps,
315                      const CPVT_WordProps* pWordProps);
316   ~CFXEU_InsertReturn() override;
317 
318   // CFX_Edit_UndoItem
319   void Redo() override;
320   void Undo() override;
321 
322  private:
323   CFX_Edit* m_pEdit;
324 
325   CPVT_WordPlace m_wpOld;
326   CPVT_WordPlace m_wpNew;
327   CPVT_SecProps m_SecProps;
328   CPVT_WordProps m_WordProps;
329 };
330 
331 class CFXEU_Backspace : public CFX_Edit_UndoItem {
332  public:
333   CFXEU_Backspace(CFX_Edit* pEdit,
334                   const CPVT_WordPlace& wpOldPlace,
335                   const CPVT_WordPlace& wpNewPlace,
336                   FX_WORD word,
337                   int32_t charset,
338                   const CPVT_SecProps& SecProps,
339                   const CPVT_WordProps& WordProps);
340   ~CFXEU_Backspace() override;
341 
342   // CFX_Edit_UndoItem
343   void Redo() override;
344   void Undo() override;
345 
346  private:
347   CFX_Edit* m_pEdit;
348 
349   CPVT_WordPlace m_wpOld;
350   CPVT_WordPlace m_wpNew;
351   FX_WORD m_Word;
352   int32_t m_nCharset;
353   CPVT_SecProps m_SecProps;
354   CPVT_WordProps m_WordProps;
355 };
356 
357 class CFXEU_Delete : public CFX_Edit_UndoItem {
358  public:
359   CFXEU_Delete(CFX_Edit* pEdit,
360                const CPVT_WordPlace& wpOldPlace,
361                const CPVT_WordPlace& wpNewPlace,
362                FX_WORD word,
363                int32_t charset,
364                const CPVT_SecProps& SecProps,
365                const CPVT_WordProps& WordProps,
366                FX_BOOL bSecEnd);
367   ~CFXEU_Delete() override;
368 
369   // CFX_Edit_UndoItem
370   void Redo() override;
371   void Undo() override;
372 
373  private:
374   CFX_Edit* m_pEdit;
375 
376   CPVT_WordPlace m_wpOld;
377   CPVT_WordPlace m_wpNew;
378   FX_WORD m_Word;
379   int32_t m_nCharset;
380   CPVT_SecProps m_SecProps;
381   CPVT_WordProps m_WordProps;
382   FX_BOOL m_bSecEnd;
383 };
384 
385 class CFXEU_Clear : public CFX_Edit_UndoItem {
386  public:
387   CFXEU_Clear(CFX_Edit* pEdit,
388               const CPVT_WordRange& wrSel,
389               const CFX_WideString& swText);
390   ~CFXEU_Clear() override;
391 
392   // CFX_Edit_UndoItem
393   void Redo() override;
394   void Undo() override;
395 
396  private:
397   CFX_Edit* m_pEdit;
398 
399   CPVT_WordRange m_wrSel;
400   CFX_WideString m_swText;
401 };
402 
403 class CFXEU_ClearRich : public CFX_Edit_UndoItem {
404  public:
405   CFXEU_ClearRich(CFX_Edit* pEdit,
406                   const CPVT_WordPlace& wpOldPlace,
407                   const CPVT_WordPlace& wpNewPlace,
408                   const CPVT_WordRange& wrSel,
409                   FX_WORD word,
410                   int32_t charset,
411                   const CPVT_SecProps& SecProps,
412                   const CPVT_WordProps& WordProps);
413   ~CFXEU_ClearRich() override;
414 
415   // CFX_Edit_UndoItem
416   void Redo() override;
417   void Undo() override;
418 
419  private:
420   CFX_Edit* m_pEdit;
421 
422   CPVT_WordPlace m_wpOld;
423   CPVT_WordPlace m_wpNew;
424   CPVT_WordRange m_wrSel;
425   FX_WORD m_Word;
426   int32_t m_nCharset;
427   CPVT_SecProps m_SecProps;
428   CPVT_WordProps m_WordProps;
429 };
430 
431 class CFXEU_InsertText : public CFX_Edit_UndoItem {
432  public:
433   CFXEU_InsertText(CFX_Edit* pEdit,
434                    const CPVT_WordPlace& wpOldPlace,
435                    const CPVT_WordPlace& wpNewPlace,
436                    const CFX_WideString& swText,
437                    int32_t charset,
438                    const CPVT_SecProps* pSecProps,
439                    const CPVT_WordProps* pWordProps);
440   ~CFXEU_InsertText() override;
441 
442   // CFX_Edit_UndoItem
443   void Redo() override;
444   void Undo() override;
445 
446  private:
447   CFX_Edit* m_pEdit;
448 
449   CPVT_WordPlace m_wpOld;
450   CPVT_WordPlace m_wpNew;
451   CFX_WideString m_swText;
452   int32_t m_nCharset;
453   CPVT_SecProps m_SecProps;
454   CPVT_WordProps m_WordProps;
455 };
456 
457 class CFXEU_SetSecProps : public CFX_Edit_UndoItem {
458  public:
459   CFXEU_SetSecProps(CFX_Edit* pEdit,
460                     const CPVT_WordPlace& place,
461                     EDIT_PROPS_E ep,
462                     const CPVT_SecProps& oldsecprops,
463                     const CPVT_WordProps& oldwordprops,
464                     const CPVT_SecProps& newsecprops,
465                     const CPVT_WordProps& newwordprops,
466                     const CPVT_WordRange& range);
467   ~CFXEU_SetSecProps() override;
468 
469   // CFX_Edit_UndoItem
470   void Redo() override;
471   void Undo() override;
472 
473  private:
474   CFX_Edit* m_pEdit;
475   CPVT_WordPlace m_wpPlace;
476   CPVT_WordRange m_wrPlace;
477   EDIT_PROPS_E m_eProps;
478 
479   CPVT_SecProps m_OldSecProps;
480   CPVT_SecProps m_NewSecProps;
481   CPVT_WordProps m_OldWordProps;
482   CPVT_WordProps m_NewWordProps;
483 };
484 
485 class CFXEU_SetWordProps : public CFX_Edit_UndoItem {
486  public:
487   CFXEU_SetWordProps(CFX_Edit* pEdit,
488                      const CPVT_WordPlace& place,
489                      EDIT_PROPS_E ep,
490                      const CPVT_WordProps& oldprops,
491                      const CPVT_WordProps& newprops,
492                      const CPVT_WordRange& range);
493   ~CFXEU_SetWordProps() override;
494 
495   // CFX_Edit_UndoItem
496   void Redo() override;
497   void Undo() override;
498 
499  private:
500   CFX_Edit* m_pEdit;
501   CPVT_WordPlace m_wpPlace;
502   CPVT_WordRange m_wrPlace;
503   EDIT_PROPS_E m_eProps;
504 
505   CPVT_WordProps m_OldWordProps;
506   CPVT_WordProps m_NewWordProps;
507 };
508 
509 /* ------------------------- CFX_Edit ---------------------------- */
510 
511 class CFX_Edit : public IFX_Edit {
512   friend class CFX_Edit_Iterator;
513   friend class CFXEU_InsertWord;
514   friend class CFXEU_InsertReturn;
515   friend class CFXEU_Backspace;
516   friend class CFXEU_Delete;
517   friend class CFXEU_Clear;
518   friend class CFXEU_ClearRich;
519   friend class CFXEU_SetSecProps;
520   friend class CFXEU_SetWordProps;
521   friend class CFXEU_InsertText;
522 
523  public:
524   explicit CFX_Edit(IPDF_VariableText* pVT);
525   ~CFX_Edit() override;
526 
527   // IFX_Edit
528   void SetFontMap(IFX_Edit_FontMap* pFontMap) override;
529   void SetVTProvider(IPDF_VariableText_Provider* pProvider) override;
530   void SetNotify(IFX_Edit_Notify* pNotify) override;
531   void SetOprNotify(IFX_Edit_OprNotify* pOprNotify) override;
532   IFX_Edit_Iterator* GetIterator() override;
533   IPDF_VariableText* GetVariableText() override;
534   IFX_Edit_FontMap* GetFontMap() override;
535   void Initialize() override;
536   void SetPlateRect(const CPDF_Rect& rect, FX_BOOL bPaint = TRUE) override;
537   void SetScrollPos(const CPDF_Point& point) override;
538   void SetAlignmentH(int32_t nFormat = 0, FX_BOOL bPaint = TRUE) override;
539   void SetAlignmentV(int32_t nFormat = 0, FX_BOOL bPaint = TRUE) override;
540   void SetPasswordChar(FX_WORD wSubWord = '*', FX_BOOL bPaint = TRUE) override;
541   void SetLimitChar(int32_t nLimitChar = 0, FX_BOOL bPaint = TRUE) override;
542   void SetCharArray(int32_t nCharArray = 0, FX_BOOL bPaint = TRUE) override;
543   void SetCharSpace(FX_FLOAT fCharSpace = 0.0f, FX_BOOL bPaint = TRUE) override;
544   void SetHorzScale(int32_t nHorzScale = 100, FX_BOOL bPaint = TRUE) override;
545   void SetLineLeading(FX_FLOAT fLineLeading, FX_BOOL bPaint = TRUE) override;
546   void SetMultiLine(FX_BOOL bMultiLine = TRUE, FX_BOOL bPaint = TRUE) override;
547   void SetAutoReturn(FX_BOOL bAuto = TRUE, FX_BOOL bPaint = TRUE) override;
548   void SetAutoFontSize(FX_BOOL bAuto = TRUE, FX_BOOL bPaint = TRUE) override;
549   void SetAutoScroll(FX_BOOL bAuto = TRUE, FX_BOOL bPaint = TRUE) override;
550   void SetFontSize(FX_FLOAT fFontSize, FX_BOOL bPaint = TRUE) override;
551   void SetTextOverflow(FX_BOOL bAllowed = FALSE,
552                        FX_BOOL bPaint = TRUE) override;
553   FX_BOOL IsRichText() const override;
554   void SetRichText(FX_BOOL bRichText = TRUE, FX_BOOL bPaint = TRUE) override;
555   FX_BOOL SetRichFontSize(FX_FLOAT fFontSize) override;
556   FX_BOOL SetRichFontIndex(int32_t nFontIndex) override;
557   FX_BOOL SetRichTextColor(FX_COLORREF dwColor) override;
558   FX_BOOL SetRichTextScript(int32_t nScriptType) override;
559   FX_BOOL SetRichTextBold(FX_BOOL bBold = TRUE) override;
560   FX_BOOL SetRichTextItalic(FX_BOOL bItalic = TRUE) override;
561   FX_BOOL SetRichTextUnderline(FX_BOOL bUnderline = TRUE) override;
562   FX_BOOL SetRichTextCrossout(FX_BOOL bCrossout = TRUE) override;
563   FX_BOOL SetRichTextCharSpace(FX_FLOAT fCharSpace) override;
564   FX_BOOL SetRichTextHorzScale(int32_t nHorzScale = 100) override;
565   FX_BOOL SetRichTextLineLeading(FX_FLOAT fLineLeading) override;
566   FX_BOOL SetRichTextLineIndent(FX_FLOAT fLineIndent) override;
567   FX_BOOL SetRichTextAlignment(int32_t nAlignment) override;
568   void OnMouseDown(const CPDF_Point& point,
569                    FX_BOOL bShift,
570                    FX_BOOL bCtrl) override;
571   void OnMouseMove(const CPDF_Point& point,
572                    FX_BOOL bShift,
573                    FX_BOOL bCtrl) override;
574   void OnVK_UP(FX_BOOL bShift, FX_BOOL bCtrl) override;
575   void OnVK_DOWN(FX_BOOL bShift, FX_BOOL bCtrl) override;
576   void OnVK_LEFT(FX_BOOL bShift, FX_BOOL bCtrl) override;
577   void OnVK_RIGHT(FX_BOOL bShift, FX_BOOL bCtrl) override;
578   void OnVK_HOME(FX_BOOL bShift, FX_BOOL bCtrl) override;
579   void OnVK_END(FX_BOOL bShift, FX_BOOL bCtrl) override;
580   void SetText(const FX_WCHAR* text,
581                int32_t charset = DEFAULT_CHARSET,
582                const CPVT_SecProps* pSecProps = NULL,
583                const CPVT_WordProps* pWordProps = NULL) override;
584   FX_BOOL InsertWord(FX_WORD word,
585                      int32_t charset = DEFAULT_CHARSET,
586                      const CPVT_WordProps* pWordProps = NULL) override;
587   FX_BOOL InsertReturn(const CPVT_SecProps* pSecProps = NULL,
588                        const CPVT_WordProps* pWordProps = NULL) override;
589   FX_BOOL Backspace() override;
590   FX_BOOL Delete() override;
591   FX_BOOL Clear() override;
592   FX_BOOL InsertText(const FX_WCHAR* text,
593                      int32_t charset = DEFAULT_CHARSET,
594                      const CPVT_SecProps* pSecProps = NULL,
595                      const CPVT_WordProps* pWordProps = NULL) override;
596   FX_BOOL Redo() override;
597   FX_BOOL Undo() override;
598   int32_t WordPlaceToWordIndex(const CPVT_WordPlace& place) const override;
599   CPVT_WordPlace WordIndexToWordPlace(int32_t index) const override;
600   CPVT_WordPlace GetLineBeginPlace(const CPVT_WordPlace& place) const override;
601   CPVT_WordPlace GetLineEndPlace(const CPVT_WordPlace& place) const override;
602   CPVT_WordPlace GetSectionBeginPlace(
603       const CPVT_WordPlace& place) const override;
604   CPVT_WordPlace GetSectionEndPlace(const CPVT_WordPlace& place) const override;
605   CPVT_WordPlace SearchWordPlace(const CPDF_Point& point) const override;
606   int32_t GetCaret() const override;
607   CPVT_WordPlace GetCaretWordPlace() const override;
608   CFX_WideString GetSelText() const override;
609   CFX_WideString GetText() const override;
610   FX_FLOAT GetFontSize() const override;
611   FX_WORD GetPasswordChar() const override;
612   CPDF_Point GetScrollPos() const override;
613   int32_t GetCharArray() const override;
614   CPDF_Rect GetPlateRect() const override;
615   CPDF_Rect GetContentRect() const override;
616   CFX_WideString GetRangeText(const CPVT_WordRange& range) const override;
617   int32_t GetHorzScale() const override;
618   FX_FLOAT GetCharSpace() const override;
619   int32_t GetTotalWords() const override;
620   void SetSel(int32_t nStartChar, int32_t nEndChar) override;
621   void GetSel(int32_t& nStartChar, int32_t& nEndChar) const override;
622   void SelectAll() override;
623   void SelectNone() override;
624   FX_BOOL IsSelected() const override;
625   void Paint() override;
626   void EnableNotify(FX_BOOL bNotify) override;
627   void EnableRefresh(FX_BOOL bRefresh) override;
628   void RefreshWordRange(const CPVT_WordRange& wr) override;
629   void SetCaret(int32_t nPos) override;
630   CPVT_WordRange GetWholeWordRange() const override;
631   CPVT_WordRange GetSelectWordRange() const override;
632   void EnableUndo(FX_BOOL bUndo) override;
633   void EnableOprNotify(FX_BOOL bNotify) override;
634   FX_BOOL IsTextFull() const override;
635   FX_BOOL IsTextOverflow() const;
636   FX_BOOL CanUndo() const override;
637   FX_BOOL CanRedo() const override;
638   FX_BOOL IsModified() const override;
639   CPVT_WordRange GetVisibleWordRange() const override;
640   void AddUndoItem(IFX_Edit_UndoItem* pUndoItem) override;
641 
642   FX_BOOL Empty();
643 
644   CPVT_WordPlace DoInsertText(const CPVT_WordPlace& place,
645                               const FX_WCHAR* text,
646                               int32_t charset,
647                               const CPVT_SecProps* pSecProps,
648                               const CPVT_WordProps* pWordProps);
649   int32_t GetCharSetFromUnicode(FX_WORD word, int32_t nOldCharset);
650 
651   int32_t GetTotalLines() const;
652 
653  private:
654   void SetSel(const CPVT_WordPlace& begin, const CPVT_WordPlace& end);
655 
656   void RearrangeAll();
657   void RearrangePart(const CPVT_WordRange& range);
658   void ScrollToCaret();
659   void SetScrollInfo();
660   void SetScrollPosX(FX_FLOAT fx);
661   void SetScrollPosY(FX_FLOAT fy);
662   void SetScrollLimit();
663   void SetContentChanged();
664 
665   void SetText(const FX_WCHAR* text,
666                int32_t charset,
667                const CPVT_SecProps* pSecProps,
668                const CPVT_WordProps* pWordProps,
669                FX_BOOL bAddUndo,
670                FX_BOOL bPaint);
671   FX_BOOL InsertWord(FX_WORD word,
672                      int32_t charset,
673                      const CPVT_WordProps* pWordProps,
674                      FX_BOOL bAddUndo,
675                      FX_BOOL bPaint);
676   FX_BOOL InsertReturn(const CPVT_SecProps* pSecProps,
677                        const CPVT_WordProps* pWordProps,
678                        FX_BOOL bAddUndo,
679                        FX_BOOL bPaint);
680   FX_BOOL Backspace(FX_BOOL bAddUndo, FX_BOOL bPaint);
681   FX_BOOL Delete(FX_BOOL bAddUndo, FX_BOOL bPaint);
682   FX_BOOL Clear(FX_BOOL bAddUndo, FX_BOOL bPaint);
683   FX_BOOL InsertText(const FX_WCHAR* text,
684                      int32_t charset,
685                      const CPVT_SecProps* pSecProps,
686                      const CPVT_WordProps* pWordProps,
687                      FX_BOOL bAddUndo,
688                      FX_BOOL bPaint);
689   FX_BOOL SetRichTextProps(EDIT_PROPS_E eProps,
690                            const CPVT_SecProps* pSecProps,
691                            const CPVT_WordProps* pWordProps);
692   FX_BOOL SetSecProps(EDIT_PROPS_E eProps,
693                       const CPVT_WordPlace& place,
694                       const CPVT_SecProps* pSecProps,
695                       const CPVT_WordProps* pWordProps,
696                       const CPVT_WordRange& wr,
697                       FX_BOOL bAddUndo);
698   FX_BOOL SetWordProps(EDIT_PROPS_E eProps,
699                        const CPVT_WordPlace& place,
700                        const CPVT_WordProps* pWordProps,
701                        const CPVT_WordRange& wr,
702                        FX_BOOL bAddUndo);
703   void PaintSetProps(EDIT_PROPS_E eProps, const CPVT_WordRange& wr);
704   void PaintInsertText(const CPVT_WordPlace& wpOld,
705                        const CPVT_WordPlace& wpNew);
706 
707   inline CPDF_Point VTToEdit(const CPDF_Point& point) const;
708   inline CPDF_Point EditToVT(const CPDF_Point& point) const;
709   inline CPDF_Rect VTToEdit(const CPDF_Rect& rect) const;
710   inline CPDF_Rect EditToVT(const CPDF_Rect& rect) const;
711 
712   void Refresh(REFRESH_PLAN_E ePlan,
713                const CPVT_WordRange* pRange1 = NULL,
714                const CPVT_WordRange* pRange2 = NULL);
715   void RefreshPushLineRects(const CPVT_WordRange& wr);
716   void RefreshPushRandomRects(const CPVT_WordRange& wr);
717 
718   void SetCaret(const CPVT_WordPlace& place);
719   void SetCaretInfo();
720   void SetCaretOrigin();
721   void SetCaretChange();
722 
723   CPVT_WordRange GetLatinWordsRange(const CPVT_WordPlace& place) const;
724   CPVT_WordRange CombineWordRange(const CPVT_WordRange& wr1,
725                                   const CPVT_WordRange& wr2);
726 
727 
728   void BeginGroupUndo(const CFX_WideString& sTitle);
729   void EndGroupUndo();
730   void AddEditUndoItem(CFX_Edit_UndoItem* pEditUndoItem);
731 
732   void SetPageInfo(const CPVT_WordPlace& place);
733   CPVT_WordPlace SearchPageEndPlace(const CPVT_WordPlace& wpPageBegin,
734                                     const CPDF_Point& point) const;
735   FX_FLOAT GetLineTop(const CPVT_WordPlace& place) const;
736   FX_FLOAT GetLineBottom(const CPVT_WordPlace& place) const;
737 
738  private:
739   IPDF_VariableText* m_pVT;
740   IFX_Edit_Notify* m_pNotify;
741   IFX_Edit_OprNotify* m_pOprNotify;
742   CFX_Edit_Provider* m_pVTProvide;
743 
744   CPVT_WordPlace m_wpCaret;
745   CPVT_WordPlace m_wpOldCaret;
746   CFX_Edit_Select m_SelState;
747 
748   CPDF_Point m_ptScrollPos;
749   CPDF_Point m_ptRefreshScrollPos;
750   FX_BOOL m_bEnableScroll;
751   IFX_Edit_Iterator* m_pIterator;
752   CFX_Edit_Refresh m_Refresh;
753   CPDF_Point m_ptCaret;
754   CFX_Edit_Undo m_Undo;
755   int32_t m_nAlignment;
756   FX_BOOL m_bNotifyFlag;
757   FX_BOOL m_bEnableOverflow;
758   FX_BOOL m_bEnableRefresh;
759   CPDF_Rect m_rcOldContent;
760   FX_BOOL m_bEnableUndo;
761   FX_BOOL m_bNotify;
762   FX_BOOL m_bOprNotify;
763   CFX_Edit_GroupUndoItem* m_pGroupUndoItem;
764 };
765 
766 /* ------------------------- CFX_Edit_Iterator ---------------------------- */
767 
768 class CFX_Edit_Iterator : public IFX_Edit_Iterator {
769  public:
770   CFX_Edit_Iterator(CFX_Edit* pEdit, IPDF_VariableText_Iterator* pVTIterator);
771   ~CFX_Edit_Iterator() override;
772 
773   // IFX_Edit_Iterator
774   FX_BOOL NextWord() override;
775   FX_BOOL NextLine() override;
776   FX_BOOL NextSection() override;
777   FX_BOOL PrevWord() override;
778   FX_BOOL PrevLine() override;
779   FX_BOOL PrevSection() override;
780   FX_BOOL GetWord(CPVT_Word& word) const override;
781   FX_BOOL GetLine(CPVT_Line& line) const override;
782   FX_BOOL GetSection(CPVT_Section& section) const override;
783   void SetAt(int32_t nWordIndex) override;
784   void SetAt(const CPVT_WordPlace& place) override;
785   const CPVT_WordPlace& GetAt() const override;
786   IFX_Edit* GetEdit() const override;
787 
788  private:
789   CFX_Edit* m_pEdit;
790   IPDF_VariableText_Iterator* m_pVTIterator;
791 };
792 
793 class CFX_Edit_Provider : public IPDF_VariableText_Provider {
794  public:
795   explicit CFX_Edit_Provider(IFX_Edit_FontMap* pFontMap);
796   ~CFX_Edit_Provider() override;
797 
798   IFX_Edit_FontMap* GetFontMap();
799 
800   // IPDF_VariableText_Provider:
801   int32_t GetCharWidth(int32_t nFontIndex,
802                        FX_WORD word,
803                        int32_t nWordStyle) override;
804   int32_t GetTypeAscent(int32_t nFontIndex) override;
805   int32_t GetTypeDescent(int32_t nFontIndex) override;
806   int32_t GetWordFontIndex(FX_WORD word,
807                            int32_t charset,
808                            int32_t nFontIndex) override;
809   int32_t GetDefaultFontIndex() override;
810   FX_BOOL IsLatinWord(FX_WORD word) override;
811 
812  private:
813   IFX_Edit_FontMap* m_pFontMap;
814 };
815 
816 #endif  // FPDFSDK_INCLUDE_FXEDIT_FXET_EDIT_H_
817