File: exp-sub.c

package info (click to toggle)
zapping 0.10~cvs6-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,856 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,812; xml: 2,742; makefile: 1,283; perl: 488
file content (1668 lines) | stat: -rw-r--r-- 34,133 bytes parent folder | download | duplicates (6)
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
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
/*
 *  libzvbi - Closed Caption and Teletext
 *            caption / subtitle export functions
 *
 *  Copyright (C) 2004 Michael H. Schimek
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: exp-sub.c,v 1.2 2005/10/14 23:40:13 mschimek Exp $ */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>		/* malloc() */
#include <math.h>		/* floor() */
#include <setjmp.h>
#include "page.h"		/* vbi3_page */
#include "conv.h"
#include "misc.h"
#include "lang.h"		/* vbi3_character_set, ... */
#ifdef ZAPPING8
#  include "common/intl-priv.h"
#else
#  include "intl-priv.h"
#endif
#include "export-priv.h"	/* vbi3_export */

/*
    Resources:

    MPSub format
    http://www.mplayerhq.hu/DOCS/HTML/en/subosd.html

    QuickTime Text
    http://www.apple.com/quicktime/tools_tips/tutorials/texttracks.html
    http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap4/
	chapter_5_section_2.html

    RealText
    http://service.real.com/help/library/encoders.html

    Understanding SAMI 1.0
    http://msdn.microsoft.com/library/en-us/dnacc/html/atg_samiarticle.asp

    Adding Closed Captions to Digital Media
    http://msdn.microsoft.com/library/en-us/dnwmt/html/wmp7_sami.asp

    SubRip
    http://membres.lycos.fr/subrip/

    SubViewer
    http://www.subviewer.com/
*/

typedef struct {
	uint16_t *		buffer;
        uint16_t *		bp;
	uint16_t *		end;
} vec;

typedef enum {
	FORMAT_MPSUB,
	FORMAT_QTTEXT,
	FORMAT_REALTEXT,
	FORMAT_SAMI,
	FORMAT_SUBRIP,
	FORMAT_SUBVIEWER,
} format;

typedef struct sub_instance {
	vbi3_export		export;

	jmp_buf			main;

	vec			text1;
	vec			text2;
	iconv_t			cd;

	format			format;
	int			encoding;
	char *			charset;
	char *			font;

	vbi3_bool		have_header;

	vbi3_pgno		last_pgno;

	double			last_timestamp;

	double			delay_time;

	unsigned int		n_pages;
	unsigned int		blank_pages;

	vbi3_char		para_ac;
	vbi3_char		last_ac;

	unsigned int		last_just;

	vbi3_bool		in_span;
	vbi3_bool		in_underline;
	vbi3_bool		in_bold;
	vbi3_bool		in_italic;
} sub_instance;

static const char *
user_encodings [] = {
	N_("ASCII"),
	N_("ISO-8859-1 (Latin-1 Western languages)"),
	N_("ISO-8859-2 (Latin-2 Central and Eastern European languages)"),
	N_("ISO-8859-4 (Latin-3 Baltic languages)"),
	N_("ISO-8859-5 (Cyrillic)"),
	N_("ISO-8859-7 (Greek)"),
	N_("ISO-8859-8 (Hebrew)"),
	N_("ISO-8859-9 (Turkish)"),
	N_("KOI8-R (Russian and Bulgarian)"),
	N_("KOI8-U (Ukranian)"),
	N_("ISO-10646/UTF-8 (Unicode)"),
};

static const char *
iconv_encodings [] = {
	"ASCII",
	"ISO-8859-1",
	"ISO-8859-2",
	"ISO-8859-4",
	"ISO-8859-5",
	"ISO-8859-7",
	"ISO-8859-8",
	"ISO-8859-9",
        "KOI8-R",
	"KOI8-U",
	"UTF-8"
};

static const vbi3_option_info
option_info1 [] = {
	_VBI3_OPTION_MENU_INITIALIZER
	("format", N_("Encoding"),
	 1, user_encodings, N_ELEMENTS (user_encodings), NULL),
        /* one for users, another for programs */
	_VBI3_OPTION_STRING_INITIALIZER
	("charset", NULL, "ISO-8859-1", NULL),
};

static const vbi3_option_info
option_info2 [] = {
	_VBI3_OPTION_MENU_INITIALIZER
	("format", N_("Encoding"),
	 0, user_encodings, N_ELEMENTS (user_encodings), NULL),
        /* one for users, another for programs */
	_VBI3_OPTION_STRING_INITIALIZER
	("charset", NULL, "ISO-8859-1", NULL),
	_VBI3_OPTION_STRING_INITIALIZER
	("font", N_("Font face"), "Tahoma", NULL),
};

#undef KEYWORD
#define KEYWORD(str) (0 == strcmp (em->export_info->keyword, str))

static vbi3_export *
sub_new			(const _vbi3_export_module *em)
{
	sub_instance *sub;

	assert (sizeof (user_encodings) == sizeof (iconv_encodings));

	if (!(sub = vbi3_malloc (sizeof (*sub))))
		return NULL;

	CLEAR (*sub);

	if (KEYWORD ("mpsub")) {
		sub->format = FORMAT_MPSUB;
	} else if (KEYWORD ("qttext")) {
		sub->format = FORMAT_QTTEXT;
	} else if (KEYWORD ("realtext")) {
		sub->format = FORMAT_REALTEXT;
	} else if (KEYWORD ("sami")) {
		sub->format = FORMAT_SAMI;
	} else if (KEYWORD ("subrip")) {
		sub->format = FORMAT_SUBRIP;
	} else if (KEYWORD ("subviewer")) {
		sub->format = FORMAT_SUBVIEWER;
	} else {
		assert (!"reached");
	}

	sub->cd = (iconv_t) -1;

	return &sub->export;
}

