summaryrefslogtreecommitdiff
path: root/atk/atktext.c
blob: 086dd1871991b1900c21d3f35dfaf8932a0badd0 (plain)
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
/* ATK - The Accessibility Toolkit for GTK+
 * Copyright 2001 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "atktext.h"
#include "atkmarshal.h"
#include "atk-enum-types.h"

#include <string.h>

static GPtrArray *extra_attributes = NULL;

enum {
  TEXT_CHANGED,
  TEXT_CARET_MOVED,
  TEXT_SELECTION_CHANGED,
  TEXT_ATTRIBUTES_CHANGED,
  TEXT_INSERT,
  TEXT_REMOVE,
  TEXT_UPDATE,
  LAST_SIGNAL
};

static const char boolean[] =
  "false\0"
  "true";
static const guint8 boolean_offsets[] = {
  0, 6
};

static const char style[] =
  "normal\0"
  "oblique\0"
  "italic";
static const guint8 style_offsets[] = {
  0, 7, 15
};

static const char variant[] =
  "normal\0"
  "small_caps";
static const guint8 variant_offsets[] = {
  0, 7
};

static const char stretch[] =
  "ultra_condensed\0"
  "extra_condensed\0"
  "condensed\0"
  "semi_condensed\0"
  "normal\0"
  "semi_expanded\0"
  "expanded\0"
  "extra_expanded\0"
  "ultra_expanded";
static const guint8 stretch_offsets[] = {
  0, 16, 32, 42, 57, 64, 78, 87, 102
};

static const char justification[] =
  "left\0"
  "right\0"
  "center\0"
  "fill";
static const guint8 justification_offsets[] = {
  0, 5, 11, 18
};

static const char direction[] =
  "none\0"
  "ltr\0"
  "rtl";
static const guint8 direction_offsets[] = {
  0, 5, 9
};

static const char wrap_mode[] =
  "none\0"
  "char\0"
  "word\0"
  "word_char";
static const guint8 wrap_mode_offsets[] = {
  0, 5, 10, 15
};

static const char underline[] =
  "none\0"
  "single\0"
  "double\0"
  "low\0"
  "error";
static const guint8 underline_offsets[] = {
  0, 5, 12, 19, 23
};

static void atk_text_base_init (AtkTextIface *class);

static void atk_text_real_get_range_extents  (AtkText          *text,
                                              gint             start_offset,
                                              gint             end_offset,
                                              AtkCoordType     coord_type,
                                              AtkTextRectangle *rect);

static AtkTextRange** atk_text_real_get_bounded_ranges (AtkText          *text,
                                                        AtkTextRectangle *rect,
                                                        AtkCoordType     coord_type,
                                                        AtkTextClipType  x_clip_type,
                                                        AtkTextClipType  y_clip_type);

static guint atk_text_signals[LAST_SIGNAL] = { 0 };

GType
atk_text_get_type (void)
{
  static GType type = 0;

  if (!type) 
    {
      static const GTypeInfo tinfo =
      {
        sizeof (AtkTextIface),
        (GBaseInitFunc) atk_text_base_init,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) NULL /* atk_text_interface_init */ ,
        (GClassFinalizeFunc) NULL,

      };

      type = g_type_register_static (G_TYPE_INTERFACE, "AtkText", &tinfo, 0);
    }

  return type;
}

