File: cairo.trm

package info (click to toggle)
gnuplot5 5.0.0~rc%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,548 kB
  • ctags: 8,104
  • sloc: ansic: 77,108; cpp: 6,848; makefile: 1,932; sh: 1,343; lisp: 657; perl: 302; awk: 235; pascal: 194; tcl: 88; python: 46
file content (1737 lines) | stat: -rw-r--r-- 64,691 bytes parent folder | download | duplicates (2)
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
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
/* Hello, Emacs: this is -*-C-*- !
 * $Id: cairo.trm,v 1.71 2014/07/01 14:46:25 sfeam Exp $
 */

/* GNUPLOT - cairo.trm */

/*[
 * Copyright 2007   Timothee Lecomte
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/

/*
 * Modifications:
 *	Peter Danenberg, Ethan Merritt	- handle png output as well as pdf
 *	Peter Danenberg - crop for PNGs
 *	René Haber - added epscairo terminal
 *	Bastian Maerkisch - cairolatex terminal
 */

#ifdef TERM_REGISTER
register_term (pdfcairo)
#endif

#ifdef TERM_PROTO
TERM_PUBLIC void cairotrm_options __PROTO ((void));
TERM_PUBLIC void cairotrm_init __PROTO ((void));
TERM_PUBLIC void cairotrm_graphics __PROTO ((void));
TERM_PUBLIC void cairotrm_text __PROTO ((void));
TERM_PUBLIC void cairotrm_linetype __PROTO ((int linetype));
TERM_PUBLIC void cairotrm_move __PROTO ((unsigned int x, unsigned int y));
TERM_PUBLIC void cairotrm_vector __PROTO ((unsigned int x, unsigned int y));
TERM_PUBLIC void cairotrm_put_text __PROTO ((unsigned int x, unsigned int y, const char *str));
TERM_PUBLIC void cairotrm_enhanced_flush();
TERM_PUBLIC void cairotrm_enhanced_writec(int c);
TERM_PUBLIC void cairotrm_enhanced_open(char* fontname, double fontsize, double base, TBOOLEAN widthflag, TBOOLEAN showflag, int overprint);
TERM_PUBLIC void cairotrm_reset __PROTO ((void));
TERM_PUBLIC int cairotrm_justify_text __PROTO ((enum JUSTIFY mode));
TERM_PUBLIC void cairotrm_point __PROTO ((unsigned int x, unsigned int y, int pointstyle));
TERM_PUBLIC void cairotrm_linewidth __PROTO ((double linewidth));
TERM_PUBLIC int cairotrm_text_angle __PROTO ((int ang));
TERM_PUBLIC void cairotrm_fillbox __PROTO ((int style, unsigned int x1, unsigned int y1, unsigned int width, unsigned int height));
TERM_PUBLIC int cairotrm_set_font __PROTO ((const char *font));
TERM_PUBLIC void cairotrm_pointsize __PROTO ((double ptsize));
TERM_PUBLIC void cairotrm_image __PROTO ((unsigned int M, unsigned int N, coordval * image, gpiPoint * corner, t_imagecolor color_mode));
TERM_PUBLIC int cairotrm_make_palette __PROTO ((t_sm_palette *palette));
TERM_PUBLIC void cairotrm_set_color __PROTO ((t_colorspec *colorspec));
TERM_PUBLIC void cairotrm_filled_polygon __PROTO ((int n, gpiPoint * corners));
#ifdef EAM_BOXED_TEXT
TERM_PUBLIC void cairotrm_boxed_text __PROTO ((unsigned int x, unsigned int y, int option));
#endif
TERM_PUBLIC void cairotrm_dashtype __PROTO ((int type, t_dashtype *custom_dash_pattern));
#endif /* TERM_PROTO */

#ifndef TERM_PROTO_ONLY

#ifdef TERM_BODY

#include "cairo-pdf.h"
#ifdef HAVE_CAIROEPS
#include "cairo-ps.h"
#endif
#include "wxterminal/gp_cairo.h"
#include "wxterminal/gp_cairo_helpers.h"
#include "glib.h"		/* For guint32 */

#define CAIROTRM_DEFAULT_FONTNAME "Sans"

static cairo_status_t cairostream_write __PROTO ((void *closure, unsigned char *data, unsigned int length));
static int cairostream_error[1];

/* Terminal type of postscript dialect */
enum CAIRO_TERMINALTYPE {
    CAIROTERM_EPS, CAIROTERM_PDF, CAIROTERM_PNG, CAIROTERM_LATEX
};

/* One struct that takes all terminal parameters
 * by Harald Harders <h.harders@tu-bs.de> */
typedef struct cairo_params_t {
    enum CAIRO_TERMINALTYPE terminal;
    size_units explicit_units;
    TBOOLEAN enhanced;
    TBOOLEAN dashed;
    float dash_length;
    rgb_color background;
    TBOOLEAN mono;
    t_linecap linecap;           /* butt/rounded/square linecaps and linejoins */
    TBOOLEAN transparent;
    TBOOLEAN crop;
    char fontname[MAX_ID_LEN + 1];    /* name of font */
    int fontsize;                     /* size of font in pts */
    float fontscale;                  /* modified by this */
    float width;
    float height;
    float base_linewidth;
    float lw;
    TBOOLEAN pdf_output;              /* format of the graphics produced by cairolatex */
} cairo_params_t;

#define CAIROEPS_PARAMS_DEFAULT { \
    CAIROTERM_EPS, INCHES, FALSE, FALSE, 1.0, {1.,1.,1.}, FALSE, FALSE, TRUE, FALSE, "", \
    12, 0.5, 5*72., 3*72., 0.25, 1.0, FALSE \
}
static cairo_params_t cairoeps_params = CAIROEPS_PARAMS_DEFAULT;
static const cairo_params_t cairoeps_params_default = CAIROEPS_PARAMS_DEFAULT;

#ifdef HAVE_CAIROEPS
#define CAIROLATEX_PARAMS_DEFAULT { \
    CAIROTERM_LATEX, INCHES, FALSE, FALSE, 1.0, {1.,1.,1.}, FALSE, FALSE, TRUE, FALSE, "", \
    11, 0.6, 5*72., 3*72., 0.25, 1.0, FALSE \
}
#else
#define CAIROLATEX_PARAMS_DEFAULT { \
    CAIROTERM_LATEX, INCHES, FALSE, FALSE, 1.0, {1.,1.,1.}, FALSE, FALSE, TRUE, FALSE, "", \
    11, 0.6, 5*72., 3*72., 0.25, 1.0, TRUE \
}
#endif
static cairo_params_t cairolatex_params = CAIROLATEX_PARAMS_DEFAULT;
static const cairo_params_t cairolatex_params_default = CAIROLATEX_PARAMS_DEFAULT;

#define CAIROPDF_PARAMS_DEFAULT { \
    CAIROTERM_PDF, INCHES, FALSE, FALSE, 1.0, {1.,1.,1.}, FALSE, FALSE, TRUE, FALSE, "", \
    12, 0.5, 5*72., 3*72., 0.25, 1.0, FALSE \
}
static cairo_params_t cairopdf_params = CAIROPDF_PARAMS_DEFAULT;
static const cairo_params_t cairopdf_params_default = CAIROPDF_PARAMS_DEFAULT;

#define CAIROPNG_PARAMS_DEFAULT { \
    CAIROTERM_PNG, PIXELS, FALSE, FALSE, 1.0, {1.,1.,1.}, FALSE, FALSE, FALSE, FALSE, "", \
    12, 1.0, 640., 480., 1.0, 1.0, FALSE \
}
static cairo_params_t cairopng_params = CAIROPNG_PARAMS_DEFAULT;
static const cairo_params_t cairopng_params_default = CAIROPNG_PARAMS_DEFAULT;

static cairo_params_t *cairo_params = &cairopdf_params;
static const cairo_params_t *cairo_params_default = &cairopdf_params;

#ifdef PSLATEX_DRIVER
#define ISCAIROLATEX (cairo_params->terminal == CAIROTERM_LATEX)
static ps_params_t cairo_epslatex_params = EPSLATEX_PARAMS_DEFAULT;
#else
#define ISCAIROLATEX (FALSE)
#endif

plot_struct plot;

enum CAIROTRM_id {
    CAIROTRM_FONT,
    CAIROTRM_FONTSCALE,
    CAIROTRM_ENHANCED,
    CAIROTRM_NOENHANCED,
    CAIROTRM_SIZE,
    CAIROTRM_ROUNDED,
    CAIROTRM_BUTT,
    CAIROTRM_SQUARE,
    CAIROTRM_LINEWIDTH,
    CAIROTRM_DASHED,
    CAIROTRM_SOLID,
    CAIROTRM_MONO,
    CAIROTRM_COLOR,
    CAIROTRM_DASHLENGTH,
    CAIROTRM_TRANSPARENT,
    CAIROTRM_NOTRANSPARENT,
    CAIROTRM_CROP,
    CAIROTRM_NOCROP,
    CAIROTRM_BACKGROUND,
    CAIROLATEX_STANDALONE,
    CAIROLATEX_INPUT,
    CAIROLATEX_HEADER,
    CAIROLATEX_NOHEADER,
    CAIROLATEX_BLACKTEXT,
    CAIROLATEX_COLORTEXT,
    CAIROLATEX_EPS,
    CAIROLATEX_PDF,
    CAIROTRM_OTHER
};

