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
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
|
/* Pango
* pango-markup.c: Parse markup into attributed text
*
* Copyright (C) 2000 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 <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pango/pango-attributes.h>
#include <pango/pango-font.h>
#include <pango/pango-utils.h>
/* FIXME */
#define _(x) x
/* CSS size levels */
typedef enum
{
XXSmall = -3,
XSmall = -2,
Small = -1,
Medium = 0,
Large = 1,
XLarge = 2,
XXLarge = 3
} SizeLevel;
typedef struct _MarkupData MarkupData;
struct _MarkupData
{
PangoAttrList *attr_list;
GString *text;
GSList *tag_stack;
gsize index;
GSList *to_apply;
gunichar accel_marker;
gunichar accel_char;
};
typedef struct _OpenTag OpenTag;
struct _OpenTag
{
GSList *attrs;
gsize start_index;
/* Current total scale level; reset whenever
* an absolute size is set.
* Each "larger" ups it 1, each "smaller" decrements it 1
*/
gint scale_level;
/* Our impact on scale_level, so we know whether we
* need to create an attribute ourselves on close
*/
gint scale_level_delta;
/* Base scale factor currently in effect
* or size that this tag
* forces, or parent's scale factor or size.
*/
double base_scale_factor;
int base_font_size;
guint has_base_font_size : 1;
};
typedef gboolean (*TagParseFunc) (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean b_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean big_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean span_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean i_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean markup_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean s_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean sub_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean sup_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean small_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean tt_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static gboolean u_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error);
static double
scale_factor (int scale_level, double base)
{
double factor = base;
int i;
/* 1.2 is the CSS scale factor between sizes */
if (scale_level > 0)
{
i = 0;
while (i < scale_level)
{
factor *= 1.2;
++i;
}
}
else if (scale_level < 0)
{
i = scale_level;
while (i < 0)
{
factor /= 1.2;
++i;
}
}
return factor;
}
static void
open_tag_free (OpenTag *ot)
{
g_slist_foreach (ot->attrs, (GFunc) pango_attribute_destroy, NULL);
g_slist_free (ot->attrs);
g_free (ot);
}
static void
open_tag_set_absolute_font_size (OpenTag *ot,
int font_size)
{
ot->base_font_size = font_size;
ot->has_base_font_size = TRUE;
ot->scale_level = 0;
ot->scale_level_delta = 0;
}
static void
open_tag_set_absolute_font_scale (OpenTag *ot,
double scale)
{
ot->base_scale_factor = scale;
ot->has_base_font_size = FALSE;
ot->scale_level = 0;
ot->scale_level_delta = 0;
}
static OpenTag*
markup_data_open_tag (MarkupData *md)
{
OpenTag *ot;
OpenTag *parent = NULL;
if (md->attr_list == NULL)
return NULL;
if (md->tag_stack)
parent = md->tag_stack->data;
ot = g_new (OpenTag, 1);
ot->attrs = NULL;
ot->start_index = md->index;
ot->scale_level_delta = 0;
if (parent == NULL)
{
ot->base_scale_factor = 1.0;
ot->base_font_size = 0;
ot->has_base_font_size = FALSE;
ot->scale_level = 0;
}
else
{
ot->base_scale_factor = parent->base_scale_factor;
ot->base_font_size = parent->base_font_size;
ot->has_base_font_size = parent->has_base_font_size;
ot->scale_level = parent->scale_level;
}
md->tag_stack = g_slist_prepend (md->tag_stack, ot);
return ot;
}
static void
markup_data_close_tag (MarkupData *md)
{
OpenTag *ot;
GSList *tmp_list;
if (md->attr_list == NULL)
return;
/* pop the stack */
ot = md->tag_stack->data;
md->tag_stack = g_slist_delete_link (md->tag_stack,
md->tag_stack);
/* Adjust end indexes, and push each attr onto the front of the
* to_apply list. This means that outermost tags are on the front of
* that list; if we apply the list in order, then the innermost
* tags will "win" which is correct.
*/
tmp_list = ot->attrs;
while (tmp_list != NULL)
{
PangoAttribute *a = tmp_list->data;
a->start_index = ot->start_index;
a->end_index = md->index;
md->to_apply = g_slist_prepend (md->to_apply, a);
tmp_list = g_slist_next (tmp_list);
}
if (ot->scale_level_delta != 0)
{
/* We affected relative font size; create an appropriate
* attribute and reverse our effects on the current level
*/
PangoAttribute *a;
if (ot->has_base_font_size)
{
/* Create a font using the absolute point size
* as the base size to be scaled from
*/
a = pango_attr_size_new (scale_factor (ot->scale_level,
1.0) *
ot->base_font_size);
}
else
{
/* Create a font using the current scale factor
* as the base size to be scaled from
*/
a = pango_attr_scale_new (scale_factor (ot->scale_level,
ot->base_scale_factor));
}
a->start_index = ot->start_index;
a->end_index = md->index;
md->to_apply = g_slist_prepend (md->to_apply, a);
}
g_slist_free (ot->attrs);
g_free (ot);
}
static void
start_element_handler (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
TagParseFunc parse_func = NULL;
OpenTag *ot;
switch (*element_name)
{
case 'b':
if (strcmp ("b", element_name) == 0)
parse_func = b_parse_func;
else if (strcmp ("big", element_name) == 0)
parse_func = big_parse_func;
break;
case 'i':
if (strcmp ("i", element_name) == 0)
parse_func = i_parse_func;
break;
case 'm':
if (strcmp ("markup", element_name) == 0)
parse_func = markup_parse_func;
break;
case 's':
if (strcmp ("span", element_name) == 0)
parse_func = span_parse_func;
else if (strcmp ("s", element_name) == 0)
parse_func = s_parse_func;
else if (strcmp ("sub", element_name) == 0)
parse_func = sub_parse_func;
else if (strcmp ("sup", element_name) == 0)
parse_func = sup_parse_func;
else if (strcmp ("small", element_name) == 0)
parse_func = small_parse_func;
break;
case 't':
if (strcmp ("tt", element_name) == 0)
parse_func = tt_parse_func;
break;
case 'u':
if (strcmp ("u", element_name) == 0)
parse_func = u_parse_func;
break;
}
if (parse_func == NULL)
{
gint line_number, char_number;
g_markup_parse_context_get_position (context,
&line_number, &char_number);
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_UNKNOWN_ELEMENT,
_("Unknown tag '%s' on line %d char %d"),
element_name,
line_number, char_number);
return;
}
ot = markup_data_open_tag (user_data);
/* note ot may be NULL if the user didn't want the attribute list */
if (!(*parse_func) (user_data, ot,
attribute_names, attribute_values,
context, error))
{
/* there's nothing to do; we return an error, and end up
* freeing ot off the tag stack later.
*/
}
}
static void
end_element_handler (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
markup_data_close_tag (user_data);
}
static void
text_handler (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
MarkupData *md = user_data;
if (md->accel_marker == 0)
{
/* Just append all the text */
md->index += text_len;
g_string_append_len (md->text, text, text_len);
}
else
{
/* Parse the accelerator */
const gchar *p;
const gchar *end;
const gchar *range_start;
const gchar *range_end;
gboolean just_saw_marker;
gssize uline_index = -1;
gsize uline_len = 0; /* Quiet GCC */
range_end = NULL;
range_start = text;
p = text;
end = text + text_len;
just_saw_marker = FALSE;
while (p != end)
{
gunichar c;
c = g_utf8_get_char (p);
if (range_end)
{
if (c == md->accel_marker)
{
/* escaped accel marker; move range_end
* past the accel marker that came before,
* append the whole thing
*/
range_end = g_utf8_next_char (range_end);
g_string_append_len (md->text,
range_start,
range_end - range_start);
md->index += range_end - range_start;
/* set next range_start, skipping accel marker */
range_start = g_utf8_next_char (p);
}
else
{
/* Don't append the accel marker (leave range_end
* alone); set the accel char to c; record location for
* underline attribute
*/
if (md->accel_char == 0)
md->accel_char = c;
g_string_append_len (md->text,
range_start,
range_end - range_start);
md->index += range_end - range_start;
/* The underline should go underneath the char
* we're setting as the next range_start
*/
uline_index = md->index;
uline_len = g_utf8_next_char (p) - p;
/* set next range_start to include this char */
range_start = p;
}
/* reset range_end */
range_end = NULL;
}
else if (c == md->accel_marker)
{
range_end = p;
}
p = g_utf8_next_char (p);
}
if (range_end)
{
g_string_append_len (md->text,
range_start,
range_end - range_start);
md->index += range_end - range_start;
}
else
{
g_string_append_len (md->text,
range_start,
end - range_start);
md->index += end - range_start;
}
if (md->attr_list != NULL && uline_index >= 0)
{
/* Add the underline indicating the accelerator */
PangoAttribute *attr;
attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
attr->start_index = uline_index;
attr->end_index = uline_index + uline_len;
pango_attr_list_change (md->attr_list, attr);
}
}
}
static gboolean
xml_isspace (char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
static const GMarkupParser pango_markup_parser = {
start_element_handler,
end_element_handler,
text_handler,
NULL,
NULL
};
/**
* pango_parse_markup:
* @markup_text: markup to parse (see <link linkend="PangoMarkupFormat">markup format</link>)
* @length: length of @markup_text, or -1 if nul-terminated
* @accel_marker: character that precedes an accelerator, or 0 for none
* @attr_list: address of return location for a #PangoAttrList, or %NULL
* @text: address of return location for text with tags stripped, or %NULL
* @accel_char: address of return location for accelerator char, or %NULL
* @error: address of return location for errors, or %NULL
*
*
* Parses marked-up text (see
* <link linkend="PangoMarkupFormat">markup format</link>) to create
* a plaintext string and an attribute list.
*
* If @accel_marker is nonzero, the given character will mark the
* character following it as an accelerator. For example, the accel
* marker might be an ampersand or underscore. All characters marked
* as an accelerator will receive a %PANGO_UNDERLINE_LOW attribute,
* and the first character so marked will be returned in @accel_char.
* Two @accel_marker characters following each other produce a single
* literal @accel_marker character.
*
* Return value: %FALSE if @error is set, otherwise %TRUE
**/
gboolean
pango_parse_markup (const char *markup_text,
int length,
gunichar accel_marker,
PangoAttrList **attr_list,
char **text,
gunichar *accel_char,
GError **error)
{
GMarkupParseContext *context = NULL;
MarkupData *md = NULL;
gboolean needs_root = TRUE;
GSList *tmp_list;
const char *p;
const char *end;
g_return_val_if_fail (markup_text != NULL, FALSE);
md = g_new (MarkupData, 1);
/* Don't bother creating these if they weren't requested;
* might be useful e.g. if you just want to validate
* some markup.
*/
if (attr_list)
md->attr_list = pango_attr_list_new ();
else
md->attr_list = NULL;
md->text = g_string_new (NULL);
if (accel_char)
*accel_char = 0;
md->accel_marker = accel_marker;
md->accel_char = 0;
md->index = 0;
md->tag_stack = NULL;
md->to_apply = NULL;
context = g_markup_parse_context_new (&pango_markup_parser,
0, md, NULL);
if (length < 0)
length = strlen (markup_text);
p = markup_text;
end = markup_text + length;
while (p != end && xml_isspace (*p))
++p;
if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
needs_root = FALSE;
if (needs_root)
if (!g_markup_parse_context_parse (context,
"<markup>",
-1,
error))
goto error;
if (!g_markup_parse_context_parse (context,
markup_text,
length,
error))
goto error;
if (needs_root)
if (!g_markup_parse_context_parse (context,
"</markup>",
-1,
error))
goto error;
if (!g_markup_parse_context_end_parse (context, error))
goto error;
g_markup_parse_context_free (context);
if (md->attr_list)
{
/* The apply list has the most-recently-closed tags first;
* we want to apply the least-recently-closed tag last.
*/
tmp_list = md->to_apply;
while (tmp_list != NULL)
{
PangoAttribute *attr = tmp_list->data;
/* Innermost tags before outermost */
pango_attr_list_change (md->attr_list, attr);
tmp_list = g_slist_next (tmp_list);
}
g_slist_free (md->to_apply);
md->to_apply = NULL;
}
if (attr_list)
*attr_list = md->attr_list;
if (text)
*text = g_string_free (md->text, FALSE);
else
g_string_free (md->text, TRUE);
if (accel_char)
*accel_char = md->accel_char;
g_assert (md->tag_stack == NULL);
g_free (md);
return TRUE;
error:
g_slist_foreach (md->tag_stack, (GFunc) open_tag_free, NULL);
g_slist_free (md->tag_stack);
g_slist_foreach (md->to_apply, (GFunc) pango_attribute_destroy, NULL);
g_slist_free (md->to_apply);
g_string_free (md->text, TRUE);
if (md->attr_list)
pango_attr_list_unref (md->attr_list);
g_free (md);
if (context)
g_markup_parse_context_free (context);
return FALSE;
}
static void
set_bad_attribute (GError **error,
GMarkupParseContext *context,
const char *element_name,
const char *attribute_name)
{
gint line_number, char_number;
g_markup_parse_context_get_position (context,
&line_number, &char_number);
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
_("Tag '%s' does not support attribute '%s' on line %d char %d"),
element_name,
attribute_name,
line_number, char_number);
}
static void
add_attribute (OpenTag *ot,
PangoAttribute *attr)
{
if (ot == NULL)
pango_attribute_destroy (attr);
else
ot->attrs = g_slist_prepend (ot->attrs, attr);
}
#define CHECK_NO_ATTRS(elem) G_STMT_START { \
if (*names != NULL) { \
set_bad_attribute (error, context, (elem), *names); \
return FALSE; \
} }G_STMT_END
static gboolean
b_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("b");
add_attribute (tag, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
return TRUE;
}
static gboolean
big_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("big");
/* Grow text one level */
if (tag)
{
tag->scale_level_delta += 1;
tag->scale_level += 1;
}
return TRUE;
}
static gboolean
parse_absolute_size (OpenTag *tag,
const char *size)
{
SizeLevel level = Medium;
double factor;
if (strcmp (size, "xx-small") == 0)
{
level = XXSmall;
}
else if (strcmp (size, "x-small") == 0)
{
level = XSmall;
}
else if (strcmp (size, "small") == 0)
{
level = Small;
}
else if (strcmp (size, "medium") == 0)
{
level = Medium;
}
else if (strcmp (size, "large") == 0)
{
level = Large;
}
else if (strcmp (size, "x-large") == 0)
{
level = XLarge;
}
else if (strcmp (size, "xx-large") == 0)
{
level = XXLarge;
}
else
return FALSE;
/* This is "absolute" in that it's relative to the base font,
* but not to sizes created by any other tags
*/
factor = scale_factor (level, 1.0);
add_attribute (tag, pango_attr_scale_new (factor));
if (tag)
open_tag_set_absolute_font_scale (tag, factor);
return TRUE;
}
#define CHECK_DUPLICATE(var) G_STMT_START{ \
if ((var) != NULL) { \
g_set_error (error, G_MARKUP_ERROR, \
G_MARKUP_ERROR_INVALID_CONTENT, \
_("Attribute '%s' occurs twice on <span> tag " \
"on line %d char %d, may only occur once"), \
names[i], line_number, char_number); \
return FALSE; \
}}G_STMT_END
static gboolean
span_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
int line_number, char_number;
int i;
const char *family = NULL;
const char *size = NULL;
const char *style = NULL;
const char *weight = NULL;
const char *variant = NULL;
const char *stretch = NULL;
const char *desc = NULL;
const char *foreground = NULL;
const char *background = NULL;
const char *underline = NULL;
const char *underline_color = NULL;
const char *strikethrough = NULL;
const char *strikethrough_color = NULL;
const char *rise = NULL;
const char *letter_spacing = NULL;
const char *lang = NULL;
const char *fallback = NULL;
g_markup_parse_context_get_position (context,
&line_number, &char_number);
i = 0;
while (names[i])
{
if (strcmp (names[i], "font_family") == 0 ||
strcmp (names[i], "face") == 0)
{
CHECK_DUPLICATE (family);
family = values[i];
}
else if (strcmp (names[i], "size") == 0)
{
CHECK_DUPLICATE (size);
size = values[i];
}
else if (strcmp (names[i], "style") == 0)
{
CHECK_DUPLICATE (style);
style = values[i];
}
else if (strcmp (names[i], "weight") == 0)
{
CHECK_DUPLICATE (weight);
weight = values[i];
}
else if (strcmp (names[i], "variant") == 0)
{
CHECK_DUPLICATE (variant);
variant = values[i];
}
else if (strcmp (names[i], "stretch") == 0)
{
CHECK_DUPLICATE (stretch);
stretch = values[i];
}
else if (strcmp (names[i], "font_desc") == 0)
{
CHECK_DUPLICATE (desc);
desc = values[i];
}
else if (strcmp (names[i], "foreground") == 0 ||
strcmp (names[i], "color") == 0)
{
CHECK_DUPLICATE (foreground);
foreground = values[i];
}
else if (strcmp (names[i], "background") == 0)
{
CHECK_DUPLICATE (background);
background = values[i];
}
else if (strcmp (names[i], "underline") == 0)
{
CHECK_DUPLICATE (underline);
underline = values[i];
}
else if (strcmp (names[i], "underline_color") == 0)
{
CHECK_DUPLICATE (underline_color);
underline_color = values[i];
}
else if (strcmp (names[i], "strikethrough") == 0)
{
CHECK_DUPLICATE (strikethrough);
strikethrough = values[i];
}
else if (strcmp (names[i], "strikethrough_color") == 0)
{
CHECK_DUPLICATE (strikethrough_color);
strikethrough_color = values[i];
}
else if (strcmp (names[i], "rise") == 0)
{
CHECK_DUPLICATE (rise);
rise = values[i];
}
else if (strcmp (names[i], "letter_spacing") == 0)
{
CHECK_DUPLICATE (letter_spacing);
letter_spacing = values[i];
}
else if (strcmp (names[i], "lang") == 0)
{
CHECK_DUPLICATE (lang);
lang = values[i];
}
else if (strcmp (names[i], "fallback") == 0)
{
CHECK_DUPLICATE (fallback);
fallback = values[i];
}
else
{
g_set_error (error, G_MARKUP_ERROR,
G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
_("Attribute '%s' is not allowed on the <span> tag "
"on line %d char %d"),
names[i], line_number, char_number);
return FALSE;
}
++i;
}
/* Parse desc first, then modify it with other font-related attributes. */
if (desc)
{
PangoFontDescription *parsed;
parsed = pango_font_description_from_string (desc);
if (parsed)
{
add_attribute (tag, pango_attr_font_desc_new (parsed));
if (tag)
open_tag_set_absolute_font_size (tag, pango_font_description_get_size (parsed));
pango_font_description_free (parsed);
}
}
if (family)
{
add_attribute (tag, pango_attr_family_new (family));
}
if (size)
{
if (g_ascii_isdigit (*size))
{
char *end = NULL;
gulong n;
n = strtoul (size, &end, 10);
if (*end != '\0')
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Value of 'size' attribute on <span> tag on line %d"
"could not be parsed; should be an integer, or a "
"string such as 'small', not '%s'"),
line_number, size);
goto error;
}
add_attribute (tag, pango_attr_size_new (n));
if (tag)
open_tag_set_absolute_font_size (tag, n);
}
else if (strcmp (size, "smaller") == 0)
{
if (tag)
{
tag->scale_level_delta -= 1;
tag->scale_level -= 1;
}
}
else if (strcmp (size, "larger") == 0)
{
if (tag)
{
tag->scale_level_delta += 1;
tag->scale_level += 1;
}
}
else if (parse_absolute_size (tag, size))
; /* nothing */
else
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Value of 'size' attribute on <span> tag on line %d"
"could not be parsed; should be an integer, or a "
"string such as 'small', not '%s'"),
line_number, size);
goto error;
}
}
if (style)
{
PangoStyle pango_style;
if (pango_parse_style (style, &pango_style, FALSE))
add_attribute (tag, pango_attr_style_new (pango_style));
else
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("'%s' is not a valid value for the 'style' attribute "
"on <span> tag, line %d; valid values are "
"'normal', 'oblique', 'italic'"),
style, line_number);
goto error;
}
}
if (weight)
{
PangoWeight pango_weight;
if (pango_parse_weight (weight, &pango_weight, FALSE))
add_attribute (tag,
pango_attr_weight_new (pango_weight));
else
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("'%s' is not a valid value for the 'weight' "
"attribute on <span> tag, line %d; valid "
"values are for example 'light', 'ultrabold' or a number"),
weight, line_number);
goto error;
}
}
if (variant)
{
PangoVariant pango_variant;
if (pango_parse_variant (variant, &pango_variant, FALSE))
add_attribute (tag, pango_attr_variant_new (pango_variant));
else
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("'%s' is not a valid value for the 'variant' "
"attribute on <span> tag, line %d; valid values are "
"'normal', 'smallcaps'"),
variant, line_number);
goto error;
}
}
if (stretch)
{
PangoStretch pango_stretch;
if (pango_parse_stretch (stretch, &pango_stretch, FALSE))
add_attribute (tag, pango_attr_stretch_new (pango_stretch));
else
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("'%s' is not a valid value for the 'stretch' "
"attribute on <span> tag, line %d; valid "
"values are for example 'condensed', "
"'ultraexpanded', 'normal'"),
stretch, line_number);
goto error;
}
}
if (foreground)
{
PangoColor color;
if (!pango_color_parse (&color, foreground))
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Could not parse foreground color specification "
"'%s' on line %d"),
foreground, line_number);
goto error;
}
add_attribute (tag, pango_attr_foreground_new (color.red, color.green, color.blue));
}
if (background)
{
PangoColor color;
if (!pango_color_parse (&color, background))
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Could not parse background color specification "
"'%s' on line %d"),
background, line_number);
goto error;
}
add_attribute (tag, pango_attr_background_new (color.red, color.green, color.blue));
}
if (underline)
{
PangoUnderline ul = PANGO_UNDERLINE_NONE;
if (strcmp (underline, "single") == 0)
ul = PANGO_UNDERLINE_SINGLE;
else if (strcmp (underline, "double") == 0)
ul = PANGO_UNDERLINE_DOUBLE;
else if (strcmp (underline, "low") == 0)
ul = PANGO_UNDERLINE_LOW;
else if (strcmp (underline, "error") == 0)
ul = PANGO_UNDERLINE_ERROR;
else if (strcmp (underline, "none") == 0)
ul = PANGO_UNDERLINE_NONE;
else
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("'%s' is not a valid value for the 'underline' "
"attribute on <span> tag, line %d; valid "
"values are for example 'single', "
"'double', 'low', 'none'"),
underline, line_number);
goto error;
}
add_attribute (tag, pango_attr_underline_new (ul));
}
if (underline_color)
{
PangoColor color;
if (!pango_color_parse (&color, underline_color))
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Could not parse underline_color color specification "
"'%s' on line %d"),
underline_color, line_number);
goto error;
}
add_attribute (tag, pango_attr_underline_color_new (color.red, color.green, color.blue));
}
if (strikethrough)
{
if (strcmp (strikethrough, "true") == 0)
add_attribute (tag, pango_attr_strikethrough_new (TRUE));
else if (strcmp (strikethrough, "false") == 0)
add_attribute (tag, pango_attr_strikethrough_new (FALSE));
else
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("'strikethrough' attribute on <span> tag "
"line %d should have one of the values "
"'true' or 'false': '%s' is not valid"),
line_number, strikethrough);
goto error;
}
}
if (strikethrough_color)
{
PangoColor color;
if (!pango_color_parse (&color, strikethrough_color))
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Could not parse strikethrough_color color specification "
"'%s' on line %d"),
strikethrough_color, line_number);
goto error;
}
add_attribute (tag, pango_attr_strikethrough_color_new (color.red, color.green, color.blue));
}
if (fallback)
{
if (strcmp (fallback, "true") == 0)
add_attribute (tag, pango_attr_fallback_new (TRUE));
else if (strcmp (fallback, "false") == 0)
add_attribute (tag, pango_attr_fallback_new (FALSE));
else
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("'fallback' attribute on <span> tag "
"line %d should have one of the values "
"'true' or 'false': '%s' is not valid"),
line_number, fallback);
goto error;
}
}
if (rise)
{
char *end = NULL;
glong n;
n = strtol (rise, &end, 10);
if (*end != '\0')
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Value of 'rise' attribute on <span> tag "
"on line %d could not be parsed; "
"should be an integer, not '%s'"),
line_number, rise);
goto error;
}
add_attribute (tag, pango_attr_rise_new (n));
}
if (letter_spacing)
{
char *end = NULL;
glong n;
n = strtol (letter_spacing, &end, 10);
if (*end != '\0')
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Value of 'letter_spacing' attribute on <span> tag "
"on line %d could not be parsed; "
"should be an integer, not '%s'"),
line_number, letter_spacing);
goto error;
}
add_attribute (tag, pango_attr_letter_spacing_new (n));
}
if (lang)
{
add_attribute (tag,
pango_attr_language_new (pango_language_from_string (lang)));
}
return TRUE;
error:
return FALSE;
}
static gboolean
i_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("i");
add_attribute (tag, pango_attr_style_new (PANGO_STYLE_ITALIC));
return TRUE;
}
static gboolean
markup_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
/* We don't do anything with this tag at the moment. */
return TRUE;
}
static gboolean
s_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("s");
add_attribute (tag, pango_attr_strikethrough_new (TRUE));
return TRUE;
}
#define SUPERSUB_RISE 5000
static gboolean
sub_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("sub");
/* Shrink font, and set a negative rise */
if (tag)
{
tag->scale_level_delta -= 1;
tag->scale_level -= 1;
}
add_attribute (tag, pango_attr_rise_new (-SUPERSUB_RISE));
return TRUE;
}
static gboolean
sup_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("sup");
/* Shrink font, and set a positive rise */
if (tag)
{
tag->scale_level_delta -= 1;
tag->scale_level -= 1;
}
add_attribute (tag, pango_attr_rise_new (SUPERSUB_RISE));
return TRUE;
}
static gboolean
small_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("small");
/* Shrink text one level */
if (tag)
{
tag->scale_level_delta -= 1;
tag->scale_level -= 1;
}
return TRUE;
}
static gboolean
tt_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("tt");
add_attribute (tag, pango_attr_family_new ("Monospace"));
return TRUE;
}
static gboolean
u_parse_func (MarkupData *md,
OpenTag *tag,
const gchar **names,
const gchar **values,
GMarkupParseContext *context,
GError **error)
{
CHECK_NO_ATTRS("u");
add_attribute (tag, pango_attr_underline_new (PANGO_UNDERLINE_SINGLE));
return TRUE;
}
|