The R Project SVN R

Rev

Rev 36139 | Rev 36990 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11507 ripley 1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
4
 *  Copyright (C) 1998--2000  R Development Core Team
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
#ifndef GRAPHICS_H_
22
#define GRAPHICS_H_
23
 
24
#define R_GRAPHICS_INTERNAL 1
25
 
11509 ripley 26
#include <R_ext/Boolean.h>
11507 ripley 27
 
30956 murrell 28
#include <R_ext/GraphicsDevice.h>
29
#include <R_ext/GraphicsEngine.h>
30
 
11507 ripley 31
#define R_MaxDevices 64
32
 
33
#define	DEG2RAD 0.01745329251994329576
34
 
35
#define COLOR_TABLE_SIZE 1024
36
 
32449 ripley 37
#define MAX_LAYOUT_ROWS 50
38
#define MAX_LAYOUT_COLS 50
39
#define MAX_LAYOUT_CELLS 500 /* must be less than 65535, 
40
				3 copies, 3bytes each */
11507 ripley 41
 
42
typedef unsigned int rcolor;
43
 
44
typedef struct {
45
	double ax;
46
	double bx;
47
	double ay;
48
	double by;
49
} GTrans;
50
 
51
	/* possible coordinate systems (for specifying locations) */
52
typedef enum {
53
 DEVICE	= 0,	/* native device coordinates (rasters) */
54
 NDC	= 1,	/* normalised device coordinates x=(0,1), y=(0,1) */
55
 INCHES = 13,	/* inches x=(0,width), y=(0,height) */
56
 NIC	= 6,	/* normalised inner region coordinates (0,1) */
57
 OMA1	= 2,	/* outer margin 1 (bottom) x=NIC, y=LINES */
58
 OMA2	= 3,	/* outer margin 2 (left) */
59
 OMA3	= 4,	/* outer margin 3 (top) */
60
 OMA4	= 5,	/* outer margin 4 (right) */
61
 NFC	= 7,	/* normalised figure region coordinates (0,1) */
62
 NPC	= 16,	/* normalised plot region coordinates (0,1) */
11510 ripley 63
 USER	= 12,	/* user/data/world coordinates;
11507 ripley 64
		 * x,=(xmin,xmax), y=(ymin,ymax) */
65
 MAR1	= 8,	/* figure margin 1 (bottom) x=USER(x), y=LINES */
66
 MAR2	= 9,	/* figure margin 2 (left)   x=USER(y), y=LINES */
67
 MAR3	= 10,	/* figure margin 3 (top)    x=USER(x), y=LINES */
68
 MAR4	= 11,	/* figure margin 4 (right)  x=USER(y), y=LINES */
69
 
70
	/* possible, units (for specifying dimensions) */
71
	/* all of the above, plus ... */
72
 
73
 LINES = 14,	/* multiples of a line in the margin (mex) */
74
 CHARS = 15	/* multiples of text height (cex) */
75
} GUnit;
76
 
77
 
