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
|
/* Pango2
* pango-attr-list.c: Attribute lists
*
* Copyright (C) 2000-2002 Red Hat Software
*
* 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 "config.h"
#include <string.h>
#include "pango-attr-list-private.h"
#include "pango-attr-private.h"
#include "pango-impl-utils.h"
G_DEFINE_BOXED_TYPE (Pango2AttrList, pango2_attr_list,
pango2_attr_list_copy,
pango2_attr_list_unref);
/* {{{ Utilities */
static void
pango2_attr_list_insert_internal (Pango2AttrList *list,
Pango2Attribute *attr,
gboolean before)
{
const guint start_index = attr->start_index;
Pango2Attribute *last_attr;
if (G_UNLIKELY (!list->attributes))
list->attributes = g_ptr_array_new ();
if (list->attributes->len == 0)
{
g_ptr_array_add (list->attributes, attr);
return;
}
g_assert (list->attributes->len > 0);
last_attr = g_ptr_array_index (list->attributes, list->attributes->len - 1);
if (last_attr->start_index < start_index ||
(!before && last_attr->start_index == start_index))
{
g_ptr_array_add (list->attributes, attr);
}
else
{
guint i, p;
for (i = 0, p = list->attributes->len; i < p; i++)
{
Pango2Attribute *cur = g_ptr_array_index (list->attributes, i);
if (cur->start_index > start_index ||
(before && cur->start_index == start_index))
{
g_ptr_array_insert (list->attributes, i, attr);
break;
}
}
}
}
/* }}} */
/* {{{ Private API */
void
pango2_attr_list_init (Pango2AttrList *list)
{
list->ref_count = 1;
list->attributes = NULL;
}
void
pango2_attr_list_destroy (Pango2AttrList *list)
{
if (!list->attributes)
return;
g_ptr_array_free (list->attributes, TRUE);
}
gboolean
pango2_attr_list_has_attributes (const Pango2AttrList *list)
{
return list && list->attributes != NULL && list->attributes->len > 0;
}
/* }}} */
/* {{{ Public API */
/**
* pango2_attr_list_new:
*
* Create a new empty attribute list with a reference
* count of one.
*
* Return value: (transfer full): the newly allocated
* `Pango2AttrList`, which should be freed with
* [method@Pango2.AttrList.unref]
*/
Pango2AttrList *
pango2_attr_list_new (void)
{
Pango2AttrList *list = g_slice_new (Pango2AttrList);
pango2_attr_list_init (list);
return list;
}
/**
* pango2_attr_list_ref:
* @list: (nullable): a `Pango2AttrList`
*
* Increase the reference count of the given attribute
* list by one.
*
* Return value: The attribute list passed in
*/
Pango2AttrList *
pango2_attr_list_ref (Pango2AttrList *list)
{
if (list == NULL)
return NULL;
g_atomic_int_inc ((int *) &list->ref_count);
return list;
}
/**
* pango2_attr_list_unref:
* @list: (nullable): a `Pango2AttrList`
*
* Decrease the reference count of the given attribute
* list by one.
*
* If the result is zero, free the attribute list
* and the attributes it contains.
*/
void
pango2_attr_list_unref (Pango2AttrList *list)
{
if (list == NULL)
return;
g_return_if_fail (list->ref_count > 0);
if (g_atomic_int_dec_and_test ((int *) &list->ref_count))
{
pango2_attr_list_destroy (list);
g_slice_free (Pango2AttrList, list);
}
}
/**
* pango2_attr_list_copy:
* @list: (nullable): a `Pango2AttrList`
*
* Copy @list and return an identical new list.
*
* Return value: (nullable): the newly allocated
* `Pango2AttrList`, with a reference count of one,
* which should be freed with [method@Pango2.AttrList.unref]
*/
Pango2AttrList *
pango2_attr_list_copy (Pango2AttrList *list)
{
Pango2AttrList *new;
if (list == NULL)
return NULL;
new = pango2_attr_list_new ();
if (!list->attributes || list->attributes->len == 0)
return new;
new->attributes = g_ptr_array_copy (list->attributes, (GCopyFunc)pango2_attribute_copy, NULL);
return new;
}
/**
* pango2_attr_list_insert:
* @list: a `Pango2AttrList`
* @attr: (transfer full): the attribute to insert
*
* Insert the given attribute into the `Pango2AttrList`.
*
* It will be inserted after all other attributes with a
* matching @start_index.
*/
void
pango2_attr_list_insert (Pango2AttrList *list,
Pango2Attribute *attr)
{
g_return_if_fail (list != NULL);
g_return_if_fail (attr != NULL);
pango2_attr_list_insert_internal (list, attr, FALSE);
}
/**
* pango2_attr_list_insert_before:
* @list: a `Pango2AttrList`
* @attr: (transfer full): the attribute to insert
*
* Insert the given attribute into the `Pango2AttrList`.
*
* It will be inserted before all other attributes with a
* matching @start_index.
*/
void
pango2_attr_list_insert_before (Pango2AttrList *list,
Pango2Attribute *attr)
{
g_return_if_fail (list != NULL);
g_return_if_fail (attr != NULL);
pango2_attr_list_insert_internal (list, attr, TRUE);
}
/**
* pango2_attr_list_change:
* @list: a `Pango2AttrList`
* @attr: (transfer full): the attribute to insert
*
* Insert the given attribute into the `Pango2AttrList`.
*
* It will replace any attributes of the same type
* on that segment and be merged with any adjoining
* attributes that are identical.
*
* This function is slower than [method@Pango2.AttrList.insert]
* for creating an attribute list in order (potentially
* much slower for large lists). However,
* [method@Pango2.AttrList.insert] is not suitable for
* continually changing a set of attributes since it
* never removes or combines existing attributes.
*/
void
pango2_attr_list_change (Pango2AttrList *list,
Pango2Attribute *attr)
{
guint i, p;
guint start_index = attr->start_index;
guint end_index = attr->end_index;
gboolean inserted;
g_return_if_fail (list != NULL);
if (start_index == end_index) /* empty, nothing to do */
{
pango2_attribute_destroy (attr);
return;
}
if (!list->attributes || list->attributes->len == 0)
{
pango2_attr_list_insert (list, attr);
return;
}
inserted = FALSE;
for (i = 0, p = list->attributes->len; i < p; i++)
{
Pango2Attribute *tmp_attr = g_ptr_array_index (list->attributes, i);
if (tmp_attr->start_index > start_index)
{
g_ptr_array_insert (list->attributes, i, attr);
inserted = TRUE;
break;
}
if (tmp_attr->type != attr->type)
continue;
if (tmp_attr->end_index < start_index)
continue; /* This attr does not overlap with the new one */
g_assert (tmp_attr->start_index <= start_index);
g_assert (tmp_attr->end_index >= start_index);
if (pango2_attribute_equal (tmp_attr, attr))
{
/* We can merge the new attribute with this attribute
*/
if (tmp_attr->end_index >= end_index)
{
/* We are totally overlapping the previous attribute.
* No action is needed.
*/
pango2_attribute_destroy (attr);
return;
}
tmp_attr->end_index = end_index;
pango2_attribute_destroy (attr);
attr = tmp_attr;
inserted = TRUE;
break;
}
else
{
/* Split, truncate, or remove the old attribute
*/
if (tmp_attr->end_index > end_index)
{
Pango2Attribute *end_attr = pango2_attribute_copy (tmp_attr);
end_attr->start_index = end_index;
pango2_attr_list_insert (list, end_attr);
}
if (tmp_attr->start_index == start_index)
{
pango2_attribute_destroy (tmp_attr);
g_ptr_array_remove_index (list->attributes, i);
break;
}
else
{
tmp_attr->end_index = start_index;
}
}
}
if (!inserted)
/* we didn't insert attr yet */
pango2_attr_list_insert (list, attr);
/* We now have the range inserted into the list one way or the
* other. Fix up the remainder
*/
/* Attention: No i = 0 here. */
for (i = i + 1, p = list->attributes->len; i < p; i++)
{
Pango2Attribute *tmp_attr = g_ptr_array_index (list->attributes, i);
if (tmp_attr->start_index > end_index)
break;
if (tmp_attr->type != attr->type)
continue;
if (tmp_attr == attr)
continue;
if (tmp_attr->end_index <= attr->end_index ||
pango2_attribute_equal (tmp_attr, attr))
{
/* We can merge the new attribute with this attribute. */
attr->end_index = MAX (end_index, tmp_attr->end_index);
pango2_attribute_destroy (tmp_attr);
g_ptr_array_remove_index (list->attributes, i);
i--;
p--;
continue;
}
else
{
/* Trim the start of this attribute that it begins at the end
* of the new attribute. This may involve moving it in the list
* to maintain the required non-decreasing order of start indices.
*/
int k, m;
tmp_attr->start_index = attr->end_index;
for (k = i + 1, m = list->attributes->len; k < m; k++)
{
Pango2Attribute *tmp_attr2 = g_ptr_array_index (list->attributes, k);
if (tmp_attr2->start_index >= tmp_attr->start_index)
break;
g_ptr_array_index (list->attributes, k - 1) = tmp_attr2;
g_ptr_array_index (list->attributes, k) = tmp_attr;
}
}
}
}
/**
* pango2_attr_list_update:
* @list: a `Pango2AttrList`
* @pos: the position of the change
* @remove: the number of removed bytes
* @add: the number of added bytes
*
* Update indices of attributes in @list for a change in the
* text they refer to.
*
* The change that this function applies is removing @remove
* bytes at position @pos and inserting @add bytes instead.
*
* Attributes that fall entirely in the (@pos, @pos + @remove)
* range are removed.
*
* Attributes that start or end inside the (@pos, @pos + @remove)
* range are shortened to reflect the removal.
*
* Attributes start and end positions are updated if they are
* behind @pos + @remove.
*/
void
pango2_attr_list_update (Pango2AttrList *list,
int pos,
int remove,
int add)
{
guint i, p;
g_return_if_fail (pos >= 0);
g_return_if_fail (remove >= 0);
g_return_if_fail (add >= 0);
if (list->attributes)
for (i = 0, p = list->attributes->len; i < p; i++)
{
Pango2Attribute *attr = g_ptr_array_index (list->attributes, i);
if (attr->start_index >= pos &&
attr->end_index < pos + remove)
{
pango2_attribute_destroy (attr);
g_ptr_array_remove_index (list->attributes, i);
i--; /* Look at this index again */
p--;
continue;
}
if (attr->start_index != PANGO2_ATTR_INDEX_FROM_TEXT_BEGINNING)
{
if (attr->start_index >= pos &&
attr->start_index < pos + remove)
{
attr->start_index = pos + add;
}
else if (attr->start_index >= pos + remove)
{
attr->start_index += add - remove;
}
}
if (attr->end_index != PANGO2_ATTR_INDEX_TO_TEXT_END)
{
if (attr->end_index >= pos &&
attr->end_index < pos + remove)
{
attr->end_index = pos;
}
else if (attr->end_index >= pos + remove)
{
if (add > remove &&
G_MAXUINT - attr->end_index < add - remove)
attr->end_index = G_MAXUINT;
else
attr->end_index += add - remove;
}
}
}
}
/**
* pango2_attr_list_splice:
* @list: a `Pango2AttrList`
* @other: another `Pango2AttrList`
* @pos: the position in @list at which to insert @other
* @len: the length of the spliced segment. (Note that this
* must be specified since the attributes in @other may only
* be present at some subsection of this range)
*
* This function opens up a hole in @list, fills it
* in with attributes from the left, and then merges
* @other on top of the hole.
*
* This operation is equivalent to stretching every attribute
* that applies at position @pos in @list by an amount @len,
* and then calling [method@Pango2.AttrList.change] with a copy
* of each attribute in @other in sequence (offset in position
* by @pos, and limited in length to @len).
*
* This operation proves useful for, for instance, inserting
* a pre-edit string in the middle of an edit buffer.
*
* For backwards compatibility, the function behaves differently
* when @len is 0. In this case, the attributes from @other are
* not imited to @len, and are just overlayed on top of @list.
*
* This mode is useful for merging two lists of attributes together.
*/
void
pango2_attr_list_splice (Pango2AttrList *list,
Pango2AttrList *other,
int pos,
int len)
{
guint i, p;
guint upos, ulen;
guint end;
g_return_if_fail (list != NULL);
g_return_if_fail (other != NULL);
g_return_if_fail (pos >= 0);
g_return_if_fail (len >= 0);
upos = (guint)pos;
ulen = (guint)len;
/* This definition only works when a and b are unsigned; overflow
* isn't defined in the C standard for signed integers
*/
#define CLAMP_ADD(a,b) (((a) + (b) < (a)) ? G_MAXUINT : (a) + (b))
end = CLAMP_ADD (upos, ulen);
if (list->attributes)
for (i = 0, p = list->attributes->len; i < p; i++)
{
Pango2Attribute *attr = g_ptr_array_index (list->attributes, i);;
if (attr->start_index <= upos)
{
if (attr->end_index > upos)
attr->end_index = CLAMP_ADD (attr->end_index, ulen);
}
else
{
/* This could result in a zero length attribute if it
* gets squashed up against G_MAXUINT, but deleting such
* an element could (in theory) suprise the caller, so
* we don't delete it.
*/
attr->start_index = CLAMP_ADD (attr->start_index, ulen);
attr->end_index = CLAMP_ADD (attr->end_index, ulen);
}
}
if (!other->attributes || other->attributes->len == 0)
return;
for (i = 0, p = other->attributes->len; i < p; i++)
{
Pango2Attribute *attr = pango2_attribute_copy (g_ptr_array_index (other->attributes, i));
if (ulen > 0)
{
attr->start_index = MIN (CLAMP_ADD (attr->start_index, upos), end);
attr->end_index = MIN (CLAMP_ADD (attr->end_index, upos), end);
}
else
{
attr->start_index = CLAMP_ADD (attr->start_index, upos);
attr->end_index = CLAMP_ADD (attr->end_index, upos);
}
/* Same as above, the attribute could be squashed to zero-length; here
* pango2_attr_list_change() will take care of deleting it.
*/
pango2_attr_list_change (list, attr);
}
#undef CLAMP_ADD
}
/**
* pango2_attr_list_get_attributes:
* @list: a `Pango2AttrList`
*
* Gets a list of all attributes in @list.
*
* Return value: (element-type Pango2.Attribute) (transfer full):
* a list of all attributes in @list. To free this value,
* call [method@Pango2.Attribute.destroy] on each value and
* [func@GLib.SList.free] on the list
*/
GSList *
pango2_attr_list_get_attributes (Pango2AttrList *list)
{
GSList *result = NULL;
guint i, p;
g_return_val_if_fail (list != NULL, NULL);
if (!list->attributes || list->attributes->len == 0)
return NULL;
for (i = 0, p = list->attributes->len; i < p; i++)
{
Pango2Attribute *attr = g_ptr_array_index (list->attributes, i);
result = g_slist_prepend (result, pango2_attribute_copy (attr));
}
return g_slist_reverse (result);
}
/**
* pango2_attr_list_equal:
* @list: a `Pango2AttrList`
* @other_list: the other `Pango2AttrList`
*
* Checks whether @list and @other_list contain the same
* attributes and whether those attributes apply to the
* same ranges.
*
* Beware that this will return wrong values if any list
* contains duplicates.
*
* Return value: %TRUE if the lists are equal, %FALSE if
* they aren't
*/
gboolean
pango2_attr_list_equal (Pango2AttrList *list,
Pango2AttrList *other_list)
{
GPtrArray *attrs, *other_attrs;
guint64 skip_bitmask = 0;
guint i;
if (list == other_list)
return TRUE;
if (list == NULL || other_list == NULL)
return FALSE;
if (list->attributes == NULL || other_list->attributes == NULL)
return list->attributes == other_list->attributes;
attrs = list->attributes;
other_attrs = other_list->attributes;
if (attrs->len != other_attrs->len)
return FALSE;
for (i = 0; i < attrs->len; i++)
{
Pango2Attribute *attr = g_ptr_array_index (attrs, i);
gboolean attr_equal = FALSE;
guint other_attr_index;
for (other_attr_index = 0; other_attr_index < other_attrs->len; other_attr_index++)
{
Pango2Attribute *other_attr = g_ptr_array_index (other_attrs, other_attr_index);
guint64 other_attr_bitmask = other_attr_index < 64 ? 1 << other_attr_index : 0;
if ((skip_bitmask & other_attr_bitmask) != 0)
continue;
if (attr->start_index == other_attr->start_index &&
attr->end_index == other_attr->end_index &&
pango2_attribute_equal (attr, other_attr))
{
skip_bitmask |= other_attr_bitmask;
attr_equal = TRUE;
break;
}
}
if (!attr_equal)
return FALSE;
}
return TRUE;
}
/**
* pango2_attr_list_filter:
* @list: a `Pango2AttrList`
* @func: (scope call) (closure data): callback function;
* returns %TRUE if an attribute should be filtered out
* @data: (closure): Data to be passed to @func
*
* Given a `Pango2AttrList` and callback function, removes
* any elements of @list for which @func returns %TRUE and
* inserts them into a new list.
*
* Return value: (transfer full) (nullable): the new
* `Pango2AttrList` or %NULL if no attributes of the
* given types were found
*/
Pango2AttrList *
pango2_attr_list_filter (Pango2AttrList *list,
Pango2AttrFilterFunc func,
gpointer data)
{
Pango2AttrList *new = NULL;
guint i, p;
g_return_val_if_fail (list != NULL, NULL);
if (!list->attributes || list->attributes->len == 0)
return NULL;
for (i = 0, p = list->attributes->len; i < p; i++)
{
Pango2Attribute *tmp_attr = g_ptr_array_index (list->attributes, i);
if ((*func) (tmp_attr, data))
{
g_ptr_array_remove_index (list->attributes, i);
i--; /* Need to look at this index again */
p--;
if (G_UNLIKELY (!new))
{
new = pango2_attr_list_new ();
new->attributes = g_ptr_array_new ();
}
g_ptr_array_add (new->attributes, tmp_attr);
}
}
return new;
}
/* }}} */
/* {{{ Serialization */
/* We serialize attribute lists to strings. The format
* is a comma-separated list of the attributes in the order
* in which they are in the list, with each attribute having
* this format:
*
* START END NICK VALUE
*
* Values that can contain a comma, such as font descriptions
* are quoted with "".
*/
static GType
get_attr_value_type (Pango2AttrType type)
{
switch ((int)type)
{
case PANGO2_ATTR_STYLE: return PANGO2_TYPE_STYLE;
case PANGO2_ATTR_WEIGHT: return PANGO2_TYPE_WEIGHT;
case PANGO2_ATTR_VARIANT: return PANGO2_TYPE_VARIANT;
case PANGO2_ATTR_STRETCH: return PANGO2_TYPE_STRETCH;
case PANGO2_ATTR_GRAVITY: return PANGO2_TYPE_GRAVITY;
case PANGO2_ATTR_GRAVITY_HINT: return PANGO2_TYPE_GRAVITY_HINT;
case PANGO2_ATTR_UNDERLINE: return PANGO2_TYPE_LINE_STYLE;
case PANGO2_ATTR_STRIKETHROUGH: return PANGO2_TYPE_LINE_STYLE;
case PANGO2_ATTR_OVERLINE: return PANGO2_TYPE_LINE_STYLE;
case PANGO2_ATTR_BASELINE_SHIFT: return PANGO2_TYPE_BASELINE_SHIFT;
case PANGO2_ATTR_FONT_SCALE: return PANGO2_TYPE_FONT_SCALE;
case PANGO2_ATTR_TEXT_TRANSFORM: return PANGO2_TYPE_TEXT_TRANSFORM;
default: return G_TYPE_INVALID;
}
}
static void
append_enum_value (GString *str,
GType type,
int value)
{
GEnumClass *enum_class;
GEnumValue *enum_value;
enum_class = g_type_class_ref (type);
enum_value = g_enum_get_value (enum_class, value);
g_type_class_unref (enum_class);
if (enum_value)
g_string_append_printf (str, " %s", enum_value->value_nick);
else
g_string_append_printf (str, " %d", value);
}
static void
attr_print (GString *str,
Pango2Attribute *attr)
{
const char *name;
name = pango2_attr_type_get_name (attr->type);
if (!name)
return;
g_string_append_printf (str, "%u %u %s", attr->start_index, attr->end_index, name);
switch (PANGO2_ATTR_VALUE_TYPE (attr))
{
case PANGO2_ATTR_VALUE_INT:
if (attr->type == PANGO2_ATTR_WEIGHT ||
attr->type == PANGO2_ATTR_STYLE ||
attr->type == PANGO2_ATTR_STRETCH ||
attr->type == PANGO2_ATTR_VARIANT ||
attr->type == PANGO2_ATTR_GRAVITY ||
attr->type == PANGO2_ATTR_GRAVITY_HINT ||
attr->type == PANGO2_ATTR_UNDERLINE ||
attr->type == PANGO2_ATTR_OVERLINE ||
attr->type == PANGO2_ATTR_BASELINE_SHIFT ||
attr->type == PANGO2_ATTR_FONT_SCALE ||
attr->type == PANGO2_ATTR_TEXT_TRANSFORM)
append_enum_value (str, get_attr_value_type (attr->type), attr->int_value);
else
g_string_append_printf (str, " %d", attr->int_value);
break;
case PANGO2_ATTR_VALUE_BOOLEAN:
g_string_append (str, attr->int_value ? " true" : " false");
break;
case PANGO2_ATTR_VALUE_STRING:
g_string_append_printf (str, " \"%s\"", attr->str_value);
break;
case PANGO2_ATTR_VALUE_LANGUAGE:
g_string_append_printf (str, " %s", pango2_language_to_string (attr->lang_value));
break;
case PANGO2_ATTR_VALUE_FLOAT:
{
char buf[20];
g_ascii_formatd (buf, 20, "%f", attr->double_value);
g_string_append_printf (str, " %s", buf);
}
break;
case PANGO2_ATTR_VALUE_FONT_DESC:
{
char *s = pango2_font_description_to_string (attr->font_value);
g_string_append_printf (str, " \"%s\"", s);
g_free (s);
}
break;
case PANGO2_ATTR_VALUE_COLOR:
{
char *s = pango2_color_to_string (&attr->color_value);
g_string_append_printf (str, " %s", s);
g_free (s);
}
break;
case PANGO2_ATTR_VALUE_POINTER:
{
char *s = pango2_attr_value_serialize (attr);
g_string_append_printf (str, " %s", s);
g_free (s);
}
break;
default:
g_assert_not_reached ();
}
}
/**
* pango2_attr_list_to_string:
* @list: a `Pango2AttrList`
*
* Serializes a `Pango2AttrList` to a string.
*
* No guarantees are made about the format of the string,
* it may change between Pango versions.
*
* The intended use of this function is testing and
* debugging. The format is not meant as a permanent
* storage format.
*
* Returns: (transfer full): a newly allocated string
*/
char *
pango2_attr_list_to_string (Pango2AttrList *list)
{
GString *s;
s = g_string_new ("");
if (list->attributes)
for (int i = 0; i < list->attributes->len; i++)
{
Pango2Attribute *attr = g_ptr_array_index (list->attributes, i);
if (i > 0)
g_string_append (s, "\n");
attr_print (s, attr);
}
return g_string_free (s, FALSE);
}
static Pango2AttrType
get_attr_type_by_nick (const char *nick,
int len)
{
GEnumClass *enum_class;
enum_class = g_type_class_ref (pango2_attr_type_get_type ());
for (GEnumValue *ev = enum_class->values; ev->value_name; ev++)
{
if (ev->value_nick && strncmp (ev->value_nick, nick, len) == 0)
{
g_type_class_unref (enum_class);
return (Pango2AttrType) ev->value;
}
}
g_type_class_unref (enum_class);
return PANGO2_ATTR_INVALID;
}
static int
get_attr_value (Pango2AttrType type,
const char *str,
int len)
{
GEnumClass *enum_class;
char *endp;
int value;
enum_class = g_type_class_ref (get_attr_value_type (type));
for (GEnumValue *ev = enum_class->values; ev->value_name; ev++)
{
if (ev->value_nick && strncmp (ev->value_nick, str, len) == 0)
{
g_type_class_unref (enum_class);
return ev->value;
}
}
g_type_class_unref (enum_class);
value = g_ascii_strtoll (str, &endp, 10);
if (endp - str == len)
return value;
return -1;
}
static gboolean
is_valid_end_char (char c)
{
return c == ',' || c == '\n' || c == '\0';
}
/**
* pango2_attr_list_from_string:
* @text: a string
*
* Deserializes a `Pango2AttrList` from a string.
*
* This is the counterpart to [method@Pango2.AttrList.to_string].
* See that functions for details about the format.
*
* Returns: (transfer full) (nullable): a new `Pango2AttrList`
*/
Pango2AttrList *
pango2_attr_list_from_string (const char *text)
{
Pango2AttrList *list;
const char *p;
g_return_val_if_fail (text != NULL, NULL);
list = pango2_attr_list_new ();
if (*text == '\0')
return list;
list->attributes = g_ptr_array_new ();
p = text + strspn (text, " \t\n");
while (*p)
{
char *endp;
gint64 start_index;
gint64 end_index;
char *str;
Pango2AttrType attr_type;
Pango2Attribute *attr;
Pango2Language *lang;
gint64 integer;
Pango2FontDescription *desc;
Pango2Color color;
double num;
start_index = g_ascii_strtoll (p, &endp, 10);
if (*endp != ' ')
goto fail;
p = endp + strspn (endp, " ");
if (!*p)
goto fail;
end_index = g_ascii_strtoll (p, &endp, 10);
if (*endp != ' ')
goto fail;
p = endp + strspn (endp, " ");
endp = (char *)p + strcspn (p, " ");
attr_type = get_attr_type_by_nick (p, endp - p);
p = endp + strspn (endp, " ");
if (*p == '\0')
goto fail;
#define INT_ATTR(name,type) \
integer = g_ascii_strtoll (p, &endp, 10); \
if (!is_valid_end_char (*endp)) goto fail; \
attr = pango2_attr_##name##_new ((type)integer);
#define BOOLEAN_ATTR(name,type) \
if (strncmp (p, "true", strlen ("true")) == 0) \
{ \
integer = 1; \
endp = (char *)(p + strlen ("true")); \
} \
else if (strncmp (p, "false", strlen ("false")) == 0) \
{ \
integer = 0; \
endp = (char *)(p + strlen ("false")); \
} \
else \
integer = g_ascii_strtoll (p, &endp, 10); \
if (!is_valid_end_char (*endp)) goto fail; \
attr = pango2_attr_##name##_new ((type)integer);
#define ENUM_ATTR(name, type, min, max) \
endp = (char *)p + strcspn (p, ",\n"); \
integer = get_attr_value (attr_type, p, endp - p); \
attr = pango2_attr_##name##_new ((type) CLAMP (integer, min, max));
#define FLOAT_ATTR(name) \
num = g_ascii_strtod (p, &endp); \
if (!is_valid_end_char (*endp)) goto fail; \
attr = pango2_attr_##name##_new ((float)num);
#define COLOR_ATTR(name) \
endp = (char *)p + strcspn (p, ",\n"); \
if (!is_valid_end_char (*endp)) goto fail; \
str = g_strndup (p, endp - p); \
if (!pango2_color_parse (&color, str)) \
{ \
g_free (str); \
goto fail; \
} \
attr = pango2_attr_##name##_new (&color); \
g_free (str);
switch (attr_type)
{
case PANGO2_ATTR_INVALID:
pango2_attr_list_unref (list);
return NULL;
case PANGO2_ATTR_LANGUAGE:
endp = (char *)p + strcspn (p, ",\n");
if (!is_valid_end_char (*endp)) goto fail;
str = g_strndup (p, endp - p);
lang = pango2_language_from_string (str);
attr = pango2_attr_language_new (lang);
g_free (str);
break;
case PANGO2_ATTR_FAMILY:
p++;
endp = strchr (p, '"');
if (!endp) goto fail;
str = g_strndup (p, endp - p);
attr = pango2_attr_family_new (str);
g_free (str);
endp++;
if (!is_valid_end_char (*endp)) goto fail;
break;
case PANGO2_ATTR_STYLE:
ENUM_ATTR(style, Pango2Style, PANGO2_STYLE_NORMAL, PANGO2_STYLE_ITALIC);
break;
case PANGO2_ATTR_WEIGHT:
ENUM_ATTR(weight, Pango2Weight, PANGO2_WEIGHT_THIN, PANGO2_WEIGHT_ULTRAHEAVY);
break;
case PANGO2_ATTR_VARIANT:
ENUM_ATTR(variant, Pango2Variant, PANGO2_VARIANT_NORMAL, PANGO2_VARIANT_TITLE_CAPS);
break;
case PANGO2_ATTR_STRETCH:
ENUM_ATTR(stretch, Pango2Stretch, PANGO2_STRETCH_ULTRA_CONDENSED, PANGO2_STRETCH_ULTRA_EXPANDED);
break;
case PANGO2_ATTR_SIZE:
INT_ATTR(size, int);
break;
case PANGO2_ATTR_FONT_DESC:
p++;
endp = strchr (p, '"');
if (!endp) goto fail;
str = g_strndup (p, endp - p);
desc = pango2_font_description_from_string (str);
attr = pango2_attr_font_desc_new (desc);
pango2_font_description_free (desc);
g_free (str);
endp++;
if (!is_valid_end_char (*endp)) goto fail;
break;
case PANGO2_ATTR_FOREGROUND:
COLOR_ATTR(foreground);
break;
case PANGO2_ATTR_BACKGROUND:
COLOR_ATTR(background);
break;
case PANGO2_ATTR_UNDERLINE:
ENUM_ATTR(underline, Pango2LineStyle, PANGO2_LINE_STYLE_NONE, PANGO2_LINE_STYLE_WAVY);
break;
case PANGO2_ATTR_UNDERLINE_POSITION:
ENUM_ATTR(underline_position, Pango2UnderlinePosition, PANGO2_UNDERLINE_POSITION_NORMAL, PANGO2_UNDERLINE_POSITION_UNDER);
break;
case PANGO2_ATTR_STRIKETHROUGH:
ENUM_ATTR(strikethrough, Pango2LineStyle, PANGO2_LINE_STYLE_NONE, PANGO2_LINE_STYLE_WAVY);
break;
case PANGO2_ATTR_RISE:
INT_ATTR(rise, int);
break;
case PANGO2_ATTR_SCALE:
FLOAT_ATTR(scale);
break;
case PANGO2_ATTR_FALLBACK:
BOOLEAN_ATTR(fallback, gboolean);
break;
case PANGO2_ATTR_LETTER_SPACING:
INT_ATTR(letter_spacing, int);
break;
case PANGO2_ATTR_UNDERLINE_COLOR:
COLOR_ATTR(underline_color);
break;
case PANGO2_ATTR_STRIKETHROUGH_COLOR:
COLOR_ATTR(strikethrough_color);
break;
case PANGO2_ATTR_ABSOLUTE_SIZE:
integer = g_ascii_strtoll (p, &endp, 10);
if (!is_valid_end_char (*endp)) goto fail;
attr = pango2_attr_size_new_absolute (integer);
break;
case PANGO2_ATTR_GRAVITY:
ENUM_ATTR(gravity, Pango2Gravity, PANGO2_GRAVITY_SOUTH, PANGO2_GRAVITY_WEST);
break;
case PANGO2_ATTR_FONT_FEATURES:
p++;
endp = strchr (p, '"');
if (!endp) goto fail;
str = g_strndup (p, endp - p);
attr = pango2_attr_font_features_new (str);
g_free (str);
endp++;
if (!is_valid_end_char (*endp)) goto fail;
break;
case PANGO2_ATTR_GRAVITY_HINT:
ENUM_ATTR(gravity_hint, Pango2GravityHint, PANGO2_GRAVITY_HINT_NATURAL, PANGO2_GRAVITY_HINT_LINE);
break;
case PANGO2_ATTR_ALLOW_BREAKS:
BOOLEAN_ATTR(allow_breaks, gboolean);
break;
case PANGO2_ATTR_SHOW:
INT_ATTR(show, Pango2ShowFlags);
break;
case PANGO2_ATTR_INSERT_HYPHENS:
BOOLEAN_ATTR(insert_hyphens, gboolean);
break;
case PANGO2_ATTR_OVERLINE:
ENUM_ATTR(overline, Pango2LineStyle, PANGO2_LINE_STYLE_NONE, PANGO2_LINE_STYLE_WAVY);
break;
case PANGO2_ATTR_OVERLINE_COLOR:
COLOR_ATTR(overline_color);
break;
case PANGO2_ATTR_LINE_HEIGHT:
FLOAT_ATTR(line_height);
break;
case PANGO2_ATTR_ABSOLUTE_LINE_HEIGHT:
integer = g_ascii_strtoll (p, &endp, 10);
if (!is_valid_end_char (*endp)) goto fail;
attr = pango2_attr_line_height_new_absolute (integer);
break;
case PANGO2_ATTR_TEXT_TRANSFORM:
ENUM_ATTR(text_transform, Pango2TextTransform, PANGO2_TEXT_TRANSFORM_NONE, PANGO2_TEXT_TRANSFORM_CAPITALIZE);
break;
case PANGO2_ATTR_WORD:
integer = g_ascii_strtoll (p, &endp, 10);
if (!is_valid_end_char (*endp)) goto fail;
attr = pango2_attr_word_new ();
break;
case PANGO2_ATTR_SENTENCE:
integer = g_ascii_strtoll (p, &endp, 10);
if (!is_valid_end_char (*endp)) goto fail;
attr = pango2_attr_sentence_new ();
break;
case PANGO2_ATTR_PARAGRAPH:
integer = g_ascii_strtoll (p, &endp, 10);
if (!is_valid_end_char (*endp)) goto fail;
attr = pango2_attr_paragraph_new ();
break;
case PANGO2_ATTR_BASELINE_SHIFT:
ENUM_ATTR(baseline_shift, Pango2BaselineShift, 0, G_MAXINT);
break;
case PANGO2_ATTR_FONT_SCALE:
ENUM_ATTR(font_scale, Pango2FontScale, PANGO2_FONT_SCALE_NONE, PANGO2_FONT_SCALE_SMALL_CAPS);
break;
case PANGO2_ATTR_LINE_SPACING:
INT_ATTR(line_spacing, int);
break;
case PANGO2_ATTR_SHAPE:
default:
g_assert_not_reached ();
}
attr->start_index = (guint)start_index;
attr->end_index = (guint)end_index;
g_ptr_array_add (list->attributes, attr);
p = endp;
if (*p)
{
if (*p == ',')
p++;
p += strspn (p, " \n");
}
}
goto success;
fail:
pango2_attr_list_unref (list);
list = NULL;
success:
return list;
}
/* }}} */
/* vim:set foldmethod=marker expandtab: */
|