static void
atk_text_base_init (AtkTextIface *class)
{
  static gboolean initialized = FALSE;
  
  if (! initialized)
    {
      /* 
       * Note that text_changed signal supports details "insert", "delete", 
       * possibly "replace". 
       */
     
      class->get_range_extents = atk_text_real_get_range_extents; 
      class->get_bounded_ranges = atk_text_real_get_bounded_ranges; 

      atk_text_signals[TEXT_CHANGED] =
	g_signal_new ("text_changed",
		      ATK_TYPE_TEXT,
		      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
		      G_STRUCT_OFFSET (AtkTextIface, text_changed), 
		      (GSignalAccumulator) NULL, NULL,
		      atk_marshal_VOID__INT_INT,
		      G_TYPE_NONE,
		      2, G_TYPE_INT, G_TYPE_INT);

      atk_text_signals[TEXT_INSERT] =
	g_signal_new ("text_insert",
		      ATK_TYPE_TEXT,
		      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
		      0,
		      (GSignalAccumulator) NULL, NULL,
		      atk_marshal_VOID__INT_INT_STRING,
		      G_TYPE_NONE,
		      3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);

      atk_text_signals[TEXT_REMOVE] =
	g_signal_new ("text_remove",
		      ATK_TYPE_TEXT,
		      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
		      0,
		      (GSignalAccumulator) NULL, NULL,
		      atk_marshal_VOID__INT_INT_STRING,
		      G_TYPE_NONE,
		      3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);

      atk_text_signals[TEXT_UPDATE] =
	g_signal_new ("text_update",
		      ATK_TYPE_TEXT,
		      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
		      0,
		      (GSignalAccumulator) NULL, NULL,
		      atk_marshal_VOID__INT_INT_INT_STRING,
		      G_TYPE_NONE,
		      4, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);

      atk_text_signals[TEXT_CARET_MOVED] =
	g_signal_new ("text_caret_moved",
		      ATK_TYPE_TEXT,
		      G_SIGNAL_RUN_LAST,
		      G_STRUCT_OFFSET (AtkTextIface, text_caret_moved),
		      (GSignalAccumulator) NULL, NULL,
		      g_cclosure_marshal_VOID__INT,
		      G_TYPE_NONE,
		      1, G_TYPE_INT);
      atk_text_signals[TEXT_SELECTION_CHANGED] =
        g_signal_new ("text_selection_changed",
                      ATK_TYPE_TEXT,
                      G_SIGNAL_RUN_LAST,
                      G_STRUCT_OFFSET (AtkTextIface, text_selection_changed),
                      (GSignalAccumulator) NULL, NULL,
                      g_cclosure_marshal_VOID__VOID,
                      G_TYPE_NONE, 0);
      atk_text_signals[TEXT_ATTRIBUTES_CHANGED] =
        g_signal_new ("text_attributes_changed",
                      ATK_TYPE_TEXT,
                      G_SIGNAL_RUN_LAST,
                      G_STRUCT_OFFSET (AtkTextIface, text_attributes_changed),
                      (GSignalAccumulator) NULL, NULL,
                      g_cclosure_marshal_VOID__VOID,
                      G_TYPE_NONE, 0);

      
      initialized = TRUE;
    }
}

/**
 * atk_text_get_text:
 * @text: an #AtkText
 * @start_offset: start position
 * @end_offset: end position, or -1 for the end of the string.
 *
 * Gets the specified text.
 *
 * Returns: a newly allocated string containing the text from @start_offset up
 *   to, but not including @end_offset. Use g_free() to free the returned string.
 **/
gchar*
atk_text_get_text (AtkText      *text,
                   gint         start_offset,
                   gint         end_offset)
{
  AtkTextIface *iface;
  
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);

  iface = ATK_TEXT_GET_IFACE (text);

  if (start_offset < 0 || end_offset < -1 ||
      (end_offset != -1 && end_offset < start_offset))
    return NULL;

  if (iface->get_text)
    return (*(iface->get_text)) (text, start_offset, end_offset);
  else
    return NULL;
}

/**
 * atk_text_get_character_at_offset:
 * @text: an #AtkText
 * @offset: position
 *
 * Gets the specified text.
 *
 * Returns: the character at @offset.
 **/
gunichar
atk_text_get_character_at_offset (AtkText      *text,
                                  gint         offset)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_character_at_offset)
    return (*(iface->get_character_at_offset)) (text, offset);
  else
    return (gunichar) 0;
}

/**
 * atk_text_get_text_after_offset:
 * @text: an #AtkText
 * @offset: position
 * @boundary_type: An #AtkTextBoundary
 * @start_offset: the start offset of the returned string
 * @end_offset: the offset of the first character after the 
 *              returned substring
 *
 * Gets the specified text.
 *
 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character after the 
 * offset is returned.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
 * is from the word start after the offset to the next word start.
 *
 * The returned string will contain the word after the offset if the offset 
 * is inside a word or if the offset is not inside a word.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_END the returned string
 * is from the word end at or after the offset to the next work end.
 *
 * The returned string will contain the word after the offset if the offset
 * is inside a word and will contain the word after the word after the offset
 * if the offset is not inside a word.
 *
 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
 * string is from the sentence start after the offset to the next sentence
 * start.
 *
 * The returned string will contain the sentence after the offset if the offset
 * is inside a sentence or if the offset is not inside a sentence.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_SENTENCE_END the returned string
 * is from the sentence end at or after the offset to the next sentence end.
 *
 * The returned string will contain the sentence after the offset if the offset
 * is inside a sentence and will contain the sentence after the sentence
 * after the offset if the offset is not inside a sentence.
 *
 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
 * string is from the line start after the offset to the next line start.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_LINE_END the returned string
 * is from the line end at or after the offset to the next line end.
 *
 * Returns: a newly allocated string containing the text after @offset bounded
 *   by the specified @boundary_type. Use g_free() to free the returned string.
 **/