static struct gen_table cairotrm_opts[] = {
    {"fontscale",   CAIROTRM_FONTSCALE},
    {"f$ont",   CAIROTRM_FONT},
    {"enh$anced", CAIROTRM_ENHANCED},
    {"noenh$anced", CAIROTRM_NOENHANCED},
    {"si$ze", CAIROTRM_SIZE},
    {"round$ed", CAIROTRM_ROUNDED},
    {"butt", CAIROTRM_BUTT},
    {"square", CAIROTRM_SQUARE},
    {"lw", CAIROTRM_LINEWIDTH},
    {"linew$idth", CAIROTRM_LINEWIDTH},
    {"dash$ed", CAIROTRM_DASHED},
    {"solid", CAIROTRM_SOLID},
    {"mono$chrome", CAIROTRM_MONO},
    {"color", CAIROTRM_COLOR},
    {"col$our", CAIROTRM_COLOR},
    {"dl", CAIROTRM_DASHLENGTH},
    {"dashl$ength", CAIROTRM_DASHLENGTH},
    {"transp$arent", CAIROTRM_TRANSPARENT},
    {"notransp$arent", CAIROTRM_NOTRANSPARENT},
    {"crop", CAIROTRM_CROP},
    {"nocrop", CAIROTRM_NOCROP},
    {"backg$round", CAIROTRM_BACKGROUND},
    {"nobackg$round", CAIROTRM_TRANSPARENT},
    {"stand$alone", CAIROLATEX_STANDALONE},
    {"inp$ut", CAIROLATEX_INPUT},
    {"header", CAIROLATEX_HEADER},
    {"noheader", CAIROLATEX_NOHEADER},
    {"b$lacktext", CAIROLATEX_BLACKTEXT},
    {"colort$ext", CAIROLATEX_COLORTEXT},
    {"colourt$ext", CAIROLATEX_COLORTEXT},
    {"eps", CAIROLATEX_EPS},
    {"pdf", CAIROLATEX_PDF},
    {NULL, CAIROTRM_OTHER}
};

/* "Called when terminal type is selected. This procedure should parse options on the command line.
* A list of the currently selected options should be stored in term_options[],
* in a form suitable for use with the set term command.
* term_options[] is used by the save command.  Use options_null() if no options are available." */
TERM_PUBLIC void cairotrm_options()
{
	char *s = NULL;
	char *font_setting = NULL;
	int sep;
	TBOOLEAN duplication = FALSE;
	TBOOLEAN set_font = FALSE, set_size = FALSE;
	TBOOLEAN set_capjoin = FALSE;
	TBOOLEAN set_standalone = FALSE, set_header = FALSE;

	char tmp_term_options[MAX_LINE_LEN+1] = "";

	/* Initialize terminal-dependent values */
	if (strcmp(term->name, "pngcairo") == 0) {
	    cairo_params = &cairopng_params;
	    cairo_params_default = &cairopng_params_default;
	} else if (strcmp(term->name, "epscairo") == 0) {
	    cairo_params = &cairoeps_params;
	    cairo_params_default = &cairoeps_params_default;
	}
#ifdef PSLATEX_DRIVER
	else if (strcmp(term->name, "cairolatex") == 0) {
	    cairo_params = &cairolatex_params;
	    cairo_params_default = &cairolatex_params_default;
	    ps_params = &cairo_epslatex_params;
	}
#endif
	else {
	    cairo_params = &cairopdf_params;
	    cairo_params_default = &cairopdf_params_default;
	}

	/* Default to enhanced text mode */
	if (!almost_equals(c_token-1, "termopt$ion")) {
		cairo_params->enhanced = TRUE;
		term->flags |= TERM_ENHANCED_TEXT;
	}

	while (!END_OF_COMMAND) {
		FPRINTF((stderr, "processing token\n"));
		switch (lookup_table(&cairotrm_opts[0], c_token)) {
		case CAIROTRM_FONT:
			c_token++;
			if (!(s = try_to_get_string()))
				int_error(c_token,"font: expecting string");
			if (!(*s)) {
				strcpy (cairo_params->fontname, "");
				cairo_params->fontsize = 0;
			} else {
				sep = strcspn(s,",");
				if (sep > 0) {
					strncpy(cairo_params->fontname, s, sep);
					cairo_params->fontname[sep] = '\0';
				}
				if (s[sep] == ',')
					sscanf(&(s[sep+1]), "%d", &cairo_params->fontsize);
			}
			font_setting = (char*) gp_alloc(strlen(s) + 1,"cairo terminal font");
			strcpy(font_setting,s);
			free(s);
			if (set_font) duplication=TRUE;
			set_font = TRUE;
			break;
		case CAIROTRM_ENHANCED:
			if (ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s", term->name);
			c_token++;
			cairo_params->enhanced = TRUE;
			term->flags |= TERM_ENHANCED_TEXT;
			break;
		case CAIROTRM_NOENHANCED:
			if (ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s", term->name);
			c_token++;
			cairo_params->enhanced = FALSE;
			term->flags &= ~TERM_ENHANCED_TEXT;
			break;
		case CAIROTRM_SIZE:
			c_token++;
			if (!strcmp(term->name,"pngcairo"))
			    cairo_params->explicit_units = parse_term_size(&cairo_params->width, &cairo_params->height, PIXELS);
			else
			    cairo_params->explicit_units = parse_term_size(&cairo_params->width, &cairo_params->height, INCHES);
			if (set_size) duplication=TRUE;
			set_size = TRUE;
			break;
		case CAIROTRM_ROUNDED:
			c_token++;
			if (set_capjoin) duplication=TRUE;
			cairo_params->linecap = ROUNDED;
			set_capjoin = TRUE;
			break;
		case CAIROTRM_BUTT:
			c_token++;
			if (set_capjoin) duplication=TRUE;
			cairo_params->linecap = BUTT;
			set_capjoin = TRUE;
			break;
		case CAIROTRM_SQUARE:
			c_token++;
			if (set_capjoin) duplication=TRUE;
			cairo_params->linecap = SQUARE;
			set_capjoin = TRUE;
			break;
		case CAIROTRM_LINEWIDTH:
			c_token++;
			cairo_params->lw = real_expression();
			if (cairo_params->lw < 0.0)
			    cairo_params->lw = cairo_params_default->lw;
			break;
		case CAIROTRM_DASHED:
		case CAIROTRM_SOLID:
			/* dashes always enabled in version 5 */
			c_token++;
			cairo_params->dashed = TRUE;
			break;
		case CAIROTRM_MONO:
			c_token++;
			cairo_params->mono = TRUE;
			term->flags |= TERM_MONOCHROME;
			break;
		case CAIROTRM_COLOR:
			c_token++;
			cairo_params->mono = FALSE;
			term->flags &= ~TERM_MONOCHROME;
			break;
		case CAIROTRM_DASHLENGTH:
			c_token++;
			cairo_params->dash_length = real_expression();
			if (cairo_params->dash_length < 0.0)
			    cairo_params->dash_length = cairo_params_default->dash_length;
			break;
		case CAIROTRM_FONTSCALE:
			c_token++;
			cairo_params->fontscale = END_OF_COMMAND ? -1 : real_expression();
			if (cairo_params->fontscale <= 0.0)
			    cairo_params->fontscale = cairo_params_default->fontscale;
			/* set_fontscale = TRUE; */
			break;
		case CAIROTRM_TRANSPARENT:
			c_token++;
			cairo_params->transparent = TRUE;
			break;
		case CAIROTRM_NOTRANSPARENT:
			c_token++;
			cairo_params->transparent = FALSE;
			break;
		case CAIROTRM_CROP:
			c_token++;
			if ((cairo_params->terminal == CAIROTERM_PNG) || 
			    (cairo_params->terminal == CAIROTERM_EPS))
			    cairo_params->crop = TRUE;
			break;
		case CAIROTRM_NOCROP:
			c_token++;
			if ((cairo_params->terminal == CAIROTERM_PNG) || 
			    (cairo_params->terminal == CAIROTERM_EPS))
			    cairo_params->crop = FALSE;
			break;
		case CAIROTRM_BACKGROUND:
			{
			int wxt_background;
			c_token++;
			wxt_background = parse_color_name();
			cairo_params->background.r = (double)((wxt_background >> 16) & 0xff) / 255.;
			cairo_params->background.g = (double)((wxt_background >>  8) & 0xff) / 255.;
			cairo_params->background.b = (double)( wxt_background        & 0xff) / 255.;
			cairo_params->transparent = FALSE;
			break;
			}

#ifdef PSLATEX_DRIVER
		case CAIROLATEX_STANDALONE:
			if (!ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s", term->name);
			ps_params->epslatex_standalone = TRUE;
			++c_token;
			if (set_standalone) duplication = TRUE;
			set_standalone = TRUE;
			break;
		case CAIROLATEX_INPUT:
			if (!ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s", term->name);
			ps_params->epslatex_standalone = FALSE;
			++c_token;
			if (set_standalone) duplication = TRUE;
			set_standalone = TRUE;
			break;
		case CAIROLATEX_HEADER:
			if (!ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s", term->name);
			++c_token;
			free(epslatex_header);
			/* Protect against int_error() bail from try_to_get_string() */
			epslatex_header = NULL;
			epslatex_header = try_to_get_string();
			if (!epslatex_header)
				int_error(c_token,"String containing header information expected");
			if (set_header) duplication = TRUE;
			set_header = TRUE;
			break;
		case CAIROLATEX_NOHEADER:
			if (!ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s", term->name);
			free(epslatex_header);
			epslatex_header = NULL;
			if (set_header) duplication = TRUE;
			set_header = TRUE;
			++c_token;
			break;
		case CAIROLATEX_BLACKTEXT:
			if (!ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s",term->name);
			ps_params->blacktext = TRUE;
			c_token++;
			break;
		case CAIROLATEX_COLORTEXT:
			if (!ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s",term->name);
			ps_params->blacktext = FALSE;
			c_token++;
			break;
		case CAIROLATEX_EPS:
			if (!ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s",term->name);
#ifdef HAVE_CAIROEPS
			cairo_params->pdf_output = FALSE;
#else
			int_error(c_token, "eps output not supported");
#endif
			c_token++;
			break;
		case CAIROLATEX_PDF:
			if (!ISCAIROLATEX)
				int_error(c_token,
					  "extraneous argument in set terminal %s",term->name);
			cairo_params->pdf_output = TRUE;
			c_token++;
			break;
#endif /* PSLATEX_DRIVER */

		case CAIROTRM_OTHER:
			/* Ignore irrelevant keywords used by other png/pdf drivers */
			if (almost_equals(c_token,"true$color")
			||  almost_equals(c_token,"inter$lace")
			||  almost_equals(c_token,"nointer$lace")) {
			    c_token++;
			    break;
			}
		default:
			int_error(c_token, "unrecognized terminal option");
			break;
		}

		if (duplication)
			int_error(c_token-1, "Duplicated or contradicting arguments in cairo terminal options.");
	}

#if 0
	/* Copy terminal-dependent values to the generic equivalent */
	cairo_params->width = cairo_params_default->width;
	cairo_params->height = cairo_params_default->height;
	cairo_params->base_linewidth = cairo_params_default->base_linewidth;
#endif

#ifdef PSLATEX_DRIVER
	if (ISCAIROLATEX) {
		sprintf(tmp_term_options, " %s %s",
		    cairo_params->pdf_output ? "pdf" : "eps",
			ps_params->epslatex_standalone ? "standalone" : "input");
		strcat(term_options, tmp_term_options);
		if (epslatex_header)
			sprintf(tmp_term_options, " header \"%s\"", epslatex_header);
		else
			sprintf(tmp_term_options, " noheader");
		strcat(term_options, tmp_term_options);
		sprintf(tmp_term_options, " %s", ps_params->blacktext ? "blacktext" : "colortext");
		strcat(term_options, tmp_term_options);
	}
#endif

	/* Save options back into options string in normalized format */
	if (cairo_params->transparent)
		strncat(term_options, ISCAIROLATEX ? " nobackground" : " transparent", 
			sizeof(term_options) - strlen(term_options)-1);
	else {
		sprintf(tmp_term_options, " background \"#%02x%02x%02x\"", 
			(int)(255 * cairo_params->background.r), 
			(int)(255 * cairo_params->background.g), 
			(int)(255 * cairo_params->background.b));
		strcat(term_options, tmp_term_options);
	}

	if (cairo_params->crop)
		strncat(term_options, " crop", sizeof(term_options)-strlen(term_options)-1);

	strncat(term_options, cairo_params->enhanced ? " enhanced" : " noenhanced",
		sizeof(term_options)-strlen(term_options)-1);

	if (set_font) {
		snprintf(tmp_term_options, sizeof(tmp_term_options), " font \"%s\"", font_setting);
		free(font_setting);
		strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)-1);
	}

	/* if (set_fontscale) */
	{
		snprintf(tmp_term_options, sizeof(tmp_term_options), " fontscale %.1f", cairo_params->fontscale);
		strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)-1);
	}

	if (cairo_params->mono)
		strncat(term_options, " monochrome", sizeof(term_options)-strlen(term_options)-1);

	if (1 || set_size) {
		if (cairo_params->explicit_units == CM)
			snprintf(tmp_term_options,
				sizeof(tmp_term_options),
				" size %.2fcm, %.2fcm ",
				2.54*cairo_params->width/72., 2.54*cairo_params->height/72.);
		else if (cairo_params->explicit_units == PIXELS)
			snprintf(tmp_term_options,
				sizeof(tmp_term_options),
				" size %d, %d ",
				(int)cairo_params->width, (int)cairo_params->height);
		else
			snprintf(tmp_term_options,
				sizeof(tmp_term_options),
				" size %.2fin, %.2fin ",
				cairo_params->width/72., cairo_params->height/72.);
		strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)-1);
	}

	if (set_capjoin) {
		strncat(term_options, 
			cairo_params->linecap == ROUNDED ? " rounded" 
			: cairo_params->linecap == BUTT ? " butt"
			: " square",
			sizeof(term_options)-strlen(term_options)-1);
	}

	if (cairo_params->lw != cairo_params_default->lw) {
		snprintf(tmp_term_options,sizeof(tmp_term_options), " linewidth %g", cairo_params->lw);
		strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)-1);
	}

	if (cairo_params->dash_length != cairo_params_default->dash_length) {
		snprintf(tmp_term_options,sizeof(tmp_term_options), " dashlength %g", cairo_params->dash_length);
		strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)-1);
	}

	/* sync settings with ps_params for latex terminal */
