Bug 1746870: part 1) Minorly extend documentation in <jsactors.rst>. r=hsivonen
[gecko.git] / widget / IMEData.cpp
blob12ee9cb63c19363b03419514f269f531b39446b8
1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "IMEData.h"
7 #include <sstream>
9 #include "gfxFontUtils.h"
11 #include "mozilla/WritingModes.h"
13 #include "nsPrintfCString.h"
14 #include "nsString.h"
16 namespace mozilla {
18 PrintStringDetail::PrintStringDetail(const nsAString& aString,
19 uint32_t aMaxLength /* = UINT32_MAX */) {
20 Assign("\"");
21 const uint32_t kFirstHalf =
22 aString.Length() <= aMaxLength ? UINT32_MAX : (aMaxLength + 1) / 2;
23 const uint32_t kSecondHalf =
24 aString.Length() <= aMaxLength ? 0 : aMaxLength / 2;
25 for (uint32_t i = 0; i < aString.Length(); i++) {
26 if (i > 0) {
27 Append(" ");
29 char32_t ch = aString.CharAt(i);
30 if (NS_IS_HIGH_SURROGATE(ch) && i + 1 < aString.Length() &&
31 NS_IS_LOW_SURROGATE(aString.CharAt(i + 1))) {
32 ch = SURROGATE_TO_UCS4(ch, aString.CharAt(i + 1));
34 Append(PrintCharData(ch));
35 if (i + 1 == kFirstHalf) {
36 Append(" ...");
37 i = aString.Length() - kSecondHalf - 1;
38 if (NS_IS_LOW_SURROGATE(aString.CharAt(i)) &&
39 NS_IS_HIGH_SURROGATE(aString.CharAt(i - 1))) {
40 if (i - 1 <= kFirstHalf) {
41 i++;
42 } else {
43 i--;
46 } else if (!IS_IN_BMP(ch)) {
47 i++;
50 Append("\"");
53 // static
54 nsCString PrintStringDetail::PrintCharData(char32_t aChar) {
55 switch (aChar) {
56 case 0x0000:
57 return "NULL (0x0000)"_ns;
58 case 0x0008:
59 return "BACKSPACE (0x0008)"_ns;
60 case 0x0009:
61 return "CHARACTER TABULATION (0x0009)"_ns;
62 case 0x000A:
63 return "LINE FEED (0x000A)"_ns;
64 case 0x000B:
65 return "LINE TABULATION (0x000B)"_ns;
66 case 0x000C:
67 return "FORM FEED (0x000C)"_ns;
68 case 0x000D:
69 return "CARRIAGE RETURN (0x000D)"_ns;
70 case 0x0018:
71 return "CANCEL (0x0018)"_ns;
72 case 0x001B:
73 return "ESCAPE (0x001B)"_ns;
74 case 0x0020:
75 return "SPACE (0x0020)"_ns;
76 case 0x007F:
77 return "DELETE (0x007F)"_ns;
78 case 0x00A0:
79 return "NO-BREAK SPACE (0x00A0)"_ns;
80 case 0x00AD:
81 return "SOFT HYPHEN (0x00AD)"_ns;
82 case 0x2000:
83 return "EN QUAD (0x2000)"_ns;
84 case 0x2001:
85 return "EM QUAD (0x2001)"_ns;
86 case 0x2002:
87 return "EN SPACE (0x2002)"_ns;
88 case 0x2003:
89 return "EM SPACE (0x2003)"_ns;
90 case 0x2004:
91 return "THREE-PER-EM SPACE (0x2004)"_ns;
92 case 0x2005:
93 return "FOUR-PER-EM SPACE (0x2005)"_ns;
94 case 0x2006:
95 return "SIX-PER-EM SPACE (0x2006)"_ns;
96 case 0x2007:
97 return "FIGURE SPACE (0x2007)"_ns;
98 case 0x2008:
99 return "PUNCTUATION SPACE (0x2008)"_ns;
100 case 0x2009:
101 return "THIN SPACE (0x2009)"_ns;
102 case 0x200A:
103 return "HAIR SPACE (0x200A)"_ns;
104 case 0x200B:
105 return "ZERO WIDTH SPACE (0x200B)"_ns;
106 case 0x200C:
107 return "ZERO WIDTH NON-JOINER (0x200C)"_ns;
108 case 0x200D:
109 return "ZERO WIDTH JOINER (0x200D)"_ns;
110 case 0x200E:
111 return "LEFT-TO-RIGHT MARK (0x200E)"_ns;
112 case 0x200F:
113 return "RIGHT-TO-LEFT MARK (0x200F)"_ns;
114 case 0x2029:
115 return "PARAGRAPH SEPARATOR (0x2029)"_ns;
116 case 0x202A:
117 return "LEFT-TO-RIGHT EMBEDDING (0x202A)"_ns;
118 case 0x202B:
119 return "RIGHT-TO-LEFT EMBEDDING (0x202B)"_ns;
120 case 0x202D:
121 return "LEFT-TO-RIGHT OVERRIDE (0x202D)"_ns;
122 case 0x202E:
123 return "RIGHT-TO-LEFT OVERRIDE (0x202E)"_ns;
124 case 0x202F:
125 return "NARROW NO-BREAK SPACE (0x202F)"_ns;
126 case 0x205F:
127 return "MEDIUM MATHEMATICAL SPACE (0x205F)"_ns;
128 case 0x2060:
129 return "WORD JOINER (0x2060)"_ns;
130 case 0x2066:
131 return "LEFT-TO-RIGHT ISOLATE (0x2066)"_ns;
132 case 0x2067:
133 return "RIGHT-TO-LEFT ISOLATE (0x2067)"_ns;
134 case 0x3000:
135 return "IDEOGRAPHIC SPACE (0x3000)"_ns;
136 case 0xFEFF:
137 return "ZERO WIDTH NO-BREAK SPACE (0xFEFF)"_ns;
138 default: {
139 if (aChar < ' ' || (aChar >= 0x80 && aChar < 0xA0)) {
140 return nsPrintfCString("Control (0x%04X)", aChar);
142 if (NS_IS_HIGH_SURROGATE(aChar)) {
143 return nsPrintfCString("High Surrogate (0x%04X)", aChar);
145 if (NS_IS_LOW_SURROGATE(aChar)) {
146 return nsPrintfCString("Low Surrogate (0x%04X)", aChar);
148 if (gfxFontUtils::IsVarSelector(aChar)) {
149 return IS_IN_BMP(aChar)
150 ? nsPrintfCString("Variant Selector (0x%04X)", aChar)
151 : nsPrintfCString("Variant Selector (0x%08X)", aChar);
153 nsAutoString utf16Str;
154 AppendUCS4ToUTF16(aChar, utf16Str);
155 return IS_IN_BMP(aChar)
156 ? nsPrintfCString("'%s' (0x%04X)",
157 NS_ConvertUTF16toUTF8(utf16Str).get(), aChar)
158 : nsPrintfCString("'%s' (0x%08X)",
159 NS_ConvertUTF16toUTF8(utf16Str).get(),
160 aChar);
165 namespace widget {
167 std::ostream& operator<<(std::ostream& aStream, const IMEEnabled& aEnabled) {
168 switch (aEnabled) {
169 case IMEEnabled::Disabled:
170 return aStream << "DISABLED";
171 case IMEEnabled::Enabled:
172 return aStream << "ENABLED";
173 case IMEEnabled::Password:
174 return aStream << "PASSWORD";
175 case IMEEnabled::Unknown:
176 return aStream << "illegal value";
178 MOZ_ASSERT_UNREACHABLE("Add a case to handle your new IMEEnabled value");
179 return aStream;
182 std::ostream& operator<<(std::ostream& aStream, const IMEState::Open& aOpen) {
183 switch (aOpen) {
184 case IMEState::DONT_CHANGE_OPEN_STATE:
185 aStream << "DONT_CHANGE_OPEN_STATE";
186 break;
187 case IMEState::OPEN:
188 aStream << "OPEN";
189 break;
190 case IMEState::CLOSED:
191 aStream << "CLOSED";
192 break;
193 default:
194 aStream << "illegal value";
195 break;
197 return aStream;
200 std::ostream& operator<<(std::ostream& aStream, const IMEState& aState) {
201 aStream << "{ mEnabled=" << aState.mEnabled << ", mOpen=" << aState.mOpen
202 << " }";
203 return aStream;
206 std::ostream& operator<<(std::ostream& aStream,
207 const InputContext::Origin& aOrigin) {
208 switch (aOrigin) {
209 case InputContext::ORIGIN_MAIN:
210 aStream << "ORIGIN_MAIN";
211 break;
212 case InputContext::ORIGIN_CONTENT:
213 aStream << "ORIGIN_CONTENT";
214 break;
215 default:
216 aStream << "illegal value";
217 break;
219 return aStream;
222 std::ostream& operator<<(std::ostream& aStream, const InputContext& aContext) {
223 aStream << "{ mIMEState=" << aContext.mIMEState
224 << ", mOrigin=" << aContext.mOrigin << ", mHTMLInputType=\""
225 << aContext.mHTMLInputType << "\", mHTMLInputInputmode=\""
226 << aContext.mHTMLInputInputmode << "\", mActionHint=\""
227 << aContext.mActionHint << "\", mAutocapitalize=\""
228 << aContext.mAutocapitalize << "\", mMayBeIMEUnaware="
229 << (aContext.mMayBeIMEUnaware ? "true" : "false")
230 << ", mIsPrivateBrowsing="
231 << (aContext.mInPrivateBrowsing ? "true" : "false") << " }";
232 return aStream;
235 std::ostream& operator<<(std::ostream& aStream,
236 const InputContextAction::Cause& aCause) {
237 switch (aCause) {
238 case InputContextAction::CAUSE_UNKNOWN:
239 aStream << "CAUSE_UNKNOWN";
240 break;
241 case InputContextAction::CAUSE_UNKNOWN_CHROME:
242 aStream << "CAUSE_UNKNOWN_CHROME";
243 break;
244 case InputContextAction::CAUSE_KEY:
245 aStream << "CAUSE_KEY";
246 break;
247 case InputContextAction::CAUSE_MOUSE:
248 aStream << "CAUSE_MOUSE";
249 break;
250 case InputContextAction::CAUSE_TOUCH:
251 aStream << "CAUSE_TOUCH";
252 break;
253 case InputContextAction::CAUSE_LONGPRESS:
254 aStream << "CAUSE_LONGPRESS";
255 break;
256 case InputContextAction::CAUSE_UNKNOWN_DURING_NON_KEYBOARD_INPUT:
257 aStream << "CAUSE_UNKNOWN_DURING_NON_KEYBOARD_INPUT";
258 break;
259 case InputContextAction::CAUSE_UNKNOWN_DURING_KEYBOARD_INPUT:
260 aStream << "CAUSE_UNKNOWN_DURING_KEYBOARD_INPUT";
261 break;
262 default:
263 aStream << "illegal value";
264 break;
266 return aStream;
269 std::ostream& operator<<(std::ostream& aStream,
270 const InputContextAction::FocusChange& aFocusChange) {
271 switch (aFocusChange) {
272 case InputContextAction::FOCUS_NOT_CHANGED:
273 aStream << "FOCUS_NOT_CHANGED";
274 break;
275 case InputContextAction::GOT_FOCUS:
276 aStream << "GOT_FOCUS";
277 break;
278 case InputContextAction::LOST_FOCUS:
279 aStream << "LOST_FOCUS";
280 break;
281 case InputContextAction::MENU_GOT_PSEUDO_FOCUS:
282 aStream << "MENU_GOT_PSEUDO_FOCUS";
283 break;
284 case InputContextAction::MENU_LOST_PSEUDO_FOCUS:
285 aStream << "MENU_LOST_PSEUDO_FOCUS";
286 break;
287 case InputContextAction::WIDGET_CREATED:
288 aStream << "WIDGET_CREATED";
289 break;
290 default:
291 aStream << "illegal value";
292 break;
294 return aStream;
297 std::ostream& operator<<(
298 std::ostream& aStream,
299 const IMENotification::SelectionChangeDataBase& aData) {
300 if (!aData.IsValid()) {
301 aStream << "{ IsValid()=false }";
302 return aStream;
304 aStream << "{ mOffset=" << aData.mOffset;
305 if (aData.mString->Length() > 20) {
306 aStream << ", mString.Length()=" << aData.mString->Length();
307 } else {
308 aStream << ", mString=\"" << NS_ConvertUTF16toUTF8(*aData.mString)
309 << "\" (Length()=" << aData.mString->Length() << ")";
312 aStream << ", GetWritingMode()=" << aData.GetWritingMode()
313 << ", mReversed=" << (aData.mReversed ? "true" : "false")
314 << ", mCausedByComposition="
315 << (aData.mCausedByComposition ? "true" : "false")
316 << ", mCausedBySelectionEvent="
317 << (aData.mCausedBySelectionEvent ? "true" : "false")
318 << ", mOccurredDuringComposition="
319 << (aData.mOccurredDuringComposition ? "true" : "false") << " }";
320 return aStream;
323 std::ostream& operator<<(std::ostream& aStream,
324 const IMENotification::TextChangeDataBase& aData) {
325 if (!aData.IsValid()) {
326 aStream << "{ IsValid()=false }";
327 return aStream;
329 aStream << "{ mStartOffset=" << aData.mStartOffset
330 << ", mRemoveEndOffset=" << aData.mRemovedEndOffset
331 << ", mAddedEndOffset=" << aData.mAddedEndOffset
332 << ", mCausedOnlyByComposition="
333 << (aData.mCausedOnlyByComposition ? "true" : "false")
334 << ", mIncludingChangesDuringComposition="
335 << (aData.mIncludingChangesDuringComposition ? "true" : "false")
336 << ", mIncludingChangesWithoutComposition="
337 << (aData.mIncludingChangesWithoutComposition ? "true" : "false")
338 << " }";
339 return aStream;
342 } // namespace widget
343 } // namespace mozilla