static void
sub_delete			(vbi3_export *		e)
{
	sub_instance *sub = PARENT (e, sub_instance, export);

	vbi3_free (sub->text1.buffer);
	vbi3_free (sub->text2.buffer);

	vbi3_free (sub->charset);
	vbi3_free (sub->font);

	if ((iconv_t) -1 == sub->cd)
		vbi3_iconv_ucs2_close (sub->cd);

	vbi3_free (sub);
}

#undef KEYWORD
#define KEYWORD(str) (0 == strcmp (keyword, str))

static vbi3_bool
option_get			(vbi3_export *		e,
				 const char *		keyword,
				 vbi3_option_value *	value)
{
	sub_instance *sub = PARENT (e, sub_instance, export);

	if (KEYWORD ("format") || KEYWORD ("encoding")) {
		value->num = sub->encoding;
	} else if (KEYWORD ("charset")) {
		value->str = _vbi3_export_strdup (e, NULL, sub->charset);
		if (!value->str)
			return FALSE;
	} else if (KEYWORD ("font")) {
		value->str = _vbi3_export_strdup (e, NULL, sub->font);
		if (!value->str)
			return FALSE;
	} else {
		_vbi3_export_unknown_option (e, keyword);
		return FALSE;
	}

	return TRUE; /* success */
}

static vbi3_bool
option_set			(vbi3_export *		e,
				 const char *		keyword,
				 va_list		ap)
{
	sub_instance *sub = PARENT (e, sub_instance, export);

	if (KEYWORD ("format") || KEYWORD ("encoding")) {
		unsigned int encoding = va_arg (ap, unsigned int);

		if (encoding >= N_ELEMENTS (user_encodings)) {
			_vbi3_export_invalid_option (e, keyword, encoding);
			return FALSE;
		}

		if (!_vbi3_export_strdup (e, &sub->charset,
					 iconv_encodings[encoding]))
			return FALSE;

		sub->encoding = encoding;
	} else if (KEYWORD ("charset")) {
		const char *string = va_arg (ap, const char *);

		if (!string) {
			_vbi3_export_invalid_option (e, keyword, string);
			return FALSE;
		}

		if (!_vbi3_export_strdup (e, &sub->charset, string))
			return FALSE;
	} else if (KEYWORD ("font")) {
		const char *string = va_arg (ap, const char *);

		if (!string) {
			_vbi3_export_invalid_option (e, keyword, string);
			return FALSE;
		}

		if (!_vbi3_export_strdup (e, &sub->font, string))
			return FALSE;
	} else {
		_vbi3_export_unknown_option (e, keyword);
		return FALSE;
	}

	return TRUE;
}

/* We write UCS-2 into an automatically growing buffer. */
static void
extend				(sub_instance *	sub,
				 vec *			v)
{
	uint16_t *buffer;
	unsigned int n;

	n = v->end - v->buffer + 2048;

	if (!(buffer = vbi3_realloc (v->buffer, n * sizeof (*v->buffer)))) {
		longjmp (sub->main, -1);
	}

	v->bp = buffer + (v->bp - v->buffer);
	v->buffer = buffer;
	v->end = buffer + n;
}

static void
putws				(sub_instance *		sub,
				 vbi3_bool		escape,
				 const char *		s);

static void
putwc				(sub_instance *		sub,
				 vbi3_bool		escape,
				 unsigned int		wc)
{
	if (escape) {
		switch (sub->format) {
		case FORMAT_REALTEXT:
		case FORMAT_SAMI:
			switch (wc) {
			case '<':
				putws (sub, FALSE, "&lt;");
				return;

			case '>':
				putws (sub, FALSE, "&gt;");
				return;

			case '&':
				putws (sub, FALSE, "&amp;");
				return;

			default:
				break;
			}

			break;

		default:
			break;
		}
	}

	if (sub->text1.bp >= sub->text1.end)
		extend (sub, &sub->text1);

	*sub->text1.bp++ = wc;
}

static void
putws				(sub_instance *		sub,
				 vbi3_bool		escape,
				 const char *		s)
{
	if (escape) {
		while (*s)
			putwc (sub, TRUE, (unsigned int) *s++);
	} else {
		uint16_t *d;
		unsigned int n;
		
		n = strlen (s);

		if (sub->text1.bp + n > sub->text1.end)
			extend (sub, &sub->text1);

		d = sub->text1.bp;

		while (n-- > 0)
			*d++ = *s++;

		sub->text1.bp = d;
	}
}

static void
wprintf				(sub_instance *		sub,
				 vbi3_bool		escape,
				 const char *		template,
				 ...)
{
	char buffer[1024];
	va_list ap;
	int n;

	va_start (ap, template);
	n = vsnprintf (buffer, sizeof (buffer), template, ap);
	va_end (ap);

	if (n < 0 || n > (int) sizeof (buffer) - 1) {
		longjmp (sub->main, -1);
	}

	putws (sub, escape, buffer);
}