78
typedef struct {
79
    /* Basic Device Driver Properties */
80
    /* These MUST be set by device drivers on open */
81
 
82
    /* These parameters cannot be set by the user */
83
    /* although left, right, bottom, and top can be */
84
    /* interrogated indirectly (i.e., par("din")) */
85
    /* and cra can be interrogated directly (i.e., par("cra")) */
86
 
87
    double left;	/* left raster coordinate */
88
    double right;	/* right raster coordinate */
89
    double bottom;	/* bottom raster coordinate */
90
    double top;		/* top raster coordinate */
91
    double xCharOffset;	/* x character addressing offset */
92
    double yCharOffset;	/* y character addressing offset */
93
    double yLineBias;	/* 1/2 interline space as fraction of line height */
94
    Rboolean canResizePlot;	/* can the graphics surface be resized */
95
    Rboolean canChangeFont;	/* device has multiple fonts */
96
    Rboolean canRotateText;	/* text can be rotated */
97
    Rboolean canResizeText;	/* text can be resized */
98
    Rboolean canClip;		/* Hardware clipping */
99
    int canHAdj;	/* Can do at least some horizontal adjustment of text
100
 
101
 
102
    /* a couple of the GRZ-like parameters that have to be */
103
    /* set by the device */
104
 
105
    double ipr[2];	/* Inches per raster; [0]=x, [1]=y */
106
    double asp;		/* Pixel aspect ratio = ipr[1]/ipr[0] */
107
    double cra[2];	/* Character size in rasters; [0]=x, [1]=y */
108
 
109
    /* Plot State */
110
    /* When the device driver is started this is 0 */
111
    /* After the first call to plot.new it is 1 */
112
    /* Every graphics operation except plot.new */
113
    /* should fail if state = 0 */
114
    /* This is checked at the highest internal function */
115
    /* level (e.g., do_lines, do_axis, do_plot_xy, ...) */
116
 
117
    int	state;		/* Plot State */
118
    Rboolean valid;	/* valid layout ? */
119
 
120
    /* GRZ-like Graphics Parameters */
121
    /* ``The horror, the horror ... '' */
122
    /* Marlon Brando - Appocalypse Now */
123
 
124
    /* General Parameters -- set and interrogated directly */
125
 
126
    double adj;		/* String adjustment */
127
    Rboolean ann;	/* Should annotation take place */
128
    Rboolean ask;	/* User confirmation of ``page eject'' */
129
    rcolor bg;		/* **R ONLY** Background color */
130
    int	bty;		/* Box type */
131
    double cex;		/* Character expansion */
30956 murrell 132
    double lheight;     /* Line height
133
			   The height of a line of text is:
134
			   ps * cex * lheight */
11507 ripley 135
    rcolor col;		/* Plotting Color */
136
    double crt;		/* Character/string rotation */
137
    double din[2];	/* device size in inches */
138
    int	err;		/* Error repporting level */
139
    rcolor fg;		/* **R ONLY** Foreground Color */
36203 ripley 140
    char family[201];  /* **R ONLY** Font family 
30956 murrell 141
			   Simple name which is mapped by device-specific
142
			   font database to device-specific name.
143
			   Only used if not "".
144
			   Default is "".
145
			   Ignored by some devices. */
11507 ripley 146
    int	font;		/* Text font */
147
    double gamma;	/* Device Gamma Correction */
148
    int	lab[3];		/* Axis labelling */
149
			/* [0] = # ticks on x-axis */
150
			/* [1] = # ticks on y-axis */
151
			/* [2] = length of axis labels */
152
    int	las;		/* Label style (rotation) */
153
    int	lty;		/* Line texture */
154
    double lwd;		/* Line width */
30956 murrell 155
    R_GE_lineend lend;  /* **R ONLY** Line end style */
156
    R_GE_linejoin ljoin;/* **R ONLY** Line join style */
157
    double lmitre;      /* **R ONLY** Line mitre limit */
11507 ripley 158
    double mgp[3];	/* Annotation location */
159
			/* [0] = location of axis title */
160
			/* [1] = location of axis label */
161
			/* [2] = location of axis line */
162
    double mkh;		/* Mark size in inches */
163
    int	pch;		/* Plotting character */
34613 murrell 164
    int ps;		/* Text & symbol pointsize */
20446 maechler 165
    int	smo;		/* Curve smoothness */
11507 ripley 166
    double srt;		/* String Rotation */
167
    double tck;		/* Tick size as in S */
168
    double tcl;		/* Tick size in "lines" */
169
    double tmag;	/* **R ONLY** Title Magnification */
36139 ripley 170
    /* int	type;	    type of plot desired -- removed in 2.3.0 */
11507 ripley 171
    double xaxp[3];	/* X Axis annotation */
172
			/* [0] = coordinate of lower tick */
173
			/* [1] = coordinate of upper tick */
174
			/* [2] = num tick intervals */
175
			/* almost always used internally */
176
    int	xaxs;		/* X Axis style */
177
    int	xaxt;		/* X Axis type */
178
    int	xpd;		/* Clip to plot region indicator */
179
    int	oldxpd;
180
    double yaxp[3];	/* Y Axis annotation */
181
    int	yaxs;		/* Y Axis style */
182
    int	yaxt;		/* Y Axis type */
183
    Rboolean xlog;	/* Log Axis for X */
184
    Rboolean ylog;	/* Log Axis for Y */
185
 
186
    /* Annotation Parameters */
187
 
188
    float cexbase;	/* Base character size */
189
    float cexmain;	/* Main title size */
190
    float cexlab;	/* xlab and ylab size */
191
    float cexsub;	/* Sub title size */
192
    float cexaxis;	/* Axis label size */
193
 
194
    int	fontmain;	/* Main title font */
195
    int	fontlab;	/* Xlab and ylab font */
196
    int	fontsub;	/* Subtitle font */
197
    int	fontaxis;	/* Axis label fonts */
198
 
199
    int	colmain;	/* Main title color */
200
    int	collab;		/* Xlab and ylab color */
201
    int	colsub;		/* Subtitle color */
202
    int	colaxis;	/* Axis label color */
203
 
204
    /* Layout Parameters */
205
 
206
    Rboolean layout;	/* has a layout been specified */
207
 
208
    int	numrows;
209
    int	numcols;
210
    int	currentFigure;
211
    int	lastFigure;
212
    double heights[MAX_LAYOUT_ROWS];
213
    double widths[MAX_LAYOUT_COLS];
214
    int	cmHeights[MAX_LAYOUT_ROWS];
215
    int	cmWidths[MAX_LAYOUT_COLS];
32449 ripley 216
    unsigned short order[MAX_LAYOUT_CELLS];
11507 ripley 217
    int	rspct;		/* 0 = none, 1 = full, 2 = see respect */
32449 ripley 218
    unsigned char respect[MAX_LAYOUT_CELLS];
11507 ripley 219
 
220
    int	mfind;		/* By row/col indicator */
221
 
222
    /* Layout parameters which can be set directly by the */
223
    /* user (e.g., par(fig=c(.5,1,0,1))) or otherwise are */
224
    /* calculated automatically */
225
    /* NOTE that *Units parameters are for internal use only */
226
 
227
    double fig[4];	/* (current) Figure size (proportion) */
228
			/* [0] = left, [1] = right */
229
			/* [2] = bottom, [3] = top */
230
    double fin[2];	/* (current) Figure size (inches) */
231
			/* [0] = width, [1] = height */
232
    GUnit fUnits;	/* (current) figure size units */
233
    double plt[4];	/* (current) Plot size (proportions) */
234
			/* [0] = left, [1] = right */
235
			/* [2] = bottom, [3] = top */
236
    double pin[2];	/* (current) plot size (inches) */
237
			/* [0] = width, [1] = height */
238
    GUnit pUnits;	/* (current) plot size units */
239
    Rboolean defaultFigure;	/* calculate figure from layout ? */
240
    Rboolean defaultPlot;	/* calculate plot from figure - margins ? */
241
 
242
    /* Layout parameters which are set directly by the user */
243
 
244
    double mar[4];	/* Plot margins in lines */
245
    double mai[4];	/* Plot margins in inches */
246
			/* [0] = bottom, [1] = left */
247
			/* [2] = top, [3] = right */
248
    GUnit mUnits;	/* plot margin units */
249
    double mex;		/* Margin expansion factor */
250
    double oma[4];	/* Outer margins in lines */
251
    double omi[4];	/* outer margins in inches */
252
    double omd[4];	/* outer margins in NDC */
253
			/* [0] = bottom, [1] = left */
254
			/* [2] = top, [3] = right */
255
    GUnit oUnits;	/* outer margin units */
256
    int	pty;		/* Plot type */
257
 
258
    /* Layout parameters which can be set by the user, but */
259
    /* almost always get automatically calculated anyway */
260
 
261
    double usr[4];	/* Graphics window */
262
			/* [0] = xmin, [1] = xmax */
263
			/* [2] = ymin, [3] = ymax */
264
 
265
    /* The logged usr parameter;  if xlog, use logusr[0:1] */
266
    /* if ylog, use logusr[2:3] */
267
 
268
    double logusr[4];
269
 
270
    /* Layout parameter: Internal flags */
271
 
272
    Rboolean new;	/* Clean plot ? */
273
    int	devmode;	/* creating new image or adding to existing one */
274
 
275
    /* Coordinate System Mappings */
276
    /* These are only used internally (i.e., cannot be */
277
    /* set directly by the user) */
278
 
279
    /* The reliability of these parameters relies on */
280
    /* the fact that plot.new is the */
281
    /* first graphics operation called in the creation */
282
    /* of a graph */
283
 
284
    /* udpated per plot.new */
285
 
286
    double xNDCPerChar;	/* Nominal character width (NDC) */
287
    double yNDCPerChar;	/* Nominal character height (NDC) */
288
    double xNDCPerLine;	/* Nominal line width (NDC) */
289
    double yNDCPerLine;	/* Nominal line height (NDC) */
290
    double xNDCPerInch;	/* xNDC -> Inches */
291
    double yNDCPerInch;	/* yNDC -> Inches */
292
 
293
    /* updated per plot.new and if inner2dev changes */
294
 
295
    GTrans fig2dev;	/* Figure to device */
296
 
297
    /* udpated per DevNewPlot and if ndc2dev changes */
298
 
299
    GTrans inner2dev;	/* Inner region to device */
300
 
301
    /* udpated per device resize */
302
 
303
    GTrans ndc2dev;	/* NDC to raw device */
304
 
305
    /* updated per plot.new and per plot.window */
306
 
307
    GTrans win2fig;	/* Window to figure mapping */
308
 
309
    /* NOTE: if user has not set fig and/or plt then */
310
    /* they need to be updated per plot.new too */
311
 
34613 murrell 312
    double scale;       /* An internal "zoom" factor to apply to ps and lwd */
313
                        /* (for fit-to-window resizing in Windows) */
314
 
11507 ripley 315
    /* device operations */
316
    Rboolean (*open)();
317
    void (*close)();
318
    void (*activate)();
319
    void (*deactivate)();
320
    void (*resize)();
321
    void (*newPage)();
322
    void (*clip)();
323
    double (*strWidth)();
324
    void (*line)();
325
    void (*polyline)();
326
    void (*text)();
327
    void (*dot)();
328
    void (*rect)();
329
    void (*circle)();
330
    void (*polygon)();
331
    Rboolean (*locator)();
332
    void (*mode)();
333
    void (*hold)();
334
    void (*metricInfo)();
335
} GPar;
336
 