gchar*
atk_text_get_text_after_offset (AtkText          *text,
                                gint             offset,
                                AtkTextBoundary  boundary_type,
 				gint             *start_offset,
				gint		 *end_offset)
{
  AtkTextIface *iface;
  gint local_start_offset, local_end_offset;
  gint *real_start_offset, *real_end_offset;

  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);

  if (start_offset)
    real_start_offset = start_offset;
  else
    real_start_offset = &local_start_offset;
  if (end_offset)
    real_end_offset = end_offset;
  else
    real_end_offset = &local_end_offset;

  if (offset < 0)
    return NULL;

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_text_after_offset)
    return (*(iface->get_text_after_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
  else
    return NULL;
}

/**
 * atk_text_get_text_at_offset:
 * @text: an #AtkText
 * @offset: position
 * @boundary_type: An #AtkTextBoundary
 * @start_offset: the start offset of the returned string
 * @end_offset: the offset of the first character after the 
 *              returned substring
 *
 * Gets the specified text.
 *
 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
 * offset is returned.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
 * is from the word start at or before the offset to the word start after 
 * the offset.
 *
 * The returned string will contain the word at the offset if the offset
 * is inside a word and will contain the word before the offset if the 
 * offset is not inside a word.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_END the returned string
 * is from the word end before the offset to the word end at or after the
 * offset.
 *
 * The returned string will contain the word at the offset if the offset
 * is inside a word and will contain the word after to the offset if the 
 * offset is not inside a word.
 *
 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
 * string is from the sentence start at or before the offset to the sentence
 * start after the offset.
 *
 * The returned string will contain the sentence at the offset if the offset
 * is inside a sentence and will contain the sentence before the offset 
 * if the offset is not inside a sentence.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_SENTENCE_END the returned string
 * is from the sentence end before the offset to the sentence end at or
 * after the offset.
 *
 * The returned string will contain the sentence at the offset if the offset
 * is inside a sentence and will contain the sentence after the offset 
 * if the offset is not inside a sentence.
 *
 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
 * string is from the line start at or before the offset to the line
 * start after the offset.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_LINE_END the returned string
 * is from the line end before the offset to the line end at or after
 * the offset.
 *
 * Returns: a newly allocated string containing the text at @offset bounded by
 *   the specified @boundary_type. Use g_free() to free the returned string.
 **/
gchar*
atk_text_get_text_at_offset (AtkText          *text,
                             gint             offset,
                             AtkTextBoundary  boundary_type,
			     gint             *start_offset,
			     gint             *end_offset)
{
  AtkTextIface *iface;
  gint local_start_offset, local_end_offset;
  gint *real_start_offset, *real_end_offset;

  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);

  if (start_offset)
    real_start_offset = start_offset;
  else
    real_start_offset = &local_start_offset;
  if (end_offset)
    real_end_offset = end_offset;
  else
    real_end_offset = &local_end_offset;

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_text_at_offset)
    return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
  else
    return NULL;
}

/**
 * atk_text_get_text_before_offset:
 * @text: an #AtkText
 * @offset: position
 * @boundary_type: An #AtkTextBoundary
 * @start_offset: the start offset of the returned string
 * @end_offset: the offset of the first character after the 
 *              returned substring
 *
 * Gets the specified text.
 *
 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character before the
 * offset is returned.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
 * is from the word start before the word start before or at the offset to 
 * the word start before or at the offset.
 *
 * The returned string will contain the word before the offset if the offset
 * is inside a word and will contain the word before the word before the 
 * offset if the offset is not inside a word.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_END the returned string
 * is from the word end before the word end before the offset to the word
 * end before the offset.
 *
 * The returned string will contain the word before the offset if the offset
 * is inside a word or if the offset is not inside a word.
 *
 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
 * string is from the sentence start before the sentence start before 
 * the offset to the sentence start before the offset.
 *
 * The returned string will contain the sentence before the offset if the 
 * offset is inside a sentence and will contain the sentence before the 
 * sentence before the offset if the offset is not inside a sentence.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_SENTENCE_END the returned string
 * is from the sentence end before the sentence end at or before the offset to 
 * the sentence end at or before the offset.
 *
 * The returned string will contain the sentence before the offset if the 
 * offset is inside a sentence or if the offset is not inside a sentence.
 *
 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
 * string is from the line start before the line start ar or before the offset 
 * to the line start ar or before the offset.
 *
 * If the boundary_type is ATK_TEXT_BOUNDARY_LINE_END the returned string
 * is from the line end before the line end before the offset to the 
 * line end before the offset.
 *
 * Returns: a newly allocated string containing the text before @offset bounded
 *   by the specified @boundary_type. Use g_free() to free the returned string.
 **/