static void
color				(sub_instance *		sub,
				 const char *		label,
				 vbi3_rgba		color)
{
	switch (sub->format) {
	case FORMAT_QTTEXT:
		wprintf (sub, FALSE, "{%s%u,%u,%u}",
			 label,
			 VBI3_R (color) * 0x0101,
			 VBI3_G (color) * 0x0101,
			 VBI3_B (color) * 0x0101);
		break;

	case FORMAT_REALTEXT:
	case FORMAT_SAMI:
		wprintf (sub, FALSE, "%s#%02x%02x%02x",
			 label,
			 VBI3_R (color),
			 VBI3_G (color),
			 VBI3_B (color));
		break;

	default:
		assert (!"reached");
	}
}

static void
sami_span			(sub_instance *		sub,
				 const vbi3_page *	pg,
				 const vbi3_char *	ac)
{
	unsigned int ct = 0;

	putws (sub, FALSE, "<SPAN style=\"");

	if (ac->foreground != sub->last_ac.foreground) {
		color (sub, "color:",
		       pg->color_map[ac->foreground]);
		ct = 1;
	}

	if (ac->background != sub->last_ac.background) {
		if (ct)
			putwc (sub, FALSE, ';');
		ct = 1;
		color (sub, "background-color:",
		       pg->color_map[ac->background]);
	}

	if (ac->attr & VBI3_UNDERLINE) {
		if (ct)
			putwc (sub, FALSE, ';');
		ct = 1;
		putws (sub, FALSE, "text-decoration:underline");
	}

	if (ac->attr & VBI3_BOLD) {
		if (ct)
			putwc (sub, FALSE, ';');
		ct = 1;
		putws (sub, FALSE, "font-weight:bold");
	}

	if (ac->attr & VBI3_ITALIC) {
		if (ct)
			putwc (sub, FALSE, ';');
		ct = 1;
		putws (sub, FALSE, "font-style:italic");
	}

	if (ac->attr & VBI3_FLASH) {
		if (ct)
			putwc (sub, FALSE, ';');
		ct = 1;
		putws (sub, FALSE, "text-decoration:blink");
	}

	putws (sub, FALSE, "\">");

	sub->last_ac = *ac;

	sub->in_span = TRUE;
}

static void
qt_style			(sub_instance *		sub,
				 const vbi3_page *	pg,
				 const vbi3_char *	ac)
{
	unsigned int attr;

	if (ac->foreground != sub->last_ac.foreground)
		color (sub, "textColor:",
		       pg->color_map[ac->foreground]);

	if (ac->background != sub->last_ac.background)
		color (sub, "backColor:",
		       pg->color_map[ac->background]);

	attr = ac->attr ^ sub->last_ac.attr;

	if (attr) /* reset */
		putws (sub, FALSE, "{plain}");

	if (attr & VBI3_UNDERLINE)
		putws (sub, FALSE, "{underline}");

	if (attr & VBI3_BOLD)
		putws (sub, FALSE, "{bold}");

	if (attr & VBI3_ITALIC)
		putws (sub, FALSE, "{italic}");

	sub->last_ac = *ac;
}

static void
real_style			(sub_instance *		sub,
				 const vbi3_page *	pg,
				 const vbi3_char *	ac)
{
	if (ac->foreground != sub->last_ac.foreground
	    || ac->background != sub->last_ac.background) {
		unsigned int ct = 0;

		putws (sub, FALSE, "<font ");

		if (ac->foreground != sub->last_ac.foreground) {
			color (sub, "color=\"",
			       pg->color_map[ac->foreground]);
			ct = 1;
		}

		if (ac->background != sub->last_ac.background) {
			if (ct)
				putws (sub, FALSE, "\" ");
			color (sub, "bgcolor=\"",
			       pg->color_map[ac->background]);
		}

		putws (sub, FALSE, "\">");

		sub->in_span = TRUE;
	}

	if (ac->attr & VBI3_UNDERLINE) {
		putws (sub, FALSE, "<u>");
		sub->in_underline = TRUE;
	}

	if (ac->attr & VBI3_BOLD) {
		putws (sub, FALSE, "<b>");
		sub->in_bold = TRUE;
	}

	if (ac->attr & VBI3_ITALIC) {
		putws (sub, FALSE, "<i>");
		sub->in_italic = TRUE;
	}

	sub->last_ac = *ac;
}

static void
real_style_end			(sub_instance *		sub)
{
	if (sub->in_italic) {
		putws (sub, FALSE, "</i>");
		sub->in_italic = FALSE;
	}

	if (sub->in_bold) {
		putws (sub, FALSE, "</b>");
		sub->in_bold = FALSE;
	}

	if (sub->in_underline) {
		putws (sub, FALSE, "</u>");
		sub->in_underline = FALSE;
	}

	if (sub->in_span) {
		putws (sub, FALSE, "</font>");
		sub->in_span = FALSE;
	}

	sub->last_ac = sub->para_ac;
}

static void
flush				(sub_instance *		sub)
{
	if (!vbi3_stdio_cd_ucs2 (sub->export.fp, sub->cd,
				sub->text1.buffer,
				(unsigned int)(sub->text1.bp
					       - sub->text1.buffer))) {
		longjmp (sub->main, -1);
	}

	sub->text1.bp = sub->text1.buffer;
}