#ifdef PSLATEX_DRIVER
	if (ISCAIROLATEX) {
		ps_params->color = !cairo_params->mono;
		ps_params->font[sizeof(ps_params->font) - 1] = '\0';
		strncpy(ps_params->font, cairo_params->fontname, sizeof(ps_params->font) - 1);
		ps_params->fontsize = cairo_params->fontsize;
		ps_params->fontscale = cairo_params->fontscale;
		if (cairo_params->transparent) {
			ps_params->background.r = -1;
			ps_params->background.g = -1;
			ps_params->background.b = -1;
		} else {
			ps_params->background.r = cairo_params->background.r;
			ps_params->background.g = cairo_params->background.g;
			ps_params->background.b = cairo_params->background.b;
		}
	}
#endif
}

/* "Called once, when the device is first selected."
 * Is the 'main' function of the terminal. */
void cairotrm_init()
{
	cairo_surface_t *surface = NULL;

	FPRINTF((stderr,"Init\n"));

	/* do a sanity check for once */
	if (strcmp(term->name, "epscairo") && strcmp(term->name, "cairolatex") && 
	    strcmp(term->name, "pdfcairo") && strcmp(term->name, "pngcairo"))
		int_error(c_token,"Unrecognized cairo terminal");

	/* cairolatex requires a file */
	if (ISCAIROLATEX && !outstr)
	    os_error(c_token, "cairolatex terminal cannot write to standard output");

	/* initialisations */
	gp_cairo_initialize_plot(&plot);
	plot.device_xmax = (double) cairo_params->width;
	plot.device_ymax = (double) cairo_params->height;
	plot.dashlength = cairo_params->dash_length;

	/* is supposed to be done in cairotrm_reset */
	if ( plot.cr )
		cairo_destroy(plot.cr);

	if (!strcmp(term->name,"pdfcairo")) {
		/* Output can either be a file or stdout */
		if (!outstr) {
		    surface = cairo_pdf_surface_create_for_stream(
			(cairo_write_func_t)cairostream_write, cairostream_error,
				plot.device_xmax /*double width_in_points*/,
				plot.device_ymax /*double height_in_points*/);
		} else {
		    surface = cairo_pdf_surface_create(outstr,
				plot.device_xmax /*double width_in_points*/,
				plot.device_ymax /*double height_in_points*/);
		}
		/* it is up to the pdf viewer to do the hinting */
		plot.hinting = 0;
		/* disable OPERATOR_SATURATE, not implemented in cairo pdf backend,
		 * results in bitmap fallback. However, polygons are drawn with seams
		 * between each other. */
		plot.polygons_saturate = FALSE;
		/* Empirical correction to make pdf output look more like wxt and png */
		plot.dashlength /= 2;
	} else if (!strcmp(term->name,"pngcairo")) {
		surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32,
				plot.device_xmax /*double width_in_points*/,
				plot.device_ymax /*double height_in_points*/);
		/* png is bitmapped, let's do the full hinting */
		plot.hinting = 100;
		/* png is produced by cairo "image" backend, which has full support
		 * of OPERATOR_SATURATE */
		plot.polygons_saturate = TRUE;
	}
