r_raster.c 8.7 KB

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