gchar*
atk_text_get_text_before_offset (AtkText          *text,
                                 gint             offset,
                                 AtkTextBoundary  boundary_type,
				 gint             *start_offset,
				 gint		  *end_offset)
{
  AtkTextIface *iface;
  gint local_start_offset, local_end_offset;
  gint *real_start_offset, *real_end_offset;

  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);

  if (start_offset)
    real_start_offset = start_offset;
  else
    real_start_offset = &local_start_offset;
  if (end_offset)
    real_end_offset = end_offset;
  else
    real_end_offset = &local_end_offset;

  if (offset < 0)
    return NULL;

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_text_before_offset)
    return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
  else
    return NULL;
}

/**
 * atk_text_get_caret_offset:
 * @text: an #AtkText
 *
 * Gets the offset position of the caret (cursor).
 *
 * Returns: the offset position of the caret (cursor).
 **/
gint
atk_text_get_caret_offset (AtkText *text)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), 0);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_caret_offset)
    return (*(iface->get_caret_offset)) (text);
  else
    return 0;
}

/**
 * atk_text_get_character_extents:
 * @text: an #AtkText
 * @offset: The offset of the text character for which bounding information is required.
 * @x: Pointer for the x cordinate of the bounding box
 * @y: Pointer for the y cordinate of the bounding box
 * @width: Pointer for the width of the bounding box
 * @height: Pointer for the height of the bounding box
 * @coords: specify whether coordinates are relative to the screen or widget window 
 *
 * Get the bounding box containing the glyph representing the character at 
 *     a particular text offset. 
 **/
void
atk_text_get_character_extents (AtkText *text,
                                gint offset,
                                gint *x,
                                gint *y,
                                gint *width,
                                gint *height,
			        AtkCoordType coords)
{
  AtkTextIface *iface;
  gint local_x, local_y, local_width, local_height;
  gint *real_x, *real_y, *real_width, *real_height;

  g_return_if_fail (ATK_IS_TEXT (text));

  if (x)
    real_x = x;
  else
    real_x = &local_x;
  if (y)
    real_y = y;
  else
    real_y = &local_y;
  if (width)
    real_width = width;
  else
    real_width = &local_width;
  if (height)
    real_height = height;
  else
    real_height = &local_height;

  *real_x = 0;
  *real_y = 0;
  *real_width = 0;
  *real_height = 0;

  if (offset < 0)
    return;
 
  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_character_extents)
    (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);

  if (*real_width <0)
    {
      *real_x = *real_x + *real_width;
      *real_width *= -1;
    }
}

/**
 * atk_text_get_run_attributes:
 *@text: an #AtkText
 *@offset: the offset at which to get the attributes, -1 means the offset of
 *the character to be inserted at the caret location.
 *@start_offset: the address to put the start offset of the range
 *@end_offset: the address to put the end offset of the range
 *
 *Creates an #AtkAttributeSet which consists of the attributes explicitly
 *set at the position @offset in the text. @start_offset and @end_offset are
 *set to the start and end of the range around @offset where the attributes are
 *invariant. Note that @end_offset is the offset of the first character
 *after the range.  See the enum AtkTextAttribute for types of text 
 *attributes that can be returned. Note that other attributes may also be 
 *returned.
 *
 *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
 * explicitly set at @offset. This #AtkAttributeSet should be freed by a call
 * to atk_attribute_set_free().
 **/
AtkAttributeSet* 
atk_text_get_run_attributes (AtkText          *text,
                             gint             offset,
                             gint             *start_offset,
                             gint             *end_offset)
{
  AtkTextIface *iface;
  gint local_start_offset, local_end_offset;
  gint *real_start_offset, *real_end_offset;

  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);

  if (start_offset)
    real_start_offset = start_offset;
  else
    real_start_offset = &local_start_offset;
  if (end_offset)
    real_end_offset = end_offset;
  else
    real_end_offset = &local_end_offset;

  if (offset < -1)
    return NULL;

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_run_attributes)
    return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
  else
    return NULL;
}

/**
 * atk_text_get_default_attributes:
 *@text: an #AtkText
 *
 *Creates an #AtkAttributeSet which consists of the default values of
 *attributes for the text. See the enum AtkTextAttribute for types of text 
 *attributes that can be returned. Note that other attributes may also be 
 *returned.
 *
 *Returns: (transfer full): an #AtkAttributeSet which contains the default
 * values of attributes.  at @offset. this #atkattributeset should be freed by
 * a call to atk_attribute_set_free().
 */
