GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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 #include <math.h>
18 #include <unistd.h>
19 
20 #include <grass/gis.h>
21 #include <grass/glocale.h>
22 #include "psdriver.h"
23 
24 #define DATE_FORMAT "%c"
25 
26 struct ps_state ps;
27 
28 static double width, height;
29 static int landscape;
30 
31 struct paper {
32  const char *name;
33  double width, height;
34  double left, right, bot, top;
35 };
36 
37 static const struct paper papers[] = {
38  /* name width height left right bottom top */
39  {"a4", 8.268, 11.693, 0.5, 0.5, 1.0, 1.0},
40  {"a3", 11.693, 16.535, 0.5, 0.5, 1.0, 1.0},
41  {"a2", 16.54, 23.39, 1.0, 1.0, 1.0, 1.0},
42  {"a1", 23.39, 33.07, 1.0, 1.0, 1.0, 1.0},
43  {"a0", 33.07, 46.77, 1.0, 1.0, 1.0, 1.0},
44  {"us-legal", 8.5, 14.0, 1.0, 1.0, 1.0, 1.0},
45  {"us-letter", 8.5, 11.0, 1.0, 1.0, 1.0, 1.0},
46  {"us-tabloid", 11.0, 17.0, 1.0, 1.0, 1.0, 1.0},
47  {NULL, 0, 0, 0, 0, 0, 0}};
48 
49 static void write_prolog(void)
50 {
51  char prolog_file[GPATH_MAX];
52  char date_str[256];
53  FILE *prolog_fp;
54  time_t t = time(NULL);
55  struct tm *tm = localtime(&t);
56 
57  strftime(date_str, sizeof(date_str), DATE_FORMAT, tm);
58 
59  sprintf(prolog_file, "%s/etc/psdriver.ps", G_gisbase());
60 
61  prolog_fp = fopen(prolog_file, "r");
62  if (!prolog_fp)
63  G_fatal_error("Unable to open prolog file");
64 
65  if (ps.encapsulated)
66  output("%%!PS-Adobe-3.0 EPSF-3.0\n");
67  else
68  output("%%!PS-Adobe-3.0\n");
69 
70  output("%%%%LanguageLevel: %d\n", 3);
71  output("%%%%Creator: GRASS PS Driver\n");
72  output("%%%%Title: %s\n", ps.outfile);
73  output("%%%%For: %s\n", G_whoami());
74  output("%%%%Orientation: %s\n", landscape ? "Landscape" : "Portrait");
75  output("%%%%BoundingBox: %d %d %d %d\n", (int)floor(ps.left),
76  (int)floor(ps.bot), (int)ceil(ps.right), (int)ceil(ps.top));
77  output("%%%%CreationDate: %s\n", date_str);
78  output("%%%%EndComments\n");
79 
80  output("%%%%BeginProlog\n");
81  while (!feof(prolog_fp)) {
82  char buf[256];
83 
84  if (!fgets(buf, sizeof(buf), prolog_fp))
85  break;
86 
87  fputs(buf, ps.tempfp);
88  }
89  output("%%%%EndProlog\n");
90 
91  fclose(prolog_fp);
92 }
93 
94 void write_setup(void)
95 {
96  output("%%%%BeginSetup\n");
97 
98  output("%.1f %.1f translate\n", ps.left, ps.bot);
99 
100  if (landscape)
101  output("90 rotate 0 1 -1 scale\n");
102  else
103  output("0 %.1f translate 1 -1 scale\n", height);
104 
105  output("%.1f %.1f BEGIN\n", width, height);
106 
107  output("%%%%EndSetup\n");
108  output("%%%%Page: 1 1\n");
109 }
110 
111 static double in2pt(double x)
112 {
113  return x * 72;
114 }
115 
116 static void swap(double *x, double *y)
117 {
118  double tmp = *x;
119 
120  *x = *y;
121  *y = tmp;
122 }
123 
124 static void get_paper(void)
125 {
126  const char *name = getenv("GRASS_RENDER_PS_PAPER");
127  const struct paper *paper;
128  int i;
129 
130  width = screen_width;
131  height = screen_height;
132 
133  ps.left = 0;
134  ps.right = width;
135  ps.bot = 0;
136  ps.top = height;
137 
138  if (landscape)
139  swap(&ps.right, &ps.top);
140 
141  if (!name)
142  return;
143 
144  for (i = 0;; i++) {
145  paper = &papers[i];
146 
147  if (!paper->name)
148  return;
149 
150  if (G_strcasecmp(name, paper->name) == 0)
151  break;
152  }
153 
154  ps.left = in2pt(paper->left);
155  ps.right = in2pt(paper->width) - in2pt(paper->right);
156  ps.bot = in2pt(paper->bot);
157  ps.top = in2pt(paper->height) - in2pt(paper->top);
158 
159  width = ps.right - ps.left;
160  height = in2pt(paper->height) - in2pt(paper->top) - in2pt(paper->bot);
161 
162  if (landscape)
163  swap(&width, &height);
164 
165  ps.right = ps.left + width;
166  ps.bot = ps.top + height;
167 }
168 
169 int PS_Graph_set(void)
170 {
171  const char *p;
172 
173  G_gisinit("PS driver");
174 
175  p = getenv("GRASS_RENDER_FILE");
176  if (!p || strlen(p) == 0)
177  p = FILE_NAME;
178 
179  ps.outfile = p;
180  p = ps.outfile + strlen(ps.outfile) - 4;
181  ps.encapsulated = (G_strcasecmp(p, ".eps") == 0);
182 
183  p = getenv("GRASS_RENDER_TRUECOLOR");
184  ps.true_color = p && strcmp(p, "TRUE") == 0;
185 
186  p = getenv("GRASS_RENDER_PS_LANDSCAPE");
187  landscape = p && strcmp(p, "TRUE") == 0;
188 
189  p = getenv("GRASS_RENDER_PS_HEADER");
190  ps.no_header = p && strcmp(p, "FALSE") == 0;
191 
192  p = getenv("GRASS_RENDER_PS_TRAILER");
193  ps.no_trailer = p && strcmp(p, "FALSE") == 0;
194 
195  G_verbose_message(_("ps: truecolor status %s"),
196  ps.true_color ? _("enabled") : _("disabled"));
197 
198  get_paper();
199 
200  ps.tempfile = G_tempfile();
201  if (ps.no_header && access(ps.outfile, F_OK) == 0)
203 
204  ps.tempfp = fopen(ps.tempfile, ps.no_header ? "a" : "w");
205 
206  if (!ps.tempfp)
207  G_fatal_error("Unable to open output file: %s", ps.outfile);
208 
209  if (!ps.no_header) {
210  write_prolog();
211  write_setup();
212  }
213 
214  G_verbose_message(_("ps: collecting to file '%s'"), ps.outfile);
215  G_verbose_message(_("ps: image size %dx%d"), screen_width, screen_height);
216 
217  fflush(ps.tempfp);
218 
219  return 0;
220 }
221 
222 /*!
223  \brief Get render file
224 
225  \return file name
226  */
227 const char *PS_Graph_get_file(void)
228 {
229  return ps.outfile;
230 }
231 
232 void output(const char *fmt, ...)
233 {
234  va_list va;
235 
236  va_start(va, fmt);
237  vfprintf(ps.tempfp, fmt, va);
238  va_end(va);
239 }
#define NULL
Definition: ccmath.h:32
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int G_rename_file(const char *, const char *)
Rename a file or a directory in the filesystem.
Definition: rename.c:31
void void G_verbose_message(const char *,...) __attribute__((format(printf
const char * G_whoami(void)
Gets user's name.
Definition: gis/whoami.c:35
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:62
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition: gisbase.c:39
int screen_height
Definition: driver/init.c:30
int screen_width
Definition: driver/init.c:29
#define G_gisinit(pgm)
Definition: gis.h:72
#define GPATH_MAX
Definition: gis.h:194
#define _(str)
Definition: glocale.h:10
#define FILE_NAME
Definition: htmlmap.h:8
const char * name
Definition: named_colr.c:6
struct ps_state ps
int PS_Graph_set(void)
const char * PS_Graph_get_file(void)
Get render file.
void output(const char *fmt,...)
#define DATE_FORMAT
void write_setup(void)
double t
Definition: r_raster.c:39
FILE * tempfp
Definition: psdriver.h:13
double left
Definition: psdriver.h:17
int no_trailer
Definition: psdriver.h:16
int no_header
Definition: psdriver.h:16
int true_color
Definition: psdriver.h:14
double top
Definition: psdriver.h:17
int encapsulated
Definition: psdriver.h:15
double bot
Definition: psdriver.h:17
const char * outfile
Definition: psdriver.h:12
const char * tempfile
Definition: psdriver.h:12
double right
Definition: psdriver.h:17
#define x