#ifdef HAVE_CAIROEPS
	else if (!strcmp(term->name,"epscairo")) {
		/* Output can either be a file or stdout */
		if (!outstr) {
		    surface = cairo_ps_surface_create_for_stream(
			(cairo_write_func_t)cairostream_write, cairostream_error,
				plot.device_xmax /*double width_in_points*/,
				plot.device_ymax /*double height_in_points*/);
		} else {
		    surface = cairo_ps_surface_create(outstr,
				plot.device_xmax /*double width_in_points*/,
				plot.device_ymax /*double height_in_points*/);
		}
		cairo_ps_surface_set_eps(surface, TRUE);
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,11,1)
		if (!cairo_params->crop) {
			/* Normally, cairo clips the bounding box of eps files, but
			   newer versions of Cairo let us enforce the bounding box. See
			   http://old.nabble.com/EPS-target%3A-force-boundingbox-td29050435.html */
			char bb[100];
 			sprintf(bb, "%%%%BoundingBox: 0 0 %i %i", plot.device_xmax, plot.device_ymax);
			cairo_ps_surface_dsc_comment(surface, bb);
			cairo_ps_surface_dsc_begin_page_setup(surface);
			sprintf(bb, "%%%%PageBoundingBox: 0 0 %i %i", plot.device_xmax, plot.device_ymax);
			cairo_ps_surface_dsc_comment(surface, bb);
		}
#endif
		/* it is up to the pdf viewer to do the hinting */
		plot.hinting = 0;
		/* disable OPERATOR_SATURATE, not implemented in cairo pdf backend,
		 * results in bitmap fallback. However, polygons are drawn with seams
		 * between each other. */
		plot.polygons_saturate = FALSE;
		/* Empirical correction to make pdf output look more like wxt and png */
		plot.dashlength /= 2;
	}
#endif /* HAVE_CAIROEPS */
#ifdef PSLATEX_DRIVER
	else if (ISCAIROLATEX) {
		EPSLATEX_reopen_output(cairo_params->pdf_output ? "pdf" : "eps");

		/* Output can only go to a stream */
		if (cairo_params->pdf_output) {
			surface = cairo_pdf_surface_create_for_stream(
				(cairo_write_func_t)cairostream_write, cairostream_error,
				plot.device_xmax /*double width_in_points*/,
				plot.device_ymax /*double height_in_points*/);
		} 
#ifdef HAVE_CAIROEPS
		else {
			surface = cairo_ps_surface_create_for_stream(
				(cairo_write_func_t)cairostream_write, cairostream_error,
				plot.device_xmax /*double width_in_points*/,
				plot.device_ymax /*double height_in_points*/);
			cairo_ps_surface_set_eps(surface, TRUE);
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,11,1)
			{
				/* Always enforce the bounding box. */
				char bb[100];
 				sprintf(bb, "%%%%BoundingBox: 0 0 %i %i", plot.device_xmax, plot.device_ymax);
				cairo_ps_surface_dsc_comment(surface, bb);
				cairo_ps_surface_dsc_begin_page_setup(surface);
				sprintf(bb, "%%%%PageBoundingBox: 0 0 %i %i", plot.device_xmax, plot.device_ymax);
				cairo_ps_surface_dsc_comment(surface, bb);
			}
#endif
		}
#endif
		/* it is up to the pdf viewer to do the hinting */
		plot.hinting = 0;
		/* disable OPERATOR_SATURATE, not implemented in cairo pdf backend,
		 * results in bitmap fallback. However, polygons are drawn with seams
		 * between each other. */
		plot.polygons_saturate = FALSE;
		/* Empirical correction to make pdf output look more like wxt and png */
		plot.dashlength /= 2;
	}
#endif

	plot.cr = cairo_create(surface);
	cairo_surface_destroy( surface );

	FPRINTF((stderr,"status = %s\n",cairo_status_to_string(cairo_status(plot.cr))));
	FPRINTF((stderr,"Init finished \n"));
}

/* "Called just before a plot is going to be displayed."
 * Should clear the terminal. */
void cairotrm_graphics()
{
	/* Initialize background */
	plot.background.r = cairo_params->background.r;
	plot.background.g = cairo_params->background.g;
	plot.background.b = cairo_params->background.b;
	gp_cairo_set_background(cairo_params->background);
	if (ISCAIROLATEX || cairo_params->transparent)
		gp_cairo_clear_background(&plot);
	else
		gp_cairo_solid_background(&plot);

	/* update the window scale factor first, cairo needs it */
	plot.xscale = 1.0;
	plot.yscale = 1.0;

	/* update graphics state properties */
	plot.linecap = cairo_params->linecap;

	FPRINTF((stderr,"Graphics1\n"));

	/* set the transformation matrix of the context, and other details */
	/* depends on plot.xscale and plot.yscale */
	gp_cairo_initialize_context(&plot);

	/* set or refresh terminal size according to the window size */
	/* oversampling_scale is updated in gp_cairo_initialize_context */
	term->xmax = (unsigned int) plot.device_xmax*plot.oversampling_scale;
	term->ymax = (unsigned int) plot.device_ymax*plot.oversampling_scale;
	term->tscale = plot.oversampling_scale;
	plot.xmax = term->xmax;
	plot.ymax = term->ymax;
	/* initialize encoding */
	plot.encoding = encoding;

	/* set font details (h_char, v_char) according to settings */
	cairotrm_set_font("");

	term->v_tic = (unsigned int) (term->v_char/2.5);
	term->h_tic = (unsigned int) (term->v_char/2.5);

#ifdef PSLATEX_DRIVER
	/* Init latex output, requires terminal size */
	if (ISCAIROLATEX)
		EPSLATEX_common_init();
#endif

#if CAIRO_VERSION < CAIRO_VERSION_ENCODE(1,11,1)
	/* Put "invisible" points in two corners to enforce bounding box. */
	if ((ISCAIROLATEX) || ((cairo_params->terminal == CAIROTERM_EPS) && !cairo_params->crop)) {
		cairo_set_line_width(plot.cr, 1);
		cairo_set_source_rgb(plot.cr, cairo_params->background.r,
				cairo_params->background.g, cairo_params->background.b);
		cairo_move_to(plot.cr, 0, 0);
		cairo_line_to(plot.cr, 1, 0);
		cairo_move_to(plot.cr, plot.xmax - 1, plot.ymax);
		cairo_line_to(plot.cr, plot.xmax, plot.ymax);
		cairo_stroke(plot.cr);
	}
#endif

	FPRINTF((stderr,"Graphics xmax %d ymax %d v_char %d h_char %d\n",
		term->xmax, term->ymax, term->v_char, term->h_char));
}


/* cairo mechanism to allow writing to an output stream */
cairo_status_t
cairostream_write(void *closure, unsigned char *data, unsigned int length)
{
	/* in case of a cairolatex terminal, output is redirected to secondary file */
	if (length != fwrite(data, 1, length, (ISCAIROLATEX) ? gppsfile : gpoutfile))
		return CAIRO_STATUS_WRITE_ERROR;
	return CAIRO_STATUS_SUCCESS;
}

void
cairopng_write_cropped_image(cairo_surface_t *surface)
{
  unsigned char *data = cairo_image_surface_get_data(surface);
  int width = cairo_image_surface_get_width(surface);
  int height = cairo_image_surface_get_height(surface);
  int stride = cairo_image_surface_get_stride(surface);
  int  i, j, x1 = 0, y1 = 0, x2 = width, y2 = height;

  guint32 *row;
  guint32 BG = (cairo_params->transparent) ? 0x0 : ~0x0;

  /* Row-wise, top-down iteration */
  for (i = 0; i < height; i++) {
    row = (guint32 *) (data + i * stride);
    for (j = 0; j < width; j++) {
      if (row[j] != BG) {
	y1 = i;
	goto found_y1;
      }
    }
  }
 found_y1:
  /* Row-wise, bottom-up iteration */
  for (i = height - 1; i >= y1; i--) {
    row = (guint32 *)(data + i * stride);
    for (j = 0; j < width; j++) {
      if (row[j] != BG) {
	y2 = i;
	goto found_y2;
      }
    }
  }
 found_y2:
  /* Column-wise, left-to-right iteration */
  for (j = 0; j < width; j++) {
    for (i = y1; i <= y2; i++) {
      row = (guint32 *) (data + i * stride);
      if (row[j] != BG) {
	x1 = j;
	goto found_x1;
      }
    }
  }
 found_x1:
  /* Column-wise, right-to-left iteration */
  for (j = width - 1; j >= x1; j--) {
    for (i = y1; i <= y2; i++) {
      row = (guint32 *) (data + i * stride);
      if (row[j] != BG) {
	x2 = j;
	goto found_x2;
      }
    }
  }
 found_x2:
  {
  const int padding = 10;
  int clip_width = GPMIN(x2 - x1 + padding, width);
  int clip_height = GPMIN(y2 - y1 + padding, height);
  cairo_surface_t *clip = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 
  			clip_width, clip_height);
  cairo_t *clip_cr = cairo_create(clip);
  cairo_set_source_surface(clip_cr, surface,
			-GPMAX(x1 - padding / 2, 0),
			-GPMAX(y1 - padding / 2, 0));
  cairo_rectangle(clip_cr, 0, 0, clip_width, clip_height);
  cairo_fill(clip_cr);

    /* Write out the clipped version of the surface and clean up */
    cairo_surface_write_to_png_stream(clip,
	    (cairo_write_func_t)cairostream_write, cairostream_error);
    cairo_surface_destroy(clip);
    cairo_destroy(clip_cr);
  }
}