AtkAttributeSet* 
atk_text_get_default_attributes (AtkText          *text)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_default_attributes)
    return (*(iface->get_default_attributes)) (text);
  else
    return NULL;
}

/**
 * atk_text_get_character_count:
 * @text: an #AtkText
 *
 * Gets the character count.
 *
 * Returns: the number of characters.
 **/
gint
atk_text_get_character_count (AtkText *text)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), -1);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_character_count)
    return (*(iface->get_character_count)) (text);
  else
    return -1;
}

/**
 * atk_text_get_offset_at_point:
 * @text: an #AtkText
 * @x: screen x-position of character
 * @y: screen y-position of character
 * @coords: specify whether coordinates are relative to the screen or
 * widget window 
 *
 * Gets the offset of the character located at coordinates @x and @y. @x and @y
 * are interpreted as being relative to the screen or this widget's window
 * depending on @coords.
 *
 * Returns: the offset to the character which is located at
 * the specified @x and @y coordinates.
 **/
gint
atk_text_get_offset_at_point (AtkText *text,
                              gint x,
                              gint y,
			      AtkCoordType coords)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), -1);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_offset_at_point)
    return (*(iface->get_offset_at_point)) (text, x, y, coords);
  else
    return -1;
}

/**
 * atk_text_get_n_selections:
 * @text: an #AtkText
 *
 * Gets the number of selected regions.
 *
 * Returns: The number of selected regions, or -1 if a failure
 *   occurred.
 **/
gint
atk_text_get_n_selections (AtkText *text)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), -1);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_n_selections)
    return (*(iface->get_n_selections)) (text);
  else
    return -1;
}

/**
 * atk_text_get_selection:
 * @text: an #AtkText
 * @selection_num: The selection number.  The selected regions are
 * assigned numbers that correspond to how far the region is from the
 * start of the text.  The selected region closest to the beginning
 * of the text region is assigned the number 0, etc.  Note that adding,
 * moving or deleting a selected region can change the numbering.
 * @start_offset: passes back the start position of the selected region
 * @end_offset: passes back the end position of (e.g. offset immediately past) 
 * the selected region
 *
 * Gets the text from the specified selection.
 *
 * Returns: a newly allocated string containing the selected text. Use g_free()
 *   to free the returned string.
 **/
gchar*
atk_text_get_selection (AtkText *text, 
                        gint    selection_num,
                        gint    *start_offset,
                        gint    *end_offset)
{
  AtkTextIface *iface;
  gint local_start_offset, local_end_offset;
  gint *real_start_offset, *real_end_offset;

  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);

  if (start_offset)
    real_start_offset = start_offset;
  else
    real_start_offset = &local_start_offset;
  if (end_offset)
    real_end_offset = end_offset;
  else
    real_end_offset = &local_end_offset;

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_selection)
  {
    return (*(iface->get_selection)) (text, selection_num,
       real_start_offset, real_end_offset);
  }
  else
    return NULL;
}

/**
 * atk_text_add_selection:
 * @text: an #AtkText
 * @start_offset: the start position of the selected region
 * @end_offset: the offset of the first character after the selected region.
 *
 * Adds a selection bounded by the specified offsets.
 *
 * Returns: %TRUE if success, %FALSE otherwise
 **/
gboolean
atk_text_add_selection (AtkText *text, 
                        gint    start_offset,
                        gint    end_offset)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->add_selection)
    return (*(iface->add_selection)) (text, start_offset, end_offset);
  else
    return FALSE;
}

/**
 * atk_text_remove_selection:
 * @text: an #AtkText
 * @selection_num: The selection number.  The selected regions are
 * assigned numbers that correspond to how far the region is from the
 * start of the text.  The selected region closest to the beginning
 * of the text region is assigned the number 0, etc.  Note that adding,
 * moving or deleting a selected region can change the numbering.
 *
 * Removes the specified selection.
 *
 * Returns: %TRUE if success, %FALSE otherwise
 **/
gboolean
atk_text_remove_selection (AtkText *text, 
                           gint    selection_num)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->remove_selection)
    return (*(iface->remove_selection)) (text, selection_num);
  else
    return FALSE;
}