337
typedef struct {
16880 murrell 338
    /* New flag to indicate that this is an "old" device
339
     * structure.
340
     */
341
    int newDevStruct;
342
    GPar dp;		/* current device default parameters */
343
    GPar gp;		/* current device current parameters */
344
    GPar dpSaved;		/* saved device default parameters */
345
    void *deviceSpecific;	/* pointer to device specific parameters */
346
    Rboolean displayListOn;	/* toggle for display list status */
347
    SEXP displayList;	/* display list */
11507 ripley 348
} DevDesc;
349
 
19934 maechler 350
/* For easy reference: Here are the source files of
11507 ripley 351
 * currently existing device drivers:
352
 * FILE				driver name prefix
353
 * ----------------------	------------------
20446 maechler 354
 * ../main/devPS.c		PS , PDF _and_  XFig
11507 ripley 355
 * ../main/devPicTeX.c		PicTeX
15168 pd 356
 * ../modules/X11/devX11.c	X11
11507 ripley 357
 * ../gnuwin32/devga.c		GA
15168 pd 358
 * ../modules/gnome/devGTK.c	GTK
359
 * ../modules/gnome/devGNOME.c	Gnome
11507 ripley 360
 */
361
 
11510 ripley 362
/* always remap private functions */
11509 ripley 363
#include <Rgraphics.h>
11510 ripley 364
#define char2col		Rf_char2col
365
#define col2name		Rf_col2name
366
#define copyGPar		Rf_copyGPar
16880 murrell 367
#define curDevice               Rf_curDevice
368
#define GetDevice               Rf_GetDevice
11510 ripley 369
#define GInit			Rf_GInit
370
#define name2col		Rf_name2col
16880 murrell 371
#define nextDevice              Rf_nextDevice
11510 ripley 372
#define number2col		Rf_number2col
16880 murrell 373
#define NumDevices              Rf_NumDevices
11510 ripley 374
#define rgb2col			Rf_rgb2col
375
#define RGB2rgb			Rf_RGB2rgb
30859 murrell 376
#define RGBA2rgb		Rf_RGBA2rgb
11510 ripley 377
#define ScaleColor		Rf_ScaleColor
378
#define str2col			Rf_str2col
379
#define StrMatch		Rf_StrMatch
30129 murrell 380
#define isNAcol                 Rf_isNAcol
11507 ripley 381
 