static void
header				(sub_instance *		sub,
				 const vbi3_page *	pg)
{
	switch (sub->format) {
	case FORMAT_MPSUB:
		putws (sub, FALSE, "TITLE=");

		if (sub->export.network) {
			putws (sub, TRUE, sub->export.network);
			putwc (sub, FALSE, 0x0020);
		}

		if (pg->pgno < 0x100) {
			putws (sub, FALSE, "Closed Caption");
		} else if (pg->subno && pg->subno != VBI3_ANY_SUBNO) {
			wprintf (sub, TRUE, _("Teletext Page %3x.%x"),
				 pg->pgno, pg->subno);
		} else {
			wprintf (sub, TRUE, _("Teletext Page %3x"),
				 pg->pgno);
		}

		putws (sub, FALSE, "\nAUTHOR=");
		putws (sub, TRUE, sub->export.creator);

		/* Times in seconds, not frames. */
		putws (sub, FALSE, "\nFORMAT=TIME\n");

		break;

	case FORMAT_QTTEXT:
	{
		/* Of course they have to use a proprietary
		   numbering scheme... */
		static const char *languages [] = {
			"en", "fr", "de", "it",
			"nl", "es", "da", "pt",
			"nn", "he", "jp", "ar",
			"fi", "el", "is", "mt",
			"tr", "hr", "zh", "ur",
			"th", "ko", "lt", "pl",
			"hu", "et", "lv", "lv",
			"??", "fo", "??", "??",
			"ru", "zh", "??", "ga",
			"sq", "ro", "cs", "sk",
			"sl", "yi", "sr", "mk",
			"bg", "uk",
		};
		static const vbi3_character_set *cs;
		unsigned int lc;

		cs = vbi3_page_get_character_set (pg, 0);

		if (!cs) {
			lc = 0;
		} else {
			for (lc = 0; lc < N_ELEMENTS (languages); ++lc)
				if (0 == strcmp (languages[lc],
						 cs->language_code[0]))
					break;

			if (lc >= N_ELEMENTS (languages))
				lc = 0;
		}

		CLEAR (sub->last_ac);

		sub->last_ac.foreground = 7;
		sub->last_ac.background = 0;

		sub->last_just = 0; /* center */

		wprintf (sub, FALSE,
			 "{QTtext}"
			 "{font:%s}"
			 "{plain}"
			 "{justify:center}",
			 sub->font);

		color (sub, "textColor:", pg->color_map[7]);
		color (sub, "backColor:", pg->color_map[0]);

		if (lc < N_ELEMENTS (languages))
			wprintf (sub, FALSE, "{language:%u}", lc);

		putws (sub, FALSE,
		       "{timeStamps:absolute}"
		       "{timeScale:100}"
		       "\n");

		break;
	}

	case FORMAT_REALTEXT:
	{
		CLEAR (sub->para_ac);

		sub->para_ac.foreground = 7;
		sub->para_ac.background = 0;

		sub->last_just = 0; /* center */

		wprintf (sub, FALSE,
			 "<window "
			 "type=\"generic\" "
			 /* RealPlayer 7+ supports ISO-8859-1 */
			 "version=\"1.2\" "
			 "charset=\"%s\" "
			 "face=\"%s\" ",
			 sub->charset,
			 sub->font);

		color (sub, "color=\"", pg->color_map[7]);
		color (sub, "\" bgcolor=\"", pg->color_map[0]);

		putws (sub, FALSE, "\">\n");

		break;
	}

	case FORMAT_SAMI:
	{
		static const vbi3_character_set *cs;
		const char *lang;

		lang = "en";

		cs = vbi3_page_get_character_set (pg, 0);

		if (cs && cs->language_code[0])
			lang = cs->language_code[0];

		putws (sub, FALSE,
		       "<SAMI>\n"
		       "<head>\n"
		       "<title>");

		if (sub->export.network) {
			putws (sub, TRUE, sub->export.network);
			putwc (sub, FALSE, 0x0020);
		}

		if (pg->pgno < 0x100) {
			putws (sub, FALSE, "Closed Caption");
		} else if (pg->subno && pg->subno != VBI3_ANY_SUBNO) {
			/* XXX gettext encoding */
			wprintf (sub, TRUE, _("Teletext Page %3x.%x"),
				 pg->pgno, pg->subno);
		} else {
			wprintf (sub, TRUE, _("Teletext Page %3x"),
				 pg->pgno);
		}

		putws (sub, FALSE,
		       "</title>\n"
		       "<SAMIParam>\n"
		       "Metrics {time:ms}\n"
		       "Spec {MSFT:1.0}\n"
		       "</SAMIParam>\n"
		       "<style type=\"text/css\"><!--\n"
		       "P {lang:");

		putws (sub, FALSE, lang);

		putws (sub, FALSE,
		       ";text-align:center;");

		sub->para_ac.foreground = 7;
		sub->para_ac.background = 0;

		color (sub, "color:", pg->color_map[7]);
		color (sub, ";background-color:", pg->color_map[0]);

		putws (sub, FALSE,
		       ";padding:5px}\n"
		       "P.l {text-align:left}\n"
		       "P.r {text-align:right}\n"
		       "--></style>\n"
		       "</head>\n"
		       "<body>\n");

		break;
	}

	case FORMAT_SUBRIP:
		/* Nothing. */
		break;

	case FORMAT_SUBVIEWER:
		wprintf (sub, FALSE,
			 "[INFORMATION]\n"
			 "[TITLE]\n"
			 "[AUTHOR]%s\n"
			 "[SOURCE]",
			 sub->export.creator);

		if (sub->export.network)
			wprintf (sub, FALSE, "%s ", sub->export.network);

		if (pg->pgno < 0x100) {
			putws (sub, FALSE, "Closed Caption");
		} else if (pg->subno && pg->subno != VBI3_ANY_SUBNO) {
			/* XXX gettext encoding */
			wprintf (sub, FALSE, _("Teletext Page %3x.%x"),
				 pg->pgno, pg->subno);
		} else {
			wprintf (sub, FALSE, _("Teletext Page %3x"),
				 pg->pgno);
		}

		/* What's this? I have no idea, just copied. */
		wprintf (sub, FALSE,
			 "\n"
			 "[PRG]\n"
			 "[FILEPATH]\n"
			 "[DELAY]0\n"
			 "[CD TRACK]0\n"
			 "[COMMENT]\n"
			 "[END INFORMATION]\n"
			 "[SUBTITLE]\n"
			 "[COLF]&HFFFFFF,[STYLE]bd,[SIZE]24,[FONT]%s\n",
			 sub->font);

		break;
	}
}