/**
 * atk_text_set_selection:
 * @text: an #AtkText
 * @selection_num: The selection number.  The selected regions are
 * assigned numbers that correspond to how far the region is from the
 * start of the text.  The selected region closest to the beginning
 * of the text region is assigned the number 0, etc.  Note that adding,
 * moving or deleting a selected region can change the numbering.
 * @start_offset: the new start position of the selection
 * @end_offset: the new end position of (e.g. offset immediately past) 
 * the selection
 *
 * Changes the start and end offset of the specified selection.
 *
 * Returns: %TRUE if success, %FALSE otherwise
 **/
gboolean
atk_text_set_selection (AtkText *text, 
                        gint    selection_num,
                        gint    start_offset, 
                        gint    end_offset)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->set_selection)
  {
    return (*(iface->set_selection)) (text, selection_num,
       start_offset, end_offset);
  }
  else
    return FALSE;
}

/**
 * atk_text_set_caret_offset:
 * @text: an #AtkText
 * @offset: position
 *
 * Sets the caret (cursor) position to the specified @offset.
 *
 * Returns: %TRUE if success, %FALSE otherwise.
 **/
gboolean
atk_text_set_caret_offset (AtkText *text,
                           gint    offset)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->set_caret_offset)
    {
      return (*(iface->set_caret_offset)) (text, offset);
    }
  else
    {
      return FALSE;
    }
}

/**
 * atk_text_get_range_extents:
 * @text: an #AtkText
 * @start_offset: The offset of the first text character for which boundary 
 *        information is required.
 * @end_offset: The offset of the text character after the last character 
 *        for which boundary information is required.
 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
 * @rect: A pointer to a AtkTextRectangle which is filled in by this function.
 *
 * Get the bounding box for text within the specified range.
 *
 * Since: 1.3
 **/
void
atk_text_get_range_extents (AtkText          *text,
                            gint             start_offset,
                            gint             end_offset,
                            AtkCoordType     coord_type,
                            AtkTextRectangle *rect)
{
  AtkTextIface *iface;

  g_return_if_fail (ATK_IS_TEXT (text));
  g_return_if_fail (rect);

  if (start_offset < 0 || start_offset >= end_offset)
    return;
 
  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_range_extents)
    (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
}

/**
 * atk_text_get_bounded_ranges:
 * @text: an #AtkText
 * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
 * @x_clip_type: Specify the horizontal clip type.
 * @y_clip_type: Specify the vertical clip type.
 *
 * Get the ranges of text in the specified bounding box.
 *
 * Since: 1.3
 *
 * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
 *          element of the array returned by this function will be NULL.
 **/
AtkTextRange**
atk_text_get_bounded_ranges (AtkText          *text,
                             AtkTextRectangle *rect,
                             AtkCoordType      coord_type,
                             AtkTextClipType   x_clip_type,
                             AtkTextClipType   y_clip_type)
{
  AtkTextIface *iface;

  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
  g_return_val_if_fail (rect, NULL);

  iface = ATK_TEXT_GET_IFACE (text);

  if (iface->get_bounded_ranges)
    return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
  else
    return NULL;
}

/**
 * atk_attribute_set_free:
 * @attrib_set: The #AtkAttributeSet to free
 *
 * Frees the memory used by an #AtkAttributeSet, including all its
 * #AtkAttributes.
 **/
void
atk_attribute_set_free (AtkAttributeSet *attrib_set)
{
  GSList *temp;

  temp = attrib_set;

  while (temp != NULL)
    {
      AtkAttribute *att;

      att = temp->data;

      g_free (att->name);
      g_free (att->value);
      g_free (att);
      temp = temp->next;
    }
  g_slist_free (attrib_set);
}

/**
 * atk_text_attribute_register:
 * @name: a name string
 *
 * Associate @name with a new #AtkTextAttribute
 *
 * Returns: an #AtkTextAttribute associated with @name
 **/
AtkTextAttribute
atk_text_attribute_register (const gchar *name)
{
  g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);

  if (!extra_attributes)
    extra_attributes = g_ptr_array_new ();

  g_ptr_array_add (extra_attributes, g_strdup (name));
  return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
}

/**
 * atk_text_attribute_get_name:
 * @attr: The #AtkTextAttribute whose name is required
 *
 * Gets the name corresponding to the #AtkTextAttribute
 *
 * Returns: a string containing the name; this string should not be freed
 **/
const gchar*
atk_text_attribute_get_name (AtkTextAttribute attr)
{
  GTypeClass *type_class;
  GEnumValue *value;
  const gchar *name = NULL;

  type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);

  value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);

  if (value)
    {
      name = value->value_nick;
    }
  else
    {
      if (extra_attributes)
        {
          gint n = attr;

          n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;

          if (n < extra_attributes->len)

            name = g_ptr_array_index (extra_attributes, n);
        }
    }
  g_type_class_unref (type_class);
  return name;
}