31938 murrell 382
/* NOTE: during replays, call == R_NilValue;
383
   ----  the following adds readability: */
384
Rboolean GRecording(SEXP, DevDesc*);
385
 
11507 ripley 386
/* Default the settings for general graphical parameters
387
 * (i.e., defaults that do not depend on the device type: */
388
void GInit(GPar*);
389
 
11510 ripley 390
void copyGPar(GPar *, GPar *);
391
 
16880 murrell 392
int curDevice(void);
11510 ripley 393
 
16880 murrell 394
DevDesc* GetDevice(int i);
11510 ripley 395
 
16880 murrell 396
int nextDevice(int from);
397
 
398
int NumDevices(void);
399
 
400
int deviceNumber(DevDesc *dd);
401
 
402
int devNumber(DevDesc *dd);
403
 
11510 ripley 404
		/* Miscellaneous (from graphics.c & colors.c) */
405
 
406
unsigned int rgb2col(char *);
407
unsigned int name2col(char *);
408
unsigned int number2col(char *);
409
unsigned int char2col(char *);/* rgb2col() or name2col() */
410
unsigned int str2col(char *);
411
 
412
char* col2name(unsigned int);
413
 
414
unsigned int ScaleColor(double x);
20446 maechler 415
unsigned int CheckColor(int x);
30129 murrell 416
Rboolean isNAcol(SEXP col, int index, int ncol);
11510 ripley 417
 