void cairotrm_text()
{
	FPRINTF((stderr,"Text0\n"));

	/* don't forget to stroke the last path if vector was the last command */
	gp_cairo_stroke(&plot);
	/* and don't forget to draw the polygons if draw_polygon was the last command */
	gp_cairo_end_polygon(&plot);

	FPRINTF((stderr,"status = %s\n",cairo_status_to_string(cairo_status(plot.cr))));

	/* finish the page - cairo_destroy still has to be called for the whole documentation
	 * to be written */
	cairo_show_page(plot.cr);
	if (!strcmp(term->name,"pngcairo")) {
	    cairo_surface_t *surface = cairo_get_target(plot.cr);
	    if (cairo_params->crop) {
		cairopng_write_cropped_image(surface);
	    } else {
		cairo_surface_write_to_png_stream(surface,
			(cairo_write_func_t)cairostream_write, cairostream_error);
	    }
	}

	FPRINTF((stderr,"status = %s\n",cairo_status_to_string(cairo_status(plot.cr))));

	FPRINTF((stderr,"Text finished\n"));
}

/* sent when gnuplot exits and when the terminal or the output change.*/
void cairotrm_reset()
{
	/* finish the document */
	cairo_destroy(plot.cr);

#ifdef PSLATEX_DRIVER
	/* finish latex output */
	if (ISCAIROLATEX)
		PSLATEX_reset();
#endif

	FPRINTF((stderr,"cairotrm_reset\n"));
}

void cairotrm_move(unsigned int x, unsigned int y)
{
	gp_cairo_move(&plot, x, term->ymax - y);
}

void cairotrm_vector(unsigned int x, unsigned int y)
{
	gp_cairo_vector(&plot, x, term->ymax - y);
}

void cairotrm_put_text(unsigned int x, unsigned int y, const char * string)
{
	if (!string || !strlen(string))
		return;

	/* if ignore_enhanced_text is set, draw with the normal routine.
	 * This is meant to avoid enhanced syntax when the enhanced mode is on */
	if (ignore_enhanced_text || !cairo_params->enhanced) {
		gp_cairo_draw_text(&plot, x, term->ymax - y, string, NULL, NULL);
		return;
	}

	/* If there are no mark-up characters we can use the normal routine */
	if (!strpbrk(string, "{}^_@&~")) {
		gp_cairo_draw_text(&plot, x, term->ymax - y, string, NULL, NULL);
		return;
	}

	/* Uses enhanced_recursion() to analyse the string to print.
	 * enhanced_recursion() calls _enhanced_open() to initialize the text drawing,
	 * then it calls _enhanced_writec() which buffers the characters to draw,
	 * and finally _enhanced_flush() to draw the buffer with the correct justification. */

	gp_cairo_enhanced_init(&plot, strlen(string));

	/* set up the global variables needed by enhanced_recursion() */
	enhanced_fontscale = cairo_params->fontscale;
	strncpy(enhanced_escape_format, "%c", sizeof(enhanced_escape_format));

	/* Set the recursion going. We say to keep going until a
	* closing brace, but we don't really expect to find one.
	* If the return value is not the nul-terminator of the
	* string, that can only mean that we did find an unmatched
	* closing brace in the string. We inplot->crement past it (else
	* we get stuck in an infinite loop) and try again. */

	while (*(string = enhanced_recursion((char*)string, TRUE, 
			plot.fontname, plot.fontsize,
			0.0, TRUE, TRUE, 0))) {
		cairotrm_enhanced_flush();

		/* we can only get here if *str == '}' */
		enh_err_check(string);

		if (!*++string)
			break; /* end of string */
		/* else carry on and process the rest of the string */
	}

	/* finish */
	gp_cairo_enhanced_finish(&plot, x, term->ymax - y);
	return;

}

void cairotrm_enhanced_flush()
{
	gp_cairo_enhanced_flush(&plot);
}

void cairotrm_enhanced_writec(int c)
{
	gp_cairo_enhanced_writec(&plot, c);
}

void cairotrm_enhanced_open(char* fontname, double fontsize, double base, TBOOLEAN widthflag, TBOOLEAN showflag, int overprint)
{
	gp_cairo_enhanced_open(&plot, fontname, fontsize, 
				base, widthflag, showflag, overprint);
}

void cairotrm_linetype(int lt)
{
	gp_cairo_set_linetype(&plot, lt);

	/* Version 5: always set solid lines at this point.  If a dashed */
	/* line is wanted there will be a later call to term->dashtype() */
	gp_cairo_set_linestyle(&plot,  GP_CAIRO_SOLID);

	if (cairo_params->mono && lt >= -1)
	    gp_cairo_set_color(&plot, gp_cairo_linetype2color( -1 ), 0.0);
	else
	    gp_cairo_set_color(&plot, gp_cairo_linetype2color( lt ), 0.0);

}


/* - fonts are selected as strings "name,size".
 * - _set_font("") restores the terminal's default font.*/
int cairotrm_set_font (const char *font)
{
	char fontname[MAX_ID_LEN + 1] = "";
	int fontsize = 0;

	if (!font || !(*font)) {
		strncpy(fontname, "", sizeof(fontname));
		fontsize = 0;
	} else {
		int sep;

		sep = strcspn(font,",");
		if (sep > 0) {
			strncpy(fontname, font, sep);
			fontname[sep] = '\0';
		}
		if (font[sep] == ',')
			sscanf(&(font[sep+1]), "%d", &fontsize);
	}

	if ( strlen(fontname) == 0 ) {
		if ( strlen(cairo_params->fontname) == 0 )
			strncpy(fontname, CAIROTRM_DEFAULT_FONTNAME, sizeof(fontname));
		else
			strncpy(fontname, cairo_params->fontname, sizeof(fontname));
	}

	if ( fontsize == 0 ) {
		if ( cairo_params->fontsize == 0 )
			fontsize = cairo_params_default->fontsize;
		else
			fontsize = cairo_params->fontsize;
	}

	/* Reset the term variables (hchar, vchar, h_tic, v_tic).
	 * They may be taken into account in next plot commands */
	gp_cairo_set_font(&plot, fontname, fontsize * cairo_params->fontscale);
	gp_cairo_set_termvar(&plot, &(term->v_char),
				    &(term->h_char));

	/* the returned int is not used anywhere */
	return 1;
}
	

int cairotrm_justify_text(enum JUSTIFY mode)
{
	gp_cairo_set_justify(&plot,mode);

	return 1; /* we can justify */
}

void cairotrm_point(unsigned int x, unsigned int y, int pointstyle)
{
	gp_cairo_draw_point(&plot, x, term->ymax - y, pointstyle);
}

void cairotrm_pointsize(double ptsize)
{
	/* same behaviour as x11 terminal */
	if (ptsize<0) ptsize = 1;

	gp_cairo_set_pointsize(&plot, ptsize);
}

void cairotrm_linewidth(double lw)
{
	lw *= cairo_params->lw * cairo_params->base_linewidth;
	gp_cairo_set_linewidth(&plot, lw);
}

int cairotrm_text_angle(int angle)
{
	/* a double is needed to compute cos, sin, etc. */
	gp_cairo_set_textangle(&plot, (double) angle);

	return 1; /* 1 means we can rotate */
}

void cairotrm_fillbox(int style, unsigned int x, unsigned int y, unsigned int width, unsigned int height)
{
	gp_cairo_draw_fillbox(&plot, x, term->ymax - y,
				width, height,
				style);
}

int cairotrm_make_palette(t_sm_palette * palette)
{
	/* we can do continuous colors */
	return 0;
}

void cairotrm_set_color(t_colorspec *colorspec)
{
	rgb_color rgb1;
	double alpha = 0.0;

	if (colorspec->type == TC_LT) {
		rgb1 = gp_cairo_linetype2color(colorspec->lt);
	} else if (colorspec->type == TC_FRAC && cairo_params->mono) {
		int save_colorMode = sm_palette.colorMode;
		sm_palette.colorMode = SMPAL_COLOR_MODE_GRAY;
		rgb1maxcolors_from_gray( colorspec->value, &rgb1 );
		sm_palette.colorMode = save_colorMode;
	} else if (colorspec->type == TC_FRAC) {
		rgb1maxcolors_from_gray( colorspec->value, &rgb1 );
	} else if (colorspec->type == TC_RGB) {
		rgb1.r = (double) ((colorspec->lt >> 16) & 0xff)/255;
		rgb1.g = (double) ((colorspec->lt >> 8) & 0xff)/255;
		rgb1.b = (double) ((colorspec->lt) & 0xff)/255;
		alpha = (double) ((colorspec->lt >> 24) & 0xff)/255;
	} else return;

	gp_cairo_set_color(&plot, rgb1, alpha);
}


/* here we send the polygon command */
void cairotrm_filled_polygon(int n, gpiPoint *corners)
{
	gpiPoint *mirrored_corners = (gpiPoint*) gp_alloc(n*sizeof(gpiPoint),"mirrored_corners");
	/* can't use memcpy() here, as we have to mirror the y axis */
	gpiPoint *corners_copy = mirrored_corners;
	while (corners_copy < (mirrored_corners + n)) {
		*corners_copy = *corners++;
		corners_copy->y = term->ymax - corners_copy->y;
		++corners_copy;
	}

	gp_cairo_draw_polygon(&plot, n, mirrored_corners);
	free(mirrored_corners);
}