/**
 * atk_text_attribute_for_name:
 * @name: a string which is the (non-localized) name of an ATK text attribute.
 *
 * Get the #AtkTextAttribute type corresponding to a text attribute name.
 *
 * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
name,
 *          or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute is found.
 **/
AtkTextAttribute
atk_text_attribute_for_name (const gchar *name)
{
  GTypeClass *type_class;
  GEnumValue *value;
  AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;

  g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);

  type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);

  value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);

  if (value)
    {
      type = value->value;
    }
  else
    {
      gint i;

      if (extra_attributes)
        {
          for (i = 0; i < extra_attributes->len; i++)
            {
              gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);

              g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);

              if (strcmp (name, extra_attribute) == 0)
                {
                  type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
                  break;
                }
            }
        }
    }
  g_type_class_unref (type_class);

  return type;
}

/**
 * atk_text_attribute_get_value:
 * @attr: The #AtkTextAttribute for which a value is required
 * @index_: The index of the required value
 *
 * Gets the value for the index of the #AtkTextAttribute
 *
 * Returns: a string containing the value; this string should not be freed;
 * NULL is returned if there are no values maintained for the attr value. 
 **/
const gchar*
atk_text_attribute_get_value (AtkTextAttribute attr,
                              gint             index)
{
  switch (attr)
    {
    case ATK_TEXT_ATTR_INVISIBLE:
    case ATK_TEXT_ATTR_EDITABLE:
    case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
    case ATK_TEXT_ATTR_STRIKETHROUGH:
    case ATK_TEXT_ATTR_BG_STIPPLE:
    case ATK_TEXT_ATTR_FG_STIPPLE:
      g_assert (index >= 0 && index < G_N_ELEMENTS (boolean_offsets));
      return boolean + boolean_offsets[index];
    case ATK_TEXT_ATTR_UNDERLINE:
      g_assert (index >= 0 && index < G_N_ELEMENTS (underline_offsets));
      return underline + underline_offsets[index];
    case ATK_TEXT_ATTR_WRAP_MODE:
      g_assert (index >= 0 && index < G_N_ELEMENTS (wrap_mode_offsets));
      return wrap_mode + wrap_mode_offsets[index];
    case ATK_TEXT_ATTR_DIRECTION:
      g_assert (index >= 0 && index < G_N_ELEMENTS (direction_offsets));
      return direction + direction_offsets[index];
    case ATK_TEXT_ATTR_JUSTIFICATION:
      g_assert (index >= 0 && index < G_N_ELEMENTS (justification_offsets));
      return justification + justification_offsets[index];
    case ATK_TEXT_ATTR_STRETCH:
      g_assert (index >= 0 && index < G_N_ELEMENTS (stretch_offsets));
      return stretch + stretch_offsets[index];
    case ATK_TEXT_ATTR_VARIANT:
      g_assert (index >= 0 && index < G_N_ELEMENTS (variant_offsets));
      return variant + variant_offsets[index];
    case ATK_TEXT_ATTR_STYLE:
      g_assert (index >= 0 && index < G_N_ELEMENTS (style_offsets));
      return style + style_offsets[index];
    default:
      return NULL;
   }
}

static void
atk_text_rectangle_union (AtkTextRectangle *src1,
                          AtkTextRectangle *src2,
                          AtkTextRectangle *dest)
{
  gint dest_x, dest_y;

  dest_x = MIN (src1->x, src2->x);
  dest_y = MIN (src1->y, src2->y);
  dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
  dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
  dest->x = dest_x;
  dest->y = dest_y;
}

static gboolean
atk_text_rectangle_contain (AtkTextRectangle *clip,
                            AtkTextRectangle *bounds,
                            AtkTextClipType  x_clip_type,
                            AtkTextClipType  y_clip_type)
{
  gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;

  x_min_ok = (bounds->x >= clip->x) ||
             ((bounds->x + bounds->width >= clip->x) &&
              ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
               (x_clip_type == ATK_TEXT_CLIP_MAX)));

  x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
             ((bounds->x <= clip->x + clip->width) &&
              ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
               (x_clip_type == ATK_TEXT_CLIP_MIN)));

  y_min_ok = (bounds->y >= clip->y) ||
             ((bounds->y + bounds->height >= clip->y) &&
              ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
               (y_clip_type == ATK_TEXT_CLIP_MAX)));

  y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
             ((bounds->y <= clip->y + clip->height) &&
              ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
               (y_clip_type == ATK_TEXT_CLIP_MIN)));

  return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
  
}

