GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
r_raster.c
Go to the documentation of this file.
1 /*!
2  \file lib/display/r_raster.c
3 
4  \brief Display Library - Raster graphics subroutines
5 
6  (C) 2001-2015 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Original author CERL
12  \author Monitors support by Martin Landa <landa.martin gmail.com>
13 */
14 
15 #include <grass/config.h>
16 
17 #include <errno.h>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 
24 #include <grass/gis.h>
25 #include <grass/glocale.h>
26 #include <grass/display.h>
27 #include <grass/spawn.h>
28 
29 #include "driver.h"
30 
31 extern const struct driver *PNG_Driver(void);
32 extern const struct driver *PS_Driver(void);
33 extern const struct driver *HTML_Driver(void);
34 #ifdef USE_CAIRO
35 extern const struct driver *Cairo_Driver(void);
36 #endif
37 
38 static struct {
39  double t, b, l, r;
40 } screen, frame;
41 
42 static void init(void)
43 {
44  const char *fenc = getenv("GRASS_ENCODING");
45  const char *font = getenv("GRASS_FONT");
46  const char *line_width = getenv("GRASS_RENDER_LINE_WIDTH");
47  const char *text_size = getenv("GRASS_RENDER_TEXT_SIZE");
48  const char *frame_str = getenv("GRASS_RENDER_FRAME");
49 
50  D_font(font ? font : "romans");
51 
52  if (fenc)
53  D_encoding(fenc);
54 
55  if (line_width)
56  COM_Line_width(atof(line_width));
57 
58  if (text_size) {
59  double s = atof(text_size);
60  D_text_size(s, s);
61  }
62 
63  D_text_rotation(0);
64 
65  COM_Get_window(&screen.t, &screen.b, &screen.l, &screen.r);
66  if (frame_str) {
67  sscanf(frame_str, "%lf,%lf,%lf,%lf", &frame.t, &frame.b, &frame.l,
68  &frame.r);
69  COM_Set_window(frame.t, frame.b, frame.l, frame.r);
70  }
71  else
72  frame = screen;
73 }
74 
75 /*!
76  \brief Open display driver
77 
78  Default display driver is Cairo, if not available PNG is used.
79 
80  \return 0 on success
81 */
82 int D_open_driver(void)
83 {
84  const char *p, *c, *m;
85  const struct driver *drv;
86 
87  G_debug(1, "D_open_driver():");
88  p = getenv("GRASS_RENDER_IMMEDIATE");
89  c = getenv("GRASS_RENDER_COMMAND");
90  m = G_getenv_nofatal("MONITOR");
91 
92  if (!p && (m || c)) {
93  char *cmd;
94  char progname[GPATH_MAX];
95 
96  cmd = G_recreate_command();
97 
98  if (c && m) {
99  G_warning(_("Both %s and %s are defined. "
100  "%s will be ignored."),
101  "GRASS_RENDER_COMMAND", "MONITOR", "MONITOR");
102  m = NULL;
103  }
104 
105  if (c)
106  sprintf(progname, "%s", c);
107  else { /* monitors managed by d.mon -> call default renderer */
108  char element[GPATH_MAX];
109 
111  strcat(element, "/");
112  strcat(element, "MONITORS");
113  strcat(element, "/");
114  strcat(element, m);
115  G_file_name(progname, element, "render.py", G_mapset());
116  }
117 
118  G_debug(1, "rendering redirected to %s", progname);
119  /* assuming Python script here (could be extended in the future) */
120  G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
121  cmd, NULL);
122 
123  G_free(cmd);
124 
125  /* force exiting GRASS command, leave rendering on
126  * GRASS_RENDER_COMMAND program */
127  exit(0);
128  }
129 
130  if (!p)
131  G_fatal_error(_("Neither %s (managed by d.mon command) nor %s "
132  "(used for direct rendering) defined"),
133  "MONITOR", "GRASS_RENDER_IMMEDIATE");
134 
135  if (p && G_strcasecmp(p, "default") == 0)
136  p = NULL;
137 
138  drv = (p && G_strcasecmp(p, "png") == 0) ? PNG_Driver()
139  : (p && G_strcasecmp(p, "ps") == 0) ? PS_Driver()
140  : (p && G_strcasecmp(p, "html") == 0) ? HTML_Driver()
141  :
142 #ifdef USE_CAIRO
143  Cairo_Driver();
144 #else
145  PNG_Driver();
146 #endif
147 
148  if (p && G_strcasecmp(drv->name, p) != 0)
149  G_warning(_("Unknown display driver <%s>"), p);
150  G_verbose_message(_("Using display driver <%s>..."), drv->name);
151  LIB_init(drv);
152 
153  init();
154 
155  return 0;
156 }
157 
158 /*!
159  \brief Close display driver
160 
161  If GRASS_NOTIFY is defined, run notifier.
162 */
163 void D_close_driver(void)
164 {
165  const char *cmd = getenv("GRASS_NOTIFY");
166 
167  COM_Graph_close();
168 
169  if (cmd && system(cmd) == -1)
170  G_warning(_("GRASS_NOTIFY command <%s> failed"), cmd);
171 }
172 
173 /*!
174  \brief Append command to the cmd file (unused)
175 
176  \todo To be removed
177 */
178 int D_save_command(const char *cmd UNUSED)
179 {
180  return 0;
181 }
182 
183 /*!
184  \brief Erase display (internal use only)
185 */
186 void D__erase(void)
187 {
188  COM_Erase();
189 }
190 
191 /*!
192  \brief Set text size (width and height)
193 
194  \param width text pixel width
195  \param height text pixel height
196 */
197 void D_text_size(double width, double height)
198 {
199  COM_Text_size(width, height);
200 }
201 
202 /*!
203  \brief Set text rotation
204 
205  \param rotation value
206 */
207 void D_text_rotation(double rotation)
208 {
209  COM_Text_rotation(rotation);
210 }
211 
212 /*!
213  \brief Draw text
214 
215  Writes <em>text</em> in the current color and font, at the current text
216  width and height, starting at the current screen location.
217 
218  \param text text to be drawn
219 */
220 void D_text(const char *text)
221 {
222  COM_Text(text);
223 }
224 
225 /*!
226  \brief Choose font
227 
228  Set current font to <em>font name</em>.
229 
230  \param name font name
231 */
232 void D_font(const char *name)
233 {
235 }
236 
237 /*!
238  \brief Set encoding
239 
240  \param name encoding name
241 */
242 void D_encoding(const char *name)
243 {
245 }
246 
247 /*!
248  \brief Get font list
249 
250  \param[out] list list of font names
251  \param[out] number of items in the list
252 */
253 void D_font_list(char ***list, int *count)
254 {
256 }
257 
258 /*!
259  \brief Get font info
260 
261  \param[out] list list of font info
262  \param[out] number of items in the list
263 */
264 void D_font_info(char ***list, int *count)
265 {
267 }
268 
269 /*!
270  * \brief get graphical clipping window
271  *
272  * Queries the graphical clipping window (origin is top right)
273  *
274  * \param[out] t top edge of clip window
275  * \param[out] b bottom edge of clip window
276  * \param[out] l left edge of clip window
277  * \param[out] r right edge of clip window
278  * \return ~
279  */
280 
281 void D_get_clip_window(double *t, double *b, double *l, double *r)
282 {
283  COM_Get_window(t, b, l, r);
284 }
285 
286 /*!
287  * \brief set graphical clipping window
288  *
289  * Sets the graphical clipping window to the specified rectangle
290  * (origin is top right)
291  *
292  * \param t top edge of clip window
293  * \param b bottom edge of clip window
294  * \param l left edge of clip window
295  * \param r right edge of clip window
296  * \return ~
297  */
298 
299 void D_set_clip_window(double t, double b, double l, double r)
300 {
301  if (t < frame.t)
302  t = frame.t;
303  if (b > frame.b)
304  b = frame.b;
305  if (l < frame.l)
306  l = frame.l;
307  if (r > frame.r)
308  r = frame.r;
309 
310  COM_Set_window(t, b, l, r);
311 }
312 
313 /*!
314  * \brief get graphical window (frame)
315  *
316  * Queries the graphical frame (origin is top right)
317  *
318  * \param[out] t top edge of frame
319  * \param[out] b bottom edge of frame
320  * \param[out] l left edge of frame
321  * \param[out] r right edge of frame
322  * \return ~
323  */
324 
325 void D_get_frame(double *t, double *b, double *l, double *r)
326 {
327  *t = frame.t;
328  *b = frame.b;
329  *l = frame.l;
330  *r = frame.r;
331 }
332 
333 /*!
334  * \brief get screen bounds
335  *
336  * Queries the screen bounds (origin is top right)
337  *
338  * \param[out] t top edge of screen
339  * \param[out] b bottom edge of screen
340  * \param[out] l left edge of screen
341  * \param[out] r right edge of screen
342  * \return ~
343  */
344 
345 void D_get_screen(double *t, double *b, double *l, double *r)
346 {
347  *t = screen.t;
348  *b = screen.b;
349  *l = screen.l;
350  *r = screen.r;
351 }
352 
353 /*!
354  * \brief set graphical clipping window to map window
355  *
356  * Sets the graphical clipping window to the pixel window that corresponds
357  * to the current database region.
358  *
359  * \param ~
360  * \return ~
361  */
362 
364 {
366  D_get_d_east());
367 }
368 
369 /*!
370  * \brief set clipping window to screen window
371  *
372  * Sets the clipping window to the pixel window that corresponds to the
373  * full screen window. Off screen rendering is still clipped.
374  *
375  * \param ~
376  * \return ~
377  */
378 
380 {
381  COM_Set_window(frame.t, frame.b, frame.l, frame.r);
382 }
void init(double work[])
Definition: as177.c:61
const struct driver * Cairo_Driver(void)
Initialize display driver.
#define NULL
Definition: ccmath.h:32
double D_get_d_south(void)
Definition: cnversions.c:264
double D_get_d_west(void)
Definition: cnversions.c:252
double D_get_d_north(void)
Definition: cnversions.c:260
double D_get_d_east(void)
Definition: cnversions.c:256
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_temp_element(char *)
Populates element with a path string.
Definition: tempfile.c:147
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition: file_name.c:61
void void G_verbose_message(const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
char * G_recreate_command(void)
Creates command to run non-interactive.
Definition: parser.c:838
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_getenv_nofatal(const char *)
Get environment variable.
Definition: env.c:405
int G_spawn_ex(const char *command,...)
Spawn new process based on command.
Definition: spawn.c:897
void COM_Get_window(double *, double *, double *, double *)
void COM_Graph_close(void)
Definition: driver/graph.c:12
void COM_Text_size(double, double)
Definition: text_size.c:5
void COM_Text_rotation(double)
Definition: text_size.c:12
void COM_Text(const char *)
Definition: driver/text.c:4
void COM_Line_width(double)
void COM_Set_font(const char *)
Definition: font.c:84
void COM_Set_window(double, double, double, double)
void COM_Font_info(char ***, int *)
Definition: font.c:154
void LIB_init(const struct driver *drv)
Initialize display driver.
Definition: driver/init.c:47
void COM_Erase(void)
Definition: driver/erase.c:4
void COM_Set_encoding(const char *)
Definition: font.c:139
void COM_Font_list(char ***, int *)
Definition: font.c:147
#define GPATH_MAX
Definition: gis.h:194
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
#define _(str)
Definition: glocale.h:10
int count
const char * name
Definition: named_colr.c:6
const struct driver * HTML_Driver(void)
int D_open_driver(void)
Open display driver.
Definition: r_raster.c:82
double b
Definition: r_raster.c:39
const struct driver * PNG_Driver(void)
Initialize display driver.
void D_set_clip_window(double t, double b, double l, double r)
set graphical clipping window
Definition: r_raster.c:299
void D_set_clip_window_to_screen_window(void)
set clipping window to screen window
Definition: r_raster.c:379
void D_set_clip_window_to_map_window(void)
set graphical clipping window to map window
Definition: r_raster.c:363
void D__erase(void)
Erase display (internal use only)
Definition: r_raster.c:186
double l
Definition: r_raster.c:39
void D_get_clip_window(double *t, double *b, double *l, double *r)
get graphical clipping window
Definition: r_raster.c:281
void D_text_size(double width, double height)
Set text size (width and height)
Definition: r_raster.c:197
void D_text_rotation(double rotation)
Set text rotation.
Definition: r_raster.c:207
void D_encoding(const char *name)
Set encoding.
Definition: r_raster.c:242
int D_save_command(const char *cmd UNUSED)
Append command to the cmd file (unused)
Definition: r_raster.c:178
double t
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
void D_get_frame(double *t, double *b, double *l, double *r)
get graphical window (frame)
Definition: r_raster.c:325
void D_close_driver(void)
Close display driver.
Definition: r_raster.c:163
void D_font_info(char ***list, int *count)
Get font info.
Definition: r_raster.c:264
void D_font_list(char ***list, int *count)
Get font list.
Definition: r_raster.c:253
void D_text(const char *text)
Draw text.
Definition: r_raster.c:220
const struct driver * PS_Driver(void)
void D_font(const char *name)
Choose font.
Definition: r_raster.c:232
void D_get_screen(double *t, double *b, double *l, double *r)
get screen bounds
Definition: r_raster.c:345
Definition: driver.h:21
char * name
Definition: driver.h:22
Definition: lidar.h:85
Definition: manage.h:4