void cairotrm_image(unsigned int M, unsigned int N, coordval * image, gpiPoint * corner, t_imagecolor color_mode)
{
	/* This routine is to plot a pixel-based image on the display device.
	'M' is the number of pixels along the y-dimension of the image and
	'N' is the number of pixels along the x-dimension of the image.  The
	coordval pointer 'image' is the pixel values normalized to the range
	[0:1].  These values should be scaled accordingly for the output
	device.  They 'image' data starts in the upper left corner and scans
	along rows finishing in the lower right corner.  If 'color_mode' is
	IC_PALETTE, the terminal is to use palette lookup to generate color
	information.  In this scenario the size of 'image' is M*N.  If
	'color_mode' is IC_RGB, the terminal is to use RGB components.  In
	this scenario the size of 'image' is 3*M*N.  The data appears in RGB
	tripples, i.e., image[0] = R(1,1), image[1] = G(1,1), image[2] =
	B(1,1), image[3] = R(1,2), image[4] = G(1,2), ..., image[3*M*N-1] =
	B(M,N).  The 'image' is actually an "input" image in the sense that
	it must also be properly resampled for the output device.  Many output
	mediums, e.g., PostScript, do this work via various driver functions.
	To determine the appropriate rescaling, the 'corner' information
	should be used.  There are four entries in the gpiPoint data array.
	'corner[0]' is the upper left corner (in terms of plot location) of
	the outer edge of the image.  Similarly, 'corner[1]' is the lower
	right corner of the outer edge of the image.  (Outer edge means the
	outer extent of the corner pixels, not the middle of the corner
	pixels.)  'corner[2]' is the upper left corner of the visible part
	of the image, and 'corner[3]' is the lower right corner of the visible
	part of the image.  The information is provided in this way because
	often it is necessary to clip a portion of the outer pixels of the
	image. */

	/* we will draw an image, scale and resize it */
	/* FIXME add palette support ??? */

	unsigned int *image255 = gp_cairo_helper_coordval_to_chars(image, M, N, color_mode);
	gp_cairo_draw_image(&plot, image255,
			corner[0].x, term->ymax - corner[0].y,
			corner[1].x, term->ymax - corner[1].y,
			corner[2].x, term->ymax - corner[2].y,
			corner[3].x, term->ymax - corner[3].y,
			M, N);
	free(image255);
}

#ifdef EAM_BOXED_TEXT
void cairotrm_boxed_text(unsigned int x, unsigned int y, int option)
{
	if (option == TEXTBOX_INIT)
	    y = term->ymax - y;
	gp_cairo_boxed_text(&plot, x, y, option);
}
#endif

void cairotrm_dashtype(int type, t_dashtype *custom_dash_pattern)
{
	gp_cairo_set_dashtype(&plot, type, custom_dash_pattern);
}

#endif /* TERM_BODY */

#ifdef TERM_TABLE
#ifdef HAVE_CAIROEPS
TERM_TABLE_START (epscairo_driver)
    "epscairo", "eps terminal based on cairo",
    /* the following values are overriden by cairotrm_graphics */
    1 /* xmax */ , 1 /* ymax */ , 1 /* vchar */ , 1 /* hchar */ ,
    1 /* vtic */ , 1 /* htic */ ,
    cairotrm_options, cairotrm_init, cairotrm_reset, cairotrm_text, null_scale, cairotrm_graphics,
    cairotrm_move, cairotrm_vector, cairotrm_linetype, cairotrm_put_text,
    cairotrm_text_angle, cairotrm_justify_text,
    cairotrm_point, do_arrow, cairotrm_set_font,
    cairotrm_pointsize,
    TERM_CAN_MULTIPLOT|TERM_BINARY|TERM_CAN_DASH|TERM_ALPHA_CHANNEL|TERM_LINEWIDTH|TERM_FONTSCALE,
    0 /* suspend */, 0 /* resume */, cairotrm_fillbox, cairotrm_linewidth
#ifdef USE_MOUSE
    , 0, 0, 0, 0, 0
#endif
    , cairotrm_make_palette, 0 /* cairotrm_previous_palette */, cairotrm_set_color, cairotrm_filled_polygon
    , cairotrm_image
    , cairotrm_enhanced_open, cairotrm_enhanced_flush, cairotrm_enhanced_writec
    , 0, 0, 1.0
    , NULL /* hypertext */
#ifdef EAM_BOXED_TEXT
    , cairotrm_boxed_text
#endif 
    , NULL /* modify_plots */
    , cairotrm_dashtype
TERM_TABLE_END (epscairo_driver)

#undef LAST_TERM
#define LAST_TERM epscairo_driver
#endif /* HAVE_CAIROEPS */

#ifdef PSLATEX_DRIVER
TERM_TABLE_START(cairolatex_driver)
    "cairolatex", "LaTeX picture environment using graphicx package and Cairo backend",
    /* the following values are overriden by cairotrm_graphics */
    1 /* xmax */ , 1 /* ymax */ , 1 /* vchar */ , 1 /* hchar */ ,
    1 /* vtic */ , 1 /* htic */ ,
    cairotrm_options, cairotrm_init, cairotrm_reset, cairotrm_text, null_scale, cairotrm_graphics,
    cairotrm_move, cairotrm_vector,
    cairotrm_linetype, EPSLATEX_put_text, 
    PS_text_angle, PS_justify_text,
    cairotrm_point, do_arrow, cairotrm_set_font,
    cairotrm_pointsize,
    TERM_CAN_MULTIPLOT|TERM_BINARY|TERM_CAN_DASH|TERM_ALPHA_CHANNEL|TERM_LINEWIDTH|TERM_FONTSCALE|TERM_IS_LATEX,
    0 /* suspend */, 0 /* resume */, cairotrm_fillbox, cairotrm_linewidth,
#ifdef USE_MOUSE
    0, 0, 0, 0, 0, /* no mouse support for postscript */
#endif
    cairotrm_make_palette, 0 /* cairotrm_previous_palette */, 
    EPSLATEX_set_color, cairotrm_filled_polygon,
    cairotrm_image,
    0, 0, 0, /* Enhanced text mode not used */
    EPSLATEX_layer, /* Used to signal front/back text */
    0, 1.0
    , NULL /* hypertext */
#ifdef EAM_BOXED_TEXT
    , EPSLATEX_boxed_text
#endif 
    , NULL /* modify_plots */
    , cairotrm_dashtype
TERM_TABLE_END(cairolatex_driver)

#undef LAST_TERM
#define LAST_TERM cairolatex_driver
#endif /* PSLATEX_DRIVER */

TERM_TABLE_START (pdfcairo_driver)
    "pdfcairo", "pdf terminal based on cairo",
    /* the following values are overriden by cairotrm_graphics */
    1 /* xmax */ , 1 /* ymax */ , 1 /* vchar */ , 1 /* hchar */ ,
    1 /* vtic */ , 1 /* htic */ ,
    cairotrm_options, cairotrm_init, cairotrm_reset, cairotrm_text, null_scale, cairotrm_graphics,
    cairotrm_move, cairotrm_vector, cairotrm_linetype, cairotrm_put_text,
    cairotrm_text_angle, cairotrm_justify_text,
    cairotrm_point, do_arrow, cairotrm_set_font,
    cairotrm_pointsize,
    TERM_CAN_MULTIPLOT|TERM_BINARY|TERM_CAN_DASH|TERM_ALPHA_CHANNEL|TERM_LINEWIDTH|TERM_FONTSCALE,
    0 /* suspend */, 0 /* resume */, cairotrm_fillbox, cairotrm_linewidth
#ifdef USE_MOUSE
    , 0, 0, 0, 0, 0
#endif
    , cairotrm_make_palette, 0 /* cairotrm_previous_palette */, cairotrm_set_color, cairotrm_filled_polygon
    , cairotrm_image
    , cairotrm_enhanced_open, cairotrm_enhanced_flush, cairotrm_enhanced_writec
    , 0, 0, 1.0
    , NULL /* hypertext */
#ifdef EAM_BOXED_TEXT
    , cairotrm_boxed_text
#endif 
    , NULL /* modify_plots */
    , cairotrm_dashtype
TERM_TABLE_END (pdfcairo_driver)

#undef LAST_TERM
#define LAST_TERM pdfcairo_driver


TERM_TABLE_START (pngcairo_driver)
    "pngcairo", "png terminal based on cairo",
    /* the following values are overriden by cairotrm_graphics */
    1 /* xmax */ , 1 /* ymax */ , 1 /* vchar */ , 1 /* hchar */ ,
    1 /* vtic */ , 1 /* htic */ ,
    cairotrm_options, cairotrm_init, cairotrm_reset, cairotrm_text, null_scale, cairotrm_graphics,
    cairotrm_move, cairotrm_vector, cairotrm_linetype, cairotrm_put_text,
    cairotrm_text_angle, cairotrm_justify_text,
    cairotrm_point, do_arrow, cairotrm_set_font,
    cairotrm_pointsize,
    TERM_BINARY|TERM_CAN_DASH|TERM_ALPHA_CHANNEL|TERM_LINEWIDTH|TERM_FONTSCALE,
    0 /* suspend */, 0 /* resume */, cairotrm_fillbox, cairotrm_linewidth