418
char* RGB2rgb(unsigned int, unsigned int, unsigned int);
30859 murrell 419
char* RGBA2rgb(unsigned int, unsigned int, unsigned int, unsigned int);
11510 ripley 420
 
421
int StrMatch(char *s, char *t);
422
 
423
double R_Log10(double);
424
 
17204 hornik 425
#include <R_ext/GraphicsBase.h>
16880 murrell 426
 
27237 murrell 427
/* 
428
 * Function to generate an R_GE_gcontext from Rf_gpptr info
429
 */
430
void gcontextFromGP(R_GE_gcontext *gc, DevDesc *dd);
431
 
16880 murrell 432
/* FIXME: Make this a macro to avoid function call overhead?
433
 */
17179 murrell 434
GPar* Rf_gpptr(DevDesc *dd);
435
GPar* Rf_dpptr(DevDesc *dd);
436
GPar* Rf_dpSavedptr(DevDesc *dd);
437
SEXP Rf_displayList(DevDesc *dd);
16880 murrell 438
 
32151 murdoch 439
/* Graphics events */
440
 
32163 murdoch 441
/* These give the indices of some known keys */    
442
 
443
typedef enum {knUNKNOWN = -1,
444
              knLEFT = 0, knUP, knRIGHT, knDOWN,
445
              knF1, knF2, knF3, knF4, knF5, knF6, knF7, knF8, knF9, knF10,
446
              knF11, knF12,
447
              knPGUP, knPGDN, knEND, knHOME, knINS, knDEL} R_KeyName;
32205 murdoch 448
 
449
/* These are the three possible mouse events */
32163 murdoch 450
 
32205 murdoch 451
typedef enum {meMouseDown = 0,
452
	      meMouseUp,
453
	      meMouseMove} R_MouseEvent;
32151 murdoch 454
 
32205 murdoch 455
SEXP doMouseEvent(SEXP eventRho, NewDevDesc *dd, R_MouseEvent event, 
456
                  int buttons, double x, double y);
457
SEXP doKeybd	(SEXP eventRho, NewDevDesc *dd, R_KeyName rkey, char *keyname);
458
 
11507 ripley 459
#endif /* GRAPHICS_H_ */