static void
footer				(sub_instance *		sub)
{
	switch (sub->format) {
	case FORMAT_MPSUB:
	case FORMAT_QTTEXT:
	case FORMAT_SUBRIP:
	case FORMAT_SUBVIEWER:
		break;

	case FORMAT_REALTEXT:
		putws (sub, FALSE,
		       "</window>\n");
		break;

	case FORMAT_SAMI:
		putws (sub, FALSE,
		       "</body>\n"
		       "</SAMI>\n");
		break;
	}
}

static void
timestamp			(sub_instance *		sub)
{
	double t1;
	double t2;
	unsigned int s1;
	unsigned int s2;

	/* Absolute stream time. */
	t1 = sub->last_timestamp;
	t2 = sub->export.stream.timestamp;

	/* Seconds and fractions (to prevent overflow). */
	s1 = floor (t1);
	t1 -= s1;

	s2 = floor (t2);
	t2 -= s2;

	switch (sub->format) {
	case FORMAT_MPSUB:
	{
		double delay_time;
		double show_time;

		/* Apparently delay_time must be integer. We round down
		   and add the fraction to show_time to compensate
		   for the error. */
		delay_time = floor (sub->delay_time);

		show_time = sub->export.stream.timestamp - sub->last_timestamp;
		show_time += sub->delay_time - delay_time;

		/* wait hold\n
		   That is how long to wait after the previous
		   page disappeared and how long to display the
		   following page. */
		wprintf (sub, FALSE, "%u %f\n",
			 (unsigned int) delay_time, show_time);
		break;
	}

	case FORMAT_QTTEXT:
		/* [hh:mm:ss.xx]\n  presentation time */
		wprintf (sub, FALSE,
			 "[%02u:%02u:%02u.%02u]\n",
			 s2 / 3600, (s2 / 60) % 60, s2 % 60,
			 (unsigned int)(t2 * 100));
		break;

	case FORMAT_REALTEXT:
		/* <time begin="hh:mm:ss.xx" end="hh:mm:ss.xx"/>
		   <clear/> just to be safe. */
		wprintf (sub, FALSE,
			 "<time begin=\"%02u:%02u:%02u.%02u\" "
			 "end=\"%02u:%02u:%02u.%02u\"/><clear/>",
			 s1 / 3600, s1 / 60, s1 % 60,
			 (unsigned int)(t1 * 100),
			 s2 / 3600, s2 / 60, s2 % 60,
			 (unsigned int)(t2 * 100));
		break;

	case FORMAT_SAMI:
	{
		double elapsed;

		elapsed = sub->export.stream.timestamp
			- sub->export.stream.start_timestamp;

		/* Presentation time in ms since start. */
		/* Note MPlayer cannot handle "start=\"%llu\"". */
		wprintf (sub, FALSE,
			 "<SYNC Start=%llu>",
			 (uint64_t)(elapsed * 1000));
		break;
	}

	case FORMAT_SUBRIP:
		/* n  number of page, starting at one.
		   hh:mm:ss,xxx --> hh:mm:ss,xxx\n  display duration */
		/* XXX encoding/charset */
		wprintf (sub, FALSE,
			 "%u\n"
			 "%02u:%02u:%02u,%03u --> "
			 "%02u:%02u:%02u,%03u\n",
			 sub->n_pages + 1,
			 s1 / 3600, (s1 / 60) % 60, s1 % 60,
			 (unsigned int)(t1 * 1000),
			 s2 / 3600, (s2 / 60) % 60, s2 % 60,
			 (unsigned int)(t2 * 1000));
		break;

	case FORMAT_SUBVIEWER:
		/* hh:mm:ss.xx,hh:mm:ss.xx\n  display duration */
		/* XXX encoding/charset */
		wprintf (sub, FALSE,
			 "%02u:%02u:%02u.%02u,"
			 "%02u:%02u:%02u.%02u\n",
			 s1 / 3600, (s1 / 60) % 60, s1 % 60,
			 (unsigned int)(t1 * 100),
			 s2 / 3600, (s2 / 60) % 60, s2 % 60,
			 (unsigned int)(t2 * 100));
		break;

	default:
		assert (!"reached");
	}
}

static vbi3_bool
same_style			(sub_instance *		sub,
				 const vbi3_char *	ac1,
				 const vbi3_char *	ac2)
{
	if (ac1->background != ac2->background)
		return FALSE;

	if (0x0020 == ac1->unicode)
		return TRUE;

	if (ac1->foreground != ac2->foreground)
		return FALSE;

	switch (sub->format) {
	case FORMAT_MPSUB:
	case FORMAT_SUBRIP:
	case FORMAT_SUBVIEWER:
		/* Has no text attributes. */
		break;

	case FORMAT_QTTEXT:
	case FORMAT_REALTEXT:
		if ((ac1->attr ^ ac2->attr)
		    & (VBI3_UNDERLINE | VBI3_BOLD | VBI3_ITALIC))
			return FALSE;
		break;

	case FORMAT_SAMI:
		if ((ac1->attr ^ ac2->attr)
		    & (VBI3_UNDERLINE | VBI3_BOLD | VBI3_ITALIC | VBI3_FLASH))
			return FALSE;
		break;
	}

	return TRUE;
}