#ifdef USE_MOUSE
    , 0, 0, 0, 0, 0
#endif
    , cairotrm_make_palette, 0 /* cairotrm_previous_palette */, cairotrm_set_color, cairotrm_filled_polygon
    , cairotrm_image
    , cairotrm_enhanced_open, cairotrm_enhanced_flush, cairotrm_enhanced_writec
    , 0, 0, 1.0
    , NULL /* hypertext */
#ifdef EAM_BOXED_TEXT
    , cairotrm_boxed_text
#endif 
    , NULL /* modify_plots */
    , cairotrm_dashtype
TERM_TABLE_END (pngcairo_driver)

#undef LAST_TERM
#define LAST_TERM pngcairo_driver

#endif /* TERM_TABLE */
#endif /* TERM_PROTO_ONLY */

#ifdef TERM_HELP
START_HELP(epscairo)
"1 epscairo",
"?set terminal epscairo",
"?terminal epscairo",
"?set term epscairo",
"?term epscairo",
"?epscairo",
" The `epscairo` terminal device generates encapsulated PostScript (*.eps) using",
" the cairo and pango support libraries.  cairo version >= 1.6 is required.",
"",
" Please read the help for the `pdfcairo` terminal."
""
END_HELP(epscairo)
#endif /* TERM_HELP */

#ifdef TERM_HELP
START_HELP(cairolatex)
"1 cairolatex",
"?set terminal cairolatex",
"?terminal cairolatex",
"?set term cairolatex",
"?term cairolatex",
"?cairolatex",
" The `cairolatex` terminal device generates encapsulated PostScript (*.eps) or",
" PDF output using the cairo and pango support libraries and uses LaTeX for",
" text output using the same routines as the `epslatex` terminal.",
"",
" Syntax:",
"       set terminal cairolatex",
"                      {eps | pdf}",
"                      {standalone | input}",
"                      {blacktext | colortext | colourtext}",
"                      {header <header> | noheader}",
"                      {mono|color}",
"                      {{no}transparent} {{no}crop} {background <rgbcolor>}",
"                      {font <font>} {fontscale <scale>}",
"                      {linewidth <lw>} {rounded|butt|square} {dashlength <dl>}",
"                      {size <XX>{unit},<YY>{unit}}",
"",
" The cairolatex terminal prints a plot like `terminal epscairo` or",
" `terminal pdfcairo` but transfers the texts to LaTeX instead of including",
" them in the graph. For reference of options not explained here see `pdfcairo`.",
"",
" `eps` and `pdf` select the type of graphics output. Use `eps` with",
" latex/dvips and `pdf` for pdflatex.",
"",
" `blacktext` forces all text to be written in black even in color mode;",
"",
" The `cairolatex` driver offers a special way of controlling text positioning:",
" (a) If any text string begins with '{', you also need to include a '}' at the",
" end of the text, and the whole text will be centered both horizontally",
" and vertically by LaTeX.  (b) If the text string begins with '[', you need",
" to continue it with: a position specification (up to two out of t,b,l,r,c),",
" ']{', the text itself, and finally, '}'. The text itself may be anything",
" LaTeX can typeset as an LR-box. \\rule{}{}'s may help for best positioning.",
" See also the documentation for the `pslatex` terminal driver.",
" To create multiline labels, use \\shortstack, for example",
"    set ylabel '[r]{\\shortstack{first line \\\\ second line}}'",
"",
" The `back` option of `set label` commands is handled slightly different",
" than in other terminals. Labels using 'back' are printed behind all other",
" elements of the plot while labels using 'front' are printed above",
" everything else.",
"",
" The driver produces two different files, one for the eps or pdf part of the",
" figure and one for the LaTeX part. The name of the LaTeX file is taken from",
" the `set output` command. The name of the eps/pdf file is derived by",
" replacing the file extension (normally '.tex') with '.eps' or '.pdf' instead.",
" There is no LaTeX output if no output file is given!  Remember to close the",
" `output file` before next plot unless in `multiplot` mode.",
"",
" In your LaTeX documents use '\\input{filename}' to include the figure.",
" The '.eps' or '.pdf' file is included by the command \\includegraphics{...},",
" so you must also include \\usepackage{graphicx} in the LaTeX preamble.  If",
" you want to use coloured text (option `colourtext`) you also have to include",
" \\usepackage{color} in the LaTeX preamble.",
"",
" The behaviour concerning font selection depends on the header mode.",
" In all cases, the given font size is used for the calculation of proper",
" spacing. When not using the `standalone` mode the actual LaTeX font and",
" font size at the point of inclusion is taken, so use LaTeX commands for",
" changing fonts. If you use e.g. 12pt as font size for your LaTeX",
" document, use '\", 12\"' as options. The font name is ignored. If using",
" `standalone` the given font and font size are used, see below for a",
" detailed description.",
"",
" If text is printed coloured is controlled by the TeX booleans \\ifGPcolor",
" and \\ifGPblacktext. Only if \\ifGPcolor is true and \\ifGPblacktext is",
" false, text is printed coloured. You may either change them in the",
" generated TeX file or provide them globally in your TeX file, for example",
" by using",
"    \\newif\\ifGPblacktext",
"    \\GPblacktexttrue",
" in the preamble of your document. The local assignment is only done if no",
" global value is given.",
"",
" When using the cairolatex terminal give the name of the TeX file in the",
" `set output` command including the file extension (normally \".tex\").",
" The graph filename is generated by replacing the extension.",
"",
" If using the `standalone` mode a complete LaTeX header is added to the",
" LaTeX file; and \"-inc\" is added to the filename of the gaph file.",
" The `standalone` mode generates a TeX file that produces",
" output with the correct size when using dvips, pdfTeX, or VTeX.",
" The default, `input`, generates a file that has to be included into a",
" LaTeX document using the \\input command.",
"",
" If a font other than \"\" or \"default\" is given it is interpreted as",
" LaTeX font name.  It contains up to three parts, separated by a comma:",
" 'fontname,fontseries,fontshape'.  If the default fontshape or fontseries",
" are requested, they can be omitted.  Thus, the real syntax for the fontname",
" is '{fontname}{,fontseries}{,fontshape}'.  The naming convention for all",
" parts is given by the LaTeX font scheme.  The fontname is 3 to 4 characters",
" long and is built as follows: One character for the font vendor, two",
" characters for the name of the font, and optionally one additional",
" character for special fonts, e.g., 'j' for fonts with old-style numerals",
" or 'x' for expert fonts. The names of many fonts is described in",
"^ <a href=\"http://www.tug.org/fontname/fontname.pdf\">",
"           http://www.tug.org/fontname/fontname.pdf",
"^ </a>",
" For example, 'cmr' stands for Computer Modern Roman, 'ptm' for Times-Roman,",
" and 'phv' for Helvetica.  The font series denotes the thickness of the",
" glyphs, in most cases 'm' for normal (\"medium\") and 'bx' or 'b' for bold",
" fonts.  The font shape is 'n' for upright, 'it' for italics, 'sl' for",
" slanted, or 'sc' for small caps, in general.  Some fonts may provide",
" different font series or shapes.",
"",
" Examples:",
"",
" Use Times-Roman boldface (with the same shape as in the surrounding text):",
"       set terminal cairolatex font 'ptm,bx'",
" Use Helvetica, boldface, italics:",
"       set terminal cairolatex font 'phv,bx,it'",
" Continue to use the surrounding font in slanted shape:",
"       set terminal cairolatex font ',,sl'",
" Use small capitals:",
"       set terminal cairolatex font ',,sc'",
"",
" By this method, only text fonts are changed. If you also want to change",
" the math fonts you have to use the \"gnuplot.cfg\" file or the `header`",
" option, described below.",
"",
" In `standalone` mode, the font size is taken from the given font size in the",
" `set terminal` command. To be able to use a specified font size, a file",
" \"size<size>.clo\" has to reside in the LaTeX search path.  By default,",
" 10pt, 11pt, and 12pt are supported.  If the package \"extsizes\" is",
" installed, 8pt, 9pt, 14pt, 17pt, and 20pt are added.",
"",
" The `header` option takes a string as argument.  This string is written",
" into the generated LaTeX file.  If using the `standalone` mode, it is",
" written into the preamble, directly before the \\begin{document} command.",
" In the `input` mode, it is placed directly after the \\begingroup command",
" to ensure that all settings are local to the plot.",
"",
" Examples:",
"",
" Use T1 fontencoding, change the text and math font to Times-Roman as well",
" as the sans-serif font to Helvetica:",
"     set terminal cairolatex standalone header \\",
"     \"\\\\usepackage[T1]{fontenc}\\n\\\\usepackage{mathptmx}\\n\\\\usepackage{helvet}\"",
" Use a boldface font in the plot, not influencing the text outside the plot:",
"     set terminal cairolatex input header \"\\\\bfseries\"",
"",
" If the file \"gnuplot.cfg\" is found by LaTeX it is input in the preamble",
" the LaTeX document, when using `standalone` mode.  It can be used for",
" further settings, e.g., changing the document font to Times-Roman,",
" Helvetica, and Courier, including math fonts (handled by \"mathptmx.sty\"):",
"       \\usepackage{mathptmx}",
"       \\usepackage[scaled=0.92]{helvet}",
"       \\usepackage{courier}",
" The file \"gnuplot.cfg\" is loaded before the header information given",
" by the `header` command.  Thus, you can use `header` to overwrite some of",
" settings performed using \"gnuplot.cfg\"",
""
END_HELP(cairolatex)
#endif /* TERM_HELP */

