Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfde_texteditengine.h
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
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 XFA_FDE_CFDE_TEXTEDITENGINE_H_
8#define XFA_FDE_CFDE_TEXTEDITENGINE_H_
9
10#include <limits>
11#include <memory>
12#include <utility>
13#include <vector>
14
15#include "core/fxcrt/retain_ptr.h"
16#include "core/fxcrt/unowned_ptr.h"
17#include "core/fxcrt/widestring.h"
18#include "core/fxge/dib/fx_dib.h"
19#include "xfa/fgas/layout/cfgas_txtbreak.h"
20
21class CFGAS_GEFont;
22class TextCharPos;
23
35
36inline FDE_TEXTEDITPIECE::FDE_TEXTEDITPIECE() = default;
38 default;
39inline FDE_TEXTEDITPIECE::~FDE_TEXTEDITPIECE() = default;
40
41class CFDE_TextEditEngine final : public CFGAS_TxtBreak::Engine {
42 public:
43 class Iterator {
44 public:
45 explicit Iterator(const CFDE_TextEditEngine* engine);
47
48 void Next(bool bPrev);
49 wchar_t GetChar() const;
50 void SetAt(size_t nIndex);
51 size_t FindNextBreakPos(bool bPrev);
52 bool IsEOF(bool bPrev) const;
53
54 private:
55 UnownedPtr<const CFDE_TextEditEngine> const engine_;
56 int32_t current_position_ = -1;
57 };
58
59 class Operation {
60 public:
61 virtual ~Operation() = default;
62 virtual void Redo() const = 0;
63 virtual void Undo() const = 0;
64 };
65
73
74 class Delegate {
75 public:
76 virtual ~Delegate() = default;
77 virtual void NotifyTextFull() = 0;
78 virtual void OnCaretChanged() = 0;
79 virtual void OnTextWillChange(TextChange* change) = 0;
80 virtual void OnTextChanged() = 0;
81 virtual void OnSelChanged() = 0;
82 virtual bool OnValidate(const WideString& wsText) = 0;
83 virtual void SetScrollOffset(float fScrollOffset) = 0;
84 };
85
87
90
91 // CFGAS_TxtBreak::Engine:
92 wchar_t GetChar(size_t idx) const override;
93 int32_t GetWidthOfChar(size_t idx) override;
94
95 void SetDelegate(Delegate* delegate) { delegate_ = delegate; }
96 void Clear();
97
98 void Insert(size_t idx,
99 const WideString& text,
101 WideString Delete(
102 size_t start_idx,
103 size_t length,
105 WideString GetText() const;
106 size_t GetLength() const;
107
108 // Non-const so we can force a layout.
110 void SetAvailableWidth(size_t width);
111
112 void SetFont(RetainPtr<CFGAS_GEFont> font);
113 RetainPtr<CFGAS_GEFont> GetFont() const;
114 void SetFontSize(float size);
115 float GetFontSize() const { return font_size_; }
116 void SetFontColor(FX_ARGB color) { font_color_ = color; }
117 FX_ARGB GetFontColor() const { return font_color_; }
118
119 void SetAlignment(uint32_t alignment);
120 float GetLineSpace() const { return line_spacing_; }
121 void SetLineSpace(float space) { line_spacing_ = space; }
122 void SetAliasChar(wchar_t alias) { password_alias_ = alias; }
123 void SetHasCharacterLimit(bool limit);
124 void SetCharacterLimit(size_t limit);
125 void SetCombText(bool enable);
126 void SetTabWidth(float width);
127 void SetVisibleLineCount(size_t lines);
128
129 void EnableValidation(bool val) { validation_enabled_ = val; }
130 void EnablePasswordMode(bool val) { password_mode_ = val; }
131 void EnableMultiLine(bool val);
132 void EnableLineWrap(bool val);
133 void LimitHorizontalScroll(bool val);
134 void LimitVerticalScroll(bool val);
135
136 bool CanUndo() const;
137 bool CanRedo() const;
138 bool Redo();
139 bool Undo();
141
142 size_t GetIndexLeft(size_t pos) const;
143 size_t GetIndexRight(size_t pos) const;
144 size_t GetIndexUp(size_t pos) const;
145 size_t GetIndexDown(size_t pos) const;
146 size_t GetIndexAtStartOfLine(size_t pos) const;
147 size_t GetIndexAtEndOfLine(size_t pos) const;
148
149 void SelectAll();
150 void SetSelection(size_t start_idx, size_t count);
151 void ClearSelection();
152 bool HasSelection() const { return has_selection_; }
153 // Returns <start_idx, count> of the selection.
155 return {selection_.start_idx, selection_.count};
156 }
157 WideString GetSelectedText() const;
158 WideString DeleteSelectedText(
160 void ReplaceSelectedText(const WideString& str);
161
162 void Layout();
163
164 // Non-const so we can force a Layout() if needed.
165 size_t GetIndexForPoint(const CFX_PointF& point);
166 // <start_idx, count>
167 std::pair<size_t, size_t> BoundsForWordAt(size_t idx) const;
168
169 // Note that if CanGenerateCharacterInfo() returns false, then
170 // GetCharacterInfo() cannot be called.
171 bool CanGenerateCharacterInfo() const { return text_length_ > 0 && font_; }
172
173 // Returns <bidi level, character rect>
174 std::pair<int32_t, CFX_RectF> GetCharacterInfo(int32_t start_idx);
175 std::vector<CFX_RectF> GetCharacterRectsInRange(int32_t start_idx,
176 int32_t count);
177
179 // Force a layout if needed.
180 Layout();
181 return text_piece_info_;
182 }
183
185
186 void SetMaxEditOperationsForTesting(size_t max);
187
188 private:
189 struct Selection {
190 size_t start_idx;
191 size_t count;
192 };
193
194 static constexpr size_t kGapSize = 128;
195 static constexpr size_t kMaxEditOperations = 128;
196 static constexpr size_t kPageWidthMax = 0xffff;
197
198 void SetCombTextWidth();
199 void AdjustGap(size_t idx, size_t length);
200 void RebuildPieces();
201 size_t CountCharsExceedingSize(const WideString& str, size_t num_to_check);
202 void AddOperationRecord(std::unique_ptr<Operation> op);
203
204 bool IsAlignedRight() const {
205 return !!(character_alignment_ & CFX_TxtLineAlignment_Right);
206 }
207
208 bool IsAlignedCenter() const {
209 return !!(character_alignment_ & CFX_TxtLineAlignment_Center);
210 }
211 std::vector<CFX_RectF> GetCharRects(const FDE_TEXTEDITPIECE& piece);
212
213 CFX_RectF contents_bounding_box_;
214 UnownedPtr<Delegate> delegate_;
215 std::vector<FDE_TEXTEDITPIECE> text_piece_info_;
216 std::vector<int32_t> char_widths_; // May be negative for combining chars.
217 CFGAS_TxtBreak text_break_;
218 RetainPtr<CFGAS_GEFont> font_;
219 FX_ARGB font_color_ = 0xff000000;
220 float font_size_ = 10.0f;
221 float line_spacing_ = 10.0f;
222 std::vector<WideString::CharType> content_;
223 size_t text_length_ = 0;
224
225 // See e.g. https://en.wikipedia.org/wiki/Gap_buffer
226 size_t gap_position_ = 0;
227 size_t gap_size_ = kGapSize;
228
229 size_t available_width_ = kPageWidthMax;
230 size_t character_limit_ = std::numeric_limits<size_t>::max();
231 size_t visible_line_count_ = 1;
232 // Ring buffer of edit operations
233 std::vector<std::unique_ptr<Operation>> operation_buffer_;
234 // Next edit operation to undo.
235 size_t next_operation_index_to_undo_ = kMaxEditOperations - 1;
236 // Next index to insert an edit operation into.
237 size_t next_operation_index_to_insert_ = 0;
238 size_t max_edit_operations_ = kMaxEditOperations;
239 uint32_t character_alignment_ = CFX_TxtLineAlignment_Left;
240 bool has_character_limit_ = false;
241 bool is_comb_text_ = false;
242 bool is_dirty_ = false;
243 bool validation_enabled_ = false;
244 bool is_multiline_ = false;
245 bool is_linewrap_enabled_ = false;
246 bool limit_horizontal_area_ = false;
247 bool limit_vertical_area_ = false;
248 bool password_mode_ = false;
249 wchar_t password_alias_ = L'*';
250 bool has_selection_ = false;
251 Selection selection_{0, 0};
252};
253
254#endif // XFA_FDE_CFDE_TEXTEDITENGINE_H_
WordBreakProperty FX_GetWordBreakProperty(wchar_t wcCodePoint)
bool FX_CheckStateChangeForWordBreak(WordBreakProperty from, WordBreakProperty to)
WordBreakProperty
@ CFX_TxtLineAlignment_Right
@ CFX_TxtLineAlignment_Center
@ CFX_TxtLineAlignment_Left
#define FX_TXTCHARSTYLE_OddBidiLevel
bool CFX_BreakTypeNoneOrPiece(CFGAS_Char::BreakType type)
virtual void OnCaretChanged()=0
virtual bool OnValidate(const WideString &wsText)=0
virtual ~Delegate()=default
virtual void OnSelChanged()=0
virtual void OnTextWillChange(TextChange *change)=0
virtual void SetScrollOffset(float fScrollOffset)=0
virtual void OnTextChanged()=0
virtual void NotifyTextFull()=0
Iterator(const CFDE_TextEditEngine *engine)
virtual void Undo() const =0
virtual void Redo() const =0
virtual ~Operation()=default
void SetAlignment(uint32_t alignment)
void SetCombText(bool enable)
size_t GetIndexAtStartOfLine(size_t pos) const
void Insert(size_t idx, const WideString &text, RecordOperation add_operation=RecordOperation::kInsertRecord)
std::pair< size_t, size_t > BoundsForWordAt(size_t idx) const
void SetAvailableWidth(size_t width)
void EnablePasswordMode(bool val)
WideString GetSelectedText() const
WideString GetText() const
size_t GetIndexAtEndOfLine(size_t pos) const
size_t GetIndexLeft(size_t pos) const
size_t GetIndexRight(size_t pos) const
void EnableValidation(bool val)
WideString Delete(size_t start_idx, size_t length, RecordOperation add_operation=RecordOperation::kInsertRecord)
~CFDE_TextEditEngine() override
void SetDelegate(Delegate *delegate)
std::pair< int32_t, CFX_RectF > GetCharacterInfo(int32_t start_idx)
void SetVisibleLineCount(size_t lines)
size_t GetIndexDown(size_t pos) const
void LimitHorizontalScroll(bool val)
size_t GetIndexForPoint(const CFX_PointF &point)
FX_ARGB GetFontColor() const
void SetSelection(size_t start_idx, size_t count)
void SetFont(RetainPtr< CFGAS_GEFont > font)
void LimitVerticalScroll(bool val)
void SetHasCharacterLimit(bool limit)
std::vector< CFX_RectF > GetCharacterRectsInRange(int32_t start_idx, int32_t count)
int32_t GetWidthOfChar(size_t idx) override
bool CanGenerateCharacterInfo() const
const std::vector< FDE_TEXTEDITPIECE > & GetTextPieces()
std::pair< size_t, size_t > GetSelection() const
std::vector< TextCharPos > GetDisplayPos(const FDE_TEXTEDITPIECE &info)
void SetCharacterLimit(size_t limit)
void SetAliasChar(wchar_t alias)
RetainPtr< CFGAS_GEFont > GetFont() const
wchar_t GetChar(size_t idx) const override
size_t GetIndexUp(size_t pos) const
void SetTabWidth(float width)
void SetMaxEditOperationsForTesting(size_t max)
void ReplaceSelectedText(const WideString &str)
WideString DeleteSelectedText(RecordOperation add_operation=RecordOperation::kInsertRecord)
void SetLineSpace(float space)
void SetFontColor(FX_ARGB color)
int32_t GetCharCount() const
int32_t GetStartPos() const
uint32_t GetCharStyles() const
int32_t GetBidiLevel() const
int32_t GetWidth() const
void Offset(float dx, float dy)
constexpr CFX_RectF()=default
CFX_RectF & operator=(const CFX_RectF &other)=default
void Union(const CFX_RectF &rt)
WideString & operator+=(const WideString &str)
WideString & operator=(WideString &&that) noexcept
bool IsEmpty() const
Definition widestring.h:118
#define FX_IsOdd(a)
#define CHECK(cvref)
FDE_TEXTEDITPIECE(const FDE_TEXTEDITPIECE &that)
bool single_line_
Definition cfde_data.h:29
bool line_wrap_
Definition cfde_data.h:30