r_raster.c 8.5 KB

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