static void
style_change			(sub_instance *		sub,
				 const vbi3_page *	pg,
				 const vbi3_char *	ac)
{
	switch (sub->format) {
	case FORMAT_QTTEXT:
		if (!same_style (sub, ac, &sub->last_ac))
			qt_style (sub, pg, ac);
		break;

	case FORMAT_REALTEXT:
		if (!same_style (sub, ac, &sub->last_ac)) {
			real_style_end (sub);

			sub->last_ac = sub->para_ac;

			if (!same_style (sub, ac, &sub->last_ac))
				real_style (sub, pg, ac);
		}

		break;

	case FORMAT_SAMI:
		if (!same_style (sub, ac, &sub->last_ac)) {
			if (sub->in_span)
				putws (sub, FALSE, "</SPAN>");

			sub->in_span = FALSE;

			sub->last_ac = sub->para_ac;

			if (!same_style (sub, ac, &sub->last_ac))
				sami_span (sub, pg, ac);
		}

		break;
				
	default:
		break;
	}
}

static void
page_layout			(unsigned int *		top,
				 unsigned int *		bottom,
				 unsigned int *		left,
				 unsigned int *		right,
				 unsigned int *		hjust,
				 const vbi3_page *	pg)
{
	unsigned int row;
	unsigned int left_min;
	unsigned int left_max;
	unsigned int right_min;
	unsigned int right_max;

	*top = pg->rows;
	*bottom = 0;

	left_min = pg->columns;
	left_max = 0;

	right_min = pg->columns;
	right_max = 0;

	row = 0;
	if (pg->pgno >= 0x100)
		row = 1;

	for (; row < pg->rows; ++row) {
		const vbi3_char *cp;
		unsigned int column;

		cp = pg->text + row * pg->columns;

		for (column = 0; column < pg->columns; ++column)
			if (0x0020 != cp[column].unicode
			    && cp[column].size < VBI3_OVER_TOP
			    && vbi3_is_print (cp[column].unicode))
				break;

		if (column >= pg->columns)
			continue; /* empty row */

		*top = MIN (*top, row);
		*bottom = MAX (*bottom, row);

		left_min = MIN (left_min, column);
		left_max = MAX (left_max, column);

		for (column = pg->columns; column > 0; --column)
			if (0x0020 != cp[column - 1].unicode
			    && cp[column - 1].size < VBI3_OVER_TOP
			    && vbi3_is_print (cp[column - 1].unicode))
				break;

		right_min = MIN (right_min, column);
		right_max = MAX (right_max, column);
	}

	*left = left_min;
	*right = right_max - 1;

	*hjust = 0; /* not justified */

	if (left_min == left_max)
		*hjust |= 1; /* left */

	if (right_min == right_max)
		*hjust |= 2; /* right */
}

static void
paragraph			(sub_instance *		sub,
				 const vbi3_page *	pg,
				 unsigned int		top,
				 unsigned int		bottom,
				 unsigned int		left,
				 unsigned int		right)
{
	unsigned int row;
	unsigned int width;

	width = right - left + 1;

	for (row = top; row <= bottom; ++row) {
		const vbi3_char *cp;
		unsigned int column;
		unsigned int spaces;
		unsigned int fluff;

		cp = pg->text + row * pg->columns;

		spaces = 0;
		fluff = 0;

		for (column = left; column <= right; ++column) {
			unsigned int c;

			if (cp[column].size >= VBI3_OVER_TOP) {
				++fluff;
				continue;
			}

			c = cp[column].unicode;

			if (0x0020 == c || !vbi3_is_print (c)) {
				switch (sub->format) {
				case FORMAT_SAMI:
					if (sub->last_ac.background
					    != cp[column].background)
						break;

					/* fall through */

				default:
					++spaces;
					continue;
				}

				c = 0x0020;
			}

			if (spaces > 0) {
				if (spaces + fluff >= (column - left)) {
					/* Discard leading spaces. */
				} else if (spaces > 1) {
					/* Runs of spaces. */

					switch (sub->format) {
					case FORMAT_SAMI:
						while (spaces-- > 0)
							putws (sub, FALSE,
							       "&nbsp;");
						break;

					default:
						/* Collapse to single space. */
						putwc (sub, FALSE, 0x0020);
						break;
					}
				} else {
					putwc (sub, FALSE, 0x0020);
				}

				spaces = 0;
			}

			fluff = 0;

			style_change (sub, pg, cp + column);

			putwc (sub, /* escape */ TRUE, c);
		}

		if (spaces > 0) {
			/* Discard trailing spaces. */
		}

		/* Line separator. */

		switch (sub->format) {
		case FORMAT_MPSUB:
		case FORMAT_QTTEXT:
		case FORMAT_SUBRIP:
			if (spaces + fluff >= width) {
				/* Suppress blank line. */
			} else {
				putwc (sub, /* escape */ FALSE, 10);
			}

			break;

		case FORMAT_REALTEXT:
		case FORMAT_SAMI:
			if (row < bottom)
				putws (sub, FALSE, "<br/>");
			break;

		case FORMAT_SUBVIEWER:
			if (row < bottom)
				putws (sub, FALSE, "[br]");
			else
				putwc (sub, FALSE, 10);
			break;
		}
	}
}

