-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathcontext.trm
More file actions
2120 lines (1936 loc) · 72.7 KB
/
context.trm
File metadata and controls
2120 lines (1936 loc) · 72.7 KB
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
/* GNUPLOT - context.trm */
/*[
* Copyright (c) 2006-2011, Mojca Miklavec
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
]*/
/*
* AUTHORS:
* Mojca Miklavec
* <mojca.miklavec.lists at gmail.com>
* - author of this file
* - improvements to m-gnuplot.tex
* - mp-gnuplot.mp - metapost macros to support gnuplot-specifics
* inside ConTeXt
*
* Hans Hagen (author of ConTeXt)
* <pragma at wxs.nl>
* - a lot of functionality added to ConTeXt to enable better support
* of Gnuplot module, many bugs fixed
* - author of the module m-gnuplot.tex to support inclusion of Gnuplot
* graphics into ConTeXt
* - constant support on the most tricky issues
* Taco Hoekwater (developer of MetaPost, pdfTeX & LuaTeX)
* <taco at elvenkind.com>
* - some code of this file, many tricks, lots of bug fixes,
* suggestions and testing
* - Hans's right hand in ConTeXt development:
* constant support on even more tricky issues
*
* For questions, suggestions, comments, improvements please contact:
* - Mojca Miklavec <mojca.miklavec.lists at gmail.com> or
* - <gnuplot-context at googlegroups.com>
* - http://wiki.contextgarden.net/Gnuplot
*
* With special thanks to:
* - Peter Münster (ctx) for the initiative
* - Renaud Aubin (ctx) for maintaining the first source code repository
* - Aditya Mahajan (ctx) for some tricky parts in m-gnuplot.tex
* - Timothée Lecomte & Ethan A Merritt (gnuplot) for many suggestion about improving this terminal
* - Bastian Märkisch (gnuplot) for code improvements
*
*/
/*
* GNUPLOT -- context.trm
*
* This driver creates a ConTeXt document with embded metapost (metafun)
* commands and is consequently used for creation of PDF documents.
*
* It is in a way similar to pslatex, but specialized for ConTeXt,
* where it can also be used in the following way:
*
* \usemodule[gnuplot]
*
* \starttext
* \title{Drawing nice graphs with \sc gnuplot}
* \startGNUPLOTscript{sin}
* plot sin(x) t '$\sin(x)$'
* \stopGNUPLOTscript
* \useGNUPLOTgraphic[sin]
* \stoptext
*
* For more information see http://wiki.contextgarden.net/Gnuplot
*
* Mostly based on:
* - default settings copied from LaTeX terminal (because I liked it)
* - path construction & drawing routines from MetaPost terminal
* - advanced functionality with reference to PostScript terminal
*
* Future plans:
* - most important
* - different syntax for font switching
* - improve support for (transparent) binary images
* - add missing functionality:
* - improved support for palettes
* - smooth shading in color bars
* - gouraud shading (if it will be implemented in gnuplot)
* - other color spaces
* - derive a better metapost terminal out of this one (to replace the old one)
*/
#include "driver.h"
#include "pm3d.h"
#ifdef TERM_REGISTER
register_term(context)
#endif
#ifdef TERM_PROTO
TERM_PUBLIC void CONTEXT_options(void);
TERM_PUBLIC void CONTEXT_init(void);
TERM_PUBLIC void CONTEXT_reset(void);
TERM_PUBLIC void CONTEXT_text(void);
TERM_PUBLIC void CONTEXT_graphics(void);
TERM_PUBLIC void CONTEXT_move(unsigned int x, unsigned int y);
TERM_PUBLIC void CONTEXT_vector(unsigned int x, unsigned int y);
TERM_PUBLIC void CONTEXT_linetype(int lt);
TERM_PUBLIC void CONTEXT_put_text(unsigned int x, unsigned int y, const char *str);
TERM_PUBLIC int CONTEXT_text_angle(float ang);
TERM_PUBLIC int CONTEXT_justify_text(enum JUSTIFY mode);
TERM_PUBLIC void CONTEXT_point(unsigned int x, unsigned int y, int number);
TERM_PUBLIC void CONTEXT_arrow(unsigned int sx, unsigned int sy, unsigned int ex, unsigned int ey, int head);
TERM_PUBLIC int CONTEXT_set_font(const char *font); /* "font,size" */
TERM_PUBLIC void CONTEXT_pointsize(double pointsize);
TERM_PUBLIC void CONTEXT_fillbox(int style, unsigned int x1, unsigned int y1, unsigned int width, unsigned int height); /* clear part of multiplot */
TERM_PUBLIC void CONTEXT_fill(int style);
TERM_PUBLIC void CONTEXT_linewidth(double linewidth);
TERM_PUBLIC int CONTEXT_make_palette(t_sm_palette *palette);
/* TERM_PUBLIC void CONTEXT_previous_palette(void); do we need it? */
TERM_PUBLIC void CONTEXT_set_color(t_colorspec *colorspec);
TERM_PUBLIC void CONTEXT_filled_polygon(int points, gpiPoint *corners);
TERM_PUBLIC void CONTEXT_image(unsigned, unsigned, coordval *, gpiPoint *, t_imagecolor);
/* Metapost < 1.750 can only deal with numbers between 2^{-16} and 2^12=4069 */
/* scale is 1cm = 1000 units */
#define CONTEXT_DPCM 1000
#define CONTEXT_DPI (2.54 * CONTEXT_DPCM)
/* default plot size will be 5in x 3in */
#define CONTEXT_XSIZE_VALUE 5
#define CONTEXT_YSIZE_VALUE 3
#define CONTEXT_SIZE_UNIT INCHES
#define CONTEXT_XMAX (CONTEXT_XSIZE_VALUE * CONTEXT_DPI)
#define CONTEXT_YMAX (CONTEXT_YSIZE_VALUE * CONTEXT_DPI)
/* default fontsize: 12pt */
#define CONTEXT_FONTSIZE 12.0
/* default height of char: 12pt by default */
#define CONTEXT_VCHAR (CONTEXT_DPI * CONTEXT_FONTSIZE / 72.27)
/* in ConTeXt 12pt LM font is the default;
* at that size digits are 5.8749847pt wide
* in LaTeX, which assumes 11pt, the ratio is 5.3/11, which is similar */
#define CONTEXT_LM_H_TO_V_RATIO 0.4895
#define CONTEXT_HCHAR (CONTEXT_LM_H_TO_V_RATIO * CONTEXT_VCHAR)
/* default tic size: 3.5bp (chosen to suit the size of plot approximately)
* - in LaTeX it is 5bp
* - in TikZ it is 0.18cm (approximately 5.1bp)
* - in PostScript it is 3.15pt
* - MetaPost uses 5pt or 5bp
*/
#define CONTEXT_HTIC (3.5 * CONTEXT_DPI / 72)
#define CONTEXT_VTIC CONTEXT_HTIC
#endif /* TERM_PROTO */
#ifndef TERM_PROTO_ONLY
#ifdef TERM_BODY
/*
* started counting when the code was included into CVS
*
* major number should change only in case of a complete rewrite or major incompatibility
* minor number should change for every new functionality
* patch number & date should change in every commit
*/
static const char CONTEXT_term_version[] = "1.0";
static const char CONTEXT_term_patch[] = "0";
static const char CONTEXT_term_date[] = "2011-11-05";
static void CONTEXT_params_reset(void);
static void CONTEXT_adjust_dimensions(void);
static void CONTEXT_fontstring_parse(char *from_string, char *to_string, int to_size, double *fontsize);
static void CONTEXT_startpath(void);
static void CONTEXT_endpath(void);
static void CONTEXT_write_palette(t_sm_palette *palette);
static void CONTEXT_write_palette_gradient(gradient_struct *gradient, int cnt);
/* Each number is divided by 100 (1/100th of a point is drawn) */
static int CONTEXT_posx;
static int CONTEXT_posy;
/* remembers where we started the path:
* if we finish it in the same point, the path is closed with --cycle
*
* After I implemented this, the functionality has been added to gnuplot core
* and PostScript terminal uses it, but to be on the safe side,
* I prefer not to add last-minute patches which could potentially break something.
* (the code written here proved to be rather safe & working so far)
* In the next version the core functionality should be integrated & tested.
*/
static int CONTEXT_path_start_x;
static int CONTEXT_path_start_y;
/* fontname, fontsize */
static char CONTEXT_font[MAX_ID_LEN+1] = "";
static double CONTEXT_fontsize = CONTEXT_FONTSIZE;
/* fontname,fontsize to be put next to font labels if needed */
static char CONTEXT_font_explicit[2*MAX_ID_LEN+1] = "";
/* this is only set to >0 if asked for explicitly (for example with set_font(",15")) */
static double CONTEXT_fontsize_explicit = 0.0;
/* the last pointsize used (it will only be changed if it becomes different) */
static double CONTEXT_old_pointsize = 1.0;
/* the last linewidth used (it will only be changed if it becomes different) */
static double CONTEXT_old_linewidth = 1.0;
/* the last linetype used (it will only be changed if it becomes different) */
static int CONTEXT_old_linetype = -3;
/* was the color changed explicitly? */
static TBOOLEAN CONTEXT_color_changed = FALSE;
/* the number of path nodes before a newline (doesn't really matter,
* could be set to 1000; check if any editors have problems with that) */
#define CONTEXT_LINEMAX 4
/* if we're inside a path (unfinished line) then path_count > 0
* (PDF has PDF_pathIsOpen) */
static unsigned int CONTEXT_path_count = 0;
/* this true/false switch is used to help distinguish dots from paths */
static unsigned int CONTEXT_path_is_dot = 0;
/* angle of text rotation */
static int CONTEXT_ang = 0;
/* left/middle/right text justification */
static enum JUSTIFY CONTEXT_justify = LEFT;
#ifdef OS2 /* same constants are defined in os2emx.h */
enum LINEJOIN { LINEJOIN_MITER=LINEJOIN_MITRE, _LINEJOIN_ROUND=LINEJOIN_ROUND, _LINEJOIN_BEVEL=LINEJOIN_BEVEL };
#else
enum LINEJOIN { LINEJOIN_MITER, LINEJOIN_ROUND, LINEJOIN_BEVEL };
#endif
enum LINECAP { LINECAP_BUTT, LINECAP_ROUND, LINECAP_SQUARE };
/* whether points are drawn with metapost or typeset with TeX (easy configurable) */
enum CONTEXT_POINTS { CONTEXT_POINTS_WITH_METAPOST, CONTEXT_POINTS_WITH_TEX };
/* whether images are inline or written out as PNGs and included (in MKII only external work) */
enum CONTEXT_IMAGES { CONTEXT_IMAGES_INLINE, CONTEXT_IMAGES_EXTERNAL };
/* counts the number of graphics */
static int CONTEXT_counter = 0;
/* counts the number of external PNG images */
static int CONTEXT_image_counter = 0;
/* length of basename for storing image name (including path, excluding extension) */
static int CONTEXT_image_filename_length = 0;
/* if name is a path, remember at which index basename starts */
static int CONTEXT_image_filename_start = 0;
static char *CONTEXT_image_filename = NULL;
/* whether images will be external or not */
enum CONTEXT_IMAGES CONTEXT_images = CONTEXT_IMAGES_INLINE;
/* Palette has to be stored for the usage in later plots */
static t_sm_palette *CONTEXT_old_palette;
/*********************/
/* global parameters */
/*********************/
typedef struct CONTEXT_params_t {
double xsize; /* 5in */
double ysize; /* 3in */
size_units unit; /* INCHES */
TBOOLEAN standalone; /* false */
TBOOLEAN timestamp; /* true */
char *header; /* "" */
TBOOLEAN color; /* true */
TBOOLEAN dashed; /* true */
enum LINEJOIN linejoin; /* MITER */
enum LINECAP linecap; /* BUTT */
double scale_dashlength; /* 1.0 */
double scale_linewidth; /* 1.0 */
double scale_text; /* 1.0 */
enum CONTEXT_POINTS points; /* CONTEXT_POINTS_WITH_METAPOST */
enum CONTEXT_IMAGES images; /* CONTEXT_IMAGES_INLINE */
char font[MAX_ID_LEN+1]; /* "" */
double fontsize; /* 12pt */
} CONTEXT_params_t;
#define CONTEXT_PARAMS_DEFAULT { \
CONTEXT_XSIZE_VALUE,\
CONTEXT_YSIZE_VALUE,\
INCHES,\
FALSE,\
TRUE,\
NULL,\
TRUE,\
TRUE,\
LINEJOIN_MITER,\
LINECAP_BUTT,\
1.0,\
1.0,\
1.0,\
CONTEXT_POINTS_WITH_METAPOST,\
CONTEXT_IMAGES_INLINE,\
"",\
CONTEXT_FONTSIZE\
}
static CONTEXT_params_t CONTEXT_params = CONTEXT_PARAMS_DEFAULT;
enum CONTEXT_id {
CONTEXT_OPT_DEFAULT,
CONTEXT_OPT_SIZE,
CONTEXT_OPT_SIZE_DEFAULT,
CONTEXT_OPT_INPUT, CONTEXT_OPT_STANDALONE,
CONTEXT_OPT_TIMESTAMP, CONTEXT_OPT_NOTIMESTAMP,
CONTEXT_OPT_HEADER, CONTEXT_OPT_NOHEADER,
CONTEXT_OPT_COLOR, CONTEXT_OPT_MONOCHROME,
CONTEXT_OPT_DASHED, CONTEXT_OPT_SOLID,
CONTEXT_OPT_LINEJOIN_MITERED, CONTEXT_OPT_LINEJOIN_ROUNDED, CONTEXT_OPT_LINEJOIN_BEVELED,
CONTEXT_OPT_LINECAP_BUTT, CONTEXT_OPT_LINECAP_ROUNDED, CONTEXT_OPT_LINECAP_SQUARED,
CONTEXT_OPT_SCALE_DASHLENGTH, CONTEXT_OPT_SCALE_LINEWIDTH, CONTEXT_OPT_SCALE_TEXT,
CONTEXT_OPT_POINTS_WITH_METAPOST, CONTEXT_OPT_POINTS_WITH_TEX,
CONTEXT_OPT_IMAGES_INLINE, CONTEXT_OPT_IMAGES_EXTERNAL,
CONTEXT_OPT_DEFAULTFONT,
CONTEXT_OPT_FONT,
CONTEXT_OPT_OTHER
};
static struct gen_table CONTEXT_opts[] = {
{ "d$efault", CONTEXT_OPT_DEFAULT },
{ "size", CONTEXT_OPT_SIZE },
{ "defaultsize", CONTEXT_OPT_SIZE_DEFAULT },
{ "inp$ut", CONTEXT_OPT_INPUT },
{ "stand$alone", CONTEXT_OPT_STANDALONE },
{ "time$stamp", CONTEXT_OPT_TIMESTAMP },
{ "notime$stamp", CONTEXT_OPT_NOTIMESTAMP },
{ "header", CONTEXT_OPT_HEADER },
{ "noheader", CONTEXT_OPT_NOHEADER },
{ "col$or", CONTEXT_OPT_COLOR },
{ "col$our", CONTEXT_OPT_COLOR },
{ "mono$chrome", CONTEXT_OPT_MONOCHROME },
{ "da$shed", CONTEXT_OPT_DASHED },
{ "so$lid", CONTEXT_OPT_SOLID },
{ "miter$ed", CONTEXT_OPT_LINEJOIN_MITERED },
{ "rounded", CONTEXT_OPT_LINEJOIN_ROUNDED },
{ "bevel$ed", CONTEXT_OPT_LINEJOIN_BEVELED },
{ "butt", CONTEXT_OPT_LINECAP_BUTT },
{ "round", CONTEXT_OPT_LINECAP_ROUNDED },
{ "square$d", CONTEXT_OPT_LINECAP_SQUARED },
{ "dashl$ength", CONTEXT_OPT_SCALE_DASHLENGTH },
{ "dl", CONTEXT_OPT_SCALE_DASHLENGTH },
{ "linew$idth", CONTEXT_OPT_SCALE_LINEWIDTH },
{ "lw", CONTEXT_OPT_SCALE_LINEWIDTH },
{ "fontscale", CONTEXT_OPT_SCALE_TEXT },
{ "textscale", CONTEXT_OPT_SCALE_TEXT }, /* backward compatibility */
{ "mp$points", CONTEXT_OPT_POINTS_WITH_METAPOST},
{ "tex$points", CONTEXT_OPT_POINTS_WITH_TEX},
{ "pointswithmp", CONTEXT_OPT_POINTS_WITH_METAPOST}, /* (removable) backward compatibility */
{ "pointswithmetapost", CONTEXT_OPT_POINTS_WITH_METAPOST}, /* (removable) backward compatibility */
{ "pointswithtex", CONTEXT_OPT_POINTS_WITH_TEX}, /* (removable) backward compatibility */
{ "inline$images", CONTEXT_OPT_IMAGES_INLINE},
{ "external$images", CONTEXT_OPT_IMAGES_EXTERNAL},
{ "font", CONTEXT_OPT_FONT },
{ "defaultfont", CONTEXT_OPT_DEFAULTFONT },
{ NULL, CONTEXT_OPT_OTHER }
};
/* **********************
* CONTEXT_params_reset *
* **********************
*
* Resets all parameters of the terminal to their default value.
*/
static void
CONTEXT_params_reset()
{
static const CONTEXT_params_t CONTEXT_params_default = CONTEXT_PARAMS_DEFAULT;
/* free the memory with header first */
if (CONTEXT_params.header) {
free(CONTEXT_params.header);
CONTEXT_params.header = NULL;
}
memcpy(&CONTEXT_params, &CONTEXT_params_default, sizeof(CONTEXT_params_t));
}
/* *****************
* CONTEXT_options *
* *****************
*
* Parses "set term context [options]".
*/
TERM_PUBLIC void
CONTEXT_options()
{
char *tmp_string;
char tmp_term_options[MAX_LINE_LEN+1] = "";
while (!END_OF_COMMAND) {
switch (lookup_table(&CONTEXT_opts[0], c_token)) {
case CONTEXT_OPT_DEFAULT:
c_token++;
/* there should be a better way to do it, but I don't know how */
CONTEXT_params_reset();
break;
case CONTEXT_OPT_SIZE: {
float xmax_t, ymax_t;
size_units unit;
c_token++;
/* size <xsize> {cm|in}, <ysize> {cm|in} */
unit = parse_term_size(&xmax_t, &ymax_t, CM);
CONTEXT_params.xsize = (double)xmax_t / gp_resolution;
CONTEXT_params.ysize = (double)ymax_t / gp_resolution;
CONTEXT_params.unit = unit;
if (unit == CM) {
CONTEXT_params.xsize *= 2.54;
CONTEXT_params.ysize *= 2.54;
}
break;
}
case CONTEXT_OPT_SIZE_DEFAULT:
c_token++;
CONTEXT_params.xsize = CONTEXT_XSIZE_VALUE;
CONTEXT_params.ysize = CONTEXT_YSIZE_VALUE;
CONTEXT_params.unit = CONTEXT_SIZE_UNIT;
break;
case CONTEXT_OPT_INPUT:
c_token++;
CONTEXT_params.standalone = FALSE;
break;
case CONTEXT_OPT_STANDALONE:
c_token++;
CONTEXT_params.standalone = TRUE;
break;
case CONTEXT_OPT_TIMESTAMP:
c_token++;
CONTEXT_params.timestamp = TRUE;
break;
case CONTEXT_OPT_NOTIMESTAMP:
c_token++;
CONTEXT_params.timestamp = FALSE;
break;
case CONTEXT_OPT_HEADER:
c_token++;
/* parse the string */
tmp_string = try_to_get_string();
if (!tmp_string) {
int_error(c_token,"String containing header information expected");
/* only touch the options if the string is OK */
} else {
/* remove the old header if any */
if (CONTEXT_params.header) {
free(CONTEXT_params.header);
CONTEXT_params.header = NULL;
}
/* and set the new one if nonempty;
* empty header will be treated as 'noheader' */
if (strlen(tmp_string) > 0) {
CONTEXT_params.header = tmp_string;
} else {
free(tmp_string);
}
}
break;
case CONTEXT_OPT_NOHEADER:
c_token++;
/* delete the header if it exists */
if (CONTEXT_params.header) {
free(CONTEXT_params.header);
CONTEXT_params.header = NULL;
}
break;
case CONTEXT_OPT_COLOR:
c_token++;
CONTEXT_params.color = TRUE;
/* just mimic other terminals; no idea what it does;
at the moment monochrome is not fully implemented either */
term->flags &= ~TERM_MONOCHROME;
break;
case CONTEXT_OPT_MONOCHROME:
c_token++;
CONTEXT_params.color = FALSE;
term->flags |= TERM_MONOCHROME;
break;
case CONTEXT_OPT_DASHED:
c_token++;
CONTEXT_params.dashed = TRUE;
break;
case CONTEXT_OPT_SOLID:
c_token++;
CONTEXT_params.dashed = FALSE;
break;
case CONTEXT_OPT_LINEJOIN_MITERED:
c_token++;
CONTEXT_params.linejoin = LINEJOIN_MITER;
break;
case CONTEXT_OPT_LINEJOIN_ROUNDED:
c_token++;
CONTEXT_params.linejoin = LINEJOIN_ROUND;
break;
case CONTEXT_OPT_LINEJOIN_BEVELED:
c_token++;
CONTEXT_params.linejoin = LINEJOIN_BEVEL;
break;
case CONTEXT_OPT_LINECAP_BUTT:
c_token++;
CONTEXT_params.linecap = LINECAP_BUTT;
break;
case CONTEXT_OPT_LINECAP_ROUNDED:
c_token++;
CONTEXT_params.linecap = LINECAP_ROUND;
break;
case CONTEXT_OPT_LINECAP_SQUARED:
c_token++;
CONTEXT_params.linecap = LINECAP_SQUARE;
break;
case CONTEXT_OPT_SCALE_DASHLENGTH:
c_token++;
CONTEXT_params.scale_dashlength = real_expression();
break;
case CONTEXT_OPT_SCALE_LINEWIDTH:
c_token++;
CONTEXT_params.scale_linewidth = real_expression();
break;
case CONTEXT_OPT_SCALE_TEXT:
c_token++;
CONTEXT_params.scale_text = real_expression();
break;
case CONTEXT_OPT_DEFAULTFONT:
c_token++;
/* CONTEXT_params.font should be an empty string */
CONTEXT_params.font[0] = 0;
/* default fontsize is 12pt */
CONTEXT_params.fontsize = CONTEXT_FONTSIZE;
break;
case CONTEXT_OPT_POINTS_WITH_METAPOST:
c_token++;
CONTEXT_params.points = CONTEXT_POINTS_WITH_METAPOST;
break;
case CONTEXT_OPT_POINTS_WITH_TEX:
c_token++;
CONTEXT_params.points = CONTEXT_POINTS_WITH_TEX;
break;
case CONTEXT_OPT_IMAGES_INLINE:
c_token++;
CONTEXT_params.images = CONTEXT_IMAGES_INLINE;
break;
case CONTEXT_OPT_IMAGES_EXTERNAL:
#ifdef WRITE_PNG_IMAGE
c_token++;
CONTEXT_params.images = CONTEXT_IMAGES_EXTERNAL;
#else
int_warn(c_token, "Gnuplot was built without support for PNG images. You cannot use this option unless you rebuild gnuplot.");
#endif
break;
/*
* The preferred way to set the font is to set it in a document itself,
* labels in gnuplot in graphs will then inherit that font.
*
* However, it is important to tell gnuplot which size is going to be used,
* so that it can estimate size of text labels.
*
* Whenever you specify
* set term context font "fontname,14"
*
* there are two possibilities:
* - if STANDALONE mode is on, then the whole string will be used as
* \setupbodyfont[fontname,14pt]
* somewhere on top of the document (you still have to make sure that you
* included the proper typescript, so that "fontname" will be recognised
* - in the other case (INPUT mode) only the font size will be used
* internally to estimate sizes of labels, but the font name
* won't be written anywhere
*/
case CONTEXT_OPT_FONT: {
double tmp_fontsize;
char tmp_font[MAX_ID_LEN+1] = "";
c_token++;
if ((tmp_string = try_to_get_string()) && (tmp_string != NULL)) {
CONTEXT_fontstring_parse(tmp_string, tmp_font, MAX_ID_LEN+1, &tmp_fontsize);
/* copies font name to parameters */
safe_strncpy(CONTEXT_params.font, tmp_font, sizeof(CONTEXT_params.font));
tmp_font[MAX_ID_LEN] = NUL;
free(tmp_string);
/* save font size:
*
* - if size > 0, copy
* - if size < 0, fix the size to default value (12pt)
* - if size = 0, ignore
*/
if (tmp_fontsize > 0)
CONTEXT_params.fontsize = tmp_fontsize;
else if (tmp_fontsize < 0)
CONTEXT_params.fontsize = CONTEXT_FONTSIZE;
}
break;
}
case CONTEXT_OPT_OTHER:
default:
/* error */
int_error(c_token, "extraneous argument in set terminal %s",term->name);
break;
}
}
/* current font size in pt (to be used in CONTEXT_adjust_dimensions) */
CONTEXT_fontsize = CONTEXT_params.fontsize;
/* sets term->xmax, ymax, vchar, hchar */
CONTEXT_adjust_dimensions();
snprintf(term_options, sizeof(term_options),
"size %g%s,%g%s %s %s %s",
CONTEXT_params.xsize,
(CONTEXT_params.unit == INCHES) ? "in" : "cm",
CONTEXT_params.ysize,
(CONTEXT_params.unit == INCHES) ? "in" : "cm",
CONTEXT_params.standalone ? "standalone" : "input",
CONTEXT_params.timestamp ? "timestamp" : "notimestamp",
CONTEXT_params.header == NULL ? "noheader \\\n " : "\\\n header ");
if (CONTEXT_params.header != NULL) {
strncat(term_options,"\"", sizeof(term_options)-strlen(term_options)-1);
strncat(term_options,CONTEXT_params.header, sizeof(term_options)-strlen(term_options)-1);
strncat(term_options,"\" \\\n ", sizeof(term_options)-strlen(term_options)-1);
}
strncat(term_options,
CONTEXT_params.color ? "color " : "monochrome ",
sizeof(term_options)-strlen(term_options)-1);
switch (CONTEXT_params.linejoin) {
case LINEJOIN_MITER:
strncat(term_options, "mitered ",
sizeof(term_options)-strlen(term_options)-1);
break;
case LINEJOIN_ROUND:
strncat(term_options, "rounded ",
sizeof(term_options)-strlen(term_options)-1);
break;
case LINEJOIN_BEVEL:
strncat(term_options, "beveled ",
sizeof(term_options)-strlen(term_options)-1);
break;
}
switch (CONTEXT_params.linecap) {
case LINECAP_BUTT :
strncat(term_options, "butt",
sizeof(term_options)-strlen(term_options)-1);
break;
case LINECAP_ROUND :
strncat(term_options, "round",
sizeof(term_options)-strlen(term_options)-1);
break;
case LINECAP_SQUARE:
strncat(term_options, "squared",
sizeof(term_options)-strlen(term_options)-1);
break;
}
snprintf(tmp_term_options, sizeof(tmp_term_options),
" %s dashlength %g linewidth %g fontscale %g \\\n ",
CONTEXT_params.dashed ? "dashed" : "solid",
CONTEXT_params.scale_dashlength,
CONTEXT_params.scale_linewidth,
CONTEXT_params.scale_text
);
strncat(term_options, tmp_term_options, sizeof(term_options)-strlen(term_options)-1);
switch (CONTEXT_params.points) {
case CONTEXT_POINTS_WITH_TEX :
strncat(term_options, "texpoints ",
sizeof(term_options)-strlen(term_options)-1);
break;
case CONTEXT_POINTS_WITH_METAPOST :
strncat(term_options, "mppoints ",
sizeof(term_options)-strlen(term_options)-1);
break;
}
#ifdef WRITE_PNG_IMAGE
switch (CONTEXT_params.images) {
case CONTEXT_IMAGES_INLINE :
strncat(term_options, "inlineimages ",
sizeof(term_options)-strlen(term_options)-1);
break;
case CONTEXT_IMAGES_EXTERNAL :
strncat(term_options, "externalimages ",
sizeof(term_options)-strlen(term_options)-1);
break;
}
#endif
snprintf(tmp_term_options, sizeof(tmp_term_options), "font \"%s,%g\"",
CONTEXT_params.font, CONTEXT_params.fontsize);
strncat(term_options, tmp_term_options, sizeof(term_options) - strlen(term_options)-1);
}
/* **************
* CONTEXT_init *
* **************
*
* Starts a new file.
*
* XXX: "set term context" multiple times will only include those graphics
* that were create before issuing a new "set term context options"
* this should be fixed in the core by reopening the file
* (removing previously written content).
* - PDF & binary terminals start a new file
* - PS & TeX-based terminals continue
*/
TERM_PUBLIC void
CONTEXT_init()
{
time_t now;
char timebuffer[100];
#ifdef WRITE_PNG_IMAGE
int i;
#endif
time(&now);
CONTEXT_posx = CONTEXT_posy = 0;
CONTEXT_path_count = 0;
CONTEXT_path_is_dot = 0;
CONTEXT_counter = 0;
/* setup bitmap images */
CONTEXT_image_counter = 0;
/* the default is to use inline images */
CONTEXT_images = CONTEXT_IMAGES_INLINE;
/* only if external images are both requested and supported, we switch to them (double paranoia) */
/* delete the stored filename first */
if (CONTEXT_image_filename) {
free(CONTEXT_image_filename);
CONTEXT_image_filename = NULL;
CONTEXT_image_filename_length = 0;
CONTEXT_image_filename_start = 0;
};
#ifdef WRITE_PNG_IMAGE
if (CONTEXT_params.images == CONTEXT_IMAGES_EXTERNAL) {
CONTEXT_images = CONTEXT_IMAGES_EXTERNAL;
/* but only if 'set output' was set because we use that string as base for image names */
if (outstr) {
CONTEXT_image_filename_length = strlen(outstr);
CONTEXT_image_filename_start = strlen(outstr) - strlen(gp_basename(outstr));
/* we will cut off the last .tex ending if present */
/* find the last dot if present */
for (i = CONTEXT_image_filename_length - 1; i >= 0 && outstr[i] != '.'; i--);
if (outstr[i] == '.')
CONTEXT_image_filename_length = i;
/* it would also be very nice to do some sanity checks on filenames */
/* <name>.xx.png; must be at least 7 characters long */
CONTEXT_image_filename = (char *)gp_alloc(CONTEXT_image_filename_length + 10, "ConTeXt image filename");
strncpy(CONTEXT_image_filename, outstr, CONTEXT_image_filename_length);
CONTEXT_image_filename[CONTEXT_image_filename_length] = 0;
} else {
CONTEXT_image_filename_length = strlen("gp_image");
CONTEXT_image_filename_start = 0;
/* <name>.xx.png; must be at least 7 characters long */
CONTEXT_image_filename = (char *)gp_alloc(CONTEXT_image_filename_length + 10, "ConTeXt image filename");
strncpy(CONTEXT_image_filename, "gp_image", CONTEXT_image_filename_length);
CONTEXT_image_filename[CONTEXT_image_filename_length] = 0;
}
}
#endif
fprintf(gpoutfile, "%% Written by ConTeXt terminal for GNUPLOT");
if (CONTEXT_params.timestamp) {
if (strftime(timebuffer, 100, "%Y-%m-%d %H:%M %Z", localtime(&now)) != 0)
fprintf(gpoutfile, " on: %s", timebuffer);
}
fprintf(gpoutfile, "\n");
fprintf(gpoutfile, "%% GNUPLOT version: %s.%s, terminal version: %s.%s (%s)\n",
gnuplot_version, gnuplot_patchlevel, CONTEXT_term_version, CONTEXT_term_patch, CONTEXT_term_date);
fprintf(gpoutfile, "%% See also http://wiki.contextgarden.net/Gnuplot\n%%\n");
/* place the header first if this is a standalone graphic */
if (CONTEXT_params.standalone) {
/* If encoding is explicitly set to UTF-8 by gnuplot, use that setting.
* \enableregime only makes a difference for pdfTeX; in LuaTeX and XeTeX UTF-8 is already default,
* so this line will be ignored.
*
* There is no extra support for other encodings on purpose:
* - ConTeXt doesn't support all encodings supported by Gnuplot.
* - In LuaTeX and XeTeX one should not use any other encoding anyway.
* - pdfTeX users are free to use "header '\enableregime[...]'" */
switch (encoding) {
case S_ENC_UTF8:
fputs("\\enableregime\n [utf-8]\n", gpoutfile);
break;
default:
/* do nothing */
break;
}
/* load the gnuplot module */
fputs("\\usemodule\n [gnuplot]\n", gpoutfile);
/* enable or disable color (the only place where "color" is indeed used so far) */
fprintf(gpoutfile, "\\setupcolors\n [state=%s]\n", CONTEXT_params.color ? "start" : "stop");
/* additional user-provided header information (if available) */
if (CONTEXT_params.header)
fprintf(gpoutfile, "%s\n", CONTEXT_params.header);
/* for some reason setting \bodyfontenvironment is needed,
* otherwise \switchtobodyfont[name] doesn't work OK */
if (!(CONTEXT_params.fontsize == CONTEXT_FONTSIZE))
fprintf(gpoutfile, "\\definebodyfontenvironment\n [%gpt]\n",
CONTEXT_params.fontsize);
/* set the proper font: \setupbodyfont[{fontname,}fontsize sizeunit] */
fprintf(gpoutfile, "\\setupbodyfont\n [%s%s%gpt]\n",
CONTEXT_params.font,
/* write a comma only if the last string was non-empty */
((strlen(CONTEXT_params.font)>0) ? "," : ""),
CONTEXT_params.fontsize);
/*---------*
* options *
*---------*/
fprintf(gpoutfile, "\\setupGNUPLOTterminal\n [context]\n [");
/* color (gp_use_color): yes/no (true/false)
* default: yes
* - doesn't do anything useful yet;
* and besides that, it's already set in \setupcolors[state=start] */
/* fprintf(gpoutfile, " color=%s, %% *yes* | no\n", CONTEXT_params.color ? "yes" : "no"); */
/* linejoin: mitered/rounded/beveled
* default: mitered */
fprintf(gpoutfile, "linejoin=");
switch (CONTEXT_params.linejoin) {
case LINEJOIN_MITER: fprintf(gpoutfile, "mitered"); break;
case LINEJOIN_ROUND: fprintf(gpoutfile, "rounded"); break;
case LINEJOIN_BEVEL: fprintf(gpoutfile, "beveled"); break;
}
fprintf(gpoutfile, ", %% *mitered* | rounded | beveled\n");
/* linecap: butt/rounded/squared
* default: butt */
fprintf(gpoutfile, " linecap=");
switch (CONTEXT_params.linecap) {
case LINECAP_BUTT : fprintf(gpoutfile, "butt"); break;
case LINECAP_ROUND : fprintf(gpoutfile, "rounded"); break;
case LINECAP_SQUARE: fprintf(gpoutfile, "squared"); break;
}
fprintf(gpoutfile, ", %% *butt* | rounded | squared\n");
/* dashed (gp_use_dashed): yes/no (true/false)
* default: yes */
fprintf(gpoutfile, " dashed=%s, %% *yes* | no\n", CONTEXT_params.dashed ? "yes" : "no");
/* dashlength (gp_scale_dashlength): 1.0 */
fprintf(gpoutfile, " dashlength=%g, %% scaling factor for dash lengths\n", CONTEXT_params.scale_dashlength);
/* linewidth (gp_scale_linewidth): 1.0 */
fprintf(gpoutfile, " linewidth=%g, %% scaling factor for line widths (1.0 means 0.5bp)\n", CONTEXT_params.scale_linewidth);
/* fontscale (gp_scale_text): 1.0 */
/* written out just for reference - it's commented out since it needs to be part of graphic
and affects estimation of label sizes */
fprintf(gpoutfile, " %%fontscale=%g, %% scaling factor for text labels\n", CONTEXT_params.scale_text);
/* points (gp_points_with): metapost/tex (gp_points_with_metapost/gp_points_with_tex)
* default: metapost */
fprintf(gpoutfile, " points=%s, %% *metapost* | tex (Should points be drawn with MetaPost or TeX?)\n",
CONTEXT_params.points == CONTEXT_POINTS_WITH_METAPOST ? "metapost" : "tex");
/* images
* default: inline */
fprintf(gpoutfile, " images=%s] %% *inline* | external (inline only works in MKIV, external requires png support in gnuplot)\n",
CONTEXT_images == CONTEXT_IMAGES_INLINE ? "inline" : "external");
/*----------------*
* end of options *
*----------------*/
fputs("\n\\starttext\n\n", gpoutfile);
} else {
/* Sorry, nothing! In non-standalone graphic, parameters make no sense.
* Setup everything in the file which includes such a graphic instead.
*/
}
}
/* ***************
* CONTEXT_reset *
* ***************
*
* finish writing the file
*/
TERM_PUBLIC void
CONTEXT_reset()
{
/* we only have to end the document if this is a stand-alone graphic */
if (CONTEXT_params.standalone) {
fputs("\\stoptext\n", gpoutfile);
} else {
/* This means that any subsequent plots to the same file will be ignored.
* I don't like that - gnuplot should delete the old contents instead,
* just as it does in case of PNG or PDF -
* but it will be at least consistent with standalone graphics that way
*/
fputs("\\endinput\n", gpoutfile);
}
/* deallocate image name if present */
if (CONTEXT_image_filename) {
free(CONTEXT_image_filename);
CONTEXT_image_filename = NULL;
CONTEXT_image_filename_length = 0;
CONTEXT_image_filename_start = 0;
};
}
/* **************
* CONTEXT_text *
* **************
*
* Ends the current graphic.
*/
TERM_PUBLIC void
CONTEXT_text()
{
/* close and draw the current path first */
if (CONTEXT_path_count > 0)
CONTEXT_endpath();
fprintf(gpoutfile, "setbounds currentpicture to unitsquare xyscaled (w,h);\n");
/* standalone graphic is a whole-page graphic */
if (CONTEXT_params.standalone) {
fputs("\\stopGNUPLOTpage\n", gpoutfile);
/* otherwise we define a MPgraphic to be included later */
} else {
fputs("\\stopGNUPLOTgraphic\n", gpoutfile);
}
}
/* ******************
* CONTEXT_graphics *
* ******************
*
* Starts a new graphic.
*/
TERM_PUBLIC void
CONTEXT_graphics()
{
/* standalone graphic is a whole-page graphic */
if (CONTEXT_params.standalone) {
fprintf(gpoutfile, "\\startGNUPLOTpage %% Graphic Nr. %d\n", ++CONTEXT_counter);
/* otherwise we define a MPgraphic to be included later */
} else {
/* the first parameter holds the graphic number */
fprintf(gpoutfile, "\\startGNUPLOTgraphic[%d]\n", ++CONTEXT_counter);
}
fprintf(gpoutfile, "string gnuplotversion; gnuplotversion := \"%s\";\n", gnuplot_version);
fprintf(gpoutfile, "string termversion; termversion := \"%s\";\n", CONTEXT_term_version);
/*
* MetaPost can only handle numbers up to 4096. Too high resolution
* would thus result in number overflow, that's why we scale down all the
* integers from gnuplot by 1000 and multiply those numbers later by
* appropriate scaling factor 'a' to get the proper dimensions.
*/
fprintf(gpoutfile, "%% scaling factor, width and height of the figure\na := 1cm; w := %.3fa; h := %.3fa; %% (%g%s, %g%s)\n",
CONTEXT_params.xsize * ((CONTEXT_params.unit == INCHES) ? 2.54 : 1), /* cm */
CONTEXT_params.ysize * ((CONTEXT_params.unit == INCHES) ? 2.54 : 1), /* cm */
CONTEXT_params.xsize, (CONTEXT_params.unit == INCHES) ? "in" : "cm",
CONTEXT_params.ysize, (CONTEXT_params.unit == INCHES) ? "in" : "cm");
/* TODO: the following if-else could be slightly nicer */
if (CONTEXT_images == CONTEXT_IMAGES_INLINE) {
fprintf(gpoutfile, "%% temporary variable for storing the path and images\nsave p, img, ima; path p; string img, ima;\n");
} else {
fprintf(gpoutfile, "%% temporary variable for storing the path\nsave p; path p;\n");
}
fprintf(gpoutfile, "%% -------------------------\n");
fprintf(gpoutfile, "%% Different initialisations\n");
fprintf(gpoutfile, "%% -------------------------\n");
fprintf(gpoutfile, "%% for additional user-defined settings\ngp_setup_before;\n");
/* needed (depends on terminal settings & needs to be passed) */
fprintf(gpoutfile, "%% text scaling factor for the whole figure\n");
fprintf(gpoutfile, "gp_scale_text := %g;\n", CONTEXT_params.scale_text);
fprintf(gpoutfile, "%% pointsize scaling factor\n");
fprintf(gpoutfile, "gp_set_pointsize(%g);\n", CONTEXT_old_pointsize);
/* needed (provided by terminal) */
fprintf(gpoutfile, "%% linewidth scaling factor for individual lines\n");
fprintf(gpoutfile, "gp_set_linewidth(%g);\n", CONTEXT_old_linewidth);
fprintf(gpoutfile, "%% for additional user-defined settings\ngp_setup_after;\n");
fprintf(gpoutfile, "%% -------------------------\n");
/* since palette is initialized only once, subsequent plots wouldn't see it
* unless we write it on the top of relevant plots explicitly */
if (is_plot_with_palette()) {