GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
psdriver/Graph_set.c
Go to the documentation of this file.
1 /*
2  * Start up graphics processing. Anything that needs to be assigned, set up,
3  * started-up, or otherwise initialized happens here. This is called only at
4  * the startup of the graphics driver.
5  *
6  * The external variables define the pixle limits of the graphics surface. The
7  * coordinate system used by the applications programs has the (0,0) origin
8  * in the upper left-hand corner. Hence,
9  * screen_left < screen_right
10  * screen_top < screen_bottom
11  */
12 
13 #include <string.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <time.h>
17 
18 #include <grass/gis.h>
19 #include "psdriver.h"
20 
21 #define DATE_FORMAT "%c"
22 
23 const char *file_name;
24 FILE *outfp;
29 
30 static int landscape;
31 static int left, right, bot, top;
32 
33 struct paper
34 {
35  const char *name;
36  double width, height;
37  double left, right, bot, top;
38 };
39 
40 static const struct paper papers[] = {
41  /* name width height left right bottom top */
42  {"a4", 8.268, 11.693, 0.5, 0.5, 1.0, 1.0},
43  {"a3", 11.693, 16.535, 0.5, 0.5, 1.0, 1.0},
44  {"a2", 16.54, 23.39, 1.0, 1.0, 1.0, 1.0},
45  {"a1", 23.39, 33.07, 1.0, 1.0, 1.0, 1.0},
46  {"a0", 33.07, 46.77, 1.0, 1.0, 1.0, 1.0},
47  {"us-legal", 8.5, 14.0, 1.0, 1.0, 1.0, 1.0},
48  {"us-letter", 8.5, 11.0, 1.0, 1.0, 1.0, 1.0},
49  {"us-tabloid", 11.0, 17.0, 1.0, 1.0, 1.0, 1.0},
50  {NULL, 0, 0, 0, 0, 0, 0}
51 };
52 
53 static void write_prolog(void)
54 {
55  char prolog_file[GPATH_MAX];
56  char date_str[256];
57  FILE *prolog_fp;
58  time_t t = time(NULL);
59  struct tm *tm = localtime(&t);
60 
61  strftime(date_str, sizeof(date_str), DATE_FORMAT, tm);
62 
63  sprintf(prolog_file, "%s/etc/psdriver.ps", G_gisbase());
64 
65  prolog_fp = fopen(prolog_file, "r");
66  if (!prolog_fp)
67  G_fatal_error("Unable to open prolog file");
68 
69  if (encapsulated)
70  output("%%!PS-Adobe-3.0 EPSF-3.0\n");
71  else
72  output("%%!PS-Adobe-3.0\n");
73 
74  output("%%%%LanguageLevel: %d\n", 3);
75  output("%%%%Creator: GRASS PS Driver\n");
76  output("%%%%Title: %s\n", file_name);
77  output("%%%%For: %s\n", G_whoami());
78  output("%%%%Orientation: %s\n", landscape ? "Landscape" : "Portrait");
79  output("%%%%BoundingBox: %d %d %d %d\n", left, bot, right, top);
80  output("%%%%CreationDate: %s\n", date_str);
81  output("%%%%EndComments\n");
82 
83  output("%%%%BeginProlog\n");
84  while (!feof(prolog_fp)) {
85  char buf[256];
86 
87  if (!fgets(buf, sizeof(buf), prolog_fp))
88  break;
89 
90  fputs(buf, outfp);
91  }
92  output("%%%%EndProlog\n");
93 
94  fclose(prolog_fp);
95 }
96 
97 void write_setup(void)
98 {
99  output("%%%%BeginSetup\n");
100 
101  output("%d %d translate\n", left, bot);
102 
103  if (landscape)
104  output("90 rotate 0 1 -1 scale\n");
105  else
106  output("0 %d translate 1 -1 scale\n", height);
107 
108  output("%d %d BEGIN\n", width, height);
109 
110  output("%%%%EndSetup\n");
111  output("%%%%Page: 1 1\n");
112 }
113 
114 static int in2pt(double x)
115 {
116  return (int)(x * 72);
117 }
118 
119 static void swap(int *x, int *y)
120 {
121  int tmp = *x;
122 
123  *x = *y;
124  *y = tmp;
125 }
126 
127 static void get_paper(void)
128 {
129  const char *name = getenv("GRASS_PAPER");
130  const struct paper *paper;
131  int i;
132 
135 
136  left = 0;
137  right = width;
138  bot = 0;
139  top = height;
140 
141  if (landscape)
142  swap(&right, &top);
143 
144  if (!name)
145  return;
146 
147  for (i = 0;; i++) {
148  paper = &papers[i];
149 
150  if (!paper->name)
151  return;
152 
153  if (G_strcasecmp(name, paper->name) == 0)
154  break;
155  }
156 
157  left = in2pt(paper->left);
158  right = in2pt(paper->width) - in2pt(paper->right);
159  bot = in2pt(paper->bot);
160  top = in2pt(paper->height) - in2pt(paper->top);
161 
162  width = right - left;
163  height = in2pt(paper->height) - in2pt(paper->top) - in2pt(paper->bot);
164 
165  if (landscape)
166  swap(&width, &height);
167 
168  screen_right = screen_left + width;
169  screen_bottom = screen_top + height;
170 }
171 
172 int PS_Graph_set(int argc, char **argv)
173 {
174  const char *p;
175 
176  G_gisinit("PS driver");
177 
178  p = getenv("GRASS_PSFILE");
179  if (!p || strlen(p) == 0)
180  p = FILE_NAME;
181 
182  file_name = p;
183  p = file_name + strlen(file_name) - 4;
184  encapsulated = (G_strcasecmp(p, ".eps") == 0);
185 
186  p = getenv("GRASS_TRUECOLOR");
187  true_color = p && strcmp(p, "TRUE") == 0;
188 
189  p = getenv("GRASS_LANDSCAPE");
190  landscape = p && strcmp(p, "TRUE") == 0;
191 
192  p = getenv("GRASS_PS_HEADER");
193  no_header = p && strcmp(p, "FALSE") == 0;
194 
195  p = getenv("GRASS_PS_TRAILER");
196  no_trailer = p && strcmp(p, "FALSE") == 0;
197 
198  G_message("PS: GRASS_TRUECOLOR status: %s",
199  true_color ? "TRUE" : "FALSE");
200 
201  get_paper();
202 
204 
205  outfp = fopen(file_name, no_header ? "a" : "w");
206 
207  if (!outfp)
208  G_fatal_error("Unable to open output file: %s", file_name);
209 
210  if (!no_header) {
211  write_prolog();
212  write_setup();
213  }
214 
215  G_message
216  ("PS: collecting to file: %s,\n GRASS_WIDTH=%d, GRASS_HEIGHT=%d",
218 
219  fflush(outfp);
220 
221  return 0;
222 }
223 
224 void output(const char *fmt, ...)
225 {
226  va_list va;
227 
228  va_start(va, fmt);
229  vfprintf(outfp, fmt, va);
230  va_end(va);
231 }
double top
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
double left
#define swap(x, y)
Definition: display/draw.c:61
const char * name
string name
Definition: render.py:1314
tuple width
int no_header
int screen_top
Definition: driver/init.c:35
int y
Definition: plot.c:34
struct tm * localtime()
double bot
#define FILE_NAME
Definition: pngdriver.h:9
int true_color
int screen_left
Definition: driver/init.c:32
char * getenv()
double height
void init_color_table(void)
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
char * file_name
int screen_right
Definition: driver/init.c:33
void write_setup(void)
int PS_Graph_set(int argc, char **argv)
char * G_whoami(void)
Gets user&#39;s name.
Definition: gis/whoami.c:40
#define DATE_FORMAT
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
fclose(fd)
char * G_gisbase(void)
top level module directory
Definition: gisbase.c:42
int screen_bottom
Definition: driver/init.c:34
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int height
int no_trailer
void output(const char *fmt,...)
int encapsulated
double width
double right
FILE * outfp