r_raster.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*!
  2. \file lib/display/r_raster.c
  3. \brief Display Library - Raster graphics subroutines
  4. (C) 2001-2014 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. \author Monitors support by Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <grass/config.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <grass/gis.h>
  18. #include <grass/glocale.h>
  19. #include <grass/display.h>
  20. #include "driver.h"
  21. extern const struct driver *PNG_Driver(void);
  22. extern const struct driver *PS_Driver(void);
  23. extern const struct driver *HTML_Driver(void);
  24. #ifdef USE_CAIRO
  25. extern const struct driver *Cairo_Driver(void);
  26. #endif
  27. static int read_env_file(const char *);
  28. static struct {
  29. double t, b, l, r;
  30. } screen, frame;
  31. static void init(void)
  32. {
  33. const char *fenc = getenv("GRASS_ENCODING");
  34. const char *font = getenv("GRASS_FONT");
  35. const char *line_width = getenv("GRASS_RENDER_LINE_WIDTH");
  36. const char *text_size = getenv("GRASS_RENDER_TEXT_SIZE");
  37. const char *frame_str = getenv("GRASS_RENDER_FRAME");
  38. D_font(font ? font : "romans");
  39. if (fenc)
  40. D_encoding(fenc);
  41. if (line_width)
  42. COM_Line_width(atof(line_width));
  43. if (text_size) {
  44. double s = atof(text_size);
  45. D_text_size(s, s);
  46. }
  47. D_text_rotation(0);
  48. COM_Get_window(&screen.t, &screen.b, &screen.l, &screen.r);
  49. if (frame_str) {
  50. sscanf(frame_str, "%lf,%lf,%lf,%lf", &frame.t, &frame.b, &frame.l, &frame.r);
  51. COM_Set_window(frame.t, frame.b, frame.l, frame.r);
  52. }
  53. else
  54. frame = screen;
  55. }
  56. int read_env_file(const char *path)
  57. {
  58. FILE *fd;
  59. char buf[1024];
  60. char **token;
  61. G_debug(1, "read_env_file(): %s", path);
  62. fd = fopen(path, "r");
  63. if (!fd)
  64. return -1;
  65. token = NULL;
  66. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  67. token = G_tokenize(buf, "=");
  68. if (G_number_of_tokens(token) != 2)
  69. continue;
  70. G_debug(3, "\tread_env_file(): %s=%s", token[0], token[1]);
  71. G_putenv(token[0], token[1]);
  72. G_free_tokens(token);
  73. token = NULL;
  74. }
  75. return 0;
  76. }
  77. /*!
  78. \brief Open display driver
  79. Default display driver is Cairo, if not available PNG is used.
  80. \return 0 on success
  81. \return 1 no monitor defined
  82. */
  83. int D_open_driver(void)
  84. {
  85. const char *p, *m;
  86. const struct driver *drv;
  87. G_debug(1, "D_open_driver():");
  88. p = getenv("GRASS_RENDER_IMMEDIATE");
  89. m = G_getenv_nofatal("MONITOR");
  90. if (m && G_strncasecmp(m, "wx", 2) == 0) {
  91. /* wx monitors always use GRASS_RENDER_IMMEDIATE. */
  92. p = NULL; /* use default display driver */
  93. }
  94. else if (m) {
  95. char *env;
  96. const char *v;
  97. char *u_m;
  98. if (p)
  99. G_warning(_("%s variable defined, %s ignored"),
  100. "MONITOR", "GRASS_RENDER_IMMEDIATE");
  101. /* GRASS variable names should be upper case. */
  102. u_m = G_store_upper(m);
  103. env = NULL;
  104. G_asprintf(&env, "MONITOR_%s_MAPFILE", u_m);
  105. v = G_getenv_nofatal(env);
  106. p = m;
  107. if (v)
  108. G_putenv("GRASS_RENDER_FILE", v);
  109. G_asprintf(&env, "MONITOR_%s_ENVFILE", u_m);
  110. v = G_getenv_nofatal(env);
  111. if (v)
  112. read_env_file(v);
  113. }
  114. else if (!p)
  115. G_fatal_error(_("Neither %s (managed by d.mon command) nor %s (used for direct rendering) defined"),
  116. "MONITOR", "GRASS_RENDER_IMMEDIATE");
  117. if (p && G_strcasecmp(p, "default") == 0)
  118. p = NULL;
  119. drv =
  120. (p && G_strcasecmp(p, "png") == 0) ? PNG_Driver() :
  121. (p && G_strcasecmp(p, "ps") == 0) ? PS_Driver() :
  122. (p && G_strcasecmp(p, "html") == 0) ? HTML_Driver() :
  123. #ifdef USE_CAIRO
  124. (p && G_strcasecmp(p, "cairo") == 0) ? Cairo_Driver() :
  125. Cairo_Driver();
  126. #else
  127. PNG_Driver();
  128. #endif
  129. if (p && G_strcasecmp(drv->name, p) != 0)
  130. G_warning(_("Unknown display driver <%s>"), p);
  131. G_verbose_message(_("Using display driver <%s>..."), drv->name);
  132. LIB_init(drv);
  133. init();
  134. return 0;
  135. }
  136. /*!
  137. \brief Close display driver
  138. If GRASS_NOTIFY is defined, run notifier.
  139. */
  140. void D_close_driver(void)
  141. {
  142. const char *cmd = getenv("GRASS_NOTIFY");
  143. COM_Graph_close();
  144. if (cmd)
  145. system(cmd);
  146. }
  147. /*!
  148. \brief Append command to the cmd file
  149. Cmd file is created by d.mon by defining GRASS variable
  150. \c MONITOR_<name>_CMDFILE, where \c \<name\> is the upper case name of
  151. the monitor.
  152. Command string is usually generated by G_recreate_command(), NULL is
  153. used to clean up list of commands (see d.erase command).
  154. \param cmd string buffer with command or NULL
  155. \return 0 no monitor selected
  156. \return -1 on error
  157. \return 1 on success
  158. */
  159. int D_save_command(const char *cmd)
  160. {
  161. const char *mon_name, *mon_cmd;
  162. char *env, *flag, *u_mon_name;
  163. FILE *fd;
  164. G_debug(1, "D_save_command(): %s", cmd);
  165. mon_name = G_getenv_nofatal("MONITOR");
  166. if (!mon_name || /* if no monitor selected */
  167. /* or wx monitor selected and display commands called by the monitor */
  168. (G_strncasecmp(mon_name, "wx", 2) == 0 &&
  169. getenv("GRASS_RENDER_IMMEDIATE")))
  170. return 0;
  171. /* GRASS variable names should be upper case. */
  172. u_mon_name = G_store_upper(mon_name);
  173. env = NULL;
  174. G_asprintf(&env, "MONITOR_%s_CMDFILE", u_mon_name);
  175. mon_cmd = G_getenv_nofatal(env);
  176. if (!mon_cmd)
  177. return 0;
  178. if (cmd)
  179. flag = "a";
  180. else
  181. flag = "w";
  182. fd = fopen(mon_cmd, flag);
  183. if (!fd) {
  184. G_warning(_("Unable to open file '%s'"), mon_cmd);
  185. return -1;
  186. }
  187. if (cmd)
  188. fprintf(fd, "%s\n", cmd);
  189. fclose(fd);
  190. return 1;
  191. }
  192. /*!
  193. \brief Erase display (internal use only)
  194. */
  195. void D__erase(void)
  196. {
  197. COM_Erase();
  198. }
  199. /*!
  200. \brief Set text size (width and height)
  201. \param width text pixel width
  202. \param height text pixel height
  203. */
  204. void D_text_size(double width, double height)
  205. {
  206. COM_Text_size(width, height);
  207. }
  208. /*!
  209. \brief Set text rotation
  210. \param rotation value
  211. */
  212. void D_text_rotation(double rotation)
  213. {
  214. COM_Text_rotation(rotation);
  215. }
  216. /*!
  217. \brief Draw text
  218. Writes <em>text</em> in the current color and font, at the current text
  219. width and height, starting at the current screen location.
  220. \param text text to be drawn
  221. */
  222. void D_text(const char *text)
  223. {
  224. COM_Text(text);
  225. }
  226. /*!
  227. \brief Choose font
  228. Set current font to <em>font name</em>.
  229. \param name font name
  230. */
  231. void D_font(const char *name)
  232. {
  233. COM_Set_font(name);
  234. }
  235. /*!
  236. \brief Set encoding
  237. \param name encoding name
  238. */
  239. void D_encoding(const char *name)
  240. {
  241. COM_Set_encoding(name);
  242. }
  243. /*!
  244. \brief Get font list
  245. \param[out] list list of font names
  246. \param[out] number of items in the list
  247. */
  248. void D_font_list(char ***list, int *count)
  249. {
  250. COM_Font_list(list, count);
  251. }
  252. /*!
  253. \brief Get font info
  254. \param[out] list list of font info
  255. \param[out] number of items in the list
  256. */
  257. void D_font_info(char ***list, int *count)
  258. {
  259. COM_Font_info(list, count);
  260. }
  261. /*!
  262. * \brief get graphical clipping window
  263. *
  264. * Queries the graphical clipping window (origin is top right)
  265. *
  266. * \param[out] t top edge of clip window
  267. * \param[out] b bottom edge of clip window
  268. * \param[out] l left edge of clip window
  269. * \param[out] r right edge of clip window
  270. * \return ~
  271. */
  272. void D_get_clip_window(double *t, double *b, double *l, double *r)
  273. {
  274. COM_Get_window(t, b, l, r);
  275. }
  276. /*!
  277. * \brief set graphical clipping window
  278. *
  279. * Sets the graphical clipping window to the specified rectangle
  280. * (origin is top right)
  281. *
  282. * \param t top edge of clip window
  283. * \param b bottom edge of clip window
  284. * \param l left edge of clip window
  285. * \param r right edge of clip window
  286. * \return ~
  287. */
  288. void D_set_clip_window(double t, double b, double l, double r)
  289. {
  290. if (t < frame.t) t = frame.t;
  291. if (b > frame.b) b = frame.b;
  292. if (l < frame.l) l = frame.l;
  293. if (r > frame.r) r = frame.r;
  294. COM_Set_window(t, b, l, r);
  295. }
  296. /*!
  297. * \brief get graphical window (frame)
  298. *
  299. * Queries the graphical frame (origin is top right)
  300. *
  301. * \param[out] t top edge of frame
  302. * \param[out] b bottom edge of frame
  303. * \param[out] l left edge of frame
  304. * \param[out] r right edge of frame
  305. * \return ~
  306. */
  307. void D_get_frame(double *t, double *b, double *l, double *r)
  308. {
  309. *t = frame.t;
  310. *b = frame.b;
  311. *l = frame.l;
  312. *r = frame.r;
  313. }
  314. /*!
  315. * \brief get screen bounds
  316. *
  317. * Queries the screen bounds (origin is top right)
  318. *
  319. * \param[out] t top edge of screen
  320. * \param[out] b bottom edge of screen
  321. * \param[out] l left edge of screen
  322. * \param[out] r right edge of screen
  323. * \return ~
  324. */
  325. void D_get_screen(double *t, double *b, double *l, double *r)
  326. {
  327. *t = screen.t;
  328. *b = screen.b;
  329. *l = screen.l;
  330. *r = screen.r;
  331. }
  332. /*!
  333. * \brief set graphical clipping window to map window
  334. *
  335. * Sets the graphical clipping window to the pixel window that corresponds
  336. * to the current database region.
  337. *
  338. * \param ~
  339. * \return ~
  340. */
  341. void D_set_clip_window_to_map_window(void)
  342. {
  343. D_set_clip_window(
  344. D_get_d_north(), D_get_d_south(),
  345. D_get_d_west(), D_get_d_east());
  346. }
  347. /*!
  348. * \brief set clipping window to screen window
  349. *
  350. * Sets the clipping window to the pixel window that corresponds to the
  351. * full screen window. Off screen rendering is still clipped.
  352. *
  353. * \param ~
  354. * \return ~
  355. */
  356. void D_set_clip_window_to_screen_window(void)
  357. {
  358. COM_Set_window(frame.t, frame.b, frame.l, frame.r);
  359. }