GRASS 8 Programmer's Manual 8.6.0dev(2026)-6050dcdd58
Loading...
Searching...
No Matches
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
31extern const struct driver *PNG_Driver(void);
32extern const struct driver *PS_Driver(void);
33extern const struct driver *HTML_Driver(void);
34#ifdef USE_CAIRO
35extern const struct driver *Cairo_Driver(void);
36#endif
37
38static struct {
39 double t, b, l, r;
40} screen, frame;
41
42static 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)
54
55 if (line_width)
57
58 if (text_size) {
59 double s = atof(text_size);
60 D_text_size(s, s);
61 }
62
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*/
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
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 snprintf(progname, sizeof(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*/
164{
165 const char *cmd = getenv("GRASS_NOTIFY");
166
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*/
178int D_save_command(const char *cmd UNUSED)
179{
180 return 0;
181}
182
183/*!
184 \brief Erase display (internal use only)
185*/
186void 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*/
197void 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*/
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*/
220void 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*/
232void D_font(const char *name)
233{
235}
236
237/*!
238 \brief Set encoding
239
240 \param name encoding name
241*/
242void 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] count of items in the list
252*/
253void 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] count of items in the list
263*/
264void 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 */
280void D_get_clip_window(double *t, double *b, double *l, double *r)
281{
282 COM_Get_window(t, b, l, r);
283}
284
285/*!
286 * \brief set graphical clipping window
287 *
288 * Sets the graphical clipping window to the specified rectangle
289 * (origin is top right)
290 *
291 * \param t top edge of clip window
292 * \param b bottom edge of clip window
293 * \param l left edge of clip window
294 * \param r right edge of clip window
295 * \return ~
296 */
297void D_set_clip_window(double t, double b, double l, double r)
298{
299 if (t < frame.t)
300 t = frame.t;
301 if (b > frame.b)
302 b = frame.b;
303 if (l < frame.l)
304 l = frame.l;
305 if (r > frame.r)
306 r = frame.r;
307
308 COM_Set_window(t, b, l, r);
309}
310
311/*!
312 * \brief get graphical window (frame)
313 *
314 * Queries the graphical frame (origin is top right)
315 *
316 * \param[out] t top edge of frame
317 * \param[out] b bottom edge of frame
318 * \param[out] l left edge of frame
319 * \param[out] r right edge of frame
320 * \return ~
321 */
322void D_get_frame(double *t, double *b, double *l, double *r)
323{
324 *t = frame.t;
325 *b = frame.b;
326 *l = frame.l;
327 *r = frame.r;
328}
329
330/*!
331 * \brief get screen bounds
332 *
333 * Queries the screen bounds (origin is top right)
334 *
335 * \param[out] t top edge of screen
336 * \param[out] b bottom edge of screen
337 * \param[out] l left edge of screen
338 * \param[out] r right edge of screen
339 * \return ~
340 */
341void D_get_screen(double *t, double *b, double *l, double *r)
342{
343 *t = screen.t;
344 *b = screen.b;
345 *l = screen.l;
346 *r = screen.r;
347}
348
349/*!
350 * \brief set graphical clipping window to map window
351 *
352 * Sets the graphical clipping window to the pixel window that corresponds
353 * to the current database region.
354 *
355 * \param ~
356 * \return ~
357 */
363
364/*!
365 * \brief set clipping window to screen window
366 *
367 * Sets the clipping window to the pixel window that corresponds to the
368 * full screen window. Off screen rendering is still clipped.
369 *
370 * \param ~
371 * \return ~
372 */
374{
375 COM_Set_window(frame.t, frame.b, frame.l, frame.r);
376}
void init(double work[])
Definition as177.c:61
const struct driver * Cairo_Driver(void)
Initialize display driver.
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
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
const char * G_getenv_nofatal(const char *)
Get environment variable.
Definition env.c:405
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
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:148
void void G_verbose_message(const char *,...) __attribute__((format(printf
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:61
char * G_recreate_command(void)
Creates command to run non-interactive.
Definition parser.c:840
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_mapset(void)
Get current mapset name.
Definition gis/mapset.c:33
int G_spawn_ex(const char *command,...)
Spawn new process based on command.
Definition spawn.c:899
void COM_Get_window(double *, double *, double *, double *)
void COM_Graph_close(void)
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:199
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:46
#define _(str)
Definition glocale.h:10
int count
const char * name
Definition named_colr.c:6
const struct driver * PNG_Driver(void)
Initialize display driver.
int D_open_driver(void)
Open display driver.
Definition r_raster.c:82
double b
Definition r_raster.c:39
const struct driver * HTML_Driver(void)
void D_set_clip_window(double t, double b, double l, double r)
set graphical clipping window
Definition r_raster.c:297
void D_set_clip_window_to_screen_window(void)
set clipping window to screen window
Definition r_raster.c:373
void D_set_clip_window_to_map_window(void)
set graphical clipping window to map window
Definition r_raster.c:358
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:280
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
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:322
int D_save_command(const char *cmd)
Append command to the cmd file (unused)
Definition r_raster.c:178
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
const struct driver * PS_Driver(void)
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
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:341
Definition manage.h:4