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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */
#include "InterfaceInitFuncs.h"
#include "mozilla/a11y/PDocAccessible.h"
#include "nsAccessibilityService.h"
#include "LocalAccessible-inl.h"
#include "HyperTextAccessible-inl.h"
#include "nsMai.h"
#include "RemoteAccessible.h"
#include "AccAttributes.h"
#include "nsIAccessibleTypes.h"
#include "nsISimpleEnumerator.h"
#include "nsUTF8Utils.h"
#include "mozilla/Likely.h"
#include "DOMtoATK.h"
using namespace mozilla;
using namespace mozilla::a11y;
static const char* sAtkTextAttrNames[ATK_TEXT_ATTR_LAST_DEFINED];
static AtkAttributeSet* ConvertToAtkTextAttributeSet(
AccAttributes* aAttributes) {
if (!aAttributes) {
// This can happen if an Accessible dies in the content process, but the
// parent hasn't been udpated yet.
return nullptr;
}
AtkAttributeSet* atkAttributeSet = nullptr;
for (auto iter : *aAttributes) {
AtkAttribute* atkAttr = (AtkAttribute*)g_malloc(sizeof(AtkAttribute));
nsAutoString value;
// We set atkAttr->name directly for each case. For the value, we set the
// value string for each case. atkAttr->value is set at the end based on the
// value string.
// Set atkAttr->name to a specific ATK attribute name.
auto atkName = [&atkAttr](AtkTextAttribute aAttrNum) {
atkAttr->name = g_strdup(sAtkTextAttrNames[aAttrNum]);
};
// Set value to a formatted ATK color value.
auto colorValue = [&iter, &value] {
// The format of the atk attribute is r,g,b and the gecko one is
// rgb(r, g, b).
auto color = iter.Value<Color>();
MOZ_ASSERT(color);
value.AppendInt(NS_GET_R(color->mValue));
value.Append(',');
value.AppendInt(NS_GET_G(color->mValue));
value.Append(',');
value.AppendInt(NS_GET_B(color->mValue));
};
nsAtom* name = iter.Name();
if (name == nsGkAtoms::color) {
atkName(ATK_TEXT_ATTR_FG_COLOR);
colorValue();
} else if (name == nsGkAtoms::backgroundColor) {
atkName(ATK_TEXT_ATTR_BG_COLOR);
colorValue();
} else if (name == nsGkAtoms::font_family) {
atkName(ATK_TEXT_ATTR_FAMILY_NAME);
iter.ValueAsString(value);
} else if (name == nsGkAtoms::font_size) {
atkName(ATK_TEXT_ATTR_SIZE);
// ATK wants the number of points without pt at the end.
auto fontSize = iter.Value<FontSize>();
MOZ_ASSERT(fontSize);
value.AppendInt(fontSize->mValue);
} else if (name == nsGkAtoms::fontWeight) {
atkName(ATK_TEXT_ATTR_WEIGHT);
iter.ValueAsString(value);
} else if (name == nsGkAtoms::invalid) {
atkName(ATK_TEXT_ATTR_INVALID);
iter.ValueAsString(value);
} else {
nsAutoString nameStr;
iter.NameAsString(nameStr);
atkAttr->name = g_strdup(NS_ConvertUTF16toUTF8(nameStr).get());
iter.ValueAsString(value);
}
atkAttr->value = g_strdup(NS_ConvertUTF16toUTF8(value).get());
atkAttributeSet = g_slist_prepend(atkAttributeSet, atkAttr);
}
// libatk-adaptor will free it
return atkAttributeSet;
}
extern "C" {
static gchar* getTextCB(AtkText* aText, gint aStartOffset, gint aEndOffset) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc || !acc->IsTextRole()) {
return nullptr;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text) {
return nullptr;
}
return DOMtoATK::NewATKString(text, aStartOffset, aEndOffset);
}
static gint getCharacterCountCB(AtkText* aText);
// Note: this does not support magic offsets, which is fine for its callers
// which do not implement any.
static gchar* getCharTextAtOffset(AtkText* aText, gint aOffset,
gint* aStartOffset, gint* aEndOffset) {
gint end = aOffset + 1;
gint count = getCharacterCountCB(aText);
if (aOffset > count) {
aOffset = count;
}
if (end > count) {
end = count;
}
if (aOffset < 0) {
aOffset = 0;
}
if (end < 0) {
end = 0;
}
*aStartOffset = aOffset;
*aEndOffset = end;
return getTextCB(aText, aOffset, end);
}
static gchar* getTextAfterOffsetCB(AtkText* aText, gint aOffset,
AtkTextBoundary aBoundaryType,
gint* aStartOffset, gint* aEndOffset) {
if (aBoundaryType == ATK_TEXT_BOUNDARY_CHAR) {
return getCharTextAtOffset(aText, aOffset + 1, aStartOffset, aEndOffset);
}
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return nullptr;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return nullptr;
}
nsAutoString autoStr;
int32_t startOffset = 0, endOffset = 0;
text->TextAfterOffset(aOffset, aBoundaryType, &startOffset, &endOffset,
autoStr);
*aStartOffset = startOffset;
*aEndOffset = endOffset;
// libspi will free it.
return DOMtoATK::Convert(autoStr);
}
static gchar* getTextAtOffsetCB(AtkText* aText, gint aOffset,
AtkTextBoundary aBoundaryType,
gint* aStartOffset, gint* aEndOffset) {
if (aBoundaryType == ATK_TEXT_BOUNDARY_CHAR) {
return getCharTextAtOffset(aText, aOffset, aStartOffset, aEndOffset);
}
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return nullptr;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return nullptr;
}
nsAutoString autoStr;
int32_t startOffset = 0, endOffset = 0;
text->TextAtOffset(aOffset, aBoundaryType, &startOffset, &endOffset, autoStr);
*aStartOffset = startOffset;
*aEndOffset = endOffset;
// libspi will free it.
return DOMtoATK::Convert(autoStr);
}
static gunichar getCharacterAtOffsetCB(AtkText* aText, gint aOffset) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return 0;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (text) {
return DOMtoATK::ATKCharacter(text, aOffset);
}
return 0;
}
static gchar* getTextBeforeOffsetCB(AtkText* aText, gint aOffset,
AtkTextBoundary aBoundaryType,
gint* aStartOffset, gint* aEndOffset) {
if (aBoundaryType == ATK_TEXT_BOUNDARY_CHAR) {
return getCharTextAtOffset(aText, aOffset - 1, aStartOffset, aEndOffset);
}
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return nullptr;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return nullptr;
}
nsAutoString autoStr;
int32_t startOffset = 0, endOffset = 0;
text->TextBeforeOffset(aOffset, aBoundaryType, &startOffset, &endOffset,
autoStr);
*aStartOffset = startOffset;
*aEndOffset = endOffset;
// libspi will free it.
return DOMtoATK::Convert(autoStr);
}
static gint getCaretOffsetCB(AtkText* aText) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return -1;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return -1;
}
return static_cast<gint>(text->CaretOffset());
}
static AtkAttributeSet* getRunAttributesCB(AtkText* aText, gint aOffset,
gint* aStartOffset,
gint* aEndOffset) {
*aStartOffset = -1;
*aEndOffset = -1;
int32_t startOffset = 0, endOffset = 0;
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return nullptr;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return nullptr;
}
RefPtr<AccAttributes> attributes =
text->TextAttributes(false, aOffset, &startOffset, &endOffset);
*aStartOffset = startOffset;
*aEndOffset = endOffset;
return ConvertToAtkTextAttributeSet(attributes);
}
static AtkAttributeSet* getDefaultAttributesCB(AtkText* aText) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return nullptr;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return nullptr;
}
RefPtr<AccAttributes> attributes = text->DefaultTextAttributes();
return ConvertToAtkTextAttributeSet(attributes);
}
static void getCharacterExtentsCB(AtkText* aText, gint aOffset, gint* aX,
gint* aY, gint* aWidth, gint* aHeight,
AtkCoordType aCoords) {
if (!aX || !aY || !aWidth || !aHeight) {
return;
}
*aX = *aY = *aWidth = *aHeight = -1;
uint32_t geckoCoordType;
if (aCoords == ATK_XY_SCREEN) {
geckoCoordType = nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE;
} else {
geckoCoordType = nsIAccessibleCoordinateType::COORDTYPE_WINDOW_RELATIVE;
}
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return;
}
LayoutDeviceIntRect rect = text->CharBounds(aOffset, geckoCoordType);
*aX = rect.x;
*aY = rect.y;
*aWidth = rect.width;
*aHeight = rect.height;
}
static void getRangeExtentsCB(AtkText* aText, gint aStartOffset,
gint aEndOffset, AtkCoordType aCoords,
AtkTextRectangle* aRect) {
if (!aRect) {
return;
}
aRect->x = aRect->y = aRect->width = aRect->height = -1;
uint32_t geckoCoordType;
if (aCoords == ATK_XY_SCREEN) {
geckoCoordType = nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE;
} else {
geckoCoordType = nsIAccessibleCoordinateType::COORDTYPE_WINDOW_RELATIVE;
}
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return;
}
LayoutDeviceIntRect rect =
text->TextBounds(aStartOffset, aEndOffset, geckoCoordType);
aRect->x = rect.x;
aRect->y = rect.y;
aRect->width = rect.width;
aRect->height = rect.height;
}
static gint getCharacterCountCB(AtkText* aText) {
if (Accessible* acc = GetInternalObj(ATK_OBJECT(aText))) {
if (HyperTextAccessibleBase* text = acc->AsHyperTextBase()) {
return static_cast<gint>(text->CharacterCount());
}
}
return 0;
}
static gint getOffsetAtPointCB(AtkText* aText, gint aX, gint aY,
AtkCoordType aCoords) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return -1;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return -1;
}
return static_cast<gint>(text->OffsetAtPoint(
aX, aY,
(aCoords == ATK_XY_SCREEN
? nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE
: nsIAccessibleCoordinateType::COORDTYPE_WINDOW_RELATIVE)));
}
static gint getTextSelectionCountCB(AtkText* aText) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return 0;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return 0;
}
return text->SelectionCount();
}
static gchar* getTextSelectionCB(AtkText* aText, gint aSelectionNum,
gint* aStartOffset, gint* aEndOffset) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return nullptr;
}
int32_t startOffset = 0, endOffset = 0;
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return nullptr;
}
text->SelectionBoundsAt(aSelectionNum, &startOffset, &endOffset);
*aStartOffset = startOffset;
*aEndOffset = endOffset;
return getTextCB(aText, *aStartOffset, *aEndOffset);
}
// set methods
static gboolean addTextSelectionCB(AtkText* aText, gint aStartOffset,
gint aEndOffset) {
AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
if (accWrap) {
HyperTextAccessible* text = accWrap->AsHyperText();
if (!text || !text->IsTextRole()) {
return FALSE;
}
return text->AddToSelection(aStartOffset, aEndOffset);
}
if (RemoteAccessible* proxy = GetProxy(ATK_OBJECT(aText))) {
return proxy->AddToSelection(aStartOffset, aEndOffset);
}
return FALSE;
}
static gboolean removeTextSelectionCB(AtkText* aText, gint aSelectionNum) {
AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
if (accWrap) {
HyperTextAccessible* text = accWrap->AsHyperText();
if (!text || !text->IsTextRole()) {
return FALSE;
}
return text->RemoveFromSelection(aSelectionNum);
}
if (RemoteAccessible* proxy = GetProxy(ATK_OBJECT(aText))) {
return proxy->RemoveFromSelection(aSelectionNum);
}
return FALSE;
}
static gboolean setTextSelectionCB(AtkText* aText, gint aSelectionNum,
gint aStartOffset, gint aEndOffset) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc || !acc->IsTextRole()) {
return FALSE;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text) {
return FALSE;
}
return text->SetSelectionBoundsAt(aSelectionNum, aStartOffset, aEndOffset);
}
static gboolean setCaretOffsetCB(AtkText* aText, gint aOffset) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return FALSE;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text || !acc->IsTextRole()) {
return FALSE;
}
text->SetCaretOffset(aOffset);
return TRUE;
}
static gboolean scrollSubstringToCB(AtkText* aText, gint aStartOffset,
gint aEndOffset, AtkScrollType aType) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return FALSE;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text) {
return FALSE;
}
text->ScrollSubstringTo(aStartOffset, aEndOffset, aType);
return TRUE;
}
static gboolean scrollSubstringToPointCB(AtkText* aText, gint aStartOffset,
gint aEndOffset, AtkCoordType aCoords,
gint aX, gint aY) {
Accessible* acc = GetInternalObj(ATK_OBJECT(aText));
if (!acc) {
return FALSE;
}
HyperTextAccessibleBase* text = acc->AsHyperTextBase();
if (!text) {
return FALSE;
}
text->ScrollSubstringToPoint(aStartOffset, aEndOffset, aCoords, aX, aY);
return TRUE;
}
}
void textInterfaceInitCB(AtkTextIface* aIface) {
NS_ASSERTION(aIface, "Invalid aIface");
if (MOZ_UNLIKELY(!aIface)) return;
aIface->get_text = getTextCB;
aIface->get_text_after_offset = getTextAfterOffsetCB;
aIface->get_text_at_offset = getTextAtOffsetCB;
aIface->get_character_at_offset = getCharacterAtOffsetCB;
aIface->get_text_before_offset = getTextBeforeOffsetCB;
aIface->get_caret_offset = getCaretOffsetCB;
aIface->get_run_attributes = getRunAttributesCB;
aIface->get_default_attributes = getDefaultAttributesCB;
aIface->get_character_extents = getCharacterExtentsCB;
aIface->get_range_extents = getRangeExtentsCB;
aIface->get_character_count = getCharacterCountCB;
aIface->get_offset_at_point = getOffsetAtPointCB;
aIface->get_n_selections = getTextSelectionCountCB;
aIface->get_selection = getTextSelectionCB;
// set methods
aIface->add_selection = addTextSelectionCB;
aIface->remove_selection = removeTextSelectionCB;
aIface->set_selection = setTextSelectionCB;
aIface->set_caret_offset = setCaretOffsetCB;
if (IsAtkVersionAtLeast(2, 32)) {
aIface->scroll_substring_to = scrollSubstringToCB;
aIface->scroll_substring_to_point = scrollSubstringToPointCB;
}
// Cache the string values of the atk text attribute names.
for (uint32_t i = 0; i < ArrayLength(sAtkTextAttrNames); i++) {
sAtkTextAttrNames[i] =
atk_text_attribute_get_name(static_cast<AtkTextAttribute>(i));
}
}
|