r_raster.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. */
  9. #include <grass/config.h>
  10. #include <errno.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <grass/gis.h>
  17. #include <grass/glocale.h>
  18. #include <grass/display.h>
  19. #include "driver.h"
  20. extern const struct driver *PNG_Driver(void);
  21. extern const struct driver *PS_Driver(void);
  22. extern const struct driver *HTML_Driver(void);
  23. #ifdef USE_CAIRO
  24. extern const struct driver *Cairo_Driver(void);
  25. #endif
  26. static int read_env_file(const char *);
  27. static void init(void)
  28. {
  29. const char *fenc = getenv("GRASS_ENCODING");
  30. const char *font = getenv("GRASS_FONT");
  31. const char *line_width = getenv("GRASS_LINE_WIDTH");
  32. const char *text_size = getenv("GRASS_TEXT_SIZE");
  33. const char *frame = getenv("GRASS_FRAME");
  34. D_font(font ? font : "romans");
  35. if (fenc)
  36. D_encoding(fenc);
  37. if (line_width)
  38. COM_Line_width(atof(line_width));
  39. if (text_size) {
  40. double s = atof(text_size);
  41. D_text_size(s, s);
  42. }
  43. D_text_rotation(0);
  44. if (frame) {
  45. double t, b, l, r;
  46. sscanf(frame, "%lf,%lf,%lf,%lf", &t, &b, &l, &r);
  47. COM_Set_window(t, b, l, r);
  48. }
  49. }
  50. int read_env_file(const char *path)
  51. {
  52. FILE *fd;
  53. char buf[1024];
  54. char **token;
  55. fd = fopen(path, "r");
  56. if (!fd)
  57. return -1;
  58. token = NULL;
  59. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  60. token = G_tokenize(buf, "=");
  61. if (G_number_of_tokens(token) != 2)
  62. continue;
  63. setenv(token[0], token[1], 1);
  64. G_free_tokens(token);
  65. token = NULL;
  66. }
  67. return 0;
  68. }
  69. /*!
  70. \brief Open display driver
  71. Default display driver is Cairo, if not available PNG is used.
  72. \todo Replace strncmp by G_strncasecmp()
  73. \return 0 on success
  74. \return 1 no monitor defined
  75. */
  76. int D_open_driver(void)
  77. {
  78. const char *p, *m;
  79. p = getenv("GRASS_RENDER_IMMEDIATE");
  80. m = G__getenv("MONITOR");
  81. if (m) {
  82. char *env;
  83. const char *v;
  84. if (p)
  85. G_warning(_("%s variable defined, %s ignored"),
  86. "MONITOR", "GRASS_RENDER_IMMEDIATE");
  87. env = NULL;
  88. G_asprintf(&env, "MONITOR_%s_MAPFILE", m);
  89. v = G__getenv(env);
  90. if (strncmp(m, "wx", 2) == 0)
  91. p = NULL; /* use default display driver */
  92. else
  93. p = m;
  94. if (v) {
  95. if (p && G_strcasecmp(p, "ps") == 0)
  96. setenv("GRASS_PSFILE", v, 1);
  97. else
  98. setenv("GRASS_PNGFILE", v, 1);
  99. }
  100. G_asprintf(&env, "MONITOR_%s_ENVFILE", m);
  101. v = G__getenv(env);
  102. if (v)
  103. read_env_file(v);
  104. }
  105. const struct driver *drv =
  106. (p && G_strcasecmp(p, "PNG") == 0) ? PNG_Driver() :
  107. (p && G_strcasecmp(p, "PS") == 0) ? PS_Driver() :
  108. (p && G_strcasecmp(p, "HTML") == 0) ? HTML_Driver() :
  109. #ifdef USE_CAIRO
  110. (p && G_strcasecmp(p, "cairo") == 0) ? Cairo_Driver() :
  111. Cairo_Driver();
  112. #else
  113. PNG_Driver();
  114. #endif
  115. if (p && G_strcasecmp(drv->name, p) != 0)
  116. G_warning(_("Unknown display driver <%s>"), p);
  117. G_verbose_message(_("Using display driver <%s>..."), drv->name);
  118. LIB_init(drv);
  119. init();
  120. if (!m)
  121. return 1;
  122. return 0;
  123. }
  124. /*!
  125. \brief Close display driver
  126. If GRASS_NOTIFY is defined, run notifier.
  127. */
  128. void D_close_driver(void)
  129. {
  130. const char *cmd = getenv("GRASS_NOTIFY");
  131. COM_Graph_close();
  132. if (cmd)
  133. system(cmd);
  134. }
  135. int D_save_command(const char *cmd)
  136. {
  137. const char *mon_name, *mon_cmd;
  138. char *env;
  139. FILE *fd;
  140. G_debug(1, "D_save_command(): %s", cmd);
  141. mon_name = G__getenv("MONITOR");
  142. if (!mon_name)
  143. return 0;
  144. env = NULL;
  145. G_asprintf(&env, "MONITOR_%s_CMDFILE", mon_name);
  146. mon_cmd = G__getenv(env);
  147. if (!mon_cmd)
  148. return 0;
  149. fd = fopen(mon_cmd, "a");
  150. if (!fd) {
  151. G_warning(_("Unable to open file '%s'"), mon_cmd);
  152. return -1;
  153. }
  154. fprintf(fd, "%s\n", cmd);
  155. fclose(fd);
  156. return 1;
  157. }
  158. /*!
  159. \brief Erase display (internal use only)
  160. */
  161. void D__erase(void)
  162. {
  163. COM_Erase();
  164. }
  165. /*!
  166. \brief Set text size (width and height)
  167. \param width text pixel width
  168. \param height text pixel height
  169. */
  170. void D_text_size(double width, double height)
  171. {
  172. COM_Text_size(width, height);
  173. }
  174. /*!
  175. \brief Set text rotation
  176. \param rotation value
  177. */
  178. void D_text_rotation(double rotation)
  179. {
  180. COM_Text_rotation(rotation);
  181. }
  182. /*!
  183. \brief Get clipping frame
  184. \param[out] t top
  185. \param[out] b bottom
  186. \param[out] l left
  187. \param[out] r right
  188. */
  189. void D_get_window(double *t, double *b, double *l, double *r)
  190. {
  191. return COM_Get_window(t, b, l, r);
  192. }
  193. /*!
  194. \brief Draw text
  195. Writes <em>text</em> in the current color and font, at the current text
  196. width and height, starting at the current screen location.
  197. \param text text to be drawn
  198. */
  199. void D_text(const char *text)
  200. {
  201. COM_Text(text);
  202. }
  203. /*!
  204. \brief Choose font
  205. Set current font to <em>font name</em>.
  206. \param name font name
  207. */
  208. void D_font(const char *name)
  209. {
  210. COM_Set_font(name);
  211. }
  212. /*!
  213. \brief Set encoding
  214. \param name encoding name
  215. */
  216. void D_encoding(const char *name)
  217. {
  218. COM_Set_encoding(name);
  219. }
  220. /*!
  221. \brief Get font list
  222. \param[out] list list of font names
  223. \param[out] number of items in the list
  224. */
  225. void D_font_list(char ***list, int *count)
  226. {
  227. COM_Font_list(list, count);
  228. }
  229. /*!
  230. \brief Get font info
  231. \param[out] list list of font info
  232. \param[out] number of items in the list
  233. */
  234. void D_font_info(char ***list, int *count)
  235. {
  236. COM_Font_info(list, count);
  237. }