static void 
atk_text_real_get_range_extents (AtkText           *text,
                                 gint              start_offset,
                                 gint              end_offset,
                                 AtkCoordType      coord_type,
                                 AtkTextRectangle  *rect)
{
  gint i;
  AtkTextRectangle cbounds, bounds;

  atk_text_get_character_extents (text, start_offset,
                                  &bounds.x, &bounds.y,
                                  &bounds.width, &bounds.height,
                                  coord_type);

  for (i = start_offset + 1; i < end_offset; i++)
    {
      atk_text_get_character_extents (text, i,
                                      &cbounds.x, &cbounds.y, 
                                      &cbounds.width, &cbounds.height, 
                                      coord_type);
      atk_text_rectangle_union (&bounds, &cbounds, &bounds);
    }

  rect->x = bounds.x;
  rect->y = bounds.y;
  rect->width = bounds.width;
  rect->height = bounds.height;
}

static AtkTextRange**
atk_text_real_get_bounded_ranges (AtkText          *text,
                                  AtkTextRectangle *rect,
                                  AtkCoordType     coord_type,
                                  AtkTextClipType  x_clip_type,
                                  AtkTextClipType  y_clip_type)
{
  gint bounds_min_offset, bounds_max_offset;
  gint min_line_start, min_line_end;
  gint max_line_start, max_line_end;
  gchar *line;
  gint curr_offset;
  gint offset;
  gint num_ranges = 0;
  gint range_size = 1;
  AtkTextRectangle cbounds;
  AtkTextRange **range;

  range = NULL;
  bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
  bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);

  if (bounds_min_offset == 0 &&
      bounds_min_offset == bounds_max_offset)
    return NULL;

  line = atk_text_get_text_at_offset (text, bounds_min_offset, 
                                      ATK_TEXT_BOUNDARY_LINE_START,
                                      &min_line_start, &min_line_end);
  g_free (line);
  line = atk_text_get_text_at_offset (text, bounds_max_offset, 
                                      ATK_TEXT_BOUNDARY_LINE_START,
                                      &max_line_start, &max_line_end);
  g_free (line);
  bounds_min_offset = MIN (min_line_start, max_line_start);
  bounds_max_offset = MAX (min_line_end, max_line_end);

  curr_offset = bounds_min_offset;
  while (curr_offset < bounds_max_offset)
    {
      offset = curr_offset;

      while (curr_offset < bounds_max_offset)
        {
          atk_text_get_character_extents (text, curr_offset,
                                          &cbounds.x, &cbounds.y,
                                          &cbounds.width, &cbounds.height,
                                          coord_type);
          if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
	    break;
          curr_offset++;
        }
      if (curr_offset > offset)
        {
          AtkTextRange *one_range = g_new (AtkTextRange, 1);

          one_range->start_offset = offset;
          one_range->end_offset = curr_offset;
          one_range->content = atk_text_get_text (text, offset, curr_offset);
          atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);

          if (num_ranges >= range_size - 1)
            {
              range_size *= 2;
              range = g_realloc (range, range_size * sizeof (gpointer));
            }
          range[num_ranges] = one_range;
          num_ranges++; 
        }   
      curr_offset++;
      if (range)
        range[num_ranges] = NULL;
    }
  return range;
}

/**
 * atk_text_free_ranges:
 * @ranges: A pointer to an array of  #AtkTextRange which is to be freed.
 *
 * Frees the memory associated with an array of AtkTextRange. It is assumed
 * that the array was returned by the function atk_text_get_bounded_ranges
 * and is NULL terminated.
 *
 * Since: 1.3
 **/
void
atk_text_free_ranges (AtkTextRange **ranges)
{
  AtkTextRange **first = ranges;

  if (ranges)
    {
      while (*ranges)
        {
          AtkTextRange *range;

          range = *ranges;
          ranges++;
          g_free (range->content);
          g_free (range);
        }
      g_free (first);
    }
}

static AtkTextRange *
atk_text_range_copy (AtkTextRange *src)
{
  AtkTextRange *dst = g_new0 (AtkTextRange, 1);
  dst->bounds = src->bounds;
  dst->start_offset = src->start_offset;
  dst->end_offset = src->end_offset;
  if (src->content)
    dst->content = g_strdup (src->content);
  return dst;
}

static void
atk_text_range_free (AtkTextRange *range)
{
  g_free (range->content);
  g_free (range);
}

G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy,
                     atk_text_range_free)