#ifdef TERM_HELP
START_HELP(pdfcairo)
"1 pdfcairo",
"?set terminal pdfcairo",
"?terminal pdfcairo",
"?set term pdfcairo",
"?term pdfcairo",
"?pdfcairo",
" The `pdfcairo` terminal device generates output in pdf. The actual",
" drawing is done via cairo, a 2D graphics library, and pango, a library for",
" laying out and rendering text.",
"",
" Syntax:",
"         set term pdfcairo",
"                      {{no}enhanced} {mono|color}",
"                      {font <font>} {fontscale <scale>}",
"                      {linewidth <lw>} {rounded|butt|square} {dashlength <dl>}",
"                      {background <rgbcolor>}",
"                      {size <XX>{unit},<YY>{unit}}",
"",
" This terminal supports an enhanced text mode, which allows font and other",
" formatting commands (subscripts, superscripts, etc.) to be embedded in labels",
" and other text strings. The enhanced text mode syntax is shared with other",
" gnuplot terminal types. See `enhanced` for more details.",
"",
" The width of all lines in the plot can be modified by the factor <lw>",
" specified in `linewidth`. The default linewidth is 0.25 points.",
" (1 \"PostScript\" point = 1/72 inch = 0.353 mm)",
"",
" `rounded` sets line caps and line joins to be rounded;",
" `butt` is the default, butt caps and mitered joins.",
"",
" The default size for the output is 5 inches x 3 inches. The `size` option",
" changes this to whatever the user requests. By default the X and Y sizes are",
" taken to be in inches, but other units are possible (currently only cm).",
" Screen coordinates always run from 0.0 to 1.0 along the full length of the",
" plot edges as specified by the `size` option.",
"",
" <font> is in the format \"FontFace,FontSize\", i.e. the face and the size",
" comma-separated in a single string. FontFace is a usual font face name, such",
" as \'Arial\'. If you do not provide FontFace, the pdfcairo terminal will use",
" \'Sans\'. FontSize is the font size, in points. If you do not provide it,",
" the pdfcairo terminal will use a nominal font size of 12 points.",
" However, the default fontscale parameter for this terminal is 0.5,",
" so the apparent font size is smaller than this if the pdf output is",
" viewed at full size.",
"    For example :",
"       set term pdfcairo font \"Arial,12\"",
"       set term pdfcairo font \"Arial\" # to change the font face only",
"       set term pdfcairo font \",12\" # to change the font size only",
"       set term pdfcairo font \"\" # to reset the font name and size",
"",
" The fonts are retrieved from the usual fonts subsystems. Under Windows,",
" those fonts are to be found and configured in the entry \"Fonts\" of the",
" control panel. Under UNIX, they are handled by \"fontconfig\".",
"",
" Pango, the library used to layout the text, is based on utf-8. Thus, the pdfcairo",
" terminal has to convert from your encoding to utf-8. The default input",
" encoding is based on your \'locale\'. If you want to use another encoding,",
" make sure gnuplot knows which one you are using. See `encoding` for more",
" details.",
"",
" Pango may give unexpected results with fonts that do not respect the unicode",
" mapping. With the Symbol font, for example, the pdfcairo terminal will use the map",
" provided by http://www.unicode.org/ to translate character codes to unicode.",
" Note that \"the Symbol font\" is to be understood as the Adobe",
" Symbol font, distributed with Acrobat Reader as \"SY______.PFB\".",
" Alternatively, the OpenSymbol font, distributed with OpenOffice.org as",
" \"opens___.ttf\", offers the same characters. Microsoft has distributed a",
" Symbol font (\"symbol.ttf\"), but it has a different character set with",
" several missing or moved mathematic characters. If you experience problems",
" with your default setup (if the demo enhancedtext.dem is not displayed",
" properly for example), you probably have to install one of the Adobe or",
" OpenOffice Symbol fonts, and remove the Microsoft one.",
" Other non-conform fonts, such as \"wingdings\" have been observed working.",
"",
" The rendering of the plot cannot be altered yet. To obtain the best output",
" possible, the rendering involves two mechanisms : antialiasing and",
" oversampling.",
" Antialiasing allows to display non-horizontal and non-vertical lines",
" smoother.",
" Oversampling combined with antialiasing provides subpixel accuracy,",
" so that gnuplot can draw a line from non-integer coordinates. This avoids",
" wobbling effects on diagonal lines ('plot x' for example).",
""
END_HELP(pdfcairo)
#endif /* TERM_HELP */

#ifdef TERM_HELP
START_HELP(pngcairo)
"1 pngcairo",
"?set terminal pngcairo",
"?terminal pngcairo",
"?set term pngcairo",
"?term pngcairo",
"?pngcairo",
" The `pngcairo` terminal device generates output in png. The actual",
" drawing is done via cairo, a 2D graphics library, and pango, a library for",
" laying out and rendering text.",
"",
" Syntax:",
"         set term pngcairo",
"                      {{no}enhanced} {mono|color}",
"                      {{no}transparent} {{no}crop} {background <rgbcolor>",
"                      {font <font>} {fontscale <scale>}",
"                      {linewidth <lw>} {rounded|butt|square} {dashlength <dl>}",
"                      {size <XX>{unit},<YY>{unit}}",
"",
" This terminal supports an enhanced text mode, which allows font and other",
" formatting commands (subscripts, superscripts, etc.) to be embedded in labels",
" and other text strings. The enhanced text mode syntax is shared with other",
" gnuplot terminal types. See `enhanced` for more details.",
"",
" The width of all lines in the plot can be modified by the factor <lw>.",
"",
" `rounded` sets line caps and line joins to be rounded;",
" `butt` is the default, butt caps and mitered joins.",
"",
" The default size for the output is 640 x 480 pixels. The `size` option",
" changes this to whatever the user requests. By default the X and Y sizes are",
" taken to be in pixels, but other units are possible (currently cm and inch).",
" A size given in centimeters or inches will be converted into pixels assuming",
" a resolution of 72 dpi. Screen coordinates always run from 0.0 to 1.0 along",
" the full length of the plot edges as specified by the `size` option.",
"",
" <font> is in the format \"FontFace,FontSize\", i.e. the face and the size",
" comma-separated in a single string. FontFace is a usual font face name, such",
" as \'Arial\'. If you do not provide FontFace, the pngcairo terminal will use",
" \'Sans\'. FontSize is the font size, in points. If you do not provide it,",
" the pngcairo terminal will use a size of 12 points.",
"    For example :",
"       set term pngcairo font \"Arial,12\"",
"       set term pngcairo font \"Arial\" # to change the font face only",
"       set term pngcairo font \",12\" # to change the font size only",
"       set term pngcairo font \"\" # to reset the font name and size",
"",
" The fonts are retrieved from the usual fonts subsystems. Under Windows,",
" those fonts are to be found and configured in the entry \"Fonts\" of the",
" control panel. Under UNIX, they are handled by \"fontconfig\".",
"",
" Pango, the library used to layout the text, is based on utf-8. Thus, the pngcairo",
" terminal has to convert from your encoding to utf-8. The default input",
" encoding is based on your \'locale\'. If you want to use another encoding,",
" make sure gnuplot knows which one you are using. See `encoding` for more",
" details.",
"",
" Pango may give unexpected results with fonts that do not respect the unicode",
" mapping. With the Symbol font, for example, the pngcairo terminal will use the map",
" provided by http://www.unicode.org/ to translate character codes to unicode.",
" Note that \"the Symbol font\" is to be understood as the Adobe",
" Symbol font, distributed with Acrobat Reader as \"SY______.PFB\".",
" Alternatively, the OpenSymbol font, distributed with OpenOffice.org as",
" \"opens___.ttf\", offers the same characters. Microsoft has distributed a",
" Symbol font (\"symbol.ttf\"), but it has a different character set with",
" several missing or moved mathematic characters. If you experience problems",
" with your default setup (if the demo enhancedtext.dem is not displayed",
" properly for example), you probably have to install one of the Adobe or",
" OpenOffice Symbol fonts, and remove the Microsoft one.",
" Other non-conform fonts, such as \"wingdings\" have been observed working.",
"",
" The rendering of the plot cannot be altered yet. To obtain the best output",
" possible, the rendering involves two mechanisms : antialiasing and",
" oversampling.",
" Antialiasing allows to display non-horizontal and non-vertical lines",
" smoother.",
" Oversampling combined with antialiasing provides subpixel accuracy,",
" so that gnuplot can draw a line from non-integer coordinates. This avoids",
" wobbling effects on diagonal lines ('plot x' for example).",
""
END_HELP(pngcairo)
#endif /* TERM_HELP */