r_raster.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 void init(void)
  29. {
  30. const char *fenc = getenv("GRASS_ENCODING");
  31. const char *font = getenv("GRASS_FONT");
  32. const char *line_width = getenv("GRASS_RENDER_LINE_WIDTH");
  33. const char *text_size = getenv("GRASS_RENDER_TEXT_SIZE");
  34. const char *frame = getenv("GRASS_RENDER_FRAME");
  35. D_font(font ? font : "romans");
  36. if (fenc)
  37. D_encoding(fenc);
  38. if (line_width)
  39. COM_Line_width(atof(line_width));
  40. if (text_size) {
  41. double s = atof(text_size);
  42. D_text_size(s, s);
  43. }
  44. D_text_rotation(0);
  45. if (frame) {
  46. double t, b, l, r;
  47. sscanf(frame, "%lf,%lf,%lf,%lf", &t, &b, &l, &r);
  48. COM_Set_window(t, b, l, r);
  49. }
  50. }
  51. int read_env_file(const char *path)
  52. {
  53. FILE *fd;
  54. char buf[1024];
  55. char **token;
  56. fd = fopen(path, "r");
  57. if (!fd)
  58. return -1;
  59. token = NULL;
  60. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  61. token = G_tokenize(buf, "=");
  62. if (G_number_of_tokens(token) != 2)
  63. continue;
  64. G_debug(3, "\tread_env_file(): %s=%s", token[0], token[1]);
  65. G_putenv(token[0], token[1]);
  66. G_free_tokens(token);
  67. token = NULL;
  68. }
  69. return 0;
  70. }
  71. /*!
  72. \brief Open display driver
  73. Default display driver is Cairo, if not available PNG is used.
  74. \return 0 on success
  75. \return 1 no monitor defined
  76. */
  77. int D_open_driver(void)
  78. {
  79. const char *p, *m;
  80. G_debug(1, "D_open_driver():");
  81. p = getenv("GRASS_RENDER_IMMEDIATE");
  82. m = G__getenv("MONITOR");
  83. if (m && G_strncasecmp(m, "wx", 2) == 0) {
  84. /* wx monitors always use GRASS_RENDER_IMMEDIATE. */
  85. p = NULL; /* use default display driver */
  86. } else if (m) {
  87. char *env;
  88. const char *v;
  89. char *u_m;
  90. if (p)
  91. G_warning(_("%s variable defined, %s ignored"),
  92. "MONITOR", "GRASS_RENDER_IMMEDIATE");
  93. /* GRASS variable names should be upper case. */
  94. u_m = G_store_upper(m);
  95. env = NULL;
  96. G_asprintf(&env, "MONITOR_%s_MAPFILE", u_m);
  97. v = G__getenv(env);
  98. p = m;
  99. if (v)
  100. G_putenv("GRASS_RENDER_FILE", v);
  101. G_asprintf(&env, "MONITOR_%s_ENVFILE", u_m);
  102. v = G__getenv(env);
  103. if (v)
  104. read_env_file(v);
  105. }
  106. const struct driver *drv =
  107. (p && G_strcasecmp(p, "png") == 0) ? PNG_Driver() :
  108. (p && G_strcasecmp(p, "ps") == 0) ? PS_Driver() :
  109. (p && G_strcasecmp(p, "html") == 0) ? HTML_Driver() :
  110. #ifdef USE_CAIRO
  111. (p && G_strcasecmp(p, "cairo") == 0) ? Cairo_Driver() :
  112. Cairo_Driver();
  113. #else
  114. PNG_Driver();
  115. #endif
  116. if (p && G_strcasecmp(drv->name, p) != 0)
  117. G_warning(_("Unknown display driver <%s>"), p);
  118. G_verbose_message(_("Using display driver <%s>..."), drv->name);
  119. LIB_init(drv);
  120. init();
  121. if (!getenv("GRASS_RENDER_IMMEDIATE") && !m)
  122. return 1;
  123. return 0;
  124. }
  125. /*!
  126. \brief Close display driver
  127. If GRASS_NOTIFY is defined, run notifier.
  128. */
  129. void D_close_driver(void)
  130. {
  131. const char *cmd = getenv("GRASS_NOTIFY");
  132. COM_Graph_close();
  133. if (cmd)
  134. system(cmd);
  135. }
  136. /*!
  137. \brief Append command to the cmd file
  138. Cmd file is created by d.mon by defining GRASS variable
  139. \c MONITOR_<name>_CMDFILE, where \c \<name\> is the upper case name of
  140. the monitor.
  141. Command string is usually generated by G_recreate_command(), NULL is
  142. used to clean up list of commands (see d.erase command).
  143. \param cmd string buffer with command or NULL
  144. \return 0 no monitor selected
  145. \return -1 on error
  146. \return 1 on success
  147. */
  148. int D_save_command(const char *cmd)
  149. {
  150. const char *mon_name, *mon_cmd;
  151. char *env, *flag, *u_mon_name;
  152. FILE *fd;
  153. G_debug(1, "D_save_command(): %s", cmd);
  154. mon_name = G__getenv("MONITOR");
  155. if (!mon_name || /* if no monitor selected */
  156. /* or wx monitor selected and display commands called by the monitor */
  157. (G_strncasecmp(mon_name, "wx", 2) == 0 &&
  158. getenv("GRASS_RENDER_IMMEDIATE")))
  159. return 0;
  160. /* GRASS variable names should be upper case. */
  161. u_mon_name = G_store_upper(mon_name);
  162. env = NULL;
  163. G_asprintf(&env, "MONITOR_%s_CMDFILE", u_mon_name);
  164. mon_cmd = G__getenv(env);
  165. if (!mon_cmd)
  166. return 0;
  167. if (cmd)
  168. flag = "a";
  169. else
  170. flag = "w";
  171. fd = fopen(mon_cmd, flag);
  172. if (!fd) {
  173. G_warning(_("Unable to open file '%s'"), mon_cmd);
  174. return -1;
  175. }
  176. if (cmd)
  177. fprintf(fd, "%s\n", cmd);
  178. fclose(fd);
  179. return 1;
  180. }
  181. /*!
  182. \brief Erase display (internal use only)
  183. */
  184. void D__erase(void)
  185. {
  186. COM_Erase();
  187. }
  188. /*!
  189. \brief Set text size (width and height)
  190. \param width text pixel width
  191. \param height text pixel height
  192. */
  193. void D_text_size(double width, double height)
  194. {
  195. COM_Text_size(width, height);
  196. }
  197. /*!
  198. \brief Set text rotation
  199. \param rotation value
  200. */
  201. void D_text_rotation(double rotation)
  202. {
  203. COM_Text_rotation(rotation);
  204. }
  205. /*!
  206. \brief Get clipping frame
  207. \param[out] t top
  208. \param[out] b bottom
  209. \param[out] l left
  210. \param[out] r right
  211. */
  212. void D_get_window(double *t, double *b, double *l, double *r)
  213. {
  214. return COM_Get_window(t, b, l, r);
  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. }