1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_TextRage_h_
#define mozilla_TextRage_h_
#include <stdint.h>
#include "mozilla/EventForwards.h"
#include "nsColor.h"
#include "nsISelectionController.h"
#include "nsITextInputProcessor.h"
#include "nsTArray.h"
namespace mozilla {
/******************************************************************************
* mozilla::TextRangeStyle
******************************************************************************/
struct TextRangeStyle {
typedef uint8_t LineStyleType;
// FYI: Modify IME_RANGE_LINE_* too when you modify LineStyle.
enum class LineStyle : LineStyleType {
None,
Solid,
Dotted,
Dashed,
Double,
Wavy,
};
inline static LineStyle ToLineStyle(RawTextRangeType aRawLineStyle) {
switch (static_cast<LineStyle>(aRawLineStyle)) {
case LineStyle::None:
case LineStyle::Solid:
case LineStyle::Dotted:
case LineStyle::Dashed:
case LineStyle::Double:
case LineStyle::Wavy:
return static_cast<LineStyle>(aRawLineStyle);
}
MOZ_ASSERT_UNREACHABLE("aRawLineStyle value is invalid");
return LineStyle::None;
}
enum {
DEFINED_NONE = 0x00,
DEFINED_LINESTYLE = 0x01,
DEFINED_FOREGROUND_COLOR = 0x02,
DEFINED_BACKGROUND_COLOR = 0x04,
DEFINED_UNDERLINE_COLOR = 0x08
};
// Initialize all members, because TextRange instances may be compared by
// memcomp.
//
// FIXME(emilio): I don't think that'd be sound, as it has padding which the
// compiler is not guaranteed to initialize.
TextRangeStyle() { Clear(); }
void Clear() {
mDefinedStyles = DEFINED_NONE;
mLineStyle = LineStyle::None;
mIsBoldLine = false;
mForegroundColor = mBackgroundColor = mUnderlineColor = NS_RGBA(0, 0, 0, 0);
}
bool IsDefined() const { return mDefinedStyles != DEFINED_NONE; }
bool IsLineStyleDefined() const {
return (mDefinedStyles & DEFINED_LINESTYLE) != 0;
}
bool IsForegroundColorDefined() const {
return (mDefinedStyles & DEFINED_FOREGROUND_COLOR) != 0;
}
bool IsBackgroundColorDefined() const {
return (mDefinedStyles & DEFINED_BACKGROUND_COLOR) != 0;
}
bool IsUnderlineColorDefined() const {
return (mDefinedStyles & DEFINED_UNDERLINE_COLOR) != 0;
}
bool IsNoChangeStyle() const {
return !IsForegroundColorDefined() && !IsBackgroundColorDefined() &&
IsLineStyleDefined() && mLineStyle == LineStyle::None;
}
bool Equals(const TextRangeStyle& aOther) const {
if (mDefinedStyles != aOther.mDefinedStyles) return false;
if (IsLineStyleDefined() && (mLineStyle != aOther.mLineStyle ||
!mIsBoldLine != !aOther.mIsBoldLine))
return false;
if (IsForegroundColorDefined() &&
(mForegroundColor != aOther.mForegroundColor))
return false;
if (IsBackgroundColorDefined() &&
(mBackgroundColor != aOther.mBackgroundColor))
return false;
if (IsUnderlineColorDefined() &&
(mUnderlineColor != aOther.mUnderlineColor))
return false;
return true;
}
bool operator!=(const TextRangeStyle& aOther) const {
return !Equals(aOther);
}
bool operator==(const TextRangeStyle& aOther) const { return Equals(aOther); }
uint8_t mDefinedStyles;
LineStyle mLineStyle; // DEFINED_LINESTYLE
bool mIsBoldLine; // DEFINED_LINESTYLE
nscolor mForegroundColor; // DEFINED_FOREGROUND_COLOR
nscolor mBackgroundColor; // DEFINED_BACKGROUND_COLOR
nscolor mUnderlineColor; // DEFINED_UNDERLINE_COLOR
};
/******************************************************************************
* mozilla::TextRange
******************************************************************************/
enum class TextRangeType : RawTextRangeType {
eUninitialized = 0x00,
eCaret = 0x01,
eRawClause = nsITextInputProcessor::ATTR_RAW_CLAUSE,
eSelectedRawClause = nsITextInputProcessor::ATTR_SELECTED_RAW_CLAUSE,
eConvertedClause = nsITextInputProcessor::ATTR_CONVERTED_CLAUSE,
eSelectedClause = nsITextInputProcessor::ATTR_SELECTED_CLAUSE
};
bool IsValidRawTextRangeValue(RawTextRangeType aRawTextRangeValue);
RawTextRangeType ToRawTextRangeType(TextRangeType aTextRangeType);
TextRangeType ToTextRangeType(RawTextRangeType aRawTextRangeType);
const char* ToChar(TextRangeType aTextRangeType);
SelectionType ToSelectionType(TextRangeType aTextRangeType);
struct TextRange {
TextRange()
: mStartOffset(0),
mEndOffset(0),
mRangeType(TextRangeType::eUninitialized) {}
uint32_t mStartOffset;
// XXX Storing end offset makes the initializing code very complicated.
// We should replace it with mLength.
uint32_t mEndOffset;
TextRangeStyle mRangeStyle;
TextRangeType mRangeType;
uint32_t Length() const { return mEndOffset - mStartOffset; }
bool IsClause() const { return mRangeType != TextRangeType::eCaret; }
bool Equals(const TextRange& aOther) const {
return mStartOffset == aOther.mStartOffset &&
mEndOffset == aOther.mEndOffset && mRangeType == aOther.mRangeType &&
mRangeStyle == aOther.mRangeStyle;
}
void RemoveCharacter(uint32_t aOffset) {
if (mStartOffset > aOffset) {
--mStartOffset;
--mEndOffset;
} else if (mEndOffset > aOffset) {
--mEndOffset;
}
}
};
/******************************************************************************
* mozilla::TextRangeArray
******************************************************************************/
class TextRangeArray final : public AutoTArray<TextRange, 10> {
friend class WidgetCompositionEvent;
~TextRangeArray() = default;
NS_INLINE_DECL_REFCOUNTING(TextRangeArray)
const TextRange* GetTargetClause() const {
for (uint32_t i = 0; i < Length(); ++i) {
const TextRange& range = ElementAt(i);
if (range.mRangeType == TextRangeType::eSelectedRawClause ||
range.mRangeType == TextRangeType::eSelectedClause) {
return ⦥
}
}
return nullptr;
}
// Returns target clause offset. If there are selected clauses, this returns
// the first selected clause offset. Otherwise, 0.
uint32_t TargetClauseOffset() const {
const TextRange* range = GetTargetClause();
return range ? range->mStartOffset : 0;
}
// Returns target clause length. If there are selected clauses, this returns
// the first selected clause length. Otherwise, UINT32_MAX.
uint32_t TargetClauseLength() const {
const TextRange* range = GetTargetClause();
return range ? range->Length() : UINT32_MAX;
}
public:
bool IsComposing() const {
for (uint32_t i = 0; i < Length(); ++i) {
if (ElementAt(i).IsClause()) {
return true;
}
}
return false;
}
bool Equals(const TextRangeArray& aOther) const {
size_t len = Length();
if (len != aOther.Length()) {
return false;
}
for (size_t i = 0; i < len; i++) {
if (!ElementAt(i).Equals(aOther.ElementAt(i))) {
return false;
}
}
return true;
}
void RemoveCharacter(uint32_t aOffset) {
for (size_t i = 0, len = Length(); i < len; i++) {
ElementAt(i).RemoveCharacter(aOffset);
}
}
bool HasCaret() const {
for (const TextRange& range : *this) {
if (range.mRangeType == TextRangeType::eCaret) {
return true;
}
}
return false;
}
bool HasClauses() const {
for (const TextRange& range : *this) {
if (range.IsClause()) {
return true;
}
}
return false;
}
uint32_t GetCaretPosition() const {
for (const TextRange& range : *this) {
if (range.mRangeType == TextRangeType::eCaret) {
return range.mStartOffset;
}
}
return UINT32_MAX;
}
const TextRange* GetFirstClause() const {
for (const TextRange& range : *this) {
// Look for the range of a clause whose start offset is 0 because the
// first clause's start offset is always 0.
if (range.IsClause() && !range.mStartOffset) {
return ⦥
}
}
MOZ_ASSERT(!HasClauses());
return nullptr;
}
};
} // namespace mozilla
#endif // mozilla_TextRage_h_
|