r_raster.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_LINE_WIDTH");
  33. const char *text_size = getenv("GRASS_TEXT_SIZE");
  34. const char *frame = getenv("GRASS_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. setenv(token[0], token[1], 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. \todo Replace strncmp by G_strncasecmp()
  75. \return 0 on success
  76. \return 1 no monitor defined
  77. */
  78. int D_open_driver(void)
  79. {
  80. const char *p, *m;
  81. G_debug(1, "D_open_driver():");
  82. p = getenv("GRASS_RENDER_IMMEDIATE");
  83. m = G__getenv("MONITOR");
  84. if (m) {
  85. char *env;
  86. const char *v;
  87. if (p)
  88. G_warning(_("%s variable defined, %s ignored"),
  89. "MONITOR", "GRASS_RENDER_IMMEDIATE");
  90. env = NULL;
  91. G_asprintf(&env, "MONITOR_%s_MAPFILE", m);
  92. v = G__getenv(env);
  93. if (strncmp(m, "wx", 2) == 0)
  94. p = NULL; /* use default display driver */
  95. else
  96. p = m;
  97. if (v) {
  98. if (p && G_strcasecmp(p, "ps") == 0)
  99. setenv("GRASS_PSFILE", v, 1);
  100. else
  101. setenv("GRASS_PNGFILE", v, 1);
  102. }
  103. G_asprintf(&env, "MONITOR_%s_ENVFILE", m);
  104. v = G__getenv(env);
  105. if (v)
  106. read_env_file(v);
  107. }
  108. const struct driver *drv =
  109. (p && G_strcasecmp(p, "PNG") == 0) ? PNG_Driver() :
  110. (p && G_strcasecmp(p, "PS") == 0) ? PS_Driver() :
  111. (p && G_strcasecmp(p, "HTML") == 0) ? HTML_Driver() :
  112. #ifdef USE_CAIRO
  113. (p && G_strcasecmp(p, "cairo") == 0) ? Cairo_Driver() :
  114. Cairo_Driver();
  115. #else
  116. PNG_Driver();
  117. #endif
  118. if (p && G_strcasecmp(drv->name, p) != 0)
  119. G_warning(_("Unknown display driver <%s>"), p);
  120. G_verbose_message(_("Using display driver <%s>..."), drv->name);
  121. LIB_init(drv);
  122. init();
  123. if (!getenv("GRASS_RENDER_IMMEDIATE") && !m)
  124. return 1;
  125. return 0;
  126. }
  127. /*!
  128. \brief Close display driver
  129. If GRASS_NOTIFY is defined, run notifier.
  130. */
  131. void D_close_driver(void)
  132. {
  133. const char *cmd = getenv("GRASS_NOTIFY");
  134. COM_Graph_close();
  135. if (cmd)
  136. system(cmd);
  137. }
  138. /*!
  139. \brief Append command to the cmd file
  140. Cmd file is created by d.mon by defining GRASS variable
  141. MONITOR_<name>_CMDFILE, where <name> is name of the monitor.
  142. Command string is usually generated by G_recreate_command(), NULL is
  143. used to clean up list of commands (see d.erase command).
  144. \param cmd string buffer with command or NULL
  145. \return 0 no monitor selected
  146. \return -1 on error
  147. \return 1 on success
  148. */
  149. int D_save_command(const char *cmd)
  150. {
  151. const char *mon_name, *mon_cmd;
  152. char *env, *flag;
  153. FILE *fd;
  154. G_debug(1, "D_save_command(): %s", cmd);
  155. mon_name = G__getenv("MONITOR");
  156. if (!mon_name)
  157. return 0;
  158. env = NULL;
  159. G_asprintf(&env, "MONITOR_%s_CMDFILE", mon_name);
  160. mon_cmd = G__getenv(env);
  161. if (!mon_cmd)
  162. return 0;
  163. if (cmd)
  164. flag = "a";
  165. else
  166. flag = "w";
  167. fd = fopen(mon_cmd, flag);
  168. if (!fd) {
  169. G_warning(_("Unable to open file '%s'"), mon_cmd);
  170. return -1;
  171. }
  172. if (cmd)
  173. fprintf(fd, "%s\n", cmd);
  174. fclose(fd);
  175. return 1;
  176. }
  177. /*!
  178. \brief Erase display (internal use only)
  179. */
  180. void D__erase(void)
  181. {
  182. COM_Erase();
  183. }
  184. /*!
  185. \brief Set text size (width and height)
  186. \param width text pixel width
  187. \param height text pixel height
  188. */
  189. void D_text_size(double width, double height)
  190. {
  191. COM_Text_size(width, height);
  192. }
  193. /*!
  194. \brief Set text rotation
  195. \param rotation value
  196. */
  197. void D_text_rotation(double rotation)
  198. {
  199. COM_Text_rotation(rotation);
  200. }
  201. /*!
  202. \brief Get clipping frame
  203. \param[out] t top
  204. \param[out] b bottom
  205. \param[out] l left
  206. \param[out] r right
  207. */
  208. void D_get_window(double *t, double *b, double *l, double *r)
  209. {
  210. return COM_Get_window(t, b, l, r);
  211. }
  212. /*!
  213. \brief Draw text
  214. Writes <em>text</em> in the current color and font, at the current text
  215. width and height, starting at the current screen location.
  216. \param text text to be drawn
  217. */
  218. void D_text(const char *text)
  219. {
  220. COM_Text(text);
  221. }
  222. /*!
  223. \brief Choose font
  224. Set current font to <em>font name</em>.
  225. \param name font name
  226. */
  227. void D_font(const char *name)
  228. {
  229. COM_Set_font(name);
  230. }
  231. /*!
  232. \brief Set encoding
  233. \param name encoding name
  234. */
  235. void D_encoding(const char *name)
  236. {
  237. COM_Set_encoding(name);
  238. }
  239. /*!
  240. \brief Get font list
  241. \param[out] list list of font names
  242. \param[out] number of items in the list
  243. */
  244. void D_font_list(char ***list, int *count)
  245. {
  246. COM_Font_list(list, count);
  247. }
  248. /*!
  249. \brief Get font info
  250. \param[out] list list of font info
  251. \param[out] number of items in the list
  252. */
  253. void D_font_info(char ***list, int *count)
  254. {
  255. COM_Font_info(list, count);
  256. }