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