r_raster.c 8.5 KB

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