static vbi3_bool
export				(vbi3_export *		e,
				 const vbi3_page *	pg)
{
	sub_instance *sub = PARENT (e, sub_instance, export);
	unsigned int top;
	unsigned int bottom;
	unsigned int left;
	unsigned int right;
	unsigned int hjust;

	if (setjmp (sub->main)) {
		/* Discard unfinished output. */
		sub->text1.bp = sub->text1.buffer;

		return FALSE;
	}

	if (!pg) {
		/* Finalize the stream. */

		if (sub->text1.bp > sub->text1.buffer) {
			/* Put timestamp in front of page (text1). */
			SWAP (sub->text1, sub->text2);
			timestamp (sub);
			flush (sub);
			SWAP (sub->text1, sub->text2);
		}

		footer (sub);
		flush (sub);

		sub->have_header = FALSE;

		vbi3_iconv_ucs2_close (sub->cd);
		sub->cd = (iconv_t) -1;

		return TRUE;
	}

	if (0 != sub->last_pgno
	    && pg->pgno != sub->last_pgno) {
		fprintf (stderr, "Multilingual subtitle recording "
				 "not supported yet: pgno=%x last_pgno=%x\n",
				 pg->pgno, sub->last_pgno);
		return FALSE;
	}

	sub->last_pgno = pg->pgno;

	if (!sub->have_header) {
		char buffer[256];
		char *d;
		size_t n;

		d = buffer;

		sub->cd = vbi3_iconv_ucs2_open (sub->charset, &d,
					       sizeof (buffer));
		if ((iconv_t) -1 == sub->cd) {
			return FALSE;
		}

		n = d - buffer;

		if (n > 0)
			if (n != fwrite (buffer, 1, n, sub->export.fp))
				longjmp (sub->main, -1);

		header (sub, pg);
		flush (sub);

		sub->have_header = TRUE;

		sub->last_timestamp = e->stream.start_timestamp;

		sub->delay_time = e->stream.timestamp
			- e->stream.start_timestamp;

		sub->n_pages = 0;
		sub->blank_pages = 0;
	}

	/* First let's determine where the text is, and if
	   it's left or right justified or centered. Not handled
	   are split cases, e. g. one para at top right, another
	   bottom centered. When the page is empty top > bottom. */

	page_layout (&top, &bottom, &left, &right, &hjust, pg);

	/* Finalize the previous page
	   and write text position/justification tags. */

	switch (sub->format) {
	case FORMAT_MPSUB:
		if (0 == sub->blank_pages) {
			if (sub->text1.bp > sub->text1.buffer) {
				SWAP (sub->text1, sub->text2);
				timestamp (sub);
				flush (sub);
				SWAP (sub->text1, sub->text2);
				flush (sub);
			}

			/* End of last non-blank page. */
			sub->last_timestamp = e->stream.timestamp;
		}

		if (top > bottom) {
			/* Ignore blank pages. */
			sub->blank_pages = 1;
			return TRUE;
		}

		/* Delay btw end of last and start of this non-blank page. */
		sub->delay_time = e->stream.timestamp - sub->last_timestamp;
		sub->blank_pages = 0;

		/* MPSub is always centered at bottom. */
		hjust = 0;

		break;

	case FORMAT_QTTEXT:
	{
		static const char *hjust_attr [] = {
			"{justify:center}",
			"{justify:left}",
			"{justify:right}",
			"{justify:center}",
		};

		timestamp (sub);

		if (top > bottom)
			goto page_separator;

		if (1 && 0 == hjust) {
			if ((int)(left + right + 8) < (int) pg->columns * 2)
				hjust = 1; /* left */
			else
			if ((int)(left + right - 8) > (int) pg->columns * 2)
				hjust = 2; /* right */
			/* else center */
		}

		if (sub->last_just != hjust) {
			putws (sub, FALSE, hjust_attr[hjust]);
			sub->last_just = hjust;
		}

		break;
	}

	case FORMAT_REALTEXT:
		if (0 == sub->blank_pages
		    && sub->text1.bp > sub->text1.buffer) {
			SWAP (sub->text1, sub->text2);
			timestamp (sub);
			flush (sub);
			SWAP (sub->text1, sub->text2);
			flush (sub);
		}

		if (top > bottom) {
			/* Ignore blank pages. */
			sub->blank_pages = 1;
			return TRUE;
		}

		hjust = 0; /* ? */

		sub->last_timestamp = sub->export.stream.timestamp;

		sub->blank_pages = 0;

		sub->last_ac = sub->para_ac;

		break;

	case FORMAT_SAMI:
		timestamp (sub);

		if (top > bottom) {
			putws (sub, FALSE, "<P>&nbsp;");
			goto page_separator;
		}

		/* Pity we cannot just define a screen center
		   relative position. */

		if (1 && 0 == hjust) {
			if ((int)(left + right + 8) < (int) pg->columns * 2)
				hjust = 1; /* left */
			else
			if ((int)(left + right - 8) > (int) pg->columns * 2)
				hjust = 2; /* right */
			/* else center */
		}

		if (0 == hjust || 3 == hjust) {
			/* Can only justify left or right. */
			/* Note MPlayer cannot handle "<p>". */
			putws (sub, FALSE, "<P>");
		} else {
			wprintf (sub, FALSE,
				 "<P class=\"%c\">", "xlrx"[hjust]);
		}

		sub->last_ac = sub->para_ac;

		break;

	case FORMAT_SUBRIP:
	case FORMAT_SUBVIEWER:
		if (0 == sub->blank_pages
		    && sub->text1.bp > sub->text1.buffer) {
			SWAP (sub->text1, sub->text2);
			timestamp (sub);
			flush (sub);
			SWAP (sub->text1, sub->text2);
			flush (sub);

			++sub->n_pages;
		}

		if (top > bottom) {
			/* Ignore blank pages. */
			sub->blank_pages = 1;
			return TRUE;
		}

		sub->last_timestamp = sub->export.stream.timestamp;

		sub->blank_pages = 0;

		break;
	}

	/* Write paragraph. */

	paragraph (sub, pg, top, bottom, left, right);

 page_separator:

	/* Page separator. */

	switch (sub->format) {
	case FORMAT_MPSUB:
	case FORMAT_SUBRIP:
	case FORMAT_SUBVIEWER:
		/* Separator is a blank line. */
		putwc (sub, FALSE, 10);
		/* Don't flush, have to add timestamp. */
		break;

	case FORMAT_QTTEXT:
		/* Nothing. */
		flush (sub);
		break;

	case FORMAT_REALTEXT:
		real_style_end (sub);
		putwc (sub, FALSE, 10);
		/* Don't flush, have to add timestamp. */
		break;

	case FORMAT_SAMI:
		if (sub->in_span) {
			putws (sub, FALSE, "</SPAN>");
			sub->in_span = FALSE;
		}
		putws (sub, FALSE, "</P></SYNC>\n");
		flush (sub);
		break;
	}

	sub->last_timestamp = sub->export.stream.timestamp;

	return TRUE;
}

static const vbi3_export_info
export_info_mpsub = {
	.keyword		= "mpsub",
	.label			= "MPSub",
	/* TRANSLATORS:
	   Caption is an aid for the hearing impaired,
	   subtitles for foreign language viewers. */
	.tooltip		= N_("MPlayer caption/subtitle file"),
	.mime_type		= NULL,
	.extension		= "sub",
	.open_format		= TRUE,
};

const _vbi3_export_module
_vbi3_export_module_mpsub = {
	.export_info		= &export_info_mpsub,
	._new			= sub_new,
	._delete		= sub_delete,
	.option_info		= option_info1,
	.option_info_size	= N_ELEMENTS (option_info1),
	.option_get		= option_get,
	.option_set		= option_set,
	.export			= export
};

static const vbi3_export_info
export_info_qttext = {
	.keyword		= "qttext",
	.label			= "QTText",
	/* TRANSLATORS:
	   Styles like bold, italic, underlined, etc.
	   Justification to the left, right, centered. */
	.tooltip		= N_("QuickTime Text caption/subtitle file "
				     "preserving text styles, justification "
				     "and color"),
	.mime_type		= NULL,
	.extension		= "txt",
	.open_format		= TRUE,
};

const _vbi3_export_module
_vbi3_export_module_qttext = {
	.export_info		= &export_info_qttext,
	._new			= sub_new,
	._delete		= sub_delete,
	.option_info		= option_info2,
	.option_info_size	= N_ELEMENTS (option_info2),
	.option_get		= option_get,
	.option_set		= option_set,
	.export			= export
};

static const vbi3_export_info
export_info_realtext = {
	.keyword		= "realtext",
	.label			= "RealText",
	.tooltip		= N_("RealText caption/subtitle file "
				     "preserving text styles and color"),
	.mime_type		= NULL,
	.extension		= "rt",
	.open_format		= TRUE,
};

const _vbi3_export_module
_vbi3_export_module_realtext = {
	.export_info		= &export_info_realtext,
	._new			= sub_new,
	._delete		= sub_delete,
	.option_info		= option_info2,
	.option_info_size	= N_ELEMENTS (option_info2),
	.option_get		= option_get,
	.option_set		= option_set,
	.export			= export
};

static const vbi3_export_info
export_info_sami = {
	.keyword		= "sami",
	.label			= "SAMI",
	.tooltip		= N_("SAMI 1.0 caption/subtitle file "
				     "preserving text styles, justification "
				     "and color"),
	.mime_type		= NULL,
	.extension		= "sami,smi",
	.open_format		= TRUE,
};

const _vbi3_export_module
_vbi3_export_module_sami = {
	.export_info		= &export_info_sami,
	._new			= sub_new,
	._delete		= sub_delete,
	.option_info		= option_info1,
	.option_info_size	= N_ELEMENTS (option_info1),
	.option_get		= option_get,
	.option_set		= option_set,
	.export			= export
};

static const vbi3_export_info
export_info_subrip = {
	.keyword		= "subrip",
	.label			= "SubRip",
	.tooltip		= N_("SubRip caption/subtitle file"),
	.mime_type		= NULL,
	.extension		= "srt",
	.open_format		= TRUE,
};

const _vbi3_export_module
_vbi3_export_module_subrip = {
	.export_info		= &export_info_subrip,
	._new			= sub_new,
	._delete		= sub_delete,
	.option_info		= option_info1,
	.option_info_size	= N_ELEMENTS (option_info1),
	.option_get		= option_get,
	.option_set		= option_set,
	.export			= export
};

static const vbi3_export_info
export_info_subviewer = {
	.keyword		= "subviewer",
	.label			= "SubViewer",
	.tooltip		= N_("SubViewer 2.x caption/subtitle file"),
	.mime_type		= NULL,
	.extension		= "sub",
	.open_format		= TRUE,
};

const _vbi3_export_module
_vbi3_export_module_subviewer = {
	.export_info		= &export_info_subviewer,
	._new			= sub_new,
	._delete		= sub_delete,
	.option_info		= option_info2,
	.option_info_size	= N_ELEMENTS (option_info2),
	.option_get		= option_get,
	.option_set		= option_set,
	.export			= export
};